rtpdvpay: catch failures to map buffer
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpdvpay.c
1 /* Farsight
2  * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
3  *           (C) 2008 Wim Taymans <wim.taymans@gmail.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include "gstrtpdvpay.h"
30
31 GST_DEBUG_CATEGORY (rtpdvpay_debug);
32 #define GST_CAT_DEFAULT (rtpdvpay_debug)
33
34 #define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
35 enum
36 {
37   PROP_0,
38   PROP_MODE
39 };
40
41 /* takes both system and non-system streams */
42 static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("video/x-dv")
47     );
48
49 static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
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\" }"
64          */
65     )
66     );
67
68 static gboolean gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload,
69     GstCaps * caps);
70 static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * payload,
71     GstBuffer * buffer);
72
73 #define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
74 static GType
75 gst_dv_pay_mode_get_type (void)
76 {
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"},
82     {0, NULL, NULL},
83   };
84
85   if (!dv_pay_mode_type) {
86     dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
87   }
88   return dv_pay_mode_type;
89 }
90
91
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);
96
97 #define gst_rtp_dv_pay_parent_class parent_class
98 G_DEFINE_TYPE (GstRTPDVPay, gst_rtp_dv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
99
100 static void
101 gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
102 {
103   GObjectClass *gobject_class;
104   GstElementClass *gstelement_class;
105   GstRTPBasePayloadClass *gstrtpbasepayload_class;
106
107   GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
108
109   gobject_class = (GObjectClass *) klass;
110   gstelement_class = (GstElementClass *) klass;
111   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
112
113   gobject_class->set_property = gst_dv_pay_set_property;
114   gobject_class->get_property = gst_dv_pay_get_property;
115
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));
121
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));
126
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>");
131
132   gstrtpbasepayload_class->set_caps = gst_rtp_dv_pay_setcaps;
133   gstrtpbasepayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
134 }
135
136 static void
137 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay)
138 {
139 }
140
141 static void
142 gst_dv_pay_set_property (GObject * object,
143     guint prop_id, const GValue * value, GParamSpec * pspec)
144 {
145   GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
146
147   switch (prop_id) {
148     case PROP_MODE:
149       rtpdvpay->mode = g_value_get_enum (value);
150       break;
151     default:
152       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153       break;
154   }
155 }
156
157 static void
158 gst_dv_pay_get_property (GObject * object,
159     guint prop_id, GValue * value, GParamSpec * pspec)
160 {
161   GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
162
163   switch (prop_id) {
164     case PROP_MODE:
165       g_value_set_enum (value, rtpdvpay->mode);
166       break;
167     default:
168       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169       break;
170   }
171 }
172
173 static gboolean
174 gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
175 {
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. */
179
180   return TRUE;
181 }
182
183 static gboolean
184 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, gsize size)
185 {
186   const gchar *encode, *media;
187   gboolean audio_bundled, res;
188
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 */
192       /* NTSC 50Mbps */
193       encode = "314M-25/525-60";
194     } else {                    /* 4:1:1 sampling */
195       /* NTSC 25Mbps */
196       encode = "SD-VCR/525-60";
197     }
198   } else {
199     /* it's a PAL format */
200     if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
201       /* PAL 50Mbps */
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";
206     } else
207       /* PAL 25Mbps 4:1:1 */
208       encode = "314M-25/625-50";
209   }
210
211   media = "video";
212   audio_bundled = FALSE;
213
214   switch (rtpdvpay->mode) {
215     case GST_DV_PAY_MODE_AUDIO:
216       media = "audio";
217       break;
218     case GST_DV_PAY_MODE_BUNDLED:
219       audio_bundled = TRUE;
220       break;
221     default:
222       break;
223   }
224   gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpdvpay), media,
225       TRUE, "DV", 90000);
226
227   if (audio_bundled) {
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);
231   } else {
232     res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
233         "encode", G_TYPE_STRING, encode, NULL);
234   }
235   return res;
236 }
237
238 static gboolean
239 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
240 {
241   gint block_type;
242   gboolean res;
243
244   block_type = data[0] >> 5;
245
246   switch (block_type) {
247     case 0:                    /* Header block */
248     case 1:                    /* Subcode block */
249     case 2:                    /* VAUX block */
250       /* always include these blocks */
251       res = TRUE;
252       break;
253     case 3:                    /* Audio block */
254       /* never include audio if we are doing video only */
255       if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
256         res = FALSE;
257       else
258         res = TRUE;
259       break;
260     case 4:                    /* Video block */
261       /* never include video if we are doing audio only */
262       if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
263         res = FALSE;
264       else
265         res = TRUE;
266       break;
267     default:                   /* Something bogus, just ignore */
268       res = FALSE;
269       break;
270   }
271   return res;
272 }
273
274 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
275  */
276 static GstFlowReturn
277 gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * basepayload,
278     GstBuffer * buffer)
279 {
280   GstRTPDVPay *rtpdvpay;
281   guint max_payload_size;
282   GstBuffer *outbuf;
283   GstFlowReturn ret = GST_FLOW_OK;
284   gint hdrlen;
285   gsize size;
286   GstMapInfo map;
287   guint8 *data;
288   guint8 *dest;
289   guint filled;
290   GstRTPBuffer rtp = { NULL, };
291
292   rtpdvpay = GST_RTP_DV_PAY (basepayload);
293
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.
298    *
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;
301
302   /* The length of the buffer to transmit. */
303   if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
304     GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
305         (NULL), ("Failed to map buffer"));
306     gst_buffer_unref (buffer);
307     return GST_FLOW_ERROR;
308   }
309   data = map.data;
310   size = map.size;
311
312   GST_DEBUG_OBJECT (rtpdvpay,
313       "DV RTP payloader got buffer of %" G_GSIZE_FORMAT
314       " bytes, splitting in %u byte " "payload fragments, at time %"
315       GST_TIME_FORMAT, size, max_payload_size,
316       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
317
318   if (!rtpdvpay->negotiated) {
319     gst_dv_pay_negotiate (rtpdvpay, data, size);
320     /* if we have not yet scanned the stream for its type, do so now */
321     rtpdvpay->negotiated = TRUE;
322   }
323
324   outbuf = NULL;
325   dest = NULL;
326   filled = 0;
327
328   /* while we have a complete DIF chunks left */
329   while (size >= 80) {
330     /* Allocate a new buffer, set the timestamp */
331     if (outbuf == NULL) {
332       outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
333       GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
334
335       if (!gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp)) {
336         gst_buffer_unref (outbuf);
337         GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
338             (NULL), ("Failed to map RTP buffer"));
339         ret = GST_FLOW_ERROR;
340         goto beach;
341       }
342       dest = gst_rtp_buffer_get_payload (&rtp);
343       filled = 0;
344     }
345
346     /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
347     if (include_dif (rtpdvpay, data)) {
348       /* copy data in packet */
349       memcpy (dest, data, 80);
350
351       dest += 80;
352       filled += 80;
353     }
354
355     /* go to next dif chunk */
356     size -= 80;
357     data += 80;
358
359     /* push out the buffer if the next one would exceed the max packet size or
360      * when we are pushing the last packet */
361     if (filled + 80 > max_payload_size || size < 80) {
362       if (size < 160) {
363         guint hlen;
364
365         /* set marker */
366         gst_rtp_buffer_set_marker (&rtp, TRUE);
367
368         /* shrink buffer to last packet */
369         hlen = gst_rtp_buffer_get_header_len (&rtp);
370         gst_rtp_buffer_set_packet_len (&rtp, hlen + filled);
371       }
372
373       /* Push out the created piece, and check for errors. */
374       gst_rtp_buffer_unmap (&rtp);
375       ret = gst_rtp_base_payload_push (basepayload, outbuf);
376       if (ret != GST_FLOW_OK)
377         break;
378
379       outbuf = NULL;
380     }
381   }
382
383 beach:
384   gst_buffer_unmap (buffer, &map);
385   gst_buffer_unref (buffer);
386
387   return ret;
388 }
389
390 gboolean
391 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
392 {
393   return gst_element_register (plugin, "rtpdvpay",
394       GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);
395 }