• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gstrtpelements.h"
32 #include "gstrtpg723pay.h"
33 #include "gstrtputils.h"
34 
35 #define G723_FRAME_DURATION (30 * GST_MSECOND)
36 
37 static gboolean gst_rtp_g723_pay_set_caps (GstRTPBasePayload * payload,
38     GstCaps * caps);
39 static GstFlowReturn gst_rtp_g723_pay_handle_buffer (GstRTPBasePayload *
40     payload, GstBuffer * buf);
41 
42 static GstStaticPadTemplate gst_rtp_g723_pay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("audio/G723, "     /* according to RFC 3551 */
47         "channels = (int) 1, " "rate = (int) 8000")
48     );
49 
50 static GstStaticPadTemplate gst_rtp_g723_pay_src_template =
51     GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-rtp, "
55         "media = (string) \"audio\", "
56         "payload = (int) " GST_RTP_PAYLOAD_G723_STRING ", "
57         "clock-rate = (int) 8000, "
58         "encoding-name = (string) \"G723\"; "
59         "application/x-rtp, "
60         "media = (string) \"audio\", "
61         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
62         "clock-rate = (int) 8000, " "encoding-name = (string) \"G723\"")
63     );
64 
65 static void gst_rtp_g723_pay_finalize (GObject * object);
66 
67 static GstStateChangeReturn gst_rtp_g723_pay_change_state (GstElement * element,
68     GstStateChange transition);
69 
70 #define gst_rtp_g723_pay_parent_class parent_class
71 G_DEFINE_TYPE (GstRTPG723Pay, gst_rtp_g723_pay, GST_TYPE_RTP_BASE_PAYLOAD);
72 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpg723pay, "rtpg723pay",
73     GST_RANK_SECONDARY, GST_TYPE_RTP_G723_PAY, rtp_element_init (plugin));
74 
75 static void
gst_rtp_g723_pay_class_init(GstRTPG723PayClass * klass)76 gst_rtp_g723_pay_class_init (GstRTPG723PayClass * klass)
77 {
78   GObjectClass *gobject_class;
79   GstElementClass *gstelement_class;
80   GstRTPBasePayloadClass *payload_class;
81 
82   gobject_class = (GObjectClass *) klass;
83   gstelement_class = (GstElementClass *) klass;
84   payload_class = (GstRTPBasePayloadClass *) klass;
85 
86   gobject_class->finalize = gst_rtp_g723_pay_finalize;
87 
88   gstelement_class->change_state = gst_rtp_g723_pay_change_state;
89 
90   gst_element_class_add_static_pad_template (gstelement_class,
91       &gst_rtp_g723_pay_sink_template);
92   gst_element_class_add_static_pad_template (gstelement_class,
93       &gst_rtp_g723_pay_src_template);
94 
95   gst_element_class_set_static_metadata (gstelement_class,
96       "RTP G.723 payloader", "Codec/Payloader/Network/RTP",
97       "Packetize G.723 audio into RTP packets",
98       "Wim Taymans <wim.taymans@gmail.com>");
99 
100   payload_class->set_caps = gst_rtp_g723_pay_set_caps;
101   payload_class->handle_buffer = gst_rtp_g723_pay_handle_buffer;
102 }
103 
104 static void
gst_rtp_g723_pay_init(GstRTPG723Pay * pay)105 gst_rtp_g723_pay_init (GstRTPG723Pay * pay)
106 {
107   GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (pay);
108 
109   pay->adapter = gst_adapter_new ();
110 
111   payload->pt = GST_RTP_PAYLOAD_G723;
112 }
113 
114 static void
gst_rtp_g723_pay_finalize(GObject * object)115 gst_rtp_g723_pay_finalize (GObject * object)
116 {
117   GstRTPG723Pay *pay;
118 
119   pay = GST_RTP_G723_PAY (object);
120 
121   g_object_unref (pay->adapter);
122   pay->adapter = NULL;
123 
124   G_OBJECT_CLASS (parent_class)->finalize (object);
125 }
126 
127 
128 static gboolean
gst_rtp_g723_pay_set_caps(GstRTPBasePayload * payload,GstCaps * caps)129 gst_rtp_g723_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
130 {
131   gboolean res;
132 
133   gst_rtp_base_payload_set_options (payload, "audio",
134       payload->pt != GST_RTP_PAYLOAD_G723, "G723", 8000);
135   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
136 
137   return res;
138 }
139 
140 static GstFlowReturn
gst_rtp_g723_pay_flush(GstRTPG723Pay * pay)141 gst_rtp_g723_pay_flush (GstRTPG723Pay * pay)
142 {
143   GstBuffer *outbuf, *payload_buf;
144   GstFlowReturn ret;
145   guint avail;
146   GstRTPBuffer rtp = { NULL };
147 
148   avail = gst_adapter_available (pay->adapter);
149 
150   outbuf =
151       gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD (pay),
152       0, 0, 0);
153   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
154 
155   GST_BUFFER_PTS (outbuf) = pay->timestamp;
156   GST_BUFFER_DURATION (outbuf) = pay->duration;
157 
158   /* copy G723 data as payload */
159   payload_buf = gst_adapter_take_buffer_fast (pay->adapter, avail);
160 
161   pay->timestamp = GST_CLOCK_TIME_NONE;
162   pay->duration = 0;
163 
164   /* set discont and marker */
165   if (pay->discont) {
166     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
167     gst_rtp_buffer_set_marker (&rtp, TRUE);
168     pay->discont = FALSE;
169   }
170   gst_rtp_buffer_unmap (&rtp);
171   gst_rtp_copy_audio_meta (pay, outbuf, payload_buf);
172 
173   outbuf = gst_buffer_append (outbuf, payload_buf);
174 
175   ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (pay), outbuf);
176 
177   return ret;
178 }
179 
180 /* 00    high-rate speech (6.3 kb/s)            24
181  * 01    low-rate speech  (5.3 kb/s)            20
182  * 10    SID frame                               4
183  * 11    reserved                                0  */
184 static const guint size_tab[4] = {
185   24, 20, 4, 0
186 };
187 
188 static GstFlowReturn
gst_rtp_g723_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buf)189 gst_rtp_g723_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buf)
190 {
191   GstFlowReturn ret = GST_FLOW_OK;
192   GstMapInfo map;
193   guint8 HDR;
194   GstRTPG723Pay *pay;
195   GstClockTime packet_dur, timestamp;
196   guint payload_len, packet_len;
197 
198   pay = GST_RTP_G723_PAY (payload);
199 
200   gst_buffer_map (buf, &map, GST_MAP_READ);
201   timestamp = GST_BUFFER_PTS (buf);
202 
203   if (GST_BUFFER_IS_DISCONT (buf)) {
204     /* flush everything on discont */
205     gst_adapter_clear (pay->adapter);
206     pay->timestamp = GST_CLOCK_TIME_NONE;
207     pay->duration = 0;
208     pay->discont = TRUE;
209   }
210 
211   /* should be one of these sizes */
212   if (map.size != 4 && map.size != 20 && map.size != 24)
213     goto invalid_size;
214 
215   /* check size by looking at the header bits */
216   HDR = map.data[0] & 0x3;
217   if (size_tab[HDR] != map.size)
218     goto wrong_size;
219 
220   /* calculate packet size and duration */
221   payload_len = gst_adapter_available (pay->adapter) + map.size;
222   packet_dur = pay->duration + G723_FRAME_DURATION;
223   packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
224 
225   if (gst_rtp_base_payload_is_filled (payload, packet_len, packet_dur)) {
226     /* size or duration would overflow the packet, flush the queued data */
227     ret = gst_rtp_g723_pay_flush (pay);
228   }
229 
230   /* update timestamp, we keep the timestamp for the first packet in the adapter
231    * but are able to calculate it from next packets. */
232   if (timestamp != GST_CLOCK_TIME_NONE && pay->timestamp == GST_CLOCK_TIME_NONE) {
233     if (timestamp > pay->duration)
234       pay->timestamp = timestamp - pay->duration;
235     else
236       pay->timestamp = 0;
237   }
238   gst_buffer_unmap (buf, &map);
239 
240   /* add packet to the queue */
241   gst_adapter_push (pay->adapter, buf);
242   pay->duration = packet_dur;
243 
244   /* check if we can flush now */
245   if (pay->duration >= payload->min_ptime) {
246     ret = gst_rtp_g723_pay_flush (pay);
247   }
248 
249   return ret;
250 
251   /* WARNINGS */
252 invalid_size:
253   {
254     GST_ELEMENT_WARNING (pay, STREAM, WRONG_TYPE,
255         ("Invalid input buffer size"),
256         ("Input size should be 4, 20 or 24, got %" G_GSIZE_FORMAT, map.size));
257     gst_buffer_unmap (buf, &map);
258     gst_buffer_unref (buf);
259     return GST_FLOW_OK;
260   }
261 wrong_size:
262   {
263     GST_ELEMENT_WARNING (pay, STREAM, WRONG_TYPE,
264         ("Wrong input buffer size"),
265         ("Expected input buffer size %u but got %" G_GSIZE_FORMAT,
266             size_tab[HDR], map.size));
267     gst_buffer_unmap (buf, &map);
268     gst_buffer_unref (buf);
269     return GST_FLOW_OK;
270   }
271 }
272 
273 static GstStateChangeReturn
gst_rtp_g723_pay_change_state(GstElement * element,GstStateChange transition)274 gst_rtp_g723_pay_change_state (GstElement * element, GstStateChange transition)
275 {
276   GstStateChangeReturn ret;
277   GstRTPG723Pay *pay;
278 
279   pay = GST_RTP_G723_PAY (element);
280 
281   switch (transition) {
282     case GST_STATE_CHANGE_READY_TO_PAUSED:
283       gst_adapter_clear (pay->adapter);
284       pay->timestamp = GST_CLOCK_TIME_NONE;
285       pay->duration = 0;
286       pay->discont = TRUE;
287       break;
288     default:
289       break;
290   }
291 
292   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
293 
294   switch (transition) {
295     case GST_STATE_CHANGE_PAUSED_TO_READY:
296       gst_adapter_clear (pay->adapter);
297       break;
298     default:
299       break;
300   }
301 
302   return ret;
303 }
304