gstbasertpaudiopayload.c 12.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* GStreamer
 * Copyright (C) <2006> Philippe Khalaf <burger@speedy.org> 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <gst/rtp/gstrtpbuffer.h>

#include "gstbasertpaudiopayload.h"

30
GST_DEBUG_CATEGORY_STATIC (basertpaudiopayload_debug);
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define GST_CAT_DEFAULT (basertpaudiopayload_debug)

static void gst_basertpaudiopayload_finalize (GObject * object);

static GstFlowReturn
gst_basertpaudiopayload_push (GstBaseRTPPayload * basepayload, guint8 * data,
    guint payload_len, GstClockTime timestamp);

static GstFlowReturn gst_basertpaudiopayload_handle_buffer (GstBaseRTPPayload *
    payload, GstBuffer * buffer);

static GstFlowReturn
gst_basertpaudiopayload_handle_frame_based_buffer (GstBaseRTPPayload *
    basepayload, GstBuffer * buffer);

static GstFlowReturn
gst_basertpaudiopayload_handle_sample_based_buffer (GstBaseRTPPayload *
    basepayload, GstBuffer * buffer);

GST_BOILERPLATE (GstBaseRTPAudioPayload, gst_basertpaudiopayload,
    GstBaseRTPPayload, GST_TYPE_BASE_RTP_PAYLOAD);

static void
gst_basertpaudiopayload_base_init (gpointer klass)
{
}

static void
gst_basertpaudiopayload_class_init (GstBaseRTPAudioPayloadClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
  GstBaseRTPPayloadClass *gstbasertppayload_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
  gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
68
69
  gobject_class->finalize =
      GST_DEBUG_FUNCPTR (gst_basertpaudiopayload_finalize);
70
71
72
73

  parent_class = g_type_class_ref (GST_TYPE_BASE_RTP_PAYLOAD);

  gstbasertppayload_class->handle_buffer =
74
      GST_DEBUG_FUNCPTR (gst_basertpaudiopayload_handle_buffer);
75
76
77
78
79
80
81
82
83

  GST_DEBUG_CATEGORY_INIT (basertpaudiopayload_debug, "basertpaudiopayload", 0,
      "base audio RTP payloader");
}

static void
gst_basertpaudiopayload_init (GstBaseRTPAudioPayload * basertpaudiopayload,
    GstBaseRTPAudioPayloadClass * klass)
{
84
  basertpaudiopayload->base_ts = 0;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

  basertpaudiopayload->type = AUDIO_CODEC_TYPE_NONE;

  /* these need to be set by child object if frame based */
  basertpaudiopayload->frame_size = 0;
  basertpaudiopayload->frame_duration = 0;

  /* these need to be set by child object if sample based */
  basertpaudiopayload->sample_size = 0;
}

static void
gst_basertpaudiopayload_finalize (GObject * object)
{
  GstBaseRTPAudioPayload *basertpaudiopayload;

  basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (object);

  GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}

106
107
108
109
110
111
112
113
/**
 * gst_basertpaudiopayload_set_frame_based:
 * @basertpaudiopayload: a pointer to the element.
 *
 * Tells #GstBaseRTPAudioPayload that the child element is for a frame based
 * audio codec
 *
 */
114
115
116
117
118
119
void
gst_basertpaudiopayload_set_frame_based (GstBaseRTPAudioPayload *
    basertpaudiopayload)
{
  g_return_if_fail (basertpaudiopayload != NULL);

120
121
  g_return_if_fail (basertpaudiopayload->type == AUDIO_CODEC_TYPE_NONE);

122
123
124
  basertpaudiopayload->type = AUDIO_CODEC_TYPE_FRAME_BASED;
}

125
126
127
128
129
130
131
132
/**
 * gst_basertpaudiopayload_set_sample_based:
 * @basertpaudiopayload: a pointer to the element.
 *
 * Tells #GstBaseRTPAudioPayload that the child element is for a sample based
 * audio codec
 *
 */
133
134
135
136
137
138
void
gst_basertpaudiopayload_set_sample_based (GstBaseRTPAudioPayload *
    basertpaudiopayload)
{
  g_return_if_fail (basertpaudiopayload != NULL);

139
140
  g_return_if_fail (basertpaudiopayload->type == AUDIO_CODEC_TYPE_NONE);

141
142
143
  basertpaudiopayload->type = AUDIO_CODEC_TYPE_SAMPLE_BASED;
}

144
145
146
147
148
149
150
151
152
/**
 * gst_basertpaudiopayload_set_frame_options:
 * @basertpaudiopayload: a pointer to the element.
 * @frame_duration: The duraction of an audio frame in milliseconds. 
 * @frame_size: The size of an audio frame in bytes.
 *
 * Sets the options for frame based audio codecs.
 *
 */
