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