Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.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 /*
31  *  0                   1                   2                   3
32  *  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
33  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  * |C| CV  |D|0|0|0|                  MBZ                          |
35  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36  * |                          Frag_offset                          |
37  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38  *
39  * C: caps inlined flag 
40  *   When C set, first part of payload contains caps definition. Caps definition
41  *   starts with variable-length length prefix and then a string of that length.
42  *   the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
43  *   is the continuation marker and the 7 next bits the data. A continuation
44  *   marker of 1 means that the next byte contains more data. 
45  *
46  * CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
47  * D: delta unit buffer
48  *
49  *
50  */
51
52 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS_ANY);
57
58 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
60     GST_PAD_SRC,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("application/x-rtp, "
63         "media = (string) \"application\", "
64         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
65         "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
66     );
67
68 static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
69     GstCaps * caps);
70 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
71     GstBuffer * buffer);
72
73 #define gst_rtp_gst_pay_parent_class parent_class
74 G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
75
76 static void
77 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
78 {
79   GstElementClass *gstelement_class;
80   GstRTPBasePayloadClass *gstrtpbasepayload_class;
81
82   gstelement_class = (GstElementClass *) klass;
83   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
84
85   gst_element_class_add_pad_template (gstelement_class,
86       gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
87   gst_element_class_add_pad_template (gstelement_class,
88       gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
89
90   gst_element_class_set_details_simple (gstelement_class,
91       "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
92       "Payload GStreamer buffers as RTP packets",
93       "Wim Taymans <wim.taymans@gmail.com>");
94
95   gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
96   gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
97 }
98
99 static void
100 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
101 {
102 }
103
104 static gboolean
105 gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
106 {
107   gboolean res;
108   gchar *capsstr, *capsenc;
109
110   capsstr = gst_caps_to_string (caps);
111   capsenc = g_base64_encode ((guchar *) capsstr, strlen (capsstr));
112   g_free (capsstr);
113
114   gst_rtp_base_payload_set_options (payload, "application", TRUE, "X-GST",
115       90000);
116   res =
117       gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
118       NULL);
119   g_free (capsenc);
120
121   return res;
122 }
123
124 static GstFlowReturn
125 gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
126     GstBuffer * buffer)
127 {
128   GstRtpGSTPay *rtpgstpay;
129   GstMapInfo map;
130   guint8 *ptr;
131   gsize left;
132   GstBuffer *outbuf;
133   GstFlowReturn ret;
134   GstClockTime timestamp;
135   guint32 frag_offset;
136   guint flags;
137
138   rtpgstpay = GST_RTP_GST_PAY (basepayload);
139
140   gst_buffer_map (buffer, &map, GST_MAP_READ);
141   timestamp = GST_BUFFER_TIMESTAMP (buffer);
142
143   ret = GST_FLOW_OK;
144
145   /* caps always from SDP for now */
146   flags = 0;
147   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
148     flags |= (1 << 3);
149
150   /*
151    *  0                   1                   2                   3
152    *  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
153    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154    * |C| CV  |D|X|Y|Z|                  MBZ                          |
155    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156    * |                          Frag_offset                          |
157    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158    */
159   frag_offset = 0;
160   ptr = map.data;
161   left = map.size;
162
163   while (left > 0) {
164     guint towrite;
165     guint8 *payload;
166     guint payload_len;
167     guint packet_len;
168     GstRTPBuffer rtp = { NULL };
169
170     /* this will be the total lenght of the packet */
171     packet_len = gst_rtp_buffer_calc_packet_len (8 + left, 0, 0);
172
173     /* fill one MTU or all available bytes */
174     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay));
175
176     /* this is the payload length */
177     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
178
179     /* create buffer to hold the payload */
180     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
181
182     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
183     payload = gst_rtp_buffer_get_payload (&rtp);
184
185     payload[0] = flags;
186     payload[1] = 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     memcpy (payload, ptr, payload_len);
196
197     ptr += payload_len;
198     left -= payload_len;
199     frag_offset += payload_len;
200
201     if (left == 0)
202       gst_rtp_buffer_set_marker (&rtp, TRUE);
203
204     gst_rtp_buffer_unmap (&rtp);
205
206     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
207
208     ret = gst_rtp_base_payload_push (basepayload, outbuf);
209   }
210   gst_buffer_unmap (buffer, &map);
211   gst_buffer_unref (buffer);
212
213   return ret;
214 }
215
216 gboolean
217 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
218 {
219   return gst_element_register (plugin, "rtpgstpay",
220       GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);
221 }