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