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