Merge branch 'dtmf-moved-from-bad'
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpbvdepay.c
1 /* GStreamer
2  * Copyright (C) <2009> 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 #include <stdlib.h>
26
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpbvdepay.h"
29
30 static GstStaticPadTemplate gst_rtp_bv_depay_sink_template =
31     GST_STATIC_PAD_TEMPLATE ("sink",
32     GST_PAD_SINK,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS ("application/x-rtp, "
35         "media = (string) \"audio\", "
36         "clock-rate = (int) 8000, "
37         "encoding-name = (string) \"BV16\"; "
38         "application/x-rtp, "
39         "media = (string) \"audio\", "
40         "clock-rate = (int) 16000, " "encoding-name = (string) \"BV32\"")
41     );
42
43 static GstStaticPadTemplate gst_rtp_bv_depay_src_template =
44 GST_STATIC_PAD_TEMPLATE ("src",
45     GST_PAD_SRC,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("audio/x-bv, " "mode = (int) { 16, 32 }")
48     );
49
50 static GstBuffer *gst_rtp_bv_depay_process (GstRTPBaseDepayload * depayload,
51     GstBuffer * buf);
52 static gboolean gst_rtp_bv_depay_setcaps (GstRTPBaseDepayload * depayload,
53     GstCaps * caps);
54
55 #define gst_rtp_bv_depay_parent_class parent_class
56 G_DEFINE_TYPE (GstRTPBVDepay, gst_rtp_bv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
57
58 static void
59 gst_rtp_bv_depay_class_init (GstRTPBVDepayClass * klass)
60 {
61   GstElementClass *gstelement_class;
62   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
63
64   gstelement_class = (GstElementClass *) klass;
65   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
66
67   gst_element_class_add_pad_template (gstelement_class,
68       gst_static_pad_template_get (&gst_rtp_bv_depay_src_template));
69   gst_element_class_add_pad_template (gstelement_class,
70       gst_static_pad_template_get (&gst_rtp_bv_depay_sink_template));
71
72   gst_element_class_set_static_metadata (gstelement_class,
73       "RTP BroadcomVoice depayloader", "Codec/Depayloader/Network/RTP",
74       "Extracts BroadcomVoice audio from RTP packets (RFC 4298)",
75       "Wim Taymans <wim.taymans@collabora.co.uk>");
76
77   gstrtpbasedepayload_class->process = gst_rtp_bv_depay_process;
78   gstrtpbasedepayload_class->set_caps = gst_rtp_bv_depay_setcaps;
79 }
80
81 static void
82 gst_rtp_bv_depay_init (GstRTPBVDepay * rtpbvdepay)
83 {
84   rtpbvdepay->mode = -1;
85 }
86
87 static gboolean
88 gst_rtp_bv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
89 {
90   GstRTPBVDepay *rtpbvdepay = GST_RTP_BV_DEPAY (depayload);
91   GstCaps *srccaps;
92   GstStructure *structure;
93   const gchar *mode_str = NULL;
94   gint mode, clock_rate, expected_rate;
95   gboolean ret;
96
97   structure = gst_caps_get_structure (caps, 0);
98
99   mode_str = gst_structure_get_string (structure, "encoding-name");
100   if (!mode_str)
101     goto no_mode;
102
103   if (!strcmp (mode_str, "BV16")) {
104     mode = 16;
105     expected_rate = 8000;
106   } else if (!strcmp (mode_str, "BV32")) {
107     mode = 32;
108     expected_rate = 16000;
109   } else
110     goto invalid_mode;
111
112   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
113     clock_rate = expected_rate;
114   else if (clock_rate != expected_rate)
115     goto wrong_rate;
116
117   depayload->clock_rate = clock_rate;
118   rtpbvdepay->mode = mode;
119
120   srccaps = gst_caps_new_simple ("audio/x-bv",
121       "mode", G_TYPE_INT, rtpbvdepay->mode, NULL);
122   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
123
124   GST_DEBUG ("set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
125   gst_caps_unref (srccaps);
126
127   return ret;
128
129   /* ERRORS */
130 no_mode:
131   {
132     GST_ERROR_OBJECT (rtpbvdepay, "did not receive an encoding-name");
133     return FALSE;
134   }
135 invalid_mode:
136   {
137     GST_ERROR_OBJECT (rtpbvdepay,
138         "invalid encoding-name, expected BV16 or BV32, got %s", mode_str);
139     return FALSE;
140   }
141 wrong_rate:
142   {
143     GST_ERROR_OBJECT (rtpbvdepay, "invalid clock-rate, expected %d, got %d",
144         expected_rate, clock_rate);
145     return FALSE;
146   }
147 }
148
149 static GstBuffer *
150 gst_rtp_bv_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
151 {
152   GstBuffer *outbuf;
153   gboolean marker;
154   GstRTPBuffer rtp = { NULL, };
155
156   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
157
158   marker = gst_rtp_buffer_get_marker (&rtp);
159
160   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
161       gst_buffer_get_size (buf), marker,
162       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
163
164   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
165   gst_rtp_buffer_unmap (&rtp);
166
167   if (marker && outbuf) {
168     /* mark start of talkspurt with DISCONT */
169     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
170   }
171
172   return outbuf;
173 }
174
175 gboolean
176 gst_rtp_bv_depay_plugin_init (GstPlugin * plugin)
177 {
178   return gst_element_register (plugin, "rtpbvdepay",
179       GST_RANK_SECONDARY, GST_TYPE_RTP_BV_DEPAY);
180 }