1/*
2 * copyright (c) 2007 Luca Abeni
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg 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 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "avformat.h"
22#include "rtpenc.h"
23
24#define MAX_FRAMES_PER_PACKET (s->max_frames_per_packet ? s->max_frames_per_packet : 5)
25#define MAX_AU_HEADERS_SIZE (2 + 2 * MAX_FRAMES_PER_PACKET)
26
27void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
28{
29    RTPMuxContext *s = s1->priv_data;
30    int len, max_packet_size;
31    uint8_t *p;
32
33    /* skip ADTS header, if present */
34    if ((s1->streams[0]->codec->extradata_size) == 0) {
35        size -= 7;
36        buff += 7;
37    }
38    max_packet_size = s->max_payload_size - MAX_AU_HEADERS_SIZE;
39
40    /* test if the packet must be sent */
41    len = (s->buf_ptr - s->buf);
42    if ((s->num_frames == MAX_FRAMES_PER_PACKET) || (len && (len + size) > max_packet_size)) {
43        int au_size = s->num_frames * 2;
44
45        p = s->buf + MAX_AU_HEADERS_SIZE - au_size - 2;
46        if (p != s->buf) {
47            memmove(p + 2, s->buf + 2, au_size);
48        }
49        /* Write the AU header size */
50        p[0] = ((au_size * 8) & 0xFF) >> 8;
51        p[1] = (au_size * 8) & 0xFF;
52
53        ff_rtp_send_data(s1, p, s->buf_ptr - p, 1);
54
55        s->num_frames = 0;
56    }
57    if (s->num_frames == 0) {
58        s->buf_ptr = s->buf + MAX_AU_HEADERS_SIZE;
59        s->timestamp = s->cur_timestamp;
60    }
61
62    if (size < max_packet_size) {
63        p = s->buf + s->num_frames++ * 2 + 2;
64        *p++ = size >> 5;
65        *p = (size & 0x1F) << 3;
66        memcpy(s->buf_ptr, buff, size);
67        s->buf_ptr += size;
68    } else {
69        if (s->buf_ptr != s->buf + MAX_AU_HEADERS_SIZE) {
70            av_log(s1, AV_LOG_ERROR, "Strange...\n");
71            av_abort();
72        }
73        max_packet_size = s->max_payload_size - 4;
74        p = s->buf;
75        p[0] = 0;
76        p[1] = 16;
77        while (size > 0) {
78            len = FFMIN(size, max_packet_size);
79            p[2] = len >> 5;
80            p[3] = (size & 0x1F) << 3;
81            memcpy(p + 4, buff, len);
82            ff_rtp_send_data(s1, p, len + 4, len == size);
83            size -= len;
84            buff += len;
85        }
86    }
87}
88