rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpg723pay.c
1 /* GStreamer
2  * Copyright (C) <2007> Nokia Corporation
3  * Copyright (C) <2007> Collabora Ltd
4  *  @author: Olivier Crete <olivier.crete@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include <gst/base/gstadapter.h>
29 #include <gst/audio/audio.h>
30
31 #include "gstrtpg723pay.h"
32 #include "gstrtputils.h"
33
34 #define G723_FRAME_DURATION (30 * GST_MSECOND)
35
36 static gboolean gst_rtp_g723_pay_set_caps (GstRTPBasePayload * payload,
37     GstCaps * caps);
38 static GstFlowReturn gst_rtp_g723_pay_handle_buffer (GstRTPBasePayload *
39     payload, GstBuffer * buf);
40
41 static GstStaticPadTemplate gst_rtp_g723_pay_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("audio/G723, "     /* according to RFC 3551 */
46         "channels = (int) 1, " "rate = (int) 8000")
47     );
48
49 static GstStaticPadTemplate gst_rtp_g723_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) \"audio\", "
55         "payload = (int) " GST_RTP_PAYLOAD_G723_STRING ", "
56         "clock-rate = (int) 8000, "
57         "encoding-name = (string) \"G723\"; "
58         "application/x-rtp, "
59         "media = (string) \"audio\", "
60         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
61         "clock-rate = (int) 8000, " "encoding-name = (string) \"G723\"")
62     );
63
64 static void gst_rtp_g723_pay_finalize (GObject * object);
65
66 static GstStateChangeReturn gst_rtp_g723_pay_change_state (GstElement * element,
67     GstStateChange transition);
68
69 #define gst_rtp_g723_pay_parent_class parent_class
70 G_DEFINE_TYPE (GstRTPG723Pay, gst_rtp_g723_pay, GST_TYPE_RTP_BASE_PAYLOAD);
71
72 static void
73 gst_rtp_g723_pay_class_init (GstRTPG723PayClass * klass)
74 {
75   GObjectClass *gobject_class;
76   GstElementClass *gstelement_class;
77   GstRTPBasePayloadClass *payload_class;
78
79   gobject_class = (GObjectClass *) klass;
80   gstelement_class = (GstElementClass *) klass;
81   payload_class = (GstRTPBasePayloadClass *) klass;
82
83   gobject_class->finalize = gst_rtp_g723_pay_finalize;
84
85   gstelement_class->change_state = gst_rtp_g723_pay_change_state;
86
87   gst_element_class_add_static_pad_template (gstelement_class,
88       &gst_rtp_g723_pay_sink_template);
89   gst_element_class_add_static_pad_template (gstelement_class,
90       &gst_rtp_g723_pay_src_template);
91
92   gst_element_class_set_static_metadata (gstelement_class,
93       "RTP G.723 payloader", "Codec/Payloader/Network/RTP",
94       "Packetize G.723 audio into RTP packets",
95       "Wim Taymans <wim.taymans@gmail.com>");
96
97   payload_class->set_caps = gst_rtp_g723_pay_set_caps;
98   payload_class->handle_buffer = gst_rtp_g723_pay_handle_buffer;
99 }
100
101 static void
102 gst_rtp_g723_pay_init (GstRTPG723Pay * pay)
103 {
104   GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (pay);
105
106   pay->adapter = gst_adapter_new ();
107
108   payload->pt = GST_RTP_PAYLOAD_G723;
109 }
110
111 static void
112 gst_rtp_g723_pay_finalize (GObject * object)
113 {
114   GstRTPG723Pay *pay;
115
116   pay = GST_RTP_G723_PAY (object);
117
118   g_object_unref (pay->adapter);
119   pay->adapter = NULL;
120
121   G_OBJECT_CLASS (parent_class)->finalize (object);
122 }
123
124
125 static gboolean
126 gst_rtp_g723_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
127 {
128   gboolean res;
129
130   gst_rtp_base_payload_set_options (payload, "audio",
131       payload->pt != GST_RTP_PAYLOAD_G723, "G723", 8000);
132   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
133
134   return res;
135 }
136
137 static GstFlowReturn
138 gst_rtp_g723_pay_flush (GstRTPG723Pay * pay)
139 {
140   GstBuffer *outbuf, *payload_buf;
141   GstFlowReturn ret;
142   guint avail;
143   GstRTPBuffer rtp = { NULL };
144
145   avail = gst_adapter_available (pay->adapter);
146
147   outbuf =
148       gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD (pay),
149       0, 0, 0);
150   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
151
152   GST_BUFFER_PTS (outbuf) = pay->timestamp;
153   GST_BUFFER_DURATION (outbuf) = pay->duration;
154
155   /* copy G723 data as payload */
156   payload_buf = gst_adapter_take_buffer_fast (pay->adapter, avail);
157
158   pay->timestamp = GST_CLOCK_TIME_NONE;
159   pay->duration = 0;
160
161   /* set discont and marker */
162   if (pay->discont) {
163     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
164     gst_rtp_buffer_set_marker (&rtp, TRUE);
165     pay->discont = FALSE;
166   }
167   gst_rtp_buffer_unmap (&rtp);
168   gst_rtp_copy_audio_meta (pay, outbuf, payload_buf);
169
170   outbuf = gst_buffer_append (outbuf, payload_buf);
171
172   ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (pay), outbuf);
173
174   return ret;
175 }
176
177 /* 00    high-rate speech (6.3 kb/s)            24
178  * 01    low-rate speech  (5.3 kb/s)            20
179  * 10    SID frame                               4
180  * 11    reserved                                0  */
181 static const guint size_tab[4] = {
182   24, 20, 4, 0
183 };
184
185 static GstFlowReturn
186 gst_rtp_g723_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buf)
187 {
188   GstFlowReturn ret = GST_FLOW_OK;
189   GstMapInfo map;
190   guint8 HDR;
191   GstRTPG723Pay *pay;
192   GstClockTime packet_dur, timestamp;
193   guint payload_len, packet_len;
194
195   pay = GST_RTP_G723_PAY (payload);
196
197   gst_buffer_map (buf, &map, GST_MAP_READ);
198   timestamp = GST_BUFFER_PTS (buf);
199
200   if (GST_BUFFER_IS_DISCONT (buf)) {
201     /* flush everything on discont */
202     gst_adapter_clear (pay->adapter);
203     pay->timestamp = GST_CLOCK_TIME_NONE;
204     pay->duration = 0;
205     pay->discont = TRUE;
206   }
207
208   /* should be one of these sizes */
209   if (map.size != 4 && map.size != 20 && map.size != 24)
210     goto invalid_size;
211
212   /* check size by looking at the header bits */
213   HDR = map.data[0] & 0x3;
214   if (size_tab[HDR] != map.size)
215     goto wrong_size;
216
217   /* calculate packet size and duration */
218   payload_len = gst_adapter_available (pay->adapter) + map.size;
219   packet_dur = pay->duration + G723_FRAME_DURATION;
220   packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
221
222   if (gst_rtp_base_payload_is_filled (payload, packet_len, packet_dur)) {
223     /* size or duration would overflow the packet, flush the queued data */
224     ret = gst_rtp_g723_pay_flush (pay);
225   }
226
227   /* update timestamp, we keep the timestamp for the first packet in the adapter
228    * but are able to calculate it from next packets. */
229   if (timestamp != GST_CLOCK_TIME_NONE && pay->timestamp == GST_CLOCK_TIME_NONE) {
230     if (timestamp > pay->duration)
231       pay->timestamp = timestamp - pay->duration;
232     else
233       pay->timestamp = 0;
234   }
235   gst_buffer_unmap (buf, &map);
236
237   /* add packet to the queue */
238   gst_adapter_push (pay->adapter, buf);
239   pay->duration = packet_dur;
240
241   /* check if we can flush now */
242   if (pay->duration >= payload->min_ptime) {
243     ret = gst_rtp_g723_pay_flush (pay);
244   }
245
246   return ret;
247
248   /* WARNINGS */
249 invalid_size:
250   {
251     GST_ELEMENT_WARNING (pay, STREAM, WRONG_TYPE,
252         ("Invalid input buffer size"),
253         ("Input size should be 4, 20 or 24, got %" G_GSIZE_FORMAT, map.size));
254     gst_buffer_unmap (buf, &map);
255     gst_buffer_unref (buf);
256     return GST_FLOW_OK;
257   }
258 wrong_size:
259   {
260     GST_ELEMENT_WARNING (pay, STREAM, WRONG_TYPE,
261         ("Wrong input buffer size"),
262         ("Expected input buffer size %u but got %" G_GSIZE_FORMAT,
263             size_tab[HDR], map.size));
264     gst_buffer_unmap (buf, &map);
265     gst_buffer_unref (buf);
266     return GST_FLOW_OK;
267   }
268 }
269
270 static GstStateChangeReturn
271 gst_rtp_g723_pay_change_state (GstElement * element, GstStateChange transition)
272 {
273   GstStateChangeReturn ret;
274   GstRTPG723Pay *pay;
275
276   pay = GST_RTP_G723_PAY (element);
277
278   switch (transition) {
279     case GST_STATE_CHANGE_READY_TO_PAUSED:
280       gst_adapter_clear (pay->adapter);
281       pay->timestamp = GST_CLOCK_TIME_NONE;
282       pay->duration = 0;
283       pay->discont = TRUE;
284       break;
285     default:
286       break;
287   }
288
289   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
290
291   switch (transition) {
292     case GST_STATE_CHANGE_PAUSED_TO_READY:
293       gst_adapter_clear (pay->adapter);
294       break;
295     default:
296       break;
297   }
298
299   return ret;
300 }
301
302 /*Plugin init functions*/
303 gboolean
304 gst_rtp_g723_pay_plugin_init (GstPlugin * plugin)
305 {
306   return gst_element_register (plugin, "rtpg723pay", GST_RANK_SECONDARY,
307       gst_rtp_g723_pay_get_type ());
308 }