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 GST_DEBUG_CATEGORY_STATIC (rtpilbcpay_debug);
29 #define GST_CAT_DEFAULT (rtpilbcpay_debug)
31 static GstStaticPadTemplate gst_rtp_ilbc_pay_sink_template =
32 GST_STATIC_PAD_TEMPLATE ("sink",
35 GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) {20, 30}")
38 static GstStaticPadTemplate gst_rtp_ilbc_pay_src_template =
39 GST_STATIC_PAD_TEMPLATE ("src",
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\" }")
51 static GstCaps *gst_rtp_ilbc_pay_sink_getcaps (GstBaseRTPPayload * payload,
53 static gboolean gst_rtp_ilbc_pay_sink_setcaps (GstBaseRTPPayload * payload,
56 GST_BOILERPLATE (GstRTPILBCPay, gst_rtp_ilbc_pay, GstBaseRTPAudioPayload,
57 GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
60 gst_rtp_ilbc_pay_base_init (gpointer klass)
62 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
64 gst_element_class_add_pad_template (element_class,
65 gst_static_pad_template_get (&gst_rtp_ilbc_pay_sink_template));
66 gst_element_class_add_pad_template (element_class,
67 gst_static_pad_template_get (&gst_rtp_ilbc_pay_src_template));
68 gst_element_class_set_details_simple (element_class, "RTP iLBC Payloader",
69 "Codec/Payloader/Network/RTP",
70 "Packetize iLBC audio streams into RTP packets",
71 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>");
75 gst_rtp_ilbc_pay_class_init (GstRTPILBCPayClass * klass)
77 GstBaseRTPPayloadClass *gstbasertppayload_class;
79 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
81 gstbasertppayload_class->set_caps = gst_rtp_ilbc_pay_sink_setcaps;
82 gstbasertppayload_class->get_caps = gst_rtp_ilbc_pay_sink_getcaps;
84 GST_DEBUG_CATEGORY_INIT (rtpilbcpay_debug, "rtpilbcpay", 0,
85 "iLBC audio RTP payloader");
89 gst_rtp_ilbc_pay_init (GstRTPILBCPay * rtpilbcpay, GstRTPILBCPayClass * klass)
91 GstBaseRTPPayload *basertppayload;
92 GstBaseRTPAudioPayload *basertpaudiopayload;
94 basertppayload = GST_BASE_RTP_PAYLOAD (rtpilbcpay);
95 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpilbcpay);
97 /* we don't set the payload type, it should be set by the application using
98 * the pt property or the default 96 will be used */
99 basertppayload->clock_rate = 8000;
101 rtpilbcpay->mode = -1;
103 /* tell basertpaudiopayload that this is a frame based codec */
104 gst_base_rtp_audio_payload_set_frame_based (basertpaudiopayload);
108 gst_rtp_ilbc_pay_sink_setcaps (GstBaseRTPPayload * basertppayload,
111 GstRTPILBCPay *rtpilbcpay;
112 GstBaseRTPAudioPayload *basertpaudiopayload;
116 GstStructure *structure;
117 const char *payload_name;
119 rtpilbcpay = GST_RTP_ILBC_PAY (basertppayload);
120 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basertppayload);
122 structure = gst_caps_get_structure (caps, 0);
124 payload_name = gst_structure_get_name (structure);
125 if (g_ascii_strcasecmp ("audio/x-iLBC", payload_name))
128 if (!gst_structure_get_int (structure, "mode", &mode))
131 if (mode != 20 && mode != 30)
134 gst_basertppayload_set_options (basertppayload, "audio", TRUE, "ILBC", 8000);
135 /* set options for this frame based audio codec */
136 gst_base_rtp_audio_payload_set_frame_options (basertpaudiopayload,
137 mode, mode == 30 ? 50 : 38);
139 mode_str = g_strdup_printf ("%d", mode);
141 gst_basertppayload_set_outcaps (basertppayload, "mode", G_TYPE_STRING,
145 if (mode != rtpilbcpay->mode && rtpilbcpay->mode != -1)
148 rtpilbcpay->mode = mode;
155 GST_ERROR_OBJECT (rtpilbcpay, "expected audio/x-iLBC, received %s",
161 GST_ERROR_OBJECT (rtpilbcpay, "did not receive a mode");
166 GST_ERROR_OBJECT (rtpilbcpay, "mode must be 20 or 30, received %d", mode);
171 GST_ERROR_OBJECT (rtpilbcpay, "Mode has changed from %d to %d! "
172 "Mode cannot change while streaming", rtpilbcpay->mode, mode);
177 /* we return the padtemplate caps with the mode field fixated to a value if we
180 gst_rtp_ilbc_pay_sink_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
182 GstCaps *otherpadcaps;
185 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
186 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
189 if (!gst_caps_is_empty (otherpadcaps)) {
190 GstStructure *structure;
191 const gchar *mode_str;
194 structure = gst_caps_get_structure (otherpadcaps, 0);
196 /* parse mode, if we can */
197 mode_str = gst_structure_get_string (structure, "mode");
199 mode = strtol (mode_str, NULL, 10);
200 if (mode == 20 || mode == 30) {
201 structure = gst_caps_get_structure (caps, 0);
202 gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
206 gst_caps_unref (otherpadcaps);
212 gst_rtp_ilbc_pay_plugin_init (GstPlugin * plugin)
214 return gst_element_register (plugin, "rtpilbcpay",
215 GST_RANK_SECONDARY, GST_TYPE_RTP_ILBC_PAY);