rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpbvpay.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-rtpbvpay
22  * @title: rtpbvpay
23  * @see_also: rtpbvdepay
24  *
25  * Payload BroadcomVoice audio into 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 <stdlib.h>
34 #include <string.h>
35
36 #include <gst/rtp/gstrtpbuffer.h>
37 #include "gstrtpbvpay.h"
38
39 GST_DEBUG_CATEGORY_STATIC (rtpbvpay_debug);
40 #define GST_CAT_DEFAULT (rtpbvpay_debug)
41
42 static GstStaticPadTemplate gst_rtp_bv_pay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("audio/x-bv, " "mode = (int) {16, 32}")
47     );
48
49 static GstStaticPadTemplate gst_rtp_bv_pay_src_template =
50     GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("application/x-rtp, "
54         "media = (string) \"audio\", "
55         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56         "clock-rate = (int) 8000, "
57         "encoding-name = (string) \"BV16\";"
58         "application/x-rtp, "
59         "media = (string) \"audio\", "
60         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
61         "clock-rate = (int) 16000, " "encoding-name = (string) \"BV32\"")
62     );
63
64
65 static GstCaps *gst_rtp_bv_pay_sink_getcaps (GstRTPBasePayload * payload,
66     GstPad * pad, GstCaps * filter);
67 static gboolean gst_rtp_bv_pay_sink_setcaps (GstRTPBasePayload * payload,
68     GstCaps * caps);
69
70 #define gst_rtp_bv_pay_parent_class parent_class
71 G_DEFINE_TYPE (GstRTPBVPay, gst_rtp_bv_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
72
73 static void
74 gst_rtp_bv_pay_class_init (GstRTPBVPayClass * klass)
75 {
76   GstElementClass *gstelement_class;
77   GstRTPBasePayloadClass *gstrtpbasepayload_class;
78
79   GST_DEBUG_CATEGORY_INIT (rtpbvpay_debug, "rtpbvpay", 0,
80       "BroadcomVoice audio RTP payloader");
81
82   gstelement_class = (GstElementClass *) klass;
83   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
84
85   gst_element_class_add_static_pad_template (gstelement_class,
86       &gst_rtp_bv_pay_sink_template);
87   gst_element_class_add_static_pad_template (gstelement_class,
88       &gst_rtp_bv_pay_src_template);
89
90   gst_element_class_set_static_metadata (gstelement_class, "RTP BV Payloader",
91       "Codec/Payloader/Network/RTP",
92       "Packetize BroadcomVoice audio streams into RTP packets (RFC 4298)",
93       "Wim Taymans <wim.taymans@collabora.co.uk>");
94
95   gstrtpbasepayload_class->set_caps = gst_rtp_bv_pay_sink_setcaps;
96   gstrtpbasepayload_class->get_caps = gst_rtp_bv_pay_sink_getcaps;
97 }
98
99 static void
100 gst_rtp_bv_pay_init (GstRTPBVPay * rtpbvpay)
101 {
102   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
103
104   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpbvpay);
105
106   rtpbvpay->mode = -1;
107
108   /* tell rtpbaseaudiopayload that this is a frame based codec */
109   gst_rtp_base_audio_payload_set_frame_based (rtpbaseaudiopayload);
110 }
111
112 static gboolean
113 gst_rtp_bv_pay_sink_setcaps (GstRTPBasePayload * rtpbasepayload, GstCaps * caps)
114 {
115   GstRTPBVPay *rtpbvpay;
116   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
117   gint mode;
118   GstStructure *structure;
119   const char *payload_name;
120
121   rtpbvpay = GST_RTP_BV_PAY (rtpbasepayload);
122   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpbasepayload);
123
124   structure = gst_caps_get_structure (caps, 0);
125
126   payload_name = gst_structure_get_name (structure);
127   if (g_ascii_strcasecmp ("audio/x-bv", payload_name))
128     goto wrong_caps;
129
130   if (!gst_structure_get_int (structure, "mode", &mode))
131     goto no_mode;
132
133   if (mode != 16 && mode != 32)
134     goto wrong_mode;
135
136   if (mode == 16) {
137     gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "BV16",
138         8000);
139     rtpbasepayload->clock_rate = 8000;
140   } else {
141     gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "BV32",
142         16000);
143     rtpbasepayload->clock_rate = 16000;
144   }
145
146   /* set options for this frame based audio codec */
147   gst_rtp_base_audio_payload_set_frame_options (rtpbaseaudiopayload,
148       mode, mode == 16 ? 10 : 20);
149
150   if (mode != rtpbvpay->mode && rtpbvpay->mode != -1)
151     goto mode_changed;
152
153   rtpbvpay->mode = mode;
154
155   return TRUE;
156
157   /* ERRORS */
158 wrong_caps:
159   {
160     GST_ERROR_OBJECT (rtpbvpay, "expected audio/x-bv, received %s",
161         payload_name);
162     return FALSE;
163   }
164 no_mode:
165   {
166     GST_ERROR_OBJECT (rtpbvpay, "did not receive a mode");
167     return FALSE;
168   }
169 wrong_mode:
170   {
171     GST_ERROR_OBJECT (rtpbvpay, "mode must be 16 or 32, received %d", mode);
172     return FALSE;
173   }
174 mode_changed:
175   {
176     GST_ERROR_OBJECT (rtpbvpay, "Mode has changed from %d to %d! "
177         "Mode cannot change while streaming", rtpbvpay->mode, mode);
178     return FALSE;
179   }
180 }
181
182 /* we return the padtemplate caps with the mode field fixated to a value if we
183  * can */
184 static GstCaps *
185 gst_rtp_bv_pay_sink_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
186     GstCaps * filter)
187 {
188   GstCaps *otherpadcaps;
189   GstCaps *caps;
190
191   caps = gst_pad_get_pad_template_caps (pad);
192
193   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
194   if (otherpadcaps) {
195     if (!gst_caps_is_empty (otherpadcaps)) {
196       GstStructure *structure;
197       const gchar *mode_str;
198       gint mode;
199
200       structure = gst_caps_get_structure (otherpadcaps, 0);
201
202       /* construct mode, if we can */
203       mode_str = gst_structure_get_string (structure, "encoding-name");
204       if (mode_str) {
205         if (!strcmp (mode_str, "BV16"))
206           mode = 16;
207         else if (!strcmp (mode_str, "BV32"))
208           mode = 32;
209         else
210           mode = -1;
211
212         if (mode == 16 || mode == 32) {
213           caps = gst_caps_make_writable (caps);
214           structure = gst_caps_get_structure (caps, 0);
215           gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
216         }
217       }
218     }
219     gst_caps_unref (otherpadcaps);
220   }
221
222   if (filter) {
223     GstCaps *tmp;
224
225     GST_DEBUG_OBJECT (rtppayload, "Intersect %" GST_PTR_FORMAT " and filter %"
226         GST_PTR_FORMAT, caps, filter);
227     tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
228     gst_caps_unref (caps);
229     caps = tmp;
230   }
231
232   return caps;
233 }
234
235 gboolean
236 gst_rtp_bv_pay_plugin_init (GstPlugin * plugin)
237 {
238   return gst_element_register (plugin, "rtpbvpay",
239       GST_RANK_SECONDARY, GST_TYPE_RTP_BV_PAY);
240 }