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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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 #include <gst/audio/audio.h>
29 
30 #include "gstrtpgsmpay.h"
31 #include "gstrtputils.h"
32 
33 GST_DEBUG_CATEGORY_STATIC (rtpgsmpay_debug);
34 #define GST_CAT_DEFAULT (rtpgsmpay_debug)
35 
36 static GstStaticPadTemplate gst_rtp_gsm_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) 8000, " "channels = (int) 1")
41     );
42 
43 static GstStaticPadTemplate gst_rtp_gsm_pay_src_template =
44     GST_STATIC_PAD_TEMPLATE ("src",
45     GST_PAD_SRC,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("application/x-rtp, "
48         "media = (string) \"audio\", "
49         "payload = (int) " GST_RTP_PAYLOAD_GSM_STRING ", "
50         "clock-rate = (int) 8000, " "encoding-name = (string) \"GSM\"; "
51         "application/x-rtp, "
52         "media = (string) \"audio\", "
53         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
54         "clock-rate = (int) 8000, " "encoding-name = (string) \"GSM\"")
55     );
56 
57 static gboolean gst_rtp_gsm_pay_setcaps (GstRTPBasePayload * payload,
58     GstCaps * caps);
59 static GstFlowReturn gst_rtp_gsm_pay_handle_buffer (GstRTPBasePayload * payload,
60     GstBuffer * buffer);
61 
62 #define gst_rtp_gsm_pay_parent_class parent_class
63 G_DEFINE_TYPE (GstRTPGSMPay, gst_rtp_gsm_pay, GST_TYPE_RTP_BASE_PAYLOAD);
64 
65 static void
gst_rtp_gsm_pay_class_init(GstRTPGSMPayClass * klass)66 gst_rtp_gsm_pay_class_init (GstRTPGSMPayClass * klass)
67 {
68   GstElementClass *gstelement_class;
69   GstRTPBasePayloadClass *gstrtpbasepayload_class;
70 
71   GST_DEBUG_CATEGORY_INIT (rtpgsmpay_debug, "rtpgsmpay", 0,
72       "GSM Audio RTP Payloader");
73 
74   gstelement_class = (GstElementClass *) klass;
75   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
76 
77   gst_element_class_add_static_pad_template (gstelement_class,
78       &gst_rtp_gsm_pay_sink_template);
79   gst_element_class_add_static_pad_template (gstelement_class,
80       &gst_rtp_gsm_pay_src_template);
81 
82   gst_element_class_set_static_metadata (gstelement_class, "RTP GSM payloader",
83       "Codec/Payloader/Network/RTP",
84       "Payload-encodes GSM audio into a RTP packet",
85       "Zeeshan Ali <zeenix@gmail.com>");
86 
87   gstrtpbasepayload_class->set_caps = gst_rtp_gsm_pay_setcaps;
88   gstrtpbasepayload_class->handle_buffer = gst_rtp_gsm_pay_handle_buffer;
89 }
90 
91 static void
gst_rtp_gsm_pay_init(GstRTPGSMPay * rtpgsmpay)92 gst_rtp_gsm_pay_init (GstRTPGSMPay * rtpgsmpay)
93 {
94   GST_RTP_BASE_PAYLOAD (rtpgsmpay)->clock_rate = 8000;
95   GST_RTP_BASE_PAYLOAD_PT (rtpgsmpay) = GST_RTP_PAYLOAD_GSM;
96 }
97 
98 static gboolean
gst_rtp_gsm_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)99 gst_rtp_gsm_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
100 {
101   const char *stname;
102   GstStructure *structure;
103   gboolean res;
104 
105   structure = gst_caps_get_structure (caps, 0);
106 
107   stname = gst_structure_get_name (structure);
108 
109   if (strcmp ("audio/x-gsm", stname))
110     goto invalid_type;
111 
112   gst_rtp_base_payload_set_options (payload, "audio",
113       payload->pt != GST_RTP_PAYLOAD_GSM, "GSM", 8000);
114   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
115 
116   return res;
117 
118   /* ERRORS */
119 invalid_type:
120   {
121     GST_WARNING_OBJECT (payload, "invalid media type received");
122     return FALSE;
123   }
124 }
125 
126 static GstFlowReturn
gst_rtp_gsm_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)127 gst_rtp_gsm_pay_handle_buffer (GstRTPBasePayload * basepayload,
128     GstBuffer * buffer)
129 {
130   GstRTPGSMPay *rtpgsmpay;
131   guint payload_len;
132   GstBuffer *outbuf;
133   GstClockTime timestamp, duration;
134   GstFlowReturn ret;
135 
136   rtpgsmpay = GST_RTP_GSM_PAY (basepayload);
137 
138   timestamp = GST_BUFFER_PTS (buffer);
139   duration = GST_BUFFER_DURATION (buffer);
140 
141   /* FIXME, only one GSM frame per RTP packet for now */
142   payload_len = gst_buffer_get_size (buffer);
143 
144   /* FIXME, just error out for now */
145   if (payload_len > GST_RTP_BASE_PAYLOAD_MTU (rtpgsmpay))
146     goto too_big;
147 
148   outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
149 
150   /* copy timestamp and duration */
151   GST_BUFFER_PTS (outbuf) = timestamp;
152   GST_BUFFER_DURATION (outbuf) = duration;
153 
154   gst_rtp_copy_audio_meta (rtpgsmpay, outbuf, buffer);
155 
156   /* append payload */
157   outbuf = gst_buffer_append (outbuf, buffer);
158 
159   GST_DEBUG ("gst_rtp_gsm_pay_chain: pushing buffer of size %" G_GSIZE_FORMAT,
160       gst_buffer_get_size (outbuf));
161 
162   ret = gst_rtp_base_payload_push (basepayload, outbuf);
163 
164   return ret;
165 
166   /* ERRORS */
167 too_big:
168   {
169     GST_ELEMENT_ERROR (rtpgsmpay, STREAM, ENCODE, (NULL),
170         ("payload_len %u > mtu %u", payload_len,
171             GST_RTP_BASE_PAYLOAD_MTU (rtpgsmpay)));
172     return GST_FLOW_ERROR;
173   }
174 }
175 
176 gboolean
gst_rtp_gsm_pay_plugin_init(GstPlugin * plugin)177 gst_rtp_gsm_pay_plugin_init (GstPlugin * plugin)
178 {
179   return gst_element_register (plugin, "rtpgsmpay",
180       GST_RANK_SECONDARY, GST_TYPE_RTP_GSM_PAY);
181 }
182