153
154
155
156
157
158
159
160
161
162
void
gst_basertpaudiopayload_set_frame_options (GstBaseRTPAudioPayload
    * basertpaudiopayload, gint frame_duration, gint frame_size)
{
  g_return_if_fail (basertpaudiopayload != NULL);

  basertpaudiopayload->frame_size = frame_size;
  basertpaudiopayload->frame_duration = frame_duration;
}

163
164
165
166
167
168
169
170
/**
 * gst_basertpaudiopayload_set_sample_options:
 * @basertpaudiopayload: a pointer to the element.
 * @sample_size: Size per sample in bytes.
 *
 * Sets the options for sample based audio codecs.
 *
 */
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
void
gst_basertpaudiopayload_set_sample_options (GstBaseRTPAudioPayload
    * basertpaudiopayload, gint sample_size)
{
  g_return_if_fail (basertpaudiopayload != NULL);

  basertpaudiopayload->sample_size = sample_size;
}

static GstFlowReturn
gst_basertpaudiopayload_handle_buffer (GstBaseRTPPayload * basepayload,
    GstBuffer * buffer)
{
  GstFlowReturn ret;
  GstBaseRTPAudioPayload *basertpaudiopayload;

  basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basepayload);

  ret = GST_FLOW_ERROR;

  if (basertpaudiopayload->type == AUDIO_CODEC_TYPE_FRAME_BASED) {
    ret = gst_basertpaudiopayload_handle_frame_based_buffer (basepayload,
        buffer);
  } else if (basertpaudiopayload->type == AUDIO_CODEC_TYPE_SAMPLE_BASED) {
    ret = gst_basertpaudiopayload_handle_sample_based_buffer (basepayload,
        buffer);
  } else {
    GST_DEBUG_OBJECT (basertpaudiopayload, "Audio codec type not set");
  }

  return ret;
}

/* this assumes all frames have a constant duration and a constant size */
static GstFlowReturn
gst_basertpaudiopayload_handle_frame_based_buffer (GstBaseRTPPayload *
    basepayload, GstBuffer * buffer)
{
  GstBaseRTPAudioPayload *basertpaudiopayload;
  guint payload_len;
  guint8 *data;
  GstFlowReturn ret;
  guint available;
  gint frame_size, frame_duration;

  guint maxptime_octets = G_MAXUINT;

  ret = GST_FLOW_ERROR;

  basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basepayload);

  if (basertpaudiopayload->frame_size == 0 ||
      basertpaudiopayload->frame_duration == 0) {
    GST_DEBUG_OBJECT (basertpaudiopayload, "Required options not set");
    gst_buffer_unref (buffer);
    return GST_FLOW_ERROR;
  }
  frame_size = basertpaudiopayload->frame_size;
  frame_duration = basertpaudiopayload->frame_duration;

231
232
  /* If buffer fits on an RTP packet, let's just push it through */
  /* this will check against max_ptime and max_mtu */
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  if (!gst_basertppayload_is_filled (basepayload,
          gst_rtp_buffer_calc_packet_len (GST_BUFFER_SIZE (buffer), 0, 0),
          GST_BUFFER_DURATION (buffer))) {
    ret = gst_basertpaudiopayload_push (basepayload, GST_BUFFER_DATA (buffer),
        GST_BUFFER_SIZE (buffer), GST_BUFFER_TIMESTAMP (buffer));
    gst_buffer_unref (buffer);

    return ret;
  }

  /* max number of bytes based on given ptime, has to be multiple of
   * frame_duration */
  if (basepayload->max_ptime != -1) {
    guint ptime_ms = basepayload->max_ptime / 1000000;

    maxptime_octets = frame_size * (int) (ptime_ms / frame_duration);
    if (maxptime_octets == 0) {
250
251
      GST_WARNING_OBJECT (basertpaudiopayload, "Given ptime %d is smaller than"
          " minimum %d ms, overwriting to minimum", ptime_ms, frame_duration);
252
253
254
255
      maxptime_octets = frame_size;
    }
  }

256
257
  /* let's set the base timestamp */
  basertpaudiopayload->base_ts = GST_BUFFER_TIMESTAMP (buffer);
258

259
260
  available = GST_BUFFER_SIZE (buffer);
  data = (guint8 *) GST_BUFFER_DATA (buffer);
261
262

  /* as long as we have full frames */
263
  /* this loop will push all available buffers till the last frame */
264
265
  while (available >= frame_size) {
    /* we need to see how many frames we can get based on maximum MTU, maximum
266
     * ptime and the number of bytes available */
267
268
269
270
271
272
273
    payload_len = MIN (MIN (
            /* MTU max */
            (int) (gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU
                    (basertpaudiopayload), 0, 0) / frame_size) * frame_size,
            /* ptime max */
            maxptime_octets),
        /* currently available */
274
        (available / frame_size) * frame_size);
275

276
277
    ret = gst_basertpaudiopayload_push (basepayload, data, payload_len,
        basertpaudiopayload->base_ts);
278
279
280
281

    gfloat ts_inc = (payload_len * frame_duration) / frame_size;

    ts_inc = ts_inc * GST_MSECOND;
