summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/mpeg/source/mpeg_server/lvs.cpp
blob: 96390dbc218f934a490cf6e4fbae9cbac352cb04 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* $Id$ */
/* Copyright (c) 1995 Oregon Graduate Institute of Science and Technology
 * P.O.Box 91000-1000, Portland, OR 97291, USA;
 * 
 * Permission to use, copy, modify, distribute, and sell this software and its 
 * documentation for any purpose is hereby granted without fee, provided that 
 * the above copyright notice appear in all copies and that both that 
 * copyright notice and this permission notice appear in supporting 
 * documentation, and that the name of O.G.I. not be used in advertising or 
 * publicity pertaining to distribution of the software without specific, 
 * written prior permission.  O.G.I. makes no representations about the 
 * suitability of this software for any purpose.  It is provided "as is" 
 * without express or implied warranty.
 * 
 * O.G.I. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 
 * O.G.I. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Shanwei Cen
 *         Department of Computer Science and Engineering
 *         email: scen@cse.ogi.edu
 */

#include <stdio.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>

#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Intrinsic.h>

#include "include/common.h" 
#include "mpeg_shared/routine.h"
#include "server_proto.h"

#ifdef LIVE_VIDEO
#include "lvs_shared.h"

LvsSharedData * shared = NULL;
static int lvspid = -1;  /* -1: no lvs, 0 - lvs child,  >0 - vs proc */
static int prefid = -1, first_fid, first_frame;
static int curbuf;

#define PID_FILE "~/.vcr/lvsPid"
#define QUALITY_FILE "~/.vcr/lvsQuality"

static int count = 0;
static unsigned session_time;

static int curpid = 0;

static void on_exit_routine(void)
{
  char buf[256];
  if (getpid() != curpid) return;
  get_full_path(PID_FILE, buf, 256);
  unlink(buf);
}

static void register_pid(void)
{
  char buf[256];
  FILE *fp;
  get_full_path(PID_FILE, buf, 256);
  fp = fopen(buf, "w");
  fprintf(fp, "%d -- pid of current VS process\n", getpid());
  fclose(fp);
}

static void set_quality_level(void)
{
  char buf[256];
  FILE *fp;
  int qos = -1;
  get_full_path(QUALITY_FILE, buf, 256);
  fp = fopen(buf, "r");
  if (fp == NULL) return;
  
  fscanf(fp, "%d", &qos);
  fclose(fp);
  if (qos >= 0 && qos <= 100) {
    shared->qos_val = qos;
  }
}

static void hup_handler(int status)
{
  setsignal(SIGHUP, SIG_IGN);
  if (getpid() != curpid) return;
  set_quality_level();
  setsignal(SIGHUP, hup_handler);
}

#endif

int InitLiveVideo(int argc, char ** argv)  /* -1 -- failed, 0 - succ */
{
#ifndef LIVE_VIDEO
  return -1;
#else
/*
#define BUFSIZE 768 * 576 * 3 / 8
#define BUFSIZE 640 * 480 * 3 / 2
*/
#define BUFSIZE 1024 * 1024
  
  char * buf = creat_shared_mem(BUFSIZE);
  int sid = creat_semaphore();
  int susid = creat_semaphore();

  enter_cs(susid);
  
  shared = (LvsSharedData *)buf;
  shared->sid = sid;
  shared->susid = susid;
  shared->initState = 0;
  shared->bufDataBytes = BUFSIZE - sizeof(*shared);
  shared->buf = (LvsBuffer *)((char *)shared + sizeof(*shared));

  register_pid();
  set_quality_level();
  
  /* all other fields to be initialized by LVS process */

  while ((lvspid = fork()) == -1);

  if (lvspid == 0) { /* child process, reading from camera */
    LiveVideoProcess(argc, argv);
    exit(1);
  }

  while (shared->initState == 0) usleep(100000);  /* wait for SunVideo to init */

  if (shared->initState == -1) {
    fprintf(stderr, "LVS error: init of child process failed\n");
    kill(lvspid, SIGINT);
    remove_shared_mem(buf);
    remove_semaphore(sid);
    remove_semaphore(susid);
    lvspid = -1;
    return -1;
  }
  else {
    
    fprintf(stderr, "LVS initialized.\n");

    curpid = getpid();
    
    atexit(on_exit_routine);

    setsignal(SIGHUP, hup_handler);
  
    return 0;
  }
#endif
}

void LeaveLiveVideo(void)
{
#ifndef LIVE_VIDEO
  return;
#else
  if (lvspid == -1) return;
  remove_shared_mem((char *)shared);
  shared = NULL;
#endif
}

void ExitLiveVideo(void)
{
#ifndef LIVE_VIDEO
  return;
#else
  if (lvspid == -1) return;
  kill(lvspid, SIGINT);
  remove_semaphore(shared->sid);
  remove_semaphore(shared->susid);
  remove_shared_mem((char *)shared);
  lvspid = -1;
  shared = NULL;
#endif
}

int OpenLiveVideo(int * format,
		  int * width, int * height,
		  int * averageFrameSize,
		  double * fps,
		  int * pelAspectRatio  /* MPEG1 encoding used here */
		  )                     /* -1 -- failed, 0 - succ */
{
#ifndef LIVE_VIDEO
  return -1;
#else
  if (lvspid == -1) return -1;
  *format = shared->format;
  *width = shared->width;
  *height = shared->height;
  *averageFrameSize = shared->averageFrameSize;
  *fps = shared->fps;
  *pelAspectRatio = shared->pelAspectRatio;
  
  fprintf(stderr, "LVS session %d opened.\n", getpid());
  
  return 0;
#endif
}

