gstdepay: check for correct fragment offset
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpmpadepay.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
27 #include "gstrtpmpadepay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpmpadepay_debug);
30 #define GST_CAT_DEFAULT (rtpmpadepay_debug)
31
32 static GstStaticPadTemplate gst_rtp_mpa_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
37     );
38
39 static GstStaticPadTemplate gst_rtp_mpa_depay_sink_template =
40     GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("application/x-rtp, "
44         "media = (string) \"audio\", "
45         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
46         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\";"
47         "application/x-rtp, "
48         "media = (string) \"audio\", "
49         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
50         "clock-rate = (int) 90000")
51     );
52
53 #define gst_rtp_mpa_depay_parent_class parent_class
54 G_DEFINE_TYPE (GstRtpMPADepay, gst_rtp_mpa_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
55
56 static gboolean gst_rtp_mpa_depay_setcaps (GstRTPBaseDepayload * depayload,
57     GstCaps * caps);
58 static GstBuffer *gst_rtp_mpa_depay_process (GstRTPBaseDepayload * depayload,
59     GstBuffer * buf);
60
61 static void
62 gst_rtp_mpa_depay_class_init (GstRtpMPADepayClass * klass)
63 {
64   GstElementClass *gstelement_class;
65   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
66
67   GST_DEBUG_CATEGORY_INIT (rtpmpadepay_debug, "rtpmpadepay", 0,
68       "MPEG Audio RTP Depayloader");
69
70   gstelement_class = (GstElementClass *) klass;
71   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
72
73   gst_element_class_add_pad_template (gstelement_class,
74       gst_static_pad_template_get (&gst_rtp_mpa_depay_src_template));
75   gst_element_class_add_pad_template (gstelement_class,
76       gst_static_pad_template_get (&gst_rtp_mpa_depay_sink_template));
77
78   gst_element_class_set_static_metadata (gstelement_class,
79       "RTP MPEG audio depayloader", "Codec/Depayloader/Network/RTP",
80       "Extracts MPEG audio from RTP packets (RFC 2038)",
81       "Wim Taymans <wim.taymans@gmail.com>");
82
83   gstrtpbasedepayload_class->set_caps = gst_rtp_mpa_depay_setcaps;
84   gstrtpbasedepayload_class->process = gst_rtp_mpa_depay_process;
85 }
86
87 static void
88 gst_rtp_mpa_depay_init (GstRtpMPADepay * rtpmpadepay)
89 {
90 }
91
92 static gboolean
93 gst_rtp_mpa_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
94 {
95   GstStructure *structure;
96   GstCaps *outcaps;
97   gint clock_rate;
98   gboolean res;
99
100   structure = gst_caps_get_structure (caps, 0);
101
102   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
103     clock_rate = 90000;
104   depayload->clock_rate = clock_rate;
105
106   outcaps =
107       gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1, NULL);
108   res = gst_pad_set_caps (depayload->srcpad, outcaps);
109   gst_caps_unref (outcaps);
110
111   return res;
112 }
113
114 static GstBuffer *
115 gst_rtp_mpa_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
116 {
117   GstRtpMPADepay *rtpmpadepay;
118   GstBuffer *outbuf;
119   GstRTPBuffer rtp = { NULL };
120   gint payload_len;
121 #if 0
122   guint8 *payload;
123   guint16 frag_offset;
124 #endif
125   gboolean marker;
126
127   rtpmpadepay = GST_RTP_MPA_DEPAY (depayload);
128
129   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
130
131   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
132
133   if (payload_len <= 4)
134     goto empty_packet;
135
136 #if 0
137   payload = gst_rtp_buffer_get_payload (&rtp);
138   /* strip off header
139    *
140    *  0                   1                   2                   3
141    *  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
142    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143    * |             MBZ               |          Frag_offset          |
144    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145    */
146   frag_offset = (payload[2] << 8) | payload[3];
147 #endif
148
149   /* subbuffer skipping the 4 header bytes */
150   outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 4, -1);
151   marker = gst_rtp_buffer_get_marker (&rtp);
152
153   if (marker) {
154     /* mark start of talkspurt with discont */
155     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
156   }
157   GST_DEBUG_OBJECT (rtpmpadepay,
158       "gst_rtp_mpa_depay_chain: pushing buffer of size %" G_GSIZE_FORMAT "",
159       gst_buffer_get_size (outbuf));
160
161   gst_rtp_buffer_unmap (&rtp);
162
163   /* FIXME, we can push half mpeg frames when they are split over multiple
164    * RTP packets */
165   return outbuf;
166
167   /* ERRORS */
168 empty_packet:
169   {
170     GST_ELEMENT_WARNING (rtpmpadepay, STREAM, DECODE,
171         ("Empty Payload."), (NULL));
172     gst_rtp_buffer_unmap (&rtp);
173     return NULL;
174   }
175 }
176
177 gboolean
178 gst_rtp_mpa_depay_plugin_init (GstPlugin * plugin)
179 {
180   return gst_element_register (plugin, "rtpmpadepay",
181       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_DEPAY);
182 }