2010-01-30 22:26:47 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2001-05-25 14:01:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <unistd.h>
|
2001-12-14 20:45:30 +00:00
|
|
|
#include <errno.h>
|
2004-12-25 11:31:32 +00:00
|
|
|
#include <limits.h>
|
2001-05-25 14:01:21 +00:00
|
|
|
|
2001-10-30 17:03:11 +00:00
|
|
|
#include "config.h"
|
2005-10-31 23:37:41 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2008-08-31 13:59:54 +00:00
|
|
|
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2008-08-31 13:59:54 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2001-05-25 14:01:21 +00:00
|
|
|
#include "url.h"
|
|
|
|
#include "http.h"
|
2006-08-04 17:01:29 +00:00
|
|
|
#include "libmpdemux/asf.h"
|
2001-05-18 16:14:06 +00:00
|
|
|
|
2001-06-04 17:54:59 +00:00
|
|
|
#include "stream.h"
|
2006-08-04 17:01:29 +00:00
|
|
|
#include "libmpdemux/demuxer.h"
|
2001-05-25 14:01:21 +00:00
|
|
|
|
2002-01-23 14:11:19 +00:00
|
|
|
#include "network.h"
|
2006-08-05 10:30:06 +00:00
|
|
|
#include "tcp.h"
|
2002-01-23 14:11:19 +00:00
|
|
|
|
2007-12-21 11:30:55 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
2008-04-09 00:32:35 +00:00
|
|
|
#include "libmpdemux/asfguid.h"
|
2001-12-12 22:35:51 +00:00
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
extern int network_bandwidth;
|
2001-12-12 22:35:51 +00:00
|
|
|
|
2002-10-29 09:18:53 +00:00
|
|
|
int asf_mmst_streaming_start( stream_t *stream );
|
2005-05-29 12:54:00 +00:00
|
|
|
static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
|
2002-10-29 09:18:53 +00:00
|
|
|
|
2007-12-21 00:13:08 +00:00
|
|
|
static int asf_read_wrapper(int fd, void *buffer, int len, streaming_ctrl_t *stream_ctrl) {
|
|
|
|
uint8_t *buf = buffer;
|
|
|
|
while (len > 0) {
|
|
|
|
int got = nop_streaming_read(fd, buf, len, stream_ctrl);
|
|
|
|
if (got <= 0) {
|
|
|
|
mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
buf += got;
|
|
|
|
len -= got;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-12-11 01:07:40 +00:00
|
|
|
// We can try several protocol for asf streaming
|
|
|
|
// * first the UDP protcol, if there is a firewall, UDP
|
2005-05-29 12:54:00 +00:00
|
|
|
// packets will not come back, so the mmsu will fail.
|
2001-12-11 01:07:40 +00:00
|
|
|
// * Then we can try TCP, but if there is a proxy for
|
|
|
|
// internet connection, the TCP connection will not get
|
|
|
|
// through
|
|
|
|
// * Then we can try HTTP.
|
2009-05-13 02:58:57 +00:00
|
|
|
//
|
2004-06-06 14:53:03 +00:00
|
|
|
// Note: Using WMP sequence MMSU then MMST and then HTTP.
|
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
|
2004-07-21 10:18:29 +00:00
|
|
|
char *proto = stream->streaming_ctrl->url->protocol;
|
|
|
|
int fd = -1;
|
2005-01-16 22:30:28 +00:00
|
|
|
int port = stream->streaming_ctrl->url->port;
|
2002-07-05 02:46:03 +00:00
|
|
|
|
2004-06-06 14:53:03 +00:00
|
|
|
// Is protocol mms or mmsu?
|
2005-07-26 18:55:20 +00:00
|
|
|
if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
|
2004-07-21 10:18:29 +00:00
|
|
|
{
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
|
2001-12-11 01:07:40 +00:00
|
|
|
//fd = asf_mmsu_streaming_start( stream );
|
2004-06-06 14:53:03 +00:00
|
|
|
if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
|
2002-09-03 01:32:13 +00:00
|
|
|
if( fd==-2 ) return -1;
|
2001-12-11 01:07:40 +00:00
|
|
|
}
|
2004-06-06 14:53:03 +00:00
|
|
|
|
|
|
|
//Is protocol mms or mmst?
|
2005-07-26 18:55:20 +00:00
|
|
|
if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
|
2004-07-21 10:18:29 +00:00
|
|
|
{
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
|
2002-05-14 22:33:30 +00:00
|
|
|
fd = asf_mmst_streaming_start( stream );
|
2005-01-16 22:30:28 +00:00
|
|
|
stream->streaming_ctrl->url->port = port;
|
2003-01-06 16:42:20 +00:00
|
|
|
if( fd>-1 ) return fd;
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
|
2002-09-03 01:32:13 +00:00
|
|
|
if( fd==-2 ) return -1;
|
2001-12-11 01:07:40 +00:00
|
|
|
}
|
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
//Is protocol http, http_proxy, or mms?
|
2005-07-26 18:55:20 +00:00
|
|
|
if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
|
2009-03-26 11:00:04 +00:00
|
|
|
!strcasecmp(proto, "mms") || !strcasecmp(proto, "mmsh") ||
|
|
|
|
!strcasecmp(proto, "mmshttp"))
|
2004-07-21 10:18:29 +00:00
|
|
|
{
|
2004-06-06 14:53:03 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
|
|
|
|
fd = asf_http_streaming_start( stream, demuxer_type );
|
2005-01-16 22:30:28 +00:00
|
|
|
stream->streaming_ctrl->url->port = port;
|
2004-06-06 14:53:03 +00:00
|
|
|
if( fd>-1 ) return fd;
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
|
|
|
|
if( fd==-2 ) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//everything failed
|
2001-12-11 01:07:40 +00:00
|
|
|
return -1;
|
2001-06-04 17:54:59 +00:00
|
|
|
}
|
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
|
2009-05-13 02:58:57 +00:00
|
|
|
/*
|
2002-01-08 07:31:29 +00:00
|
|
|
printf("ASF stream chunck size=%d\n", stream_chunck->size);
|
|
|
|
printf("length: %d\n", length );
|
|
|
|
printf("0x%02X\n", stream_chunck->type );
|
|
|
|
*/
|
|
|
|
if( drop_packet!=NULL ) *drop_packet = 0;
|
|
|
|
|
|
|
|
if( stream_chunck->size<8 ) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
|
2002-01-08 07:31:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if( stream_chunck->size!=stream_chunck->size_confirm ) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
|
2002-01-08 07:31:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
/*
|
2002-01-08 07:31:29 +00:00
|
|
|
printf(" type: 0x%02X\n", stream_chunck->type );
|
|
|
|
printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
|
|
|
|
printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
|
|
|
|
printf(" unknown: 0x%02X\n", stream_chunck->unknown );
|
|
|
|
printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
|
|
|
|
*/
|
|
|
|
switch(stream_chunck->type) {
|
2002-04-14 05:51:32 +00:00
|
|
|
case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
|
2002-01-08 07:31:29 +00:00
|
|
|
if( drop_packet!=NULL ) *drop_packet = 1;
|
|
|
|
return stream_chunck->size;
|
|
|
|
break;
|
2002-04-14 05:51:32 +00:00
|
|
|
case ASF_STREAMING_DATA: // $D Data follows
|
2002-01-08 07:31:29 +00:00
|
|
|
// printf("=====> Data follows\n");
|
|
|
|
break;
|
2002-04-14 05:51:32 +00:00
|
|
|
case ASF_STREAMING_END_TRANS: // $E Transfer complete
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
|
2002-01-08 07:31:29 +00:00
|
|
|
if( drop_packet!=NULL ) *drop_packet = 1;
|
|
|
|
return stream_chunck->size;
|
|
|
|
break;
|
2002-04-14 05:51:32 +00:00
|
|
|
case ASF_STREAMING_HEADER: // $H ASF header chunk follows
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
|
2002-01-08 07:31:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
|
2002-01-08 07:31:29 +00:00
|
|
|
}
|
|
|
|
return stream_chunck->size+4;
|
|
|
|
}
|
|
|
|
|
2004-12-25 11:31:32 +00:00
|
|
|
extern int audio_id;
|
|
|
|
extern int video_id;
|
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
static void close_s(stream_t *stream) {
|
2009-03-26 13:35:12 +00:00
|
|
|
closesocket(stream->fd);
|
2005-05-29 12:54:00 +00:00
|
|
|
stream->fd=-1;
|
|
|
|
}
|
|
|
|
|
2004-12-25 11:31:32 +00:00
|
|
|
static int max_idx(int s_count, int *s_rates, int bound) {
|
2005-02-03 20:13:44 +00:00
|
|
|
int i, best = -1, rate = -1;
|
2004-12-25 11:31:32 +00:00
|
|
|
for (i = 0; i < s_count; i++) {
|
|
|
|
if (s_rates[i] > rate && s_rates[i] <= bound) {
|
|
|
|
rate = s_rates[i];
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
|
2001-12-16 21:23:36 +00:00
|
|
|
ASF_stream_chunck_t chunk;
|
2007-12-21 11:37:47 +00:00
|
|
|
asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
|
2001-12-16 21:23:36 +00:00
|
|
|
char* buffer=NULL, *chunk_buffer=NULL;
|
|
|
|
int i,r,size,pos = 0;
|
2004-12-25 11:31:32 +00:00
|
|
|
int start;
|
2001-12-16 21:23:36 +00:00
|
|
|
int buffer_size = 0;
|
|
|
|
int chunk_size2read = 0;
|
2004-12-25 11:31:32 +00:00
|
|
|
int bw = streaming_ctrl->bandwidth;
|
|
|
|
int *v_rates = NULL, *a_rates = NULL;
|
|
|
|
int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-12-16 21:23:36 +00:00
|
|
|
if(asf_ctrl == NULL) return -1;
|
|
|
|
|
|
|
|
// The ASF header can be in several network chunks. For example if the content description
|
|
|
|
// is big, the ASF header will be split in 2 network chunk.
|
|
|
|
// So we need to retrieve all the chunk before starting to parse the header.
|
|
|
|
do {
|
2007-12-21 00:13:08 +00:00
|
|
|
if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
|
|
|
|
return -1;
|
2002-04-14 05:51:32 +00:00
|
|
|
// Endian handling of the stream chunk
|
|
|
|
le2me_ASF_stream_chunck_t(&chunk);
|
2001-12-16 21:23:36 +00:00
|
|
|
size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
|
2005-10-31 23:37:41 +00:00
|
|
|
if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
|
2001-12-16 21:23:36 +00:00
|
|
|
if(size < 0){
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
|
2001-12-16 21:23:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-04-14 05:51:32 +00:00
|
|
|
if (chunk.type != ASF_STREAMING_HEADER) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
|
2001-12-16 21:23:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-06-04 22:41:27 +00:00
|
|
|
// audit: do not overflow buffer_size
|
|
|
|
if (size > SIZE_MAX - buffer_size) return -1;
|
2007-12-21 10:53:32 +00:00
|
|
|
buffer = malloc(size+buffer_size);
|
2001-12-16 21:23:36 +00:00
|
|
|
if(buffer == NULL) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
|
2001-12-16 21:23:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if( chunk_buffer!=NULL ) {
|
|
|
|
memcpy( buffer, chunk_buffer, buffer_size );
|
|
|
|
free( chunk_buffer );
|
|
|
|
}
|
|
|
|
chunk_buffer = buffer;
|
|
|
|
buffer += buffer_size;
|
|
|
|
buffer_size += size;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2007-12-21 00:13:08 +00:00
|
|
|
if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
|
|
|
|
return -1;
|
2001-12-16 21:23:36 +00:00
|
|
|
|
|
|
|
if( chunk_size2read==0 ) {
|
2007-12-21 11:56:18 +00:00
|
|
|
ASF_header_t *asfh = (ASF_header_t *)buffer;
|
|
|
|
if(size < (int)sizeof(ASF_header_t)) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
|
2001-12-16 21:23:36 +00:00
|
|
|
return -1;
|
2002-04-30 16:58:54 +00:00
|
|
|
} else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
|
2007-12-21 11:56:18 +00:00
|
|
|
chunk_size2read = AV_RL64(&asfh->objh.size);
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
|
|
|
} while( buffer_size<chunk_size2read);
|
|
|
|
buffer = chunk_buffer;
|
2001-12-17 01:58:24 +00:00
|
|
|
size = buffer_size;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2007-12-21 11:56:18 +00:00
|
|
|
start = sizeof(ASF_header_t);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2004-12-25 11:31:32 +00:00
|
|
|
pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
|
|
|
|
if (pos >= 0) {
|
|
|
|
ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
|
|
|
|
pos += sizeof(ASF_file_header_t);
|
|
|
|
if (pos > size) goto len_err_out;
|
2002-01-20 21:38:11 +00:00
|
|
|
/*
|
2001-12-16 21:23:36 +00:00
|
|
|
if(fileh.packetsize != fileh.packetsize2) {
|
|
|
|
printf("Error packetsize check don't match\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-01-20 21:38:11 +00:00
|
|
|
*/
|
2007-12-21 11:46:58 +00:00
|
|
|
asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
|
2009-05-13 02:58:57 +00:00
|
|
|
// before playing.
|
2002-01-20 21:38:11 +00:00
|
|
|
// preroll: time in ms to bufferize before playing
|
2004-12-25 11:31:32 +00:00
|
|
|
streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = start;
|
|
|
|
while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
|
|
|
|
{
|
|
|
|
ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
|
|
|
|
pos += sizeof(ASF_stream_header_t);
|
|
|
|
if (pos > size) goto len_err_out;
|
|
|
|
switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
|
2001-12-16 21:23:36 +00:00
|
|
|
case 0xF8699E40 : // audio stream
|
|
|
|
if(asf_ctrl->audio_streams == NULL){
|
2006-07-13 16:41:13 +00:00
|
|
|
asf_ctrl->audio_streams = malloc(sizeof(int));
|
2001-12-16 21:23:36 +00:00
|
|
|
asf_ctrl->n_audio = 1;
|
|
|
|
} else {
|
|
|
|
asf_ctrl->n_audio++;
|
2007-12-21 10:53:32 +00:00
|
|
|
asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
|
2001-12-16 21:23:36 +00:00
|
|
|
asf_ctrl->n_audio*sizeof(int));
|
|
|
|
}
|
2007-12-21 11:46:58 +00:00
|
|
|
asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
|
2001-12-16 21:23:36 +00:00
|
|
|
break;
|
|
|
|
case 0xBC19EFC0 : // video stream
|
|
|
|
if(asf_ctrl->video_streams == NULL){
|
2006-07-13 16:41:13 +00:00
|
|
|
asf_ctrl->video_streams = malloc(sizeof(int));
|
2001-12-16 21:23:36 +00:00
|
|
|
asf_ctrl->n_video = 1;
|
|
|
|
} else {
|
|
|
|
asf_ctrl->n_video++;
|
2007-12-21 10:53:32 +00:00
|
|
|
asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
|
2001-12-16 21:23:36 +00:00
|
|
|
asf_ctrl->n_video*sizeof(int));
|
|
|
|
}
|
2007-12-21 11:46:58 +00:00
|
|
|
asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
|
2001-12-16 21:23:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-12-25 11:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// always allocate to avoid lots of ifs later
|
|
|
|
v_rates = calloc(asf_ctrl->n_video, sizeof(int));
|
|
|
|
a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
|
|
|
|
|
|
|
|
pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
|
|
|
|
if (pos >= 0) {
|
|
|
|
// stream bitrate properties object
|
|
|
|
int stream_count;
|
|
|
|
char *ptr = &buffer[pos];
|
2007-12-21 11:30:55 +00:00
|
|
|
char *end = &buffer[size];
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2004-12-28 11:16:23 +00:00
|
|
|
mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
|
2007-12-21 11:34:04 +00:00
|
|
|
if (ptr + 2 > end) goto len_err_out;
|
2007-12-21 11:30:55 +00:00
|
|
|
stream_count = AV_RL16(ptr);
|
|
|
|
ptr += 2;
|
2004-12-25 11:31:32 +00:00
|
|
|
mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
|
|
|
|
stream_count, stream_count );
|
|
|
|
for( i=0 ; i<stream_count ; i++ ) {
|
|
|
|
uint32_t rate;
|
|
|
|
int id;
|
|
|
|
int j;
|
2007-12-21 11:34:04 +00:00
|
|
|
if (ptr + 6 > end) goto len_err_out;
|
2007-12-21 11:30:55 +00:00
|
|
|
id = AV_RL16(ptr);
|
|
|
|
ptr += 2;
|
|
|
|
rate = AV_RL32(ptr);
|
|
|
|
ptr += 4;
|
2004-12-25 11:31:32 +00:00
|
|
|
mp_msg(MSGT_NETWORK, MSGL_V,
|
|
|
|
" stream id=[0x%x][%u]\n", id, id);
|
|
|
|
mp_msg(MSGT_NETWORK, MSGL_V,
|
|
|
|
" max bitrate=[0x%x][%u]\n", rate, rate);
|
|
|
|
for (j = 0; j < asf_ctrl->n_video; j++) {
|
|
|
|
if (id == asf_ctrl->video_streams[j]) {
|
|
|
|
mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
|
|
|
|
v_rates[j] = rate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (j = 0; j < asf_ctrl->n_audio; j++) {
|
|
|
|
if (id == asf_ctrl->audio_streams[j]) {
|
|
|
|
mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
|
|
|
|
a_rates[j] = rate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-07-05 02:46:03 +00:00
|
|
|
}
|
2001-12-12 22:35:51 +00:00
|
|
|
}
|
2001-12-16 21:23:36 +00:00
|
|
|
free(buffer);
|
2004-12-25 11:31:32 +00:00
|
|
|
|
|
|
|
// automatic stream selection based on bandwidth
|
|
|
|
if (bw == 0) bw = INT_MAX;
|
|
|
|
mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
|
|
|
|
|
|
|
|
if (asf_ctrl->n_audio) {
|
|
|
|
// find lowest-bitrate audio stream
|
|
|
|
a_rate = a_rates[0];
|
|
|
|
a_idx = 0;
|
|
|
|
for (i = 0; i < asf_ctrl->n_audio; i++) {
|
|
|
|
if (a_rates[i] < a_rate) {
|
|
|
|
a_rate = a_rates[i];
|
|
|
|
a_idx = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
|
|
|
|
// both audio and video are not possible, try video only next
|
|
|
|
a_idx = -1;
|
|
|
|
a_rate = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// find best video stream
|
|
|
|
v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
|
|
|
|
if (v_idx >= 0)
|
|
|
|
v_rate = v_rates[v_idx];
|
|
|
|
|
|
|
|
// find best audio stream
|
|
|
|
a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2004-12-25 11:31:32 +00:00
|
|
|
free(v_rates);
|
|
|
|
free(a_rates);
|
|
|
|
|
|
|
|
if (a_idx < 0 && v_idx < 0) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
|
2004-12-25 11:31:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio_id > 0)
|
|
|
|
// a audio stream was forced
|
|
|
|
asf_ctrl->audio_id = audio_id;
|
|
|
|
else if (a_idx >= 0)
|
|
|
|
asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
|
|
|
|
else if (asf_ctrl->n_audio) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
|
2004-12-25 11:31:32 +00:00
|
|
|
audio_id = -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (video_id > 0)
|
|
|
|
// a video stream was forced
|
|
|
|
asf_ctrl->video_id = video_id;
|
|
|
|
else if (v_idx >= 0)
|
|
|
|
asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
|
|
|
|
else if (asf_ctrl->n_video) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
|
2004-12-25 11:31:32 +00:00
|
|
|
video_id = -2;
|
|
|
|
}
|
|
|
|
|
2001-12-16 21:23:36 +00:00
|
|
|
return 1;
|
2004-12-25 11:31:32 +00:00
|
|
|
|
|
|
|
len_err_out:
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
|
2004-12-25 11:31:32 +00:00
|
|
|
if (buffer) free(buffer);
|
|
|
|
if (v_rates) free(v_rates);
|
|
|
|
if (a_rates) free(a_rates);
|
|
|
|
return -1;
|
2001-12-12 22:35:51 +00:00
|
|
|
}
|
2001-10-26 18:26:06 +00:00
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
|
2001-12-12 22:35:51 +00:00
|
|
|
static ASF_stream_chunck_t chunk;
|
|
|
|
int read,chunk_size = 0;
|
2002-09-22 02:33:28 +00:00
|
|
|
static int rest = 0, drop_chunk = 0, waiting = 0;
|
2001-12-16 21:23:36 +00:00
|
|
|
asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
|
2001-12-12 22:35:51 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if (rest == 0 && waiting == 0) {
|
2007-12-21 00:13:08 +00:00
|
|
|
if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
|
|
|
|
return -1;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-04-14 05:51:32 +00:00
|
|
|
// Endian handling of the stream chunk
|
|
|
|
le2me_ASF_stream_chunck_t(&chunk);
|
2001-12-16 21:23:36 +00:00
|
|
|
chunk_size = asf_streaming( &chunk, &drop_chunk );
|
2001-12-12 22:35:51 +00:00
|
|
|
if(chunk_size < 0) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
|
2001-12-12 22:35:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
chunk_size -= sizeof(ASF_stream_chunck_t);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-04-14 05:51:32 +00:00
|
|
|
if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
|
2001-12-16 21:23:36 +00:00
|
|
|
if (asf_http_ctrl->packet_size < chunk_size) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
|
2001-12-12 22:35:51 +00:00
|
|
|
return -1;
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
2001-12-16 21:23:36 +00:00
|
|
|
waiting = asf_http_ctrl->packet_size;
|
2001-12-12 22:35:51 +00:00
|
|
|
} else {
|
|
|
|
waiting = chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (rest){
|
|
|
|
chunk_size = rest;
|
|
|
|
rest = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
read = 0;
|
|
|
|
if ( waiting >= chunk_size) {
|
|
|
|
if (chunk_size > size){
|
|
|
|
rest = chunk_size - size;
|
|
|
|
chunk_size = size;
|
|
|
|
}
|
2007-12-21 00:13:08 +00:00
|
|
|
if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
|
|
|
|
return -1;
|
|
|
|
read = chunk_size;
|
2001-12-12 22:35:51 +00:00
|
|
|
waiting -= read;
|
|
|
|
if (drop_chunk) continue;
|
|
|
|
}
|
|
|
|
if (rest == 0 && waiting > 0 && size-read > 0) {
|
2007-03-01 12:54:38 +00:00
|
|
|
int s = FFMIN(waiting,size-read);
|
2001-12-12 22:35:51 +00:00
|
|
|
memset(buffer+read,0,s);
|
|
|
|
waiting -= s;
|
|
|
|
read += s;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-12-16 21:23:36 +00:00
|
|
|
|
2001-12-12 22:35:51 +00:00
|
|
|
return read;
|
2001-10-26 18:26:06 +00:00
|
|
|
}
|
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
|
2001-11-20 22:20:20 +00:00
|
|
|
return -1;
|
2002-10-29 09:18:53 +00:00
|
|
|
// to shut up gcc warning
|
|
|
|
fd++;
|
|
|
|
pos++;
|
|
|
|
streaming_ctrl=NULL;
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_header_check( HTTP_header_t *http_hdr ) {
|
2002-01-23 08:50:30 +00:00
|
|
|
ASF_obj_header_t *objh;
|
|
|
|
if( http_hdr==NULL ) return -1;
|
|
|
|
if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
|
|
|
|
|
|
|
|
objh = (ASF_obj_header_t*)http_hdr->body;
|
|
|
|
if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
|
2001-05-29 17:09:19 +00:00
|
|
|
if( content_type==NULL ) return ASF_Unknown_e;
|
2002-09-03 01:32:13 +00:00
|
|
|
if( !strcasecmp(content_type, "application/octet-stream") ||
|
|
|
|
!strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
|
2003-11-01 14:10:32 +00:00
|
|
|
!strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
|
2009-11-26 17:57:33 +00:00
|
|
|
!strcasecmp(content_type, "video/x-ms-wmv") ||
|
2009-05-13 02:58:57 +00:00
|
|
|
!strcasecmp(content_type, "video/x-ms-asf")) {
|
2002-09-03 01:32:13 +00:00
|
|
|
|
2003-01-02 15:38:26 +00:00
|
|
|
if( strstr(features, "broadcast") ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
|
2001-05-29 17:09:19 +00:00
|
|
|
return ASF_Live_e;
|
2001-05-18 16:14:06 +00:00
|
|
|
} else {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
|
2001-05-29 17:09:19 +00:00
|
|
|
return ASF_Prerecorded_e;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2002-01-23 08:50:30 +00:00
|
|
|
// Ok in a perfect world, web servers should be well configured
|
|
|
|
// so we could used mime type to know the stream type,
|
|
|
|
// but guess what? All of them are not well configured.
|
|
|
|
// So we have to check for an asf header :(, but it works :p
|
2002-01-24 09:29:52 +00:00
|
|
|
if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
|
|
|
|
if( asf_header_check( http_hdr )==0 ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
|
2002-01-24 09:29:52 +00:00
|
|
|
return ASF_PlainText_e;
|
2003-01-02 15:38:26 +00:00
|
|
|
} else if( (!strcasecmp(content_type, "text/html")) ) {
|
2004-06-25 17:02:51 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
|
2003-01-02 15:38:26 +00:00
|
|
|
return ASF_Unknown_e;
|
2002-01-24 09:29:52 +00:00
|
|
|
} else {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
|
2002-01-24 09:29:52 +00:00
|
|
|
return ASF_Redirector_e;
|
|
|
|
}
|
2002-01-23 08:50:30 +00:00
|
|
|
} else {
|
2002-01-24 09:29:52 +00:00
|
|
|
if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
|
|
|
|
(!strcasecmp(content_type, "audio/x-ms-wma")) ||
|
|
|
|
(!strcasecmp(content_type, "video/x-ms-asf")) ||
|
|
|
|
(!strcasecmp(content_type, "video/x-ms-afs")) ||
|
|
|
|
(!strcasecmp(content_type, "video/x-ms-wmv")) ||
|
|
|
|
(!strcasecmp(content_type, "video/x-ms-wma")) ) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
|
2002-01-24 09:29:52 +00:00
|
|
|
return ASF_Redirector_e;
|
|
|
|
} else if( !strcasecmp(content_type, "text/plain") ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
|
2002-01-24 09:29:52 +00:00
|
|
|
return ASF_PlainText_e;
|
|
|
|
} else {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
|
2002-01-24 09:29:52 +00:00
|
|
|
return ASF_Unknown_e;
|
|
|
|
}
|
2002-01-23 08:50:30 +00:00
|
|
|
}
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2001-05-29 17:09:19 +00:00
|
|
|
return ASF_Unknown_e;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2001-05-25 14:01:21 +00:00
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
|
2001-05-25 14:01:21 +00:00
|
|
|
HTTP_header_t *http_hdr;
|
2002-01-12 21:08:12 +00:00
|
|
|
URL_t *url = NULL;
|
|
|
|
URL_t *server_url = NULL;
|
|
|
|
asf_http_streaming_ctrl_t *asf_http_ctrl;
|
2001-05-25 14:01:21 +00:00
|
|
|
char str[250];
|
2001-06-04 17:54:59 +00:00
|
|
|
char *ptr;
|
2002-07-05 02:46:03 +00:00
|
|
|
int i, enable;
|
2001-05-25 14:01:21 +00:00
|
|
|
|
2001-12-11 01:07:40 +00:00
|
|
|
int offset_hi=0, offset_lo=0, length=0;
|
2002-07-05 02:46:03 +00:00
|
|
|
int asf_nb_stream=0, stream_id;
|
2001-05-25 14:01:21 +00:00
|
|
|
|
2002-01-12 21:08:12 +00:00
|
|
|
// Sanity check
|
|
|
|
if( streaming_ctrl==NULL ) return NULL;
|
|
|
|
url = streaming_ctrl->url;
|
|
|
|
asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
|
|
|
|
if( url==NULL || asf_http_ctrl==NULL ) return NULL;
|
|
|
|
|
2001-05-25 14:01:21 +00:00
|
|
|
// Common header for all requests.
|
|
|
|
http_hdr = http_new_header();
|
|
|
|
http_set_field( http_hdr, "Accept: */*" );
|
|
|
|
http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
|
2002-07-08 04:11:23 +00:00
|
|
|
http_add_basic_authentication( http_hdr, url->username, url->password );
|
2002-01-12 21:08:12 +00:00
|
|
|
|
|
|
|
// Check if we are using a proxy
|
2002-01-14 01:12:44 +00:00
|
|
|
if( !strcasecmp( url->protocol, "http_proxy" ) ) {
|
2002-01-12 21:08:12 +00:00
|
|
|
server_url = url_new( (url->file)+1 );
|
|
|
|
if( server_url==NULL ) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
|
2002-01-12 21:08:12 +00:00
|
|
|
http_free( http_hdr );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
http_set_uri( http_hdr, server_url->url );
|
2003-09-25 00:36:04 +00:00
|
|
|
sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
|
2002-01-12 21:08:12 +00:00
|
|
|
url_free( server_url );
|
|
|
|
} else {
|
|
|
|
http_set_uri( http_hdr, url->file );
|
2003-09-25 00:36:04 +00:00
|
|
|
sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
|
2002-01-12 21:08:12 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-05-25 14:01:21 +00:00
|
|
|
http_set_field( http_hdr, str );
|
|
|
|
http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
|
2009-05-13 02:58:57 +00:00
|
|
|
sprintf(str,
|
2001-05-25 14:01:21 +00:00
|
|
|
"Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
|
2001-12-11 01:07:40 +00:00
|
|
|
offset_hi, offset_lo, asf_http_ctrl->request, length );
|
2001-05-25 14:01:21 +00:00
|
|
|
http_set_field( http_hdr, str );
|
|
|
|
|
2001-12-11 01:07:40 +00:00
|
|
|
switch( asf_http_ctrl->streaming_type ) {
|
2001-05-25 14:01:21 +00:00
|
|
|
case ASF_Live_e:
|
|
|
|
case ASF_Prerecorded_e:
|
|
|
|
http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
|
2001-06-04 17:54:59 +00:00
|
|
|
ptr = str;
|
|
|
|
ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
|
2001-12-16 21:23:36 +00:00
|
|
|
if(asf_http_ctrl->n_audio > 0) {
|
2002-07-05 02:46:03 +00:00
|
|
|
for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
|
|
|
|
stream_id = asf_http_ctrl->audio_streams[i];
|
2004-12-25 11:31:32 +00:00
|
|
|
if(stream_id == asf_http_ctrl->audio_id) {
|
2002-07-05 02:46:03 +00:00
|
|
|
enable = 0;
|
|
|
|
} else {
|
|
|
|
enable = 2;
|
2004-12-25 11:31:32 +00:00
|
|
|
continue;
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
2002-07-05 02:46:03 +00:00
|
|
|
asf_nb_stream++;
|
2008-01-29 23:50:08 +00:00
|
|
|
ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
|
2001-06-04 17:54:59 +00:00
|
|
|
}
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
|
|
|
if(asf_http_ctrl->n_video > 0) {
|
2002-07-05 02:46:03 +00:00
|
|
|
for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
|
|
|
|
stream_id = asf_http_ctrl->video_streams[i];
|
2004-12-25 11:31:32 +00:00
|
|
|
if(stream_id == asf_http_ctrl->video_id) {
|
2002-07-05 02:46:03 +00:00
|
|
|
enable = 0;
|
|
|
|
} else {
|
|
|
|
enable = 2;
|
2004-12-25 11:31:32 +00:00
|
|
|
continue;
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
2002-07-05 02:46:03 +00:00
|
|
|
asf_nb_stream++;
|
2008-01-29 23:50:08 +00:00
|
|
|
ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
|
2001-06-04 17:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
http_set_field( http_hdr, str );
|
2001-05-25 14:01:21 +00:00
|
|
|
sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
|
|
|
|
http_set_field( http_hdr, str );
|
|
|
|
break;
|
|
|
|
case ASF_Redirector_e:
|
|
|
|
break;
|
|
|
|
case ASF_Unknown_e:
|
|
|
|
// First request goes here.
|
|
|
|
break;
|
|
|
|
default:
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
|
2001-05-25 14:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http_set_field( http_hdr, "Connection: Close" );
|
2001-05-29 17:09:19 +00:00
|
|
|
http_build_request( http_hdr );
|
2001-05-25 14:01:21 +00:00
|
|
|
|
2001-05-29 17:09:19 +00:00
|
|
|
return http_hdr;
|
2001-05-25 14:01:21 +00:00
|
|
|
}
|
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
|
2001-05-25 14:01:21 +00:00
|
|
|
char *content_type, *pragma;
|
|
|
|
char features[64] = "\0";
|
2002-10-29 09:18:53 +00:00
|
|
|
size_t len;
|
2001-05-29 17:09:19 +00:00
|
|
|
if( http_response_parse(http_hdr)<0 ) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
|
2001-05-29 17:09:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-07-09 08:26:34 +00:00
|
|
|
switch( http_hdr->status_code ) {
|
|
|
|
case 200:
|
|
|
|
break;
|
|
|
|
case 401: // Authentication required
|
|
|
|
return ASF_Authenticate_e;
|
|
|
|
default:
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
|
2002-07-09 08:26:34 +00:00
|
|
|
return -1;
|
2001-05-25 14:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
content_type = http_get_field( http_hdr, "Content-Type");
|
2001-11-20 22:20:20 +00:00
|
|
|
//printf("Content-Type: [%s]\n", content_type);
|
2001-05-25 14:01:21 +00:00
|
|
|
|
|
|
|
pragma = http_get_field( http_hdr, "Pragma");
|
2001-05-29 17:09:19 +00:00
|
|
|
while( pragma!=NULL ) {
|
2001-05-25 14:01:21 +00:00
|
|
|
char *comma_ptr=NULL;
|
|
|
|
char *end;
|
2001-11-20 22:20:20 +00:00
|
|
|
//printf("Pragma: [%s]\n", pragma );
|
2009-05-13 02:58:57 +00:00
|
|
|
// The pragma line can get severals attributes
|
2001-05-25 14:01:21 +00:00
|
|
|
// separeted with a comma ','.
|
|
|
|
do {
|
|
|
|
if( !strncasecmp( pragma, "features=", 9) ) {
|
|
|
|
pragma += 9;
|
|
|
|
end = strstr( pragma, "," );
|
|
|
|
if( end==NULL ) {
|
2006-03-12 21:44:22 +00:00
|
|
|
len = strlen(pragma);
|
2009-05-13 02:58:57 +00:00
|
|
|
} else {
|
2006-03-12 21:44:22 +00:00
|
|
|
len = (unsigned int)(end-pragma);
|
|
|
|
}
|
|
|
|
if(len > sizeof(features) - 1) {
|
2006-03-12 22:29:27 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
|
2006-03-12 21:44:22 +00:00
|
|
|
len = sizeof(features) - 1;
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
2001-05-25 14:01:21 +00:00
|
|
|
strncpy( features, pragma, len );
|
|
|
|
features[len]='\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
comma_ptr = strstr( pragma, "," );
|
|
|
|
if( comma_ptr!=NULL ) {
|
|
|
|
pragma = comma_ptr+1;
|
|
|
|
if( pragma[0]==' ' ) pragma++;
|
|
|
|
}
|
|
|
|
} while( comma_ptr!=NULL );
|
|
|
|
pragma = http_get_next_field( http_hdr );
|
2001-05-29 17:09:19 +00:00
|
|
|
}
|
2002-10-29 09:18:53 +00:00
|
|
|
asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
|
|
|
|
return 0;
|
2001-05-25 14:01:21 +00:00
|
|
|
}
|
|
|
|
|
2005-06-03 15:33:26 +00:00
|
|
|
static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
|
2001-05-29 17:09:19 +00:00
|
|
|
HTTP_header_t *http_hdr=NULL;
|
2001-11-20 22:20:20 +00:00
|
|
|
URL_t *url = stream->streaming_ctrl->url;
|
2001-12-11 01:07:40 +00:00
|
|
|
asf_http_streaming_ctrl_t *asf_http_ctrl;
|
2001-05-29 17:09:19 +00:00
|
|
|
char buffer[BUFFER_SIZE];
|
2001-12-11 01:07:40 +00:00
|
|
|
int i, ret;
|
2001-11-20 22:20:20 +00:00
|
|
|
int fd = stream->fd;
|
2001-12-11 01:07:40 +00:00
|
|
|
int done;
|
2002-07-09 08:26:34 +00:00
|
|
|
int auth_retry = 0;
|
2001-06-04 17:54:59 +00:00
|
|
|
|
2006-07-13 16:41:13 +00:00
|
|
|
asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
|
2001-12-11 01:07:40 +00:00
|
|
|
if( asf_http_ctrl==NULL ) {
|
2005-11-05 00:53:33 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2001-12-11 01:07:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
asf_http_ctrl->streaming_type = ASF_Unknown_e;
|
|
|
|
asf_http_ctrl->request = 1;
|
2001-12-16 21:23:36 +00:00
|
|
|
asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
|
|
|
|
asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
|
2001-12-11 01:07:40 +00:00
|
|
|
stream->streaming_ctrl->data = (void*)asf_http_ctrl;
|
|
|
|
|
2001-05-29 17:09:19 +00:00
|
|
|
do {
|
2001-12-11 01:07:40 +00:00
|
|
|
done = 1;
|
2003-06-11 16:48:09 +00:00
|
|
|
if( fd>0 ) closesocket( fd );
|
2001-06-04 17:54:59 +00:00
|
|
|
|
2002-01-14 01:12:44 +00:00
|
|
|
if( !strcasecmp( url->protocol, "http_proxy" ) ) {
|
2002-01-12 21:08:12 +00:00
|
|
|
if( url->port==0 ) url->port = 8080;
|
|
|
|
} else {
|
|
|
|
if( url->port==0 ) url->port = 80;
|
|
|
|
}
|
2003-08-15 19:13:23 +00:00
|
|
|
fd = connect2Server( url->hostname, url->port, 1);
|
2002-09-03 01:32:13 +00:00
|
|
|
if( fd<0 ) return fd;
|
2001-05-29 17:09:19 +00:00
|
|
|
|
2001-12-11 01:07:40 +00:00
|
|
|
http_hdr = asf_http_request( stream->streaming_ctrl );
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
|
2002-10-29 09:18:53 +00:00
|
|
|
for(i=0; i < (int)http_hdr->buffer_size ; ) {
|
2003-05-29 19:36:58 +00:00
|
|
|
int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
|
2001-12-14 20:45:30 +00:00
|
|
|
if(r <0) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
|
2006-12-09 19:12:51 +00:00
|
|
|
goto err_out;
|
2001-12-14 20:45:30 +00:00
|
|
|
}
|
|
|
|
i += r;
|
2009-05-13 02:58:57 +00:00
|
|
|
}
|
2001-12-16 21:23:36 +00:00
|
|
|
http_free( http_hdr );
|
2001-05-29 17:09:19 +00:00
|
|
|
http_hdr = http_new_header();
|
|
|
|
do {
|
2003-05-29 19:36:58 +00:00
|
|
|
i = recv( fd, buffer, BUFFER_SIZE, 0 );
|
2002-01-12 21:08:12 +00:00
|
|
|
//printf("read: %d\n", i );
|
2002-09-07 20:38:44 +00:00
|
|
|
if( i<=0 ) {
|
2001-06-04 17:54:59 +00:00
|
|
|
perror("read");
|
2006-12-09 19:12:51 +00:00
|
|
|
goto err_out;
|
2001-06-04 17:54:59 +00:00
|
|
|
}
|
2001-05-29 17:09:19 +00:00
|
|
|
http_response_append( http_hdr, buffer, i );
|
2001-10-26 18:26:06 +00:00
|
|
|
} while( !http_is_header_entire( http_hdr ) );
|
2006-03-24 08:12:03 +00:00
|
|
|
if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
|
2001-12-16 21:23:36 +00:00
|
|
|
http_hdr->buffer[http_hdr->buffer_size]='\0';
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
2002-10-29 09:18:53 +00:00
|
|
|
ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
|
|
|
|
if( ret<0 ) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
|
2006-12-09 19:12:51 +00:00
|
|
|
goto err_out;
|
2001-05-29 17:09:19 +00:00
|
|
|
}
|
2002-10-29 09:18:53 +00:00
|
|
|
switch( asf_http_ctrl->streaming_type ) {
|
2001-05-29 17:09:19 +00:00
|
|
|
case ASF_Live_e:
|
|
|
|
case ASF_Prerecorded_e:
|
2001-10-26 18:26:06 +00:00
|
|
|
case ASF_PlainText_e:
|
2001-06-04 17:54:59 +00:00
|
|
|
if( http_hdr->body_size>0 ) {
|
2001-11-20 22:20:20 +00:00
|
|
|
if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
|
2006-12-09 19:12:51 +00:00
|
|
|
goto err_out;
|
2001-06-04 17:54:59 +00:00
|
|
|
}
|
2001-05-29 17:09:19 +00:00
|
|
|
}
|
2001-12-11 01:07:40 +00:00
|
|
|
if( asf_http_ctrl->request==1 ) {
|
2002-10-29 09:18:53 +00:00
|
|
|
if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
|
2001-12-16 21:23:36 +00:00
|
|
|
// First request, we only got the ASF header.
|
|
|
|
ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
|
2006-12-09 19:12:51 +00:00
|
|
|
if(ret < 0) goto err_out;
|
2001-12-16 21:23:36 +00:00
|
|
|
if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
|
2006-12-09 19:12:51 +00:00
|
|
|
goto err_out;
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
2002-01-23 08:50:30 +00:00
|
|
|
asf_http_ctrl->request++;
|
|
|
|
done = 0;
|
|
|
|
} else {
|
|
|
|
done = 1;
|
2001-12-16 21:23:36 +00:00
|
|
|
}
|
2001-12-11 01:07:40 +00:00
|
|
|
}
|
2001-05-29 17:09:19 +00:00
|
|
|
break;
|
|
|
|
case ASF_Redirector_e:
|
2002-01-10 01:42:31 +00:00
|
|
|
if( http_hdr->body_size>0 ) {
|
|
|
|
if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
|
2006-12-09 19:12:51 +00:00
|
|
|
goto err_out;
|
2002-01-10 01:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-30 17:10:36 +00:00
|
|
|
*demuxer_type = DEMUXER_TYPE_PLAYLIST;
|
2002-01-10 01:42:31 +00:00
|
|
|
done = 1;
|
|
|
|
break;
|
2002-07-09 08:26:34 +00:00
|
|
|
case ASF_Authenticate_e:
|
|
|
|
if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
|
|
|
|
asf_http_ctrl->streaming_type = ASF_Unknown_e;
|
|
|
|
done = 0;
|
|
|
|
break;
|
2001-05-29 17:09:19 +00:00
|
|
|
case ASF_Unknown_e:
|
|
|
|
default:
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
|
2006-12-09 19:12:51 +00:00
|
|
|
goto err_out;
|
2001-05-29 17:09:19 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
// Check if we got a redirect.
|
2001-05-29 17:09:19 +00:00
|
|
|
} while(!done);
|
|
|
|
|
2002-01-10 01:42:31 +00:00
|
|
|
stream->fd = fd;
|
2002-10-29 09:18:53 +00:00
|
|
|
if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
|
2001-11-20 22:20:20 +00:00
|
|
|
stream->streaming_ctrl->streaming_read = nop_streaming_read;
|
|
|
|
stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
|
2001-10-26 18:26:06 +00:00
|
|
|
} else {
|
2001-11-20 22:20:20 +00:00
|
|
|
stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
|
|
|
|
stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
|
2002-01-20 21:38:11 +00:00
|
|
|
stream->streaming_ctrl->buffering = 1;
|
2001-10-26 18:26:06 +00:00
|
|
|
}
|
2001-11-20 22:20:20 +00:00
|
|
|
stream->streaming_ctrl->status = streaming_playing_e;
|
2005-05-29 12:54:00 +00:00
|
|
|
stream->close = close_s;
|
2001-06-04 17:54:59 +00:00
|
|
|
|
|
|
|
http_free( http_hdr );
|
2002-01-10 01:42:31 +00:00
|
|
|
return 0;
|
2006-12-09 19:12:51 +00:00
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (fd > 0)
|
|
|
|
closesocket(fd);
|
|
|
|
stream->fd = -1;
|
|
|
|
http_free(http_hdr);
|
|
|
|
return -1;
|
2001-05-29 17:09:19 +00:00
|
|
|
}
|
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
|
|
|
|
URL_t *url;
|
|
|
|
|
|
|
|
stream->streaming_ctrl = streaming_ctrl_new();
|
|
|
|
if( stream->streaming_ctrl==NULL ) {
|
|
|
|
return STREAM_ERROR;
|
|
|
|
}
|
|
|
|
stream->streaming_ctrl->bandwidth = network_bandwidth;
|
|
|
|
url = url_new(stream->url);
|
|
|
|
stream->streaming_ctrl->url = check4proxies(url);
|
2005-09-06 21:08:36 +00:00
|
|
|
url_free(url);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-11-06 15:25:08 +00:00
|
|
|
mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
|
2005-05-29 12:54:00 +00:00
|
|
|
if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
|
|
|
|
streaming_ctrl_free(stream->streaming_ctrl);
|
|
|
|
stream->streaming_ctrl = NULL;
|
2007-08-28 22:38:45 +00:00
|
|
|
return STREAM_UNSUPPORTED;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(asf_streaming_start(stream, file_format) < 0) {
|
2005-10-31 23:37:41 +00:00
|
|
|
mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
|
2005-05-29 12:54:00 +00:00
|
|
|
streaming_ctrl_free(stream->streaming_ctrl);
|
|
|
|
stream->streaming_ctrl = NULL;
|
2007-08-28 22:38:45 +00:00
|
|
|
return STREAM_UNSUPPORTED;
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2007-12-15 19:16:49 +00:00
|
|
|
if (*file_format != DEMUXER_TYPE_PLAYLIST)
|
2007-12-15 19:17:36 +00:00
|
|
|
*file_format = DEMUXER_TYPE_ASF;
|
2005-05-29 12:54:00 +00:00
|
|
|
stream->type = STREAMTYPE_STREAM;
|
|
|
|
fixup_network_stream_cache(stream);
|
|
|
|
return STREAM_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-02 13:22:53 +00:00
|
|
|
const stream_info_t stream_info_asf = {
|
2005-05-29 12:54:00 +00:00
|
|
|
"mms and mms over http streaming",
|
|
|
|
"null",
|
|
|
|
"Bertrand, Reimar Doeffinger, Albeu",
|
|
|
|
"originally based on work by Majormms (is that code still there?)",
|
|
|
|
open_s,
|
2009-03-26 11:00:04 +00:00
|
|
|
{"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL},
|
2005-05-29 12:54:00 +00:00
|
|
|
NULL,
|
|
|
|
0 // Urls are an option string
|
|
|
|
};
|
|
|
|
|