Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtmpproto.c @ 333e8943

History | View | Annotate | Download (33.8 KB)

1 9fd6b843 Kostya Shishkov
/*
2
 * RTMP network protocol
3
 * Copyright (c) 2009 Kostya Shishkov
4
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 9fd6b843 Kostya Shishkov
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 9fd6b843 Kostya Shishkov
 * 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 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 9fd6b843 Kostya Shishkov
 * 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 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 9fd6b843 Kostya Shishkov
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23 ba87f080 Diego Biurrun
 * @file
24 9fd6b843 Kostya Shishkov
 * RTMP protocol
25
 */
26
27
#include "libavcodec/bytestream.h"
28
#include "libavutil/avstring.h"
29
#include "libavutil/lfg.h"
30
#include "libavutil/sha.h"
31
#include "avformat.h"
32 e4a9e3cc Aurelien Jacobs
#include "internal.h"
33 9fd6b843 Kostya Shishkov
34
#include "network.h"
35
36
#include "flv.h"
37
#include "rtmp.h"
38
#include "rtmppkt.h"
39
40
/* we can't use av_log() with URLContext yet... */
41 ade800c5 Aurelien Jacobs
#if FF_API_URL_CLASS
42 9fd6b843 Kostya Shishkov
#define LOG_CONTEXT s
43 ade800c5 Aurelien Jacobs
#else
44
#define LOG_CONTEXT NULL
45 9fd6b843 Kostya Shishkov
#endif
46
47 cfac91fe Kostya Shishkov
//#define DEBUG
48
49 9fd6b843 Kostya Shishkov
/** RTMP protocol handler state */
50
typedef enum {
51
    STATE_START,      ///< client has not done anything yet
52
    STATE_HANDSHAKED, ///< client has performed handshake
53 6bf22e18 Sergiy
    STATE_RELEASING,  ///< client releasing stream before publish it (for output)
54
    STATE_FCPUBLISH,  ///< client FCPublishing stream (for output)
55 9fd6b843 Kostya Shishkov
    STATE_CONNECTING, ///< client connected to server successfully
56
    STATE_READY,      ///< client has sent all needed commands and waits for server reply
57
    STATE_PLAYING,    ///< client has started receiving multimedia data from server
58 6bf22e18 Sergiy
    STATE_PUBLISHING, ///< client has started sending multimedia data to server (for output)
59 72b870b9 Martin Storsjö
    STATE_STOPPED,    ///< the broadcast has been stopped
60 9fd6b843 Kostya Shishkov
} ClientState;
61
62
/** protocol handler context */
63
typedef struct RTMPContext {
64
    URLContext*   stream;                     ///< TCP stream used in interactions with RTMP server
65
    RTMPPacket    prev_pkt[2][RTMP_CHANNELS]; ///< packet history used when reading and sending packets
66
    int           chunk_size;                 ///< size of the chunks RTMP packets are divided into
67 b316991b Sergiy
    int           is_input;                   ///< input/output flag
68 9fd6b843 Kostya Shishkov
    char          playpath[256];              ///< path to filename to play (with possible "mp4:" prefix)
69 5e9ad759 Sergiy
    char          app[128];                   ///< application
70 9fd6b843 Kostya Shishkov
    ClientState   state;                      ///< current state
71
    int           main_channel_id;            ///< an additional channel ID which is used for some invocations
72
    uint8_t*      flv_data;                   ///< buffer with data for demuxer
73
    int           flv_size;                   ///< current buffer size
74
    int           flv_off;                    ///< number of bytes read from current buffer
75 6bf22e18 Sergiy
    RTMPPacket    out_pkt;                    ///< rtmp packet, created from flv a/v or metadata (for output)
76 bf7c1719 Kostya Shishkov
    uint32_t      client_report_size;         ///< number of bytes after which client should report to server
77
    uint32_t      bytes_read;                 ///< number of bytes read from server
78
    uint32_t      last_bytes_read;            ///< number of bytes read last reported to server
79 9fd6b843 Kostya Shishkov
} RTMPContext;
80
81
#define PLAYER_KEY_OPEN_PART_LEN 30   ///< length of partial key used for first client digest signing
82
/** Client key used for digest signing */
83
static const uint8_t rtmp_player_key[] = {
84
    'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
85
    'F', 'l', 'a', 's', 'h', ' ', 'P', 'l', 'a', 'y', 'e', 'r', ' ', '0', '0', '1',
86
87
    0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 0x02,
88
    0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 0x93, 0xB8,
89
    0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
90
};
91
92
#define SERVER_KEY_OPEN_PART_LEN 36   ///< length of partial key used for first server digest signing
93
/** Key used for RTMP server digest signing */
94
static const uint8_t rtmp_server_key[] = {
95
    'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
96
    'F', 'l', 'a', 's', 'h', ' ', 'M', 'e', 'd', 'i', 'a', ' ',
97
    'S', 'e', 'r', 'v', 'e', 'r', ' ', '0', '0', '1',
98
99
    0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 0x02,
100
    0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 0x93, 0xB8,
101
    0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
102
};
103
104
/**
105 49bd8e4b Måns Rullgård
 * Generate 'connect' call and send it to the server.
106 9fd6b843 Kostya Shishkov
 */
107
static void gen_connect(URLContext *s, RTMPContext *rt, const char *proto,
108 5e9ad759 Sergiy
                        const char *host, int port)
109 9fd6b843 Kostya Shishkov
{
110
    RTMPPacket pkt;
111 6bf22e18 Sergiy
    uint8_t ver[64], *p;
112 9fd6b843 Kostya Shishkov
    char tcurl[512];
113
114 31da5966 Sergiy
    ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0, 4096);
115 9fd6b843 Kostya Shishkov
    p = pkt.data;
116
117 57b5555c Martin Storsjö
    ff_url_join(tcurl, sizeof(tcurl), proto, NULL, host, port, "/%s", rt->app);
118 9fd6b843 Kostya Shishkov
    ff_amf_write_string(&p, "connect");
119
    ff_amf_write_number(&p, 1.0);
120
    ff_amf_write_object_start(&p);
121
    ff_amf_write_field_name(&p, "app");
