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