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
|
/* GStreamer
* Copyright (C) 2011 David Schleef <ds@entropywave.com>
*
* 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., 51 Franklin Street, Suite 500,
* Boston, MA 02110-1335, USA.
*/
/**
* SECTION:element-gstavcsrc
*
* The avcsrc element captures video from an OS/X AVC Video Services
* devices, typically a FireWire camera.
*
* <refsect2>
* <title>Example launch line</title>
* |[
* gst-launch -v avcsrc ! decodebin ! osxvideosink
* ]|
*
* This pipeline captures from an AVC source, decodes the stream (either
* DV or HDV), and displays the video.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <AVCVideoServices/AVCVideoServices.h>
using namespace AVS;
#include <gst/gst.h>
#include <gst/base/gstbasesrc.h>
#include "gstavcsrc.h"
GST_DEBUG_CATEGORY_STATIC (gst_avc_src_debug_category);
#define GST_CAT_DEFAULT gst_avc_src_debug_category
/* prototypes */
static void gst_avc_src_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_avc_src_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
static void gst_avc_src_dispose (GObject * object);
static void gst_avc_src_finalize (GObject * object);
static GstCaps *gst_avc_src_get_caps (GstBaseSrc * src);
static gboolean gst_avc_src_start (GstBaseSrc * src);
static gboolean gst_avc_src_stop (GstBaseSrc * src);
static gboolean gst_avc_src_is_seekable (GstBaseSrc * src);
static gboolean gst_avc_src_unlock (GstBaseSrc * src);
static gboolean gst_avc_src_event (GstBaseSrc * src, GstEvent * event);
static GstFlowReturn
gst_avc_src_create (GstBaseSrc * src, guint64 offset, guint size,
GstBuffer ** buf);
static gboolean gst_avc_src_query (GstBaseSrc * src, GstQuery * query);
static gboolean gst_avc_src_unlock_stop (GstBaseSrc * src);
enum
{
PROP_0
};
/* pad templates */
static GstStaticPadTemplate gst_avc_src_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS
("video/dv,systemstream=true;video/mpegts,systemstream=true,packetsize=188")
);
/* class initialization */
#define DEBUG_INIT(bla) \
GST_DEBUG_CATEGORY_INIT (gst_avc_src_debug_category, "avcsrc", 0, \
"debug category for avcsrc element");
GST_BOILERPLATE_FULL (GstAVCSrc, gst_avc_src, GstBaseSrc,
GST_TYPE_BASE_SRC, DEBUG_INIT);
static void
gst_avc_src_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_static_pad_template (element_class,
&gst_avc_src_src_template);
gst_element_class_set_details_simple (element_class,
"AVC Video Services Source", "Video/Source",
"Captures DV or HDV video from Firewire port",
"David Schleef <ds@entropywave.com>");
}
static void
gst_avc_src_class_init (GstAVCSrcClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstBaseSrcClass *base_src_class = GST_BASE_SRC_CLASS (klass);
gobject_class->set_property = gst_avc_src_set_property;
gobject_class->get_property = gst_avc_src_get_property;
gobject_class->dispose = gst_avc_src_dispose;
gobject_class->finalize = gst_avc_src_finalize;
base_src_class->get_caps = GST_DEBUG_FUNCPTR (gst_avc_src_get_caps);
base_src_class->start = GST_DEBUG_FUNCPTR (gst_avc_src_start);
base_src_class->stop = GST_DEBUG_FUNCPTR (gst_avc_src_stop);
base_src_class->is_seekable = GST_DEBUG_FUNCPTR (gst_avc_src_is_seekable);
base_src_class->unlock = GST_DEBUG_FUNCPTR (gst_avc_src_unlock);
base_src_class->event = GST_DEBUG_FUNCPTR (gst_avc_src_event);
base_src_class->create = GST_DEBUG_FUNCPTR (gst_avc_src_create);
if (0)
base_src_class->query = GST_DEBUG_FUNCPTR (gst_avc_src_query);
if (0)
base_src_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_avc_src_unlock_stop);
}
static void
gst_avc_src_init (GstAVCSrc * avcsrc, GstAVCSrcClass * avcsrc_class)
{
gst_base_src_set_live (GST_BASE_SRC (avcsrc), TRUE);
avcsrc->srcpad = gst_pad_new_from_static_template (&gst_avc_src_src_template,
"src");
avcsrc->queue = gst_atomic_queue_new (16);
avcsrc->cond = g_cond_new ();
avcsrc->queue_lock = g_mutex_new ();
}
void
gst_avc_src_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
/* GstAVCSrc *avcsrc = GST_AVC_SRC (object); */
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_avc_src_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
/* GstAVCSrc *avcsrc = GST_AVC_SRC (object); */
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_avc_src_dispose (GObject * object)
{
/* GstAVCSrc *avcsrc = GST_AVC_SRC (object); */
/* clean up as possible. may be called multiple times */
G_OBJECT_CLASS (parent_class)->dispose (object);
}
void
gst_avc_src_finalize (GObject * object)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (object);
/* clean up object here */
gst_atomic_queue_unref (avcsrc->queue);
g_cond_free (avcsrc->cond);
g_mutex_free (avcsrc->queue_lock);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static GstCaps *
gst_avc_src_get_caps (GstBaseSrc * src)
{
/* GstAVCSrc *avcsrc = GST_AVC_SRC (src); */
return gst_caps_from_string ("video/mpegts,systemstream=true,packetsize=188");
}
#define kNumCyclesInMPEGReceiverSegment 200
#define kNumSegmentsInMPEGReceiverProgram 10
void
MPEGReceiverMessageReceivedProc (UInt32 msg, UInt32 param1, UInt32 param2,
void *pRefCon)
{
}
IOReturn
MyStructuredDataPushProc (UInt32 CycleDataCount,
MPEGReceiveCycleData * pCycleData, void *pRefCon)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (pRefCon);
if (avcsrc) {
UInt32 numPackets = 0;
for (UInt32 cycle = 0; cycle < CycleDataCount; cycle++)
numPackets += pCycleData[cycle].tsPacketCount;
GstBuffer *buffer;
buffer = gst_buffer_new_and_alloc (numPackets*kMPEG2TSPacketSize);
guint8 *data = GST_BUFFER_DATA (buffer);
for (UInt32 cycle = 0; cycle < CycleDataCount; cycle++) {
GST_LOG("Received cycle %lu of %lu - %lu packets (fw time %lx)",
cycle, CycleDataCount, pCycleData[cycle].tsPacketCount,
pCycleData[cycle].fireWireTimeStamp);
for (UInt32 sourcePacket = 0; sourcePacket < pCycleData[cycle].tsPacketCount;
sourcePacket++) {
memcpy (data,
pCycleData[cycle].pBuf[sourcePacket], kMPEG2TSPacketSize);
data += kMPEG2TSPacketSize;
avcsrc->packets_enqueued++;
}
}
gst_atomic_queue_push (avcsrc->queue, buffer);
g_mutex_lock (avcsrc->queue_lock);
g_cond_signal (avcsrc->cond);
g_mutex_unlock (avcsrc->queue_lock);
}
return 0;
}
static gboolean
gst_avc_src_start (GstBaseSrc * src)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GST_DEBUG_OBJECT (avcsrc, "start");
avcsrc->unlock = FALSE;
// Create a AVCDeviceController
if (!avcsrc->pAVCDeviceController)
CreateAVCDeviceController (&avcsrc->pAVCDeviceController);
if (!avcsrc->pAVCDeviceController) {
// TODO: This should never happen (unless we've run out of memory), but we should handle it cleanly anyway
GST_ERROR ("Failed to create AVC device controller.");
return FALSE;
}
GST_INFO ("Created AVC device controller.");
if (avcsrc->deviceIndex >= CFArrayGetCount (avcsrc->pAVCDeviceController->avcDeviceArray)) {
GST_ERROR ("Failed to find AVC device %d", avcsrc->deviceIndex);
return FALSE;
}
avcsrc->pAVCDevice = (AVCDevice *)
CFArrayGetValueAtIndex (avcsrc->pAVCDeviceController->avcDeviceArray,
avcsrc->deviceIndex);
if (!avcsrc->pAVCDevice) {
GST_ERROR ("Failed to find AVC device %d", avcsrc->deviceIndex);
return FALSE;
}
GST_INFO ("Found device with GUID 0x%016llX\n", avcsrc->pAVCDevice->guid);
avcsrc->pAVCDevice->openDevice (nil, nil);
avcsrc->pAVCDeviceStream = avcsrc->pAVCDevice->CreateMPEGReceiverForDevicePlug (0, nil, // We'll install the structured callback later (MyStructuredDataPushProc),
nil,
MPEGReceiverMessageReceivedProc,
nil,
nil, kNumCyclesInMPEGReceiverSegment, kNumSegmentsInMPEGReceiverProgram);
avcsrc->pAVCDeviceStream->pMPEGReceiver->registerStructuredDataPushCallback
(MyStructuredDataPushProc,
kNumCyclesInMPEGReceiverSegment, (void *) avcsrc);
avcsrc->pAVCDevice->StartAVCDeviceStream (avcsrc->pAVCDeviceStream);
return TRUE;
}
static gboolean
gst_avc_src_stop (GstBaseSrc * src)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GstBuffer *buffer;
GST_DEBUG_OBJECT (avcsrc, "stop");
// Stop the stream
avcsrc->pAVCDevice->StopAVCDeviceStream(avcsrc->pAVCDeviceStream);
// Destroy the stream
avcsrc->pAVCDevice->DestroyAVCDeviceStream(avcsrc->pAVCDeviceStream);
avcsrc->pAVCDeviceStream = nil;
// Forget about the device (don't destroy it; pAVCDeviceController manages it)
avcsrc->pAVCDevice = nil;
GST_DEBUG("Packets enqueued = %llu", avcsrc->packets_enqueued);
GST_DEBUG("Packets dequeued = %llu", avcsrc->packets_dequeued);
while ((buffer = GST_BUFFER (gst_atomic_queue_pop (avcsrc->queue))) != NULL) {
gst_buffer_unref (buffer);
}
return TRUE;
}
static gboolean
gst_avc_src_is_seekable (GstBaseSrc * src)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GST_DEBUG_OBJECT (avcsrc, "is_seekable");
return FALSE;
}
static gboolean
gst_avc_src_unlock (GstBaseSrc * src)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GST_DEBUG_OBJECT (avcsrc, "unlock");
g_mutex_lock (avcsrc->queue_lock);
avcsrc->unlock = TRUE;
g_cond_signal (avcsrc->cond);
g_mutex_unlock (avcsrc->queue_lock);
return TRUE;
}
static gboolean
gst_avc_src_event (GstBaseSrc * src, GstEvent * event)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GST_DEBUG_OBJECT (avcsrc, "event of type '%s'", GST_EVENT_TYPE_NAME(event));
GST_DEBUG("Packets enqueued = %llu, dequeued = %llu",
avcsrc->packets_enqueued, avcsrc->packets_dequeued);
return TRUE;
}
static GstFlowReturn
gst_avc_src_create (GstBaseSrc * src, guint64 offset, guint size,
GstBuffer ** buf)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GstBuffer *buffer;
GST_DEBUG_OBJECT (avcsrc, "create");
g_mutex_lock (avcsrc->queue_lock);
buffer = GST_BUFFER (gst_atomic_queue_pop (avcsrc->queue));
while (buffer == NULL && !avcsrc->unlock) {
g_cond_wait (avcsrc->cond, avcsrc->queue_lock);
buffer = GST_BUFFER (gst_atomic_queue_pop (avcsrc->queue));
}
g_mutex_unlock (avcsrc->queue_lock);
if (avcsrc->unlock) {
if (buffer)
gst_buffer_unref (buffer);
return GST_FLOW_WRONG_STATE;
}
gst_buffer_set_caps (buffer, GST_PAD_CAPS (avcsrc->srcpad));
*buf = buffer;
avcsrc->packets_dequeued++;
return GST_FLOW_OK;
}
static gboolean
gst_avc_src_query (GstBaseSrc * src, GstQuery * query)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GST_DEBUG_OBJECT (avcsrc, "query");
return TRUE;
}
static gboolean
gst_avc_src_unlock_stop (GstBaseSrc * src)
{
GstAVCSrc *avcsrc = GST_AVC_SRC (src);
GST_DEBUG_OBJECT (avcsrc, "stop");
return TRUE;
}
|