• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24 
25 #include <gst/rtp/gstrtpbuffer.h>
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gstrtpelements.h"
30 #include "gstrtpg723depay.h"
31 
32 GST_DEBUG_CATEGORY_STATIC (rtpg723depay_debug);
33 #define GST_CAT_DEFAULT (rtpg723depay_debug)
34 
35 
36 /* references:
37  *
38  * RFC 3551 (4.5.3)
39  */
40 
41 enum
42 {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46 
47 enum
48 {
49   PROP_0
50 };
51 
52 /* input is an RTP packet
53  *
54  */
55 static GstStaticPadTemplate gst_rtp_g723_depay_sink_template =
56     GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp, "
60         "media = (string) \"audio\", "
61         "clock-rate = (int) 8000, "
62         "encoding-name = (string) \"G723\"; "
63         "application/x-rtp, "
64         "media = (string) \"audio\", "
65         "payload = (int) " GST_RTP_PAYLOAD_G723_STRING ", "
66         "clock-rate = (int) 8000")
67     );
68 
69 static GstStaticPadTemplate gst_rtp_g723_depay_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("audio/G723, " "channels = (int) 1," "rate = (int) 8000")
74     );
75 
76 static gboolean gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload,
77     GstCaps * caps);
78 static GstBuffer *gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload,
79     GstRTPBuffer * rtp);
80 
81 #define gst_rtp_g723_depay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpG723Depay, gst_rtp_g723_depay,
83     GST_TYPE_RTP_BASE_DEPAYLOAD);
84 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpg723depay, "rtpg723depay",
85     GST_RANK_SECONDARY, GST_TYPE_RTP_G723_DEPAY, rtp_element_init (plugin));
86 
87 static void
gst_rtp_g723_depay_class_init(GstRtpG723DepayClass * klass)88 gst_rtp_g723_depay_class_init (GstRtpG723DepayClass * klass)
89 {
90   GstElementClass *gstelement_class;
91   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
92 
93   GST_DEBUG_CATEGORY_INIT (rtpg723depay_debug, "rtpg723depay", 0,
94       "G.723 RTP Depayloader");
95 
96   gstelement_class = (GstElementClass *) klass;
97   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
98 
99   gst_element_class_add_static_pad_template (gstelement_class,
100       &gst_rtp_g723_depay_src_template);
101   gst_element_class_add_static_pad_template (gstelement_class,
102       &gst_rtp_g723_depay_sink_template);
103 
104   gst_element_class_set_static_metadata (gstelement_class,
105       "RTP G.723 depayloader", "Codec/Depayloader/Network/RTP",
106       "Extracts G.723 audio from RTP packets (RFC 3551)",
107       "Wim Taymans <wim.taymans@gmail.com>");
108 
109   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g723_depay_process;
110   gstrtpbasedepayload_class->set_caps = gst_rtp_g723_depay_setcaps;
111 }
112 
113 static void
gst_rtp_g723_depay_init(GstRtpG723Depay * rtpg723depay)114 gst_rtp_g723_depay_init (GstRtpG723Depay * rtpg723depay)
115 {
116   GstRTPBaseDepayload *depayload;
117 
118   depayload = GST_RTP_BASE_DEPAYLOAD (rtpg723depay);
119 
120   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
121 }
122 
123 static gboolean
gst_rtp_g723_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)124 gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
125 {
126   GstStructure *structure;
127   GstCaps *srccaps;
128   GstRtpG723Depay *rtpg723depay;
129   const gchar *params;
130   gint clock_rate, channels;
131   gboolean ret;
132 
133   rtpg723depay = GST_RTP_G723_DEPAY (depayload);
134 
135   structure = gst_caps_get_structure (caps, 0);
136 
137   if (!(params = gst_structure_get_string (structure, "encoding-params")))
138     channels = 1;
139   else {
140     channels = atoi (params);
141   }
142 
143   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
144     clock_rate = 8000;
145 
146   if (channels != 1)
147     goto wrong_channels;
148 
149   if (clock_rate != 8000)
150     goto wrong_clock_rate;
151 
152   depayload->clock_rate = clock_rate;
153 
154   srccaps = gst_caps_new_simple ("audio/G723",
155       "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, clock_rate, NULL);
156   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
157   gst_caps_unref (srccaps);
158 
159   return ret;
160 
161   /* ERRORS */
162 wrong_channels:
163   {
164     GST_DEBUG_OBJECT (rtpg723depay, "expected 1 channel, got %d", channels);
165     return FALSE;
166   }
167 wrong_clock_rate:
168   {
169     GST_DEBUG_OBJECT (rtpg723depay, "expected 8000 clock-rate, got %d",
170         clock_rate);
171     return FALSE;
172   }
173 }
174 
175 
176 static GstBuffer *
gst_rtp_g723_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)177 gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
178 {
179   GstRtpG723Depay *rtpg723depay;
180   GstBuffer *outbuf = NULL;
181   gint payload_len;
182   gboolean marker;
183 
184   rtpg723depay = GST_RTP_G723_DEPAY (depayload);
185 
186   payload_len = gst_rtp_buffer_get_payload_len (rtp);
187 
188   /* At least 4 bytes */
189   if (payload_len < 4)
190     goto too_small;
191 
192   GST_LOG_OBJECT (rtpg723depay, "payload len %d", payload_len);
193 
194   outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
195   marker = gst_rtp_buffer_get_marker (rtp);
196 
197   if (marker) {
198     /* marker bit starts talkspurt */
199     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
200   }
201 
202   GST_LOG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
203       gst_buffer_get_size (outbuf));
204 
205   return outbuf;
206 
207   /* ERRORS */
208 too_small:
209   {
210     GST_ELEMENT_WARNING (rtpg723depay, STREAM, DECODE,
211         (NULL), ("G723 RTP payload too small (%d)", payload_len));
212     goto bad_packet;
213   }
214 bad_packet:
215   {
216     /* no fatal error */
217     return NULL;
218   }
219 }
220