webrtc/nice: Support domain name as connection-address of ICE candidate
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpmpvdepay.c
1 /* GStreamer
2  * Copyright (C) <2006> 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 <gst/rtp/gstrtpbuffer.h>
25 #include <gst/video/video.h>
26
27 #include <string.h>
28 #include "gstrtpelements.h"
29 #include "gstrtpmpvdepay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpmpvdepay_debug);
33 #define GST_CAT_DEFAULT (rtpmpvdepay_debug)
34
35 /* FIXME, we set the mpeg version to 2, we should ideally be looking at contents
36  * of the stream to figure out the version */
37 static GstStaticPadTemplate gst_rtp_mpv_depay_src_template =
38 GST_STATIC_PAD_TEMPLATE ("src",
39     GST_PAD_SRC,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS
42     ("video/mpeg, mpegversion = (int) 2, systemstream = (boolean) FALSE")
43     );
44
45 static GstStaticPadTemplate gst_rtp_mpv_depay_sink_template =
46     GST_STATIC_PAD_TEMPLATE ("sink",
47     GST_PAD_SINK,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) \"video\", "
51         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\";"
52         "application/x-rtp, "
53         "media = (string) \"video\", "
54         "payload = (int) " GST_RTP_PAYLOAD_MPV_STRING ", "
55         "clock-rate = (int) 90000")
56     );
57
58 G_DEFINE_TYPE (GstRtpMPVDepay, gst_rtp_mpv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
59 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpmpvdepay, "rtpmpvdepay",
60     GST_RANK_SECONDARY, GST_TYPE_RTP_MPV_DEPAY, rtp_element_init (plugin));
61
62 static gboolean gst_rtp_mpv_depay_setcaps (GstRTPBaseDepayload * depayload,
63     GstCaps * caps);
64 static GstBuffer *gst_rtp_mpv_depay_process (GstRTPBaseDepayload * depayload,
65     GstRTPBuffer * rtp);
66
67 static void
68 gst_rtp_mpv_depay_class_init (GstRtpMPVDepayClass * klass)
69 {
70   GstElementClass *gstelement_class;
71   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
72
73   gstelement_class = (GstElementClass *) klass;
74   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
75
76   gst_element_class_add_static_pad_template (gstelement_class,
77       &gst_rtp_mpv_depay_src_template);
78   gst_element_class_add_static_pad_template (gstelement_class,
79       &gst_rtp_mpv_depay_sink_template);
80
81   gst_element_class_set_static_metadata (gstelement_class,
82       "RTP MPEG video depayloader", "Codec/Depayloader/Network/RTP",
83       "Extracts MPEG video from RTP packets (RFC 2250)",
84       "Wim Taymans <wim.taymans@gmail.com>");
85
86   gstrtpbasedepayload_class->set_caps = gst_rtp_mpv_depay_setcaps;
87   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_mpv_depay_process;
88
89   GST_DEBUG_CATEGORY_INIT (rtpmpvdepay_debug, "rtpmpvdepay", 0,
90       "MPEG Video RTP Depayloader");
91 }
92
93 static void
94 gst_rtp_mpv_depay_init (GstRtpMPVDepay * rtpmpvdepay)
95 {
96 }
97
98 static gboolean
99 gst_rtp_mpv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
100 {
101   GstStructure *structure;
102   gint clock_rate;
103   GstCaps *outcaps;
104   gboolean res;
105
106   structure = gst_caps_get_structure (caps, 0);
107
108   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
109     clock_rate = 90000;         /* default */
110   depayload->clock_rate = clock_rate;
111
112   outcaps = gst_caps_new_simple ("video/mpeg",
113       "mpegversion", G_TYPE_INT, 2,
114       "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
115   res = gst_pad_set_caps (depayload->srcpad, outcaps);
116   gst_caps_unref (outcaps);
117
118   return res;
119 }
120
121 static GstBuffer *
122 gst_rtp_mpv_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
123 {
124   GstRtpMPVDepay *rtpmpvdepay;
125   GstBuffer *outbuf = NULL;
126
127   rtpmpvdepay = GST_RTP_MPV_DEPAY (depayload);
128
129   {
130     gint payload_len, payload_header;
131     guint8 *payload;
132     guint8 T;
133
134     payload_len = gst_rtp_buffer_get_payload_len (rtp);
135     payload = gst_rtp_buffer_get_payload (rtp);
136     payload_header = 0;
137
138     if (payload_len <= 4)
139       goto empty_packet;
140
141     /* 3.4 MPEG Video-specific header
142      *
143      *  0                   1                   2                   3
144      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
145      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146      * |    MBZ  |T|         TR        | |N|S|B|E|  P  | | BFC | | FFC |
147      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148      *                                  AN              FBV     FFV
149      */
150     T = (payload[0] & 0x04);
151
152     payload_len -= 4;
153     payload_header += 4;
154     payload += 4;
155
156     if (T) {
157       /* 
158        * 3.4.1 MPEG-2 Video-specific header extension
159        *
160        *  0                   1                   2                   3
161        *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
162        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163        * |X|E|f_[0,0]|f_[0,1]|f_[1,0]|f_[1,1]| DC| PS|T|P|C|Q|V|A|R|H|G|D|
164        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165        */
166       if (payload_len <= 4)
167         goto empty_packet;
168
169       payload_len -= 4;
170       payload_header += 4;
171       payload += 4;
172     }
173
174     outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, payload_header, -1);
175
176     if (outbuf) {
177       GST_DEBUG_OBJECT (rtpmpvdepay,
178           "gst_rtp_mpv_depay_chain: pushing buffer of size %" G_GSIZE_FORMAT,
179           gst_buffer_get_size (outbuf));
180       gst_rtp_drop_non_video_meta (rtpmpvdepay, outbuf);
181
182     }
183   }
184
185   return outbuf;
186
187   /* ERRORS */
188 empty_packet:
189   {
190     GST_ELEMENT_WARNING (rtpmpvdepay, STREAM, DECODE,
191         (NULL), ("Empty payload."));
192     return NULL;
193   }
194 }