Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpgsmpay.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2005> Zeeshan Ali <zeenix@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include "gstrtpgsmpay.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtpgsmpay_debug);
32 #define GST_CAT_DEFAULT (rtpgsmpay_debug)
33
34 static GstStaticPadTemplate gst_rtp_gsm_pay_sink_template =
35 GST_STATIC_PAD_TEMPLATE ("sink",
36     GST_PAD_SINK,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) 8000, " "channels = (int) 1")
39     );
40
41 static GstStaticPadTemplate gst_rtp_gsm_pay_src_template =
42     GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-rtp, "
46         "media = (string) \"audio\", "
47         "payload = (int) " GST_RTP_PAYLOAD_GSM_STRING ", "
48         "clock-rate = (int) 8000, " "encoding-name = (string) \"GSM\"; "
49         "application/x-rtp, "
50         "media = (string) \"audio\", "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) 8000, " "encoding-name = (string) \"GSM\"")
53     );
54
55 static gboolean gst_rtp_gsm_pay_setcaps (GstBaseRTPPayload * payload,
56     GstCaps * caps);
57 static GstFlowReturn gst_rtp_gsm_pay_handle_buffer (GstBaseRTPPayload * payload,
58     GstBuffer * buffer);
59
60 #define gst_rtp_gsm_pay_parent_class parent_class
61 G_DEFINE_TYPE (GstRTPGSMPay, gst_rtp_gsm_pay, GST_TYPE_BASE_RTP_PAYLOAD);
62
63 static void
64 gst_rtp_gsm_pay_class_init (GstRTPGSMPayClass * klass)
65 {
66   GstElementClass *gstelement_class;
67   GstBaseRTPPayloadClass *gstbasertppayload_class;
68
69   GST_DEBUG_CATEGORY_INIT (rtpgsmpay_debug, "rtpgsmpay", 0,
70       "GSM Audio RTP Payloader");
71
72   gstelement_class = (GstElementClass *) klass;
73   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
74
75   gst_element_class_add_pad_template (gstelement_class,
76       gst_static_pad_template_get (&gst_rtp_gsm_pay_sink_template));
77   gst_element_class_add_pad_template (gstelement_class,
78       gst_static_pad_template_get (&gst_rtp_gsm_pay_src_template));
79
80   gst_element_class_set_details_simple (gstelement_class, "RTP GSM payloader",
81       "Codec/Payloader/Network/RTP",
82       "Payload-encodes GSM audio into a RTP packet",
83       "Zeeshan Ali <zeenix@gmail.com>");
84
85   gstbasertppayload_class->set_caps = gst_rtp_gsm_pay_setcaps;
86   gstbasertppayload_class->handle_buffer = gst_rtp_gsm_pay_handle_buffer;
87 }
88
89 static void
90 gst_rtp_gsm_pay_init (GstRTPGSMPay * rtpgsmpay)
91 {
92   GST_BASE_RTP_PAYLOAD (rtpgsmpay)->clock_rate = 8000;
93   GST_BASE_RTP_PAYLOAD_PT (rtpgsmpay) = GST_RTP_PAYLOAD_GSM;
94 }
95
96 static gboolean
97 gst_rtp_gsm_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
98 {
99   const char *stname;
100   GstStructure *structure;
101   gboolean res;
102
103   structure = gst_caps_get_structure (caps, 0);
104
105   stname = gst_structure_get_name (structure);
106
107   if (strcmp ("audio/x-gsm", stname))
108     goto invalid_type;
109
110   gst_basertppayload_set_options (payload, "audio", FALSE, "GSM", 8000);
111   res = gst_basertppayload_set_outcaps (payload, NULL);
112
113   return res;
114
115   /* ERRORS */
116 invalid_type:
117   {
118     GST_WARNING_OBJECT (payload, "invalid media type received");
119     return FALSE;
120   }
121 }
122
123 static GstFlowReturn
124 gst_rtp_gsm_pay_handle_buffer (GstBaseRTPPayload * basepayload,
125     GstBuffer * buffer)
126 {
127   GstRTPGSMPay *rtpgsmpay;
128   guint payload_len;
129   GstBuffer *outbuf;
130   guint8 *payload, *data;
131   GstClockTime timestamp, duration;
132   GstFlowReturn ret;
133   gsize size;
134   GstRTPBuffer rtp = { NULL };
135
136   rtpgsmpay = GST_RTP_GSM_PAY (basepayload);
137
138   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
139
140   timestamp = GST_BUFFER_TIMESTAMP (buffer);
141   duration = GST_BUFFER_DURATION (buffer);
142
143   /* FIXME, only one GSM frame per RTP packet for now */
144   payload_len = size;
145
146   /* FIXME, just error out for now */
147   if (payload_len > GST_BASE_RTP_PAYLOAD_MTU (rtpgsmpay))
148     goto too_big;
149
150   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
151
152   /* copy timestamp and duration */
153   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
154   GST_BUFFER_DURATION (outbuf) = duration;
155
156   /* get payload */
157   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
158
159   /* copy data in payload */
160   payload = gst_rtp_buffer_get_payload (&rtp);
161   memcpy (payload, data, size);
162
163   gst_rtp_buffer_unmap (&rtp);
164
165   gst_buffer_unmap (buffer, data, size);
166   gst_buffer_unref (buffer);
167
168   GST_DEBUG ("gst_rtp_gsm_pay_chain: pushing buffer of size %d",
169       gst_buffer_get_size (outbuf));
170
171   ret = gst_basertppayload_push (basepayload, outbuf);
172
173   return ret;
174
175   /* ERRORS */
176 too_big:
177   {
178     GST_ELEMENT_ERROR (rtpgsmpay, STREAM, ENCODE, (NULL),
179         ("payload_len %u > mtu %u", payload_len,
180             GST_BASE_RTP_PAYLOAD_MTU (rtpgsmpay)));
181     gst_buffer_unmap (buffer, data, size);
182     return GST_FLOW_ERROR;
183   }
184 }
185
186 gboolean
187 gst_rtp_gsm_pay_plugin_init (GstPlugin * plugin)
188 {
189   return gst_element_register (plugin, "rtpgsmpay",
190       GST_RANK_SECONDARY, GST_TYPE_RTP_GSM_PAY);
191 }