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