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>
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.
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
23 #include <gst/rtp/gstrtpbuffer.h>
25 #include "gstrtppcmupay.h"
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>");
34 static GstStaticPadTemplate gst_rtp_pcmu_pay_sink_template =
35 GST_STATIC_PAD_TEMPLATE ("sink",
38 GST_STATIC_CAPS ("audio/x-mulaw, channels=(int)1, rate=(int)8000")
41 static GstStaticPadTemplate gst_rtp_pcmu_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
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\"")
51 static gboolean gst_rtp_pcmu_pay_setcaps (GstBaseRTPPayload * payload,
53 static GstFlowReturn gst_rtp_pcmu_pay_handle_buffer (GstBaseRTPPayload *
54 payload, GstBuffer * buffer);
55 static void gst_rtp_pcmu_pay_finalize (GObject * object);
57 GST_BOILERPLATE (GstRtpPcmuPay, gst_rtp_pcmu_pay, GstBaseRTPPayload,
58 GST_TYPE_BASE_RTP_PAYLOAD);
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
66 gst_rtp_pcmu_pay_base_init (gpointer klass)
68 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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);
78 gst_rtp_pcmu_pay_class_init (GstRtpPcmuPayClass * klass)
80 GObjectClass *gobject_class;
81 GstElementClass *gstelement_class;
82 GstBaseRTPPayloadClass *gstbasertppayload_class;
84 gobject_class = (GObjectClass *) klass;
85 gstelement_class = (GstElementClass *) klass;
86 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
88 parent_class = g_type_class_peek_parent (klass);
89 gobject_class->finalize = gst_rtp_pcmu_pay_finalize;
91 gstbasertppayload_class->set_caps = gst_rtp_pcmu_pay_setcaps;
92 gstbasertppayload_class->handle_buffer = gst_rtp_pcmu_pay_handle_buffer;
96 gst_rtp_pcmu_pay_init (GstRtpPcmuPay * rtppcmupay, GstRtpPcmuPayClass * klass)
98 rtppcmupay->adapter = gst_adapter_new ();
99 GST_BASE_RTP_PAYLOAD (rtppcmupay)->clock_rate = 8000;
103 gst_rtp_pcmu_pay_finalize (GObject * object)
105 GstRtpPcmuPay *rtppcmupay;
107 rtppcmupay = GST_RTP_PCMU_PAY (object);
109 g_object_unref (rtppcmupay->adapter);
110 rtppcmupay->adapter = NULL;
112 G_OBJECT_CLASS (parent_class)->finalize (object);
116 gst_rtp_pcmu_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
118 payload->pt = GST_RTP_PAYLOAD_PCMU;
119 gst_basertppayload_set_options (payload, "audio", FALSE, "PCMU", 8000);
121 gst_basertppayload_set_outcaps (payload, NULL);
127 gst_rtp_pcmu_pay_flush (GstRtpPcmuPay * rtppcmupay)
132 guint maxptime_octets = G_MAXUINT;
133 guint minptime_octets = GST_RTP_PCMU_MIN_PTIME_OCTETS;
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 */
139 GST_BASE_RTP_PAYLOAD (rtppcmupay)->max_ptime *
140 GST_BASE_RTP_PAYLOAD (rtppcmupay)->clock_rate / GST_SECOND;
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);
150 while (avail >= minptime_octets) {
156 /* fill one MTU or all available bytes */
158 MIN (MIN (GST_BASE_RTP_PAYLOAD_MTU (rtppcmupay), maxptime_octets),
161 /* this will be the total lenght of the packet */
162 packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
164 /* create buffer to hold the payload */
165 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
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);
175 avail -= payload_len;
177 GST_BUFFER_TIMESTAMP (outbuf) = rtppcmupay->first_ts;
178 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtppcmupay), outbuf);
185 gst_rtp_pcmu_pay_handle_buffer (GstBaseRTPPayload * basepayload,
188 GstRtpPcmuPay *rtppcmupay;
189 guint size, packet_len, avail;
191 GstClockTime duration;
193 rtppcmupay = GST_RTP_PCMU_PAY (basepayload);
195 size = GST_BUFFER_SIZE (buffer);
196 duration = GST_BUFFER_TIMESTAMP (buffer);
198 avail = gst_adapter_available (rtppcmupay->adapter);
200 rtppcmupay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
201 rtppcmupay->duration = 0;
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);
207 /* if this buffer is going to overflow the packet, flush what we
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;
218 gst_adapter_push (rtppcmupay->adapter, buffer);
219 rtppcmupay->duration += duration;
225 gst_rtp_pcmu_pay_plugin_init (GstPlugin * plugin)
227 return gst_element_register (plugin, "rtppcmupay",
228 GST_RANK_NONE, GST_TYPE_RTP_PCMU_PAY);