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 #define GST_ASTERISKH263_HEADER_LEN 6
31 typedef struct _GstAsteriskH263Header
33 guint32 timestamp; /* Timestamp */
34 guint16 length; /* Length */
35 } GstAsteriskH263Header;
37 #define GST_ASTERISKH263_HEADER_TIMESTAMP(data) (((GstAsteriskH263Header *)(data))->timestamp)
38 #define GST_ASTERISKH263_HEADER_LENGTH(data) (((GstAsteriskH263Header *)(data))->length)
40 static GstStaticPadTemplate gst_asteriskh263_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("application/x-asteriskh263")
47 static GstStaticPadTemplate gst_asteriskh263_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_STATIC_CAPS ("application/x-rtp, "
52 "media = (string) \"video\", "
53 "payload = (int) [ 96, 127 ], "
54 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
57 static void gst_asteriskh263_finalize (GObject * object);
59 static GstFlowReturn gst_asteriskh263_chain (GstPad * pad, GstObject * parent,
62 static GstStateChangeReturn gst_asteriskh263_change_state (GstElement *
63 element, GstStateChange transition);
65 #define gst_asteriskh263_parent_class parent_class
66 G_DEFINE_TYPE (GstAsteriskh263, gst_asteriskh263, GST_TYPE_ELEMENT);
69 gst_asteriskh263_class_init (GstAsteriskh263Class * klass)
71 GObjectClass *gobject_class;
72 GstElementClass *gstelement_class;
74 gobject_class = (GObjectClass *) klass;
75 gstelement_class = (GstElementClass *) klass;
77 gobject_class->finalize = gst_asteriskh263_finalize;
79 gstelement_class->change_state = gst_asteriskh263_change_state;
81 gst_element_class_add_pad_template (gstelement_class,
82 gst_static_pad_template_get (&gst_asteriskh263_src_template));
83 gst_element_class_add_pad_template (gstelement_class,
84 gst_static_pad_template_get (&gst_asteriskh263_sink_template));
86 gst_element_class_set_static_metadata (gstelement_class,
87 "RTP Asterisk H263 depayloader", "Codec/Depayloader/Network/RTP",
88 "Extracts H263 video from RTP and encodes in Asterisk H263 format",
89 "Neil Stratford <neils@vipadia.com>");
93 gst_asteriskh263_init (GstAsteriskh263 * asteriskh263)
95 asteriskh263->srcpad =
96 gst_pad_new_from_static_template (&gst_asteriskh263_src_template, "src");
97 gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->srcpad);
99 asteriskh263->sinkpad =
100 gst_pad_new_from_static_template (&gst_asteriskh263_sink_template,
102 gst_pad_set_chain_function (asteriskh263->sinkpad, gst_asteriskh263_chain);
103 gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->sinkpad);
105 asteriskh263->adapter = gst_adapter_new ();
109 gst_asteriskh263_finalize (GObject * object)
111 GstAsteriskh263 *asteriskh263;
113 asteriskh263 = GST_ASTERISK_H263 (object);
115 g_object_unref (asteriskh263->adapter);
116 asteriskh263->adapter = NULL;
118 G_OBJECT_CLASS (parent_class)->finalize (object);
122 gst_asteriskh263_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
124 GstAsteriskh263 *asteriskh263;
128 asteriskh263 = GST_ASTERISK_H263 (parent);
136 guint16 asterisk_len;
137 GstRTPBuffer rtp = { NULL };
140 if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
143 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
144 payload = gst_rtp_buffer_get_payload (&rtp);
146 M = gst_rtp_buffer_get_marker (&rtp);
147 timestamp = gst_rtp_buffer_get_timestamp (&rtp);
149 gst_rtp_buffer_unmap (&rtp);
151 outbuf = gst_buffer_new_and_alloc (payload_len +
152 GST_ASTERISKH263_HEADER_LEN);
154 /* build the asterisk header */
155 asterisk_len = payload_len;
157 asterisk_len |= 0x8000;
158 if (!asteriskh263->lastts)
159 asteriskh263->lastts = timestamp;
160 samples = timestamp - asteriskh263->lastts;
161 asteriskh263->lastts = timestamp;
163 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
164 GST_ASTERISKH263_HEADER_TIMESTAMP (map.data) = g_htonl (samples);
165 GST_ASTERISKH263_HEADER_LENGTH (map.data) = g_htons (asterisk_len);
167 /* copy the data into place */
168 memcpy (map.data + GST_ASTERISKH263_HEADER_LEN, payload, payload_len);
170 gst_buffer_unmap (outbuf, &map);
172 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
173 if (!gst_pad_has_current_caps (asteriskh263->srcpad)) {
176 caps = gst_pad_get_pad_template_caps (asteriskh263->srcpad);
177 gst_pad_set_caps (asteriskh263->srcpad, caps);
178 gst_caps_unref (caps);
181 ret = gst_pad_push (asteriskh263->srcpad, outbuf);
183 gst_buffer_unref (buf);
190 GST_DEBUG ("Packet does not validate");
191 gst_buffer_unref (buf);
192 return GST_FLOW_ERROR;
196 static GstStateChangeReturn
197 gst_asteriskh263_change_state (GstElement * element, GstStateChange transition)
199 GstAsteriskh263 *asteriskh263;
200 GstStateChangeReturn ret;
202 asteriskh263 = GST_ASTERISK_H263 (element);
204 switch (transition) {
205 case GST_STATE_CHANGE_READY_TO_PAUSED:
206 gst_adapter_clear (asteriskh263->adapter);
212 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
215 switch (transition) {
216 case GST_STATE_CHANGE_READY_TO_NULL:
226 gst_asteriskh263_plugin_init (GstPlugin * plugin)
228 return gst_element_register (plugin, "asteriskh263",
229 GST_RANK_SECONDARY, GST_TYPE_ASTERISK_H263);