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;
136 #ifndef TIZEN_FEATURE_GST_UPSTREAM_AVOID_BUILD_BREAK
137 gst_type_mark_as_plugin_api (GST_TYPE_DV_PAY_MODE, 0);
142 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay)
147 gst_dv_pay_set_property (GObject * object,
148 guint prop_id, const GValue * value, GParamSpec * pspec)
150 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
154 rtpdvpay->mode = g_value_get_enum (value);
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 gst_dv_pay_get_property (GObject * object,
164 guint prop_id, GValue * value, GParamSpec * pspec)
166 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
170 g_value_set_enum (value, rtpdvpay->mode);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
181 /* We don't do anything here, but we could check if it's a system stream and if
182 * it's not, default to sending the video only. We will negotiate downstream
183 * caps when we get to see the first frame. */
189 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, gsize size)
191 const gchar *encode, *media;
192 gboolean audio_bundled, res;
194 if ((data[3] & 0x80) == 0) { /* DSF flag */
195 /* it's an NTSC format */
196 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
198 encode = "314M-25/525-60";
199 } else { /* 4:1:1 sampling */
201 encode = "SD-VCR/525-60";
204 /* it's a PAL format */
205 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
207 encode = "314M-50/625-50";
208 } else if ((data[5] & 0x07) == 0) { /* APT flag */
209 /* PAL 25Mbps 4:2:0 */
210 encode = "SD-VCR/625-50";
212 /* PAL 25Mbps 4:1:1 */
213 encode = "314M-25/625-50";
217 audio_bundled = FALSE;
219 switch (rtpdvpay->mode) {
220 case GST_DV_PAY_MODE_AUDIO:
223 case GST_DV_PAY_MODE_BUNDLED:
224 audio_bundled = TRUE;
229 gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpdvpay), media,
233 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
234 "encode", G_TYPE_STRING, encode,
235 "audio", G_TYPE_STRING, "bundled", NULL);
237 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
238 "encode", G_TYPE_STRING, encode, NULL);
244 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
249 block_type = data[0] >> 5;
251 switch (block_type) {
252 case 0: /* Header block */
253 case 1: /* Subcode block */
254 case 2: /* VAUX block */
255 /* always include these blocks */
258 case 3: /* Audio block */
259 /* never include audio if we are doing video only */
260 if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
265 case 4: /* Video block */
266 /* never include video if we are doing audio only */
267 if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
272 default: /* Something bogus, just ignore */
279 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
282 gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * basepayload,
285 GstRTPDVPay *rtpdvpay;
286 guint max_payload_size;
288 GstFlowReturn ret = GST_FLOW_OK;
295 GstRTPBuffer rtp = { NULL, };
297 rtpdvpay = GST_RTP_DV_PAY (basepayload);
299 hdrlen = gst_rtp_buffer_calc_header_len (0);
300 /* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
301 * each, and we should put an integral number of them in each RTP packet.
302 * Therefore, we round the available room down to the nearest multiple of 80.
304 * The available room is just the packet MTU, minus the RTP header length. */
305 max_payload_size = ((GST_RTP_BASE_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
307 /* The length of the buffer to transmit. */
308 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
309 GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
310 (NULL), ("Failed to map buffer"));
311 gst_buffer_unref (buffer);
312 return GST_FLOW_ERROR;
317 GST_DEBUG_OBJECT (rtpdvpay,
318 "DV RTP payloader got buffer of %" G_GSIZE_FORMAT
319 " bytes, splitting in %u byte " "payload fragments, at time %"
320 GST_TIME_FORMAT, size, max_payload_size,
321 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
323 if (!rtpdvpay->negotiated) {
324 gst_dv_pay_negotiate (rtpdvpay, data, size);
325 /* if we have not yet scanned the stream for its type, do so now */
326 rtpdvpay->negotiated = TRUE;
333 /* while we have a complete DIF chunks left */
335 /* Allocate a new buffer, set the timestamp */
336 if (outbuf == NULL) {
337 outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
338 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buffer);
340 if (!gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp)) {
341 gst_buffer_unref (outbuf);
342 GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
343 (NULL), ("Failed to map RTP buffer"));
344 ret = GST_FLOW_ERROR;
347 dest = gst_rtp_buffer_get_payload (&rtp);
351 /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
352 if (include_dif (rtpdvpay, data)) {
353 /* copy data in packet */
354 memcpy (dest, data, 80);
360 /* go to next dif chunk */
364 /* push out the buffer if the next one would exceed the max packet size or
365 * when we are pushing the last packet */
366 if (filled + 80 > max_payload_size || size < 80) {
371 gst_rtp_buffer_set_marker (&rtp, TRUE);
373 /* shrink buffer to last packet */
374 hlen = gst_rtp_buffer_get_header_len (&rtp);
375 gst_rtp_buffer_set_packet_len (&rtp, hlen + filled);
378 /* Push out the created piece, and check for errors. */
379 gst_rtp_buffer_unmap (&rtp);
380 gst_rtp_copy_meta (GST_ELEMENT_CAST (basepayload), outbuf, buffer, 0);
381 ret = gst_rtp_base_payload_push (basepayload, outbuf);
382 if (ret != GST_FLOW_OK)
390 gst_buffer_unmap (buffer, &map);
391 gst_buffer_unref (buffer);
397 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
399 return gst_element_register (plugin, "rtpdvpay",
400 GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);