122 5e9ad759 Sergiy
    ff_amf_write_string(&p, rt->app);
123 9fd6b843 Kostya Shishkov
124 6bf22e18 Sergiy
    if (rt->is_input) {
125 c7240611 Kostya Shishkov
        snprintf(ver, sizeof(ver), "%s %d,%d,%d,%d", RTMP_CLIENT_PLATFORM, RTMP_CLIENT_VER1,
126
                 RTMP_CLIENT_VER2, RTMP_CLIENT_VER3, RTMP_CLIENT_VER4);
127 6bf22e18 Sergiy
    } else {
128
        snprintf(ver, sizeof(ver), "FMLE/3.0 (compatible; %s)", LIBAVFORMAT_IDENT);
129
        ff_amf_write_field_name(&p, "type");
130
        ff_amf_write_string(&p, "nonprivate");
131
    }
132 9fd6b843 Kostya Shishkov
    ff_amf_write_field_name(&p, "flashVer");
133
    ff_amf_write_string(&p, ver);
134
    ff_amf_write_field_name(&p, "tcUrl");
135
    ff_amf_write_string(&p, tcurl);
136 6bf22e18 Sergiy
    if (rt->is_input) {
137 c7240611 Kostya Shishkov
        ff_amf_write_field_name(&p, "fpad");
138
        ff_amf_write_bool(&p, 0);
139
        ff_amf_write_field_name(&p, "capabilities");
140
        ff_amf_write_number(&p, 15.0);
141
        ff_amf_write_field_name(&p, "audioCodecs");
142
        ff_amf_write_number(&p, 1639.0);
143
        ff_amf_write_field_name(&p, "videoCodecs");
144
        ff_amf_write_number(&p, 252.0);
145
        ff_amf_write_field_name(&p, "videoFunction");
146
        ff_amf_write_number(&p, 1.0);
147 6bf22e18 Sergiy
    }
148 9fd6b843 Kostya Shishkov
    ff_amf_write_object_end(&p);
149
150
    pkt.data_size = p - pkt.data;
151
152
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
153 f8caa544 Martin Storsjö
    ff_rtmp_packet_destroy(&pkt);
154 9fd6b843 Kostya Shishkov
}
155
156
/**
157 49bd8e4b Måns Rullgård
 * Generate 'releaseStream' call and send it to the server. It should make
158 6bf22e18 Sergiy
 * the server release some channel for media streams.
159
 */
160
static void gen_release_stream(URLContext *s, RTMPContext *rt)
161
{
162
    RTMPPacket pkt;
163
    uint8_t *p;
164
165
    ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0,
166
                          29 + strlen(rt->playpath));
167
168
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Releasing stream...\n");
169
    p = pkt.data;
170
    ff_amf_write_string(&p, "releaseStream");
171
    ff_amf_write_number(&p, 2.0);
172
    ff_amf_write_null(&p);
173
    ff_amf_write_string(&p, rt->playpath);
174
175
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
176
    ff_rtmp_packet_destroy(&pkt);
177
}
178
179
/**
180 49bd8e4b Måns Rullgård
 * Generate 'FCPublish' call and send it to the server. It should make
181 6bf22e18 Sergiy
 * the server preapare for receiving media streams.
182
 */
183
static void gen_fcpublish_stream(URLContext *s, RTMPContext *rt)
184
{
185
    RTMPPacket pkt;
186
    uint8_t *p;
187
188
    ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0,
189
                          25 + strlen(rt->playpath));
190
191
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "FCPublish stream...\n");
192
    p = pkt.data;
193
    ff_amf_write_string(&p, "FCPublish");
194
    ff_amf_write_number(&p, 3.0);
195
    ff_amf_write_null(&p);
196
    ff_amf_write_string(&p, rt->playpath);
197
198
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
199
    ff_rtmp_packet_destroy(&pkt);
200
}
201
202
/**
203 49bd8e4b Måns Rullgård
 * Generate 'FCUnpublish' call and send it to the server. It should make
204 6bf22e18 Sergiy
 * the server destroy stream.
205
 */
206
static void gen_fcunpublish_stream(URLContext *s, RTMPContext *rt)
207
{
208
    RTMPPacket pkt;
209
    uint8_t *p;
210
211
    ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0,
212
                          27 + strlen(rt->playpath));
213
214
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "UnPublishing stream...\n");
215
    p = pkt.data;
216
    ff_amf_write_string(&p, "FCUnpublish");
217
    ff_amf_write_number(&p, 5.0);
218
    ff_amf_write_null(&p);
219
    ff_amf_write_string(&p, rt->playpath);
220
221
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
222
    ff_rtmp_packet_destroy(&pkt);
223
}
224
225
/**
226 49bd8e4b Måns Rullgård
 * Generate 'createStream' call and send it to the server. It should make
227 9fd6b843 Kostya Shishkov
 * the server allocate some channel for media streams.
228
 */
229
static void gen_create_stream(URLContext *s, RTMPContext *rt)
230
{
231
    RTMPPacket pkt;
232
    uint8_t *p;
233
234
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Creating stream...\n");
235 31da5966 Sergiy
    ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0, 25);
236 9fd6b843 Kostya Shishkov
237
    p = pkt.data;
238
    ff_amf_write_string(&p, "createStream");
239 6bf22e18 Sergiy
    ff_amf_write_number(&p, rt->is_input ? 3.0 : 4.0);
240
    ff_amf_write_null(&p);
241
242
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
243
    ff_rtmp_packet_destroy(&pkt);
244
}
245
246
247
/**
248 49bd8e4b Måns Rullgård
 * Generate 'deleteStream' call and send it to the server. It should make
249 6bf22e18 Sergiy
 * the server remove some channel for media streams.
250
 */
251
static void gen_delete_stream(URLContext *s, RTMPContext *rt)
252
{
253
    RTMPPacket pkt;
254
    uint8_t *p;
255
256
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Deleting stream...\n");
257
    ff_rtmp_packet_create(&pkt, RTMP_SYSTEM_CHANNEL, RTMP_PT_INVOKE, 0, 34);
258
259
    p = pkt.data;
260
    ff_amf_write_string(&p, "deleteStream");
261
    ff_amf_write_number(&p, 0.0);
262 9fd6b843 Kostya Shishkov
    ff_amf_write_null(&p);
263 6bf22e18 Sergiy
    ff_amf_write_number(&p, rt->main_channel_id);
264 9fd6b843 Kostya Shishkov
265
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
266
    ff_rtmp_packet_destroy(&pkt);
267
}
268
269
/**
270 49bd8e4b Måns Rullgård
 * Generate 'play' call and send it to the server, then ping the server
271 9fd6b843 Kostya Shishkov
 * to start actual playing.
272
 */