void StartPlayLiveVideo(void)
{
#ifndef LIVE_VIDEO
  return;
#else
  if (lvspid == -1) return;

  count = 0;
  session_time = get_msec();
  
  enter_cs(shared->sid);
  
  prefid = -1;
  first_fid = -1;
  first_frame = -1;
  
  if (shared->activeSessions <= 0) shared->activeSessions = 1;
  else shared->activeSessions ++;
  
  leave_cs(shared->sid);

  while (shared->curbuf <= shared->wait_curbuf) {
    /*
    fprintf(stderr, "curbuf %d, wait_curbuf %d\n",
	    shared->curbuf, shared->wait_curbuf);
    */
    usleep(10000);
  }
  
  fprintf(stderr, "LVS session %d started\n", getpid());
  
  return;
#endif
}

void StopPlayLiveVideo(void)
{
#ifndef LIVE_VIDEO
  return;
#else
  if (lvspid == -1) return;
  enter_cs(shared->sid);
  shared->activeSessions --;
  leave_cs(shared->sid);
  
  fprintf(stderr, "LVS session %d stopped, at %5.2f fps.\n", getpid(),
	  ((double)count * 1000.0) / get_duration(session_time, get_msec()));
  
  return;
#endif
}

#if 1
int ReadLiveVideoPicture(int * frame, char * buf, int size) /* ret # bytes */
{
#ifndef LIVE_VIDEO
  return 0;
#else
  int pframe = * frame;
  int ssize;
  int bufptr;
  
  if (lvspid == -1) return 0;

  if (first_frame == -1) {  /* first frame in current session */
    curbuf = shared->curbuf - 1;
    bufptr = curbuf % shared->bufnum;
    enter_cs(shared->sid);
  }
  else {
    int bufnum = shared->bufnum < 3 ? shared->bufnum : 3;
    do {
      curbuf ++;
      if (curbuf <= shared->curbuf - bufnum) {
	/*
	fprintf(stderr, "LVS session %d skipped %d buffers\n",
		getpid(), shared->curbuf - bufnum + 1 - curbuf);
	*/
	curbuf = shared->curbuf - bufnum + 1;
      }
      bufptr = curbuf % shared->bufnum;
      while (shared->buf[bufptr].fid <= prefid) {
	/*
	fprintf(stderr,
		"LVS session %d wait: curbuf %d, shared->curbuf %d, prefid %d, shared->fid %d\n",
		getpid(), curbuf, shared->curbuf, prefid, shared->buf[bufptr].fid);
	*/
	enter_cs(shared->sid);
	shared->suscount ++;
	leave_cs(shared->sid);
	enter_cs(shared->susid);
      }
    } while (pframe > first_frame + (shared->buf[bufptr].fid - first_fid));
    
    enter_cs(shared->sid);
    /*
    fprintf(stderr, "pframe %d, aframe %d\n",
	    pframe, first_frame + (shared->buf[bufptr].fid - first_fid));
    */
  }
  shared->buf[bufptr].refcnt ++;
  leave_cs(shared->sid);
  
  ssize = shared->buf[bufptr].size;

  ssize = (ssize < size ? ssize : size);
  memcpy(buf, shared->buf[bufptr].data, ssize);

  enter_cs(shared->sid);

  shared->buf[bufptr].refcnt --;
  prefid = shared->buf[bufptr].fid;
  if (first_frame == -1) {
    first_fid = prefid;
    first_frame = * frame;
  }
  else {
    * frame = first_frame + (prefid - first_fid);
  }
  leave_cs(shared->sid);
  /*
  fprintf(stderr,
	  "LVS session %d read curbuf %d fid %d, frame %d size %d (ffid %d, fframe %d)\n",
	  getpid(), curbuf, prefid, *frame, ssize, first_fid, first_frame);
  */
  /*
  fprintf(stderr, "LVS read fid %d, frame %d\n", prefid, *frame);
  */
  count ++;
  return ssize;
#endif
}
#endif

#if 0
/* following version is for testing the speed of the vcr path, excluding the
   xil stuff */

static long start_time;

int ReadLiveVideoPicture(int * frame, char * buf, int size) /* ret # bytes */
{
#ifndef LIVE_VIDEO
  return 0;
#else
  int pframe = * frame;
  int ssize;
  char * sbuf;
  if (lvspid == -1) return 0;

 loop_begin:
  /*
  enter_cs(shared->sid);
  */
  if (first_frame == -1) {
    first_frame = *frame;
    first_fid = shared->fid;
    start_time = get_msec();
  }
  else {
    int f = first_frame + ((double)get_duration(start_time, get_msec()) /
			    ((double)1000.0 / (double)shared->fps));
    if (f < *frame) {
      
      leave_cs(shared->sid);
      /*
      fprintf(stderr, "f=%d < *frame=%d\n", f, *frame);
      */
      usleep(10000);
      goto loop_begin;
    }
    *frame = f;
  }
  
  prefid = shared->fid;
  sbuf = shared->buf[shared->curbuf].data;
  ssize = shared->buf[shared->curbuf].size;

  ssize = (ssize < size ? ssize : size);
  memcpy(buf, sbuf, ssize);
  /*
  leave_cs(shared->sid);
  */
  /*
  fprintf(stderr, "LVS read fid %d, frame %d size %d (ffid %d, fframe %d)\n",
	  shared->fid, *frame, ssize, first_fid, first_frame);
  */
  count ++;
  return ssize;
#endif
}
#endif