2 * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
3 * (C) 2008 Wim Taymans <wim.taymans@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtpdvpay.h"
31 GST_DEBUG_CATEGORY (rtpdvpay_debug);
32 #define GST_CAT_DEFAULT (rtpdvpay_debug)
34 #define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
41 /* takes both system and non-system streams */
42 static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("video/x-dv")
49 static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("application/x-rtp, "
54 "media = (string) { \"video\", \"audio\" } ,"
55 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56 "encoding-name = (string) \"DV\", "
57 "clock-rate = (int) 90000,"
58 "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
59 "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
60 "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
61 "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
62 /* optional parameters can't go in the template
63 * "audio = (string) { \"bundled\", \"none\" }"
68 static gboolean gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload,
70 static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * payload,
73 #define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
75 gst_dv_pay_mode_get_type (void)
77 static GType dv_pay_mode_type = 0;
78 static const GEnumValue dv_pay_modes[] = {
79 {GST_DV_PAY_MODE_VIDEO, "Video only", "video"},
80 {GST_DV_PAY_MODE_BUNDLED, "Video and Audio bundled", "bundled"},
81 {GST_DV_PAY_MODE_AUDIO, "Audio only", "audio"},
85 if (!dv_pay_mode_type) {
86 dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
88 return dv_pay_mode_type;
92 static void gst_dv_pay_set_property (GObject * object,
93 guint prop_id, const GValue * value, GParamSpec * pspec);
94 static void gst_dv_pay_get_property (GObject * object,
95 guint prop_id, GValue * value, GParamSpec * pspec);
97 GST_BOILERPLATE (GstRTPDVPay, gst_rtp_dv_pay, GstBaseRTPPayload,
98 GST_TYPE_BASE_RTP_PAYLOAD)
100 static void gst_rtp_dv_pay_base_init (gpointer g_class)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
104 gst_element_class_add_static_pad_template (element_class,
105 &gst_rtp_dv_pay_sink_template);
106 gst_element_class_add_static_pad_template (element_class,
107 &gst_rtp_dv_pay_src_template);
108 gst_element_class_set_details_simple (element_class, "RTP DV Payloader",
109 "Codec/Payloader/Network/RTP",
110 "Payloads DV into RTP packets (RFC 3189)",
111 "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
115 gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
117 GObjectClass *gobject_class;
118 GstBaseRTPPayloadClass *gstbasertppayload_class;
120 gobject_class = (GObjectClass *) klass;
121 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
123 gobject_class->set_property = gst_dv_pay_set_property;
124 gobject_class->get_property = gst_dv_pay_get_property;
126 gstbasertppayload_class->set_caps = gst_rtp_dv_pay_setcaps;
127 gstbasertppayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
129 g_object_class_install_property (gobject_class, PROP_MODE,
130 g_param_spec_enum ("mode", "Mode",
131 "The payload mode of payloading",
132 GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
133 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
135 GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
139 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay, GstRTPDVPayClass * klass)
144 gst_dv_pay_set_property (GObject * object,
145 guint prop_id, const GValue * value, GParamSpec * pspec)
147 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
151 rtpdvpay->mode = g_value_get_enum (value);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 gst_dv_pay_get_property (GObject * object,
161 guint prop_id, GValue * value, GParamSpec * pspec)
163 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
167 g_value_set_enum (value, rtpdvpay->mode);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
178 /* We don't do anything here, but we could check if it's a system stream and if
179 * it's not, default to sending the video only. We will negotiate downstream
180 * caps when we get to see the first frame. */
186 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, guint size)
188 const gchar *encode, *media;
189 gboolean audio_bundled, res;
191 if ((data[3] & 0x80) == 0) { /* DSF flag */
192 /* it's an NTSC format */
193 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
195 encode = "314M-25/525-60";
196 } else { /* 4:1:1 sampling */
198 encode = "SD-VCR/525-60";
201 /* it's a PAL format */
202 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
204 encode = "314M-50/625-50";
205 } else if ((data[5] & 0x07) == 0) { /* APT flag */
206 /* PAL 25Mbps 4:2:0 */
207 encode = "SD-VCR/625-50";
209 /* PAL 25Mbps 4:1:1 */
210 encode = "314M-25/625-50";
214 audio_bundled = FALSE;
216 switch (rtpdvpay->mode) {
217 case GST_DV_PAY_MODE_AUDIO:
220 case GST_DV_PAY_MODE_BUNDLED:
221 audio_bundled = TRUE;
226 gst_basertppayload_set_options (GST_BASE_RTP_PAYLOAD (rtpdvpay), media, TRUE,
230 res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
231 "encode", G_TYPE_STRING, encode,
232 "audio", G_TYPE_STRING, "bundled", NULL);
234 res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
235 "encode", G_TYPE_STRING, encode, NULL);
241 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
246 block_type = data[0] >> 5;
248 switch (block_type) {
249 case 0: /* Header block */
250 case 1: /* Subcode block */
251 case 2: /* VAUX block */
252 /* always include these blocks */
255 case 3: /* Audio block */
256 /* never include audio if we are doing video only */
257 if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
262 case 4: /* Video block */
263 /* never include video if we are doing audio only */
264 if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
269 default: /* Something bogus, just ignore */
276 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
279 gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
282 GstRTPDVPay *rtpdvpay;
283 guint max_payload_size;
285 GstFlowReturn ret = GST_FLOW_OK;
292 rtpdvpay = GST_RTP_DV_PAY (basepayload);
294 hdrlen = gst_rtp_buffer_calc_header_len (0);
295 /* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
296 * each, and we should put an integral number of them in each RTP packet.
297 * Therefore, we round the available room down to the nearest multiple of 80.
299 * The available room is just the packet MTU, minus the RTP header length. */
300 max_payload_size = ((GST_BASE_RTP_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
302 /* The length of the buffer to transmit. */
303 size = GST_BUFFER_SIZE (buffer);
304 data = GST_BUFFER_DATA (buffer);
306 GST_DEBUG_OBJECT (rtpdvpay,
307 "DV RTP payloader got buffer of %u bytes, splitting in %u byte "
308 "payload fragments, at time %" GST_TIME_FORMAT, size, max_payload_size,
309 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
311 if (!rtpdvpay->negotiated) {
312 gst_dv_pay_negotiate (rtpdvpay, data, size);
313 /* if we have not yet scanned the stream for its type, do so now */
314 rtpdvpay->negotiated = TRUE;
321 /* while we have a complete DIF chunks left */
323 /* Allocate a new buffer, set the timestamp */
324 if (outbuf == NULL) {
325 outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
326 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
327 dest = gst_rtp_buffer_get_payload (outbuf);
331 /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
332 if (include_dif (rtpdvpay, data)) {
333 /* copy data in packet */
334 memcpy (dest, data, 80);
340 /* go to next dif chunk */
344 /* push out the buffer if the next one would exceed the max packet size or
345 * when we are pushing the last packet */
346 if (filled + 80 > max_payload_size || size < 80) {
351 gst_rtp_buffer_set_marker (outbuf, TRUE);
353 /* shrink buffer to last packet */
354 hlen = gst_rtp_buffer_get_header_len (outbuf);
355 gst_rtp_buffer_set_packet_len (outbuf, hlen + filled);
357 /* Push out the created piece, and check for errors. */
358 ret = gst_basertppayload_push (basepayload, outbuf);
359 if (ret != GST_FLOW_OK)
365 gst_buffer_unref (buffer);
371 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
373 return gst_element_register (plugin, "rtpdvpay",
374 GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);