273
static void gen_play(URLContext *s, RTMPContext *rt)
274
{
275
    RTMPPacket pkt;
276
    uint8_t *p;
277
278
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Sending play command for '%s'\n", rt->playpath);
279
    ff_rtmp_packet_create(&pkt, RTMP_VIDEO_CHANNEL, RTMP_PT_INVOKE, 0,
280 f836fa17 Kostya Shishkov
                          20 + strlen(rt->playpath));
281 9fd6b843 Kostya Shishkov
    pkt.extra = rt->main_channel_id;
282
283
    p = pkt.data;
284
    ff_amf_write_string(&p, "play");
285
    ff_amf_write_number(&p, 0.0);
286
    ff_amf_write_null(&p);
287
    ff_amf_write_string(&p, rt->playpath);
288
289
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
290
    ff_rtmp_packet_destroy(&pkt);
291
292
    // set client buffer time disguised in ping packet
293
    ff_rtmp_packet_create(&pkt, RTMP_NETWORK_CHANNEL, RTMP_PT_PING, 1, 10);
294
295
    p = pkt.data;
296
    bytestream_put_be16(&p, 3);
297
    bytestream_put_be32(&p, 1);
298
    bytestream_put_be32(&p, 256); //TODO: what is a good value here?
299
300
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
301
    ff_rtmp_packet_destroy(&pkt);
302
}
303
304
/**
305 49bd8e4b Måns Rullgård
 * Generate 'publish' call and send it to the server.
306 6bf22e18 Sergiy
 */
307
static void gen_publish(URLContext *s, RTMPContext *rt)
308
{
309
    RTMPPacket pkt;
310
    uint8_t *p;
311
312
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Sending publish command for '%s'\n", rt->playpath);
313
    ff_rtmp_packet_create(&pkt, RTMP_SOURCE_CHANNEL, RTMP_PT_INVOKE, 0,
314
                          30 + strlen(rt->playpath));
315
    pkt.extra = rt->main_channel_id;
316
317
    p = pkt.data;
318
    ff_amf_write_string(&p, "publish");
319
    ff_amf_write_number(&p, 0.0);
320
    ff_amf_write_null(&p);
321
    ff_amf_write_string(&p, rt->playpath);
322
    ff_amf_write_string(&p, "live");
323
324
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
325
    ff_rtmp_packet_destroy(&pkt);
326
}
327
328
/**
329 49bd8e4b Måns Rullgård
 * Generate ping reply and send it to the server.
330 9fd6b843 Kostya Shishkov
 */
331
static void gen_pong(URLContext *s, RTMPContext *rt, RTMPPacket *ppkt)
332
{
333
    RTMPPacket pkt;
334
    uint8_t *p;
335
336
    ff_rtmp_packet_create(&pkt, RTMP_NETWORK_CHANNEL, RTMP_PT_PING, ppkt->timestamp + 1, 6);
337
    p = pkt.data;
338
    bytestream_put_be16(&p, 7);
339 4aaebf78 Martin Storsjö
    bytestream_put_be32(&p, AV_RB32(ppkt->data+2));
340 9fd6b843 Kostya Shishkov
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
341
    ff_rtmp_packet_destroy(&pkt);
342
}
343
344 bf7c1719 Kostya Shishkov
/**
345 49bd8e4b Måns Rullgård
 * Generate report on bytes read so far and send it to the server.
346 bf7c1719 Kostya Shishkov
 */
347
static void gen_bytes_read(URLContext *s, RTMPContext *rt, uint32_t ts)
348
{
349
    RTMPPacket pkt;
350
    uint8_t *p;
351
352
    ff_rtmp_packet_create(&pkt, RTMP_NETWORK_CHANNEL, RTMP_PT_BYTES_READ, ts, 4);
353
    p = pkt.data;
354
    bytestream_put_be32(&p, rt->bytes_read);
355
    ff_rtmp_packet_write(rt->stream, &pkt, rt->chunk_size, rt->prev_pkt[1]);
356
    ff_rtmp_packet_destroy(&pkt);
357
}
358
359 9fd6b843 Kostya Shishkov
//TODO: Move HMAC code somewhere. Eventually.
360
#define HMAC_IPAD_VAL 0x36
361
#define HMAC_OPAD_VAL 0x5C
362
363
/**
364 49bd8e4b Måns Rullgård
 * Calculate HMAC-SHA2 digest for RTMP handshake packets.
365 9fd6b843 Kostya Shishkov
 *
366
 * @param src    input buffer
367
 * @param len    input buffer length (should be 1536)
368
 * @param gap    offset in buffer where 32 bytes should not be taken into account
369
 *               when calculating digest (since it will be used to store that digest)
370
 * @param key    digest key
371
 * @param keylen digest key length
372
 * @param dst    buffer where calculated digest will be stored (32 bytes)
373
 */
374
static void rtmp_calc_digest(const uint8_t *src, int len, int gap,
375
                             const uint8_t *key, int keylen, uint8_t *dst)
