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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpelements.h"
28 #include "gstasteriskh263.h"
30 #define GST_ASTERISKH263_HEADER_LEN 6
32 typedef struct _GstAsteriskH263Header
34 guint32 timestamp; /* Timestamp */
35 guint16 length; /* Length */
36 } GstAsteriskH263Header;
38 #define GST_ASTERISKH263_HEADER_TIMESTAMP(data) (((GstAsteriskH263Header *)(data))->timestamp)
39 #define GST_ASTERISKH263_HEADER_LENGTH(data) (((GstAsteriskH263Header *)(data))->length)
41 static GstStaticPadTemplate gst_asteriskh263_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
45 GST_STATIC_CAPS ("application/x-asteriskh263")
48 static GstStaticPadTemplate gst_asteriskh263_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS ("application/x-rtp, "
53 "media = (string) \"video\", "
54 "payload = (int) [ 96, 127 ], "
55 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
58 static void gst_asteriskh263_finalize (GObject * object);
60 static GstFlowReturn gst_asteriskh263_chain (GstPad * pad, GstObject * parent,
63 static GstStateChangeReturn gst_asteriskh263_change_state (GstElement *
64 element, GstStateChange transition);
66 #define gst_asteriskh263_parent_class parent_class
67 G_DEFINE_TYPE (GstAsteriskh263, gst_asteriskh263, GST_TYPE_ELEMENT);
68 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (asteriskh263, "asteriskh263",
69 GST_RANK_NONE, GST_TYPE_ASTERISK_H263, rtp_element_init (plugin));
72 gst_asteriskh263_class_init (GstAsteriskh263Class * klass)
74 GObjectClass *gobject_class;
75 GstElementClass *gstelement_class;
77 gobject_class = (GObjectClass *) klass;
78 gstelement_class = (GstElementClass *) klass;
80 gobject_class->finalize = gst_asteriskh263_finalize;
82 gstelement_class->change_state = gst_asteriskh263_change_state;
84 gst_element_class_add_static_pad_template (gstelement_class,
85 &gst_asteriskh263_src_template);
86 gst_element_class_add_static_pad_template (gstelement_class,
87 &gst_asteriskh263_sink_template);
89 gst_element_class_set_static_metadata (gstelement_class,
90 "RTP Asterisk H263 depayloader", "Codec/Depayloader/Network/RTP",
91 "Extracts H263 video from RTP and encodes in Asterisk H263 format",
92 "Neil Stratford <neils@vipadia.com>");
96 gst_asteriskh263_init (GstAsteriskh263 * asteriskh263)
98 asteriskh263->srcpad =
99 gst_pad_new_from_static_template (&gst_asteriskh263_src_template, "src");
100 gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->srcpad);
102 asteriskh263->sinkpad =
103 gst_pad_new_from_static_template (&gst_asteriskh263_sink_template,
105 gst_pad_set_chain_function (asteriskh263->sinkpad, gst_asteriskh263_chain);
106 gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->sinkpad);
108 asteriskh263->adapter = gst_adapter_new ();
112 gst_asteriskh263_finalize (GObject * object)
114 GstAsteriskh263 *asteriskh263;
116 asteriskh263 = GST_ASTERISK_H263 (object);
118 g_object_unref (asteriskh263->adapter);
119 asteriskh263->adapter = NULL;
121 G_OBJECT_CLASS (parent_class)->finalize (object);
125 gst_asteriskh263_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
127 GstAsteriskh263 *asteriskh263;
131 asteriskh263 = GST_ASTERISK_H263 (parent);
139 guint16 asterisk_len;
140 GstRTPBuffer rtp = { NULL };
143 if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
146 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
147 payload = gst_rtp_buffer_get_payload (&rtp);
149 M = gst_rtp_buffer_get_marker (&rtp);
150 timestamp = gst_rtp_buffer_get_timestamp (&rtp);
152 gst_rtp_buffer_unmap (&rtp);
154 outbuf = gst_buffer_new_and_alloc (payload_len +
155 GST_ASTERISKH263_HEADER_LEN);
157 /* build the asterisk header */
158 asterisk_len = payload_len;
160 asterisk_len |= 0x8000;
161 if (!asteriskh263->lastts)
162 asteriskh263->lastts = timestamp;
163 samples = timestamp - asteriskh263->lastts;
164 asteriskh263->lastts = timestamp;
166 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
167 GST_ASTERISKH263_HEADER_TIMESTAMP (map.data) = g_htonl (samples);
168 GST_ASTERISKH263_HEADER_LENGTH (map.data) = g_htons (asterisk_len);
170 /* copy the data into place */
171 memcpy (map.data + GST_ASTERISKH263_HEADER_LEN, payload, payload_len);
173 gst_buffer_unmap (outbuf, &map);
175 GST_BUFFER_PTS (outbuf) = timestamp;
176 if (!gst_pad_has_current_caps (asteriskh263->srcpad)) {
179 caps = gst_pad_get_pad_template_caps (asteriskh263->srcpad);
180 gst_pad_set_caps (asteriskh263->srcpad, caps);
181 gst_caps_unref (caps);
184 ret = gst_pad_push (asteriskh263->srcpad, outbuf);
186 gst_buffer_unref (buf);
193 GST_DEBUG ("Packet does not validate");
194 gst_buffer_unref (buf);
195 return GST_FLOW_ERROR;
199 static GstStateChangeReturn
200 gst_asteriskh263_change_state (GstElement * element, GstStateChange transition)
202 GstAsteriskh263 *asteriskh263;
203 GstStateChangeReturn ret;
205 asteriskh263 = GST_ASTERISK_H263 (element);
207 switch (transition) {
208 case GST_STATE_CHANGE_READY_TO_PAUSED:
209 gst_adapter_clear (asteriskh263->adapter);
215 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
218 switch (transition) {
219 case GST_STATE_CHANGE_READY_TO_NULL: