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
|
/* Farsight
* Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
* (C) 2008 Wim Taymans <wim.taymans@gmail.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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <gst/rtp/gstrtpbuffer.h>
#include "gstrtpdvpay.h"
GST_DEBUG_CATEGORY (rtpdvpay_debug);
#define GST_CAT_DEFAULT (rtpdvpay_debug)
#define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
enum
{
PROP_0,
PROP_MODE
};
/* takes both system and non-system streams */
static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-dv")
);
static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-rtp, "
"media = (string) { \"video\", \"audio\" } ,"
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
"encoding-name = (string) \"DV\", "
"clock-rate = (int) 90000,"
"encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
"\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
"\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
"\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
/* optional parameters can't go in the template
* "audio = (string) { \"bundled\", \"none\" }"
*/
)
);
static gboolean gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload,
GstCaps * caps);
static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * payload,
GstBuffer * buffer);
#define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
static GType
gst_dv_pay_mode_get_type (void)
{
static GType dv_pay_mode_type = 0;
static const GEnumValue dv_pay_modes[] = {
{GST_DV_PAY_MODE_VIDEO, "Video only", "video"},
{GST_DV_PAY_MODE_BUNDLED, "Video and Audio bundled", "bundled"},
{GST_DV_PAY_MODE_AUDIO, "Audio only", "audio"},
{0, NULL, NULL},
};
if (!dv_pay_mode_type) {
dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
}
return dv_pay_mode_type;
}
static void gst_dv_pay_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_dv_pay_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
GST_BOILERPLATE (GstRTPDVPay, gst_rtp_dv_pay, GstBaseRTPPayload,
GST_TYPE_BASE_RTP_PAYLOAD)
static void gst_rtp_dv_pay_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_dv_pay_sink_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_dv_pay_src_template));
gst_element_class_set_details_simple (element_class, "RTP DV Payloader",
"Codec/Payloader/Network/RTP",
"Payloads DV into RTP packets (RFC 3189)",
"Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
}
static void
gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
{
GObjectClass *gobject_class;
GstBaseRTPPayloadClass *gstbasertppayload_class;
gobject_class = (GObjectClass *) klass;
gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
gobject_class->set_property = gst_dv_pay_set_property;
gobject_class->get_property = gst_dv_pay_get_property;
gstbasertppayload_class->set_caps = gst_rtp_dv_pay_setcaps;
gstbasertppayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
g_object_class_install_property (gobject_class, PROP_MODE,
g_param_spec_enum ("mode", "Mode",
"The payload mode of payloading",
GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
}
static void
gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay, GstRTPDVPayClass * klass)
{
}
static void
gst_dv_pay_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec)
{
GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
switch (prop_id) {
case PROP_MODE:
rtpdvpay->mode = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_dv_pay_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
{
GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
switch (prop_id) {
case PROP_MODE:
g_value_set_enum (value, rtpdvpay->mode);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
{
/* We don't do anything here, but we could check if it's a system stream and if
* it's not, default to sending the video only. We will negotiate downstream
* caps when we get to see the first frame. */
return TRUE;
}
static gboolean
gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, guint size)
{
const gchar *encode, *media;
gboolean audio_bundled, res;
if ((data[3] & 0x80) == 0) { /* DSF flag */
/* it's an NTSC format */
if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
/* NTSC 50Mbps */
encode = "314M-25/525-60";
} else { /* 4:1:1 sampling */
/* NTSC 25Mbps */
encode = "SD-VCR/525-60";
}
} else {
/* it's a PAL format */
if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
/* PAL 50Mbps */
encode = "314M-50/625-50";
} else if ((data[5] & 0x07) == 0) { /* APT flag */
/* PAL 25Mbps 4:2:0 */
encode = "SD-VCR/625-50";
} else
/* PAL 25Mbps 4:1:1 */
encode = "314M-25/625-50";
}
media = "video";
audio_bundled = FALSE;
switch (rtpdvpay->mode) {
case GST_DV_PAY_MODE_AUDIO:
media = "audio";
break;
case GST_DV_PAY_MODE_BUNDLED:
audio_bundled = TRUE;
break;
default:
break;
}
gst_basertppayload_set_options (GST_BASE_RTP_PAYLOAD (rtpdvpay), media, TRUE,
"DV", 90000);
if (audio_bundled) {
res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
"encode", G_TYPE_STRING, encode,
"audio", G_TYPE_STRING, "bundled", NULL);
} else {
res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
"encode", G_TYPE_STRING, encode, NULL);
}
return res;
}
static gboolean
include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
{
gint block_type;
gboolean res;
block_type = data[0] >> 5;
switch (block_type) {
case 0: /* Header block */
case 1: /* Subcode block */
case 2: /* VAUX block */
/* always include these blocks */
res = TRUE;
break;
case 3: /* Audio block */
/* never include audio if we are doing video only */
if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
res = FALSE;
else
res = TRUE;
break;
case 4: /* Video block */
/* never include video if we are doing audio only */
if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
res = FALSE;
else
res = TRUE;
break;
default: /* Something bogus, just ignore */
res = FALSE;
break;
}
return res;
}
/* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
*/
static GstFlowReturn
gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
GstBuffer * buffer)
{
GstRTPDVPay *rtpdvpay;
guint max_payload_size;
GstBuffer *outbuf;
GstFlowReturn ret = GST_FLOW_OK;
gint hdrlen;
guint size;
guint8 *data;
guint8 *dest;
guint filled;
rtpdvpay = GST_RTP_DV_PAY (basepayload);
hdrlen = gst_rtp_buffer_calc_header_len (0);
/* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
* each, and we should put an integral number of them in each RTP packet.
* Therefore, we round the available room down to the nearest multiple of 80.
*
* The available room is just the packet MTU, minus the RTP header length. */
max_payload_size = ((GST_BASE_RTP_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
/* The length of the buffer to transmit. */
size = GST_BUFFER_SIZE (buffer);
data = GST_BUFFER_DATA (buffer);
GST_DEBUG_OBJECT (rtpdvpay,
"DV RTP payloader got buffer of %u bytes, splitting in %u byte "
"payload fragments, at time %" GST_TIME_FORMAT, size, max_payload_size,
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
if (!rtpdvpay->negotiated) {
gst_dv_pay_negotiate (rtpdvpay, data, size);
/* if we have not yet scanned the stream for its type, do so now */
rtpdvpay->negotiated = TRUE;
}
outbuf = NULL;
dest = NULL;
filled = 0;
/* while we have a complete DIF chunks left */
while (size >= 80) {
/* Allocate a new buffer, set the timestamp */
if (outbuf == NULL) {
outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
dest = gst_rtp_buffer_get_payload (outbuf);
filled = 0;
}
/* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
if (include_dif (rtpdvpay, data)) {
/* copy data in packet */
memcpy (dest, data, 80);
dest += 80;
filled += 80;
}
/* go to next dif chunk */
size -= 80;
data += 80;
/* push out the buffer if the next one would exceed the max packet size or
* when we are pushing the last packet */
if (filled + 80 > max_payload_size || size < 80) {
if (size < 160) {
guint hlen;
/* set marker */
gst_rtp_buffer_set_marker (outbuf, TRUE);
/* shrink buffer to last packet */
hlen = gst_rtp_buffer_get_header_len (outbuf);
gst_rtp_buffer_set_packet_len (outbuf, hlen + filled);
}
/* Push out the created piece, and check for errors. */
ret = gst_basertppayload_push (basepayload, outbuf);
if (ret != GST_FLOW_OK)
break;
outbuf = NULL;
}
}
gst_buffer_unref (buffer);
return ret;
}
gboolean
gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "rtpdvpay",
GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);
}
|