376
{
377
    struct AVSHA *sha;
378
    uint8_t hmac_buf[64+32] = {0};
379
    int i;
380
381
    sha = av_mallocz(av_sha_size);
382
383
    if (keylen < 64) {
384
        memcpy(hmac_buf, key, keylen);
385
    } else {
386
        av_sha_init(sha, 256);
387
        av_sha_update(sha,key, keylen);
388
        av_sha_final(sha, hmac_buf);
389
    }
390
    for (i = 0; i < 64; i++)
391
        hmac_buf[i] ^= HMAC_IPAD_VAL;
392
393
    av_sha_init(sha, 256);
394
    av_sha_update(sha, hmac_buf, 64);
395
    if (gap <= 0) {
396
        av_sha_update(sha, src, len);
397
    } else { //skip 32 bytes used for storing digest
398
        av_sha_update(sha, src, gap);
399
        av_sha_update(sha, src + gap + 32, len - gap - 32);
400
    }
401
    av_sha_final(sha, hmac_buf + 64);
402
403
    for (i = 0; i < 64; i++)
404
        hmac_buf[i] ^= HMAC_IPAD_VAL ^ HMAC_OPAD_VAL; //reuse XORed key for opad
405
    av_sha_init(sha, 256);
406
    av_sha_update(sha, hmac_buf, 64+32);
407
    av_sha_final(sha, dst);
408
409
    av_free(sha);
410
}
411
412
/**
413 49bd8e4b Måns Rullgård
 * Put HMAC-SHA2 digest of packet data (except for the bytes where this digest
414 9fd6b843 Kostya Shishkov
 * will be stored) into that packet.
415
 *
416
 * @param buf handshake data (1536 bytes)
417
 * @return offset to the digest inside input data
418
 */
419
static int rtmp_handshake_imprint_with_digest(uint8_t *buf)
420
{
421
    int i, digest_pos = 0;
422
423
    for (i = 8; i < 12; i++)
424
        digest_pos += buf[i];
425
    digest_pos = (digest_pos % 728) + 12;
426
427
    rtmp_calc_digest(buf, RTMP_HANDSHAKE_PACKET_SIZE, digest_pos,
428
                     rtmp_player_key, PLAYER_KEY_OPEN_PART_LEN,
429
                     buf + digest_pos);
430
    return digest_pos;
431
}
432
433
/**
434 49bd8e4b Måns Rullgård
 * Verify that the received server response has the expected digest value.
435 9fd6b843 Kostya Shishkov
 *
436
 * @param buf handshake data received from the server (1536 bytes)
437
 * @param off position to search digest offset from
438
 * @return 0 if digest is valid, digest position otherwise
439
 */
440
static int rtmp_validate_digest(uint8_t *buf, int off)
441
{
442
    int i, digest_pos = 0;
443
    uint8_t digest[32];
444
445
    for (i = 0; i < 4; i++)
446
        digest_pos += buf[i + off];
447
    digest_pos = (digest_pos % 728) + off + 4;
448
449
    rtmp_calc_digest(buf, RTMP_HANDSHAKE_PACKET_SIZE, digest_pos,
450
                     rtmp_server_key, SERVER_KEY_OPEN_PART_LEN,
451
                     digest);
452
    if (!memcmp(digest, buf + digest_pos, 32))
453
        return digest_pos;
454
    return 0;
455
}
456
457
/**
458 49bd8e4b Måns Rullgård
 * Perform handshake with the server by means of exchanging pseudorandom data
459 9fd6b843 Kostya Shishkov
 * signed with HMAC-SHA2 digest.
460
 *
461
 * @return 0 if handshake succeeds, negative value otherwise
462
 */
463
static int rtmp_handshake(URLContext *s, RTMPContext *rt)
464
{
465
    AVLFG rnd;
466
    uint8_t tosend    [RTMP_HANDSHAKE_PACKET_SIZE+1] = {
467
        3,                // unencrypted data
468
        0, 0, 0, 0,       // client uptime
469
        RTMP_CLIENT_VER1,
470
        RTMP_CLIENT_VER2,
471
        RTMP_CLIENT_VER3,
472
        RTMP_CLIENT_VER4,
473
    };
474
    uint8_t clientdata[RTMP_HANDSHAKE_PACKET_SIZE];
475
    uint8_t serverdata[RTMP_HANDSHAKE_PACKET_SIZE+1];
476
    int i;
477
    int server_pos, client_pos;
478
    uint8_t digest[32];
479
480
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Handshaking...\n");
481
482
    av_lfg_init(&rnd, 0xDEADC0DE);
483
    // generate handshake packet - 1536 bytes of pseudorandom data
484
    for (i = 9; i <= RTMP_HANDSHAKE_PACKET_SIZE; i++)
485
        tosend[i] = av_lfg_get(&rnd) >> 24;
486
    client_pos = rtmp_handshake_imprint_with_digest(tosend + 1);
487
488
    url_write(rt->stream, tosend, RTMP_HANDSHAKE_PACKET_SIZE + 1);
489
    i = url_read_complete(rt->stream, serverdata, RTMP_HANDSHAKE_PACKET_SIZE + 1);
490
    if (i != RTMP_HANDSHAKE_PACKET_SIZE + 1) {
491
        av_log(LOG_CONTEXT, AV_LOG_ERROR, "Cannot read RTMP handshake response\n");
492
        return -1;
493
    }
494
    i = url_read_complete(rt->stream, clientdata, RTMP_HANDSHAKE_PACKET_SIZE);
495
    if (i != RTMP_HANDSHAKE_PACKET_SIZE) {
496
        av_log(LOG_CONTEXT, AV_LOG_ERROR, "Cannot read RTMP handshake response\n");
497
        return -1;
498
    }
499
500
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Server version %d.%d.%d.%d\n",
501
           serverdata[5], serverdata[6], serverdata[7], serverdata[8]);
