summaryrefslogtreecommitdiff
path: root/sys/v4l/v4lmjpegsink_calls.c
blob: 416f80ea0bb22c7e9ce361263c40a1fab1def846 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* GStreamer
 *
 * v4lmjpegsink_calls.c: functions for hardware MJPEG video sink
 *
 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include "v4lmjpegsink_calls.h"

/* On some systems MAP_FAILED seems to be missing */
#ifndef MAP_FAILED
#define MAP_FAILED ( (caddr_t) -1 )
#endif

GST_DEBUG_CATEGORY_EXTERN (v4lmjpegsink_debug);
#define GST_CAT_DEFAULT v4lmjpegsink_debug

/******************************************************
 * gst_v4lmjpegsink_sync_thread()
 *   thread keeps track of played frames
 ******************************************************/

static void *
gst_v4lmjpegsink_sync_thread (void *arg)
{
  GstV4lMjpegSink *v4lmjpegsink = GST_V4LMJPEGSINK (arg);
  gint frame = 0;               /* frame that we're currently syncing on */

  GST_DEBUG_OBJECT (v4lmjpegsink, "starting sync thread");

#if 0
  /* Allow easy shutting down by other processes... */
  pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
  pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
#endif

  while (1) {
    g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
    if (!v4lmjpegsink->isqueued_queued_frames[frame]) {
      g_cond_wait (v4lmjpegsink->cond_queued_frames[frame],
          v4lmjpegsink->mutex_queued_frames);
    }
    if (v4lmjpegsink->isqueued_queued_frames[frame] != 1) {
      g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
      goto end;
    }
    g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);

    GST_DEBUG_OBJECT (v4lmjpegsink, "thread-syncing on next frame");
    if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_SYNC,
            &(v4lmjpegsink->bsync)) < 0) {
      GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, SYNC, (NULL),
          ("Failed to sync on frame %d: %s", frame, g_strerror (errno)));
      g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
      v4lmjpegsink->isqueued_queued_frames[frame] = -1;
      g_cond_broadcast (v4lmjpegsink->cond_queued_frames[frame]);
      g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
      goto end;
    } else {
      /* be sure that we're not confusing */
      if (frame != v4lmjpegsink->bsync.frame) {
        GST_ELEMENT_ERROR (v4lmjpegsink, CORE, TOO_LAZY, (NULL),
            ("Internal error: frame number confusion"));
        goto end;
      }
      g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
      v4lmjpegsink->isqueued_queued_frames[frame] = 0;
      g_cond_broadcast (v4lmjpegsink->cond_queued_frames[frame]);
      g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
    }

    frame = (frame + 1) % v4lmjpegsink->breq.count;
  }

end:
  GST_DEBUG_OBJECT (v4lmjpegsink, "Sync thread got signalled to exit");
  g_thread_exit (NULL);
  return NULL;
}


/******************************************************
 * gst_v4lmjpegsink_queue_frame()
 *   queue a frame for playback
 * return value: TRUE on success, FALSE on error
 ******************************************************/

static gboolean
gst_v4lmjpegsink_queue_frame (GstV4lMjpegSink * v4lmjpegsink, gint num)
{
  GST_DEBUG_OBJECT (v4lmjpegsink, "queueing frame %d", num);

  /* queue on this frame */
  if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_QBUF_PLAY,
          &num) < 0) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, WRITE, (NULL),
        ("Failed to queue frame %d: %s", num, g_strerror (errno)));
    return FALSE;
  }

  g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
  v4lmjpegsink->isqueued_queued_frames[num] = 1;
  g_cond_broadcast (v4lmjpegsink->cond_queued_frames[num]);
  g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_sync_frame()
 *   wait for a frame to be finished playing
 * return value: TRUE on success, FALSE on error
 ******************************************************/

