1 /* GStreamer
2 * Copyright (C) <2006> Philippe Khalaf <burger@speedy.org>
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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include "gstrtpilbcpay.h"
27
28 GST_DEBUG_CATEGORY_STATIC (rtpilbcpay_debug);
29 #define GST_CAT_DEFAULT (rtpilbcpay_debug)
30
31 static GstStaticPadTemplate gst_rtp_ilbc_pay_sink_template =
32 GST_STATIC_PAD_TEMPLATE ("sink",
33 GST_PAD_SINK,
34 GST_PAD_ALWAYS,
35 GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) {20, 30}")
36 );
37
38 static GstStaticPadTemplate gst_rtp_ilbc_pay_src_template =
39 GST_STATIC_PAD_TEMPLATE ("src",
40 GST_PAD_SRC,
41 GST_PAD_ALWAYS,
42 GST_STATIC_CAPS ("application/x-rtp, "
43 "media = (string) \"audio\", "
44 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
45 "clock-rate = (int) 8000, "
46 "encoding-name = (string) \"ILBC\", "
47 "mode = (string) { \"20\", \"30\" }")
48 );
49
50
51 static GstCaps *gst_rtp_ilbc_pay_sink_getcaps (GstRTPBasePayload * payload,
52 GstPad * pad, GstCaps * filter);
53 static gboolean gst_rtp_ilbc_pay_sink_setcaps (GstRTPBasePayload * payload,
54 GstCaps * caps);
55
56 #define gst_rtp_ilbc_pay_parent_class parent_class
57 G_DEFINE_TYPE (GstRTPILBCPay, gst_rtp_ilbc_pay,
58 GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
59
60 static void
gst_rtp_ilbc_pay_class_init(GstRTPILBCPayClass * klass)61 gst_rtp_ilbc_pay_class_init (GstRTPILBCPayClass * klass)
62 {
63 GstElementClass *gstelement_class;
64 GstRTPBasePayloadClass *gstrtpbasepayload_class;
65
66 GST_DEBUG_CATEGORY_INIT (rtpilbcpay_debug, "rtpilbcpay", 0,
67 "iLBC audio RTP payloader");
68
69 gstelement_class = (GstElementClass *) klass;
70 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
71
72 gst_element_class_add_static_pad_template (gstelement_class,
73 &gst_rtp_ilbc_pay_sink_template);
74 gst_element_class_add_static_pad_template (gstelement_class,
75 &gst_rtp_ilbc_pay_src_template);
76
77 gst_element_class_set_static_metadata (gstelement_class, "RTP iLBC Payloader",
78 "Codec/Payloader/Network/RTP",
79 "Packetize iLBC audio streams into RTP packets",
80 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>");
81
82 gstrtpbasepayload_class->set_caps = gst_rtp_ilbc_pay_sink_setcaps;
83 gstrtpbasepayload_class->get_caps = gst_rtp_ilbc_pay_sink_getcaps;
84 }
85
86 static void
gst_rtp_ilbc_pay_init(GstRTPILBCPay * rtpilbcpay)87 gst_rtp_ilbc_pay_init (GstRTPILBCPay * rtpilbcpay)
88 {
89 GstRTPBasePayload *rtpbasepayload;
90 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
91
92 rtpbasepayload = GST_RTP_BASE_PAYLOAD (rtpilbcpay);
93 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpilbcpay);
94
95 /* we don't set the payload type, it should be set by the application using
96 * the pt property or the default 96 will be used */
97 rtpbasepayload->clock_rate = 8000;
98
99 rtpilbcpay->mode = -1;
100
101 /* tell rtpbaseaudiopayload that this is a frame based codec */
102 gst_rtp_base_audio_payload_set_frame_based (rtpbaseaudiopayload);
103 }
104
105 static gboolean
gst_rtp_ilbc_pay_sink_setcaps(GstRTPBasePayload * rtpbasepayload,GstCaps * caps)106 gst_rtp_ilbc_pay_sink_setcaps (GstRTPBasePayload * rtpbasepayload,
107 GstCaps * caps)
108 {
109 GstRTPILBCPay *rtpilbcpay;
110 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
111 gboolean ret;
112 gint mode;
113 gchar *mode_str;
114 GstStructure *structure;
115 const char *payload_name;
116
117 rtpilbcpay = GST_RTP_ILBC_PAY (rtpbasepayload);
118 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpbasepayload);
119
120 structure = gst_caps_get_structure (caps, 0);
121
122 payload_name = gst_structure_get_name (structure);
123 if (g_ascii_strcasecmp ("audio/x-iLBC", payload_name))
124 goto wrong_caps;
125
126 if (!gst_structure_get_int (structure, "mode", &mode))
127 goto no_mode;
128
129 if (mode != 20 && mode != 30)
130 goto wrong_mode;
131
132 gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "ILBC",
133 8000);
134 /* set options for this frame based audio codec */
135 gst_rtp_base_audio_payload_set_frame_options (rtpbaseaudiopayload,
136 mode, mode == 30 ? 50 : 38);
137
138 mode_str = g_strdup_printf ("%d", mode);
139 ret =
140 gst_rtp_base_payload_set_outcaps (rtpbasepayload, "mode", G_TYPE_STRING,
141 mode_str, NULL);
142 g_free (mode_str);
143
144 if (mode != rtpilbcpay->mode && rtpilbcpay->mode != -1)
145 goto mode_changed;
146
147 rtpilbcpay->mode = mode;
148
149 return ret;
150
151 /* ERRORS */
152 wrong_caps:
153 {
154 GST_ERROR_OBJECT (rtpilbcpay, "expected audio/x-iLBC, received %s",
155 payload_name);
156 return FALSE;
157 }
158 no_mode:
159 {
160 GST_ERROR_OBJECT (rtpilbcpay, "did not receive a mode");
161 return FALSE;
162 }
163 wrong_mode:
164 {
165 GST_ERROR_OBJECT (rtpilbcpay, "mode must be 20 or 30, received %d", mode);
166 return FALSE;
167 }
168 mode_changed:
169 {
170 GST_ERROR_OBJECT (rtpilbcpay, "Mode has changed from %d to %d! "
171 "Mode cannot change while streaming", rtpilbcpay->mode, mode);
172 return FALSE;
173 }
174 }
175
176 /* we return the padtemplate caps with the mode field fixated to a value if we
177 * can */
178 static GstCaps *
gst_rtp_ilbc_pay_sink_getcaps(GstRTPBasePayload * rtppayload,GstPad * pad,GstCaps * filter)179 gst_rtp_ilbc_pay_sink_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
180 GstCaps * filter)
181 {
182 GstCaps *otherpadcaps;
183 GstCaps *caps;
184
185 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
186 caps = gst_pad_get_pad_template_caps (pad);
187
188 if (otherpadcaps) {
189 if (!gst_caps_is_empty (otherpadcaps)) {
190 GstStructure *structure;
191 const gchar *mode_str;
192 gint mode;
193
194 structure = gst_caps_get_structure (otherpadcaps, 0);
195
196 /* parse mode, if we can */
197 mode_str = gst_structure_get_string (structure, "mode");
198 if (mode_str) {
199 mode = strtol (mode_str, NULL, 10);
200 if (mode == 20 || mode == 30) {
201 caps = gst_caps_make_writable (caps);
202 structure = gst_caps_get_structure (caps, 0);
203 gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
204 }
205 }
206 }
207 gst_caps_unref (otherpadcaps);
208 }
209
210 if (filter) {
211 GstCaps *tmp;
212
213 GST_DEBUG_OBJECT (rtppayload, "Intersect %" GST_PTR_FORMAT " and filter %"
214 GST_PTR_FORMAT, caps, filter);
215 tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
216 gst_caps_unref (caps);
217 caps = tmp;
218 }
219
220 return caps;
221 }
222
223 gboolean
gst_rtp_ilbc_pay_plugin_init(GstPlugin * plugin)224 gst_rtp_ilbc_pay_plugin_init (GstPlugin * plugin)
225 {
226 return gst_element_register (plugin, "rtpilbcpay",
227 GST_RANK_SECONDARY, GST_TYPE_RTP_ILBC_PAY);
228 }
229