rtpac3depay: should output audio/x-ac3 not audio/ac3
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpmpvpay.c
1 /* GStreamer
2  * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
28
29 #include "gstrtpelements.h"
30 #include "gstrtpmpvpay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpmpvpay_debug);
34 #define GST_CAT_DEFAULT (rtpmpvpay_debug)
35
36 static GstStaticPadTemplate gst_rtp_mpv_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("video/mpeg, "
41         "mpegversion = (int) 2, systemstream = (boolean) FALSE")
42     );
43
44 static GstStaticPadTemplate gst_rtp_mpv_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) \"video\", "
50         "payload = (int) " GST_RTP_PAYLOAD_MPV_STRING ", "
51         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"; "
52         "application/x-rtp, "
53         "media = (string) \"video\", "
54         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"")
56     );
57
58 static GstStateChangeReturn gst_rtp_mpv_pay_change_state (GstElement * element,
59     GstStateChange transition);
60
61 static void gst_rtp_mpv_pay_finalize (GObject * object);
62
63 static GstFlowReturn gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay);
64 static gboolean gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload,
65     GstCaps * caps);
66 static GstFlowReturn gst_rtp_mpv_pay_handle_buffer (GstRTPBasePayload *
67     payload, GstBuffer * buffer);
68 static gboolean gst_rtp_mpv_pay_sink_event (GstRTPBasePayload * payload,
69     GstEvent * event);
70
71 #define gst_rtp_mpv_pay_parent_class parent_class
72 G_DEFINE_TYPE (GstRTPMPVPay, gst_rtp_mpv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
73 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpmpvpay, "rtpmpvpay",
74     GST_RANK_SECONDARY, GST_TYPE_RTP_MPV_PAY, rtp_element_init (plugin));
75
76 static void
77 gst_rtp_mpv_pay_class_init (GstRTPMPVPayClass * klass)
78 {
79   GObjectClass *gobject_class;
80   GstElementClass *gstelement_class;
81   GstRTPBasePayloadClass *gstrtpbasepayload_class;
82
83   gobject_class = (GObjectClass *) klass;
84   gstelement_class = (GstElementClass *) klass;
85   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
86
87   gobject_class->finalize = gst_rtp_mpv_pay_finalize;
88
89   gstelement_class->change_state = gst_rtp_mpv_pay_change_state;
90
91   gst_element_class_add_static_pad_template (gstelement_class,
92       &gst_rtp_mpv_pay_sink_template);
93   gst_element_class_add_static_pad_template (gstelement_class,
94       &gst_rtp_mpv_pay_src_template);
95
96   gst_element_class_set_static_metadata (gstelement_class,
97       "RTP MPEG2 ES video payloader", "Codec/Payloader/Network/RTP",
98       "Payload-encodes MPEG2 ES into RTP packets (RFC 2250)",
99       "Thijs Vermeir <thijsvermeir@gmail.com>");
100
101   gstrtpbasepayload_class->set_caps = gst_rtp_mpv_pay_setcaps;
102   gstrtpbasepayload_class->handle_buffer = gst_rtp_mpv_pay_handle_buffer;
103   gstrtpbasepayload_class->sink_event = gst_rtp_mpv_pay_sink_event;
104
105   GST_DEBUG_CATEGORY_INIT (rtpmpvpay_debug, "rtpmpvpay", 0,
106       "MPEG2 ES Video RTP Payloader");
107 }
108
109 static void
110 gst_rtp_mpv_pay_init (GstRTPMPVPay * rtpmpvpay)
111 {
112   GST_RTP_BASE_PAYLOAD (rtpmpvpay)->clock_rate = 90000;
113   GST_RTP_BASE_PAYLOAD_PT (rtpmpvpay) = GST_RTP_PAYLOAD_MPV;
114
115   rtpmpvpay->adapter = gst_adapter_new ();
116 }
117
118 static void
119 gst_rtp_mpv_pay_finalize (GObject * object)
120 {
121   GstRTPMPVPay *rtpmpvpay;
122
123   rtpmpvpay = GST_RTP_MPV_PAY (object);
124
125   g_object_unref (rtpmpvpay->adapter);
126   rtpmpvpay->adapter = NULL;
127
128   G_OBJECT_CLASS (parent_class)->finalize (object);
129 }
130
131 static void
132 gst_rtp_mpv_pay_reset (GstRTPMPVPay * pay)
133 {
134   pay->first_ts = -1;
135   pay->duration = 0;
136   gst_adapter_clear (pay->adapter);
137   GST_DEBUG_OBJECT (pay, "reset depayloader");
138 }
139
140 static gboolean
141 gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
142 {
143   gst_rtp_base_payload_set_options (payload, "video",
144       payload->pt != GST_RTP_PAYLOAD_MPV, "MPV", 90000);
145   return gst_rtp_base_payload_set_outcaps (payload, NULL);
146 }
147
148 static gboolean
149 gst_rtp_mpv_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
150 {
151   gboolean ret;
152   GstRTPMPVPay *rtpmpvpay;
153
154   rtpmpvpay = GST_RTP_MPV_PAY (payload);
155
156   switch (GST_EVENT_TYPE (event)) {
157     case GST_EVENT_EOS:
158       /* make sure we push the last packets in the adapter on EOS */
159       gst_rtp_mpv_pay_flush (rtpmpvpay);
160       break;
161     case GST_EVENT_FLUSH_STOP:
162       gst_rtp_mpv_pay_reset (rtpmpvpay);
163       break;
164     default:
165       break;
166   }
167
168   ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
169
170   return ret;
171 }
172
173 #define RTP_HEADER_LEN 12
174
175 static GstFlowReturn
176 gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay)
177 {
178   GstFlowReturn ret;
179   guint avail;
180   GstBufferList *list;
181   GstBuffer *outbuf;
182
183   guint8 *payload;
184
185   avail = gst_adapter_available (rtpmpvpay->adapter);
186
187   ret = GST_FLOW_OK;
188
189   GST_DEBUG_OBJECT (rtpmpvpay, "available %u", avail);
190   if (avail == 0)
191     return GST_FLOW_OK;
192
193   list =
194       gst_buffer_list_new_sized (avail / (GST_RTP_BASE_PAYLOAD_MTU (rtpmpvpay) -
195           RTP_HEADER_LEN) + 1);
196
197   while (avail > 0) {
198     guint towrite;
199     guint packet_len;
200     guint payload_len;
201     GstRTPBuffer rtp = { NULL };
202     GstBuffer *paybuf;
203
204     packet_len = gst_rtp_buffer_calc_packet_len (avail + 4, 0, 0);
205
206     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpvpay));
207
208     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
209
210     outbuf =
211         gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
212         (rtpmpvpay), 4, 0, 0);
213
214     payload_len -= 4;
215
216     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
217
218     payload = gst_rtp_buffer_get_payload (&rtp);
219     /* enable MPEG Video-specific header
220      *
221      *  0                   1                   2                   3
222      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
223      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
224      * |    MBZ  |T|         TR        | |N|S|B|E|  P  | | BFC | | FFC |
225      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
226      *                                  AN              FBV     FFV
227      */
228
229     /* fill in the MPEG Video-specific header 
230      * data is set to 0x0 here
231      */
232     memset (payload, 0x0, 4);
233
234     avail -= payload_len;
235
236     gst_rtp_buffer_set_marker (&rtp, avail == 0);
237     if (avail == 0)
238       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
239     gst_rtp_buffer_unmap (&rtp);
240
241     paybuf = gst_adapter_take_buffer_fast (rtpmpvpay->adapter, payload_len);
242     gst_rtp_copy_video_meta (rtpmpvpay, outbuf, paybuf);
243     outbuf = gst_buffer_append (outbuf, paybuf);
244
245     GST_DEBUG_OBJECT (rtpmpvpay, "Adding buffer");
246
247     GST_BUFFER_PTS (outbuf) = rtpmpvpay->first_ts;
248     gst_buffer_list_add (list, outbuf);
249   }
250
251   ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmpvpay), list);
252
253   return ret;
254 }
255
256 static GstFlowReturn
257 gst_rtp_mpv_pay_handle_buffer (GstRTPBasePayload * basepayload,
258     GstBuffer * buffer)
259 {
260   GstRTPMPVPay *rtpmpvpay;
261   guint avail, packet_len;
262   GstClockTime timestamp, duration;
263   GstFlowReturn ret = GST_FLOW_OK;
264
265   rtpmpvpay = GST_RTP_MPV_PAY (basepayload);
266
267   timestamp = GST_BUFFER_PTS (buffer);
268   duration = GST_BUFFER_DURATION (buffer);
269
270   if (GST_BUFFER_IS_DISCONT (buffer)) {
271     GST_DEBUG_OBJECT (rtpmpvpay, "DISCONT");
272     gst_rtp_mpv_pay_reset (rtpmpvpay);
273   }
274
275   avail = gst_adapter_available (rtpmpvpay->adapter);
276
277   if (duration == -1)
278     duration = 0;
279
280   if (rtpmpvpay->first_ts == GST_CLOCK_TIME_NONE || avail == 0)
281     rtpmpvpay->first_ts = timestamp;
282
283   if (avail == 0) {
284     rtpmpvpay->duration = duration;
285   } else {
286     rtpmpvpay->duration += duration;
287   }
288
289   gst_adapter_push (rtpmpvpay->adapter, buffer);
290   avail = gst_adapter_available (rtpmpvpay->adapter);
291
292   /* get packet length of previous data and this new data,
293    * payload length includes a 4 byte MPEG video-specific header */
294   packet_len = gst_rtp_buffer_calc_packet_len (avail, 4, 0);
295   GST_LOG_OBJECT (rtpmpvpay, "available %d, rtp packet length %d", avail,
296       packet_len);
297
298   if (gst_rtp_base_payload_is_filled (basepayload,
299           packet_len, rtpmpvpay->duration)) {
300     ret = gst_rtp_mpv_pay_flush (rtpmpvpay);
301   } else {
302     rtpmpvpay->first_ts = timestamp;
303   }
304
305   return ret;
306 }
307
308 static GstStateChangeReturn
309 gst_rtp_mpv_pay_change_state (GstElement * element, GstStateChange transition)
310 {
311   GstRTPMPVPay *rtpmpvpay;
312   GstStateChangeReturn ret;
313
314   rtpmpvpay = GST_RTP_MPV_PAY (element);
315
316   switch (transition) {
317     case GST_STATE_CHANGE_READY_TO_PAUSED:
318       gst_rtp_mpv_pay_reset (rtpmpvpay);
319       break;
320     default:
321       break;
322   }
323
324   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
325
326   switch (transition) {
327     case GST_STATE_CHANGE_PAUSED_TO_READY:
328       gst_rtp_mpv_pay_reset (rtpmpvpay);
329       break;
330     default:
331       break;
332   }
333   return ret;
334 }