RTMP protocol support (as a client)
[libav.git] / libavformat / rtmppkt.c
CommitLineData
9fd6b843
KS
1/*
2 * RTMP input format
3 * Copyright (c) 2009 Kostya Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavcodec/bytestream.h"
23#include "libavutil/avstring.h"
24#include "avformat.h"
25
26#include "rtmppkt.h"
27#include "flv.h"
28
29void ff_amf_write_bool(uint8_t **dst, int val)
30{
31 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
32 bytestream_put_byte(dst, val);
33}
34
35void ff_amf_write_number(uint8_t **dst, double val)
36{
37 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
38 bytestream_put_be64(dst, av_dbl2int(val));
39}
40
41void ff_amf_write_string(uint8_t **dst, const char *str)
42{
43 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
44 bytestream_put_be16(dst, strlen(str));
45 bytestream_put_buffer(dst, str, strlen(str));
46}
47
48void ff_amf_write_null(uint8_t **dst)
49{
50 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
51}
52
53void ff_amf_write_object_start(uint8_t **dst)
54{
55 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
56}
57
58void ff_amf_write_field_name(uint8_t **dst, const char *str)
59{
60 bytestream_put_be16(dst, strlen(str));
61 bytestream_put_buffer(dst, str, strlen(str));
62}
63
64void ff_amf_write_object_end(uint8_t **dst)
65{
66 /* first two bytes are field name length = 0,
67 * AMF object should end with it and end marker
68 */
69 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
70}
71
72int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
73 int chunk_size, RTMPPacket *prev_pkt)
74{
75 uint8_t hdr, t, buf[16];
76 int channel_id, timestamp, data_size, offset = 0;
77 uint32_t extra = 0;
78 uint8_t type;
79
80 if (url_read(h, &hdr, 1) != 1)
81 return AVERROR(EIO);
82 channel_id = hdr & 0x3F;
83
84 data_size = prev_pkt[channel_id].data_size;
85 type = prev_pkt[channel_id].type;
86 extra = prev_pkt[channel_id].extra;
87
88 hdr >>= 6;
89 if (hdr == RTMP_PS_ONEBYTE) {
90 //todo
91 return -1;
92 } else {
93 if (url_read_complete(h, buf, 3) != 3)
94 return AVERROR(EIO);
95 timestamp = AV_RB24(buf);
96 if (hdr != RTMP_PS_FOURBYTES) {
97 if (url_read_complete(h, buf, 3) != 3)
98 return AVERROR(EIO);
99 data_size = AV_RB24(buf);
100 if (url_read_complete(h, &type, 1) != 1)
101 return AVERROR(EIO);
102 if (hdr == RTMP_PS_TWELVEBYTES) {
103 if (url_read_complete(h, buf, 4) != 4)
104 return AVERROR(EIO);
105 extra = AV_RL32(buf);
106 }
107 }
108 }
109 if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
110 return -1;
111 p->extra = extra;
112 // save history
113 prev_pkt[channel_id].channel_id = channel_id;
114 prev_pkt[channel_id].type = type;
115 prev_pkt[channel_id].data_size = data_size;
116 prev_pkt[channel_id].timestamp = timestamp;
117 prev_pkt[channel_id].extra = extra;
118 while (data_size > 0) {
119 int toread = FFMIN(data_size, chunk_size);
120 if (url_read_complete(h, p->data + offset, toread) != toread) {
121 ff_rtmp_packet_destroy(p);
122 return AVERROR(EIO);
123 }
124 data_size -= chunk_size;
125 offset += chunk_size;
126 if (data_size > 0) {
127 url_read_complete(h, &t, 1); //marker
128 if (t != (0xC0 + channel_id))
129 return -1;
130 }
131 }
132 return 0;
133}
134
135int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
136 int chunk_size, RTMPPacket *prev_pkt)
137{
138 uint8_t pkt_hdr[16], *p = pkt_hdr;
139 int mode = RTMP_PS_TWELVEBYTES;
140 int off = 0;
141
142 //TODO: header compression
143 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
144 if (mode != RTMP_PS_ONEBYTE) {
145 bytestream_put_be24(&p, pkt->timestamp);
146 if (mode != RTMP_PS_FOURBYTES) {
147 bytestream_put_be24(&p, pkt->data_size);
148 bytestream_put_byte(&p, pkt->type);
149 if (mode == RTMP_PS_TWELVEBYTES)
150 bytestream_put_le32(&p, pkt->extra);
151 }
152 }
153 url_write(h, pkt_hdr, p-pkt_hdr);
154 while (off < pkt->data_size) {
155 int towrite = FFMIN(chunk_size, pkt->data_size - off);
156 url_write(h, pkt->data + off, towrite);
157 off += towrite;
158 if (off < pkt->data_size) {
159 uint8_t marker = 0xC0 | pkt->channel_id;
160 url_write(h, &marker, 1);
161 }
162 }
163 return 0;
164}
165
166int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
167 int timestamp, int size)
168{
169 pkt->data = av_malloc(size);
170 if (!pkt->data)
171 return AVERROR(ENOMEM);
172 pkt->data_size = size;
173 pkt->channel_id = channel_id;
174 pkt->type = type;
175 pkt->timestamp = timestamp;
176 pkt->extra = 0;
177
178 return 0;
179}
180
181void ff_rtmp_packet_destroy(RTMPPacket *pkt)
182{
183 if (!pkt)
184 return;
185 av_freep(&pkt->data);
186 pkt->data_size = 0;
187}
188
189int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
190{
191 const uint8_t *base = data;
192
193 if (data >= data_end)
194 return -1;
195 switch (*data++) {
196 case AMF_DATA_TYPE_NUMBER: return 9;
197 case AMF_DATA_TYPE_BOOL: return 2;
198 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
199 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
200 case AMF_DATA_TYPE_NULL: return 1;
201 case AMF_DATA_TYPE_ARRAY:
202 data += 4;
203 case AMF_DATA_TYPE_OBJECT:
204 for (;;) {
205 int size = bytestream_get_be16(&data);
206 int t;
207 if (!size) {
208 data++;
209 break;
210 }
211 if (data + size >= data_end || data + size < data)
212 return -1;
213 data += size;
214 t = ff_amf_tag_size(data, data_end);
215 if (t < 0 || data + t >= data_end)
216 return -1;
217 data += t;
218 }
219 return data - base;
220 case AMF_DATA_TYPE_OBJECT_END: return 1;
221 default: return -1;
222 }
223}
224
225int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
226 const uint8_t *name, uint8_t *dst, int dst_size)
227{
228 int namelen = strlen(name);
229 int len;
230
231 if (data_end - data < 3)
232 return -1;
233 if (*data++ != AMF_DATA_TYPE_OBJECT)
234 return -1;
235 for (;;) {
236 int size = bytestream_get_be16(&data);
237 if (!size)
238 break;
239 if (data + size >= data_end || data + size < data)
240 return -1;
241 data += size;
242 if (size == namelen && !memcmp(data-size, name, namelen)) {
243 switch (*data++) {
244 case AMF_DATA_TYPE_NUMBER:
245 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
246 break;
247 case AMF_DATA_TYPE_BOOL:
248 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
249 break;
250 case AMF_DATA_TYPE_STRING:
251 len = bytestream_get_be16(&data);
252 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
253 break;
254 default:
255 return -1;
256 }
257 return 0;
258 }
259 len = ff_amf_tag_size(data, data_end);
260 if (len < 0 || data + len >= data_end || data + len < data)
261 return -1;
262 data += len;
263 }
264 return -1;
265}