summaryrefslogtreecommitdiffstats
path: root/mpeglib_artsplug/mpeglibartsplay.cpp
blob: 66b69150a2bce09b2e17acd531300dc7e07bb0ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// vim:ts=2:sw=2:sts=2:et
/**
   Starter for plugins.
   The plugins are identified by their extension

   .wav ->WavPlayObject
   .mp3 ->MP3PlayObject
   .mpg ->MPGPlayObject
   .ogg ->OGGPlayObject
*/


#include "soundserver.h"
#include "kmedia2.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iostream>
#include <climits>

using namespace std;

#if defined(HAVE_GETOPT_H) 
#include <getopt.h>
#endif


string file1;
string file2;

Arts::Dispatcher* d=0;
Arts::SimpleSoundServer* server=0;



void usage() {
  cout << "mpeglibartsply command line tool for arts playobjects"<<endl;
  cout << "Usage : mpeglibartsply [url]"<<endl;
  cout << endl;
  cout << "-1 : create/destroy PlayObject test."<<endl;
  cout << "-2 : create/seek/play/destroy PlayObject test."<<endl;
  cout << "-3 : torture seek implementation test"<<endl;
  cout << "-h : help"<<endl;
  cout << endl;
  cout << "THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! " \
       << "USE AT YOUR OWN RISK!"<<endl;
  cout << endl;
  exit(0);
}


void initServer() {
  d=new Arts::Dispatcher();
  server= new Arts::SimpleSoundServer (Arts::Reference("global:Arts_SimpleSoundServer"));
  
  if(server->isNull()) {
    cerr << "Can't connect to sound server" << endl;
    exit(0);
  }

}


void destroyServer() {
  delete server;
  delete d;
}

/**
   create destroy playobjects without playing anything
*/
void doStress1() {
  int cnt=0;
  while (cnt < 10000) {
    Arts::PlayObject play=server->createPlayObject(file1);
    cout << "cnt:"<<cnt++<<endl;
  }
}

/**
   create playobjects playing some time, do seek play destroy
*/
void doStress2() {
  int cnt=0;
  while (cnt < 1000) {
    Arts::PlayObject play=server->createPlayObject(file1);
    play.play();
    sleep(3);
    Arts::poTime seekTime;
    seekTime.seconds=100;
    seekTime.ms=0;
    play.seek(seekTime);
    cout << "cnt:"<<cnt++<<endl;
  }
}

/**
   create playobject seek random play seek again
*/
void doStress3() {
  int cnt=0;
  Arts::PlayObject play=server->createPlayObject(file1);
  play.play();
  long secs=0;
  while(1) {
    cout << "waiting for length info.."<<endl;
    sleep(3);
    Arts::poTime length=play.overallTime();
    secs=length.seconds;
    if (secs > 0) break;
  }
  // do not jump near to end, the danger that
  // we get an eof is to high
  if (secs < 100) {
    cout << "file to short for test"<<endl;
    exit(-1);
  }
  secs-=10;
  Arts::poTime seekTime;
  Arts::poTime startTime;
  startTime.seconds=0;
  startTime.ms=0;

  while (cnt < 1000) {

    //
    // seek to a know position
    //
    play.seek(startTime);
    // now wait for seek completion we need a poll here
    while(1) {
      Arts::poTime current=play.currentTime();
      if (current.seconds < 5) {
        break;
      }
      usleep(50000);
    }

    //
    // now we know that the playObject is near the beginning
    //

    int randNo=rand();
    long seekPos=(long)(((float)randNo*(float)secs)/(float)RAND_MAX);
    // need to jump over the start area

    seekPos+=5;
    cout << "seek to :"<<seekPos
	 << " | cnt:"<<cnt++
	 << " of 1000"<<endl;
    seekTime.seconds=seekPos;
    seekTime.ms=0;
    play.seek(seekTime);
    // now wait for seek completion we need a poll here
    while(1) {
      Arts::poTime current=play.currentTime();
      if (current.seconds > seekPos) {
	break;
      }
      if (current.seconds == seekPos) {
	if (current.ms > 0) {
	  break;
	}
      }      
      usleep(50000);
    }
    

  }
  cout << "stresstest successfully passed."<<endl;
}


int main(int argc, char **argv) {
  int testNr=0;
  int c;
  initServer();

  while(1) { 
    c = getopt (argc, argv, "123456h");
    if (c == -1) break;
    switch(c) {
    case '1': {    
      testNr=1;
      break;
    }
    case '2': {
      testNr=2;
      break;
    }
    case '3': {
      testNr=3;
      break;
    }
    case '4': {
      testNr=4;
      break;
    }
    case '5': {
      testNr=5;
      break;
    }
    case '6': {
      testNr=5;
      break;
    }
    case 'h': {
      usage();
      break;
    }
    default:
      printf ("?? getopt returned character code 0%o ??\n", c);
      usage();
      exit(-1);
    }
  }

  if (optind >= argc ) {
    usage();
    exit(-1);
  }

  if (optind < argc ) {
    file1=argv[optind];
    optind++;
    file2=file1;
  }
  if (optind < argc ) {
    file2=argv[optind];
  }  


  switch(testNr) {
  case 1: {
    doStress1();
    break;
  }
  case 2: {
    doStress2();
    break;
  }
  case 3: {
    doStress3();
    break;
  }
  default:
    if (file1[0] != '/') {
      char buf[PATH_MAX+1];
      char *path = getcwd(buf, PATH_MAX - file1.length());
      if (path) {
        file1.insert(0, "/");
        file1.insert(0, path);
      }
    }
    Arts::PlayObject play=server->createPlayObject(file1);
    if (play.isNull()) {
      cout << "cannot play this"<<endl;
      destroyServer();
      exit(0);
    }
    play.play();
    while (play.state() != Arts::posIdle) {
      sleep(1);
    }
  }

  destroyServer();
}