13 #define closesocket close
28 #define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid))
30 #define ASF_LOAD_GUID_PREFIX(guid) \
31 ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0])
34 extern int network_bandwidth
;
36 int asf_mmst_streaming_start( stream_t
*stream
);
37 static int asf_http_streaming_start(stream_t
*stream
, int *demuxer_type
);
39 // We can try several protocol for asf streaming
40 // * first the UDP protcol, if there is a firewall, UDP
41 // packets will not come back, so the mmsu will fail.
42 // * Then we can try TCP, but if there is a proxy for
43 // internet connection, the TCP connection will not get
45 // * Then we can try HTTP.
47 // Note: Using WMP sequence MMSU then MMST and then HTTP.
49 static int asf_streaming_start( stream_t
*stream
, int *demuxer_type
) {
50 char *proto
= stream
->streaming_ctrl
->url
->protocol
;
52 int port
= stream
->streaming_ctrl
->url
->port
;
54 // Is protocol mms or mmsu?
55 if (!strcasecmp(proto
, "mmsu") || !strcasecmp(proto
, "mms"))
57 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/UDP...\n");
58 //fd = asf_mmsu_streaming_start( stream );
59 if( fd
>-1 ) return fd
; //mmsu support is not implemented yet - using this code
60 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/UDP failed\n");
61 if( fd
==-2 ) return -1;
64 //Is protocol mms or mmst?
65 if (!strcasecmp(proto
, "mmst") || !strcasecmp(proto
, "mms"))
67 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/TCP...\n");
68 fd
= asf_mmst_streaming_start( stream
);
69 stream
->streaming_ctrl
->url
->port
= port
;
70 if( fd
>-1 ) return fd
;
71 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/TCP failed\n");
72 if( fd
==-2 ) return -1;
75 //Is protocol http, http_proxy, or mms?
76 if (!strcasecmp(proto
, "http_proxy") || !strcasecmp(proto
, "http") ||
77 !strcasecmp(proto
, "mms") || !strcasecmp(proto
, "mmshttp"))
79 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/HTTP...\n");
80 fd
= asf_http_streaming_start( stream
, demuxer_type
);
81 stream
->streaming_ctrl
->url
->port
= port
;
82 if( fd
>-1 ) return fd
;
83 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/HTTP failed\n");
84 if( fd
==-2 ) return -1;
91 static int asf_streaming(ASF_stream_chunck_t
*stream_chunck
, int *drop_packet
) {
93 printf("ASF stream chunck size=%d\n", stream_chunck->size);
94 printf("length: %d\n", length );
95 printf("0x%02X\n", stream_chunck->type );
97 if( drop_packet
!=NULL
) *drop_packet
= 0;
99 if( stream_chunck
->size
<8 ) {
100 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small
, stream_chunck
->size
);
103 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
104 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch
, stream_chunck
->size
, stream_chunck
->size_confirm
);
108 printf(" type: 0x%02X\n", stream_chunck->type );
109 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
110 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
111 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
112 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
114 switch(stream_chunck
->type
) {
115 case ASF_STREAMING_CLEAR
: // $C Clear ASF configuration
116 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Clearing ASF stream configuration!\n");
117 if( drop_packet
!=NULL
) *drop_packet
= 1;
118 return stream_chunck
->size
;
120 case ASF_STREAMING_DATA
: // $D Data follows
121 // printf("=====> Data follows\n");
123 case ASF_STREAMING_END_TRANS
: // $E Transfer complete
124 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Transfer complete\n");
125 if( drop_packet
!=NULL
) *drop_packet
= 1;
126 return stream_chunck
->size
;
128 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
129 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
132 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
134 return stream_chunck
->size
+4;
137 extern int find_asf_guid(char *buf
, const char *guid
, int cur_pos
, int buf_len
);
138 extern const char asf_file_header_guid
[];
139 extern const char asf_stream_header_guid
[];
140 extern const char asf_stream_group_guid
[];
144 static void close_s(stream_t
*stream
) {
149 static int max_idx(int s_count
, int *s_rates
, int bound
) {
150 int i
, best
= -1, rate
= -1;
151 for (i
= 0; i
< s_count
; i
++) {
152 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
160 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
162 ASF_stream_chunck_t chunk
;
163 asf_http_streaming_ctrl_t
* asf_ctrl
= (asf_http_streaming_ctrl_t
*) streaming_ctrl
->data
;
164 char* buffer
=NULL
, *chunk_buffer
=NULL
;
165 int i
,r
,size
,pos
= 0;
168 int chunk_size2read
= 0;
169 int bw
= streaming_ctrl
->bandwidth
;
170 int *v_rates
= NULL
, *a_rates
= NULL
;
171 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
173 if(asf_ctrl
== NULL
) return -1;
175 // The ASF header can be in several network chunks. For example if the content description
176 // is big, the ASF header will be split in 2 network chunk.
177 // So we need to retrieve all the chunk before starting to parse the header.
179 for( r
=0; r
< (int)sizeof(ASF_stream_chunck_t
) ; ) {
180 i
= nop_streaming_read(fd
,((char*)&chunk
)+r
,sizeof(ASF_stream_chunck_t
) - r
,streaming_ctrl
);
181 if(i
<= 0) return -1;
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 buffer
= (char*) malloc(size
+buffer_size
);
199 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MPDEMUX_ASF_BufferMallocFailed
,size
+buffer_size
);
202 if( chunk_buffer
!=NULL
) {
203 memcpy( buffer
, chunk_buffer
, buffer_size
);
204 free( chunk_buffer
);
206 chunk_buffer
= buffer
;
207 buffer
+= buffer_size
;
210 for(r
= 0; r
< size
;) {
211 i
= nop_streaming_read(fd
,buffer
+r
,size
-r
,streaming_ctrl
);
213 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream
);
219 if( chunk_size2read
==0 ) {
220 if(size
< (int)sizeof(asfh
)) {
221 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunk2Small
);
223 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
224 memcpy(&asfh
,buffer
,sizeof(asfh
));
225 le2me_ASF_header_t(&asfh
);
226 chunk_size2read
= asfh
.objh
.size
;
227 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
229 } while( buffer_size
<chunk_size2read
);
230 buffer
= chunk_buffer
;
234 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrSubChunkNumberInvalid
);
238 start
= sizeof(asfh
);
240 pos
= find_asf_guid(buffer
, asf_file_header_guid
, start
, size
);
242 ASF_file_header_t
*fileh
= (ASF_file_header_t
*) &buffer
[pos
];
243 pos
+= sizeof(ASF_file_header_t
);
244 if (pos
> size
) goto len_err_out
;
245 le2me_ASF_file_header_t(fileh
);
247 if(fileh.packetsize != fileh.packetsize2) {
248 printf("Error packetsize check don't match\n");
252 asf_ctrl
->packet_size
= fileh
->max_packet_size
;
254 // preroll: time in ms to bufferize before playing
255 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
259 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
261 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
262 pos
+= sizeof(ASF_stream_header_t
);
263 if (pos
> size
) goto len_err_out
;
264 le2me_ASF_stream_header_t(streamh
);
265 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
266 case 0xF8699E40 : // audio stream
267 if(asf_ctrl
->audio_streams
== NULL
){
268 asf_ctrl
->audio_streams
= (int*)malloc(sizeof(int));
269 asf_ctrl
->n_audio
= 1;
272 asf_ctrl
->audio_streams
= (int*)realloc(asf_ctrl
->audio_streams
,
273 asf_ctrl
->n_audio
*sizeof(int));
275 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = streamh
->stream_no
;
277 case 0xBC19EFC0 : // video stream
278 if(asf_ctrl
->video_streams
== NULL
){
279 asf_ctrl
->video_streams
= (int*)malloc(sizeof(int));
280 asf_ctrl
->n_video
= 1;
283 asf_ctrl
->video_streams
= (int*)realloc(asf_ctrl
->video_streams
,
284 asf_ctrl
->n_video
*sizeof(int));
286 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = streamh
->stream_no
;
291 // always allocate to avoid lots of ifs later
292 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
293 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
295 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
297 // stream bitrate properties object
299 char *ptr
= &buffer
[pos
];
301 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
302 stream_count
= le2me_16(*(uint16_t*)ptr
);
303 ptr
+= sizeof(uint16_t);
304 if (ptr
> &buffer
[size
]) goto len_err_out
;
305 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
306 stream_count
, stream_count
);
307 for( i
=0 ; i
<stream_count
; i
++ ) {
311 id
= le2me_16(*(uint16_t*)ptr
);
312 ptr
+= sizeof(uint16_t);
313 if (ptr
> &buffer
[size
]) goto len_err_out
;
314 memcpy(&rate
, ptr
, sizeof(uint32_t));// workaround unaligment bug on sparc
315 ptr
+= sizeof(uint32_t);
316 if (ptr
> &buffer
[size
]) goto len_err_out
;
317 rate
= le2me_32(rate
);
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) {
415 while(read
< (int)sizeof(ASF_stream_chunck_t
)){
416 int r
= nop_streaming_read( fd
, ((char*)&chunk
) + read
,
417 sizeof(ASF_stream_chunck_t
)-read
,
421 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrReadingChunkHeader
);
427 // Endian handling of the stream chunk
428 le2me_ASF_stream_chunck_t(&chunk
);
429 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
431 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader
);
434 chunk_size
-= sizeof(ASF_stream_chunck_t
);
436 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
437 if (asf_http_ctrl
->packet_size
< chunk_size
) {
438 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket
);
441 waiting
= asf_http_ctrl
->packet_size
;
443 waiting
= chunk_size
;
452 if ( waiting
>= chunk_size
) {
453 if (chunk_size
> size
){
454 rest
= chunk_size
- size
;
457 while(read
< chunk_size
) {
458 int got
= nop_streaming_read( fd
,buffer
+read
,chunk_size
-read
,streaming_ctrl
);
461 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ErrReadingChunk
);
467 if (drop_chunk
) continue;
469 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
470 int s
= MIN(waiting
,size
-read
);
471 memset(buffer
+read
,0,s
);
481 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
483 // to shut up gcc warning
489 static int asf_header_check( HTTP_header_t
*http_hdr
) {
490 ASF_obj_header_t
*objh
;
491 if( http_hdr
==NULL
) return -1;
492 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
494 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
495 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
499 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
500 if( content_type
==NULL
) return ASF_Unknown_e
;
501 if( !strcasecmp(content_type
, "application/octet-stream") ||
502 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
503 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
504 !strcasecmp(content_type
, "video/x-ms-asf")) {
506 if( strstr(features
, "broadcast") ) {
507 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
510 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
511 return ASF_Prerecorded_e
;
514 // Ok in a perfect world, web servers should be well configured
515 // so we could used mime type to know the stream type,
516 // but guess what? All of them are not well configured.
517 // So we have to check for an asf header :(, but it works :p
518 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
519 if( asf_header_check( http_hdr
)==0 ) {
520 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
521 return ASF_PlainText_e
;
522 } else if( (!strcasecmp(content_type
, "text/html")) ) {
523 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
524 return ASF_Unknown_e
;
526 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
527 return ASF_Redirector_e
;
530 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
531 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
532 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
533 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
534 (!strcasecmp(content_type
, "video/x-ms-wvx")) ||
535 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
536 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
537 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ASFRedirector
);
538 return ASF_Redirector_e
;
539 } else if( !strcasecmp(content_type
, "text/plain") ) {
540 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
541 return ASF_PlainText_e
;
543 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
544 return ASF_Unknown_e
;
548 return ASF_Unknown_e
;
551 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
552 HTTP_header_t
*http_hdr
;
554 URL_t
*server_url
= NULL
;
555 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
560 int offset_hi
=0, offset_lo
=0, length
=0;
561 int asf_nb_stream
=0, stream_id
;
564 if( streaming_ctrl
==NULL
) return NULL
;
565 url
= streaming_ctrl
->url
;
566 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
567 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
569 // Common header for all requests.
570 http_hdr
= http_new_header();
571 http_set_field( http_hdr
, "Accept: */*" );
572 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
573 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
575 // Check if we are using a proxy
576 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
577 server_url
= url_new( (url
->file
)+1 );
578 if( server_url
==NULL
) {
579 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_InvalidProxyURL
);
580 http_free( http_hdr
);
583 http_set_uri( http_hdr
, server_url
->url
);
584 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
585 url_free( server_url
);
587 http_set_uri( http_hdr
, url
->file
);
588 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
591 http_set_field( http_hdr
, str
);
592 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
594 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
595 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
596 http_set_field( http_hdr
, str
);
598 switch( asf_http_ctrl
->streaming_type
) {
600 case ASF_Prerecorded_e
:
601 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
603 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
604 if(asf_http_ctrl
->n_audio
> 0) {
605 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
606 stream_id
= asf_http_ctrl
->audio_streams
[i
];
607 if(stream_id
== asf_http_ctrl
->audio_id
) {
614 ptr
+= sprintf(ptr
, "ffff:%d:%d ", stream_id
, enable
);
617 if(asf_http_ctrl
->n_video
> 0) {
618 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
619 stream_id
= asf_http_ctrl
->video_streams
[i
];
620 if(stream_id
== asf_http_ctrl
->video_id
) {
627 ptr
+= sprintf(ptr
, "ffff:%d:%d ", stream_id
, enable
);
630 http_set_field( http_hdr
, str
);
631 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
632 http_set_field( http_hdr
, str
);
634 case ASF_Redirector_e
:
637 // First request goes here.
640 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamType
);
643 http_set_field( http_hdr
, "Connection: Close" );
644 http_build_request( http_hdr
);
649 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
650 char *content_type
, *pragma
;
651 char features
[64] = "\0";
653 if( http_response_parse(http_hdr
)<0 ) {
654 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse
);
657 switch( http_hdr
->status_code
) {
660 case 401: // Authentication required
661 return ASF_Authenticate_e
;
663 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_ServerReturn
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
667 content_type
= http_get_field( http_hdr
, "Content-Type");
668 //printf("Content-Type: [%s]\n", content_type);
670 pragma
= http_get_field( http_hdr
, "Pragma");
671 while( pragma
!=NULL
) {
672 char *comma_ptr
=NULL
;
674 //printf("Pragma: [%s]\n", pragma );
675 // The pragma line can get severals attributes
676 // separeted with a comma ','.
678 if( !strncasecmp( pragma
, "features=", 9) ) {
680 end
= strstr( pragma
, "," );
682 len
= strlen(pragma
);
684 len
= (unsigned int)(end
-pragma
);
686 if(len
> sizeof(features
) - 1) {
687 mp_msg(MSGT_NETWORK
,MSGL_WARN
,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma
,pragma
,len
,sizeof(features
) - 1);
688 len
= sizeof(features
) - 1;
690 strncpy( features
, pragma
, len
);
694 comma_ptr
= strstr( pragma
, "," );
695 if( comma_ptr
!=NULL
) {
696 pragma
= comma_ptr
+1;
697 if( pragma
[0]==' ' ) pragma
++;
699 } while( comma_ptr
!=NULL
);
700 pragma
= http_get_next_field( http_hdr
);
702 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
706 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
707 HTTP_header_t
*http_hdr
=NULL
;
708 URL_t
*url
= stream
->streaming_ctrl
->url
;
709 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
710 char buffer
[BUFFER_SIZE
];
716 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)malloc(sizeof(asf_http_streaming_ctrl_t
));
717 if( asf_http_ctrl
==NULL
) {
718 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
721 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
722 asf_http_ctrl
->request
= 1;
723 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
724 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
725 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
729 if( fd
>0 ) closesocket( fd
);
731 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
732 if( url
->port
==0 ) url
->port
= 8080;
734 if( url
->port
==0 ) url
->port
= 80;
736 fd
= connect2Server( url
->hostname
, url
->port
, 1);
737 if( fd
<0 ) return fd
;
739 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
740 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
741 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
742 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, 0 );
744 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_SocketWriteError
,strerror(errno
));
749 http_free( http_hdr
);
750 http_hdr
= http_new_header();
752 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
753 //printf("read: %d\n", i );
756 http_free( http_hdr
);
759 http_response_append( http_hdr
, buffer
, i
);
760 } while( !http_is_header_entire( http_hdr
) );
761 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
762 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
763 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
765 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
767 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_HeaderParseFailed
);
768 http_free( http_hdr
);
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 ) {
777 http_free( http_hdr
);
781 if( asf_http_ctrl
->request
==1 ) {
782 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
783 // First request, we only got the ASF header.
784 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
785 if(ret
< 0) return -1;
786 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
787 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_NoStreamFound
);
790 asf_http_ctrl
->request
++;
797 case ASF_Redirector_e
:
798 if( http_hdr
->body_size
>0 ) {
799 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
800 http_free( http_hdr
);
804 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
807 case ASF_Authenticate_e
:
808 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
809 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
814 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType
);
816 http_free( http_hdr
);
819 // Check if we got a redirect.
823 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
824 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
825 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
827 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
828 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
829 stream
->streaming_ctrl
->buffering
= 1;
831 stream
->streaming_ctrl
->status
= streaming_playing_e
;
832 stream
->close
= close_s
;
834 http_free( http_hdr
);
838 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
841 stream
->streaming_ctrl
= streaming_ctrl_new();
842 if( stream
->streaming_ctrl
==NULL
) {
845 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
846 url
= url_new(stream
->url
);
847 stream
->streaming_ctrl
->url
= check4proxies(url
);
850 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_MPDEMUX_ASF_InfoStreamASFURL
, stream
->url
);
851 if((!strncmp(stream
->url
, "http", 4)) && (*file_format
!=DEMUXER_TYPE_ASF
&& *file_format
!=DEMUXER_TYPE_UNKNOWN
)) {
852 streaming_ctrl_free(stream
->streaming_ctrl
);
853 stream
->streaming_ctrl
= NULL
;
854 return STREAM_UNSUPORTED
;
857 if(asf_streaming_start(stream
, file_format
) < 0) {
858 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_ASF_StreamingFailed
);
859 streaming_ctrl_free(stream
->streaming_ctrl
);
860 stream
->streaming_ctrl
= NULL
;
861 return STREAM_UNSUPORTED
;
864 *file_format
= DEMUXER_TYPE_ASF
;
865 stream
->type
= STREAMTYPE_STREAM
;
866 fixup_network_stream_cache(stream
);
870 stream_info_t stream_info_asf
= {
871 "mms and mms over http streaming",
873 "Bertrand, Reimar Doeffinger, Albeu",
874 "originally based on work by Majormms (is that code still there?)",
876 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL
},
878 0 // Urls are an option string