1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2005> Edgard Lima <edgard.lima@gmail.com>
4  * Copyright (C) <2005> Zeeshan Ali <zeenix@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include <gst/audio/audio.h>
29 #include "gstrtppcmudepay.h"
30 #include "gstrtputils.h"
31 
32 /* RtpPcmuDepay signals and args */
33 enum
34 {
35   /* FILL ME */
36   LAST_SIGNAL
37 };
38 
39 enum
40 {
41   PROP_0
42 };
43 
44 static GstStaticPadTemplate gst_rtp_pcmu_depay_sink_template =
45     GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) \"audio\", "
50         "payload = (int) " GST_RTP_PAYLOAD_PCMU_STRING ", "
51         "clock-rate = (int) 8000; "
52         "application/x-rtp, "
53         "media = (string) \"audio\", "
54         "encoding-name = (string) \"PCMU\", clock-rate = (int) [1, MAX ]")
55     );
56 
57 static GstStaticPadTemplate gst_rtp_pcmu_depay_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("audio/x-mulaw, "
62         "channels = (int) 1, rate = (int) [1, MAX ]")
63     );
64 
65 static GstBuffer *gst_rtp_pcmu_depay_process (GstRTPBaseDepayload * depayload,
66     GstRTPBuffer * rtp);
67 static gboolean gst_rtp_pcmu_depay_setcaps (GstRTPBaseDepayload * depayload,
68     GstCaps * caps);
69 
70 #define gst_rtp_pcmu_depay_parent_class parent_class
71 G_DEFINE_TYPE (GstRtpPcmuDepay, gst_rtp_pcmu_depay,
72     GST_TYPE_RTP_BASE_DEPAYLOAD);
73 
74 static void
gst_rtp_pcmu_depay_class_init(GstRtpPcmuDepayClass * klass)75 gst_rtp_pcmu_depay_class_init (GstRtpPcmuDepayClass * klass)
76 {
77   GstElementClass *gstelement_class;
78   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
79 
80   gstelement_class = (GstElementClass *) klass;
81   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
82 
83   gst_element_class_add_static_pad_template (gstelement_class,
84       &gst_rtp_pcmu_depay_src_template);
85   gst_element_class_add_static_pad_template (gstelement_class,
86       &gst_rtp_pcmu_depay_sink_template);
87 
88   gst_element_class_set_static_metadata (gstelement_class,
89       "RTP PCMU depayloader", "Codec/Depayloader/Network/RTP",
90       "Extracts PCMU audio from RTP packets",
91       "Edgard Lima <edgard.lima@gmail.com>, Zeeshan Ali <zeenix@gmail.com>");
92 
93   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_pcmu_depay_process;
94   gstrtpbasedepayload_class->set_caps = gst_rtp_pcmu_depay_setcaps;
95 }
96 
97 static void
gst_rtp_pcmu_depay_init(GstRtpPcmuDepay * rtppcmudepay)98 gst_rtp_pcmu_depay_init (GstRtpPcmuDepay * rtppcmudepay)
99 {
100   GstRTPBaseDepayload *depayload;
101 
102   depayload = GST_RTP_BASE_DEPAYLOAD (rtppcmudepay);
103 
104   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
105 }
106 
107 static gboolean
gst_rtp_pcmu_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)108 gst_rtp_pcmu_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
109 {
110   GstCaps *srccaps;
111   GstStructure *structure;
112   gboolean ret;
113   gint clock_rate;
114 
115   structure = gst_caps_get_structure (caps, 0);
116 
117   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
118     clock_rate = 8000;          /* default */
119   depayload->clock_rate = clock_rate;
120 
121   srccaps = gst_caps_new_simple ("audio/x-mulaw",
122       "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
123   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
124   gst_caps_unref (srccaps);
125 
126   return ret;
127 }
128 
129 static GstBuffer *
gst_rtp_pcmu_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)130 gst_rtp_pcmu_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
131 {
132   GstBuffer *outbuf = NULL;
133   guint len;
134   gboolean marker;
135 
136   marker = gst_rtp_buffer_get_marker (rtp);
137 
138   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
139       gst_buffer_get_size (rtp->buffer), marker,
140       gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
141 
142   len = gst_rtp_buffer_get_payload_len (rtp);
143   outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
144 
145   if (outbuf) {
146     GST_BUFFER_DURATION (outbuf) =
147         gst_util_uint64_scale_int (len, GST_SECOND, depayload->clock_rate);
148 
149     if (marker) {
150       /* mark start of talkspurt with RESYNC */
151       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
152     }
153 
154     gst_rtp_drop_non_audio_meta (depayload, outbuf);
155   }
156 
157   return outbuf;
158 }
159 
160 gboolean
gst_rtp_pcmu_depay_plugin_init(GstPlugin * plugin)161 gst_rtp_pcmu_depay_plugin_init (GstPlugin * plugin)
162 {
163   return gst_element_register (plugin, "rtppcmudepay",
164       GST_RANK_SECONDARY, GST_TYPE_RTP_PCMU_DEPAY);
165 }
166