rtpvp8: port some more to new memory API
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpg722pay.c
1 /* GStreamer
2  * Copyright (C) <2010> 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 <string.h>
25
26 #include <gst/audio/audio.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include "gstrtpg722pay.h"
30 #include "gstrtpchannels.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpg722pay_debug);
33 #define GST_CAT_DEFAULT (rtpg722pay_debug)
34
35 static GstStaticPadTemplate gst_rtp_g722_pay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
37     GST_PAD_SINK,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/G722, " "rate = (int) 16000, " "channels = (int) 1")
40     );
41
42 static GstStaticPadTemplate gst_rtp_g722_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
44     GST_PAD_SRC,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("application/x-rtp, "
47         "media = (string) \"audio\", "
48         "encoding-name = (string) \"G722\", "
49         "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
50         "clock-rate = (int) 8000")
51     );
52
53 static gboolean gst_rtp_g722_pay_setcaps (GstRTPBasePayload * basepayload,
54     GstCaps * caps);
55 static GstCaps *gst_rtp_g722_pay_getcaps (GstRTPBasePayload * rtppayload,
56     GstPad * pad, GstCaps * filter);
57
58 #define gst_rtp_g722_pay_parent_class parent_class
59 G_DEFINE_TYPE (GstRtpG722Pay, gst_rtp_g722_pay,
60     GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
61
62 static void
63 gst_rtp_g722_pay_class_init (GstRtpG722PayClass * klass)
64 {
65   GstElementClass *gstelement_class;
66   GstRTPBasePayloadClass *gstrtpbasepayload_class;
67
68   GST_DEBUG_CATEGORY_INIT (rtpg722pay_debug, "rtpg722pay", 0,
69       "G722 RTP Payloader");
70
71   gstelement_class = (GstElementClass *) klass;
72   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
73
74   gst_element_class_add_pad_template (gstelement_class,
75       gst_static_pad_template_get (&gst_rtp_g722_pay_src_template));
76   gst_element_class_add_pad_template (gstelement_class,
77       gst_static_pad_template_get (&gst_rtp_g722_pay_sink_template));
78
79   gst_element_class_set_static_metadata (gstelement_class,
80       "RTP audio payloader", "Codec/Payloader/Network/RTP",
81       "Payload-encode Raw audio into RTP packets (RFC 3551)",
82       "Wim Taymans <wim.taymans@gmail.com>");
83
84   gstrtpbasepayload_class->set_caps = gst_rtp_g722_pay_setcaps;
85   gstrtpbasepayload_class->get_caps = gst_rtp_g722_pay_getcaps;
86 }
87
88 static void
89 gst_rtp_g722_pay_init (GstRtpG722Pay * rtpg722pay)
90 {
91   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
92
93   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpg722pay);
94
95   /* tell rtpbaseaudiopayload that this is a sample based codec */
96   gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
97 }
98
99 static gboolean
100 gst_rtp_g722_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
101 {
102   GstRtpG722Pay *rtpg722pay;
103   GstStructure *structure;
104   gint rate, channels, clock_rate;
105   gboolean res;
106   gchar *params;
107 #if 0
108   GstAudioChannelPosition *pos;
109   const GstRTPChannelOrder *order;
110 #endif
111   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
112
113   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
114   rtpg722pay = GST_RTP_G722_PAY (basepayload);
115
116   structure = gst_caps_get_structure (caps, 0);
117
118   /* first parse input caps */
119   if (!gst_structure_get_int (structure, "rate", &rate))
120     goto no_rate;
121
122   if (!gst_structure_get_int (structure, "channels", &channels))
123     goto no_channels;
124
125   /* FIXME: Do something with the channel positions */
126 #if 0
127   /* get the channel order */
128   pos = gst_audio_get_channel_positions (structure);
129   if (pos)
130     order = gst_rtp_channels_get_by_pos (channels, pos);
131   else
132     order = NULL;
133 #endif
134
135   /* Clock rate is always 8000 Hz for G722 according to
136    * RFC 3551 although the sampling rate is 16000 Hz */
137   clock_rate = 8000;
138
139   gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "G722",
140       clock_rate);
141   params = g_strdup_printf ("%d", channels);
142
143 #if 0
144   if (!order && channels > 2) {
145     GST_ELEMENT_WARNING (rtpg722pay, STREAM, DECODE,
146         (NULL), ("Unknown channel order for %d channels", channels));
147   }
148
149   if (order && order->name) {
150     res = gst_rtp_base_payload_set_outcaps (basepayload,
151         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
152         channels, "channel-order", G_TYPE_STRING, order->name, NULL);
153   } else {
154 #endif
155     res = gst_rtp_base_payload_set_outcaps (basepayload,
156         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
157         channels, NULL);
158 #if 0
159   }
160 #endif
161
162   g_free (params);
163 #if 0
164   g_free (pos);
165 #endif
166
167   rtpg722pay->rate = rate;
168   rtpg722pay->channels = channels;
169
170   /* bits-per-sample is 4 * channels for G722, but as the RTP clock runs at
171    * half speed (8 instead of 16 khz), pretend it's 8 bits per sample
172    * channels. */
173   gst_rtp_base_audio_payload_set_samplebits_options (rtpbaseaudiopayload,
174       8 * rtpg722pay->channels);
175
176   return res;
177
178   /* ERRORS */
179 no_rate:
180   {
181     GST_DEBUG_OBJECT (rtpg722pay, "no rate given");
182     return FALSE;
183   }
184 no_channels:
185   {
186     GST_DEBUG_OBJECT (rtpg722pay, "no channels given");
187     return FALSE;
188   }
189 }
190
191 static GstCaps *
192 gst_rtp_g722_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
193     GstCaps * filter)
194 {
195   GstCaps *otherpadcaps;
196   GstCaps *caps;
197
198   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
199   caps = gst_pad_get_pad_template_caps (pad);
200
201   if (otherpadcaps) {
202     if (!gst_caps_is_empty (otherpadcaps)) {
203       caps = gst_caps_make_writable (caps);
204       gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
205       gst_caps_set_simple (caps, "rate", G_TYPE_INT, 16000, NULL);
206     }
207     gst_caps_unref (otherpadcaps);
208   }
209   return caps;
210 }
211
212 gboolean
213 gst_rtp_g722_pay_plugin_init (GstPlugin * plugin)
214 {
215   return gst_element_register (plugin, "rtpg722pay",
216       GST_RANK_SECONDARY, GST_TYPE_RTP_G722_PAY);
217 }