rtpvp8: port some more to new memory API
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpgstpay.c
1 /* GStreamer
2  * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpgstpay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (gst_rtp_pay_debug);
31 #define GST_CAT_DEFAULT gst_rtp_pay_debug
32
33 /*
34  *  0                   1                   2                   3
35  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
36  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37  * |C| CV  |D|0|0|0|     ETYPE     |  MBZ                          |
38  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  * |                          Frag_offset                          |
40  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  *
42  * C: caps inlined flag
43  *   When C set, first part of payload contains caps definition. Caps definition
44  *   starts with variable-length length prefix and then a string of that length.
45  *   the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
46  *   is the continuation marker and the 7 next bits the data. A continuation
47  *   marker of 1 means that the next byte contains more data.
48  *
49  * CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
50  * D: delta unit buffer
51  * ETYPE: type of event. Payload contains the event, prefixed with a
52  *        variable length field.
53  *   0 = NO event
54  *   1 = GST_EVENT_TAG
55  *   2 = GST_EVENT_CUSTOM_DOWNSTREAM
56  *   3 = GST_EVENT_CUSTOM_BOTH
57  */
58
59 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
61     GST_PAD_SINK,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS_ANY);
64
65 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
67     GST_PAD_SRC,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS ("application/x-rtp, "
70         "media = (string) \"application\", "
71         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
72         "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
73     );
74
75 static void gst_rtp_gst_pay_finalize (GObject * obj);
76
77 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
78     GstCaps * caps);
79 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
80     GstBuffer * buffer);
81 static gboolean gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload,
82     GstEvent * event);
83
84 #define gst_rtp_gst_pay_parent_class parent_class
85 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
86
87 static void
88 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
89 {
90   GObjectClass *gobject_class;
91   GstElementClass *gstelement_class;
92   GstRTPBasePayloadClass *gstrtpbasepayload_class;
93
94   gobject_class = (GObjectClass *) klass;
95   gstelement_class = (GstElementClass *) klass;
96   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
97
98   gobject_class->finalize = gst_rtp_gst_pay_finalize;
99
100   gst_element_class_add_pad_template (gstelement_class,
101       gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
102   gst_element_class_add_pad_template (gstelement_class,
103       gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
104
105   gst_element_class_set_static_metadata (gstelement_class,
106       "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
107       "Payload GStreamer buffers as RTP packets",
108       "Wim Taymans <wim.taymans@gmail.com>");
109
110   gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
111   gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
112   gstrtpbasepayload_class->sink_event = gst_rtp_gst_pay_sink_event;
113
114   GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
115       "rtpgstpay element");
116 }
117
118 static void
119 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
120 {
121   rtpgstpay->adapter = gst_adapter_new ();
122   gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpgstpay),
123       "application", TRUE, "X-GST", 90000);
124 }
125
126 static void
127 gst_rtp_gst_pay_finalize (GObject * obj)
128 {
129   GstRtpGSTPay *rtpgstpay;
130
131   rtpgstpay = GST_RTP_GST_PAY (obj);
132
133   g_object_unref (rtpgstpay->adapter);
134
135   G_OBJECT_CLASS (parent_class)->finalize (obj);
136 }
137
138 static GstFlowReturn
139 gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
140 {
141   GstFlowReturn ret;
142   guint avail;
143   guint frag_offset;
144
145   frag_offset = 0;
146   avail = gst_adapter_available (rtpgstpay->adapter);
147
148   while (avail) {
149     guint towrite;
150     guint8 *payload;
151     guint payload_len;
152     guint packet_len;
153     GstBuffer *outbuf;
154     GstRTPBuffer rtp = { NULL };
155
156
157     /* this will be the total lenght of the packet */
158     packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
159
160     /* fill one MTU or all available bytes */
161     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay));
162
163     /* this is the payload length */
164     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
165
166     /* create buffer to hold the payload */
167     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
168
169     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
170     payload = gst_rtp_buffer_get_payload (&rtp);
171
172     GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
173         frag_offset);
174
175     /*
176      *  0                   1                   2                   3
177      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
178      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
179      * |C| CV  |D|0|0|0|     ETYPE     |  MBZ                          |
180      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
181      * |                          Frag_offset                          |
182      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
183      */
184     payload[0] = rtpgstpay->flags;
185     payload[1] = rtpgstpay->etype;
186     payload[2] = payload[3] = 0;
187     payload[4] = frag_offset >> 24;
188     payload[5] = frag_offset >> 16;
189     payload[6] = frag_offset >> 8;
190     payload[7] = frag_offset & 0xff;
191
192     payload += 8;
193     payload_len -= 8;
194
195     GST_DEBUG_OBJECT (rtpgstpay, "copy %u bytes from adapter", payload_len);
196
197     gst_adapter_copy (rtpgstpay->adapter, payload, 0, payload_len);
198     gst_adapter_flush (rtpgstpay->adapter, payload_len);
199
200     frag_offset += payload_len;
201     avail -= payload_len;
202
203     if (avail == 0)
204       gst_rtp_buffer_set_marker (&rtp, TRUE);
205
206     gst_rtp_buffer_unmap (&rtp);
207
208     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
209
210     ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpgstpay), outbuf);
211     if (ret != GST_FLOW_OK)
212       goto push_failed;
213   }
214   rtpgstpay->flags &= 0x70;
215   rtpgstpay->etype = 0;
216
217   return GST_FLOW_OK;
218
219   /* ERRORS */
220 push_failed:
221   {
222     GST_DEBUG_OBJECT (rtpgstpay, "push failed %d (%s)", ret,
223         gst_flow_get_name (ret));
224     gst_adapter_clear (rtpgstpay->adapter);
225     rtpgstpay->flags &= 0x70;
226     rtpgstpay->etype = 0;
227     return ret;
228   }
229 }
230
231 static GstBuffer *
232 make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
233 {
234   guint plen;
235   guint8 *ptr;
236   GstBuffer *outbuf;
237   GstMapInfo map;
238
239   /* calculate length */
240   plen = 1;
241   while (size >> (7 * plen))
242     plen++;
243
244   outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
245
246   gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
247   ptr = map.data;
248
249   /* write length */
250   while (plen) {
251     plen--;
252     *ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
253   }
254   /* copy data */
255   memcpy (ptr, data, size);
256   gst_buffer_unmap (outbuf, &map);
257
258   return outbuf;
259 }
260
261 static gboolean
262 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
263 {
264   GstRtpGSTPay *rtpgstpay;
265   gboolean res;
266   gchar *capsstr, *capsenc, *capsver;
267   guint capslen;
268   GstBuffer *outbuf;
269
270   rtpgstpay = GST_RTP_GST_PAY (payload);
271
272   capsstr = gst_caps_to_string (caps);
273   capslen = strlen (capsstr);
274
275   rtpgstpay->current_CV = rtpgstpay->next_CV;
276
277   /* encode without 0 byte */
278   capsenc = g_base64_encode ((guchar *) capsstr, capslen);
279   GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
280   /* for 0 byte */
281   capslen++;
282
283   /* make a data buffer of it */
284   outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
285   g_free (capsstr);
286
287   /* store in adapter, we don't flush yet, buffer might follow */
288   rtpgstpay->flags = (1 << 7) | (rtpgstpay->current_CV << 4);
289   rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
290   gst_adapter_push (rtpgstpay->adapter, outbuf);
291
292   /* make caps for SDP */
293   capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
294   res =
295       gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
296       "capsversion", G_TYPE_STRING, capsver, NULL);
297   g_free (capsenc);
298   g_free (capsver);
299
300   return res;
301 }
302
303 static gboolean
304 gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
305 {
306   gboolean ret;
307   GstRtpGSTPay *rtpgstpay;
308   guint etype;
309
310   rtpgstpay = GST_RTP_GST_PAY (payload);
311
312   ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
313
314   switch (GST_EVENT_TYPE (event)) {
315     case GST_EVENT_TAG:
316       etype = 1;
317       break;
318     case GST_EVENT_CUSTOM_DOWNSTREAM:
319       etype = 2;
320       break;
321     case GST_EVENT_CUSTOM_BOTH:
322       etype = 2;
323       break;
324     default:
325       etype = 0;
326       GST_LOG_OBJECT (rtpgstpay, "no event for %s",
327           GST_EVENT_TYPE_NAME (event));
328       break;
329   }
330   if (etype) {
331     const GstStructure *s;
332     gchar *estr;
333     guint elen;
334     GstBuffer *outbuf;
335
336     /* make sure the adapter is flushed */
337     gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
338
339     GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
340         etype, GST_EVENT_TYPE_NAME (event));
341     s = gst_event_get_structure (event);
342
343     estr = gst_structure_to_string (s);
344     elen = strlen (estr);
345     outbuf = make_data_buffer (rtpgstpay, estr, elen);
346     g_free (estr);
347
348     rtpgstpay->etype = etype;
349     gst_adapter_push (rtpgstpay->adapter, outbuf);
350     /* flush the adapter immediately */
351     gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
352   }
353
354   return ret;
355 }
356
357 static GstFlowReturn
358 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
359     GstBuffer * buffer)
360 {
361   GstFlowReturn ret;
362   GstRtpGSTPay *rtpgstpay;
363   GstClockTime timestamp;
364
365   rtpgstpay = GST_RTP_GST_PAY (basepayload);
366
367   timestamp = GST_BUFFER_TIMESTAMP (buffer);
368
369   /* caps always from SDP for now */
370   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
371     rtpgstpay->flags |= (1 << 3);
372
373   gst_adapter_push (rtpgstpay->adapter, buffer);
374   ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
375
376   return ret;
377 }
378
379 gboolean
380 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
381 {
382   return gst_element_register (plugin, "rtpgstpay",
383       GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);
384 }