gstdepay: check for correct fragment offset
[platform/upstream/gstreamer.git] / gst / rtp / gstrtppcmudepay.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
4  * Copyright (C) <2005> Zeeshan Ali <zeenix@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtppcmudepay.h"
29
30 /* RtpPcmuDepay signals and args */
31 enum
32 {
33   /* FILL ME */
34   LAST_SIGNAL
35 };
36
37 enum
38 {
39   ARG_0
40 };
41
42 static GstStaticPadTemplate gst_rtp_pcmu_depay_sink_template =
43     GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("application/x-rtp, "
47         "media = (string) \"audio\", "
48         "payload = (int) " GST_RTP_PAYLOAD_PCMU_STRING ", "
49         "encoding-name = (string) \"PCMU\", clock-rate = (int) 8000; "
50         "application/x-rtp, "
51         "media = (string) \"audio\", "
52         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53         "encoding-name = (string) \"PCMU\", clock-rate = (int) [1, MAX ]")
54     );
55
56 static GstStaticPadTemplate gst_rtp_pcmu_depay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("audio/x-mulaw, "
61         "channels = (int) 1, rate = (int) [1, MAX ]")
62     );
63
64 static GstBuffer *gst_rtp_pcmu_depay_process (GstRTPBaseDepayload * depayload,
65     GstBuffer * buf);
66 static gboolean gst_rtp_pcmu_depay_setcaps (GstRTPBaseDepayload * depayload,
67     GstCaps * caps);
68
69 #define gst_rtp_pcmu_depay_parent_class parent_class
70 G_DEFINE_TYPE (GstRtpPcmuDepay, gst_rtp_pcmu_depay,
71     GST_TYPE_RTP_BASE_DEPAYLOAD);
72
73 static void
74 gst_rtp_pcmu_depay_class_init (GstRtpPcmuDepayClass * klass)
75 {
76   GstElementClass *gstelement_class;
77   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
78
79   gstelement_class = (GstElementClass *) klass;
80   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
81
82   gst_element_class_add_pad_template (gstelement_class,
83       gst_static_pad_template_get (&gst_rtp_pcmu_depay_src_template));
84   gst_element_class_add_pad_template (gstelement_class,
85       gst_static_pad_template_get (&gst_rtp_pcmu_depay_sink_template));
86
87   gst_element_class_set_static_metadata (gstelement_class,
88       "RTP PCMU depayloader", "Codec/Depayloader/Network/RTP",
89       "Extracts PCMU audio from RTP packets",
90       "Edgard Lima <edgard.lima@indt.org.br>, Zeeshan Ali <zeenix@gmail.com>");
91
92   gstrtpbasedepayload_class->process = gst_rtp_pcmu_depay_process;
93   gstrtpbasedepayload_class->set_caps = gst_rtp_pcmu_depay_setcaps;
94 }
95
96 static void
97 gst_rtp_pcmu_depay_init (GstRtpPcmuDepay * rtppcmudepay)
98 {
99   GstRTPBaseDepayload *depayload;
100
101   depayload = GST_RTP_BASE_DEPAYLOAD (rtppcmudepay);
102
103   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
104 }
105
106 static gboolean
107 gst_rtp_pcmu_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
108 {
109   GstCaps *srccaps;
110   GstStructure *structure;
111   gboolean ret;
112   gint clock_rate;
113
114   structure = gst_caps_get_structure (caps, 0);
115
116   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
117     clock_rate = 8000;          /* default */
118   depayload->clock_rate = clock_rate;
119
120   srccaps = gst_caps_new_simple ("audio/x-mulaw",
121       "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
122   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
123   gst_caps_unref (srccaps);
124
125   return ret;
126 }
127
128 static GstBuffer *
129 gst_rtp_pcmu_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
130 {
131   GstBuffer *outbuf = NULL;
132   guint len;
133   gboolean marker;
134   GstRTPBuffer rtp = { NULL };
135
136   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
137
138   marker = gst_rtp_buffer_get_marker (&rtp);
139
140   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
141       gst_buffer_get_size (buf), marker,
142       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
143
144   len = gst_rtp_buffer_get_payload_len (&rtp);
145   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
146   gst_rtp_buffer_unmap (&rtp);
147
148   if (outbuf) {
149     GST_BUFFER_DURATION (outbuf) =
150         gst_util_uint64_scale_int (len, GST_SECOND, depayload->clock_rate);
151
152     if (marker) {
153       /* mark start of talkspurt with DISCONT */
154       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
155     }
156   }
157
158   return outbuf;
159 }
160
161 gboolean
162 gst_rtp_pcmu_depay_plugin_init (GstPlugin * plugin)
163 {
164   return gst_element_register (plugin, "rtppcmudepay",
165       GST_RANK_SECONDARY, GST_TYPE_RTP_PCMU_DEPAY);
166 }