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