502
503 e2ee11e8 Art Clarke
    if (rt->is_input && serverdata[5] >= 3) {
504 c7240611 Kostya Shishkov
        server_pos = rtmp_validate_digest(serverdata + 1, 772);
505 9fd6b843 Kostya Shishkov
        if (!server_pos) {
506 c7240611 Kostya Shishkov
            server_pos = rtmp_validate_digest(serverdata + 1, 8);
507
            if (!server_pos) {
508
                av_log(LOG_CONTEXT, AV_LOG_ERROR, "Server response validating failed\n");
509
                return -1;
510
            }
511 9fd6b843 Kostya Shishkov
        }
512
513 c7240611 Kostya Shishkov
        rtmp_calc_digest(tosend + 1 + client_pos, 32, 0,
514
                         rtmp_server_key, sizeof(rtmp_server_key),
515
                         digest);
516
        rtmp_calc_digest(clientdata, RTMP_HANDSHAKE_PACKET_SIZE-32, 0,
517
                         digest, 32,
518
                         digest);
519
        if (memcmp(digest, clientdata + RTMP_HANDSHAKE_PACKET_SIZE - 32, 32)) {
520
            av_log(LOG_CONTEXT, AV_LOG_ERROR, "Signature mismatch\n");
521
            return -1;
522
        }
523 9fd6b843 Kostya Shishkov
524 c7240611 Kostya Shishkov
        for (i = 0; i < RTMP_HANDSHAKE_PACKET_SIZE; i++)
525
            tosend[i] = av_lfg_get(&rnd) >> 24;
526
        rtmp_calc_digest(serverdata + 1 + server_pos, 32, 0,
527
                         rtmp_player_key, sizeof(rtmp_player_key),
528
                         digest);
529
        rtmp_calc_digest(tosend,  RTMP_HANDSHAKE_PACKET_SIZE - 32, 0,
530
                         digest, 32,
531
                         tosend + RTMP_HANDSHAKE_PACKET_SIZE - 32);
532
533
        // write reply back to the server
534
        url_write(rt->stream, tosend, RTMP_HANDSHAKE_PACKET_SIZE);
535 6bf22e18 Sergiy
    } else {
536
        url_write(rt->stream, serverdata+1, RTMP_HANDSHAKE_PACKET_SIZE);
537
    }
538
539 9fd6b843 Kostya Shishkov
    return 0;
540
}
541
542
/**
543 49bd8e4b Måns Rullgård
 * Parse received packet and possibly perform some action depending on
544 9fd6b843 Kostya Shishkov
 * the packet contents.
545
 * @return 0 for no errors, negative values for serious errors which prevent
546
 *         further communications, positive values for uncritical errors
547
 */
548
static int rtmp_parse_result(URLContext *s, RTMPContext *rt, RTMPPacket *pkt)
549
{
550
    int i, t;
551
    const uint8_t *data_end = pkt->data + pkt->data_size;
552
553 cfac91fe Kostya Shishkov
#ifdef DEBUG
554
    ff_rtmp_packet_dump(LOG_CONTEXT, pkt);
555
#endif
556
557 9fd6b843 Kostya Shishkov
    switch (pkt->type) {
558
    case RTMP_PT_CHUNK_SIZE:
559
        if (pkt->data_size != 4) {
560
            av_log(LOG_CONTEXT, AV_LOG_ERROR,
561
                   "Chunk size change packet is not 4 bytes long (%d)\n", pkt->data_size);
562
            return -1;
563
        }
564 6bf22e18 Sergiy
        if (!rt->is_input)
565
            ff_rtmp_packet_write(rt->stream, pkt, rt->chunk_size, rt->prev_pkt[1]);
566 9fd6b843 Kostya Shishkov
        rt->chunk_size = AV_RB32(pkt->data);
567
        if (rt->chunk_size <= 0) {
568
            av_log(LOG_CONTEXT, AV_LOG_ERROR, "Incorrect chunk size %d\n", rt->chunk_size);
569
            return -1;
570
        }
571
        av_log(LOG_CONTEXT, AV_LOG_DEBUG, "New chunk size = %d\n", rt->chunk_size);
572
        break;
573
    case RTMP_PT_PING:
574
        t = AV_RB16(pkt->data);
575
        if (t == 6)
576
            gen_pong(s, rt, pkt);
577
        break;
578 bf7c1719 Kostya Shishkov
    case RTMP_PT_CLIENT_BW:
579
        if (pkt->data_size < 4) {
580
            av_log(LOG_CONTEXT, AV_LOG_ERROR,
581
                   "Client bandwidth report packet is less than 4 bytes long (%d)\n",
582
                   pkt->data_size);
583
            return -1;
584
        }
585
        av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Client bandwidth = %d\n", AV_RB32(pkt->data));
586
        rt->client_report_size = AV_RB32(pkt->data) >> 1;
587
        break;
588 9fd6b843 Kostya Shishkov
    case RTMP_PT_INVOKE:
589
        //TODO: check for the messages sent for wrong state?
590
        if (!memcmp(pkt->data, "\002\000\006_error", 9)) {
591
            uint8_t tmpstr[256];
592
593
            if (!ff_amf_get_field_value(pkt->data + 9, data_end,
594
                                        "description", tmpstr, sizeof(tmpstr)))
595
                av_log(LOG_CONTEXT, AV_LOG_ERROR, "Server error: %s\n",tmpstr);
596
            return -1;
597
        } else if (!memcmp(pkt->data, "\002\000\007_result", 10)) {
598
            switch (rt->state) {
599
            case STATE_HANDSHAKED:
600 6bf22e18 Sergiy
                if (!rt->is_input) {
601
                    gen_release_stream(s, rt);
602
                    gen_fcpublish_stream(s, rt);
603
                    rt->state = STATE_RELEASING;
604
                } else {
605
                    rt->state = STATE_CONNECTING;
606
                }
607 9fd6b843 Kostya Shishkov
                gen_create_stream(s, rt);
608 6bf22e18 Sergiy
                break;
609
            case STATE_FCPUBLISH:
610 9fd6b843 Kostya Shishkov
                rt->state = STATE_CONNECTING;
611
                break;
612 6bf22e18 Sergiy
            case STATE_RELEASING:
613
                rt->state = STATE_FCPUBLISH;
614
                /* hack for Wowza Media Server, it does not send result for
615
                 * releaseStream and FCPublish calls */
616
                if (!pkt->data[10]) {
617
                    int pkt_id = (int) av_int2dbl(AV_RB64(pkt->data + 11));
618
                    if (pkt_id == 4)
619
                        rt->state = STATE_CONNECTING;
620
                }
621 e07c92e4 Kostya Shishkov
                if (rt->state != STATE_CONNECTING)
622 6bf22e18 Sergiy
                    break;
623 9fd6b843 Kostya Shishkov
            case STATE_CONNECTING:
624
                //extract a number from the result
625
                if (pkt->data[10] || pkt->data[19] != 5 || pkt->data[20]) {
626
                    av_log(LOG_CONTEXT, AV_LOG_WARNING, "Unexpected reply on connect()\n");
627
                } else {
628
                    rt->main_channel_id = (int) av_int2dbl(AV_RB64(pkt->data + 21));
629
                }
630 6bf22e18 Sergiy
                if (rt->is_input) {
631 c7240611 Kostya Shishkov
                    gen_play(s, rt);
632 6bf22e18 Sergiy
                } else {
633
                    gen_publish(s, rt);
634
                }
635 9fd6b843 Kostya Shishkov
                rt->state = STATE_READY;
636
                break;
637
            }
638
        } else if (!memcmp(pkt->data, "\002\000\010onStatus", 11)) {
639
            const uint8_t* ptr = pkt->data + 11;
640
            uint8_t tmpstr[256];
641
642
            for (i = 0; i < 2; i++) {
643
                t = ff_amf_tag_size(ptr, data_end);
644
                if (t < 0)
645
                    return 1;
646
                ptr += t;
647
            }
648
            t = ff_amf_get_field_value(ptr, data_end,
649
                                       "level", tmpstr, sizeof(tmpstr));
650
            if (!t && !strcmp(tmpstr, "error")) {
651
                if (!ff_amf_get_field_value(ptr, data_end,
652
                                            "description", tmpstr, sizeof(tmpstr)))
653
                    av_log(LOG_CONTEXT, AV_LOG_ERROR, "Server error: %s\n",tmpstr);
654
                return -1;
655
            }
656
            t = ff_amf_get_field_value(ptr, data_end,
657
                                       "code", tmpstr, sizeof(tmpstr));
658 6bf22e18 Sergiy
            if (!t && !strcmp(tmpstr, "NetStream.Play.Start")) rt->state = STATE_PLAYING;
659 72b870b9 Martin Storsjö
            if (!t && !strcmp(tmpstr, "NetStream.Play.Stop")) rt->state = STATE_STOPPED;
660
            if (!t && !strcmp(tmpstr, "NetStream.Play.UnpublishNotify")) rt->state = STATE_STOPPED;
661 6bf22e18 Sergiy
            if (!t && !strcmp(tmpstr, "NetStream.Publish.Start")) rt->state = STATE_PUBLISHING;
662 9fd6b843 Kostya Shishkov
        }
663
        break;
664
    }
665
    return 0;
666
}
667
668
/**
669 49bd8e4b Måns Rullgård
 * Interact with the server by receiving and sending RTMP packets until
670 9fd6b843 Kostya Shishkov
 * there is some significant data (media data or expected status notification).
671
 *
672
 * @param s          reading context
673 1d8041b3 Stefano Sabatini
 * @param for_header non-zero value tells function to work until it
674
 * gets notification from the server that playing has been started,
675
 * otherwise function will work until some media data is received (or
676
 * an error happens)
677 9fd6b843 Kostya Shishkov
 * @return 0 for successful operation, negative value in case of error
678
 */
