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 "gstrtpelements.h"
30 #include "gstrtppcmadepay.h"
31 #include "gstrtputils.h"
32
33 /* RtpPcmaDepay signals and args */
34 enum
35 {
36 /* FILL ME */
37 LAST_SIGNAL
38 };
39
40 enum
41 {
42 PROP_0
43 };
44
45 static GstStaticPadTemplate gst_rtp_pcma_depay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_PAD_SINK,
48 GST_PAD_ALWAYS,
49 GST_STATIC_CAPS ("application/x-rtp, "
50 "media = (string) \"audio\", "
51 "payload = (int) " GST_RTP_PAYLOAD_PCMA_STRING ", "
52 "clock-rate = (int) 8000;"
53 "application/x-rtp, "
54 "media = (string) \"audio\", "
55 "clock-rate = (int) [1, MAX ], encoding-name = (string) \"PCMA\"")
56 );
57
58 static GstStaticPadTemplate gst_rtp_pcma_depay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_PAD_SRC,
61 GST_PAD_ALWAYS,
62 GST_STATIC_CAPS ("audio/x-alaw, channels = (int) 1, rate = (int) [1, MAX ]")
63 );
64
65 static GstBuffer *gst_rtp_pcma_depay_process (GstRTPBaseDepayload * depayload,
66 GstRTPBuffer * rtp);
67 static gboolean gst_rtp_pcma_depay_setcaps (GstRTPBaseDepayload * depayload,
68 GstCaps * caps);
69
70 #define gst_rtp_pcma_depay_parent_class parent_class
71 G_DEFINE_TYPE (GstRtpPcmaDepay, gst_rtp_pcma_depay,
72 GST_TYPE_RTP_BASE_DEPAYLOAD);
73 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtppcmadepay, "rtppcmadepay",
74 GST_RANK_SECONDARY, GST_TYPE_RTP_PCMA_DEPAY, rtp_element_init (plugin));
75
76 static void
gst_rtp_pcma_depay_class_init(GstRtpPcmaDepayClass * klass)77 gst_rtp_pcma_depay_class_init (GstRtpPcmaDepayClass * klass)
78 {
79 GstElementClass *gstelement_class;
80 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
81
82 gstelement_class = (GstElementClass *) klass;
83 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
84
85 gst_element_class_add_static_pad_template (gstelement_class,
86 &gst_rtp_pcma_depay_src_template);
87 gst_element_class_add_static_pad_template (gstelement_class,
88 &gst_rtp_pcma_depay_sink_template);
89
90 gst_element_class_set_static_metadata (gstelement_class,
91 "RTP PCMA depayloader", "Codec/Depayloader/Network/RTP",
92 "Extracts PCMA audio from RTP packets",
93 "Edgard Lima <edgard.lima@gmail.com>, Zeeshan Ali <zeenix@gmail.com>");
94
95 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_pcma_depay_process;
96 gstrtpbasedepayload_class->set_caps = gst_rtp_pcma_depay_setcaps;
97 }
98
99 static void
gst_rtp_pcma_depay_init(GstRtpPcmaDepay * rtppcmadepay)100 gst_rtp_pcma_depay_init (GstRtpPcmaDepay * rtppcmadepay)
101 {
102 GstRTPBaseDepayload *depayload;
103
104 depayload = GST_RTP_BASE_DEPAYLOAD (rtppcmadepay);
105
106 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
107 }
108
109 static gboolean
gst_rtp_pcma_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)110 gst_rtp_pcma_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
111 {
112 GstCaps *srccaps;
113 GstStructure *structure;
114 gboolean ret;
115 gint clock_rate;
116
117 structure = gst_caps_get_structure (caps, 0);
118
119 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
120 clock_rate = 8000; /* default */
121 depayload->clock_rate = clock_rate;
122
123 srccaps = gst_caps_new_simple ("audio/x-alaw",
124 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
125 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
126 gst_caps_unref (srccaps);
127
128 return ret;
129 }
130
131 static GstBuffer *
gst_rtp_pcma_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)132 gst_rtp_pcma_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
133 {
134 GstBuffer *outbuf = NULL;
135 gboolean marker;
136 guint len;
137
138 marker = gst_rtp_buffer_get_marker (rtp);
139
140 GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
141 gst_buffer_get_size (rtp->buffer), marker,
142 gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
143
144 len = gst_rtp_buffer_get_payload_len (rtp);
145 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
146
147 if (outbuf) {
148 GST_BUFFER_DURATION (outbuf) =
149 gst_util_uint64_scale_int (len, GST_SECOND, depayload->clock_rate);
150
151 if (marker) {
152 /* mark start of talkspurt with RESYNC */
153 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
154 }
155
156 gst_rtp_drop_non_audio_meta (depayload, outbuf);
157 }
158
159 return outbuf;
160 }
161