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"
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 (GstRTPBasePayload * payload,
70 static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * 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 #define gst_rtp_dv_pay_parent_class parent_class
98 G_DEFINE_TYPE (GstRTPDVPay, gst_rtp_dv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
101 gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
103 GObjectClass *gobject_class;
104 GstElementClass *gstelement_class;
105 GstRTPBasePayloadClass *gstrtpbasepayload_class;
107 GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
111 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
113 gobject_class->set_property = gst_dv_pay_set_property;
114 gobject_class->get_property = gst_dv_pay_get_property;
116 g_object_class_install_property (gobject_class, PROP_MODE,
117 g_param_spec_enum ("mode", "Mode",
118 "The payload mode of payloading",
119 GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
120 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122 gst_element_class_add_pad_template (gstelement_class,
123 gst_static_pad_template_get (&gst_rtp_dv_pay_sink_template));
124 gst_element_class_add_pad_template (gstelement_class,
125 gst_static_pad_template_get (&gst_rtp_dv_pay_src_template));
127 gst_element_class_set_static_metadata (gstelement_class, "RTP DV Payloader",
128 "Codec/Payloader/Network/RTP",
129 "Payloads DV into RTP packets (RFC 3189)",
130 "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
132 gstrtpbasepayload_class->set_caps = gst_rtp_dv_pay_setcaps;
133 gstrtpbasepayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
137 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay)
142 gst_dv_pay_set_property (GObject * object,
143 guint prop_id, const GValue * value, GParamSpec * pspec)
145 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
149 rtpdvpay->mode = g_value_get_enum (value);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 gst_dv_pay_get_property (GObject * object,
159 guint prop_id, GValue * value, GParamSpec * pspec)
161 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
165 g_value_set_enum (value, rtpdvpay->mode);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
176 /* We don't do anything here, but we could check if it's a system stream and if
177 * it's not, default to sending the video only. We will negotiate downstream
178 * caps when we get to see the first frame. */
184 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, gsize size)
186 const gchar *encode, *media;
187 gboolean audio_bundled, res;
189 if ((data[3] & 0x80) == 0) { /* DSF flag */
190 /* it's an NTSC format */
191 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
193 encode = "314M-25/525-60";
194 } else { /* 4:1:1 sampling */
196 encode = "SD-VCR/525-60";
199 /* it's a PAL format */
200 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
202 encode = "314M-50/625-50";
203 } else if ((data[5] & 0x07) == 0) { /* APT flag */
204 /* PAL 25Mbps 4:2:0 */
205 encode = "SD-VCR/625-50";
207 /* PAL 25Mbps 4:1:1 */
208 encode = "314M-25/625-50";
212 audio_bundled = FALSE;
214 switch (rtpdvpay->mode) {
215 case GST_DV_PAY_MODE_AUDIO:
218 case GST_DV_PAY_MODE_BUNDLED:
219 audio_bundled = TRUE;
224 gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpdvpay), media,
228 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
229 "encode", G_TYPE_STRING, encode,
230 "audio", G_TYPE_STRING, "bundled", NULL);
232 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
233 "encode", G_TYPE_STRING, encode, NULL);
239 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
244 block_type = data[0] >> 5;
246 switch (block_type) {
247 case 0: /* Header block */
248 case 1: /* Subcode block */
249 case 2: /* VAUX block */
250 /* always include these blocks */
253 case 3: /* Audio block */
254 /* never include audio if we are doing video only */
255 if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
260 case 4: /* Video block */
261 /* never include video if we are doing audio only */
262 if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
267 default: /* Something bogus, just ignore */
274 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
277 gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * basepayload,
280 GstRTPDVPay *rtpdvpay;
281 guint max_payload_size;
283 GstFlowReturn ret = GST_FLOW_OK;
290 GstRTPBuffer rtp = { NULL, };
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_RTP_BASE_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
302 /* The length of the buffer to transmit. */
303 gst_buffer_map (buffer, &map, GST_MAP_READ);
307 GST_DEBUG_OBJECT (rtpdvpay,
308 "DV RTP payloader got buffer of %" G_GSIZE_FORMAT
309 " bytes, splitting in %u byte " "payload fragments, at time %"
310 GST_TIME_FORMAT, size, max_payload_size,
311 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
313 if (!rtpdvpay->negotiated) {
314 gst_dv_pay_negotiate (rtpdvpay, data, size);
315 /* if we have not yet scanned the stream for its type, do so now */
316 rtpdvpay->negotiated = TRUE;
323 /* while we have a complete DIF chunks left */
325 /* Allocate a new buffer, set the timestamp */
326 if (outbuf == NULL) {
327 outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
328 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
330 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
331 dest = gst_rtp_buffer_get_payload (&rtp);
335 /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
336 if (include_dif (rtpdvpay, data)) {
337 /* copy data in packet */
338 memcpy (dest, data, 80);
344 /* go to next dif chunk */
348 /* push out the buffer if the next one would exceed the max packet size or
349 * when we are pushing the last packet */
350 if (filled + 80 > max_payload_size || size < 80) {
355 gst_rtp_buffer_set_marker (&rtp, TRUE);
357 /* shrink buffer to last packet */
358 hlen = gst_rtp_buffer_get_header_len (&rtp);
359 gst_rtp_buffer_set_packet_len (&rtp, hlen + filled);
362 /* Push out the created piece, and check for errors. */
363 gst_rtp_buffer_unmap (&rtp);
364 ret = gst_rtp_base_payload_push (basepayload, outbuf);
365 if (ret != GST_FLOW_OK)
371 gst_buffer_unmap (buffer, &map);
372 gst_buffer_unref (buffer);
378 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
380 return gst_element_register (plugin, "rtpdvpay",
381 GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);