Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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|X|Y|Z|                  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  * X: media 1 flag
49  * Y: media 2 flag
50  * Z: media 3 flag
51  *
52  *
53  */
54
55 static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS_ANY);
60
61 static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("application/x-rtp, "
66         "media = (string) \"application\", "
67         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
68         "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
69     );
70
71 static gboolean gst_rtp_gst_pay_setcaps (GstBaseRTPPayload * payload,
72     GstCaps * caps);
73 static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstBaseRTPPayload * payload,
74     GstBuffer * buffer);
75
76 GST_BOILERPLATE (GstRtpGSTPay, gst_rtp_gst_pay, GstBaseRTPPayload,
77     GST_TYPE_BASE_RTP_PAYLOAD)
78
79      static void gst_rtp_gst_pay_base_init (gpointer klass)
80 {
81   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
82
83   gst_element_class_add_static_pad_template (element_class,
84       &gst_rtp_gst_pay_src_template);
85   gst_element_class_add_static_pad_template (element_class,
86       &gst_rtp_gst_pay_sink_template);
87
88   gst_element_class_set_details_simple (element_class,
89       "RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
90       "Payload GStreamer buffers as RTP packets",
91       "Wim Taymans <wim.taymans@gmail.com>");
92 }
93
94 static void
95 gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
96 {
97   GstBaseRTPPayloadClass *gstbasertppayload_class;
98
99   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
100
101   gstbasertppayload_class->set_caps = gst_rtp_gst_pay_setcaps;
102   gstbasertppayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
103 }
104
105 static void
106 gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay, GstRtpGSTPayClass * klass)
107 {
108 }
109
110 static gboolean
111 gst_rtp_gst_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
112 {
113   gboolean res;
114   gchar *capsstr, *capsenc;
115
116   capsstr = gst_caps_to_string (caps);
117   capsenc = g_base64_encode ((guchar *) capsstr, strlen (capsstr));
118   g_free (capsstr);
119
120   gst_basertppayload_set_options (payload, "application", TRUE, "X-GST", 90000);
121   res =
122       gst_basertppayload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
123       NULL);
124   g_free (capsenc);
125
126   return res;
127 }
128
129 static GstFlowReturn
130 gst_rtp_gst_pay_handle_buffer (GstBaseRTPPayload * basepayload,
131     GstBuffer * buffer)
132 {
133   GstRtpGSTPay *rtpgstpay;
134   guint8 *data;
135   guint size;
136   GstBuffer *outbuf;
137   GstFlowReturn ret;
138   GstClockTime timestamp;
139   guint32 frag_offset;
140   guint flags;
141
142   rtpgstpay = GST_RTP_GST_PAY (basepayload);
143
144   size = GST_BUFFER_SIZE (buffer);
145   data = GST_BUFFER_DATA (buffer);
146   timestamp = GST_BUFFER_TIMESTAMP (buffer);
147
148   ret = GST_FLOW_OK;
149
150   /* caps always from SDP for now */
151   flags = 0;
152   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
153     flags |= (1 << 3);
154   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MEDIA1))
155     flags |= (1 << 2);
156   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MEDIA2))
157     flags |= (1 << 1);
158   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MEDIA3))
159     flags |= (1 << 0);
160
161   /*
162    *  0                   1                   2                   3
163    *  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
164    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165    * |C| CV  |D|X|Y|Z|                  MBZ                          |
166    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167    * |                          Frag_offset                          |
168    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169    */
170   frag_offset = 0;
171
172   while (size > 0) {
173     guint towrite;
174     guint8 *payload;
175     guint payload_len;
176     guint packet_len;
177
178     /* this will be the total lenght of the packet */
179     packet_len = gst_rtp_buffer_calc_packet_len (8 + size, 0, 0);
180
181     /* fill one MTU or all available bytes */
182     towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpgstpay));
183
184     /* this is the payload length */
185     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
186
187     /* create buffer to hold the payload */
188     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
189     payload = gst_rtp_buffer_get_payload (outbuf);
190
191     payload[0] = flags;
192     payload[1] = payload[2] = payload[3] = 0;
193     payload[4] = frag_offset >> 24;
194     payload[5] = frag_offset >> 16;
195     payload[6] = frag_offset >> 8;
196     payload[7] = frag_offset & 0xff;
197
198     payload += 8;
199     payload_len -= 8;
200
201     memcpy (payload, data, payload_len);
202
203     data += payload_len;
204     size -= payload_len;
205     frag_offset += payload_len;
206
207     if (size == 0)
208       gst_rtp_buffer_set_marker (outbuf, TRUE);
209
210     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
211
212     ret = gst_basertppayload_push (basepayload, outbuf);
213   }
214   gst_buffer_unref (buffer);
215
216   return ret;
217 }
218
219 gboolean
220 gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
221 {
222   return gst_element_register (plugin, "rtpgstpay",
223       GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);
224 }