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