282
    basertpaudiopayload->base_ts += ts_inc;
283

284
285
    available -= payload_len;
    data += payload_len;
286
287
  }

288
289
  gst_buffer_unref (buffer);

290
  /* none should be available by now */
291
  if (available != 0) {
292
293
    GST_ERROR_OBJECT (basertpaudiopayload, "The buffer size is not a multiple"
        " of the frame_size");
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    return GST_FLOW_ERROR;
  }

  return ret;
}

static GstFlowReturn
gst_basertpaudiopayload_handle_sample_based_buffer (GstBaseRTPPayload *
    basepayload, GstBuffer * buffer)
{
  GstBaseRTPAudioPayload *basertpaudiopayload;
  guint payload_len;
  guint8 *data;
  GstFlowReturn ret;
  guint available;

  guint maxptime_octets = G_MAXUINT;

  guint sample_size;

  ret = GST_FLOW_ERROR;

  basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basepayload);

  if (basertpaudiopayload->sample_size == 0) {
    GST_DEBUG_OBJECT (basertpaudiopayload, "Required options not set");
    gst_buffer_unref (buffer);
    return GST_FLOW_ERROR;
  }
  sample_size = basertpaudiopayload->sample_size;

325
326
  /* If buffer fits on an RTP packet, let's just push it through */
  /* this will check against max_ptime and max_mtu */
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  if (!gst_basertppayload_is_filled (basepayload,
          gst_rtp_buffer_calc_packet_len (GST_BUFFER_SIZE (buffer), 0, 0),
          GST_BUFFER_DURATION (buffer))) {
    ret = gst_basertpaudiopayload_push (basepayload, GST_BUFFER_DATA (buffer),
        GST_BUFFER_SIZE (buffer), GST_BUFFER_TIMESTAMP (buffer));
    gst_buffer_unref (buffer);

    return ret;
  }

  /* max number of bytes based on given ptime */
  if (basepayload->max_ptime != -1) {
    maxptime_octets = basepayload->max_ptime * basepayload->clock_rate /
        (sample_size * GST_SECOND);
341
342
    GST_DEBUG_OBJECT (basertpaudiopayload, "Calculated max_octects %u",
        maxptime_octets);
343
344
  }

345
346
347
348
  /* let's set the base timestamp */
  basertpaudiopayload->base_ts = GST_BUFFER_TIMESTAMP (buffer);
  GST_DEBUG_OBJECT (basertpaudiopayload, "Setting to %" GST_TIME_FORMAT,
      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
349

350
351
  available = GST_BUFFER_SIZE (buffer);
  data = (guint8 *) GST_BUFFER_DATA (buffer);
352
353

  /* as long as we have full frames */
354
355
  /* this loop will use all available data until the last byte */
  while (available) {
356
    /* we need to see how many frames we can get based on maximum MTU, maximum
357
     * ptime and the number of bytes available */
358
359
360
361
362
363
364
365
366
    payload_len = MIN (MIN (
            /* MTU max */
            gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU
                (basertpaudiopayload), 0, 0),
            /* ptime max */
            maxptime_octets),
        /* currently available */
        available);

367
368
    ret = gst_basertpaudiopayload_push (basepayload, data, payload_len,
        basertpaudiopayload->base_ts);
369
370
371
372

    gfloat num = payload_len;
    gfloat datarate = (sample_size * basepayload->clock_rate);

373
    basertpaudiopayload->base_ts +=
374
375
376
        /* payload_len (bytes) * nsecs/sec / datarate (bytes*sec) */
        num / datarate * GST_SECOND;
    GST_DEBUG_OBJECT (basertpaudiopayload, "New ts is %" GST_TIME_FORMAT,
377
        GST_TIME_ARGS (basertpaudiopayload->base_ts));
378

379
380
    available -= payload_len;
    data += payload_len;
381
382
  }

383
384
  gst_buffer_unref (buffer);

385
386
387
388
389
390
391
392
393
394
395
  return ret;
}

static GstFlowReturn
gst_basertpaudiopayload_push (GstBaseRTPPayload * basepayload, guint8 * data,
    guint payload_len, GstClockTime timestamp)
{
  GstBuffer *outbuf;
  guint8 *payload;
  GstFlowReturn ret;

396
397
398
  GST_DEBUG_OBJECT (basepayload, "Pushing %d bytes ts %" GST_TIME_FORMAT,
      payload_len, GST_TIME_ARGS (timestamp));

399
400
401
402
403
404
405
406
407
408
409
410
411
  /* create buffer to hold the payload */
  outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);

  /* copy payload */
  gst_rtp_buffer_set_payload_type (outbuf, basepayload->pt);
  payload = gst_rtp_buffer_get_payload (outbuf);
  memcpy (payload, data, payload_len);

  GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
  ret = gst_basertppayload_push (basepayload, outbuf);

  return ret;
}