reverting rtp patches to fix freeze break on -base as explained on the list
[platform/upstream/gstreamer.git] / gst / rtp / gstrtppcmupay.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> Nokia Corporation <kai.vehmanen@nokia.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 
15  */
16
17 #ifdef HAVE_CONFIG_H
18 #  include "config.h"
19 #endif
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <gst/rtp/gstrtpbuffer.h>
24
25 #include "gstrtppcmupay.h"
26
27 /* elementfactory information */
28 static GstElementDetails gst_rtp_pcmu_pay_details =
29 GST_ELEMENT_DETAILS ("RTP packet parser",
30     "Codec/Payloader/Network",
31     "Payload-encodes PCMU audio into a RTP packet",
32     "Edgard Lima <edgard.lima@indt.org.br>");
33
34 static GstStaticPadTemplate gst_rtp_pcmu_pay_sink_template =
35 GST_STATIC_PAD_TEMPLATE ("sink",
36     GST_PAD_SINK,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("audio/x-mulaw, channels=(int)1, rate=(int)8000")
39     );
40
41 static GstStaticPadTemplate gst_rtp_pcmu_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-rtp, "
46         "media = (string) \"audio\", "
47         "payload = (int) " GST_RTP_PAYLOAD_PCMU_STRING ", "
48         "clock-rate = (int) 8000, " "encoding-name = (string) \"PCMU\"")
49     );
50
51 static gboolean gst_rtp_pcmu_pay_setcaps (GstBaseRTPPayload * payload,
52     GstCaps * caps);
53 static GstFlowReturn gst_rtp_pcmu_pay_handle_buffer (GstBaseRTPPayload *
54     payload, GstBuffer * buffer);
55 static void gst_rtp_pcmu_pay_finalize (GObject * object);
56
57 GST_BOILERPLATE (GstRtpPcmuPay, gst_rtp_pcmu_pay, GstBaseRTPPayload,
58     GST_TYPE_BASE_RTP_PAYLOAD);
59
60 /* The lower limit for number of octet to put in one packet
61  * (clock-rate=8000, octet-per-sample=1). The default 80 is equal
62  * to to 10msec (see RFC3551) */
63 #define GST_RTP_PCMU_MIN_PTIME_OCTETS   80
64
65 static void
66 gst_rtp_pcmu_pay_base_init (gpointer klass)
67 {
68   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
69
70   gst_element_class_add_pad_template (element_class,
71       gst_static_pad_template_get (&gst_rtp_pcmu_pay_sink_template));
72   gst_element_class_add_pad_template (element_class,
73       gst_static_pad_template_get (&gst_rtp_pcmu_pay_src_template));
74   gst_element_class_set_details (element_class, &gst_rtp_pcmu_pay_details);
75 }
76
77 static void
78 gst_rtp_pcmu_pay_class_init (GstRtpPcmuPayClass * klass)
79 {
80   GObjectClass *gobject_class;
81   GstElementClass *gstelement_class;
82   GstBaseRTPPayloadClass *gstbasertppayload_class;
83
84   gobject_class = (GObjectClass *) klass;
85   gstelement_class = (GstElementClass *) klass;
86   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
87
88   parent_class = g_type_class_peek_parent (klass);
89   gobject_class->finalize = gst_rtp_pcmu_pay_finalize;
90
91   gstbasertppayload_class->set_caps = gst_rtp_pcmu_pay_setcaps;
92   gstbasertppayload_class->handle_buffer = gst_rtp_pcmu_pay_handle_buffer;
93 }
94
95 static void
96 gst_rtp_pcmu_pay_init (GstRtpPcmuPay * rtppcmupay, GstRtpPcmuPayClass * klass)
97 {
98   rtppcmupay->adapter = gst_adapter_new ();
99   GST_BASE_RTP_PAYLOAD (rtppcmupay)->clock_rate = 8000;
100 }
101
102 static void
103 gst_rtp_pcmu_pay_finalize (GObject * object)
104 {
105   GstRtpPcmuPay *rtppcmupay;
106
107   rtppcmupay = GST_RTP_PCMU_PAY (object);
108
109   g_object_unref (rtppcmupay->adapter);
110   rtppcmupay->adapter = NULL;
111
112   G_OBJECT_CLASS (parent_class)->finalize (object);
113 }
114
115 static gboolean
116 gst_rtp_pcmu_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
117 {
118   payload->pt = GST_RTP_PAYLOAD_PCMU;
119   gst_basertppayload_set_options (payload, "audio", FALSE, "PCMU", 8000);
120
121   gst_basertppayload_set_outcaps (payload, NULL);
122
123   return TRUE;
124 }
125
126 static GstFlowReturn
127 gst_rtp_pcmu_pay_flush (GstRtpPcmuPay * rtppcmupay)
128 {
129   guint avail;
130   GstBuffer *outbuf;
131   GstFlowReturn ret;
132   guint maxptime_octets = G_MAXUINT;
133   guint minptime_octets = GST_RTP_PCMU_MIN_PTIME_OCTETS;
134
135   if (GST_BASE_RTP_PAYLOAD (rtppcmupay)->max_ptime > 0) {
136     /* calculate octet count with:
137        maxptime-nsec * samples-per-sec / nsecs-per-sec * octets-per-sample */
138     maxptime_octets =
139         GST_BASE_RTP_PAYLOAD (rtppcmupay)->max_ptime *
140         GST_BASE_RTP_PAYLOAD (rtppcmupay)->clock_rate / GST_SECOND;
141   }
142
143   /* the data available in the adapter is either smaller
144    * than the MTU or bigger. In the case it is smaller, the complete
145    * adapter contents can be put in one packet.  */
146   avail = gst_adapter_available (rtppcmupay->adapter);
147
148   ret = GST_FLOW_OK;
149
150   while (avail >= minptime_octets) {
151     guint8 *payload;
152     guint8 *data;
153     guint payload_len;
154     guint packet_len;
155
156     /* fill one MTU or all available bytes */
157     payload_len =
158         MIN (MIN (GST_BASE_RTP_PAYLOAD_MTU (rtppcmupay), maxptime_octets),
159         avail);
160
161     /* this will be the total lenght of the packet */
162     packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
163
164     /* create buffer to hold the payload */
165     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
166
167     /* copy payload */
168     gst_rtp_buffer_set_payload_type (outbuf,
169         GST_BASE_RTP_PAYLOAD_PT (rtppcmupay));
170     payload = gst_rtp_buffer_get_payload (outbuf);
171     data = (guint8 *) gst_adapter_peek (rtppcmupay->adapter, payload_len);
172     memcpy (payload, data, payload_len);
173     gst_adapter_flush (rtppcmupay->adapter, payload_len);
174
175     avail -= payload_len;
176
177     GST_BUFFER_TIMESTAMP (outbuf) = rtppcmupay->first_ts;
178     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtppcmupay), outbuf);
179   }
180
181   return ret;
182 }
183
184 static GstFlowReturn
185 gst_rtp_pcmu_pay_handle_buffer (GstBaseRTPPayload * basepayload,
186     GstBuffer * buffer)
187 {
188   GstRtpPcmuPay *rtppcmupay;
189   guint size, packet_len, avail;
190   GstFlowReturn ret;
191   GstClockTime duration;
192
193   rtppcmupay = GST_RTP_PCMU_PAY (basepayload);
194
195   size = GST_BUFFER_SIZE (buffer);
196   duration = GST_BUFFER_TIMESTAMP (buffer);
197
198   avail = gst_adapter_available (rtppcmupay->adapter);
199   if (avail == 0) {
200     rtppcmupay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
201     rtppcmupay->duration = 0;
202   }
203
204   /* get packet length of data and see if we exceeded MTU. */
205   packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
206
207   /* if this buffer is going to overflow the packet, flush what we
208    * have. */
209   if (gst_basertppayload_is_filled (basepayload,
210           packet_len, rtppcmupay->duration + duration)) {
211     ret = gst_rtp_pcmu_pay_flush (rtppcmupay);
212     rtppcmupay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
213     rtppcmupay->duration = 0;
214   } else {
215     ret = GST_FLOW_OK;
216   }
217
218   gst_adapter_push (rtppcmupay->adapter, buffer);
219   rtppcmupay->duration += duration;
220
221   return ret;
222 }
223
224 gboolean
225 gst_rtp_pcmu_pay_plugin_init (GstPlugin * plugin)
226 {
227   return gst_element_register (plugin, "rtppcmupay",
228       GST_RANK_NONE, GST_TYPE_RTP_PCMU_PAY);
229 }