679
static int get_packet(URLContext *s, int for_header)
680
{
681
    RTMPContext *rt = s->priv_data;
682
    int ret;
683 56e29bf2 Sergiy
    uint8_t *p;
684
    const uint8_t *next;
685
    uint32_t data_size;
686
    uint32_t ts, cts, pts=0;
687 9fd6b843 Kostya Shishkov
688 72b870b9 Martin Storsjö
    if (rt->state == STATE_STOPPED)
689
        return AVERROR_EOF;
690
691 e07c92e4 Kostya Shishkov
    for (;;) {
692 9fd6b843 Kostya Shishkov
        RTMPPacket rpkt;
693
        if ((ret = ff_rtmp_packet_read(rt->stream, &rpkt,
694 adb54961 Kostya Shishkov
                                       rt->chunk_size, rt->prev_pkt[0])) <= 0) {
695 b381a823 Kostya Shishkov
            if (ret == 0) {
696 9fd6b843 Kostya Shishkov
                return AVERROR(EAGAIN);
697
            } else {
698
                return AVERROR(EIO);
699
            }
700
        }
701 bf7c1719 Kostya Shishkov
        rt->bytes_read += ret;
702
        if (rt->bytes_read > rt->last_bytes_read + rt->client_report_size) {
703
            av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Sending bytes read report\n");
704
            gen_bytes_read(s, rt, rpkt.timestamp + 1);
705
            rt->last_bytes_read = rt->bytes_read;
706
        }
707 9fd6b843 Kostya Shishkov
708
        ret = rtmp_parse_result(s, rt, &rpkt);
709
        if (ret < 0) {//serious error in current packet
710
            ff_rtmp_packet_destroy(&rpkt);
711
            return -1;
712
        }
713 72b870b9 Martin Storsjö
        if (rt->state == STATE_STOPPED) {
714
            ff_rtmp_packet_destroy(&rpkt);
715
            return AVERROR_EOF;
716
        }
717 6bf22e18 Sergiy
        if (for_header && (rt->state == STATE_PLAYING || rt->state == STATE_PUBLISHING)) {
718 9fd6b843 Kostya Shishkov
            ff_rtmp_packet_destroy(&rpkt);
719
            return 0;
720
        }
721 6bf22e18 Sergiy
        if (!rpkt.data_size || !rt->is_input) {
722 9fd6b843 Kostya Shishkov
            ff_rtmp_packet_destroy(&rpkt);
723
            continue;
724
        }
725
        if (rpkt.type == RTMP_PT_VIDEO || rpkt.type == RTMP_PT_AUDIO ||
726 afbacb93 Kostya Shishkov
           (rpkt.type == RTMP_PT_NOTIFY && !memcmp("\002\000\012onMetaData", rpkt.data, 13))) {
727 56e29bf2 Sergiy
            ts = rpkt.timestamp;
728 9fd6b843 Kostya Shishkov
729
            // generate packet header and put data into buffer for FLV demuxer
730
            rt->flv_off  = 0;
731
            rt->flv_size = rpkt.data_size + 15;
732
            rt->flv_data = p = av_realloc(rt->flv_data, rt->flv_size);
733
            bytestream_put_byte(&p, rpkt.type);
734
            bytestream_put_be24(&p, rpkt.data_size);
735
            bytestream_put_be24(&p, ts);
736
            bytestream_put_byte(&p, ts >> 24);
737
            bytestream_put_be24(&p, 0);
738
            bytestream_put_buffer(&p, rpkt.data, rpkt.data_size);
739
            bytestream_put_be32(&p, 0);
740
            ff_rtmp_packet_destroy(&rpkt);
741
            return 0;
742
        } else if (rpkt.type == RTMP_PT_METADATA) {
743
            // we got raw FLV data, make it available for FLV demuxer
744
            rt->flv_off  = 0;
745
            rt->flv_size = rpkt.data_size;
746
            rt->flv_data = av_realloc(rt->flv_data, rt->flv_size);
747 56e29bf2 Sergiy
            /* rewrite timestamps */
748
            next = rpkt.data;
749
            ts = rpkt.timestamp;
750
            while (next - rpkt.data < rpkt.data_size - 11) {
751
                next++;
752
                data_size = bytestream_get_be24(&next);
753
                p=next;
754
                cts = bytestream_get_be24(&next);
755 aae9a093 trueice@gmail.com
                cts |= bytestream_get_byte(&next) << 24;
756 56e29bf2 Sergiy
                if (pts==0)
757
                    pts=cts;
758
                ts += cts - pts;
759
                pts = cts;
760
                bytestream_put_be24(&p, ts);
761
                bytestream_put_byte(&p, ts >> 24);
762
                next += data_size + 3 + 4;
763
            }
764 9fd6b843 Kostya Shishkov
            memcpy(rt->flv_data, rpkt.data, rpkt.data_size);
765
            ff_rtmp_packet_destroy(&rpkt);
766
            return 0;
767
        }
768
        ff_rtmp_packet_destroy(&rpkt);
769
    }
770
    return 0;
771
}
772
773
static int rtmp_close(URLContext *h)
774
{
775
    RTMPContext *rt = h->priv_data;
776
777 e07c92e4 Kostya Shishkov
    if (!rt->is_input) {
778 6bf22e18 Sergiy
        rt->flv_data = NULL;
779
        if (rt->out_pkt.data_size)
780
            ff_rtmp_packet_destroy(&rt->out_pkt);
781 615c2879 Sergiy
        if (rt->state > STATE_FCPUBLISH)
782
            gen_fcunpublish_stream(h, rt);
783 6bf22e18 Sergiy
    }
784 615c2879 Sergiy
    if (rt->state > STATE_HANDSHAKED)
785
        gen_delete_stream(h, rt);
786 6bf22e18 Sergiy
787 9fd6b843 Kostya Shishkov
    av_freep(&rt->flv_data);
788
    url_close(rt->stream);
789
    av_free(rt);
790
    return 0;
791
}
792
793
/**
794 49bd8e4b Måns Rullgård
 * Open RTMP connection and verify that the stream can be played.
795 9fd6b843 Kostya Shishkov
 *
796
 * URL syntax: rtmp://server[:port][/app][/playpath]
797
 *             where 'app' is first one or two directories in the path
798
 *             (e.g. /ondemand/, /flash/live/, etc.)
799
 *             and 'playpath' is a file name (the rest of the path,
800
 *             may be prefixed with "mp4:")
801
 */
