Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.git] / gst / rtp / gstrtppcmadepay.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 "gstrtppcmadepay.h"
29
30 /* RtpPcmaDepay signals and args */
31 enum
32 {
33   /* FILL ME */
34   LAST_SIGNAL
35 };
36
37 enum
38 {
39   ARG_0
40 };
41
42 static GstStaticPadTemplate gst_rtp_pcma_depay_sink_template =
43     GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("application/x-rtp, "
47         "media = (string) \"audio\", "
48         "payload = (int) " GST_RTP_PAYLOAD_PCMA_STRING ", "
49         "clock-rate = (int) 8000, encoding-name = (string) \"PCMA\";"
50         "application/x-rtp, "
51         "media = (string) \"audio\", "
52         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53         "clock-rate = (int) [1, MAX ], encoding-name = (string) \"PCMA\"")
54     );
55
56 static GstStaticPadTemplate gst_rtp_pcma_depay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("audio/x-alaw, channels = (int) 1, rate = (int) [1, MAX ]")
61     );
62
63 static GstBuffer *gst_rtp_pcma_depay_process (GstRTPBaseDepayload * depayload,
64     GstBuffer * buf);
65 static gboolean gst_rtp_pcma_depay_setcaps (GstRTPBaseDepayload * depayload,
66     GstCaps * caps);
67
68 #define gst_rtp_pcma_depay_parent_class parent_class
69 G_DEFINE_TYPE (GstRtpPcmaDepay, gst_rtp_pcma_depay,
70     GST_TYPE_RTP_BASE_DEPAYLOAD);
71
72 static void
73 gst_rtp_pcma_depay_class_init (GstRtpPcmaDepayClass * klass)
74 {
75   GstElementClass *gstelement_class;
76   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
77
78   gstelement_class = (GstElementClass *) klass;
79   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
80
81   gst_element_class_add_pad_template (gstelement_class,
82       gst_static_pad_template_get (&gst_rtp_pcma_depay_src_template));
83   gst_element_class_add_pad_template (gstelement_class,
84       gst_static_pad_template_get (&gst_rtp_pcma_depay_sink_template));
85
86   gst_element_class_set_static_metadata (gstelement_class,
87       "RTP PCMA depayloader", "Codec/Depayloader/Network/RTP",
88       "Extracts PCMA audio from RTP packets",
89       "Edgard Lima <edgard.lima@indt.org.br>, Zeeshan Ali <zeenix@gmail.com>");
90
91   gstrtpbasedepayload_class->process = gst_rtp_pcma_depay_process;
92   gstrtpbasedepayload_class->set_caps = gst_rtp_pcma_depay_setcaps;
93 }
94
95 static void
96 gst_rtp_pcma_depay_init (GstRtpPcmaDepay * rtppcmadepay)
97 {
98   GstRTPBaseDepayload *depayload;
99
100   depayload = GST_RTP_BASE_DEPAYLOAD (rtppcmadepay);
101
102   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
103 }
104
105 static gboolean
106 gst_rtp_pcma_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
107 {
108   GstCaps *srccaps;
109   GstStructure *structure;
110   gboolean ret;
111   gint clock_rate;
112
113   structure = gst_caps_get_structure (caps, 0);
114
115   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
116     clock_rate = 8000;          /* default */
117   depayload->clock_rate = clock_rate;
118
119   srccaps = gst_caps_new_simple ("audio/x-alaw",
120       "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
121   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
122   gst_caps_unref (srccaps);
123
124   return ret;
125 }
126
127 static GstBuffer *
128 gst_rtp_pcma_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
129 {
130   GstBuffer *outbuf = NULL;
131   gboolean marker;
132   guint len;
133   GstRTPBuffer rtp = { NULL };
134
135   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
136
137   marker = gst_rtp_buffer_get_marker (&rtp);
138
139   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
140       gst_buffer_get_size (buf), marker,
141       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
142
143   len = gst_rtp_buffer_get_payload_len (&rtp);
144   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
145   gst_rtp_buffer_unmap (&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 DISCONT */
153       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
154     }
155   }
156
157
158   return outbuf;
159 }
160
161 gboolean
162 gst_rtp_pcma_depay_plugin_init (GstPlugin * plugin)
163 {
164   return gst_element_register (plugin, "rtppcmadepay",
165       GST_RANK_SECONDARY, GST_TYPE_RTP_PCMA_DEPAY);
166 }