2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 "gstrtpL16pay.h"
27 /* elementfactory information */
28 static GstElementDetails gst_rtpL16pay_details = {
29 "RTP RAW Audio Payloader",
30 "Codec/Payloader/Network",
31 "Payload-encodes Raw Audio into a RTP packet",
32 "Zeeshan Ali <zak147@yahoo.com>"
35 /* RtpL16Pay signals and args */
48 static GstStaticPadTemplate gst_rtpL16pay_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS ("audio/x-raw-int, "
53 "endianness = (int) BYTE_ORDER, "
54 "signed = (boolean) true, "
57 "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]")
60 static GstStaticPadTemplate gst_rtpL16pay_src_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS ("application/x-rtp")
67 static void gst_rtpL16pay_class_init (GstRtpL16PayClass * klass);
68 static void gst_rtpL16pay_base_init (GstRtpL16PayClass * klass);
69 static void gst_rtpL16pay_init (GstRtpL16Pay * rtpL16enc);
70 static void gst_rtpL16pay_chain (GstPad * pad, GstData * _data);
71 static void gst_rtpL16pay_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
73 static void gst_rtpL16pay_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
75 static GstPadLinkReturn gst_rtpL16pay_sinkconnect (GstPad * pad,
76 const GstCaps * caps);
77 static GstStateChangeReturn gst_rtpL16pay_change_state (GstElement * element,
78 GstStateChange transition);
80 static GstElementClass *parent_class = NULL;
83 gst_rtpL16pay_get_type (void)
85 static GType rtpL16pay_type = 0;
87 if (!rtpL16pay_type) {
88 static const GTypeInfo rtpL16pay_info = {
89 sizeof (GstRtpL16PayClass),
90 (GBaseInitFunc) gst_rtpL16pay_base_init,
92 (GClassInitFunc) gst_rtpL16pay_class_init,
95 sizeof (GstRtpL16Pay),
97 (GInstanceInitFunc) gst_rtpL16pay_init,
101 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Pay",
104 return rtpL16pay_type;
108 gst_rtpL16pay_base_init (GstRtpL16PayClass * klass)
110 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
112 gst_element_class_add_pad_template (element_class,
113 gst_static_pad_template_get (&gst_rtpL16pay_sink_template));
114 gst_element_class_add_pad_template (element_class,
115 gst_static_pad_template_get (&gst_rtpL16pay_src_template));
116 gst_element_class_set_details (element_class, &gst_rtpL16pay_details);
120 gst_rtpL16pay_class_init (GstRtpL16PayClass * klass)
122 GObjectClass *gobject_class;
123 GstElementClass *gstelement_class;
125 gobject_class = (GObjectClass *) klass;
126 gstelement_class = (GstElementClass *) klass;
128 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
130 gobject_class->set_property = gst_rtpL16pay_set_property;
131 gobject_class->get_property = gst_rtpL16pay_get_property;
133 gstelement_class->change_state = gst_rtpL16pay_change_state;
137 gst_rtpL16pay_init (GstRtpL16Pay * rtpL16enc)
140 gst_pad_new_from_template (gst_static_pad_template_get
141 (&gst_rtpL16pay_sink_template), "sink");
143 gst_pad_new_from_template (gst_static_pad_template_get
144 (&gst_rtpL16pay_src_template), "src");
145 gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->sinkpad);
146 gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->srcpad);
147 gst_pad_set_chain_function (rtpL16enc->sinkpad, gst_rtpL16pay_chain);
148 gst_pad_set_link_function (rtpL16enc->sinkpad, gst_rtpL16pay_sinkconnect);
150 rtpL16enc->frequency = 44100;
151 rtpL16enc->channels = 2;
153 rtpL16enc->next_time = 0;
154 rtpL16enc->time_interval = 0;
157 rtpL16enc->ssrc = random ();
160 static GstPadLinkReturn
161 gst_rtpL16pay_sinkconnect (GstPad * pad, const GstCaps * caps)
163 GstRtpL16Pay *rtpL16enc;
164 GstStructure *structure;
167 rtpL16enc = GST_RTP_L16_PAY (gst_pad_get_parent (pad));
169 structure = gst_caps_get_structure (caps, 0);
171 ret = gst_structure_get_int (structure, "rate", &rtpL16enc->frequency);
172 ret &= gst_structure_get_int (structure, "channels", &rtpL16enc->channels);
175 gst_object_unref (rtpL16enc);
176 return GST_PAD_LINK_REFUSED;
179 /* Pre-calculate what we can */
180 rtpL16enc->time_interval =
181 GST_SECOND / (2 * rtpL16enc->channels * rtpL16enc->frequency);
183 gst_object_unref (rtpL16enc);
185 return GST_PAD_LINK_OK;
190 gst_rtpL16pay_htons (GstBuffer * buf)
194 /* FIXME: is this code correct or even sane at all? */
195 i = (gint16 *) GST_BUFFER_DATA (buf);
196 len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
198 for (; i < len; i++) {
204 gst_rtpL16pay_chain (GstPad * pad, GstData * _data)
206 GstBuffer *buf = GST_BUFFER (_data);
207 GstRtpL16Pay *rtpL16enc;
211 g_return_if_fail (pad != NULL);
212 g_return_if_fail (GST_IS_PAD (pad));
213 g_return_if_fail (buf != NULL);
215 rtpL16enc = GST_RTP_L16_PAY (GST_OBJECT_PARENT (pad));
217 g_return_if_fail (rtpL16enc != NULL);
218 g_return_if_fail (GST_IS_RTP_L16_PAY (rtpL16enc));
220 if (GST_IS_EVENT (buf)) {
221 GstEvent *event = GST_EVENT (buf);
223 switch (GST_EVENT_TYPE (event)) {
224 case GST_EVENT_DISCONTINUOUS:
225 GST_DEBUG ("discont");
226 rtpL16enc->next_time = 0;
227 gst_pad_event_default (pad, event);
230 gst_pad_event_default (pad, event);
235 /* We only need the header */
236 packet = rtp_packet_new_allocate (0, 0, 0);
238 rtp_packet_set_csrc_count (packet, 0);
239 rtp_packet_set_extension (packet, 0);
240 rtp_packet_set_padding (packet, 0);
241 rtp_packet_set_version (packet, RTP_VERSION);
242 rtp_packet_set_marker (packet, 0);
243 rtp_packet_set_ssrc (packet, g_htonl (rtpL16enc->ssrc));
244 rtp_packet_set_seq (packet, g_htons (rtpL16enc->seq));
245 rtp_packet_set_timestamp (packet,
246 g_htonl ((guint32) rtpL16enc->next_time / GST_SECOND));
248 if (rtpL16enc->channels == 1) {
249 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_MONO);
253 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_STEREO);
256 /* FIXME: According to RFC 1890, this is required, right? */
257 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
258 gst_rtpL16pay_htons (buf);
261 outbuf = gst_buffer_new ();
262 GST_BUFFER_SIZE (outbuf) =
263 rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
264 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
265 GST_BUFFER_TIMESTAMP (outbuf) = rtpL16enc->next_time;
267 memcpy (GST_BUFFER_DATA (outbuf), packet->data,
268 rtp_packet_get_packet_len (packet));
269 memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len (packet),
270 GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
272 GST_DEBUG ("gst_rtpL16pay_chain: pushing buffer of size %d",
273 GST_BUFFER_SIZE (outbuf));
274 gst_pad_push (rtpL16enc->srcpad, GST_DATA (outbuf));
277 rtpL16enc->next_time += rtpL16enc->time_interval * GST_BUFFER_SIZE (buf);
279 rtp_packet_free (packet);
280 gst_buffer_unref (buf);
284 gst_rtpL16pay_set_property (GObject * object, guint prop_id,
285 const GValue * value, GParamSpec * pspec)
287 GstRtpL16Pay *rtpL16enc;
289 g_return_if_fail (GST_IS_RTP_L16_PAY (object));
290 rtpL16enc = GST_RTP_L16_PAY (object);
299 gst_rtpL16pay_get_property (GObject * object, guint prop_id, GValue * value,
302 GstRtpL16Pay *rtpL16enc;
304 g_return_if_fail (GST_IS_RTP_L16_PAY (object));
305 rtpL16enc = GST_RTP_L16_PAY (object);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314 static GstStateChangeReturn
315 gst_rtpL16pay_change_state (GstElement * element, GstStateChange transition)
317 GstRtpL16Pay *rtpL16enc;
319 g_return_val_if_fail (GST_IS_RTP_L16_PAY (element), GST_STATE_CHANGE_FAILURE);
321 rtpL16enc = GST_RTP_L16_PAY (element);
323 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
325 /* if going down into NULL state, close the file if it's open */
326 switch (transition) {
327 case GST_STATE_CHANGE_NULL_TO_READY:
330 case GST_STATE_CHANGE_READY_TO_NULL:
337 /* if we haven't failed already, give the parent class a chance to ;-) */
338 if (GST_ELEMENT_CLASS (parent_class)->change_state)
339 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
341 return GST_STATE_CHANGE_SUCCESS;
345 gst_rtpL16pay_plugin_init (GstPlugin * plugin)
347 return gst_element_register (plugin, "rtpL16enc",
348 GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);