2 * Copyright (C) <2005> 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/rtp/gstrtpbuffer.h>
27 #include "gstasteriskh263.h"
29 /* Cygwin has both netinet/in.h and winsock2.h, but
30 * only one can be included, so prefer the unix one */
31 #ifdef HAVE_NETINET_IN_H
32 # include <netinet/in.h>
34 #ifdef HAVE_WINSOCK2_H
35 # include <winsock2.h>
39 #define GST_ASTERISKH263_HEADER_LEN 6
41 typedef struct _GstAsteriskH263Header
43 guint32 timestamp; /* Timestamp */
44 guint16 length; /* Length */
45 } GstAsteriskH263Header;
47 #define GST_ASTERISKH263_HEADER_TIMESTAMP(buf) (((GstAsteriskH263Header *)(GST_BUFFER_DATA (buf)))->timestamp)
48 #define GST_ASTERISKH263_HEADER_LENGTH(buf) (((GstAsteriskH263Header *)(GST_BUFFER_DATA (buf)))->length)
50 static GstStaticPadTemplate gst_asteriskh263_src_template =
51 GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS ("application/x-asteriskh263")
57 static GstStaticPadTemplate gst_asteriskh263_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/x-rtp, "
62 "media = (string) \"video\", "
63 "payload = (int) [ 96, 127 ], "
64 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
67 static void gst_asteriskh263_finalize (GObject * object);
69 static GstFlowReturn gst_asteriskh263_chain (GstPad * pad, GstBuffer * buffer);
71 static GstStateChangeReturn gst_asteriskh263_change_state (GstElement *
72 element, GstStateChange transition);
74 GST_BOILERPLATE (GstAsteriskh263, gst_asteriskh263, GstElement,
78 gst_asteriskh263_base_init (gpointer klass)
80 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
82 gst_element_class_add_static_pad_template (element_class,
83 &gst_asteriskh263_src_template);
84 gst_element_class_add_static_pad_template (element_class,
85 &gst_asteriskh263_sink_template);
87 gst_element_class_set_details_simple (element_class,
88 "RTP Asterisk H263 depayloader", "Codec/Depayloader/Network/RTP",
89 "Extracts H263 video from RTP and encodes in Asterisk H263 format",
90 "Neil Stratford <neils@vipadia.com>");
94 gst_asteriskh263_class_init (GstAsteriskh263Class * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
99 gobject_class = (GObjectClass *) klass;
100 gstelement_class = (GstElementClass *) klass;
102 gobject_class->finalize = gst_asteriskh263_finalize;
104 gstelement_class->change_state = gst_asteriskh263_change_state;
108 gst_asteriskh263_init (GstAsteriskh263 * asteriskh263,
109 GstAsteriskh263Class * klass)
111 asteriskh263->srcpad =
112 gst_pad_new_from_static_template (&gst_asteriskh263_src_template, "src");
113 gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->srcpad);
115 asteriskh263->sinkpad =
116 gst_pad_new_from_static_template (&gst_asteriskh263_sink_template,
118 gst_pad_set_chain_function (asteriskh263->sinkpad, gst_asteriskh263_chain);
119 gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->sinkpad);
121 asteriskh263->adapter = gst_adapter_new ();
125 gst_asteriskh263_finalize (GObject * object)
127 GstAsteriskh263 *asteriskh263;
129 asteriskh263 = GST_ASTERISK_H263 (object);
131 g_object_unref (asteriskh263->adapter);
132 asteriskh263->adapter = NULL;
134 G_OBJECT_CLASS (parent_class)->finalize (object);
138 gst_asteriskh263_chain (GstPad * pad, GstBuffer * buf)
140 GstAsteriskh263 *asteriskh263;
144 asteriskh263 = GST_ASTERISK_H263 (GST_OBJECT_PARENT (pad));
146 if (!gst_rtp_buffer_validate (buf))
155 guint16 asterisk_len;
157 payload_len = gst_rtp_buffer_get_payload_len (buf);
158 payload = gst_rtp_buffer_get_payload (buf);
160 M = gst_rtp_buffer_get_marker (buf);
161 timestamp = gst_rtp_buffer_get_timestamp (buf);
163 outbuf = gst_buffer_new_and_alloc (payload_len +
164 GST_ASTERISKH263_HEADER_LEN);
166 /* build the asterisk header */
167 asterisk_len = payload_len;
169 asterisk_len |= 0x8000;
170 if (!asteriskh263->lastts)
171 asteriskh263->lastts = timestamp;
172 samples = timestamp - asteriskh263->lastts;
173 asteriskh263->lastts = timestamp;
175 GST_ASTERISKH263_HEADER_TIMESTAMP (outbuf) = g_htonl (samples);
176 GST_ASTERISKH263_HEADER_LENGTH (outbuf) = g_htons (asterisk_len);
178 /* copy the data into place */
179 memcpy (GST_BUFFER_DATA (outbuf) + GST_ASTERISKH263_HEADER_LEN, payload,
182 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
183 gst_buffer_set_caps (outbuf,
184 (GstCaps *) gst_pad_get_pad_template_caps (asteriskh263->srcpad));
186 ret = gst_pad_push (asteriskh263->srcpad, outbuf);
188 gst_buffer_unref (buf);
195 GST_DEBUG ("Packet does not validate");
196 gst_buffer_unref (buf);
197 return GST_FLOW_ERROR;
201 static GstStateChangeReturn
202 gst_asteriskh263_change_state (GstElement * element, GstStateChange transition)
204 GstAsteriskh263 *asteriskh263;
205 GstStateChangeReturn ret;
207 asteriskh263 = GST_ASTERISK_H263 (element);
209 switch (transition) {
210 case GST_STATE_CHANGE_READY_TO_PAUSED:
211 gst_adapter_clear (asteriskh263->adapter);
217 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
220 switch (transition) {
221 case GST_STATE_CHANGE_READY_TO_NULL:
231 gst_asteriskh263_plugin_init (GstPlugin * plugin)
233 return gst_element_register (plugin, "asteriskh263",
234 GST_RANK_SECONDARY, GST_TYPE_ASTERISK_H263);