802
static int rtmp_open(URLContext *s, const char *uri, int flags)
803
{
804
    RTMPContext *rt;
805 5e9ad759 Sergiy
    char proto[8], hostname[256], path[1024], *fname;
806 9fd6b843 Kostya Shishkov
    uint8_t buf[2048];
807 b316991b Sergiy
    int port;
808 9fd6b843 Kostya Shishkov
    int ret;
809
810
    rt = av_mallocz(sizeof(RTMPContext));
811
    if (!rt)
812
        return AVERROR(ENOMEM);
813
    s->priv_data = rt;
814 b316991b Sergiy
    rt->is_input = !(flags & URL_WRONLY);
815 9fd6b843 Kostya Shishkov
816 f3bfe388 Måns Rullgård
    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
817 f984dcf6 Martin Storsjö
                 path, sizeof(path), s->filename);
818 9fd6b843 Kostya Shishkov
819
    if (port < 0)
820
        port = RTMP_DEFAULT_PORT;
821 57b5555c Martin Storsjö
    ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
822 9fd6b843 Kostya Shishkov
823 fe523958 Kostya Shishkov
    if (url_open(&rt->stream, buf, URL_RDWR) < 0) {
824
        av_log(LOG_CONTEXT, AV_LOG_ERROR, "Cannot open connection %s\n", buf);
825 9fd6b843 Kostya Shishkov
        goto fail;
826 fe523958 Kostya Shishkov
    }
827 9fd6b843 Kostya Shishkov
828 c7240611 Kostya Shishkov
    rt->state = STATE_START;
829
    if (rtmp_handshake(s, rt))
830
        return -1;
831 9fd6b843 Kostya Shishkov
832 c7240611 Kostya Shishkov
    rt->chunk_size = 128;
833
    rt->state = STATE_HANDSHAKED;
834
    //extract "app" part from path
835
    if (!strncmp(path, "/ondemand/", 10)) {
836
        fname = path + 10;
837
        memcpy(rt->app, "ondemand", 9);
838
    } else {
839
        char *p = strchr(path + 1, '/');
840
        if (!p) {
841
            fname = path + 1;
842
            rt->app[0] = '\0';
843 9fd6b843 Kostya Shishkov
        } else {
844 c7240611 Kostya Shishkov
            char *c = strchr(p + 1, ':');
845
            fname = strchr(p + 1, '/');
846
            if (!fname || c < fname) {
847
                fname = p + 1;
848
                av_strlcpy(rt->app, path + 1, p - path);
849 9fd6b843 Kostya Shishkov
            } else {
850 c7240611 Kostya Shishkov
                fname++;
851
                av_strlcpy(rt->app, path + 1, fname - path - 1);
852 9fd6b843 Kostya Shishkov
            }
853
        }
854 c7240611 Kostya Shishkov
    }
855
    if (!strchr(fname, ':') &&
856
        (!strcmp(fname + strlen(fname) - 4, ".f4v") ||
857
         !strcmp(fname + strlen(fname) - 4, ".mp4"))) {
858
        memcpy(rt->playpath, "mp4:", 5);
859
    } else {
860
        rt->playpath[0] = 0;
861
    }
862
    strncat(rt->playpath, fname, sizeof(rt->playpath) - 5);
