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