Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / ext / opus / gstrtpopuspay.c
1 /*
2  * Opus Payloader Gst Element
3  *
4  *   @author: Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>
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
28 #include <gst/rtp/gstrtpbuffer.h>
29
30 #include "gstrtpopuspay.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpopuspay_debug);
33 #define GST_CAT_DEFAULT (rtpopuspay_debug)
34
35
36 static GstStaticPadTemplate gst_rtp_opus_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("audio/x-opus, multistream = (boolean) FALSE")
41     );
42
43 static GstStaticPadTemplate gst_rtp_opus_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_DYNAMIC_STRING ", "
50         "clock-rate = (int) 48000, "
51         "encoding-name = (string) \"X-GST-OPUS-DRAFT-SPITTKA-00\"")
52     );
53
54 static gboolean gst_rtp_opus_pay_setcaps (GstBaseRTPPayload * payload,
55     GstCaps * caps);
56 static GstFlowReturn gst_rtp_opus_pay_handle_buffer (GstBaseRTPPayload *
57     payload, GstBuffer * buffer);
58
59 GST_BOILERPLATE (GstRtpOPUSPay, gst_rtp_opus_pay, GstBaseRTPPayload,
60     GST_TYPE_BASE_RTP_PAYLOAD);
61
62 static void
63 gst_rtp_opus_pay_base_init (gpointer klass)
64 {
65   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
66
67   gst_element_class_add_pad_template (element_class,
68       gst_static_pad_template_get (&gst_rtp_opus_pay_src_template));
69   gst_element_class_add_pad_template (element_class,
70       gst_static_pad_template_get (&gst_rtp_opus_pay_sink_template));
71
72   gst_element_class_set_details_simple (element_class,
73       "RTP Opus payloader",
74       "Codec/Payloader/Network/RTP",
75       "Puts Opus audio in RTP packets",
76       "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
77 }
78
79 static void
80 gst_rtp_opus_pay_class_init (GstRtpOPUSPayClass * klass)
81 {
82   GstBaseRTPPayloadClass *gstbasertppayload_class;
83
84   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
85
86   gstbasertppayload_class->set_caps = gst_rtp_opus_pay_setcaps;
87   gstbasertppayload_class->handle_buffer = gst_rtp_opus_pay_handle_buffer;
88
89   GST_DEBUG_CATEGORY_INIT (rtpopuspay_debug, "rtpopuspay", 0,
90       "Opus RTP Payloader");
91 }
92
93 static void
94 gst_rtp_opus_pay_init (GstRtpOPUSPay * rtpopuspay, GstRtpOPUSPayClass * klass)
95 {
96 }
97
98 static gboolean
99 gst_rtp_opus_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
100 {
101   gboolean res;
102   gchar *capsstr;
103
104   capsstr = gst_caps_to_string (caps);
105
106   gst_basertppayload_set_options (payload, "audio", FALSE,
107       "X-GST-OPUS-DRAFT-SPITTKA-00", 48000);
108   res =
109       gst_basertppayload_set_outcaps (payload, "caps", G_TYPE_STRING, capsstr,
110       NULL);
111   g_free (capsstr);
112
113   return res;
114 }
115
116 static GstFlowReturn
117 gst_rtp_opus_pay_handle_buffer (GstBaseRTPPayload * basepayload,
118     GstBuffer * buffer)
119 {
120   GstBuffer *outbuf;
121   GstClockTime timestamp;
122
123   guint size;
124   guint8 *data;
125   guint8 *payload;
126
127   size = GST_BUFFER_SIZE (buffer);
128   data = GST_BUFFER_DATA (buffer);
129   timestamp = GST_BUFFER_TIMESTAMP (buffer);
130
131   outbuf = gst_rtp_buffer_new_allocate (size, 0, 0);
132   payload = gst_rtp_buffer_get_payload (outbuf);
133
134   memcpy (payload, data, size);
135
136   gst_rtp_buffer_set_marker (outbuf, FALSE);
137   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
138
139   return gst_basertppayload_push (basepayload, outbuf);
140 }