13 #ifdef HAVE_WINSOCK2_H
19 #include "libmpdemux/asf.h"
22 #include "libmpdemux/demuxer.h"
27 #include "libavutil/intreadwrite.h"
29 #include "libmpdemux/asfguid.h"
31 extern int network_bandwidth
;
33 int asf_mmst_streaming_start( stream_t
*stream
);
34 static int asf_http_streaming_start(stream_t
*stream
, int *demuxer_type
);
36 static int asf_read_wrapper(int fd
, void *buffer
, int len
, streaming_ctrl_t
*stream_ctrl
) {
37 uint8_t *buf
= buffer
;
39 int got
= nop_streaming_read(fd
, buf
, len
, stream_ctrl
);
41 mp_msg(MSGT_NETWORK
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream
);
50 // We can try several protocol for asf streaming
51 // * first the UDP protcol, if there is a firewall, UDP
52 // packets will not come back, so the mmsu will fail.
53 // * Then we can try TCP, but if there is a proxy for
54 // internet connection, the TCP connection will not get
56 // * Then we can try HTTP.
58 // Note: Using WMP sequence MMSU then MMST and then HTTP.
60 static int asf_streaming_start( stream_t
*stream
, int *demuxer_type
) {
61 char *proto
= stream
->streaming_ctrl
->url
->protocol
;
63 int port
= stream
->streaming_ctrl
->url
->port
;
65 // Is protocol mms or mmsu?
66 if (!strcasecmp(proto
, "mmsu") || !strcasecmp(proto
, "mms"))
68 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/UDP...\n");
69 //fd = asf_mmsu_streaming_start( stream );
70 if( fd
>-1 ) return fd
; //mmsu support is not implemented yet - using this code
71 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/UDP failed\n");
72 if( fd
==-2 ) return -1;
75 //Is protocol mms or mmst?
76 if (!strcasecmp(proto
, "mmst") || !strcasecmp(proto
, "mms"))
78 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/TCP...\n");
79 fd
= asf_mmst_streaming_start( stream
);
80 stream
->streaming_ctrl
->url
->port
= port
;
81 if( fd
>-1 ) return fd
;
82 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/TCP failed\n");
83 if( fd
==-2 ) return -1;
86 //Is protocol http, http_proxy, or mms?
87 if (!strcasecmp(proto
, "http_proxy") || !strcasecmp(proto
, "http") ||
88 !strcasecmp(proto
, "mms") || !strcasecmp(proto
, "mmshttp"))
90 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/HTTP...\n");
91 fd
= asf_http_streaming_start( stream
, demuxer_type
);
92 stream
->streaming_ctrl
->url
->port
= port
;
93 if( fd
>-1 ) return fd
;
94 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/HTTP failed\n");
95 if( fd
==-2 ) return -1;
102 static int asf_streaming(ASF_stream_chunck_t
*stream_chunck
, int *drop_packet
) {
104 printf("ASF stream chunck size=%d\n", stream_chunck->size);
105 printf("length: %d\n", length );
106 printf("0x%02X\n", stream_chunck->type );
108 if( drop_packet
!=NULL
) *drop_packet
= 0;
110 if( stream_chunck
->size
<8 ) {
111 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small
, stream_chunck
->size
);
114 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
115 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch
, stream_chunck
->size
, stream_chunck
->size_confirm
);
119 printf(" type: 0x%02X\n", stream_chunck->type );
120 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
121 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
122 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
123 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
125 switch(stream_chunck
->type
) {
126 case ASF_STREAMING_CLEAR
: // $C Clear ASF configuration
127 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Clearing ASF stream configuration!\n");
128 if( drop_packet
!=NULL
) *drop_packet
= 1;
129 return stream_chunck
->size
;
131 case ASF_STREAMING_DATA
: // $D Data follows
132 // printf("=====> Data follows\n");
134 case ASF_STREAMING_END_TRANS
: // $E Transfer complete
135 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Transfer complete\n");
136 if( drop_packet
!=NULL
) *drop_packet
= 1;
137 return stream_chunck
->size
;
139 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
140 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
143 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
145 return stream_chunck
->size
+4;
148 static void close_s(stream_t
*stream
) {
153 static int max_idx(int s_count
, int *s_rates
, int bound
) {
154 int i
, best
= -1, rate
= -1;
155 for (i
= 0; i
< s_count
; i
++) {
156 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
164 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
165 ASF_stream_chunck_t chunk
;
166 asf_http_streaming_ctrl_t
* asf_ctrl
= streaming_ctrl
->data
;
167 char* buffer
=NULL
, *chunk_buffer
=NULL
;
168 int i
,r
,size
,pos
= 0;
171 int chunk_size2read
= 0;
172 int bw
= streaming_ctrl
->bandwidth
;
173 int *v_rates
= NULL
, *a_rates
= NULL
;
174 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
176 if(asf_ctrl
== NULL
) return -1;
178 // The ASF header can be in several network chunks. For example if the content description
179 // is big, the ASF header will be split in 2 network chunk.
180 // So we need to retrieve all the chunk before starting to parse the header.
182 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
184 // Endian handling of the stream chunk
185 le2me_ASF_stream_chunck_t(&chunk
);
186 size
= asf_streaming( &chunk
, &r
) - sizeof(ASF_stream_chunck_t
);
187 if(r
) mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_WarnDropHeader
);
189 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
192 if (chunk
.type
!= ASF_STREAMING_HEADER
) {
193 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk
);
197 // audit: do not overflow buffer_size
198 if (size
> SIZE_MAX
- buffer_size
) return -1;
199 buffer
= malloc(size
+buffer_size
);
201 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MPDEMUX_ASF_BufferMallocFailed
,size
+buffer_size
);
204 if( chunk_buffer
!=NULL
) {
205 memcpy( buffer
, chunk_buffer
, buffer_size
);
206 free( chunk_buffer
);
208 chunk_buffer
= buffer
;
209 buffer
+= buffer_size
;
212 if (asf_read_wrapper(fd
, buffer
, size
, streaming_ctrl
) <= 0)
215 if( chunk_size2read
==0 ) {
216 ASF_header_t
*asfh
= (ASF_header_t
*)buffer
;
217 if(size
< (int)sizeof(ASF_header_t
)) {
218 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunk2Small
);
220 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
221 chunk_size2read
= AV_RL64(&asfh
->objh
.size
);
222 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
224 } while( buffer_size
<chunk_size2read
);
225 buffer
= chunk_buffer
;
228 start
= sizeof(ASF_header_t
);
230 pos
= find_asf_guid(buffer
, asf_file_header_guid
, start
, size
);
232 ASF_file_header_t
*fileh
= (ASF_file_header_t
*) &buffer
[pos
];
233 pos
+= sizeof(ASF_file_header_t
);
234 if (pos
> size
) goto len_err_out
;
236 if(fileh.packetsize != fileh.packetsize2) {
237 printf("Error packetsize check don't match\n");
241 asf_ctrl
->packet_size
= AV_RL32(&fileh
->max_packet_size
);
243 // preroll: time in ms to bufferize before playing
244 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
248 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
250 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
251 pos
+= sizeof(ASF_stream_header_t
);
252 if (pos
> size
) goto len_err_out
;
253 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
254 case 0xF8699E40 : // audio stream
255 if(asf_ctrl
->audio_streams
== NULL
){
256 asf_ctrl
->audio_streams
= malloc(sizeof(int));
257 asf_ctrl
->n_audio
= 1;
260 asf_ctrl
->audio_streams
= realloc(asf_ctrl
->audio_streams
,
261 asf_ctrl
->n_audio
*sizeof(int));
263 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = AV_RL16(&streamh
->stream_no
);
265 case 0xBC19EFC0 : // video stream
266 if(asf_ctrl
->video_streams
== NULL
){
267 asf_ctrl
->video_streams
= malloc(sizeof(int));
268 asf_ctrl
->n_video
= 1;
271 asf_ctrl
->video_streams
= realloc(asf_ctrl
->video_streams
,
272 asf_ctrl
->n_video
*sizeof(int));
274 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = AV_RL16(&streamh
->stream_no
);
279 // always allocate to avoid lots of ifs later
280 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
281 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
283 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
285 // stream bitrate properties object
287 char *ptr
= &buffer
[pos
];
288 char *end
= &buffer
[size
];
290 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
291 if (ptr
+ 2 > end
) goto len_err_out
;
292 stream_count
= AV_RL16(ptr
);
294 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
295 stream_count
, stream_count
);
296 for( i
=0 ; i
<stream_count
; i
++ ) {
300 if (ptr
+ 6 > end
) goto len_err_out
;
305 mp_msg(MSGT_NETWORK
, MSGL_V
,
306 " stream id=[0x%x][%u]\n", id
, id
);
307 mp_msg(MSGT_NETWORK
, MSGL_V
,
308 " max bitrate=[0x%x][%u]\n", rate
, rate
);
309 for (j
= 0; j
< asf_ctrl
->n_video
; j
++) {
310 if (id
== asf_ctrl
->video_streams
[j
]) {
311 mp_msg(MSGT_NETWORK
, MSGL_V
, " is video stream\n");
316 for (j
= 0; j
< asf_ctrl
->n_audio
; j
++) {
317 if (id
== asf_ctrl
->audio_streams
[j
]) {
318 mp_msg(MSGT_NETWORK
, MSGL_V
, " is audio stream\n");
327 // automatic stream selection based on bandwidth
328 if (bw
== 0) bw
= INT_MAX
;
329 mp_msg(MSGT_NETWORK
, MSGL_V
, "Max bandwidth set to %d\n", bw
);
331 if (asf_ctrl
->n_audio
) {
332 // find lowest-bitrate audio stream
335 for (i
= 0; i
< asf_ctrl
->n_audio
; i
++) {
336 if (a_rates
[i
] < a_rate
) {
341 if (max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
) < 0) {
342 // both audio and video are not possible, try video only next
347 // find best video stream
348 v_idx
= max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
);
350 v_rate
= v_rates
[v_idx
];
352 // find best audio stream
353 a_idx
= max_idx(asf_ctrl
->n_audio
, a_rates
, bw
- v_rate
);
358 if (a_idx
< 0 && v_idx
< 0) {
359 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay
);
363 if (*streaming_ctrl
->audio_id_ptr
> 0)
364 // a audio stream was forced
365 asf_ctrl
->audio_id
= *streaming_ctrl
->audio_id_ptr
;
367 asf_ctrl
->audio_id
= asf_ctrl
->audio_streams
[a_idx
];
368 else if (asf_ctrl
->n_audio
) {
369 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio
);
370 *streaming_ctrl
->audio_id_ptr
= -2;
373 if (*streaming_ctrl
->video_id_ptr
> 0)
374 // a video stream was forced
375 asf_ctrl
->video_id
= *streaming_ctrl
->video_id_ptr
;
377 asf_ctrl
->video_id
= asf_ctrl
->video_streams
[v_idx
];
378 else if (asf_ctrl
->n_video
) {
379 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo
);
380 *streaming_ctrl
->video_id_ptr
= -2;
386 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_InvalidLenInHeader
);
387 if (buffer
) free(buffer
);
388 if (v_rates
) free(v_rates
);
389 if (a_rates
) free(a_rates
);
393 static int asf_http_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*streaming_ctrl
) {
394 static ASF_stream_chunck_t chunk
;
395 int read
,chunk_size
= 0;
396 static int rest
= 0, drop_chunk
= 0, waiting
= 0;
397 asf_http_streaming_ctrl_t
*asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
400 if (rest
== 0 && waiting
== 0) {
401 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
404 // Endian handling of the stream chunk
405 le2me_ASF_stream_chunck_t(&chunk
);
406 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
408 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
411 chunk_size
-= sizeof(ASF_stream_chunck_t
);
413 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
414 if (asf_http_ctrl
->packet_size
< chunk_size
) {
415 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket
);
418 waiting
= asf_http_ctrl
->packet_size
;
420 waiting
= chunk_size
;
429 if ( waiting
>= chunk_size
) {
430 if (chunk_size
> size
){
431 rest
= chunk_size
- size
;
434 if (asf_read_wrapper(fd
, buffer
, chunk_size
, streaming_ctrl
) <= 0)
438 if (drop_chunk
) continue;
440 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
441 int s
= FFMIN(waiting
,size
-read
);
442 memset(buffer
+read
,0,s
);
452 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
454 // to shut up gcc warning
460 static int asf_header_check( HTTP_header_t
*http_hdr
) {
461 ASF_obj_header_t
*objh
;
462 if( http_hdr
==NULL
) return -1;
463 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
465 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
466 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
470 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
471 if( content_type
==NULL
) return ASF_Unknown_e
;
472 if( !strcasecmp(content_type
, "application/octet-stream") ||
473 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
474 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
475 !strcasecmp(content_type
, "video/x-ms-asf")) {
477 if( strstr(features
, "broadcast") ) {
478 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
481 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
482 return ASF_Prerecorded_e
;
485 // Ok in a perfect world, web servers should be well configured
486 // so we could used mime type to know the stream type,
487 // but guess what? All of them are not well configured.
488 // So we have to check for an asf header :(, but it works :p
489 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
490 if( asf_header_check( http_hdr
)==0 ) {
491 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
492 return ASF_PlainText_e
;
493 } else if( (!strcasecmp(content_type
, "text/html")) ) {
494 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
495 return ASF_Unknown_e
;
497 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
498 return ASF_Redirector_e
;
501 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
502 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
503 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
504 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
505 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
506 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
507 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ASFRedirector
);
508 return ASF_Redirector_e
;
509 } else if( !strcasecmp(content_type
, "text/plain") ) {
510 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
511 return ASF_PlainText_e
;
513 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
514 return ASF_Unknown_e
;
518 return ASF_Unknown_e
;
521 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
522 HTTP_header_t
*http_hdr
;
524 URL_t
*server_url
= NULL
;
525 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
530 int offset_hi
=0, offset_lo
=0, length
=0;
531 int asf_nb_stream
=0, stream_id
;
534 if( streaming_ctrl
==NULL
) return NULL
;
535 url
= streaming_ctrl
->url
;
536 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
537 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
539 // Common header for all requests.
540 http_hdr
= http_new_header();
541 http_set_field( http_hdr
, "Accept: */*" );
542 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
543 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
545 // Check if we are using a proxy
546 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
547 server_url
= url_new( (url
->file
)+1 );
548 if( server_url
==NULL
) {
549 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_InvalidProxyURL
);
550 http_free( http_hdr
);
553 http_set_uri( http_hdr
, server_url
->url
);
554 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
555 url_free( server_url
);
557 http_set_uri( http_hdr
, url
->file
);
558 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
561 http_set_field( http_hdr
, str
);
562 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
564 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
565 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
566 http_set_field( http_hdr
, str
);
568 switch( asf_http_ctrl
->streaming_type
) {
570 case ASF_Prerecorded_e
:
571 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
573 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
574 if(asf_http_ctrl
->n_audio
> 0) {
575 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
576 stream_id
= asf_http_ctrl
->audio_streams
[i
];
577 if(stream_id
== asf_http_ctrl
->audio_id
) {
584 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
587 if(asf_http_ctrl
->n_video
> 0) {
588 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
589 stream_id
= asf_http_ctrl
->video_streams
[i
];
590 if(stream_id
== asf_http_ctrl
->video_id
) {
597 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
600 http_set_field( http_hdr
, str
);
601 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
602 http_set_field( http_hdr
, str
);
604 case ASF_Redirector_e
:
607 // First request goes here.
610 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamType
);
613 http_set_field( http_hdr
, "Connection: Close" );
614 http_build_request( http_hdr
);
619 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
620 char *content_type
, *pragma
;
621 char features
[64] = "\0";
623 if( http_response_parse(http_hdr
)<0 ) {
624 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse
);
627 switch( http_hdr
->status_code
) {
630 case 401: // Authentication required
631 return ASF_Authenticate_e
;
633 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ServerReturn
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
637 content_type
= http_get_field( http_hdr
, "Content-Type");
638 //printf("Content-Type: [%s]\n", content_type);
640 pragma
= http_get_field( http_hdr
, "Pragma");
641 while( pragma
!=NULL
) {
642 char *comma_ptr
=NULL
;
644 //printf("Pragma: [%s]\n", pragma );
645 // The pragma line can get severals attributes
646 // separeted with a comma ','.
648 if( !strncasecmp( pragma
, "features=", 9) ) {
650 end
= strstr( pragma
, "," );
652 len
= strlen(pragma
);
654 len
= (unsigned int)(end
-pragma
);
656 if(len
> sizeof(features
) - 1) {
657 mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma
,pragma
,len
,sizeof(features
) - 1);
658 len
= sizeof(features
) - 1;
660 strncpy( features
, pragma
, len
);
664 comma_ptr
= strstr( pragma
, "," );
665 if( comma_ptr
!=NULL
) {
666 pragma
= comma_ptr
+1;
667 if( pragma
[0]==' ' ) pragma
++;
669 } while( comma_ptr
!=NULL
);
670 pragma
= http_get_next_field( http_hdr
);
672 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
676 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
677 HTTP_header_t
*http_hdr
=NULL
;
678 URL_t
*url
= stream
->streaming_ctrl
->url
;
679 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
680 char buffer
[BUFFER_SIZE
];
686 asf_http_ctrl
= malloc(sizeof(asf_http_streaming_ctrl_t
));
687 if( asf_http_ctrl
==NULL
) {
688 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
691 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
692 asf_http_ctrl
->request
= 1;
693 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
694 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
695 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
699 if( fd
>0 ) closesocket( fd
);
701 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
702 if( url
->port
==0 ) url
->port
= 8080;
704 if( url
->port
==0 ) url
->port
= 80;
706 fd
= connect2Server( url
->hostname
, url
->port
, 1);
707 if( fd
<0 ) return fd
;
709 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
710 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
711 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
712 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, 0 );
714 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SocketWriteError
,strerror(errno
));
719 http_free( http_hdr
);
720 http_hdr
= http_new_header();
722 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
723 //printf("read: %d\n", i );
728 http_response_append( http_hdr
, buffer
, i
);
729 } while( !http_is_header_entire( http_hdr
) );
730 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
731 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
732 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
734 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
736 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_HeaderParseFailed
);
739 switch( asf_http_ctrl
->streaming_type
) {
741 case ASF_Prerecorded_e
:
742 case ASF_PlainText_e
:
743 if( http_hdr
->body_size
>0 ) {
744 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
748 if( asf_http_ctrl
->request
==1 ) {
749 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
750 // First request, we only got the ASF header.
751 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
752 if(ret
< 0) goto err_out
;
753 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
754 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoStreamFound
);
757 asf_http_ctrl
->request
++;
764 case ASF_Redirector_e
:
765 if( http_hdr
->body_size
>0 ) {
766 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
770 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
773 case ASF_Authenticate_e
:
774 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
775 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
780 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType
);
783 // Check if we got a redirect.
787 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
788 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
789 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
791 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
792 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
793 stream
->streaming_ctrl
->buffering
= 1;
795 stream
->streaming_ctrl
->status
= streaming_playing_e
;
796 stream
->close
= close_s
;
798 http_free( http_hdr
);
809 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
812 stream
->streaming_ctrl
= streaming_ctrl_new();
813 if( stream
->streaming_ctrl
==NULL
) {
816 stream
->streaming_ctrl
->audio_id_ptr
= &stream
->opts
->audio_id
;
817 stream
->streaming_ctrl
->video_id_ptr
= &stream
->opts
->video_id
;
818 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
819 url
= url_new(stream
->url
);
820 stream
->streaming_ctrl
->url
= check4proxies(url
);
823 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_MPDEMUX_ASF_InfoStreamASFURL
, stream
->url
);
824 if((!strncmp(stream
->url
, "http", 4)) && (*file_format
!=DEMUXER_TYPE_ASF
&& *file_format
!=DEMUXER_TYPE_UNKNOWN
)) {
825 streaming_ctrl_free(stream
->streaming_ctrl
);
826 stream
->streaming_ctrl
= NULL
;
827 return STREAM_UNSUPPORTED
;
830 if(asf_streaming_start(stream
, file_format
) < 0) {
831 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_StreamingFailed
);
832 streaming_ctrl_free(stream
->streaming_ctrl
);
833 stream
->streaming_ctrl
= NULL
;
834 return STREAM_UNSUPPORTED
;
837 if (*file_format
!= DEMUXER_TYPE_PLAYLIST
)
838 *file_format
= DEMUXER_TYPE_ASF
;
839 stream
->type
= STREAMTYPE_STREAM
;
840 fixup_network_stream_cache(stream
);
844 const stream_info_t stream_info_asf
= {
845 "mms and mms over http streaming",
847 "Bertrand, Reimar Doeffinger, Albeu",
848 "originally based on work by Majormms (is that code still there?)",
850 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL
},
852 0 // Urls are an option string