13 #define closesocket close
20 #include "libmpdemux/asf.h"
23 #include "libmpdemux/demuxer.h"
29 #define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid))
31 #define ASF_LOAD_GUID_PREFIX(guid) \
32 ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0])
35 extern int network_bandwidth
;
37 int asf_mmst_streaming_start( stream_t
*stream
);
38 static int asf_http_streaming_start(stream_t
*stream
, int *demuxer_type
);
40 // We can try several protocol for asf streaming
41 // * first the UDP protcol, if there is a firewall, UDP
42 // packets will not come back, so the mmsu will fail.
43 // * Then we can try TCP, but if there is a proxy for
44 // internet connection, the TCP connection will not get
46 // * Then we can try HTTP.
48 // Note: Using WMP sequence MMSU then MMST and then HTTP.
50 static int asf_streaming_start( stream_t
*stream
, int *demuxer_type
) {
51 char *proto
= stream
->streaming_ctrl
->url
->protocol
;
53 int port
= stream
->streaming_ctrl
->url
->port
;
55 // Is protocol mms or mmsu?
56 if (!strcasecmp(proto
, "mmsu") || !strcasecmp(proto
, "mms"))
58 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/UDP...\n");
59 //fd = asf_mmsu_streaming_start( stream );
60 if( fd
>-1 ) return fd
; //mmsu support is not implemented yet - using this code
61 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/UDP failed\n");
62 if( fd
==-2 ) return -1;
65 //Is protocol mms or mmst?
66 if (!strcasecmp(proto
, "mmst") || !strcasecmp(proto
, "mms"))
68 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/TCP...\n");
69 fd
= asf_mmst_streaming_start( stream
);
70 stream
->streaming_ctrl
->url
->port
= port
;
71 if( fd
>-1 ) return fd
;
72 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/TCP failed\n");
73 if( fd
==-2 ) return -1;
76 //Is protocol http, http_proxy, or mms?
77 if (!strcasecmp(proto
, "http_proxy") || !strcasecmp(proto
, "http") ||
78 !strcasecmp(proto
, "mms") || !strcasecmp(proto
, "mmshttp"))
80 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/HTTP...\n");
81 fd
= asf_http_streaming_start( stream
, demuxer_type
);
82 stream
->streaming_ctrl
->url
->port
= port
;
83 if( fd
>-1 ) return fd
;
84 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/HTTP failed\n");
85 if( fd
==-2 ) return -1;
92 static int asf_streaming(ASF_stream_chunck_t
*stream_chunck
, int *drop_packet
) {
94 printf("ASF stream chunck size=%d\n", stream_chunck->size);
95 printf("length: %d\n", length );
96 printf("0x%02X\n", stream_chunck->type );
98 if( drop_packet
!=NULL
) *drop_packet
= 0;
100 if( stream_chunck
->size
<8 ) {
101 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small
, stream_chunck
->size
);
104 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
105 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch
, stream_chunck
->size
, stream_chunck
->size_confirm
);
109 printf(" type: 0x%02X\n", stream_chunck->type );
110 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
111 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
112 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
113 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
115 switch(stream_chunck
->type
) {
116 case ASF_STREAMING_CLEAR
: // $C Clear ASF configuration
117 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Clearing ASF stream configuration!\n");
118 if( drop_packet
!=NULL
) *drop_packet
= 1;
119 return stream_chunck
->size
;
121 case ASF_STREAMING_DATA
: // $D Data follows
122 // printf("=====> Data follows\n");
124 case ASF_STREAMING_END_TRANS
: // $E Transfer complete
125 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Transfer complete\n");
126 if( drop_packet
!=NULL
) *drop_packet
= 1;
127 return stream_chunck
->size
;
129 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
130 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
133 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
135 return stream_chunck
->size
+4;
138 extern int find_asf_guid(char *buf
, const char *guid
, int cur_pos
, int buf_len
);
139 extern const char asf_file_header_guid
[];
140 extern const char asf_stream_header_guid
[];
141 extern const char asf_stream_group_guid
[];
145 static void close_s(stream_t
*stream
) {
150 static int max_idx(int s_count
, int *s_rates
, int bound
) {
151 int i
, best
= -1, rate
= -1;
152 for (i
= 0; i
< s_count
; i
++) {
153 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
161 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
163 ASF_stream_chunck_t chunk
;
164 asf_http_streaming_ctrl_t
* asf_ctrl
= (asf_http_streaming_ctrl_t
*) streaming_ctrl
->data
;
165 char* buffer
=NULL
, *chunk_buffer
=NULL
;
166 int i
,r
,size
,pos
= 0;
169 int chunk_size2read
= 0;
170 int bw
= streaming_ctrl
->bandwidth
;
171 int *v_rates
= NULL
, *a_rates
= NULL
;
172 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
174 if(asf_ctrl
== NULL
) return -1;
176 // The ASF header can be in several network chunks. For example if the content description
177 // is big, the ASF header will be split in 2 network chunk.
178 // So we need to retrieve all the chunk before starting to parse the header.
180 for( r
=0; r
< (int)sizeof(ASF_stream_chunck_t
) ; ) {
181 i
= nop_streaming_read(fd
,((char*)&chunk
)+r
,sizeof(ASF_stream_chunck_t
) - r
,streaming_ctrl
);
182 if(i
<= 0) return -1;
185 // Endian handling of the stream chunk
186 le2me_ASF_stream_chunck_t(&chunk
);
187 size
= asf_streaming( &chunk
, &r
) - sizeof(ASF_stream_chunck_t
);
188 if(r
) mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_WarnDropHeader
);
190 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
193 if (chunk
.type
!= ASF_STREAMING_HEADER
) {
194 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk
);
198 // audit: do not overflow buffer_size
199 if (size
> SIZE_MAX
- buffer_size
) return -1;
200 buffer
= (char*) malloc(size
+buffer_size
);
202 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MPDEMUX_ASF_BufferMallocFailed
,size
+buffer_size
);
205 if( chunk_buffer
!=NULL
) {
206 memcpy( buffer
, chunk_buffer
, buffer_size
);
207 free( chunk_buffer
);
209 chunk_buffer
= buffer
;
210 buffer
+= buffer_size
;
213 for(r
= 0; r
< size
;) {
214 i
= nop_streaming_read(fd
,buffer
+r
,size
-r
,streaming_ctrl
);
216 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream
);
222 if( chunk_size2read
==0 ) {
223 if(size
< (int)sizeof(asfh
)) {
224 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunk2Small
);
226 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
227 memcpy(&asfh
,buffer
,sizeof(asfh
));
228 le2me_ASF_header_t(&asfh
);
229 chunk_size2read
= asfh
.objh
.size
;
230 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
232 } while( buffer_size
<chunk_size2read
);
233 buffer
= chunk_buffer
;
237 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrSubChunkNumberInvalid
);
241 start
= sizeof(asfh
);
243 pos
= find_asf_guid(buffer
, asf_file_header_guid
, start
, size
);
245 ASF_file_header_t
*fileh
= (ASF_file_header_t
*) &buffer
[pos
];
246 pos
+= sizeof(ASF_file_header_t
);
247 if (pos
> size
) goto len_err_out
;
248 le2me_ASF_file_header_t(fileh
);
250 if(fileh.packetsize != fileh.packetsize2) {
251 printf("Error packetsize check don't match\n");
255 asf_ctrl
->packet_size
= fileh
->max_packet_size
;
257 // preroll: time in ms to bufferize before playing
258 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
262 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
264 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
265 pos
+= sizeof(ASF_stream_header_t
);
266 if (pos
> size
) goto len_err_out
;
267 le2me_ASF_stream_header_t(streamh
);
268 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
269 case 0xF8699E40 : // audio stream
270 if(asf_ctrl
->audio_streams
== NULL
){
271 asf_ctrl
->audio_streams
= malloc(sizeof(int));
272 asf_ctrl
->n_audio
= 1;
275 asf_ctrl
->audio_streams
= (int*)realloc(asf_ctrl
->audio_streams
,
276 asf_ctrl
->n_audio
*sizeof(int));
278 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = streamh
->stream_no
;
280 case 0xBC19EFC0 : // video stream
281 if(asf_ctrl
->video_streams
== NULL
){
282 asf_ctrl
->video_streams
= malloc(sizeof(int));
283 asf_ctrl
->n_video
= 1;
286 asf_ctrl
->video_streams
= (int*)realloc(asf_ctrl
->video_streams
,
287 asf_ctrl
->n_video
*sizeof(int));
289 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = streamh
->stream_no
;
294 // always allocate to avoid lots of ifs later
295 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
296 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
298 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
300 // stream bitrate properties object
302 char *ptr
= &buffer
[pos
];
304 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
305 stream_count
= le2me_16(*(uint16_t*)ptr
);
306 ptr
+= sizeof(uint16_t);
307 if (ptr
> &buffer
[size
]) goto len_err_out
;
308 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
309 stream_count
, stream_count
);
310 for( i
=0 ; i
<stream_count
; i
++ ) {
314 id
= le2me_16(*(uint16_t*)ptr
);
315 ptr
+= sizeof(uint16_t);
316 if (ptr
> &buffer
[size
]) goto len_err_out
;
317 memcpy(&rate
, ptr
, sizeof(uint32_t));// workaround unaligment bug on sparc
318 ptr
+= sizeof(uint32_t);
319 if (ptr
> &buffer
[size
]) goto len_err_out
;
320 rate
= le2me_32(rate
);
321 mp_msg(MSGT_NETWORK
, MSGL_V
,
322 " stream id=[0x%x][%u]\n", id
, id
);
323 mp_msg(MSGT_NETWORK
, MSGL_V
,
324 " max bitrate=[0x%x][%u]\n", rate
, rate
);
325 for (j
= 0; j
< asf_ctrl
->n_video
; j
++) {
326 if (id
== asf_ctrl
->video_streams
[j
]) {
327 mp_msg(MSGT_NETWORK
, MSGL_V
, " is video stream\n");
332 for (j
= 0; j
< asf_ctrl
->n_audio
; j
++) {
333 if (id
== asf_ctrl
->audio_streams
[j
]) {
334 mp_msg(MSGT_NETWORK
, MSGL_V
, " is audio stream\n");
343 // automatic stream selection based on bandwidth
344 if (bw
== 0) bw
= INT_MAX
;
345 mp_msg(MSGT_NETWORK
, MSGL_V
, "Max bandwidth set to %d\n", bw
);
347 if (asf_ctrl
->n_audio
) {
348 // find lowest-bitrate audio stream
351 for (i
= 0; i
< asf_ctrl
->n_audio
; i
++) {
352 if (a_rates
[i
] < a_rate
) {
357 if (max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
) < 0) {
358 // both audio and video are not possible, try video only next
363 // find best video stream
364 v_idx
= max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
);
366 v_rate
= v_rates
[v_idx
];
368 // find best audio stream
369 a_idx
= max_idx(asf_ctrl
->n_audio
, a_rates
, bw
- v_rate
);
374 if (a_idx
< 0 && v_idx
< 0) {
375 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay
);
380 // a audio stream was forced
381 asf_ctrl
->audio_id
= audio_id
;
383 asf_ctrl
->audio_id
= asf_ctrl
->audio_streams
[a_idx
];
384 else if (asf_ctrl
->n_audio
) {
385 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio
);
390 // a video stream was forced
391 asf_ctrl
->video_id
= video_id
;
393 asf_ctrl
->video_id
= asf_ctrl
->video_streams
[v_idx
];
394 else if (asf_ctrl
->n_video
) {
395 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo
);
402 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_InvalidLenInHeader
);
403 if (buffer
) free(buffer
);
404 if (v_rates
) free(v_rates
);
405 if (a_rates
) free(a_rates
);
409 static int asf_http_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*streaming_ctrl
) {
410 static ASF_stream_chunck_t chunk
;
411 int read
,chunk_size
= 0;
412 static int rest
= 0, drop_chunk
= 0, waiting
= 0;
413 asf_http_streaming_ctrl_t
*asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
416 if (rest
== 0 && waiting
== 0) {
418 while(read
< (int)sizeof(ASF_stream_chunck_t
)){
419 int r
= nop_streaming_read( fd
, ((char*)&chunk
) + read
,
420 sizeof(ASF_stream_chunck_t
)-read
,
424 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrReadingChunkHeader
);
430 // Endian handling of the stream chunk
431 le2me_ASF_stream_chunck_t(&chunk
);
432 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
434 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
437 chunk_size
-= sizeof(ASF_stream_chunck_t
);
439 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
440 if (asf_http_ctrl
->packet_size
< chunk_size
) {
441 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket
);
444 waiting
= asf_http_ctrl
->packet_size
;
446 waiting
= chunk_size
;
455 if ( waiting
>= chunk_size
) {
456 if (chunk_size
> size
){
457 rest
= chunk_size
- size
;
460 while(read
< chunk_size
) {
461 int got
= nop_streaming_read( fd
,buffer
+read
,chunk_size
-read
,streaming_ctrl
);
464 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrReadingChunk
);
470 if (drop_chunk
) continue;
472 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
473 int s
= FFMIN(waiting
,size
-read
);
474 memset(buffer
+read
,0,s
);
484 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
486 // to shut up gcc warning
492 static int asf_header_check( HTTP_header_t
*http_hdr
) {
493 ASF_obj_header_t
*objh
;
494 if( http_hdr
==NULL
) return -1;
495 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
497 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
498 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
502 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
503 if( content_type
==NULL
) return ASF_Unknown_e
;
504 if( !strcasecmp(content_type
, "application/octet-stream") ||
505 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
506 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
507 !strcasecmp(content_type
, "video/x-ms-asf")) {
509 if( strstr(features
, "broadcast") ) {
510 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
513 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
514 return ASF_Prerecorded_e
;
517 // Ok in a perfect world, web servers should be well configured
518 // so we could used mime type to know the stream type,
519 // but guess what? All of them are not well configured.
520 // So we have to check for an asf header :(, but it works :p
521 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
522 if( asf_header_check( http_hdr
)==0 ) {
523 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
524 return ASF_PlainText_e
;
525 } else if( (!strcasecmp(content_type
, "text/html")) ) {
526 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
527 return ASF_Unknown_e
;
529 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
530 return ASF_Redirector_e
;
533 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
534 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
535 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
536 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
537 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
538 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
539 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ASFRedirector
);
540 return ASF_Redirector_e
;
541 } else if( !strcasecmp(content_type
, "text/plain") ) {
542 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
543 return ASF_PlainText_e
;
545 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
546 return ASF_Unknown_e
;
550 return ASF_Unknown_e
;
553 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
554 HTTP_header_t
*http_hdr
;
556 URL_t
*server_url
= NULL
;
557 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
562 int offset_hi
=0, offset_lo
=0, length
=0;
563 int asf_nb_stream
=0, stream_id
;
566 if( streaming_ctrl
==NULL
) return NULL
;
567 url
= streaming_ctrl
->url
;
568 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
569 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
571 // Common header for all requests.
572 http_hdr
= http_new_header();
573 http_set_field( http_hdr
, "Accept: */*" );
574 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
575 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
577 // Check if we are using a proxy
578 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
579 server_url
= url_new( (url
->file
)+1 );
580 if( server_url
==NULL
) {
581 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_InvalidProxyURL
);
582 http_free( http_hdr
);
585 http_set_uri( http_hdr
, server_url
->url
);
586 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
587 url_free( server_url
);
589 http_set_uri( http_hdr
, url
->file
);
590 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
593 http_set_field( http_hdr
, str
);
594 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
596 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
597 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
598 http_set_field( http_hdr
, str
);
600 switch( asf_http_ctrl
->streaming_type
) {
602 case ASF_Prerecorded_e
:
603 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
605 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
606 if(asf_http_ctrl
->n_audio
> 0) {
607 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
608 stream_id
= asf_http_ctrl
->audio_streams
[i
];
609 if(stream_id
== asf_http_ctrl
->audio_id
) {
616 ptr
+= sprintf(ptr
, "ffff:%d:%d ", stream_id
, enable
);
619 if(asf_http_ctrl
->n_video
> 0) {
620 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
621 stream_id
= asf_http_ctrl
->video_streams
[i
];
622 if(stream_id
== asf_http_ctrl
->video_id
) {
629 ptr
+= sprintf(ptr
, "ffff:%d:%d ", stream_id
, enable
);
632 http_set_field( http_hdr
, str
);
633 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
634 http_set_field( http_hdr
, str
);
636 case ASF_Redirector_e
:
639 // First request goes here.
642 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamType
);
645 http_set_field( http_hdr
, "Connection: Close" );
646 http_build_request( http_hdr
);
651 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
652 char *content_type
, *pragma
;
653 char features
[64] = "\0";
655 if( http_response_parse(http_hdr
)<0 ) {
656 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse
);
659 switch( http_hdr
->status_code
) {
662 case 401: // Authentication required
663 return ASF_Authenticate_e
;
665 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ServerReturn
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
669 content_type
= http_get_field( http_hdr
, "Content-Type");
670 //printf("Content-Type: [%s]\n", content_type);
672 pragma
= http_get_field( http_hdr
, "Pragma");
673 while( pragma
!=NULL
) {
674 char *comma_ptr
=NULL
;
676 //printf("Pragma: [%s]\n", pragma );
677 // The pragma line can get severals attributes
678 // separeted with a comma ','.
680 if( !strncasecmp( pragma
, "features=", 9) ) {
682 end
= strstr( pragma
, "," );
684 len
= strlen(pragma
);
686 len
= (unsigned int)(end
-pragma
);
688 if(len
> sizeof(features
) - 1) {
689 mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma
,pragma
,len
,sizeof(features
) - 1);
690 len
= sizeof(features
) - 1;
692 strncpy( features
, pragma
, len
);
696 comma_ptr
= strstr( pragma
, "," );
697 if( comma_ptr
!=NULL
) {
698 pragma
= comma_ptr
+1;
699 if( pragma
[0]==' ' ) pragma
++;
701 } while( comma_ptr
!=NULL
);
702 pragma
= http_get_next_field( http_hdr
);
704 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
708 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
709 HTTP_header_t
*http_hdr
=NULL
;
710 URL_t
*url
= stream
->streaming_ctrl
->url
;
711 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
712 char buffer
[BUFFER_SIZE
];
718 asf_http_ctrl
= malloc(sizeof(asf_http_streaming_ctrl_t
));
719 if( asf_http_ctrl
==NULL
) {
720 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
723 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
724 asf_http_ctrl
->request
= 1;
725 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
726 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
727 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
731 if( fd
>0 ) closesocket( fd
);
733 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
734 if( url
->port
==0 ) url
->port
= 8080;
736 if( url
->port
==0 ) url
->port
= 80;
738 fd
= connect2Server( url
->hostname
, url
->port
, 1);
739 if( fd
<0 ) return fd
;
741 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
742 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
743 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
744 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, 0 );
746 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SocketWriteError
,strerror(errno
));
751 http_free( http_hdr
);
752 http_hdr
= http_new_header();
754 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
755 //printf("read: %d\n", i );
760 http_response_append( http_hdr
, buffer
, i
);
761 } while( !http_is_header_entire( http_hdr
) );
762 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
763 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
764 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
766 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
768 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_HeaderParseFailed
);
771 switch( asf_http_ctrl
->streaming_type
) {
773 case ASF_Prerecorded_e
:
774 case ASF_PlainText_e
:
775 if( http_hdr
->body_size
>0 ) {
776 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
780 if( asf_http_ctrl
->request
==1 ) {
781 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
782 // First request, we only got the ASF header.
783 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
784 if(ret
< 0) goto err_out
;
785 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
786 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoStreamFound
);
789 asf_http_ctrl
->request
++;
796 case ASF_Redirector_e
:
797 if( http_hdr
->body_size
>0 ) {
798 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
802 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
805 case ASF_Authenticate_e
:
806 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
807 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
812 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType
);
815 // Check if we got a redirect.
819 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
820 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
821 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
823 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
824 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
825 stream
->streaming_ctrl
->buffering
= 1;
827 stream
->streaming_ctrl
->status
= streaming_playing_e
;
828 stream
->close
= close_s
;
830 http_free( http_hdr
);
841 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
844 stream
->streaming_ctrl
= streaming_ctrl_new();
845 if( stream
->streaming_ctrl
==NULL
) {
848 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
849 url
= url_new(stream
->url
);
850 stream
->streaming_ctrl
->url
= check4proxies(url
);
853 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_MPDEMUX_ASF_InfoStreamASFURL
, stream
->url
);
854 if((!strncmp(stream
->url
, "http", 4)) && (*file_format
!=DEMUXER_TYPE_ASF
&& *file_format
!=DEMUXER_TYPE_UNKNOWN
)) {
855 streaming_ctrl_free(stream
->streaming_ctrl
);
856 stream
->streaming_ctrl
= NULL
;
857 return STREAM_UNSUPPORTED
;
860 if(asf_streaming_start(stream
, file_format
) < 0) {
861 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_StreamingFailed
);
862 streaming_ctrl_free(stream
->streaming_ctrl
);
863 stream
->streaming_ctrl
= NULL
;
864 return STREAM_UNSUPPORTED
;
867 if (*file_format
!= DEMUXER_TYPE_PLAYLIST
)
868 *file_format
= DEMUXER_TYPE_ASF
;
869 stream
->type
= STREAMTYPE_STREAM
;
870 fixup_network_stream_cache(stream
);
874 const stream_info_t stream_info_asf
= {
875 "mms and mms over http streaming",
877 "Bertrand, Reimar Doeffinger, Albeu",
878 "originally based on work by Majormms (is that code still there?)",
880 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL
},
882 0 // Urls are an option string