gst/rtp/: Added G729 pay and depayloaders. Fixes #532409.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpg729pay.c
1 /* GStreamer
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Library General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 02111-1307, USA.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "gstrtpg729pay.h"
24 #include <gst/rtp/gstrtpbuffer.h>
25
26 /* elementfactory information */
27 static GstElementDetails gst_rtpg729pay_details = {
28   "RTP Payloader for G729 Audio",
29   "Codec/Payloader/Network",
30   "Packetize G729 audio streams into RTP packets",
31   "Laurent Glayal <spglegle@yahoo.fr>"
32 };
33
34 GST_DEBUG_CATEGORY_STATIC (rtpg729pay_debug);
35 #define GST_CAT_DEFAULT (rtpg729pay_debug)
36
37 static GstStaticPadTemplate gst_rtpg729pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
39     GST_PAD_SINK,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("audio/G729, channels=(int)1, rate=(int)8000")
42     );
43
44 static GstStaticPadTemplate gst_rtpg729pay_src_template =
45     GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) \"audio\", "
50         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51         "clock-rate = (int) 8000, " "encoding-name = (string) \"G729\";"
52         "application/x-rtp, "
53         "media = (string) \"audio\", "
54         "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
55         "clock-rate = (int) 8000")
56     );
57
58 static gboolean gst_rtpg729pay_setcaps (GstBaseRTPPayload * payload,
59     GstCaps * caps);
60
61 GST_BOILERPLATE (GstRtpG729Pay, gst_rtpg729pay, GstBaseRTPAudioPayload,
62     GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
63
64 static void
65 gst_rtpg729pay_base_init (gpointer klass)
66 {
67   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
68
69   gst_element_class_add_pad_template (element_class,
70       gst_static_pad_template_get (&gst_rtpg729pay_sink_template));
71   gst_element_class_add_pad_template (element_class,
72       gst_static_pad_template_get (&gst_rtpg729pay_src_template));
73   gst_element_class_set_details (element_class, &gst_rtpg729pay_details);
74 }
75
76 static void
77 gst_rtpg729pay_class_init (GstRtpG729PayClass * klass)
78 {
79   GObjectClass *gobject_class;
80   GstElementClass *gstelement_class;
81   GstBaseRTPPayloadClass *gstbasertppayload_class;
82
83   gobject_class = (GObjectClass *) klass;
84   gstelement_class = (GstElementClass *) klass;
85   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
86
87   parent_class = g_type_class_ref (GST_TYPE_BASE_RTP_PAYLOAD);
88
89   gstbasertppayload_class->set_caps = gst_rtpg729pay_setcaps;
90
91   GST_DEBUG_CATEGORY_INIT (rtpg729pay_debug, "rtpg729pay", 0,
92       "G729 audio RTP payloader");
93 }
94
95 static void
96 gst_rtpg729pay_init (GstRtpG729Pay * rtpg729pay, GstRtpG729PayClass * klass)
97 {
98   GstBaseRTPPayload *basertppayload;
99   GstBaseRTPAudioPayload *basertpaudiopayload;
100
101   basertppayload = GST_BASE_RTP_PAYLOAD (rtpg729pay);
102   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpg729pay);
103
104   /* we don't set the payload type, it should be set by the application using
105    * the pt property or the default 96 will be used */
106   basertppayload->clock_rate = 8000;
107
108   /* tell basertpaudiopayload that this is a frame based codec */
109   gst_base_rtp_audio_payload_set_frame_based (basertpaudiopayload);
110   gst_basertppayload_set_options (basertppayload, "audio", FALSE, "G729", 8000);
111   gst_base_rtp_audio_payload_set_frame_options (basertpaudiopayload, 10, 10);
112 }
113
114 static gboolean
115 gst_rtpg729pay_setcaps (GstBaseRTPPayload * basertppayload, GstCaps * caps)
116 {
117   GstRtpG729Pay *rtpg729pay;
118   GstBaseRTPAudioPayload *basertpaudiopayload;
119   gboolean ret;
120   GstStructure *structure;
121   const char *payload_name;
122
123   rtpg729pay = GST_RTP_G729_PAY (basertppayload);
124   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basertppayload);
125
126   structure = gst_caps_get_structure (caps, 0);
127
128   payload_name = gst_structure_get_name (structure);
129   if (g_strcasecmp ("audio/G729", payload_name) != 0)
130     goto wrong_name;
131
132   ret = gst_basertppayload_set_outcaps (basertppayload, NULL);
133
134   return ret;
135
136   /* ERRORS */
137 wrong_name:
138   {
139     GST_ERROR_OBJECT (rtpg729pay, "wrong name, expected 'audio/G729', got '%s'",
140         payload_name);
141     return FALSE;
142   }
143 }
144
145 gboolean
146 gst_rtp_g729_pay_plugin_init (GstPlugin * plugin)
147 {
148   return gst_element_register (plugin, "rtpg729pay",
149       GST_RANK_NONE, GST_TYPE_RTP_G729_PAY);
150 }