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.
21 #include "gstrtpL16enc.h"
23 /* elementfactory information */
24 static GstElementDetails gst_rtpL16enc_details = {
25 "RTP RAW Audio Encoder",
28 "Encodes Raw Audio into an RTP packet",
30 "Zeeshan Ali <zak147@yahoo.com>",
34 /* RtpL16Enc signals and args */
47 GST_PAD_TEMPLATE_FACTORY (sink_factory,
54 "format", GST_PROPS_STRING ("int"),
55 "law", GST_PROPS_INT (0),
56 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
57 "signed", GST_PROPS_BOOLEAN (TRUE),
58 "width", GST_PROPS_INT (16),
59 "depth", GST_PROPS_INT (16),
60 "rate", GST_PROPS_INT_RANGE (1000, 48000),
61 "channels", GST_PROPS_INT_RANGE (1, 2)
65 GST_PAD_TEMPLATE_FACTORY (src_factory,
75 static void gst_rtpL16enc_class_init (GstRtpL16EncClass * klass);
76 static void gst_rtpL16enc_init (GstRtpL16Enc * rtpL16enc);
77 static void gst_rtpL16enc_chain (GstPad * pad, GstBuffer * buf);
78 static void gst_rtpL16enc_set_property (GObject * object, guint prop_id,
79 const GValue * value, GParamSpec * pspec);
80 static void gst_rtpL16enc_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
82 static GstPadLinkReturn gst_rtpL16enc_sinkconnect (GstPad * pad, GstCaps * caps);
83 static GstElementStateReturn gst_rtpL16enc_change_state (GstElement * element);
85 static GstElementClass *parent_class = NULL;
87 static GType gst_rtpL16enc_get_type (void)
89 static GType rtpL16enc_type = 0;
91 if (!rtpL16enc_type) {
92 static const GTypeInfo rtpL16enc_info = {
93 sizeof (GstRtpL16EncClass),
96 (GClassInitFunc) gst_rtpL16enc_class_init,
99 sizeof (GstRtpL16Enc),
101 (GInstanceInitFunc) gst_rtpL16enc_init,
104 rtpL16enc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Enc", &rtpL16enc_info, 0);
106 return rtpL16enc_type;
110 gst_rtpL16enc_class_init (GstRtpL16EncClass * klass)
112 GObjectClass *gobject_class;
113 GstElementClass *gstelement_class;
115 gobject_class = (GObjectClass *) klass;
116 gstelement_class = (GstElementClass *) klass;
118 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
120 gobject_class->set_property = gst_rtpL16enc_set_property;
121 gobject_class->get_property = gst_rtpL16enc_get_property;
123 gstelement_class->change_state = gst_rtpL16enc_change_state;
127 gst_rtpL16enc_init (GstRtpL16Enc * rtpL16enc)
129 rtpL16enc->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_factory), "sink");
130 rtpL16enc->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_factory), "src");
131 gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->sinkpad);
132 gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->srcpad);
133 gst_pad_set_chain_function (rtpL16enc->sinkpad, gst_rtpL16enc_chain);
134 gst_pad_set_link_function (rtpL16enc->sinkpad, gst_rtpL16enc_sinkconnect);
136 rtpL16enc->frequency = 44100;
137 rtpL16enc->channels = 2;
139 rtpL16enc->next_time = 0;
140 rtpL16enc->time_interval = 0;
143 rtpL16enc->ssrc = random ();
146 static GstPadLinkReturn
147 gst_rtpL16enc_sinkconnect (GstPad * pad, GstCaps * caps)
149 GstRtpL16Enc *rtpL16enc;
151 rtpL16enc = GST_RTP_L16_ENC (gst_pad_get_parent (pad));
153 gst_caps_get_int (caps, "rate", &rtpL16enc->frequency);
154 gst_caps_get_int (caps, "channels", &rtpL16enc->channels);
156 /* Pre-calculate what we can */
157 rtpL16enc->time_interval = GST_SECOND / (2 * rtpL16enc->channels * rtpL16enc->frequency);
159 return GST_PAD_LINK_OK;
164 gst_rtpL16enc_htons (GstBuffer *buf)
168 /* FIXME: is this code correct or even sane at all? */
169 i = (guint16 *) GST_BUFFER_DATA(buf);
170 len = i + GST_BUFFER_SIZE (buf) / sizeof (guint16 *);
178 gst_rtpL16enc_chain (GstPad * pad, GstBuffer * buf)
180 GstRtpL16Enc *rtpL16enc;
184 g_return_if_fail (pad != NULL);
185 g_return_if_fail (GST_IS_PAD (pad));
186 g_return_if_fail (buf != NULL);
188 rtpL16enc = GST_RTP_L16_ENC (GST_OBJECT_PARENT (pad));
190 g_return_if_fail (rtpL16enc != NULL);
191 g_return_if_fail (GST_IS_RTP_L16_ENC (rtpL16enc));
193 if (GST_IS_EVENT (buf)) {
194 GstEvent *event = GST_EVENT (buf);
196 switch (GST_EVENT_TYPE (event)) {
197 case GST_EVENT_DISCONTINUOUS:
198 GST_DEBUG (GST_CAT_EVENT, "discont");
199 rtpL16enc->next_time = 0;
200 gst_pad_event_default (pad, event);
203 gst_pad_event_default (pad, event);
208 /* We only need the header */
209 packet = rtp_packet_new_allocate (0, 0, 0);
211 rtp_packet_set_csrc_count (packet, 0);
212 rtp_packet_set_extension (packet, 0);
213 rtp_packet_set_padding (packet, 0);
214 rtp_packet_set_version (packet, RTP_VERSION);
215 rtp_packet_set_marker (packet, 0);
216 rtp_packet_set_ssrc (packet, g_htonl (rtpL16enc->ssrc));
217 rtp_packet_set_seq (packet, g_htons (rtpL16enc->seq));
218 rtp_packet_set_timestamp (packet, g_htonl ((guint32) rtpL16enc->next_time / GST_SECOND));
220 if (rtpL16enc->channels == 1) {
221 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_MONO);
225 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_STEREO);
228 /* FIXME: According to RFC 1890, this is required, right? */
229 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
230 gst_rtpL16enc_htons (buf);
233 outbuf = gst_buffer_new ();
234 GST_BUFFER_SIZE (outbuf) = rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
235 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
236 GST_BUFFER_TIMESTAMP (outbuf) = rtpL16enc->next_time;
238 memcpy (GST_BUFFER_DATA (outbuf), packet->data, rtp_packet_get_packet_len (packet));
239 memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len(packet), GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
241 GST_DEBUG (0,"gst_rtpL16enc_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
242 gst_pad_push (rtpL16enc->srcpad, outbuf);
245 rtpL16enc->next_time += rtpL16enc->time_interval * GST_BUFFER_SIZE (buf);
247 rtp_packet_free (packet);
248 gst_buffer_unref (buf);
252 gst_rtpL16enc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
254 GstRtpL16Enc *rtpL16enc;
256 /* it's not null if we got it, but it might not be ours */
257 g_return_if_fail (GST_IS_RTP_L16_ENC (object));
258 rtpL16enc = GST_RTP_L16_ENC (object);
267 gst_rtpL16enc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
269 GstRtpL16Enc *rtpL16enc;
271 /* it's not null if we got it, but it might not be ours */
272 g_return_if_fail (GST_IS_RTP_L16_ENC (object));
273 rtpL16enc = GST_RTP_L16_ENC (object);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 static GstElementStateReturn
283 gst_rtpL16enc_change_state (GstElement * element)
285 GstRtpL16Enc *rtpL16enc;
287 g_return_val_if_fail (GST_IS_RTP_L16_ENC (element), GST_STATE_FAILURE);
289 rtpL16enc = GST_RTP_L16_ENC (element);
291 GST_DEBUG (0, "state pending %d\n", GST_STATE_PENDING (element));
293 /* if going down into NULL state, close the file if it's open */
294 switch (GST_STATE_TRANSITION (element)) {
295 case GST_STATE_NULL_TO_READY:
298 case GST_STATE_READY_TO_NULL:
305 /* if we haven't failed already, give the parent class a chance to ;-) */
306 if (GST_ELEMENT_CLASS (parent_class)->change_state)
307 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
309 return GST_STATE_SUCCESS;
313 gst_rtpL16enc_plugin_init (GModule * module, GstPlugin * plugin)
315 GstElementFactory *rtpL16enc;
317 rtpL16enc = gst_element_factory_new ("rtpL16enc", GST_TYPE_RTP_L16_ENC, &gst_rtpL16enc_details);
318 g_return_val_if_fail (rtpL16enc != NULL, FALSE);
320 gst_element_factory_add_pad_template (rtpL16enc, GST_PAD_TEMPLATE_GET (sink_factory));
321 gst_element_factory_add_pad_template (rtpL16enc, GST_PAD_TEMPLATE_GET (src_factory));
323 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (rtpL16enc));