static gboolean
gst_v4lmjpegsink_sync_frame (GstV4lMjpegSink * v4lmjpegsink, gint * num)
{
  GST_DEBUG_OBJECT (v4lmjpegsink, "syncing on next frame");

  /* calculate next frame */
  v4lmjpegsink->current_frame =
      (v4lmjpegsink->current_frame + 1) % v4lmjpegsink->breq.count;
  *num = v4lmjpegsink->current_frame;

  g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
  if (v4lmjpegsink->isqueued_queued_frames[*num] == 1) {
    g_cond_wait (v4lmjpegsink->cond_queued_frames[*num],
        v4lmjpegsink->mutex_queued_frames);
  }
  if (v4lmjpegsink->isqueued_queued_frames[*num] != 0) {
    g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
    return FALSE;
  } else
    g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_set_buffer()
 *   set buffer options
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_set_buffer (GstV4lMjpegSink * v4lmjpegsink,
    gint numbufs, gint bufsize)
{
  GST_DEBUG_OBJECT (v4lmjpegsink,
      "setting buffer info to numbufs = %d, bufsize = %d KB", numbufs, bufsize);
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));

  v4lmjpegsink->breq.size = bufsize * 1024;
  v4lmjpegsink->breq.count = numbufs;

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_set_playback()
 *   set playback options (video, interlacing, etc.)
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_set_playback (GstV4lMjpegSink * v4lmjpegsink,
    gint width,
    gint height, gint x_offset, gint y_offset, gint norm, gint interlacing)
{
  gint mw, mh;
  struct mjpeg_params bparm;

  GST_DEBUG_OBJECT (v4lmjpegsink,
      "setting size=%dx%d, X/Y offsets=%d/%d, norm=%d, interlacing=%d\n",
      width, height, x_offset, y_offset, norm, interlacing);
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  /*GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsink)); */

  if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_G_PARAMS,
          &bparm) < 0) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, SETTINGS, (NULL),
        GST_ERROR_SYSTEM);
    return FALSE;
  }

  bparm.input = 0;
  bparm.norm = norm;
  bparm.decimation = 0;         /* we'll set proper values later on */

  /* maxwidth is broken on marvel cards */
  mw = GST_V4LELEMENT (v4lmjpegsink)->vcap.maxwidth;
  if (mw != 768 && mw != 640)
    mw = 720;
  mh = (norm == VIDEO_MODE_NTSC ? 480 : 576);

  if (width > mw || height > mh) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
        ("Video dimensions (%dx%d) are larger than device max (%dx%d)",
            width, height, mw, mh));
    return FALSE;
  }

  if (width <= mw / 4)
    bparm.HorDcm = 4;
  else if (width <= mw / 2)
    bparm.HorDcm = 2;
  else
    bparm.HorDcm = 1;

  /* TODO: add proper interlacing handling */
#if 0
  if (interlacing != INTERLACING_NOT_INTERLACED) {
    bparm.field_per_buff = 2;
    bparm.TmpDcm = 1;

    if (height <= mh / 2)
      bparm.VerDcm = 2;
    else
      bparm.VerDcm = 1;
  } else
#endif
  {
    if (height > mh / 2) {
      GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
          ("Video dimensions (%dx%d) too large for non-interlaced playback (%dx%d)",
              width, height, mw, mh / 2));
      return FALSE;
    }

    bparm.field_per_buff = 1;
    bparm.TmpDcm = 2;

    if (height <= mh / 4)
      bparm.VerDcm = 2;
    else
      bparm.VerDcm = 1;
  }

  /* TODO: add proper interlacing handling */
#if 0
  bparm.odd_even = (interlacing == INTERLACING_TOP_FIRST);
#endif

  bparm.quality = 100;
  bparm.img_width = bparm.HorDcm * width;
  bparm.img_height = bparm.VerDcm * height / bparm.field_per_buff;

  /* image X/Y offset on device */
  if (x_offset < 0)
    bparm.img_x = (mw - bparm.img_width) / 2;
  else {
    if (x_offset + bparm.img_width > mw)
      bparm.img_x = mw - bparm.img_width;
    else
      bparm.img_x = x_offset;
  }

  if (y_offset < 0)
    bparm.img_y = (mh / 2 - bparm.img_height) / 2;
  else {
    if (y_offset + bparm.img_height * 2 > mh)
      bparm.img_y = mh / 2 - bparm.img_height;
    else
      bparm.img_y = y_offset / 2;
  }

  if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_S_PARAMS,
          &bparm) < 0) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, SETTINGS, (NULL),
        GST_ERROR_SYSTEM);
    return FALSE;
  }

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_playback_init()
 *   initialize playback system, set up buffer, etc.
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_playback_init (GstV4lMjpegSink * v4lmjpegsink)
{
  gint n;

  GST_DEBUG_OBJECT (v4lmjpegsink, "initting playback subsystem");
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));

  /* Request buffers */
  if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_REQBUFS,
          &(v4lmjpegsink->breq)) < 0) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
    return FALSE;
  }

  GST_INFO_OBJECT (v4lmjpegsink, "Got %ld buffers of size %ld KB",
      v4lmjpegsink->breq.count, v4lmjpegsink->breq.size / 1024);

  /* Map the buffers */
  GST_V4LELEMENT (v4lmjpegsink)->buffer = mmap (0,
      v4lmjpegsink->breq.count * v4lmjpegsink->breq.size,
      PROT_READ | PROT_WRITE, MAP_SHARED,
      GST_V4LELEMENT (v4lmjpegsink)->video_fd, 0);
  if (GST_V4LELEMENT (v4lmjpegsink)->buffer == MAP_FAILED) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
        ("Error mapping video buffers: %s", g_strerror (errno)));
    GST_V4LELEMENT (v4lmjpegsink)->buffer = NULL;
    return FALSE;
  }

  /* allocate/init the GThread thingies */
  v4lmjpegsink->mutex_queued_frames = g_mutex_new ();
  v4lmjpegsink->isqueued_queued_frames = (gint8 *)
      malloc (sizeof (gint8) * v4lmjpegsink->breq.count);
  if (!v4lmjpegsink->isqueued_queued_frames) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
        ("Failed to create queue tracker: %s", g_strerror (errno)));
    return FALSE;
  }
  v4lmjpegsink->cond_queued_frames = (GCond **)
      malloc (sizeof (GCond *) * v4lmjpegsink->breq.count);
  if (!v4lmjpegsink->cond_queued_frames) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
        ("Failed to create queue condition holders: %s", g_strerror (errno)));
    return FALSE;
  }
  for (n = 0; n < v4lmjpegsink->breq.count; n++)
    v4lmjpegsink->cond_queued_frames[n] = g_cond_new ();

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_playback_start()
 *   start playback system
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_playback_start (GstV4lMjpegSink * v4lmjpegsink)
{
  GError *error;
  gint n;

  GST_DEBUG_OBJECT (v4lmjpegsink, "starting playback");
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));

  /* mark all buffers as unqueued */
  for (n = 0; n < v4lmjpegsink->breq.count; n++)
    v4lmjpegsink->isqueued_queued_frames[n] = 0;

  v4lmjpegsink->current_frame = -1;

  /* create sync() thread */
  v4lmjpegsink->thread_queued_frames =
      g_thread_create (gst_v4lmjpegsink_sync_thread, (void *) v4lmjpegsink,
      TRUE, &error);
  if (!v4lmjpegsink->thread_queued_frames) {
    GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
        ("Failed to create sync thread: %s", error->message));
    return FALSE;
  }

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_get_buffer()
 *   get address of a buffer
 * return value: buffer's address or NULL
 ******************************************************/

