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])
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
buffer = (char*) malloc(size+buffer_size);
200
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
203
if( chunk_buffer!=NULL ) {
204
memcpy( buffer, chunk_buffer, buffer_size );
205
free( chunk_buffer );
207
chunk_buffer = buffer;
208
buffer += buffer_size;
211
for(r = 0; r < size;) {
212
i = nop_streaming_read(fd,buffer+r,size-r,streaming_ctrl);
214
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
220
if( chunk_size2read==0 ) {
221
if(size < (int)sizeof(asfh)) {
222
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
224
} else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
225
memcpy(&asfh,buffer,sizeof(asfh));
226
le2me_ASF_header_t(&asfh);
227
chunk_size2read = asfh.objh.size;
228
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
230
} while( buffer_size<chunk_size2read);
231
buffer = chunk_buffer;
235
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrSubChunkNumberInvalid);
239
start = sizeof(asfh);
241
pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
243
ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
244
pos += sizeof(ASF_file_header_t);
245
if (pos > size) goto len_err_out;
246
le2me_ASF_file_header_t(fileh);
248
if(fileh.packetsize != fileh.packetsize2) {
249
printf("Error packetsize check don't match\n");
253
asf_ctrl->packet_size = fileh->max_packet_size;
255
// preroll: time in ms to bufferize before playing
256
streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
260
while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
262
ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
263
pos += sizeof(ASF_stream_header_t);
264
if (pos > size) goto len_err_out;
265
le2me_ASF_stream_header_t(streamh);
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 = (int*)malloc(sizeof(int));
270
asf_ctrl->n_audio = 1;
273
asf_ctrl->audio_streams = (int*)realloc(asf_ctrl->audio_streams,
274
asf_ctrl->n_audio*sizeof(int));
276
asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = streamh->stream_no;
278
case 0xBC19EFC0 : // video stream
279
if(asf_ctrl->video_streams == NULL){
280
asf_ctrl->video_streams = (int*)malloc(sizeof(int));
281
asf_ctrl->n_video = 1;
284
asf_ctrl->video_streams = (int*)realloc(asf_ctrl->video_streams,
285
asf_ctrl->n_video*sizeof(int));
287
asf_ctrl->video_streams[asf_ctrl->n_video-1] = 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];
302
mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
303
stream_count = le2me_16(*(uint16_t*)ptr);
304
ptr += sizeof(uint16_t);
305
if (ptr > &buffer[size]) goto len_err_out;
306
mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
307
stream_count, stream_count );
308
for( i=0 ; i<stream_count ; i++ ) {
312
id = le2me_16(*(uint16_t*)ptr);
313
ptr += sizeof(uint16_t);
314
if (ptr > &buffer[size]) goto len_err_out;
315
memcpy(&rate, ptr, sizeof(uint32_t));// workaround unaligment bug on sparc
316
ptr += sizeof(uint32_t);
317
if (ptr > &buffer[size]) goto len_err_out;
318
rate = le2me_32(rate);
319
mp_msg(MSGT_NETWORK, MSGL_V,
320
" stream id=[0x%x][%u]\n", id, id);
321
mp_msg(MSGT_NETWORK, MSGL_V,
322
" max bitrate=[0x%x][%u]\n", rate, rate);
323
for (j = 0; j < asf_ctrl->n_video; j++) {
324
if (id == asf_ctrl->video_streams[j]) {
325
mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
330
for (j = 0; j < asf_ctrl->n_audio; j++) {
331
if (id == asf_ctrl->audio_streams[j]) {
332
mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
341
// automatic stream selection based on bandwidth
342
if (bw == 0) bw = INT_MAX;
343
mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
345
if (asf_ctrl->n_audio) {
346
// find lowest-bitrate audio stream
349
for (i = 0; i < asf_ctrl->n_audio; i++) {
350
if (a_rates[i] < a_rate) {
355
if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
356
// both audio and video are not possible, try video only next
361
// find best video stream
362
v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
364
v_rate = v_rates[v_idx];
366
// find best audio stream
367
a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
372
if (a_idx < 0 && v_idx < 0) {
373
mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
378
// a audio stream was forced
379
asf_ctrl->audio_id = audio_id;
381
asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
382
else if (asf_ctrl->n_audio) {
383
mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
388
// a video stream was forced
389
asf_ctrl->video_id = video_id;
391
asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
392
else if (asf_ctrl->n_video) {
393
mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
400
mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
401
if (buffer) free(buffer);
402
if (v_rates) free(v_rates);
403
if (a_rates) free(a_rates);
407
static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
408
static ASF_stream_chunck_t chunk;
409
int read,chunk_size = 0;
410
static int rest = 0, drop_chunk = 0, waiting = 0;
411
asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
414
if (rest == 0 && waiting == 0) {
416
while(read < (int)sizeof(ASF_stream_chunck_t)){
417
int r = nop_streaming_read( fd, ((char*)&chunk) + read,
418
sizeof(ASF_stream_chunck_t)-read,
422
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunkHeader);
428
// Endian handling of the stream chunk
429
le2me_ASF_stream_chunck_t(&chunk);
430
chunk_size = asf_streaming( &chunk, &drop_chunk );
432
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
435
chunk_size -= sizeof(ASF_stream_chunck_t);
437
if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
438
if (asf_http_ctrl->packet_size < chunk_size) {
439
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
442
waiting = asf_http_ctrl->packet_size;
444
waiting = chunk_size;
453
if ( waiting >= chunk_size) {
454
if (chunk_size > size){
455
rest = chunk_size - size;
458
while(read < chunk_size) {
459
int got = nop_streaming_read( fd,buffer+read,chunk_size-read,streaming_ctrl );
462
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunk);
468
if (drop_chunk) continue;
470
if (rest == 0 && waiting > 0 && size-read > 0) {
471
int s = MIN(waiting,size-read);
472
memset(buffer+read,0,s);
482
static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
484
// to shut up gcc warning
490
static int asf_header_check( HTTP_header_t *http_hdr ) {
491
ASF_obj_header_t *objh;
492
if( http_hdr==NULL ) return -1;
493
if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
495
objh = (ASF_obj_header_t*)http_hdr->body;
496
if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
500
static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
501
if( content_type==NULL ) return ASF_Unknown_e;
502
if( !strcasecmp(content_type, "application/octet-stream") ||
503
!strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
504
!strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
505
!strcasecmp(content_type, "video/x-ms-asf")) {
507
if( strstr(features, "broadcast") ) {
508
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
511
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
512
return ASF_Prerecorded_e;
515
// Ok in a perfect world, web servers should be well configured
516
// so we could used mime type to know the stream type,
517
// but guess what? All of them are not well configured.
518
// So we have to check for an asf header :(, but it works :p
519
if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
520
if( asf_header_check( http_hdr )==0 ) {
521
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
522
return ASF_PlainText_e;
523
} else if( (!strcasecmp(content_type, "text/html")) ) {
524
mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
525
return ASF_Unknown_e;
527
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
528
return ASF_Redirector_e;
531
if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
532
(!strcasecmp(content_type, "audio/x-ms-wma")) ||
533
(!strcasecmp(content_type, "video/x-ms-asf")) ||
534
(!strcasecmp(content_type, "video/x-ms-afs")) ||
535
(!strcasecmp(content_type, "video/x-ms-wvx")) ||
536
(!strcasecmp(content_type, "video/x-ms-wmv")) ||
537
(!strcasecmp(content_type, "video/x-ms-wma")) ) {
538
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
539
return ASF_Redirector_e;
540
} else if( !strcasecmp(content_type, "text/plain") ) {
541
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
542
return ASF_PlainText_e;
544
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
545
return ASF_Unknown_e;
549
return ASF_Unknown_e;
552
static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
553
HTTP_header_t *http_hdr;
555
URL_t *server_url = NULL;
556
asf_http_streaming_ctrl_t *asf_http_ctrl;
561
int offset_hi=0, offset_lo=0, length=0;
562
int asf_nb_stream=0, stream_id;
565
if( streaming_ctrl==NULL ) return NULL;
566
url = streaming_ctrl->url;
567
asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
568
if( url==NULL || asf_http_ctrl==NULL ) return NULL;
570
// Common header for all requests.
571
http_hdr = http_new_header();
572
http_set_field( http_hdr, "Accept: */*" );
573
http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
574
http_add_basic_authentication( http_hdr, url->username, url->password );
576
// Check if we are using a proxy
577
if( !strcasecmp( url->protocol, "http_proxy" ) ) {
578
server_url = url_new( (url->file)+1 );
579
if( server_url==NULL ) {
580
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
581
http_free( http_hdr );
584
http_set_uri( http_hdr, server_url->url );
585
sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
586
url_free( server_url );
588
http_set_uri( http_hdr, url->file );
589
sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
592
http_set_field( http_hdr, str );
593
http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
595
"Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
596
offset_hi, offset_lo, asf_http_ctrl->request, length );
597
http_set_field( http_hdr, str );
599
switch( asf_http_ctrl->streaming_type ) {
601
case ASF_Prerecorded_e:
602
http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
604
ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
605
if(asf_http_ctrl->n_audio > 0) {
606
for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
607
stream_id = asf_http_ctrl->audio_streams[i];
608
if(stream_id == asf_http_ctrl->audio_id) {
615
ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
618
if(asf_http_ctrl->n_video > 0) {
619
for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
620
stream_id = asf_http_ctrl->video_streams[i];
621
if(stream_id == asf_http_ctrl->video_id) {
628
ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
631
http_set_field( http_hdr, str );
632
sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
633
http_set_field( http_hdr, str );
635
case ASF_Redirector_e:
638
// First request goes here.
641
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
644
http_set_field( http_hdr, "Connection: Close" );
645
http_build_request( http_hdr );
650
static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
651
char *content_type, *pragma;
652
char features[64] = "\0";
654
if( http_response_parse(http_hdr)<0 ) {
655
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
658
switch( http_hdr->status_code ) {
661
case 401: // Authentication required
662
return ASF_Authenticate_e;
664
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
668
content_type = http_get_field( http_hdr, "Content-Type");
669
//printf("Content-Type: [%s]\n", content_type);
671
pragma = http_get_field( http_hdr, "Pragma");
672
while( pragma!=NULL ) {
673
char *comma_ptr=NULL;
675
//printf("Pragma: [%s]\n", pragma );
676
// The pragma line can get severals attributes
677
// separeted with a comma ','.
679
if( !strncasecmp( pragma, "features=", 9) ) {
681
end = strstr( pragma, "," );
683
size_t s = strlen(pragma);
684
if(s > sizeof(features)) {
685
mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,s,sizeof(features));
686
len = sizeof(features);
691
len = MIN((unsigned int)(end-pragma),sizeof(features));
693
strncpy( features, pragma, len );
697
comma_ptr = strstr( pragma, "," );
698
if( comma_ptr!=NULL ) {
699
pragma = comma_ptr+1;
700
if( pragma[0]==' ' ) pragma++;
702
} while( comma_ptr!=NULL );
703
pragma = http_get_next_field( http_hdr );
705
asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
709
static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
710
HTTP_header_t *http_hdr=NULL;
711
URL_t *url = stream->streaming_ctrl->url;
712
asf_http_streaming_ctrl_t *asf_http_ctrl;
713
char buffer[BUFFER_SIZE];
719
asf_http_ctrl = (asf_http_streaming_ctrl_t*)malloc(sizeof(asf_http_streaming_ctrl_t));
720
if( asf_http_ctrl==NULL ) {
721
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
724
asf_http_ctrl->streaming_type = ASF_Unknown_e;
725
asf_http_ctrl->request = 1;
726
asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
727
asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
728
stream->streaming_ctrl->data = (void*)asf_http_ctrl;
732
if( fd>0 ) closesocket( fd );
734
if( !strcasecmp( url->protocol, "http_proxy" ) ) {
735
if( url->port==0 ) url->port = 8080;
737
if( url->port==0 ) url->port = 80;
739
fd = connect2Server( url->hostname, url->port, 1);
740
if( fd<0 ) return fd;
742
http_hdr = asf_http_request( stream->streaming_ctrl );
743
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
744
for(i=0; i < (int)http_hdr->buffer_size ; ) {
745
int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
747
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
752
http_free( http_hdr );
753
http_hdr = http_new_header();
755
i = recv( fd, buffer, BUFFER_SIZE, 0 );
756
//printf("read: %d\n", i );
759
http_free( http_hdr );
762
http_response_append( http_hdr, buffer, i );
763
} while( !http_is_header_entire( http_hdr ) );
765
http_hdr->buffer[http_hdr->buffer_size]='\0';
766
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
768
ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
770
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
771
http_free( http_hdr );
774
switch( asf_http_ctrl->streaming_type ) {
776
case ASF_Prerecorded_e:
777
case ASF_PlainText_e:
778
if( http_hdr->body_size>0 ) {
779
if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
780
http_free( http_hdr );
784
if( asf_http_ctrl->request==1 ) {
785
if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
786
// First request, we only got the ASF header.
787
ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
788
if(ret < 0) return -1;
789
if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
790
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
793
asf_http_ctrl->request++;
800
case ASF_Redirector_e:
801
if( http_hdr->body_size>0 ) {
802
if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
803
http_free( http_hdr );
807
*demuxer_type = DEMUXER_TYPE_PLAYLIST;
810
case ASF_Authenticate_e:
811
if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
812
asf_http_ctrl->streaming_type = ASF_Unknown_e;
817
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
819
http_free( http_hdr );
822
// Check if we got a redirect.
826
if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
827
stream->streaming_ctrl->streaming_read = nop_streaming_read;
828
stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
830
stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
831
stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
832
stream->streaming_ctrl->buffering = 1;
834
stream->streaming_ctrl->status = streaming_playing_e;
835
stream->close = close_s;
837
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_UNSUPORTED;
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_UNSUPORTED;
867
*file_format = DEMUXER_TYPE_ASF;
868
stream->type = STREAMTYPE_STREAM;
869
fixup_network_stream_cache(stream);
873
stream_info_t stream_info_asf = {
874
"mms and mms over http streaming",
876
"Bertrand, Reimar Doeffinger, Albeu",
877
"originally based on work by Majormms (is that code still there?)",
879
{"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL},
881
0 // Urls are an option string