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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtpdvpay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY (rtpdvpay_debug);
33 #define GST_CAT_DEFAULT (rtpdvpay_debug)
35 #define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
42 /* takes both system and non-system streams */
43 static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_STATIC_CAPS ("video/x-dv")
50 static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
51 GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS ("application/x-rtp, "
55 "media = (string) { \"video\", \"audio\" } ,"
56 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
57 "encoding-name = (string) \"DV\", "
58 "clock-rate = (int) 90000,"
59 "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
60 "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
61 "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
62 "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
63 /* optional parameters can't go in the template
64 * "audio = (string) { \"bundled\", \"none\" }"
69 static gboolean gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload,
71 static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * payload,
74 #define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
76 gst_dv_pay_mode_get_type (void)
78 static GType dv_pay_mode_type = 0;
79 static const GEnumValue dv_pay_modes[] = {
80 {GST_DV_PAY_MODE_VIDEO, "Video only", "video"},
81 {GST_DV_PAY_MODE_BUNDLED, "Video and Audio bundled", "bundled"},
82 {GST_DV_PAY_MODE_AUDIO, "Audio only", "audio"},
86 if (!dv_pay_mode_type) {
87 dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
89 return dv_pay_mode_type;
93 static void gst_dv_pay_set_property (GObject * object,
94 guint prop_id, const GValue * value, GParamSpec * pspec);
95 static void gst_dv_pay_get_property (GObject * object,
96 guint prop_id, GValue * value, GParamSpec * pspec);
98 #define gst_rtp_dv_pay_parent_class parent_class
99 G_DEFINE_TYPE (GstRTPDVPay, gst_rtp_dv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
102 gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
104 GObjectClass *gobject_class;
105 GstElementClass *gstelement_class;
106 GstRTPBasePayloadClass *gstrtpbasepayload_class;
108 GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
110 gobject_class = (GObjectClass *) klass;
111 gstelement_class = (GstElementClass *) klass;
112 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
114 gobject_class->set_property = gst_dv_pay_set_property;
115 gobject_class->get_property = gst_dv_pay_get_property;
117 g_object_class_install_property (gobject_class, PROP_MODE,
118 g_param_spec_enum ("mode", "Mode",
119 "The payload mode of payloading",
120 GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
121 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
123 gst_element_class_add_static_pad_template (gstelement_class,
124 &gst_rtp_dv_pay_sink_template);
125 gst_element_class_add_static_pad_template (gstelement_class,
126 &gst_rtp_dv_pay_src_template);
128 gst_element_class_set_static_metadata (gstelement_class, "RTP DV Payloader",
129 "Codec/Payloader/Network/RTP",
130 "Payloads DV into RTP packets (RFC 3189)",
131 "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
133 gstrtpbasepayload_class->set_caps = gst_rtp_dv_pay_setcaps;
134 gstrtpbasepayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
138 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay)
143 gst_dv_pay_set_property (GObject * object,
144 guint prop_id, const GValue * value, GParamSpec * pspec)
146 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
150 rtpdvpay->mode = g_value_get_enum (value);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gst_dv_pay_get_property (GObject * object,
160 guint prop_id, GValue * value, GParamSpec * pspec)
162 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
166 g_value_set_enum (value, rtpdvpay->mode);
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
177 /* We don't do anything here, but we could check if it's a system stream and if
178 * it's not, default to sending the video only. We will negotiate downstream
179 * caps when we get to see the first frame. */
185 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, gsize size)
187 const gchar *encode, *media;
188 gboolean audio_bundled, res;
190 if ((data[3] & 0x80) == 0) { /* DSF flag */
191 /* it's an NTSC format */
192 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
194 encode = "314M-25/525-60";
195 } else { /* 4:1:1 sampling */
197 encode = "SD-VCR/525-60";
200 /* it's a PAL format */
201 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
203 encode = "314M-50/625-50";
204 } else if ((data[5] & 0x07) == 0) { /* APT flag */
205 /* PAL 25Mbps 4:2:0 */
206 encode = "SD-VCR/625-50";
208 /* PAL 25Mbps 4:1:1 */
209 encode = "314M-25/625-50";
213 audio_bundled = FALSE;
215 switch (rtpdvpay->mode) {
216 case GST_DV_PAY_MODE_AUDIO:
219 case GST_DV_PAY_MODE_BUNDLED:
220 audio_bundled = TRUE;
225 gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpdvpay), media,
229 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
230 "encode", G_TYPE_STRING, encode,
231 "audio", G_TYPE_STRING, "bundled", NULL);
233 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
234 "encode", G_TYPE_STRING, encode, NULL);
240 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
245 block_type = data[0] >> 5;
247 switch (block_type) {
248 case 0: /* Header block */
249 case 1: /* Subcode block */
250 case 2: /* VAUX block */
251 /* always include these blocks */
254 case 3: /* Audio block */
255 /* never include audio if we are doing video only */
256 if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
261 case 4: /* Video block */
262 /* never include video if we are doing audio only */
263 if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
268 default: /* Something bogus, just ignore */
275 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
278 gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * basepayload,
281 GstRTPDVPay *rtpdvpay;
282 guint max_payload_size;
284 GstFlowReturn ret = GST_FLOW_OK;
291 GstRTPBuffer rtp = { NULL, };
293 rtpdvpay = GST_RTP_DV_PAY (basepayload);
295 hdrlen = gst_rtp_buffer_calc_header_len (0);
296 /* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
297 * each, and we should put an integral number of them in each RTP packet.
298 * Therefore, we round the available room down to the nearest multiple of 80.
300 * The available room is just the packet MTU, minus the RTP header length. */
301 max_payload_size = ((GST_RTP_BASE_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
303 /* The length of the buffer to transmit. */
304 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
305 GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
306 (NULL), ("Failed to map buffer"));
307 gst_buffer_unref (buffer);
308 return GST_FLOW_ERROR;
313 GST_DEBUG_OBJECT (rtpdvpay,
314 "DV RTP payloader got buffer of %" G_GSIZE_FORMAT
315 " bytes, splitting in %u byte " "payload fragments, at time %"
316 GST_TIME_FORMAT, size, max_payload_size,
317 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
319 if (!rtpdvpay->negotiated) {
320 gst_dv_pay_negotiate (rtpdvpay, data, size);
321 /* if we have not yet scanned the stream for its type, do so now */
322 rtpdvpay->negotiated = TRUE;
329 /* while we have a complete DIF chunks left */
331 /* Allocate a new buffer, set the timestamp */
332 if (outbuf == NULL) {
333 outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
334 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buffer);
336 if (!gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp)) {
337 gst_buffer_unref (outbuf);
338 GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
339 (NULL), ("Failed to map RTP buffer"));
340 ret = GST_FLOW_ERROR;
343 dest = gst_rtp_buffer_get_payload (&rtp);
347 /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
348 if (include_dif (rtpdvpay, data)) {
349 /* copy data in packet */
350 memcpy (dest, data, 80);
356 /* go to next dif chunk */
360 /* push out the buffer if the next one would exceed the max packet size or
361 * when we are pushing the last packet */
362 if (filled + 80 > max_payload_size || size < 80) {
367 gst_rtp_buffer_set_marker (&rtp, TRUE);
369 /* shrink buffer to last packet */
370 hlen = gst_rtp_buffer_get_header_len (&rtp);
371 gst_rtp_buffer_set_packet_len (&rtp, hlen + filled);
374 /* Push out the created piece, and check for errors. */
375 gst_rtp_buffer_unmap (&rtp);
376 gst_rtp_copy_meta (GST_ELEMENT_CAST (basepayload), outbuf, buffer, 0);
377 ret = gst_rtp_base_payload_push (basepayload, outbuf);
378 if (ret != GST_FLOW_OK)
386 gst_buffer_unmap (buffer, &map);
387 gst_buffer_unref (buffer);
393 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
395 return gst_element_register (plugin, "rtpdvpay",
396 GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);