guint8 *
gst_v4lmjpegsink_get_buffer (GstV4lMjpegSink * v4lmjpegsink, gint num)
{
  /*GST_DEBUG_OBJECT (v4lmjpegsink, gst_v4lmjpegsink_get_buffer(), num = %d", num); */

  if (!GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lmjpegsink)) ||
      !GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lmjpegsink)))
    return NULL;

  if (num < 0 || num >= v4lmjpegsink->breq.count)
    return NULL;

  return GST_V4LELEMENT (v4lmjpegsink)->buffer +
      (v4lmjpegsink->breq.size * num);
}


/******************************************************
 * gst_v4lmjpegsink_play_frame()
 *   queue a new buffer
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_play_frame (GstV4lMjpegSink * v4lmjpegsink, gint num)
{
  GST_DEBUG_OBJECT (v4lmjpegsink, "playing frame %d", num);
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));

  if (!gst_v4lmjpegsink_queue_frame (v4lmjpegsink, num))
    return FALSE;

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_wait_frame()
 *   wait for buffer to be actually played
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_wait_frame (GstV4lMjpegSink * v4lmjpegsink, gint * num)
{
  GST_DEBUG_OBJECT (v4lmjpegsink,
      "waiting for next frame to be finished playing");
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));

  if (!gst_v4lmjpegsink_sync_frame (v4lmjpegsink, num))
    return FALSE;

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_playback_stop()
 *   stop playback system and sync on remaining frames
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_playback_stop (GstV4lMjpegSink * v4lmjpegsink)
{
  gint num;

  GST_DEBUG_OBJECT (v4lmjpegsink, "stopping playback");
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));

  /* mark next buffer as wrong */
  if (!gst_v4lmjpegsink_sync_frame (v4lmjpegsink, &num) ||
      !gst_v4lmjpegsink_queue_frame (v4lmjpegsink, num)) {
    return FALSE;
  }

  /* .. and wait for all buffers to be queued on */
  g_thread_join (v4lmjpegsink->thread_queued_frames);

  return TRUE;
}


/******************************************************
 * gst_v4lmjpegsink_playback_deinit()
 *   deinitialize the playback system and unmap buffer
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4lmjpegsink_playback_deinit (GstV4lMjpegSink * v4lmjpegsink)
{
  int n;

  GST_DEBUG_OBJECT (v4lmjpegsink, "quitting playback subsystem");
  GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
  GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));

  /* free GThread thingies */
  g_mutex_free (v4lmjpegsink->mutex_queued_frames);
  for (n = 0; n < v4lmjpegsink->breq.count; n++)
    g_cond_free (v4lmjpegsink->cond_queued_frames[n]);
  free (v4lmjpegsink->cond_queued_frames);
  free (v4lmjpegsink->isqueued_queued_frames);

  /* unmap the buffer */
  munmap (GST_V4LELEMENT (v4lmjpegsink)->buffer,
      v4lmjpegsink->breq.size * v4lmjpegsink->breq.count);
  GST_V4LELEMENT (v4lmjpegsink)->buffer = NULL;

  return TRUE;
}