Originally committed as revision 23301 to svn://svn.ffmpeg.org/ffmpeg/trunk
Zhentan Feng authored on 2010/05/25 06:59:32... | ... |
@@ -275,6 +275,7 @@ OBJS+= avio.o aviobuf.o |
275 | 275 |
OBJS-$(CONFIG_FILE_PROTOCOL) += file.o |
276 | 276 |
OBJS-$(CONFIG_GOPHER_PROTOCOL) += gopher.o |
277 | 277 |
OBJS-$(CONFIG_HTTP_PROTOCOL) += http.o httpauth.o |
278 |
+OBJS-$(CONFIG_MMST_PROTOCOL) += mmst.o asf.o |
|
278 | 279 |
OBJS-$(CONFIG_PIPE_PROTOCOL) += file.o |
279 | 280 |
|
280 | 281 |
# external or internal rtmp |
... | ... |
@@ -219,6 +219,7 @@ void av_register_all(void) |
219 | 219 |
REGISTER_PROTOCOL (FILE, file); |
220 | 220 |
REGISTER_PROTOCOL (GOPHER, gopher); |
221 | 221 |
REGISTER_PROTOCOL (HTTP, http); |
222 |
+ REGISTER_PROTOCOL (MMST, mmst); |
|
222 | 223 |
REGISTER_PROTOCOL (PIPE, pipe); |
223 | 224 |
REGISTER_PROTOCOL (RTMP, rtmp); |
224 | 225 |
#if CONFIG_LIBRTMP |
... | ... |
@@ -22,8 +22,8 @@ |
22 | 22 |
#define AVFORMAT_AVFORMAT_H |
23 | 23 |
|
24 | 24 |
#define LIBAVFORMAT_VERSION_MAJOR 52 |
25 |
-#define LIBAVFORMAT_VERSION_MINOR 64 |
|
26 |
-#define LIBAVFORMAT_VERSION_MICRO 1 |
|
25 |
+#define LIBAVFORMAT_VERSION_MINOR 65 |
|
26 |
+#define LIBAVFORMAT_VERSION_MICRO 0 |
|
27 | 27 |
|
28 | 28 |
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ |
29 | 29 |
LIBAVFORMAT_VERSION_MINOR, \ |
30 | 30 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,690 @@ |
0 |
+/* |
|
1 |
+ * MMS protocol over TCP |
|
2 |
+ * Copyright (c) 2006,2007 Ryan Martell |
|
3 | ||
4 |
+ * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com> |
|
5 |
+ * |
|
6 |
+ * This file is part of FFmpeg. |
|
7 |
+ * |
|
8 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
9 |
+ * modify it under the terms of the GNU Lesser General Public |
|
10 |
+ * License as published by the Free Software Foundation; either |
|
11 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
12 |
+ * |
|
13 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
14 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
+ * Lesser General Public License for more details. |
|
17 |
+ * |
|
18 |
+ * You should have received a copy of the GNU Lesser General Public |
|
19 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
20 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
21 |
+ */ |
|
22 |
+#include "avformat.h" |
|
23 |
+#include "internal.h" |
|
24 |
+#include "libavutil/intreadwrite.h" |
|
25 |
+#include "libavcodec/bytestream.h" |
|
26 |
+#include "network.h" |
|
27 |
+#include "asf.h" |
|
28 |
+ |
|
29 |
+#define LOCAL_ADDRESS 0xc0a80081 // FIXME get and use correct local ip address. |
|
30 |
+#define LOCAL_PORT 1037 // as above. |
|
31 |
+/** Client to server packet types. */ |
|
32 |
+typedef enum { |
|
33 |
+ CS_PKT_INITIAL = 0x01, |
|
34 |
+ CS_PKT_PROTOCOL_SELECT = 0x02, |
|
35 |
+ CS_PKT_MEDIA_FILE_REQUEST = 0x05, |
|
36 |
+ CS_PKT_START_FROM_PKT_ID = 0x07, |
|
37 |
+ CS_PKT_STREAM_PAUSE = 0x09, |
|
38 |
+ CS_PKT_STREAM_CLOSE = 0x0d, |
|
39 |
+ CS_PKT_MEDIA_HEADER_REQUEST = 0x15, |
|
40 |
+ CS_PKT_TIMING_DATA_REQUEST = 0x18, |
|
41 |
+ CS_PKT_USER_PASSWORD = 0x1a, |
|
42 |
+ CS_PKT_KEEPALIVE = 0x1b, |
|
43 |
+ CS_PKT_STREAM_ID_REQUEST = 0x33, |
|
44 |
+} MMSCSPacketType; |
|
45 |
+ |
|
46 |
+/** Server to client packet types. */ |
|
47 |
+typedef enum { |
|
48 |
+ /** Control packets. */ |
|
49 |
+ /*@{*/ |
|
50 |
+ SC_PKT_CLIENT_ACCEPTED = 0x01, |
|
51 |
+ SC_PKT_PROTOCOL_ACCEPTED = 0x02, |
|
52 |
+ SC_PKT_PROTOCOL_FAILED = 0x03, |
|
53 |
+ SC_PKT_MEDIA_PKT_FOLLOWS = 0x05, |
|
54 |
+ SC_PKT_MEDIA_FILE_DETAILS = 0x06, |
|
55 |
+ SC_PKT_HEADER_REQUEST_ACCEPTED = 0x11, |
|
56 |
+ SC_PKT_TIMING_TEST_REPLY = 0x15, |
|
57 |
+ SC_PKT_PASSWORD_REQUIRED = 0x1a, |
|
58 |
+ SC_PKT_KEEPALIVE = 0x1b, |
|
59 |
+ SC_PKT_STREAM_STOPPED = 0x1e, |
|
60 |
+ SC_PKT_STREAM_CHANGING = 0x20, |
|
61 |
+ SC_PKT_STREAM_ID_ACCEPTED = 0x21, |
|
62 |
+ /*@}*/ |
|
63 |
+ |
|
64 |
+ /** Pseudo packets. */ |
|
65 |
+ /*@{*/ |
|
66 |
+ SC_PKT_CANCEL = -1, |
|
67 |
+ SC_PKT_NO_DATA = -2, |
|
68 |
+ /*@}*/ |
|
69 |
+ |
|
70 |
+ /** Data packets. */ |
|
71 |
+ /*@{*/ |
|
72 |
+ SC_PKT_ASF_HEADER = 0x010000,// make it bigger than 0xFF in case of |
|
73 |
+ SC_PKT_ASF_MEDIA = 0x010001,// receiving false data packets. |
|
74 |
+ /*@}*/ |
|
75 |
+} MMSSCPacketType; |
|
76 |
+ |
|
77 |
+typedef struct { |
|
78 |
+ int id; |
|
79 |
+}MMSStream; |
|
80 |
+ |
|
81 |
+typedef struct { |
|
82 |
+ int outgoing_packet_seq; ///< Outgoing packet sequence number. |
|
83 |
+ char path[256]; ///< Path of the resource being asked for. |
|
84 |
+ char host[128]; ///< Host of the resources. |
|
85 |
+ |
|
86 |
+ URLContext *mms_hd; ///< TCP connection handle |
|
87 |
+ MMSStream streams[MAX_STREAMS]; |
|
88 |
+ |
|
89 |
+ /** Buffer for outgoing packets. */ |
|
90 |
+ /*@{*/ |
|
91 |
+ uint8_t *write_out_ptr; ///< Pointer for writting the buffer. |
|
92 |
+ uint8_t out_buffer[512]; ///< Buffer for outgoing packet. |
|
93 |
+ /*@}*/ |
|
94 |
+ |
|
95 |
+ /** Buffer for incoming packets. */ |
|
96 |
+ /*@{*/ |
|
97 |
+ uint8_t in_buffer[8192]; ///< Buffer for incoming packets. |
|
98 |
+ uint8_t *read_in_ptr; ///< Pointer for reading from incoming buffer. |
|
99 |
+ int remaining_in_len; ///< Reading length from incoming buffer. |
|
100 |
+ /*@}*/ |
|
101 |
+ |
|
102 |
+ int incoming_packet_seq; ///< Incoming packet sequence number. |
|
103 |
+ int incoming_flags; ///< Incoming packet flags. |
|
104 |
+ |
|
105 |
+ int packet_id; ///< Identifier for packets in the current stream. |
|
106 |
+ unsigned int header_packet_id; ///< default is 2. |
|
107 |
+ |
|
108 |
+ /** Internal handling of the ASF header */ |
|
109 |
+ /*@{*/ |
|
110 |
+ uint8_t *asf_header; ///< Stored ASF header. |
|
111 |
+ int asf_header_size; ///< Size of stored ASF header. |
|
112 |
+ int header_parsed; ///< The header has been received and parsed. |
|
113 |
+ int asf_packet_len; |
|
114 |
+ /*@}*/ |
|
115 |
+ |
|
116 |
+ int stream_num; ///< stream numbers. |
|
117 |
+ int is_playing; |
|
118 |
+} MMSContext; |
|
119 |
+ |
|
120 |
+/** Create MMST command packet header */ |
|
121 |
+static void start_command_packet(MMSContext *mms, MMSCSPacketType packet_type) |
|
122 |
+{ |
|
123 |
+ mms->write_out_ptr = mms->out_buffer; |
|
124 |
+ |
|
125 |
+ bytestream_put_le32(&mms->write_out_ptr, 1); // start sequence |
|
126 |
+ bytestream_put_le32(&mms->write_out_ptr, 0xb00bface); |
|
127 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); // Length starts from after the protocol type bytes |
|
128 |
+ bytestream_put_le32(&mms->write_out_ptr, MKTAG('M','M','S',' ')); |
|
129 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
130 |
+ bytestream_put_le32(&mms->write_out_ptr, mms->outgoing_packet_seq++); |
|
131 |
+ bytestream_put_le64(&mms->write_out_ptr, 0); // timestamp |
|
132 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
133 |
+ bytestream_put_le16(&mms->write_out_ptr, packet_type); |
|
134 |
+ bytestream_put_le16(&mms->write_out_ptr, 3); // direction to server |
|
135 |
+} |
|
136 |
+ |
|
137 |
+/** Add prefixes to MMST command packet. */ |
|
138 |
+static void insert_command_prefixes(MMSContext *mms, |
|
139 |
+ uint32_t prefix1, uint32_t prefix2) |
|
140 |
+{ |
|
141 |
+ bytestream_put_le32(&mms->write_out_ptr, prefix1); // first prefix |
|
142 |
+ bytestream_put_le32(&mms->write_out_ptr, prefix2); // second prefix |
|
143 |
+} |
|
144 |
+ |
|
145 |
+/** Send a prepared MMST command packet. */ |
|
146 |
+static int send_command_packet(MMSContext *mms) |
|
147 |
+{ |
|
148 |
+ int exact_length= mms->write_out_ptr - mms->out_buffer; |
|
149 |
+ int first_length= exact_length - 16; |
|
150 |
+ int len8= first_length/8; |
|
151 |
+ int write_result; |
|
152 |
+ |
|
153 |
+ // update packet length fields. |
|
154 |
+ AV_WL32(mms->out_buffer + 8, first_length); |
|
155 |
+ AV_WL32(mms->out_buffer + 16, len8); |
|
156 |
+ AV_WL32(mms->out_buffer + 32, len8-2); |
|
157 |
+ |
|
158 |
+ // write it out. |
|
159 |
+ write_result= url_write(mms->mms_hd, mms->out_buffer, exact_length); |
|
160 |
+ if(write_result != exact_length) { |
|
161 |
+ dprintf(NULL, "url_write returned: %d != %d\n", |
|
162 |
+ write_result, exact_length); |
|
163 |
+ return AVERROR_IO; |
|
164 |
+ } |
|
165 |
+ |
|
166 |
+ return 0; |
|
167 |
+} |
|
168 |
+ |
|
169 |
+static void mms_put_utf16(MMSContext *mms, uint8_t *src) |
|
170 |
+{ |
|
171 |
+ ByteIOContext bic; |
|
172 |
+ int size = mms->write_out_ptr - mms->out_buffer; |
|
173 |
+ int len; |
|
174 |
+ init_put_byte(&bic, mms->write_out_ptr, |
|
175 |
+ sizeof(mms->out_buffer) - size, 1, NULL, NULL, NULL, NULL); |
|
176 |
+ |
|
177 |
+ len = ff_put_str16_nolen(&bic, src); |
|
178 |
+ mms->write_out_ptr += len; |
|
179 |
+} |
|
180 |
+ |
|
181 |
+static int send_protocol_select(MMSContext *mms) |
|
182 |
+{ |
|
183 |
+ char data_string[256]; |
|
184 |
+ |
|
185 |
+ start_command_packet(mms, CS_PKT_PROTOCOL_SELECT); |
|
186 |
+ insert_command_prefixes(mms, 0, 0xffffffff); |
|
187 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); // maxFunnelBytes |
|
188 |
+ bytestream_put_le32(&mms->write_out_ptr, 0x00989680); // maxbitRate |
|
189 |
+ bytestream_put_le32(&mms->write_out_ptr, 2); // funnelMode |
|
190 |
+ snprintf(data_string, sizeof(data_string), "\\\\%d.%d.%d.%d\\%s\\%d", |
|
191 |
+ (LOCAL_ADDRESS>>24)&0xff, |
|
192 |
+ (LOCAL_ADDRESS>>16)&0xff, |
|
193 |
+ (LOCAL_ADDRESS>>8)&0xff, |
|
194 |
+ LOCAL_ADDRESS&0xff, |
|
195 |
+ "TCP", // or UDP |
|
196 |
+ LOCAL_PORT); |
|
197 |
+ |
|
198 |
+ mms_put_utf16(mms, data_string); |
|
199 |
+ return send_command_packet(mms); |
|
200 |
+} |
|
201 |
+ |
|
202 |
+static int send_media_file_request(MMSContext *mms) |
|
203 |
+{ |
|
204 |
+ start_command_packet(mms, CS_PKT_MEDIA_FILE_REQUEST); |
|
205 |
+ insert_command_prefixes(mms, 1, 0xffffffff); |
|
206 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
207 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
208 |
+ mms_put_utf16(mms, mms->path + 1); // +1 for skip "/" |
|
209 |
+ |
|
210 |
+ return send_command_packet(mms); |
|
211 |
+} |
|
212 |
+ |
|
213 |
+static void handle_packet_stream_changing_type(MMSContext *mms) |
|
214 |
+{ |
|
215 |
+ dprintf(NULL, "Stream changing!\n"); |
|
216 |
+ |
|
217 |
+ // 40 is the packet header size, 7 is the prefix size. |
|
218 |
+ mms->header_packet_id= AV_RL32(mms->in_buffer + 40 + 7); |
|
219 |
+ dprintf(NULL, "Changed header prefix to 0x%x", mms->header_packet_id); |
|
220 |
+} |
|
221 |
+ |
|
222 |
+static int send_keepalive_packet(MMSContext *mms) |
|
223 |
+{ |
|
224 |
+ // respond to a keepalive with a keepalive... |
|
225 |
+ start_command_packet(mms, CS_PKT_KEEPALIVE); |
|
226 |
+ insert_command_prefixes(mms, 1, 0x100FFFF); |
|
227 |
+ return send_command_packet(mms); |
|
228 |
+} |
|
229 |
+ |
|
230 |
+/** Pad media packets smaller than max_packet_size and/or adjust read position |
|
231 |
+ * after a seek. */ |
|
232 |
+static void pad_media_packet(MMSContext *mms) |
|
233 |
+{ |
|
234 |
+ if(mms->remaining_in_len<mms->asf_packet_len) { |
|
235 |
+ int padding_size = mms->asf_packet_len - mms->remaining_in_len; |
|
236 |
+ memset(mms->in_buffer + mms->remaining_in_len, 0, padding_size); |
|
237 |
+ mms->remaining_in_len += padding_size; |
|
238 |
+ } |
|
239 |
+} |
|
240 |
+ |
|
241 |
+/** Read incoming MMST media, header or command packet. */ |
|
242 |
+static MMSSCPacketType get_tcp_server_response(MMSContext *mms) |
|
243 |
+{ |
|
244 |
+ int read_result; |
|
245 |
+ MMSSCPacketType packet_type= -1; |
|
246 |
+ |
|
247 |
+ for(;;) { |
|
248 |
+ if((read_result= url_read_complete(mms->mms_hd, mms->in_buffer, 8))==8) { |
|
249 |
+ // handle command packet. |
|
250 |
+ if(AV_RL32(mms->in_buffer + 4)==0xb00bface) { |
|
251 |
+ mms->incoming_flags= mms->in_buffer[3]; |
|
252 |
+ read_result= url_read_complete(mms->mms_hd, mms->in_buffer+8, 4); |
|
253 |
+ if(read_result == 4) { |
|
254 |
+ int length_remaining= AV_RL32(mms->in_buffer+8) + 4; |
|
255 |
+ |
|
256 |
+ dprintf(NULL, "Length remaining is %d\n", length_remaining); |
|
257 |
+ // read the rest of the packet. |
|
258 |
+ if (length_remaining < 0 |
|
259 |
+ || length_remaining > sizeof(mms->in_buffer) - 12) { |
|
260 |
+ dprintf("Incoming message len %d exceeds buffer len %d\n", |
|
261 |
+ length_remaining, sizeof(mms->in_buffer) - 12); |
|
262 |
+ return -1; |
|
263 |
+ } |
|
264 |
+ read_result = url_read_complete(mms->mms_hd, mms->in_buffer + 12, |
|
265 |
+ length_remaining) ; |
|
266 |
+ if (read_result == length_remaining) { |
|
267 |
+ packet_type= AV_RL16(mms->in_buffer+36); |
|
268 |
+ } else { |
|
269 |
+ dprintf(NULL, "read for packet type failed%d!\n", read_result); |
|
270 |
+ return -1; |
|
271 |
+ } |
|
272 |
+ } else { |
|
273 |
+ dprintf(NULL, "read for length remaining failed%d!\n", read_result); |
|
274 |
+ return -1; |
|
275 |
+ } |
|
276 |
+ } else { |
|
277 |
+ int length_remaining; |
|
278 |
+ int packet_id_type; |
|
279 |
+ int tmp; |
|
280 |
+ |
|
281 |
+ assert(mms->remaining_in_len==0); |
|
282 |
+ |
|
283 |
+ // note we cache the first 8 bytes, |
|
284 |
+ // then fill up the buffer with the others |
|
285 |
+ tmp = AV_RL16(mms->in_buffer + 6); |
|
286 |
+ length_remaining = (tmp - 8) & 0xffff; |
|
287 |
+ mms->incoming_packet_seq = AV_RL32(mms->in_buffer); |
|
288 |
+ packet_id_type = mms->in_buffer[4]; |
|
289 |
+ mms->incoming_flags = mms->in_buffer[5]; |
|
290 |
+ |
|
291 |
+ if (length_remaining < 0 |
|
292 |
+ || length_remaining > sizeof(mms->in_buffer) - 8) { |
|
293 |
+ dprintf("Incoming data len %d exceeds buffer len %d\n", |
|
294 |
+ length_remaining, sizeof(mms->in_buffer)); |
|
295 |
+ return -1; |
|
296 |
+ } |
|
297 |
+ mms->remaining_in_len = length_remaining; |
|
298 |
+ mms->read_in_ptr = mms->in_buffer; |
|
299 |
+ read_result= url_read_complete(mms->mms_hd, mms->in_buffer, length_remaining); |
|
300 |
+ if(read_result != length_remaining) { |
|
301 |
+ dprintf(NULL, "read_bytes result: %d asking for %d\n", |
|
302 |
+ read_result, length_remaining); |
|
303 |
+ return -1; |
|
304 |
+ } else { |
|
305 |
+ // if we successfully read everything. |
|
306 |
+ if(packet_id_type == mms->header_packet_id) { |
|
307 |
+ packet_type = SC_PKT_ASF_HEADER; |
|
308 |
+ // Store the asf header |
|
309 |
+ if(!mms->header_parsed) { |
|
310 |
+ void *p = av_realloc(mms->asf_header, |
|
311 |
+ mms->asf_header_size |
|
312 |
+ + mms->remaining_in_len); |
|
313 |
+ if (!p) { |
|
314 |
+ av_freep(&mms->asf_header); |
|
315 |
+ return AVERROR(ENOMEM); |
|
316 |
+ } |
|
317 |
+ mms->asf_header = p; |
|
318 |
+ memcpy(mms->asf_header + mms->asf_header_size, |
|
319 |
+ mms->read_in_ptr, |
|
320 |
+ mms->remaining_in_len); |
|
321 |
+ mms->asf_header_size += mms->remaining_in_len; |
|
322 |
+ } |
|
323 |
+ } else if(packet_id_type == mms->packet_id) { |
|
324 |
+ packet_type = SC_PKT_ASF_MEDIA; |
|
325 |
+ } else { |
|
326 |
+ dprintf(NULL, "packet id type %d is old.", packet_id_type); |
|
327 |
+ continue; |
|
328 |
+ } |
|
329 |
+ } |
|
330 |
+ } |
|
331 |
+ |
|
332 |
+ // preprocess some packet type |
|
333 |
+ if(packet_type == SC_PKT_KEEPALIVE) { |
|
334 |
+ send_keepalive_packet(mms); |
|
335 |
+ continue; |
|
336 |
+ } else if(packet_type == SC_PKT_STREAM_CHANGING) { |
|
337 |
+ handle_packet_stream_changing_type(mms); |
|
338 |
+ } else if(packet_type == SC_PKT_ASF_MEDIA) { |
|
339 |
+ pad_media_packet(mms); |
|
340 |
+ } |
|
341 |
+ return packet_type; |
|
342 |
+ } else { |
|
343 |
+ if(read_result<0) { |
|
344 |
+ dprintf(NULL, "Read error (or cancelled) returned %d!\n", read_result); |
|
345 |
+ packet_type = SC_PKT_CANCEL; |
|
346 |
+ } else { |
|
347 |
+ dprintf(NULL, "Read result of zero?!\n"); |
|
348 |
+ packet_type = SC_PKT_NO_DATA; |
|
349 |
+ } |
|
350 |
+ return packet_type; |
|
351 |
+ } |
|
352 |
+ } |
|
353 |
+} |
|
354 |
+ |
|
355 |
+static int mms_safe_send_recv(MMSContext *mms, |
|
356 |
+ int (*send_fun)(MMSContext *mms), |
|
357 |
+ const MMSSCPacketType expect_type) |
|
358 |
+{ |
|
359 |
+ MMSSCPacketType type; |
|
360 |
+ if(send_fun) { |
|
361 |
+ int ret = send_fun(mms); |
|
362 |
+ if (ret < 0) { |
|
363 |
+ dprintf(NULL, "Send Packet error before expecting recv packet %d\n", expect_type); |
|
364 |
+ return ret; |
|
365 |
+ } |
|
366 |
+ } |
|
367 |
+ |
|
368 |
+ if ((type = get_tcp_server_response(mms)) != expect_type) { |
|
369 |
+ dprintf(NULL,"Unexpected packet type %d with type %d\n", type, expect_type); |
|
370 |
+ return -1; |
|
371 |
+ } else { |
|
372 |
+ return 0; |
|
373 |
+ } |
|
374 |
+} |
|
375 |
+ |
|
376 |
+static int send_media_header_request(MMSContext *mms) |
|
377 |
+{ |
|
378 |
+ start_command_packet(mms, CS_PKT_MEDIA_HEADER_REQUEST); |
|
379 |
+ insert_command_prefixes(mms, 1, 0); |
|
380 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
381 |
+ bytestream_put_le32(&mms->write_out_ptr, 0x00800000); |
|
382 |
+ bytestream_put_le32(&mms->write_out_ptr, 0xffffffff); |
|
383 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
384 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
385 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
386 |
+ |
|
387 |
+ // the media preroll value in milliseconds? |
|
388 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
389 |
+ bytestream_put_le32(&mms->write_out_ptr, 0x40AC2000); |
|
390 |
+ bytestream_put_le32(&mms->write_out_ptr, 2); |
|
391 |
+ bytestream_put_le32(&mms->write_out_ptr, 0); |
|
392 |
+ |
|
393 |
+ return send_command_packet(mms); |
|
394 |
+} |
|
395 |
+ |
|
396 |
+/** Send the initial handshake. */ |
|
397 |
+static int send_startup_packet(MMSContext *mms) |
|
398 |
+{ |
|
399 |
+ char data_string[256]; |
|
400 |
+ // SubscriberName is defined in MS specification linked below. |
|
401 |
+ // The guid value can be any valid value. |
|
402 |
+ // http://download.microsoft.com/ |
|
403 |
+ // download/9/5/E/95EF66AF-9026-4BB0-A41D-A4F81802D92C/%5BMS-WMSP%5D.pdf |
|
404 |
+ snprintf(data_string, sizeof(data_string), |
|
405 |
+ "NSPlayer/7.0.0.1956; {%s}; Host: %s", |
|
406 |
+ "7E667F5D-A661-495E-A512-F55686DDA178", mms->host); |
|
407 |
+ |
|
408 |
+ start_command_packet(mms, CS_PKT_INITIAL); |
|
409 |
+ insert_command_prefixes(mms, 0, 0x0004000b); |
|
410 |
+ bytestream_put_le32(&mms->write_out_ptr, 0x0003001c); |
|
411 |
+ mms_put_utf16(mms, data_string); |
|
412 |
+ return send_command_packet(mms); |
|
413 |
+} |
|
414 |
+ |
|
415 |
+static int asf_header_parser(MMSContext *mms) |
|
416 |
+{ |
|
417 |
+ uint8_t *p = mms->asf_header; |
|
418 |
+ uint8_t *end; |
|
419 |
+ int flags, stream_id, real_header_size; |
|
420 |
+ mms->stream_num = 0; |
|
421 |
+ |
|
422 |
+ if (mms->asf_header_size < sizeof(ff_asf_guid) * 2 + 22 || |
|
423 |
+ memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) |
|
424 |
+ return -1; |
|
425 |
+ |
|
426 |
+ real_header_size = AV_RL64(p + sizeof(ff_asf_guid)); |
|
427 |
+ end = mms->asf_header + real_header_size; |
|
428 |
+ |
|
429 |
+ p += sizeof(ff_asf_guid) + 14; |
|
430 |
+ while(end - p >= sizeof(ff_asf_guid) + 8) { |
|
431 |
+ uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid)); |
|
432 |
+ if (!chunksize || chunksize > end - p) { |
|
433 |
+ dprintf("chunksize is exceptional value:%d!\n", chunksize); |
|
434 |
+ return -1; |
|
435 |
+ } |
|
436 |
+ if (!memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) { |
|
437 |
+ /* read packet size */ |
|
438 |
+ if (end - p > sizeof(ff_asf_guid) * 2 + 68) { |
|
439 |
+ mms->asf_packet_len = AV_RL32(p + sizeof(ff_asf_guid) * 2 + 64); |
|
440 |
+ if (mms->asf_packet_len <= 0 || mms->asf_packet_len > sizeof(mms->in_buffer)) { |
|
441 |
+ dprintf(NULL,"Too large packet len:%d" |
|
442 |
+ " may overwrite in_buffer when padding", mms->asf_packet_len); |
|
443 |
+ return -1; |
|
444 |
+ } |
|
445 |
+ } |
|
446 |
+ } else if (!memcmp(p, ff_asf_stream_header, sizeof(ff_asf_guid))) { |
|
447 |
+ flags = AV_RL16(p + sizeof(ff_asf_guid)*3 + 24); |
|
448 |
+ stream_id = flags & 0x7F; |
|
449 |
+ //The second condition is for checking CS_PKT_STREAM_ID_REQUEST packet size, |
|
450 |
+ //we can calcuate the packet size by stream_num. |
|
451 |
+ //Please see function send_stream_selection_request(). |
|
452 |
+ if (mms->stream_num < MAX_STREAMS && |
|
453 |
+ 46 + mms->stream_num * 6 < sizeof(mms->out_buffer)) { |
|
454 |
+ mms->streams[mms->stream_num].id = stream_id; |
|
455 |
+ mms->stream_num++; |
|
456 |
+ } else { |
|
457 |
+ dprintf("Too many streams.\n"); |
|
458 |
+ return -1; |
|
459 |
+ } |
|
460 |
+ } |
|
461 |
+ p += chunksize; |
|
462 |
+ } |
|
463 |
+ |
|
464 |
+ return 0; |
|
465 |
+} |
|
466 |
+ |
|
467 |
+/** Send MMST stream selection command based on the AVStream->discard values. */ |
|
468 |
+static int send_stream_selection_request(MMSContext *mms) |
|
469 |
+{ |
|
470 |
+ int i; |
|
471 |
+ |
|
472 |
+ // send the streams we want back... |
|
473 |
+ start_command_packet(mms, CS_PKT_STREAM_ID_REQUEST); |
|
474 |
+ bytestream_put_le32(&mms->write_out_ptr, mms->stream_num); // stream nums |
|
475 |
+ for(i= 0; i<mms->stream_num; i++) { |
|
476 |
+ bytestream_put_le16(&mms->write_out_ptr, 0xffff); // flags |
|
477 |
+ bytestream_put_le16(&mms->write_out_ptr, mms->streams[i].id); // stream id |
|
478 |
+ bytestream_put_le16(&mms->write_out_ptr, 0); // selection |
|
479 |
+ } |
|
480 |
+ |
|
481 |
+ bytestream_put_le16(&mms->write_out_ptr, 0); |
|
482 |
+ |
|
483 |
+ return send_command_packet(mms); |
|
484 |
+} |
|
485 |
+ |
|
486 |
+static int read_data(MMSContext *mms, uint8_t *buf, const int buf_size) |
|
487 |
+{ |
|
488 |
+ int read_size; |
|
489 |
+ read_size = FFMIN(buf_size, mms->remaining_in_len); |
|
490 |
+ memcpy(buf, mms->read_in_ptr, read_size); |
|
491 |
+ mms->remaining_in_len -= read_size; |
|
492 |
+ mms->read_in_ptr += read_size; |
|
493 |
+ return read_size; |
|
494 |
+} |
|
495 |
+ |
|
496 |
+/** Read at most one media packet (or a whole header). */ |
|
497 |
+static int read_mms_packet(MMSContext *mms, uint8_t *buf, int buf_size) |
|
498 |
+{ |
|
499 |
+ int result = 0, read_header_size = 0; |
|
500 |
+ int size_to_copy; |
|
501 |
+ |
|
502 |
+ do { |
|
503 |
+ if(read_header_size < mms->asf_header_size && !mms->is_playing) { |
|
504 |
+ /* Read from ASF header buffer */ |
|
505 |
+ size_to_copy= FFMIN(buf_size, |
|
506 |
+ mms->asf_header_size - read_header_size); |
|
507 |
+ memcpy(buf, mms->asf_header + read_header_size, size_to_copy); |
|
508 |
+ read_header_size += size_to_copy; |
|
509 |
+ result += size_to_copy; |
|
510 |
+ dprintf(NULL, "Copied %d bytes from stored header. left: %d\n", |
|
511 |
+ size_to_copy, mms->asf_header_size - read_header_size); |
|
512 |
+ if (mms->asf_header_size == read_header_size) { |
|
513 |
+ av_freep(&mms->asf_header); |
|
514 |
+ mms->is_playing = 1; |
|
515 |
+ } |
|
516 |
+ } else if(mms->remaining_in_len) { |
|
517 |
+ /* Read remaining packet data to buffer. |
|
518 |
+ * the result can not be zero because remaining_in_len is positive.*/ |
|
519 |
+ result = read_data(mms, buf, buf_size); |
|
520 |
+ } else { |
|
521 |
+ /* Read from network */ |
|
522 |
+ int err = mms_safe_send_recv(mms, NULL, SC_PKT_ASF_MEDIA); |
|
523 |
+ if (err == 0) { |
|
524 |
+ if(mms->remaining_in_len>mms->asf_packet_len) { |
|
525 |
+ dprintf(NULL, "Incoming packet" |
|
526 |
+ "larger than the asf packet size stated (%d>%d)\n", |
|
527 |
+ mms->remaining_in_len, mms->asf_packet_len); |
|
528 |
+ result= AVERROR_IO; |
|
529 |
+ } else { |
|
530 |
+ // copy the data to the packet buffer. |
|
531 |
+ result = read_data(mms, buf, buf_size); |
|
532 |
+ if (result == 0) { |
|
533 |
+ dprintf(NULL, "read asf media paket size is zero!\n"); |
|
534 |
+ break; |
|
535 |
+ } |
|
536 |
+ } |
|
537 |
+ } else { |
|
538 |
+ dprintf(NULL, "read packet error!\n"); |
|
539 |
+ break; |
|
540 |
+ } |
|
541 |
+ } |
|
542 |
+ } while(!result); // only return one packet. |
|
543 |
+ return result; |
|
544 |
+} |
|
545 |
+ |
|
546 |
+static int send_close_packet(MMSContext *mms) |
|
547 |
+{ |
|
548 |
+ start_command_packet(mms, CS_PKT_STREAM_CLOSE); |
|
549 |
+ insert_command_prefixes(mms, 1, 1); |
|
550 |
+ |
|
551 |
+ return send_command_packet(mms); |
|
552 |
+} |
|
553 |
+ |
|
554 |
+/** Close the MMSH/MMST connection */ |
|
555 |
+static int mms_close(URLContext *h) |
|
556 |
+{ |
|
557 |
+ MMSContext *mms = (MMSContext *)h->priv_data; |
|
558 |
+ |
|
559 |
+ if(mms->mms_hd) { |
|
560 |
+ send_close_packet(mms); |
|
561 |
+ url_close(mms->mms_hd); |
|
562 |
+ } |
|
563 |
+ |
|
564 |
+ /* free all separately allocated pointers in mms */ |
|
565 |
+ av_free(mms->asf_header); |
|
566 |
+ av_freep(&h->priv_data); |
|
567 |
+ |
|
568 |
+ return 0; |
|
569 |
+} |
|
570 |
+ |
|
571 |
+static int mms_open(URLContext *h, const char *uri, int flags) |
|
572 |
+{ |
|
573 |
+ MMSContext *mms; |
|
574 |
+ int port, err; |
|
575 |
+ char tcpname[256]; |
|
576 |
+ |
|
577 |
+ h->is_streamed = 1; |
|
578 |
+ mms = h->priv_data = av_mallocz(sizeof(MMSContext)); |
|
579 |
+ if (!h->priv_data) |
|
580 |
+ return AVERROR(ENOMEM); |
|
581 |
+ |
|
582 |
+ // only for MMS over TCP, so set proto = NULL |
|
583 |
+ ff_url_split(NULL, 0, NULL, 0, |
|
584 |
+ mms->host, sizeof(mms->host), &port, mms->path, |
|
585 |
+ sizeof(mms->path), uri); |
|
586 |
+ |
|
587 |
+ if(port<0) |
|
588 |
+ port = 1755; // defaut mms protocol port |
|
589 |
+ |
|
590 |
+ // establish tcp connection. |
|
591 |
+ ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, mms->host, port, NULL); |
|
592 |
+ err = url_open(&mms->mms_hd, tcpname, URL_RDWR); |
|
593 |
+ if (err) |
|
594 |
+ goto fail; |
|
595 |
+ |
|
596 |
+ mms->packet_id = 3; // default, initial value. |
|
597 |
+ mms->header_packet_id = 2; // default, initial value. |
|
598 |
+ err = mms_safe_send_recv(mms, send_startup_packet, SC_PKT_CLIENT_ACCEPTED); |
|
599 |
+ if (err) |
|
600 |
+ goto fail; |
|
601 |
+ err = mms_safe_send_recv(mms, send_protocol_select, SC_PKT_PROTOCOL_ACCEPTED); |
|
602 |
+ if (err) |
|
603 |
+ goto fail; |
|
604 |
+ err = mms_safe_send_recv(mms, send_media_file_request, SC_PKT_MEDIA_FILE_DETAILS); |
|
605 |
+ if (err) |
|
606 |
+ goto fail; |
|
607 |
+ err = mms_safe_send_recv(mms, send_media_header_request, SC_PKT_HEADER_REQUEST_ACCEPTED); |
|
608 |
+ if (err) |
|
609 |
+ goto fail; |
|
610 |
+ err = mms_safe_send_recv(mms, NULL, SC_PKT_ASF_HEADER); |
|
611 |
+ if (err) |
|
612 |
+ goto fail; |
|
613 |
+ if((mms->incoming_flags != 0X08) && (mms->incoming_flags != 0X0C)) |
|
614 |
+ goto fail; |
|
615 |
+ err = asf_header_parser(mms); |
|
616 |
+ if (err) { |
|
617 |
+ dprintf(NULL, "asf header parsed failed!\n"); |
|
618 |
+ goto fail; |
|
619 |
+ } |
|
620 |
+ mms->header_parsed = 1; |
|
621 |
+ |
|
622 |
+ if (!mms->asf_packet_len || !mms->stream_num) |
|
623 |
+ goto fail; |
|
624 |
+ |
|
625 |
+ dprintf(NULL, "Leaving open (success)\n"); |
|
626 |
+ return 0; |
|
627 |
+fail: |
|
628 |
+ mms_close(h); |
|
629 |
+ dprintf(NULL, "Leaving open (failure: %d)\n", err); |
|
630 |
+ return err; |
|
631 |
+} |
|
632 |
+ |
|
633 |
+static int send_media_packet_request(MMSContext *mms) |
|
634 |
+{ |
|
635 |
+ start_command_packet(mms, CS_PKT_START_FROM_PKT_ID); |
|
636 |
+ insert_command_prefixes(mms, 1, 0x0001FFFF); |
|
637 |
+ bytestream_put_le64(&mms->write_out_ptr, 0); // seek timestamp |
|
638 |
+ bytestream_put_le32(&mms->write_out_ptr, 0xffffffff); // unknown |
|
639 |
+ bytestream_put_le32(&mms->write_out_ptr, 0xffffffff); // packet offset |
|
640 |
+ bytestream_put_byte(&mms->write_out_ptr, 0xff); // max stream time limit |
|
641 |
+ bytestream_put_byte(&mms->write_out_ptr, 0xff); // max stream time limit |
|
642 |
+ bytestream_put_byte(&mms->write_out_ptr, 0xff); // max stream time limit |
|
643 |
+ bytestream_put_byte(&mms->write_out_ptr, 0x00); // stream time limit flag |
|
644 |
+ |
|
645 |
+ mms->packet_id++; // new packet_id |
|
646 |
+ bytestream_put_le32(&mms->write_out_ptr, mms->packet_id); |
|
647 |
+ return send_command_packet(mms); |
|
648 |
+} |
|
649 |
+ |
|
650 |
+ |
|
651 |
+static void clear_stream_buffers(MMSContext *mms) |
|
652 |
+{ |
|
653 |
+ mms->remaining_in_len = 0; |
|
654 |
+ mms->read_in_ptr = mms->in_buffer; |
|
655 |
+} |
|
656 |
+ |
|
657 |
+/** Read ASF data through the protocol. */ |
|
658 |
+static int mms_read(URLContext *h, uint8_t *buf, int size) |
|
659 |
+{ |
|
660 |
+ /* TODO: see tcp.c:tcp_read() about a possible timeout scheme */ |
|
661 |
+ MMSContext *mms = h->priv_data; |
|
662 |
+ int result = 0; |
|
663 |
+ |
|
664 |
+ /* Since we read the header at open(), this shouldn't be possible */ |
|
665 |
+ assert(mms->header_parsed); |
|
666 |
+ |
|
667 |
+ if (!mms->is_playing) { |
|
668 |
+ dprintf(NULL, "mms_read() before play().\n"); |
|
669 |
+ clear_stream_buffers(mms); |
|
670 |
+ result = mms_safe_send_recv(mms, send_stream_selection_request, SC_PKT_STREAM_ID_ACCEPTED); |
|
671 |
+ if (result) |
|
672 |
+ return result; |
|
673 |
+ // send media packet request |
|
674 |
+ result = mms_safe_send_recv(mms, send_media_packet_request, SC_PKT_MEDIA_PKT_FOLLOWS); |
|
675 |
+ if (result) { |
|
676 |
+ return result; |
|
677 |
+ } |
|
678 |
+ } |
|
679 |
+ return read_mms_packet(mms, buf, size); |
|
680 |
+} |
|
681 |
+ |
|
682 |
+URLProtocol mmst_protocol = { |
|
683 |
+ "mmst", |
|
684 |
+ mms_open, |
|
685 |
+ mms_read, |
|
686 |
+ NULL, // write |
|
687 |
+ NULL, // seek |
|
688 |
+ mms_close, |
|
689 |
+}; |