2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
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.
26 #include <gst/audio/audio.h>
27 #include <gst/audio/multichannel.h>
28 #include <gst/rtp/gstrtpbuffer.h>
30 #include "gstrtpL16pay.h"
31 #include "gstrtpchannels.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
34 #define GST_CAT_DEFAULT (rtpL16pay_debug)
36 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("audio/x-raw, "
41 "format = (string) S16_BE, "
42 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
45 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("application/x-rtp, "
50 "media = (string) \"audio\", "
51 "payload = (int) [ 96, 127 ], "
52 "clock-rate = (int) [ 1, MAX ], "
53 "encoding-name = (string) \"L16\", "
54 "channels = (int) [ 1, MAX ];"
56 "media = (string) \"audio\", "
57 "encoding-name = (string) \"L16\", "
58 "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
59 "clock-rate = (int) 44100;"
61 "media = (string) \"audio\", "
62 "encoding-name = (string) \"L16\", "
63 "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
64 "clock-rate = (int) 44100")
67 static gboolean gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload,
69 static GstCaps *gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload,
70 GstPad * pad, GstCaps * filter);
72 #define gst_rtp_L16_pay_parent_class parent_class
73 G_DEFINE_TYPE (GstRtpL16Pay, gst_rtp_L16_pay, GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
76 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
78 GstElementClass *gstelement_class;
79 GstBaseRTPPayloadClass *gstbasertppayload_class;
81 gstelement_class = (GstElementClass *) klass;
82 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
84 gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
85 gstbasertppayload_class->get_caps = gst_rtp_L16_pay_getcaps;
87 gst_element_class_add_pad_template (gstelement_class,
88 gst_static_pad_template_get (&gst_rtp_L16_pay_src_template));
89 gst_element_class_add_pad_template (gstelement_class,
90 gst_static_pad_template_get (&gst_rtp_L16_pay_sink_template));
92 gst_element_class_set_details_simple (gstelement_class, "RTP audio payloader",
93 "Codec/Payloader/Network/RTP",
94 "Payload-encode Raw audio into RTP packets (RFC 3551)",
95 "Wim Taymans <wim.taymans@gmail.com>");
97 GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
102 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
104 GstBaseRTPAudioPayload *basertpaudiopayload;
106 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpL16pay);
108 /* tell basertpaudiopayload that this is a sample based codec */
109 gst_base_rtp_audio_payload_set_sample_based (basertpaudiopayload);
113 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
115 GstRtpL16Pay *rtpL16pay;
116 GstStructure *structure;
120 GstAudioChannelPosition *pos;
121 const GstRTPChannelOrder *order;
122 GstBaseRTPAudioPayload *basertpaudiopayload;
124 basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basepayload);
125 rtpL16pay = GST_RTP_L16_PAY (basepayload);
127 structure = gst_caps_get_structure (caps, 0);
129 /* first parse input caps */
130 if (!gst_structure_get_int (structure, "rate", &rate))
133 if (!gst_structure_get_int (structure, "channels", &channels))
136 /* get the channel order */
137 pos = gst_audio_get_channel_positions (structure);
139 order = gst_rtp_channels_get_by_pos (channels, pos);
143 gst_basertppayload_set_options (basepayload, "audio", TRUE, "L16", rate);
144 params = g_strdup_printf ("%d", channels);
146 if (!order && channels > 2) {
147 GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
148 (NULL), ("Unknown channel order for %d channels", channels));
151 if (order && order->name) {
152 res = gst_basertppayload_set_outcaps (basepayload,
153 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
154 channels, "channel-order", G_TYPE_STRING, order->name, NULL);
156 res = gst_basertppayload_set_outcaps (basepayload,
157 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
164 rtpL16pay->rate = rate;
165 rtpL16pay->channels = channels;
167 /* octet-per-sample is 2 * channels for L16 */
168 gst_base_rtp_audio_payload_set_sample_options (basertpaudiopayload,
169 2 * rtpL16pay->channels);
176 GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
181 GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
187 gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad,
190 GstCaps *otherpadcaps;
193 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
194 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
197 if (!gst_caps_is_empty (otherpadcaps)) {
198 GstStructure *structure;
203 structure = gst_caps_get_structure (otherpadcaps, 0);
205 if (gst_structure_get_int (structure, "channels", &channels)) {
206 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
207 } else if (gst_structure_get_int (structure, "payload", &pt)) {
209 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
211 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
214 if (gst_structure_get_int (structure, "clock-rate", &rate)) {
215 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
216 } else if (gst_structure_get_int (structure, "payload", &pt)) {
217 if (pt == 10 || pt == 11)
218 gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
222 gst_caps_unref (otherpadcaps);
226 GstCaps *tcaps = caps;
228 caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
229 gst_caps_unref (tcaps);
236 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
238 return gst_element_register (plugin, "rtpL16pay",
239 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_PAY);