Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 (GstBaseRTPDepayload * depayload,
53     GstBuffer * buf);
54 static gboolean gst_rtp_bv_depay_setcaps (GstBaseRTPDepayload * depayload,
55     GstCaps * caps);
56
57 GST_BOILERPLATE (GstRTPBVDepay, gst_rtp_bv_depay, GstBaseRTPDepayload,
58     GST_TYPE_BASE_RTP_DEPAYLOAD);
59
60 static void
61 gst_rtp_bv_depay_base_init (gpointer klass)
62 {
63   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
64
65   gst_element_class_add_static_pad_template (element_class,
66       &gst_rtp_bv_depay_src_template);
67   gst_element_class_add_static_pad_template (element_class,
68       &gst_rtp_bv_depay_sink_template);
69   gst_element_class_set_details_simple (element_class,
70       "RTP BroadcomVoice depayloader", "Codec/Depayloader/Network/RTP",
71       "Extracts BroadcomVoice audio from RTP packets (RFC 4298)",
72       "Wim Taymans <wim.taymans@collabora.co.uk>");
73 }
74
75 static void
76 gst_rtp_bv_depay_class_init (GstRTPBVDepayClass * klass)
77 {
78   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
79
80   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
81
82   gstbasertpdepayload_class->process = gst_rtp_bv_depay_process;
83   gstbasertpdepayload_class->set_caps = gst_rtp_bv_depay_setcaps;
84 }
85
86 static void
87 gst_rtp_bv_depay_init (GstRTPBVDepay * rtpbvdepay, GstRTPBVDepayClass * klass)
88 {
89   rtpbvdepay->mode = -1;
90 }
91
92 static gboolean
93 gst_rtp_bv_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
94 {
95   GstRTPBVDepay *rtpbvdepay = GST_RTP_BV_DEPAY (depayload);
96   GstCaps *srccaps;
97   GstStructure *structure;
98   const gchar *mode_str = NULL;
99   gint mode, clock_rate, expected_rate;
100   gboolean ret;
101
102   structure = gst_caps_get_structure (caps, 0);
103
104   mode_str = gst_structure_get_string (structure, "encoding-name");
105   if (!mode_str)
106     goto no_mode;
107
108   if (!strcmp (mode_str, "BV16")) {
109     mode = 16;
110     expected_rate = 8000;
111   } else if (!strcmp (mode_str, "BV32")) {
112     mode = 32;
113     expected_rate = 16000;
114   } else
115     goto invalid_mode;
116
117   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
118     clock_rate = expected_rate;
119   else if (clock_rate != expected_rate)
120     goto wrong_rate;
121
122   depayload->clock_rate = clock_rate;
123   rtpbvdepay->mode = mode;
124
125   srccaps = gst_caps_new_simple ("audio/x-bv",
126       "mode", G_TYPE_INT, rtpbvdepay->mode, NULL);
127   ret = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
128
129   GST_DEBUG ("set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
130   gst_caps_unref (srccaps);
131
132   return ret;
133
134   /* ERRORS */
135 no_mode:
136   {
137     GST_ERROR_OBJECT (rtpbvdepay, "did not receive an encoding-name");
138     return FALSE;
139   }
140 invalid_mode:
141   {
142     GST_ERROR_OBJECT (rtpbvdepay,
143         "invalid encoding-name, expected BV16 or BV32, got %s", mode_str);
144     return FALSE;
145   }
146 wrong_rate:
147   {
148     GST_ERROR_OBJECT (rtpbvdepay, "invalid clock-rate, expected %d, got %d",
149         expected_rate, clock_rate);
150     return FALSE;
151   }
152 }
153
154 static GstBuffer *
155 gst_rtp_bv_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
156 {
157   GstBuffer *outbuf;
158   gboolean marker;
159
160   marker = gst_rtp_buffer_get_marker (buf);
161
162   GST_DEBUG ("process : got %d bytes, mark %d ts %u seqn %d",
163       GST_BUFFER_SIZE (buf), marker,
164       gst_rtp_buffer_get_timestamp (buf), gst_rtp_buffer_get_seq (buf));
165
166   outbuf = gst_rtp_buffer_get_payload_buffer (buf);
167
168   if (marker && outbuf) {
169     /* mark start of talkspurt with DISCONT */
170     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
171   }
172
173   return outbuf;
174 }
175
176 gboolean
177 gst_rtp_bv_depay_plugin_init (GstPlugin * plugin)
178 {
179   return gst_element_register (plugin, "rtpbvdepay",
180       GST_RANK_SECONDARY, GST_TYPE_RTP_BV_DEPAY);
181 }