863 9fd6b843 Kostya Shishkov
864 bf7c1719 Kostya Shishkov
    rt->client_report_size = 1048576;
865
    rt->bytes_read = 0;
866
    rt->last_bytes_read = 0;
867
868 c7240611 Kostya Shishkov
    av_log(LOG_CONTEXT, AV_LOG_DEBUG, "Proto = %s, path = %s, app = %s, fname = %s\n",
869
           proto, path, rt->app, rt->playpath);
870
    gen_connect(s, rt, proto, hostname, port);
871 9fd6b843 Kostya Shishkov
872 c7240611 Kostya Shishkov
    do {
873
        ret = get_packet(s, 1);
874
    } while (ret == EAGAIN);
875
    if (ret < 0)
876
        goto fail;
877 6bf22e18 Sergiy
878
    if (rt->is_input) {
879 9fd6b843 Kostya Shishkov
        // generate FLV header for demuxer
880
        rt->flv_size = 13;
881
        rt->flv_data = av_realloc(rt->flv_data, rt->flv_size);
882
        rt->flv_off  = 0;
883
        memcpy(rt->flv_data, "FLV\1\5\0\0\0\011\0\0\0\0", rt->flv_size);
884 6bf22e18 Sergiy
    } else {
885
        rt->flv_size = 0;
886
        rt->flv_data = NULL;
887
        rt->flv_off  = 0;
888 9fd6b843 Kostya Shishkov
    }
889
890
    s->max_packet_size = url_get_max_packet_size(rt->stream);
891
    s->is_streamed     = 1;
892
    return 0;
893
894
fail:
895
    rtmp_close(s);
896
    return AVERROR(EIO);
897
}
898
899
static int rtmp_read(URLContext *s, uint8_t *buf, int size)
900
{
901
    RTMPContext *rt = s->priv_data;
902
    int orig_size = size;
903
    int ret;
904
905
    while (size > 0) {
906
        int data_left = rt->flv_size - rt->flv_off;
907
908
        if (data_left >= size) {
909
            memcpy(buf, rt->flv_data + rt->flv_off, size);
910
            rt->flv_off += size;
911
            return orig_size;
912
        }
913
        if (data_left > 0) {
914
            memcpy(buf, rt->flv_data + rt->flv_off, data_left);
915
            buf  += data_left;
916
            size -= data_left;
917
            rt->flv_off = rt->flv_size;
918 e8ccf245 Martin Storsjö
            return data_left;
919 9fd6b843 Kostya Shishkov
        }
920
        if ((ret = get_packet(s, 0)) < 0)
921
           return ret;
922
    }
923
    return orig_size;
924
}
925
926 9ad4c65f Anton Khirnov
static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
927 9fd6b843 Kostya Shishkov
{
928 9ad4c65f Anton Khirnov
    RTMPContext *rt = s->priv_data;
929 6bf22e18 Sergiy
    int size_temp = size;
930
    int pktsize, pkttype;
931
    uint32_t ts;
932
    const uint8_t *buf_temp = buf;
933
934
    if (size < 11) {
935
        av_log(LOG_CONTEXT, AV_LOG_DEBUG, "FLV packet too small %d\n", size);
936
        return 0;
937
    }
938
939
    do {
940
        if (!rt->flv_off) {
941
            //skip flv header
942
            if (buf_temp[0] == 'F' && buf_temp[1] == 'L' && buf_temp[2] == 'V') {
943
                buf_temp += 9 + 4;
944
                size_temp -= 9 + 4;
945
            }
946
947
            pkttype = bytestream_get_byte(&buf_temp);
948
            pktsize = bytestream_get_be24(&buf_temp);
949
            ts = bytestream_get_be24(&buf_temp);
950
            ts |= bytestream_get_byte(&buf_temp) << 24;
951
            bytestream_get_be24(&buf_temp);
952
            size_temp -= 11;
953
            rt->flv_size = pktsize;
954
955
            //force 12bytes header
956
            if (((pkttype == RTMP_PT_VIDEO || pkttype == RTMP_PT_AUDIO) && ts == 0) ||
957
                pkttype == RTMP_PT_NOTIFY) {
958
                if (pkttype == RTMP_PT_NOTIFY)
959
                    pktsize += 16;
960
                rt->prev_pkt[1][RTMP_SOURCE_CHANNEL].channel_id = 0;
961
            }
962
963
            //this can be a big packet, it's better to send it right here
964
            ff_rtmp_packet_create(&rt->out_pkt, RTMP_SOURCE_CHANNEL, pkttype, ts, pktsize);
965
            rt->out_pkt.extra = rt->main_channel_id;
966
            rt->flv_data = rt->out_pkt.data;
967
968
            if (pkttype == RTMP_PT_NOTIFY)
969
                ff_amf_write_string(&rt->flv_data, "@setDataFrame");
970
        }
971
972
        if (rt->flv_size - rt->flv_off > size_temp) {
973
            bytestream_get_buffer(&buf_temp, rt->flv_data + rt->flv_off, size_temp);
974
            rt->flv_off += size_temp;
975
        } else {
976
            bytestream_get_buffer(&buf_temp, rt->flv_data + rt->flv_off, rt->flv_size - rt->flv_off);
977
            rt->flv_off += rt->flv_size - rt->flv_off;
978
        }
979
980
        if (rt->flv_off == rt->flv_size) {
981
            bytestream_get_be32(&buf_temp);
982
983
            ff_rtmp_packet_write(rt->stream, &rt->out_pkt, rt->chunk_size, rt->prev_pkt[1]);
984
            ff_rtmp_packet_destroy(&rt->out_pkt);
985
            rt->flv_size = 0;
986
            rt->flv_off = 0;
987
        }
988
    } while (buf_temp - buf < size_temp);
989
    return size;
990 9fd6b843 Kostya Shishkov
}
991
992 c6610a21 Diego Elio Pettenò
URLProtocol ff_rtmp_protocol = {
993 9fd6b843 Kostya Shishkov
    "rtmp",
994
    rtmp_open,
995
    rtmp_read,
996
    rtmp_write,
997
    NULL, /* seek */
998
    rtmp_close,
999
};