2 * Copyright (C) <2006> Philippe Khalaf <burger@speedy.org>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include "gstrtpilbcpay.h"
28 /* elementfactory information */
29 static GstElementDetails gst_rtpilbcpay_details = {
31 "Codec/Payloader/Network",
32 "Packetize iLBC audio streams into RTP packets",
33 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>"
36 GST_DEBUG_CATEGORY_STATIC (rtpilbcpay_debug);
37 #define GST_CAT_DEFAULT (rtpilbcpay_debug)
39 static GstStaticPadTemplate gst_rtpilbcpay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) {20, 30}")
46 static GstStaticPadTemplate gst_rtpilbcpay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
50 GST_STATIC_CAPS ("application/x-rtp, "
51 "media = (string) \"audio\", "
52 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53 "clock-rate = (int) 8000, "
54 "encoding-name = (string) \"ILBC\", "
55 "mode = (string) { \"20\", \"30\" }")
59 static GstCaps *gst_rtpilbcpay_sink_getcaps (GstBaseRTPPayload * payload,
61 static gboolean gst_rtpilbcpay_sink_setcaps (GstBaseRTPPayload * payload,
64 GST_BOILERPLATE (GstRTPILBCPay, gst_rtpilbcpay, GstBaseRTPAudioPayload,
65 GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
68 gst_rtpilbcpay_base_init (gpointer klass)
70 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
72 gst_element_class_add_pad_template (element_class,
73 gst_static_pad_template_get (&gst_rtpilbcpay_sink_template));
74 gst_element_class_add_pad_template (element_class,
75 gst_static_pad_template_get (&gst_rtpilbcpay_src_template));
76 gst_element_class_set_details (element_class, &gst_rtpilbcpay_details);
80 gst_rtpilbcpay_class_init (GstRTPILBCPayClass * klass)
82 GstBaseRTPPayloadClass *gstbasertppayload_class;
84 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
86 parent_class = g_type_class_ref (GST_TYPE_BASE_RTP_PAYLOAD);
88 gstbasertppayload_class->set_caps = gst_rtpilbcpay_sink_setcaps;
89 gstbasertppayload_class->get_caps = gst_rtpilbcpay_sink_getcaps;
91 GST_DEBUG_CATEGORY_INIT (rtpilbcpay_debug, "rtpilbcpay", 0,
92 "iLBC audio RTP payloader");
96 gst_rtpilbcpay_init (GstRTPILBCPay * rtpilbcpay, GstRTPILBCPayClass * klass)
98 GstBaseRTPPayload *basertppayload;
99 GstBaseRTPAudioPayload *basertpaudiopayload;
101 basertppayload = GST_BASE_RTP_PAYLOAD (rtpilbcpay);
102 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpilbcpay);
104 /* we don't set the payload type, it should be set by the application using
105 * the pt property or the default 96 will be used */
106 basertppayload->clock_rate = 8000;
108 rtpilbcpay->mode = -1;
110 /* tell basertpaudiopayload that this is a frame based codec */
111 gst_base_rtp_audio_payload_set_frame_based (basertpaudiopayload);
115 gst_rtpilbcpay_sink_setcaps (GstBaseRTPPayload * basertppayload, GstCaps * caps)
117 GstRTPILBCPay *rtpilbcpay;
118 GstBaseRTPAudioPayload *basertpaudiopayload;
122 GstStructure *structure;
123 const char *payload_name;
125 rtpilbcpay = GST_RTP_ILBC_PAY (basertppayload);
126 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basertppayload);
128 structure = gst_caps_get_structure (caps, 0);
130 payload_name = gst_structure_get_name (structure);
131 if (g_ascii_strcasecmp ("audio/x-iLBC", payload_name))
134 if (!gst_structure_get_int (structure, "mode", &mode))
137 if (mode != 20 && mode != 30)
140 gst_basertppayload_set_options (basertppayload, "audio", TRUE, "ILBC", 8000);
141 /* set options for this frame based audio codec */
142 gst_base_rtp_audio_payload_set_frame_options (basertpaudiopayload,
143 mode, mode == 30 ? 50 : 38);
145 mode_str = g_strdup_printf ("%d", mode);
147 gst_basertppayload_set_outcaps (basertppayload, "mode", G_TYPE_STRING,
151 if (mode != rtpilbcpay->mode && rtpilbcpay->mode != -1)
154 rtpilbcpay->mode = mode;
161 GST_ERROR_OBJECT (rtpilbcpay, "expected audio/x-iLBC, received %s",
167 GST_ERROR_OBJECT (rtpilbcpay, "did not receive a mode");
172 GST_ERROR_OBJECT (rtpilbcpay, "mode must be 20 or 30, received %d", mode);
177 GST_ERROR_OBJECT (rtpilbcpay, "Mode has changed from %d to %d! "
178 "Mode cannot change while streaming", rtpilbcpay->mode, mode);
183 /* we return the padtemplate caps with the mode field fixated to a value if we
186 gst_rtpilbcpay_sink_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
188 GstCaps *otherpadcaps;
191 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
192 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
195 if (!gst_caps_is_empty (otherpadcaps)) {
196 GstStructure *structure;
197 const gchar *mode_str;
200 structure = gst_caps_get_structure (otherpadcaps, 0);
202 /* parse mode, if we can */
203 mode_str = gst_structure_get_string (structure, "mode");
205 mode = strtol (mode_str, NULL, 10);
206 if (mode == 20 || mode == 30) {
207 structure = gst_caps_get_structure (caps, 0);
208 gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
212 gst_caps_unref (otherpadcaps);
218 gst_rtp_ilbc_pay_plugin_init (GstPlugin * plugin)
220 return gst_element_register (plugin, "rtpilbcpay",
221 GST_RANK_NONE, GST_TYPE_RTP_ILBC_PAY);