1 /* GStreamer
2 * Copyright (C) <2009> Wim Taymans <wim.taymans@gmail.com>
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 /**
21 * SECTION:element-rtpbvpay
22 * @see_also: rtpbvdepay
23 *
24 * Payload BroadcomVoice audio into RTP packets according to RFC 4298.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc4298.txt
26 */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <gst/rtp/gstrtpbuffer.h>
36 #include "gstrtpbvpay.h"
37
38 GST_DEBUG_CATEGORY_STATIC (rtpbvpay_debug);
39 #define GST_CAT_DEFAULT (rtpbvpay_debug)
40
41 static GstStaticPadTemplate gst_rtp_bv_pay_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_PAD_SINK,
44 GST_PAD_ALWAYS,
45 GST_STATIC_CAPS ("audio/x-bv, " "mode = (int) {16, 32}")
46 );
47
48 static GstStaticPadTemplate gst_rtp_bv_pay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
50 GST_PAD_SRC,
51 GST_PAD_ALWAYS,
52 GST_STATIC_CAPS ("application/x-rtp, "
53 "media = (string) \"audio\", "
54 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55 "clock-rate = (int) 8000, "
56 "encoding-name = (string) \"BV16\";"
57 "application/x-rtp, "
58 "media = (string) \"audio\", "
59 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
60 "clock-rate = (int) 16000, " "encoding-name = (string) \"BV32\"")
61 );
62
63
64 static GstCaps *gst_rtp_bv_pay_sink_getcaps (GstRTPBasePayload * payload,
65 GstPad * pad, GstCaps * filter);
66 static gboolean gst_rtp_bv_pay_sink_setcaps (GstRTPBasePayload * payload,
67 GstCaps * caps);
68
69 #define gst_rtp_bv_pay_parent_class parent_class
70 G_DEFINE_TYPE (GstRTPBVPay, gst_rtp_bv_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
71
72 static void
gst_rtp_bv_pay_class_init(GstRTPBVPayClass * klass)73 gst_rtp_bv_pay_class_init (GstRTPBVPayClass * klass)
74 {
75 GstElementClass *gstelement_class;
76 GstRTPBasePayloadClass *gstrtpbasepayload_class;
77
78 GST_DEBUG_CATEGORY_INIT (rtpbvpay_debug, "rtpbvpay", 0,
79 "BroadcomVoice audio RTP payloader");
80
81 gstelement_class = (GstElementClass *) klass;
82 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
83
84 gst_element_class_add_static_pad_template (gstelement_class,
85 &gst_rtp_bv_pay_sink_template);
86 gst_element_class_add_static_pad_template (gstelement_class,
87 &gst_rtp_bv_pay_src_template);
88
89 gst_element_class_set_static_metadata (gstelement_class, "RTP BV Payloader",
90 "Codec/Payloader/Network/RTP",
91 "Packetize BroadcomVoice audio streams into RTP packets (RFC 4298)",
92 "Wim Taymans <wim.taymans@collabora.co.uk>");
93
94 gstrtpbasepayload_class->set_caps = gst_rtp_bv_pay_sink_setcaps;
95 gstrtpbasepayload_class->get_caps = gst_rtp_bv_pay_sink_getcaps;
96 }
97
98 static void
gst_rtp_bv_pay_init(GstRTPBVPay * rtpbvpay)99 gst_rtp_bv_pay_init (GstRTPBVPay * rtpbvpay)
100 {
101 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
102
103 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpbvpay);
104
105 rtpbvpay->mode = -1;
106
107 /* tell rtpbaseaudiopayload that this is a frame based codec */
108 gst_rtp_base_audio_payload_set_frame_based (rtpbaseaudiopayload);
109 }
110
111 static gboolean
gst_rtp_bv_pay_sink_setcaps(GstRTPBasePayload * rtpbasepayload,GstCaps * caps)112 gst_rtp_bv_pay_sink_setcaps (GstRTPBasePayload * rtpbasepayload, GstCaps * caps)
113 {
114 GstRTPBVPay *rtpbvpay;
115 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
116 gint mode;
117 GstStructure *structure;
118 const char *payload_name;
119
120 rtpbvpay = GST_RTP_BV_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-bv", payload_name))
127 goto wrong_caps;
128
129 if (!gst_structure_get_int (structure, "mode", &mode))
130 goto no_mode;
131
132 if (mode != 16 && mode != 32)
133 goto wrong_mode;
134
135 if (mode == 16) {
136 gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "BV16",
137 8000);
138 rtpbasepayload->clock_rate = 8000;
139 } else {
140 gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "BV32",
141 16000);
142 rtpbasepayload->clock_rate = 16000;
143 }
144
145 /* set options for this frame based audio codec */
146 gst_rtp_base_audio_payload_set_frame_options (rtpbaseaudiopayload,
147 mode, mode == 16 ? 10 : 20);
148
149 if (mode != rtpbvpay->mode && rtpbvpay->mode != -1)
150 goto mode_changed;
151
152 rtpbvpay->mode = mode;
153
154 return TRUE;
155
156 /* ERRORS */
157 wrong_caps:
158 {
159 GST_ERROR_OBJECT (rtpbvpay, "expected audio/x-bv, received %s",
160 payload_name);
161 return FALSE;
162 }
163 no_mode:
164 {
165 GST_ERROR_OBJECT (rtpbvpay, "did not receive a mode");
166 return FALSE;
167 }
168 wrong_mode:
169 {
170 GST_ERROR_OBJECT (rtpbvpay, "mode must be 16 or 32, received %d", mode);
171 return FALSE;
172 }
173 mode_changed:
174 {
175 GST_ERROR_OBJECT (rtpbvpay, "Mode has changed from %d to %d! "
176 "Mode cannot change while streaming", rtpbvpay->mode, mode);
177 return FALSE;
178 }
179 }
180
181 /* we return the padtemplate caps with the mode field fixated to a value if we
182 * can */
183 static GstCaps *
gst_rtp_bv_pay_sink_getcaps(GstRTPBasePayload * rtppayload,GstPad * pad,GstCaps * filter)184 gst_rtp_bv_pay_sink_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
185 GstCaps * filter)
186 {
187 GstCaps *otherpadcaps;
188 GstCaps *caps;
189
190 caps = gst_pad_get_pad_template_caps (pad);
191
192 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
193 if (otherpadcaps) {
194 if (!gst_caps_is_empty (otherpadcaps)) {
195 GstStructure *structure;
196 const gchar *mode_str;
197 gint mode;
198
199 structure = gst_caps_get_structure (otherpadcaps, 0);
200
201 /* construct mode, if we can */
202 mode_str = gst_structure_get_string (structure, "encoding-name");
203 if (mode_str) {
204 if (!strcmp (mode_str, "BV16"))
205 mode = 16;
206 else if (!strcmp (mode_str, "BV32"))
207 mode = 32;
208 else
209 mode = -1;
210
211 if (mode == 16 || mode == 32) {
212 caps = gst_caps_make_writable (caps);
213 structure = gst_caps_get_structure (caps, 0);
214 gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
215 }
216 }
217 }
218 gst_caps_unref (otherpadcaps);
219 }
220
221 if (filter) {
222 GstCaps *tmp;
223
224 GST_DEBUG_OBJECT (rtppayload, "Intersect %" GST_PTR_FORMAT " and filter %"
225 GST_PTR_FORMAT, caps, filter);
226 tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
227 gst_caps_unref (caps);
228 caps = tmp;
229 }
230
231 return caps;
232 }
233
234 gboolean
gst_rtp_bv_pay_plugin_init(GstPlugin * plugin)235 gst_rtp_bv_pay_plugin_init (GstPlugin * plugin)
236 {
237 return gst_element_register (plugin, "rtpbvpay",
238 GST_RANK_SECONDARY, GST_TYPE_RTP_BV_PAY);
239 }
240