webrtc/nice: Support domain name as connection-address of ICE candidate
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstasteriskh263.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpelements.h"
28 #include "gstasteriskh263.h"
29
30 #define GST_ASTERISKH263_HEADER_LEN 6
31
32 typedef struct _GstAsteriskH263Header
33 {
34   guint32 timestamp;            /* Timestamp */
35   guint16 length;               /* Length */
36 } GstAsteriskH263Header;
37
38 #define GST_ASTERISKH263_HEADER_TIMESTAMP(data) (((GstAsteriskH263Header *)(data))->timestamp)
39 #define GST_ASTERISKH263_HEADER_LENGTH(data) (((GstAsteriskH263Header *)(data))->length)
40
41 static GstStaticPadTemplate gst_asteriskh263_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-asteriskh263")
46     );
47
48 static GstStaticPadTemplate gst_asteriskh263_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
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\"")
56     );
57
58 static void gst_asteriskh263_finalize (GObject * object);
59
60 static GstFlowReturn gst_asteriskh263_chain (GstPad * pad, GstObject * parent,
61     GstBuffer * buffer);
62
63 static GstStateChangeReturn gst_asteriskh263_change_state (GstElement *
64     element, GstStateChange transition);
65
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));
70
71 static void
72 gst_asteriskh263_class_init (GstAsteriskh263Class * klass)
73 {
74   GObjectClass *gobject_class;
75   GstElementClass *gstelement_class;
76
77   gobject_class = (GObjectClass *) klass;
78   gstelement_class = (GstElementClass *) klass;
79
80   gobject_class->finalize = gst_asteriskh263_finalize;
81
82   gstelement_class->change_state = gst_asteriskh263_change_state;
83
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);
88
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>");
93 }
94
95 static void
96 gst_asteriskh263_init (GstAsteriskh263 * asteriskh263)
97 {
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);
101
102   asteriskh263->sinkpad =
103       gst_pad_new_from_static_template (&gst_asteriskh263_sink_template,
104       "sink");
105   gst_pad_set_chain_function (asteriskh263->sinkpad, gst_asteriskh263_chain);
106   gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->sinkpad);
107
108   asteriskh263->adapter = gst_adapter_new ();
109 }
110
111 static void
112 gst_asteriskh263_finalize (GObject * object)
113 {
114   GstAsteriskh263 *asteriskh263;
115
116   asteriskh263 = GST_ASTERISK_H263 (object);
117
118   g_object_unref (asteriskh263->adapter);
119   asteriskh263->adapter = NULL;
120
121   G_OBJECT_CLASS (parent_class)->finalize (object);
122 }
123
124 static GstFlowReturn
125 gst_asteriskh263_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
126 {
127   GstAsteriskh263 *asteriskh263;
128   GstBuffer *outbuf;
129   GstFlowReturn ret;
130
131   asteriskh263 = GST_ASTERISK_H263 (parent);
132
133   {
134     gint payload_len;
135     guint8 *payload;
136     gboolean M;
137     guint32 timestamp;
138     guint32 samples;
139     guint16 asterisk_len;
140     GstRTPBuffer rtp = { NULL };
141     GstMapInfo map;
142
143     if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
144       goto bad_packet;
145
146     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
147     payload = gst_rtp_buffer_get_payload (&rtp);
148
149     M = gst_rtp_buffer_get_marker (&rtp);
150     timestamp = gst_rtp_buffer_get_timestamp (&rtp);
151
152     gst_rtp_buffer_unmap (&rtp);
153
154     outbuf = gst_buffer_new_and_alloc (payload_len +
155         GST_ASTERISKH263_HEADER_LEN);
156
157     /* build the asterisk header */
158     asterisk_len = payload_len;
159     if (M)
160       asterisk_len |= 0x8000;
161     if (!asteriskh263->lastts)
162       asteriskh263->lastts = timestamp;
163     samples = timestamp - asteriskh263->lastts;
164     asteriskh263->lastts = timestamp;
165
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);
169
170     /* copy the data into place */
171     memcpy (map.data + GST_ASTERISKH263_HEADER_LEN, payload, payload_len);
172
173     gst_buffer_unmap (outbuf, &map);
174
175     GST_BUFFER_PTS (outbuf) = timestamp;
176     if (!gst_pad_has_current_caps (asteriskh263->srcpad)) {
177       GstCaps *caps;
178
179       caps = gst_pad_get_pad_template_caps (asteriskh263->srcpad);
180       gst_pad_set_caps (asteriskh263->srcpad, caps);
181       gst_caps_unref (caps);
182     }
183
184     ret = gst_pad_push (asteriskh263->srcpad, outbuf);
185
186     gst_buffer_unref (buf);
187   }
188
189   return ret;
190
191 bad_packet:
192   {
193     GST_DEBUG ("Packet does not validate");
194     gst_buffer_unref (buf);
195     return GST_FLOW_ERROR;
196   }
197 }
198
199 static GstStateChangeReturn
200 gst_asteriskh263_change_state (GstElement * element, GstStateChange transition)
201 {
202   GstAsteriskh263 *asteriskh263;
203   GstStateChangeReturn ret;
204
205   asteriskh263 = GST_ASTERISK_H263 (element);
206
207   switch (transition) {
208     case GST_STATE_CHANGE_READY_TO_PAUSED:
209       gst_adapter_clear (asteriskh263->adapter);
210       break;
211     default:
212       break;
213   }
214
215   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
216
217   /*
218      switch (transition) {
219      case GST_STATE_CHANGE_READY_TO_NULL:
220      break;
221      default:
222      break;
223      }
224    */
225   return ret;
226 }