2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #include "libmpdemux/asf.h"
39 #include "libmpdemux/demuxer.h"
40 #include "asf_mmst_streaming.h"
44 #include "libavutil/intreadwrite.h"
46 #include "libmpdemux/asfguid.h"
48 extern int network_bandwidth
;
50 static int asf_http_streaming_start(stream_t
*stream
, int *demuxer_type
);
52 static int asf_read_wrapper(int fd
, void *buffer
, int len
, streaming_ctrl_t
*stream_ctrl
) {
53 uint8_t *buf
= buffer
;
55 int got
= nop_streaming_read(fd
, buf
, len
, stream_ctrl
);
57 mp_msg(MSGT_NETWORK
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream
);
66 // We can try several protocol for asf streaming
67 // * first the UDP protcol, if there is a firewall, UDP
68 // packets will not come back, so the mmsu will fail.
69 // * Then we can try TCP, but if there is a proxy for
70 // internet connection, the TCP connection will not get
72 // * Then we can try HTTP.
74 // Note: Using WMP sequence MMSU then MMST and then HTTP.
76 static int asf_streaming_start( stream_t
*stream
, int *demuxer_type
) {
77 char *proto
= stream
->streaming_ctrl
->url
->protocol
;
79 int port
= stream
->streaming_ctrl
->url
->port
;
81 // Is protocol mms or mmsu?
82 if (!strcasecmp(proto
, "mmsu") || !strcasecmp(proto
, "mms"))
84 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/UDP...\n");
85 //fd = asf_mmsu_streaming_start( stream );
86 if( fd
>-1 ) return fd
; //mmsu support is not implemented yet - using this code
87 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/UDP failed\n");
88 if( fd
==-2 ) return -1;
91 //Is protocol mms or mmst?
92 if (!strcasecmp(proto
, "mmst") || !strcasecmp(proto
, "mms"))
94 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/TCP...\n");
95 fd
= asf_mmst_streaming_start( stream
);
96 stream
->streaming_ctrl
->url
->port
= port
;
97 if( fd
>-1 ) return fd
;
98 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/TCP failed\n");
99 if( fd
==-2 ) return -1;
102 //Is protocol http, http_proxy, or mms?
103 if (!strcasecmp(proto
, "http_proxy") || !strcasecmp(proto
, "http") ||
104 !strcasecmp(proto
, "mms") || !strcasecmp(proto
, "mmsh") ||
105 !strcasecmp(proto
, "mmshttp"))
107 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/HTTP...\n");
108 fd
= asf_http_streaming_start( stream
, demuxer_type
);
109 stream
->streaming_ctrl
->url
->port
= port
;
110 if( fd
>-1 ) return fd
;
111 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/HTTP failed\n");
112 if( fd
==-2 ) return -1;
119 static int asf_streaming(ASF_stream_chunck_t
*stream_chunck
, int *drop_packet
) {
121 printf("ASF stream chunck size=%d\n", stream_chunck->size);
122 printf("length: %d\n", length );
123 printf("0x%02X\n", stream_chunck->type );
125 if( drop_packet
!=NULL
) *drop_packet
= 0;
127 if( stream_chunck
->size
<8 ) {
128 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small
, stream_chunck
->size
);
131 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
132 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch
, stream_chunck
->size
, stream_chunck
->size_confirm
);
136 printf(" type: 0x%02X\n", stream_chunck->type );
137 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
138 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
139 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
140 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
142 switch(stream_chunck
->type
) {
143 case ASF_STREAMING_CLEAR
: // $C Clear ASF configuration
144 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Clearing ASF stream configuration!\n");
145 if( drop_packet
!=NULL
) *drop_packet
= 1;
146 return stream_chunck
->size
;
148 case ASF_STREAMING_DATA
: // $D Data follows
149 // printf("=====> Data follows\n");
151 case ASF_STREAMING_END_TRANS
: // $E Transfer complete
152 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Transfer complete\n");
153 if( drop_packet
!=NULL
) *drop_packet
= 1;
154 return stream_chunck
->size
;
156 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
157 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
160 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
162 return stream_chunck
->size
+4;
168 static void close_s(stream_t
*stream
) {
169 closesocket(stream
->fd
);
173 static int max_idx(int s_count
, int *s_rates
, int bound
) {
174 int i
, best
= -1, rate
= -1;
175 for (i
= 0; i
< s_count
; i
++) {
176 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
184 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
185 ASF_stream_chunck_t chunk
;
186 asf_http_streaming_ctrl_t
* asf_ctrl
= streaming_ctrl
->data
;
187 char* buffer
=NULL
, *chunk_buffer
=NULL
;
188 int i
,r
,size
,pos
= 0;
191 int chunk_size2read
= 0;
192 int bw
= streaming_ctrl
->bandwidth
;
193 int *v_rates
= NULL
, *a_rates
= NULL
;
194 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
196 if(asf_ctrl
== NULL
) return -1;
198 // The ASF header can be in several network chunks. For example if the content description
199 // is big, the ASF header will be split in 2 network chunk.
200 // So we need to retrieve all the chunk before starting to parse the header.
202 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
204 // Endian handling of the stream chunk
205 le2me_ASF_stream_chunck_t(&chunk
);
206 size
= asf_streaming( &chunk
, &r
) - sizeof(ASF_stream_chunck_t
);
207 if(r
) mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_WarnDropHeader
);
209 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
212 if (chunk
.type
!= ASF_STREAMING_HEADER
) {
213 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk
);
217 // audit: do not overflow buffer_size
218 if (size
> SIZE_MAX
- buffer_size
) return -1;
219 buffer
= malloc(size
+buffer_size
);
221 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MPDEMUX_ASF_BufferMallocFailed
,size
+buffer_size
);
224 if( chunk_buffer
!=NULL
) {
225 memcpy( buffer
, chunk_buffer
, buffer_size
);
226 free( chunk_buffer
);
228 chunk_buffer
= buffer
;
229 buffer
+= buffer_size
;
232 if (asf_read_wrapper(fd
, buffer
, size
, streaming_ctrl
) <= 0)
235 if( chunk_size2read
==0 ) {
236 ASF_header_t
*asfh
= (ASF_header_t
*)buffer
;
237 if(size
< (int)sizeof(ASF_header_t
)) {
238 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunk2Small
);
240 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
241 chunk_size2read
= AV_RL64(&asfh
->objh
.size
);
242 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
244 } while( buffer_size
<chunk_size2read
);
245 buffer
= chunk_buffer
;
248 start
= sizeof(ASF_header_t
);
250 pos
= find_asf_guid(buffer
, asf_file_header_guid
, start
, size
);
252 ASF_file_header_t
*fileh
= (ASF_file_header_t
*) &buffer
[pos
];
253 pos
+= sizeof(ASF_file_header_t
);
254 if (pos
> size
) goto len_err_out
;
256 if(fileh.packetsize != fileh.packetsize2) {
257 printf("Error packetsize check don't match\n");
261 asf_ctrl
->packet_size
= AV_RL32(&fileh
->max_packet_size
);
263 // preroll: time in ms to bufferize before playing
264 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
268 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
270 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
271 pos
+= sizeof(ASF_stream_header_t
);
272 if (pos
> size
) goto len_err_out
;
273 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
274 case 0xF8699E40 : // audio stream
275 if(asf_ctrl
->audio_streams
== NULL
){
276 asf_ctrl
->audio_streams
= malloc(sizeof(int));
277 asf_ctrl
->n_audio
= 1;
280 asf_ctrl
->audio_streams
= realloc(asf_ctrl
->audio_streams
,
281 asf_ctrl
->n_audio
*sizeof(int));
283 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = AV_RL16(&streamh
->stream_no
);
285 case 0xBC19EFC0 : // video stream
286 if(asf_ctrl
->video_streams
== NULL
){
287 asf_ctrl
->video_streams
= malloc(sizeof(int));
288 asf_ctrl
->n_video
= 1;
291 asf_ctrl
->video_streams
= realloc(asf_ctrl
->video_streams
,
292 asf_ctrl
->n_video
*sizeof(int));
294 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = AV_RL16(&streamh
->stream_no
);
299 // always allocate to avoid lots of ifs later
300 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
301 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
303 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
305 // stream bitrate properties object
307 char *ptr
= &buffer
[pos
];
308 char *end
= &buffer
[size
];
310 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
311 if (ptr
+ 2 > end
) goto len_err_out
;
312 stream_count
= AV_RL16(ptr
);
314 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
315 stream_count
, stream_count
);
316 for( i
=0 ; i
<stream_count
; i
++ ) {
320 if (ptr
+ 6 > end
) goto len_err_out
;
325 mp_msg(MSGT_NETWORK
, MSGL_V
,
326 " stream id=[0x%x][%u]\n", id
, id
);
327 mp_msg(MSGT_NETWORK
, MSGL_V
,
328 " max bitrate=[0x%x][%u]\n", rate
, rate
);
329 for (j
= 0; j
< asf_ctrl
->n_video
; j
++) {
330 if (id
== asf_ctrl
->video_streams
[j
]) {
331 mp_msg(MSGT_NETWORK
, MSGL_V
, " is video stream\n");
336 for (j
= 0; j
< asf_ctrl
->n_audio
; j
++) {
337 if (id
== asf_ctrl
->audio_streams
[j
]) {
338 mp_msg(MSGT_NETWORK
, MSGL_V
, " is audio stream\n");
347 // automatic stream selection based on bandwidth
348 if (bw
== 0) bw
= INT_MAX
;
349 mp_msg(MSGT_NETWORK
, MSGL_V
, "Max bandwidth set to %d\n", bw
);
351 if (asf_ctrl
->n_audio
) {
352 // find lowest-bitrate audio stream
355 for (i
= 0; i
< asf_ctrl
->n_audio
; i
++) {
356 if (a_rates
[i
] < a_rate
) {
361 if (max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
) < 0) {
362 // both audio and video are not possible, try video only next
367 // find best video stream
368 v_idx
= max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
);
370 v_rate
= v_rates
[v_idx
];
372 // find best audio stream
373 a_idx
= max_idx(asf_ctrl
->n_audio
, a_rates
, bw
- v_rate
);
378 if (a_idx
< 0 && v_idx
< 0) {
379 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay
);
384 // a audio stream was forced
385 asf_ctrl
->audio_id
= audio_id
;
387 asf_ctrl
->audio_id
= asf_ctrl
->audio_streams
[a_idx
];
388 else if (asf_ctrl
->n_audio
) {
389 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio
);
394 // a video stream was forced
395 asf_ctrl
->video_id
= video_id
;
397 asf_ctrl
->video_id
= asf_ctrl
->video_streams
[v_idx
];
398 else if (asf_ctrl
->n_video
) {
399 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo
);
406 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_InvalidLenInHeader
);
407 if (buffer
) free(buffer
);
408 if (v_rates
) free(v_rates
);
409 if (a_rates
) free(a_rates
);
413 static int asf_http_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*streaming_ctrl
) {
414 static ASF_stream_chunck_t chunk
;
415 int read
,chunk_size
= 0;
416 static int rest
= 0, drop_chunk
= 0, waiting
= 0;
417 asf_http_streaming_ctrl_t
*asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
420 if (rest
== 0 && waiting
== 0) {
421 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
424 // Endian handling of the stream chunk
425 le2me_ASF_stream_chunck_t(&chunk
);
426 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
428 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
431 chunk_size
-= sizeof(ASF_stream_chunck_t
);
433 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
434 if (asf_http_ctrl
->packet_size
< chunk_size
) {
435 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket
);
438 waiting
= asf_http_ctrl
->packet_size
;
440 waiting
= chunk_size
;
449 if ( waiting
>= chunk_size
) {
450 if (chunk_size
> size
){
451 rest
= chunk_size
- size
;
454 if (asf_read_wrapper(fd
, buffer
, chunk_size
, streaming_ctrl
) <= 0)
458 if (drop_chunk
) continue;
460 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
461 int s
= FFMIN(waiting
,size
-read
);
462 memset(buffer
+read
,0,s
);
472 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
474 // to shut up gcc warning
480 static int asf_header_check( HTTP_header_t
*http_hdr
) {
481 ASF_obj_header_t
*objh
;
482 if( http_hdr
==NULL
) return -1;
483 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
485 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
486 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
490 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
491 if( content_type
==NULL
) return ASF_Unknown_e
;
492 if( !strcasecmp(content_type
, "application/octet-stream") ||
493 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
494 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
495 !strcasecmp(content_type
, "video/x-ms-wmv") ||
496 !strcasecmp(content_type
, "video/x-ms-asf")) {
498 if( strstr(features
, "broadcast") ) {
499 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
502 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
503 return ASF_Prerecorded_e
;
506 // Ok in a perfect world, web servers should be well configured
507 // so we could used mime type to know the stream type,
508 // but guess what? All of them are not well configured.
509 // So we have to check for an asf header :(, but it works :p
510 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
511 if( asf_header_check( http_hdr
)==0 ) {
512 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
513 return ASF_PlainText_e
;
514 } else if( (!strcasecmp(content_type
, "text/html")) ) {
515 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
516 return ASF_Unknown_e
;
518 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
519 return ASF_Redirector_e
;
522 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
523 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
524 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
525 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
526 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
527 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
528 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ASFRedirector
);
529 return ASF_Redirector_e
;
530 } else if( !strcasecmp(content_type
, "text/plain") ) {
531 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
532 return ASF_PlainText_e
;
534 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
535 return ASF_Unknown_e
;
539 return ASF_Unknown_e
;
542 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
543 HTTP_header_t
*http_hdr
;
545 URL_t
*server_url
= NULL
;
546 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
551 int offset_hi
=0, offset_lo
=0, length
=0;
552 int asf_nb_stream
=0, stream_id
;
555 if( streaming_ctrl
==NULL
) return NULL
;
556 url
= streaming_ctrl
->url
;
557 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
558 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
560 // Common header for all requests.
561 http_hdr
= http_new_header();
562 http_set_field( http_hdr
, "Accept: */*" );
563 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
564 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
566 // Check if we are using a proxy
567 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
568 server_url
= url_new( (url
->file
)+1 );
569 if( server_url
==NULL
) {
570 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_InvalidProxyURL
);
571 http_free( http_hdr
);
574 http_set_uri( http_hdr
, server_url
->url
);
575 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
576 url_free( server_url
);
578 http_set_uri( http_hdr
, url
->file
);
579 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
582 http_set_field( http_hdr
, str
);
583 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
585 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
586 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
587 http_set_field( http_hdr
, str
);
589 switch( asf_http_ctrl
->streaming_type
) {
591 case ASF_Prerecorded_e
:
592 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
594 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
595 if(asf_http_ctrl
->n_audio
> 0) {
596 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
597 stream_id
= asf_http_ctrl
->audio_streams
[i
];
598 if(stream_id
== asf_http_ctrl
->audio_id
) {
605 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
608 if(asf_http_ctrl
->n_video
> 0) {
609 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
610 stream_id
= asf_http_ctrl
->video_streams
[i
];
611 if(stream_id
== asf_http_ctrl
->video_id
) {
618 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
621 http_set_field( http_hdr
, str
);
622 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
623 http_set_field( http_hdr
, str
);
625 case ASF_Redirector_e
:
628 // First request goes here.
631 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamType
);
634 http_set_field( http_hdr
, "Connection: Close" );
635 http_build_request( http_hdr
);
640 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
641 char *content_type
, *pragma
;
642 char features
[64] = "\0";
644 if( http_response_parse(http_hdr
)<0 ) {
645 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse
);
648 switch( http_hdr
->status_code
) {
651 case 401: // Authentication required
652 return ASF_Authenticate_e
;
654 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ServerReturn
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
658 content_type
= http_get_field( http_hdr
, "Content-Type");
659 //printf("Content-Type: [%s]\n", content_type);
661 pragma
= http_get_field( http_hdr
, "Pragma");
662 while( pragma
!=NULL
) {
663 char *comma_ptr
=NULL
;
665 //printf("Pragma: [%s]\n", pragma );
666 // The pragma line can get severals attributes
667 // separeted with a comma ','.
669 if( !strncasecmp( pragma
, "features=", 9) ) {
671 end
= strstr( pragma
, "," );
673 len
= strlen(pragma
);
675 len
= (unsigned int)(end
-pragma
);
677 if(len
> sizeof(features
) - 1) {
678 mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma
,pragma
,len
,sizeof(features
) - 1);
679 len
= sizeof(features
) - 1;
681 strncpy( features
, pragma
, len
);
685 comma_ptr
= strstr( pragma
, "," );
686 if( comma_ptr
!=NULL
) {
687 pragma
= comma_ptr
+1;
688 if( pragma
[0]==' ' ) pragma
++;
690 } while( comma_ptr
!=NULL
);
691 pragma
= http_get_next_field( http_hdr
);
693 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
697 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
698 HTTP_header_t
*http_hdr
=NULL
;
699 URL_t
*url
= stream
->streaming_ctrl
->url
;
700 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
701 char buffer
[BUFFER_SIZE
];
707 asf_http_ctrl
= malloc(sizeof(asf_http_streaming_ctrl_t
));
708 if( asf_http_ctrl
==NULL
) {
709 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
712 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
713 asf_http_ctrl
->request
= 1;
714 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
715 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
716 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
720 if( fd
>0 ) closesocket( fd
);
722 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
723 if( url
->port
==0 ) url
->port
= 8080;
725 if( url
->port
==0 ) url
->port
= 80;
727 fd
= connect2Server( url
->hostname
, url
->port
, 1);
728 if( fd
<0 ) return fd
;
730 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
731 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
732 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
733 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, 0 );
735 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SocketWriteError
,strerror(errno
));
740 http_free( http_hdr
);
741 http_hdr
= http_new_header();
743 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
744 //printf("read: %d\n", i );
749 http_response_append( http_hdr
, buffer
, i
);
750 } while( !http_is_header_entire( http_hdr
) );
751 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
752 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
753 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
755 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
757 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_HeaderParseFailed
);
760 switch( asf_http_ctrl
->streaming_type
) {
762 case ASF_Prerecorded_e
:
763 case ASF_PlainText_e
:
764 if( http_hdr
->body_size
>0 ) {
765 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
769 if( asf_http_ctrl
->request
==1 ) {
770 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
771 // First request, we only got the ASF header.
772 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
773 if(ret
< 0) goto err_out
;
774 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
775 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoStreamFound
);
778 asf_http_ctrl
->request
++;
785 case ASF_Redirector_e
:
786 if( http_hdr
->body_size
>0 ) {
787 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
791 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
794 case ASF_Authenticate_e
:
795 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
796 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
801 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType
);
804 // Check if we got a redirect.
808 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
809 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
810 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
812 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
813 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
814 stream
->streaming_ctrl
->buffering
= 1;
816 stream
->streaming_ctrl
->status
= streaming_playing_e
;
817 stream
->close
= close_s
;
819 http_free( http_hdr
);
830 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
833 stream
->streaming_ctrl
= streaming_ctrl_new();
834 if( stream
->streaming_ctrl
==NULL
) {
837 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
838 url
= url_new(stream
->url
);
839 stream
->streaming_ctrl
->url
= check4proxies(url
);
842 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_MPDEMUX_ASF_InfoStreamASFURL
, stream
->url
);
843 if((!strncmp(stream
->url
, "http", 4)) && (*file_format
!=DEMUXER_TYPE_ASF
&& *file_format
!=DEMUXER_TYPE_UNKNOWN
)) {
844 streaming_ctrl_free(stream
->streaming_ctrl
);
845 stream
->streaming_ctrl
= NULL
;
846 return STREAM_UNSUPPORTED
;
849 if(asf_streaming_start(stream
, file_format
) < 0) {
850 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_StreamingFailed
);
851 streaming_ctrl_free(stream
->streaming_ctrl
);
852 stream
->streaming_ctrl
= NULL
;
853 return STREAM_UNSUPPORTED
;
856 if (*file_format
!= DEMUXER_TYPE_PLAYLIST
)
857 *file_format
= DEMUXER_TYPE_ASF
;
858 stream
->type
= STREAMTYPE_STREAM
;
859 fixup_network_stream_cache(stream
);
863 const stream_info_t stream_info_asf
= {
864 "mms and mms over http streaming",
866 "Bertrand, Reimar Doeffinger, Albeu",
867 "originally based on work by Majormms (is that code still there?)",
869 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL
},
871 0 // Urls are an option string