13 #define closesocket close
20 #include "libmpdemux/asf.h"
23 #include "libmpdemux/demuxer.h"
28 #include "libavutil/intreadwrite.h"
31 #define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid))
33 #define ASF_LOAD_GUID_PREFIX(guid) \
34 ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0])
37 extern int network_bandwidth
;
39 int asf_mmst_streaming_start( stream_t
*stream
);
40 static int asf_http_streaming_start(stream_t
*stream
, int *demuxer_type
);
42 static int asf_read_wrapper(int fd
, void *buffer
, int len
, streaming_ctrl_t
*stream_ctrl
) {
43 uint8_t *buf
= buffer
;
45 int got
= nop_streaming_read(fd
, buf
, len
, stream_ctrl
);
47 mp_msg(MSGT_NETWORK
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream
);
56 // We can try several protocol for asf streaming
57 // * first the UDP protcol, if there is a firewall, UDP
58 // packets will not come back, so the mmsu will fail.
59 // * Then we can try TCP, but if there is a proxy for
60 // internet connection, the TCP connection will not get
62 // * Then we can try HTTP.
64 // Note: Using WMP sequence MMSU then MMST and then HTTP.
66 static int asf_streaming_start( stream_t
*stream
, int *demuxer_type
) {
67 char *proto
= stream
->streaming_ctrl
->url
->protocol
;
69 int port
= stream
->streaming_ctrl
->url
->port
;
71 // Is protocol mms or mmsu?
72 if (!strcasecmp(proto
, "mmsu") || !strcasecmp(proto
, "mms"))
74 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/UDP...\n");
75 //fd = asf_mmsu_streaming_start( stream );
76 if( fd
>-1 ) return fd
; //mmsu support is not implemented yet - using this code
77 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/UDP failed\n");
78 if( fd
==-2 ) return -1;
81 //Is protocol mms or mmst?
82 if (!strcasecmp(proto
, "mmst") || !strcasecmp(proto
, "mms"))
84 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/TCP...\n");
85 fd
= asf_mmst_streaming_start( stream
);
86 stream
->streaming_ctrl
->url
->port
= port
;
87 if( fd
>-1 ) return fd
;
88 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/TCP failed\n");
89 if( fd
==-2 ) return -1;
92 //Is protocol http, http_proxy, or mms?
93 if (!strcasecmp(proto
, "http_proxy") || !strcasecmp(proto
, "http") ||
94 !strcasecmp(proto
, "mms") || !strcasecmp(proto
, "mmshttp"))
96 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/HTTP...\n");
97 fd
= asf_http_streaming_start( stream
, demuxer_type
);
98 stream
->streaming_ctrl
->url
->port
= port
;
99 if( fd
>-1 ) return fd
;
100 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/HTTP failed\n");
101 if( fd
==-2 ) return -1;
108 static int asf_streaming(ASF_stream_chunck_t
*stream_chunck
, int *drop_packet
) {
110 printf("ASF stream chunck size=%d\n", stream_chunck->size);
111 printf("length: %d\n", length );
112 printf("0x%02X\n", stream_chunck->type );
114 if( drop_packet
!=NULL
) *drop_packet
= 0;
116 if( stream_chunck
->size
<8 ) {
117 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small
, stream_chunck
->size
);
120 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
121 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch
, stream_chunck
->size
, stream_chunck
->size_confirm
);
125 printf(" type: 0x%02X\n", stream_chunck->type );
126 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
127 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
128 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
129 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
131 switch(stream_chunck
->type
) {
132 case ASF_STREAMING_CLEAR
: // $C Clear ASF configuration
133 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Clearing ASF stream configuration!\n");
134 if( drop_packet
!=NULL
) *drop_packet
= 1;
135 return stream_chunck
->size
;
137 case ASF_STREAMING_DATA
: // $D Data follows
138 // printf("=====> Data follows\n");
140 case ASF_STREAMING_END_TRANS
: // $E Transfer complete
141 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Transfer complete\n");
142 if( drop_packet
!=NULL
) *drop_packet
= 1;
143 return stream_chunck
->size
;
145 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
146 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
149 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
151 return stream_chunck
->size
+4;
154 extern int find_asf_guid(char *buf
, const char *guid
, int cur_pos
, int buf_len
);
155 extern const char asf_file_header_guid
[];
156 extern const char asf_stream_header_guid
[];
157 extern const char asf_stream_group_guid
[];
161 static void close_s(stream_t
*stream
) {
166 static int max_idx(int s_count
, int *s_rates
, int bound
) {
167 int i
, best
= -1, rate
= -1;
168 for (i
= 0; i
< s_count
; i
++) {
169 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
177 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
178 ASF_stream_chunck_t chunk
;
179 asf_http_streaming_ctrl_t
* asf_ctrl
= streaming_ctrl
->data
;
180 char* buffer
=NULL
, *chunk_buffer
=NULL
;
181 int i
,r
,size
,pos
= 0;
184 int chunk_size2read
= 0;
185 int bw
= streaming_ctrl
->bandwidth
;
186 int *v_rates
= NULL
, *a_rates
= NULL
;
187 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
189 if(asf_ctrl
== NULL
) return -1;
191 // The ASF header can be in several network chunks. For example if the content description
192 // is big, the ASF header will be split in 2 network chunk.
193 // So we need to retrieve all the chunk before starting to parse the header.
195 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
197 // Endian handling of the stream chunk
198 le2me_ASF_stream_chunck_t(&chunk
);
199 size
= asf_streaming( &chunk
, &r
) - sizeof(ASF_stream_chunck_t
);
200 if(r
) mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_WarnDropHeader
);
202 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
205 if (chunk
.type
!= ASF_STREAMING_HEADER
) {
206 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk
);
210 // audit: do not overflow buffer_size
211 if (size
> SIZE_MAX
- buffer_size
) return -1;
212 buffer
= malloc(size
+buffer_size
);
214 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MPDEMUX_ASF_BufferMallocFailed
,size
+buffer_size
);
217 if( chunk_buffer
!=NULL
) {
218 memcpy( buffer
, chunk_buffer
, buffer_size
);
219 free( chunk_buffer
);
221 chunk_buffer
= buffer
;
222 buffer
+= buffer_size
;
225 if (asf_read_wrapper(fd
, buffer
, size
, streaming_ctrl
) <= 0)
228 if( chunk_size2read
==0 ) {
229 ASF_header_t
*asfh
= (ASF_header_t
*)buffer
;
230 if(size
< (int)sizeof(ASF_header_t
)) {
231 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunk2Small
);
233 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
234 chunk_size2read
= AV_RL64(&asfh
->objh
.size
);
235 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
237 } while( buffer_size
<chunk_size2read
);
238 buffer
= chunk_buffer
;
241 start
= sizeof(ASF_header_t
);
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
;
249 if(fileh.packetsize != fileh.packetsize2) {
250 printf("Error packetsize check don't match\n");
254 asf_ctrl
->packet_size
= AV_RL32(&fileh
->max_packet_size
);
256 // preroll: time in ms to bufferize before playing
257 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
261 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
263 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
264 pos
+= sizeof(ASF_stream_header_t
);
265 if (pos
> size
) goto len_err_out
;
266 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
267 case 0xF8699E40 : // audio stream
268 if(asf_ctrl
->audio_streams
== NULL
){
269 asf_ctrl
->audio_streams
= malloc(sizeof(int));
270 asf_ctrl
->n_audio
= 1;
273 asf_ctrl
->audio_streams
= realloc(asf_ctrl
->audio_streams
,
274 asf_ctrl
->n_audio
*sizeof(int));
276 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = AV_RL16(&streamh
->stream_no
);
278 case 0xBC19EFC0 : // video stream
279 if(asf_ctrl
->video_streams
== NULL
){
280 asf_ctrl
->video_streams
= malloc(sizeof(int));
281 asf_ctrl
->n_video
= 1;
284 asf_ctrl
->video_streams
= realloc(asf_ctrl
->video_streams
,
285 asf_ctrl
->n_video
*sizeof(int));
287 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = AV_RL16(&streamh
->stream_no
);
292 // always allocate to avoid lots of ifs later
293 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
294 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
296 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
298 // stream bitrate properties object
300 char *ptr
= &buffer
[pos
];
301 char *end
= &buffer
[size
];
303 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
304 if (ptr
+ 2 > end
) goto len_err_out
;
305 stream_count
= AV_RL16(ptr
);
307 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
308 stream_count
, stream_count
);
309 for( i
=0 ; i
<stream_count
; i
++ ) {
313 if (ptr
+ 6 > end
) goto len_err_out
;
318 mp_msg(MSGT_NETWORK
, MSGL_V
,
319 " stream id=[0x%x][%u]\n", id
, id
);
320 mp_msg(MSGT_NETWORK
, MSGL_V
,
321 " max bitrate=[0x%x][%u]\n", rate
, rate
);
322 for (j
= 0; j
< asf_ctrl
->n_video
; j
++) {
323 if (id
== asf_ctrl
->video_streams
[j
]) {
324 mp_msg(MSGT_NETWORK
, MSGL_V
, " is video stream\n");
329 for (j
= 0; j
< asf_ctrl
->n_audio
; j
++) {
330 if (id
== asf_ctrl
->audio_streams
[j
]) {
331 mp_msg(MSGT_NETWORK
, MSGL_V
, " is audio stream\n");
340 // automatic stream selection based on bandwidth
341 if (bw
== 0) bw
= INT_MAX
;
342 mp_msg(MSGT_NETWORK
, MSGL_V
, "Max bandwidth set to %d\n", bw
);
344 if (asf_ctrl
->n_audio
) {
345 // find lowest-bitrate audio stream
348 for (i
= 0; i
< asf_ctrl
->n_audio
; i
++) {
349 if (a_rates
[i
] < a_rate
) {
354 if (max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
) < 0) {
355 // both audio and video are not possible, try video only next
360 // find best video stream
361 v_idx
= max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
);
363 v_rate
= v_rates
[v_idx
];
365 // find best audio stream
366 a_idx
= max_idx(asf_ctrl
->n_audio
, a_rates
, bw
- v_rate
);
371 if (a_idx
< 0 && v_idx
< 0) {
372 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay
);
377 // a audio stream was forced
378 asf_ctrl
->audio_id
= audio_id
;
380 asf_ctrl
->audio_id
= asf_ctrl
->audio_streams
[a_idx
];
381 else if (asf_ctrl
->n_audio
) {
382 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio
);
387 // a video stream was forced
388 asf_ctrl
->video_id
= video_id
;
390 asf_ctrl
->video_id
= asf_ctrl
->video_streams
[v_idx
];
391 else if (asf_ctrl
->n_video
) {
392 mp_msg(MSGT_NETWORK
, MSGL_WARN
, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo
);
399 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MPDEMUX_ASF_InvalidLenInHeader
);
400 if (buffer
) free(buffer
);
401 if (v_rates
) free(v_rates
);
402 if (a_rates
) free(a_rates
);
406 static int asf_http_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*streaming_ctrl
) {
407 static ASF_stream_chunck_t chunk
;
408 int read
,chunk_size
= 0;
409 static int rest
= 0, drop_chunk
= 0, waiting
= 0;
410 asf_http_streaming_ctrl_t
*asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
413 if (rest
== 0 && waiting
== 0) {
414 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
417 // Endian handling of the stream chunk
418 le2me_ASF_stream_chunck_t(&chunk
);
419 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
421 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
424 chunk_size
-= sizeof(ASF_stream_chunck_t
);
426 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
427 if (asf_http_ctrl
->packet_size
< chunk_size
) {
428 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket
);
431 waiting
= asf_http_ctrl
->packet_size
;
433 waiting
= chunk_size
;
442 if ( waiting
>= chunk_size
) {
443 if (chunk_size
> size
){
444 rest
= chunk_size
- size
;
447 if (asf_read_wrapper(fd
, buffer
, chunk_size
, streaming_ctrl
) <= 0)
451 if (drop_chunk
) continue;
453 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
454 int s
= FFMIN(waiting
,size
-read
);
455 memset(buffer
+read
,0,s
);
465 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
467 // to shut up gcc warning
473 static int asf_header_check( HTTP_header_t
*http_hdr
) {
474 ASF_obj_header_t
*objh
;
475 if( http_hdr
==NULL
) return -1;
476 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
478 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
479 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
483 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
484 if( content_type
==NULL
) return ASF_Unknown_e
;
485 if( !strcasecmp(content_type
, "application/octet-stream") ||
486 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
487 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
488 !strcasecmp(content_type
, "video/x-ms-asf")) {
490 if( strstr(features
, "broadcast") ) {
491 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
494 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
495 return ASF_Prerecorded_e
;
498 // Ok in a perfect world, web servers should be well configured
499 // so we could used mime type to know the stream type,
500 // but guess what? All of them are not well configured.
501 // So we have to check for an asf header :(, but it works :p
502 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
503 if( asf_header_check( http_hdr
)==0 ) {
504 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
505 return ASF_PlainText_e
;
506 } else if( (!strcasecmp(content_type
, "text/html")) ) {
507 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
508 return ASF_Unknown_e
;
510 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
511 return ASF_Redirector_e
;
514 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
515 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
516 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
517 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
518 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
519 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
520 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ASFRedirector
);
521 return ASF_Redirector_e
;
522 } else if( !strcasecmp(content_type
, "text/plain") ) {
523 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
524 return ASF_PlainText_e
;
526 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
527 return ASF_Unknown_e
;
531 return ASF_Unknown_e
;
534 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
535 HTTP_header_t
*http_hdr
;
537 URL_t
*server_url
= NULL
;
538 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
543 int offset_hi
=0, offset_lo
=0, length
=0;
544 int asf_nb_stream
=0, stream_id
;
547 if( streaming_ctrl
==NULL
) return NULL
;
548 url
= streaming_ctrl
->url
;
549 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
550 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
552 // Common header for all requests.
553 http_hdr
= http_new_header();
554 http_set_field( http_hdr
, "Accept: */*" );
555 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
556 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
558 // Check if we are using a proxy
559 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
560 server_url
= url_new( (url
->file
)+1 );
561 if( server_url
==NULL
) {
562 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_InvalidProxyURL
);
563 http_free( http_hdr
);
566 http_set_uri( http_hdr
, server_url
->url
);
567 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
568 url_free( server_url
);
570 http_set_uri( http_hdr
, url
->file
);
571 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
574 http_set_field( http_hdr
, str
);
575 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
577 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
578 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
579 http_set_field( http_hdr
, str
);
581 switch( asf_http_ctrl
->streaming_type
) {
583 case ASF_Prerecorded_e
:
584 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
586 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
587 if(asf_http_ctrl
->n_audio
> 0) {
588 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
589 stream_id
= asf_http_ctrl
->audio_streams
[i
];
590 if(stream_id
== asf_http_ctrl
->audio_id
) {
597 ptr
+= sprintf(ptr
, "ffff:%d:%d ", stream_id
, enable
);
600 if(asf_http_ctrl
->n_video
> 0) {
601 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
602 stream_id
= asf_http_ctrl
->video_streams
[i
];
603 if(stream_id
== asf_http_ctrl
->video_id
) {
610 ptr
+= sprintf(ptr
, "ffff:%d:%d ", stream_id
, enable
);
613 http_set_field( http_hdr
, str
);
614 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
615 http_set_field( http_hdr
, str
);
617 case ASF_Redirector_e
:
620 // First request goes here.
623 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamType
);
626 http_set_field( http_hdr
, "Connection: Close" );
627 http_build_request( http_hdr
);
632 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
633 char *content_type
, *pragma
;
634 char features
[64] = "\0";
636 if( http_response_parse(http_hdr
)<0 ) {
637 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse
);
640 switch( http_hdr
->status_code
) {
643 case 401: // Authentication required
644 return ASF_Authenticate_e
;
646 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ServerReturn
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
650 content_type
= http_get_field( http_hdr
, "Content-Type");
651 //printf("Content-Type: [%s]\n", content_type);
653 pragma
= http_get_field( http_hdr
, "Pragma");
654 while( pragma
!=NULL
) {
655 char *comma_ptr
=NULL
;
657 //printf("Pragma: [%s]\n", pragma );
658 // The pragma line can get severals attributes
659 // separeted with a comma ','.
661 if( !strncasecmp( pragma
, "features=", 9) ) {
663 end
= strstr( pragma
, "," );
665 len
= strlen(pragma
);
667 len
= (unsigned int)(end
-pragma
);
669 if(len
> sizeof(features
) - 1) {
670 mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma
,pragma
,len
,sizeof(features
) - 1);
671 len
= sizeof(features
) - 1;
673 strncpy( features
, pragma
, len
);
677 comma_ptr
= strstr( pragma
, "," );
678 if( comma_ptr
!=NULL
) {
679 pragma
= comma_ptr
+1;
680 if( pragma
[0]==' ' ) pragma
++;
682 } while( comma_ptr
!=NULL
);
683 pragma
= http_get_next_field( http_hdr
);
685 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
689 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
690 HTTP_header_t
*http_hdr
=NULL
;
691 URL_t
*url
= stream
->streaming_ctrl
->url
;
692 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
693 char buffer
[BUFFER_SIZE
];
699 asf_http_ctrl
= malloc(sizeof(asf_http_streaming_ctrl_t
));
700 if( asf_http_ctrl
==NULL
) {
701 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
704 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
705 asf_http_ctrl
->request
= 1;
706 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
707 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
708 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
712 if( fd
>0 ) closesocket( fd
);
714 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
715 if( url
->port
==0 ) url
->port
= 8080;
717 if( url
->port
==0 ) url
->port
= 80;
719 fd
= connect2Server( url
->hostname
, url
->port
, 1);
720 if( fd
<0 ) return fd
;
722 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
723 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
724 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
725 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, 0 );
727 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SocketWriteError
,strerror(errno
));
732 http_free( http_hdr
);
733 http_hdr
= http_new_header();
735 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
736 //printf("read: %d\n", i );
741 http_response_append( http_hdr
, buffer
, i
);
742 } while( !http_is_header_entire( http_hdr
) );
743 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
744 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
745 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
747 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
749 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_HeaderParseFailed
);
752 switch( asf_http_ctrl
->streaming_type
) {
754 case ASF_Prerecorded_e
:
755 case ASF_PlainText_e
:
756 if( http_hdr
->body_size
>0 ) {
757 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
761 if( asf_http_ctrl
->request
==1 ) {
762 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
763 // First request, we only got the ASF header.
764 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
765 if(ret
< 0) goto err_out
;
766 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
767 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoStreamFound
);
770 asf_http_ctrl
->request
++;
777 case ASF_Redirector_e
:
778 if( http_hdr
->body_size
>0 ) {
779 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
783 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
786 case ASF_Authenticate_e
:
787 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
788 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
793 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType
);
796 // Check if we got a redirect.
800 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
801 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
802 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
804 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
805 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
806 stream
->streaming_ctrl
->buffering
= 1;
808 stream
->streaming_ctrl
->status
= streaming_playing_e
;
809 stream
->close
= close_s
;
811 http_free( http_hdr
);
822 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
825 stream
->streaming_ctrl
= streaming_ctrl_new();
826 if( stream
->streaming_ctrl
==NULL
) {
829 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
830 url
= url_new(stream
->url
);
831 stream
->streaming_ctrl
->url
= check4proxies(url
);
834 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_MPDEMUX_ASF_InfoStreamASFURL
, stream
->url
);
835 if((!strncmp(stream
->url
, "http", 4)) && (*file_format
!=DEMUXER_TYPE_ASF
&& *file_format
!=DEMUXER_TYPE_UNKNOWN
)) {
836 streaming_ctrl_free(stream
->streaming_ctrl
);
837 stream
->streaming_ctrl
= NULL
;
838 return STREAM_UNSUPPORTED
;
841 if(asf_streaming_start(stream
, file_format
) < 0) {
842 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_StreamingFailed
);
843 streaming_ctrl_free(stream
->streaming_ctrl
);
844 stream
->streaming_ctrl
= NULL
;
845 return STREAM_UNSUPPORTED
;
848 if (*file_format
!= DEMUXER_TYPE_PLAYLIST
)
849 *file_format
= DEMUXER_TYPE_ASF
;
850 stream
->type
= STREAMTYPE_STREAM
;
851 fixup_network_stream_cache(stream
);
855 const stream_info_t stream_info_asf
= {
856 "mms and mms over http streaming",
858 "Bertrand, Reimar Doeffinger, Albeu",
859 "originally based on work by Majormms (is that code still there?)",
861 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL
},
863 0 // Urls are an option string