From 854303ad49d188d96af8151b290162916c81c993 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 7 Jul 2013 19:40:14 +0200 Subject: Remove internal network support This commit removes the "old" networking code in favor of libavformat's code. The code was still used for mp_http, udp, ftp, cddb. http has been mapped to libavformat's http support since approximately 6 months ago. udp and ftp have support in ffmpeg (though ftp was added only last month). cddb support is removed with this commit - it's probably not important and rarely used if at all, so we don't care about it. --- stream/asf_mmst_streaming.c | 686 ------------------------------- stream/asf_mmst_streaming.h | 26 -- stream/asf_streaming.c | 855 --------------------------------------- stream/cookies.c | 86 +--- stream/cookies.h | 4 - stream/http.c | 966 -------------------------------------------- stream/http.h | 70 ---- stream/network.c | 455 --------------------- stream/network.h | 84 ---- stream/stream.c | 76 +--- stream/stream.h | 29 -- stream/stream_cdda.c | 32 +- stream/stream_cddb.c | 902 ----------------------------------------- stream/stream_ftp.c | 520 ------------------------ stream/stream_lavf.c | 3 +- stream/stream_udp.c | 105 ----- stream/tcp.c | 279 ------------- stream/tcp.h | 35 -- stream/udp.c | 201 --------- stream/udp.h | 30 -- stream/url.c | 506 ----------------------- stream/url.h | 55 --- 22 files changed, 9 insertions(+), 5996 deletions(-) delete mode 100644 stream/asf_mmst_streaming.c delete mode 100644 stream/asf_mmst_streaming.h delete mode 100644 stream/asf_streaming.c delete mode 100644 stream/http.c delete mode 100644 stream/http.h delete mode 100644 stream/network.c delete mode 100644 stream/network.h delete mode 100644 stream/stream_cddb.c delete mode 100644 stream/stream_ftp.c delete mode 100644 stream/stream_udp.c delete mode 100644 stream/tcp.c delete mode 100644 stream/tcp.h delete mode 100644 stream/udp.c delete mode 100644 stream/udp.h delete mode 100644 stream/url.c delete mode 100644 stream/url.h (limited to 'stream') diff --git a/stream/asf_mmst_streaming.c b/stream/asf_mmst_streaming.c deleted file mode 100644 index 98c386d11d..0000000000 --- a/stream/asf_mmst_streaming.c +++ /dev/null @@ -1,686 +0,0 @@ -/* - * MMST implementation taken from the xine-mms plugin made by - * Major MMS (http://geocities.com/majormms/). - * Ported to MPlayer by Abhijeet Phatak . - * - * Information about the MMS protocol can be found at http://get.to/sdp - * - * copyright (C) 2002 Abhijeet Phatak - * copyright (C) 2002 the xine project - * copyright (C) 2000-2001 major mms - * - * 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. - */ - - -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "core/options.h" -#include "core/mp_msg.h" - -#if HAVE_WINSOCK2_H -#include -#endif - -#ifdef CONFIG_ICONV -#include -#endif - -#include "url.h" -#include "demux/asf.h" - -#include "stream.h" -#include "asf_mmst_streaming.h" -#include "network.h" -#include "tcp.h" - -extern int audio_id; -extern int video_id; - -#define BUF_SIZE 102400 -#define HDR_BUF_SIZE 8192 -#define MAX_STREAMS 20 - -typedef struct -{ - uint8_t buf[BUF_SIZE]; - int num_bytes; - -} command_t; - -static int seq_num; -static int num_stream_ids; -static int stream_ids[MAX_STREAMS]; - -static int get_data (int s, char *buf, size_t count); - -static void put_32 (command_t *cmd, uint32_t value) -{ - cmd->buf[cmd->num_bytes ] = value % 256; - value = value >> 8; - cmd->buf[cmd->num_bytes+1] = value % 256 ; - value = value >> 8; - cmd->buf[cmd->num_bytes+2] = value % 256 ; - value = value >> 8; - cmd->buf[cmd->num_bytes+3] = value % 256 ; - - cmd->num_bytes += 4; -} - -static uint32_t get_32 (unsigned char *cmd, int offset) -{ - uint32_t ret; - - ret = cmd[offset] ; - ret |= cmd[offset+1]<<8 ; - ret |= cmd[offset+2]<<16 ; - ret |= cmd[offset+3]<<24 ; - - return ret; -} - -static void send_command (int s, int command, uint32_t switches, - uint32_t extra, int length, - char *data) -{ - command_t cmd; - int len8; - - len8 = (length + 7) / 8; - - cmd.num_bytes = 0; - - put_32 (&cmd, 0x00000001); /* start sequence */ - put_32 (&cmd, 0xB00BFACE); /* #-)) */ - put_32 (&cmd, len8*8 + 32); - put_32 (&cmd, 0x20534d4d); /* protocol type "MMS " */ - put_32 (&cmd, len8 + 4); - put_32 (&cmd, seq_num); - seq_num++; - put_32 (&cmd, 0x0); /* unknown */ - put_32 (&cmd, 0x0); - put_32 (&cmd, len8+2); - put_32 (&cmd, 0x00030000 | command); /* dir | command */ - put_32 (&cmd, switches); - put_32 (&cmd, extra); - - memcpy (&cmd.buf[48], data, length); - if (length & 7) - memset(&cmd.buf[48 + length], 0, 8 - (length & 7)); - - if (send (s, cmd.buf, len8*8+48, 0) != (len8*8+48)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"write error\n"); - } -} - -#ifdef CONFIG_ICONV -static iconv_t url_conv; -#endif - -static void string_utf16(char *dest, char *src, int len) { - int i; -#ifdef CONFIG_ICONV - size_t len1, len2; - char *ip, *op; - - if (url_conv != (iconv_t)(-1)) - { - memset(dest, 0, 1000); - len1 = len; len2 = 1000; - ip = src; op = dest; - - iconv(url_conv, &ip, &len1, &op, &len2); - } - else - { -#endif - if (len > 499) len = 499; - for (i=0; i HDR_BUF_SIZE - header_len) { - mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid header size, giving up.\n"); - return 0; - } - - if (!get_data (s, &header[header_len], packet_len)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Header data read failed.\n"); - return 0; - } - - header_len += packet_len; - - if ( (header[header_len-1] == 1) && (header[header_len-2]==1)) { - - - if( streaming_bufferize( streaming_ctrl, header, header_len )<0 ) { - return -1; - } - - // mp_msg(MSGT_NETWORK,MSGL_INFO,"get header packet finished\n"); - - return header_len; - - } - - } else { - - int32_t packet_len; - int command; - char data[BUF_SIZE]; - - if (!get_data (s, (char*)&packet_len, 4)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"packet_len read failed.\n"); - return 0; - } - - packet_len = get_32 ((unsigned char*)&packet_len, 0) + 4; - -// mp_msg(MSGT_NETWORK,MSGL_INFO,"command packet detected, len=%d\n", packet_len); - - if (packet_len < 0 || packet_len > BUF_SIZE) { - mp_tmsg(MSGT_NETWORK, MSGL_FATAL, - "Invalid RTSP packet size, giving up.\n"); - return 0; - } - - if (!get_data (s, data, packet_len)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Command data read failed.\n"); - return 0; - } - - command = get_32 (data, 24) & 0xFFFF; - -// mp_msg(MSGT_NETWORK,MSGL_INFO,"command: %02x\n", command); - - if (command == 0x1b) - send_command (s, 0x1b, 0, 0, 0, data); - - } - -// mp_msg(MSGT_NETWORK,MSGL_INFO,"get header packet succ\n"); - } -} - -static int interp_header (uint8_t *header, int header_len) -{ - int i; - int packet_length=-1; - - /* - * parse header - */ - - i = 30; - while (i BUF_SIZE) { - mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid RTSP packet size, giving up.\n"); - return 0; - } - - if (!get_data (s, data, packet_len)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Media data read failed.\n"); - return 0; - } - - streaming_bufferize(stream_ctrl, data, padding); - - } else { - - int32_t packet_len; - int command; - - if (!get_data (s, (char*)&packet_len, 4)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"packet_len read failed.\n"); - return 0; - } - - packet_len = get_32 ((unsigned char*)&packet_len, 0) + 4; - - if (packet_len < 0 || packet_len > BUF_SIZE) { - mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Invalid RTSP packet size, giving up.\n"); - return 0; - } - - if (!get_data (s, data, packet_len)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Command data read failed.\n"); - return 0; - } - - if ( (pre_header[7] != 0xb0) || (pre_header[6] != 0x0b) - || (pre_header[5] != 0xfa) || (pre_header[4] != 0xce) ) { - - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"missing signature\n"); - return -1; - } - - command = get_32 (data, 24) & 0xFFFF; - -// mp_msg(MSGT_NETWORK,MSGL_INFO,"\ncommand packet detected, len=%d cmd=0x%X\n", packet_len, command); - - if (command == 0x1b) - send_command (s, 0x1b, 0, 0, 0, data); - else if (command == 0x1e) { - mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Everything done. Thank you for downloading a media file containing proprietary and patented technology.\n"); - return 0; - } - else if (command == 0x21 ) { - // Looks like it's new in WMS9 - // Unknown command, but ignoring it seems to work. - return 0; - } - else if (command != 0x05) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown command %02x\n",command); - return -1; - } - } - -// mp_msg(MSGT_NETWORK,MSGL_INFO,"get media packet succ\n"); - - return 1; -} - - -static int packet_length1; - -static int asf_mmst_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) -{ - int len; - - while( stream_ctrl->buffer_size==0 ) { - // buffer is empty - fill it! - int ret = get_media_packet( fd, packet_length1, stream_ctrl); - if( ret<0 ) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"get_media_packet error : %s\n",strerror(errno)); - return -1; - } else if (ret==0) //EOF? - return ret; - } - - len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos; - if(len>size) len=size; - memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len ); - stream_ctrl->buffer_pos += len; - if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) { - free( stream_ctrl->buffer ); - stream_ctrl->buffer = NULL; - stream_ctrl->buffer_size = 0; - stream_ctrl->buffer_pos = 0; - } - return len; - -} - -static int asf_mmst_streaming_seek( int fd, int64_t pos, streaming_ctrl_t *streaming_ctrl ) -{ - return -1; - // Shut up gcc warning - fd++; - pos++; - streaming_ctrl=NULL; -} - -int asf_mmst_streaming_start(stream_t *stream) -{ - char str[1024]; - char data[BUF_SIZE]; - uint8_t asf_header[HDR_BUF_SIZE]; - int asf_header_len; - int i, packet_length; - char *path, *unescpath; - URL_t *url1 = stream->streaming_ctrl->url; - int s = stream->fd; - - if( s>0 ) { - closesocket( stream->fd ); - stream->fd = -1; - } - - /* parse url */ - path = strchr(url1->file,'/') + 1; - - /* mmst filename are not url_escaped by MS MediaPlayer and are expected as - * "plain text" by the server, so need to decode it here - */ - unescpath=malloc(strlen(path)+1); - if (!unescpath) { - mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n"); - return -1; - } - url_unescape_string(unescpath,path); - path=unescpath; - - - if( url1->port==0 ) { - url1->port=1755; - } - s = connect2Server( url1->hostname, url1->port, 1); - if( s<0 ) { - free(path); - return s; - } - mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Connected\n"); - - seq_num=0; - - /* - * Send the initial connect info including player version no. Client GUID (random) and the host address being connected to. - * This command is sent at the very start of protocol initiation. It sends local information to the serve - * cmd 1 0x01 - * */ - - /* prepare for the url encoding conversion */ -#ifdef CONFIG_ICONV - url_conv = iconv_open("UTF-16LE", "UTF-8"); -#endif - - snprintf (str, 1023, "\034\003NSPlayer/7.0.0.1956; {33715801-BAB3-9D85-24E9-03B90328270A}; Host: %s", url1->hostname); - string_utf16 (data, str, strlen(str)); -// send_command(s, commandno ....) - send_command (s, 1, 0, 0x0004000b, strlen(str)*2+2, data); - - recv (s, data, BUF_SIZE, 0) ; - - /*This sends details of the local machine IP address to a Funnel system at the server. - * Also, the TCP or UDP transport selection is sent. - * - * here 192.168.0.1 is local ip address TCP/UDP states the tronsport we r using - * and 1037 is the local TCP or UDP socket number - * cmd 2 0x02 - * */ - - string_utf16 (&data[8], "\002\000\\\\192.168.0.1\\TCP\\1037", 24); - memset (data, 0, 8); - send_command (s, 2, 0, 0, 24*2+10, data); - - recv (s, data, BUF_SIZE, 0) ; - - /* This command sends file path (at server) and file name request to the server. - * 0x5 */ - - string_utf16 (&data[8], path, strlen(path)); - memset (data, 0, 8); - send_command (s, 5, 0, 0, strlen(path)*2+10, data); - free(path); - - get_answer (s); - - /* The ASF header chunk request. Includes ?session' variable for pre header value. - * After this command is sent, - * the server replies with 0x11 command and then the header chunk with header data follows. - * 0x15 */ - - memset (data, 0, 40); - data[32] = 2; - - send_command (s, 0x15, 1, 0, 40, data); - - num_stream_ids = 0; - /* get_headers(s, asf_header); */ - - asf_header_len = get_header (s, asf_header, stream->streaming_ctrl); -// mp_msg(MSGT_NETWORK,MSGL_INFO,"---------------------------------- asf_header %d\n",asf_header); - if (asf_header_len==0) { //error reading header - closesocket(s); - return -1; - } - packet_length = interp_header (asf_header, asf_header_len); - - - /* - * This command is the media stream MBR selector. Switches are always 6 bytes in length. - * After all switch elements, data ends with bytes [00 00] 00 20 ac 40 [02]. - * Where: - * [00 00] shows 0x61 0x00 (on the first 33 sent) or 0xff 0xff for ASF files, and with no ending data for WMV files. - * It is not yet understood what all this means. - * And the last [02] byte is probably the header ?session' value. - * - * 0x33 */ - - memset (data, 0, 40); - - int audio_id = stream->opts->audio_id; - if (audio_id > 0) { - data[2] = 0xFF; - data[3] = 0xFF; - data[4] = audio_id; - send_command(s, 0x33, num_stream_ids, 0xFFFF | audio_id << 16, 8, data); - } else { - for (i=1; ifd = s; - stream->streaming_ctrl->streaming_read = asf_mmst_streaming_read; - stream->streaming_ctrl->streaming_seek = asf_mmst_streaming_seek; - stream->streaming_ctrl->status = streaming_playing_e; - stream->streaming = true; - - packet_length1 = packet_length; - mp_msg(MSGT_NETWORK,MSGL_INFO,"mmst packet_length = %d\n", packet_length); - -#ifdef CONFIG_ICONV - if (url_conv != (iconv_t)(-1)) - iconv_close(url_conv); -#endif - - return 0; -} diff --git a/stream/asf_mmst_streaming.h b/stream/asf_mmst_streaming.h deleted file mode 100644 index 204cf0a450..0000000000 --- a/stream/asf_mmst_streaming.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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. - */ - -#ifndef MPLAYER_ASF_MMST_STREAMING_H -#define MPLAYER_ASF_MMST_STREAMING_H - -#include "stream.h" - -int asf_mmst_streaming_start(stream_t *stream); - -#endif /* MPLAYER_ASF_MMST_STREAMING_H */ diff --git a/stream/asf_streaming.c b/stream/asf_streaming.c deleted file mode 100644 index 862d7c3177..0000000000 --- a/stream/asf_streaming.c +++ /dev/null @@ -1,855 +0,0 @@ -/* - * 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. - */ - -#include -#include -#include -#include -#include -#include - -#include - -#include "config.h" -#include "core/mp_msg.h" -#include "core/options.h" - -#if HAVE_WINSOCK2_H -#include -#endif - -#include "url.h" -#include "http.h" -#include "demux/asf.h" - -#include "stream.h" -#include "demux/demux.h" -#include "asf_mmst_streaming.h" -#include "network.h" -#include "tcp.h" - -#include "demux/asfguid.h" - -static int asf_http_streaming_start(stream_t *stream, int *demuxer_type); - -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_tmsg(MSGT_NETWORK, MSGL_ERR, "Error while reading network stream.\n"); - return got; - } - buf += got; - len -= got; - } - return 1; -} - -// We can try several protocol for asf streaming -// * First 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. -// -// Note: Using WMP sequence MMST and then HTTP. - -static int asf_streaming_start( stream_t *stream, int *demuxer_type) { - char *proto = stream->streaming_ctrl->url->protocol; - int fd = -1; - int port = stream->streaming_ctrl->url->port; - - //Is protocol mms or mmst? - if (!strcasecmp(proto, "mp_mmst") || !strcasecmp(proto, "mp_mms")) - { - mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n"); - fd = asf_mmst_streaming_start( stream ); - stream->streaming_ctrl->url->port = port; - if( fd>-1 ) return fd; - mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n"); - if( fd==-2 ) return -1; - } - - //Is protocol http, http_proxy, or mms? - if (!strcasecmp(proto, "mp_http_proxy") || !strcasecmp(proto, "mp_http") || - !strcasecmp(proto, "mp_mms") || !strcasecmp(proto, "mp_mmsh") || - !strcasecmp(proto, "mp_mmshttp")) - { - mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n"); - fd = asf_http_streaming_start( stream, demuxer_type ); - stream->streaming_ctrl->url->port = port; - if( fd>-1 ) return fd; - mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n"); - if( fd==-2 ) return -1; - } - - //everything failed - return -1; -} - -static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) { -/* -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 ) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Ahhhh, stream_chunck size is too small: %d\n", stream_chunck->size); - return -1; - } - if( stream_chunck->size!=stream_chunck->size_confirm ) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"size_confirm mismatch!: %d %d\n", stream_chunck->size, stream_chunck->size_confirm); - return -1; - } -/* - 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) { - case ASF_STREAMING_CLEAR: // $C Clear ASF configuration - mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n"); - if( drop_packet!=NULL ) *drop_packet = 1; - return stream_chunck->size; - break; - case ASF_STREAMING_DATA: // $D Data follows -// printf("=====> Data follows\n"); - break; - case ASF_STREAMING_END_TRANS: // $E Transfer complete - mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n"); - if( drop_packet!=NULL ) *drop_packet = 1; - return stream_chunck->size; - break; - case ASF_STREAMING_HEADER: // $H ASF header chunk follows - mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n"); - break; - default: - mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type ); - } - return stream_chunck->size+4; -} - -static void close_s(stream_t *stream) { - closesocket(stream->fd); - stream->fd=-1; -} - -static int max_idx(int s_count, int *s_rates, int bound) { - int i, best = -1, rate = -1; - for (i = 0; i < s_count; i++) { - if (s_rates[i] > rate && s_rates[i] <= bound) { - rate = s_rates[i]; - best = i; - } - } - return best; -} - -static int asf_streaming_parse_header(stream_t *s, int fd) { - streaming_ctrl_t* streaming_ctrl = s->streaming_ctrl; - ASF_stream_chunck_t chunk; - asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data; - char* buffer=NULL, *chunk_buffer=NULL; - int i,r,size,pos = 0; - int start; - int buffer_size = 0; - int chunk_size2read = 0; - 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; - - 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 { - if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0) - return -1; - // Endian handling of the stream chunk - le2me_ASF_stream_chunck_t(&chunk); - size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t); - if(r) mp_tmsg(MSGT_NETWORK,MSGL_WARN,"Warning: drop header ????\n"); - if(size < 0){ - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n"); - return -1; - } - if (chunk.type != ASF_STREAMING_HEADER) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Didn't get a header as first chunk !!!!\n"); - return -1; - } - - // audit: do not overflow buffer_size - if (size > SIZE_MAX - buffer_size) return -1; - buffer = malloc(size+buffer_size); - if(buffer == NULL) { - mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Error: Can't allocate %d bytes buffer.\n",size+buffer_size); - return -1; - } - if( chunk_buffer!=NULL ) { - memcpy( buffer, chunk_buffer, buffer_size ); - free( chunk_buffer ); - } - chunk_buffer = buffer; - buffer += buffer_size; - buffer_size += size; - - if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0) - return -1; - - if( chunk_size2read==0 ) { - ASF_header_t *asfh = (ASF_header_t *)buffer; - if(size < (int)sizeof(ASF_header_t)) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: Chunk is too small.\n"); - return -1; - } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n"); - chunk_size2read = AV_RL64(&asfh->objh.size); - mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read); - } - } while( buffer_size= 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; -/* - if(fileh.packetsize != fileh.packetsize2) { - printf("Error packetsize check don't match\n"); - return -1; - } -*/ - asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size); - // before playing. - // preroll: time in ms to bufferize before playing - unsigned int preroll = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0)); - // buffer in KBytes, *5 assuming the prefill is 20% of the buffer. - s->cache_size = preroll / 1024 * 5; - } - - 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)) { - case 0xF8699E40 : // audio stream - if(asf_ctrl->audio_streams == NULL){ - asf_ctrl->audio_streams = malloc(sizeof(int)); - asf_ctrl->n_audio = 1; - } else { - asf_ctrl->n_audio++; - asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams, - asf_ctrl->n_audio*sizeof(int)); - } - asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no); - break; - case 0xBC19EFC0 : // video stream - if(asf_ctrl->video_streams == NULL){ - asf_ctrl->video_streams = malloc(sizeof(int)); - asf_ctrl->n_video = 1; - } else { - asf_ctrl->n_video++; - asf_ctrl->video_streams = realloc(asf_ctrl->video_streams, - asf_ctrl->n_video*sizeof(int)); - } - asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no); - break; - } - } - - // 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]; - char *end = &buffer[size]; - - mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n"); - if (ptr + 2 > end) goto len_err_out; - stream_count = AV_RL16(ptr); - ptr += 2; - mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n", - stream_count, stream_count ); - for( i=0 ; i end) goto len_err_out; - id = AV_RL16(ptr); - ptr += 2; - rate = AV_RL32(ptr); - ptr += 4; - 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; - } - } - } - } - free(buffer); - - // 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); - - free(v_rates); - free(a_rates); - - if (a_idx < 0 && v_idx < 0) { - mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Bandwidth too small, file cannot be played!\n"); - return -1; - } - - if (*streaming_ctrl->audio_id_ptr > 0) - // a audio stream was forced - asf_ctrl->audio_id = *streaming_ctrl->audio_id_ptr; - else if (a_idx >= 0) - asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx]; - else if (asf_ctrl->n_audio) { - mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected audio stream.\n"); - *streaming_ctrl->audio_id_ptr = -2; - } - - if (*streaming_ctrl->video_id_ptr > 0) - // a video stream was forced - asf_ctrl->video_id = *streaming_ctrl->video_id_ptr; - else if (v_idx >= 0) - asf_ctrl->video_id = asf_ctrl->video_streams[v_idx]; - else if (asf_ctrl->n_video) { - mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected video stream.\n"); - *streaming_ctrl->video_id_ptr = -2; - } - - return 1; - -len_err_out: - mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid length in ASF header!\n"); - free(buffer); - free(v_rates); - free(a_rates); - return -1; -} - -static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) { - static ASF_stream_chunck_t chunk; - int read,chunk_size = 0; - static int rest = 0, drop_chunk = 0, waiting = 0; - asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data; - - while(1) { - if (rest == 0 && waiting == 0) { - if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0) - return -1; - - // Endian handling of the stream chunk - le2me_ASF_stream_chunck_t(&chunk); - chunk_size = asf_streaming( &chunk, &drop_chunk ); - if(chunk_size < 0) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n"); - return -1; - } - chunk_size -= sizeof(ASF_stream_chunck_t); - - if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) { - if (asf_http_ctrl->packet_size < chunk_size) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: chunk_size > packet_size\n"); - return -1; - } - waiting = asf_http_ctrl->packet_size; - } 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; - } - if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0) - return -1; - read = chunk_size; - waiting -= read; - if (drop_chunk) continue; - } - if (rest == 0 && waiting > 0 && size-read > 0) { - int s = FFMIN(waiting,size-read); - memset(buffer+read,0,s); - waiting -= s; - read += s; - } - break; - } - - return read; -} - -static int asf_http_streaming_seek( int fd, int64_t pos, streaming_ctrl_t *streaming_ctrl ) { - return -1; - // to shut up gcc warning - fd++; - pos++; - streaming_ctrl=NULL; -} - -static int asf_header_check( HTTP_header_t *http_hdr ) { - ASF_obj_header_t *objh; - if( http_hdr==NULL ) return -1; - if( http_hdr->body==NULL || http_hdr->body_sizebody; - if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0; - return -1; -} - -static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) { - if( content_type==NULL ) return ASF_Unknown_e; - if( !strcasecmp(content_type, "application/octet-stream") || - !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request - !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request - !strcasecmp(content_type, "video/x-ms-wmv") || - !strcasecmp(content_type, "video/x-ms-asf")) { - - if( strstr(features, "broadcast") ) { - mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n"); - return ASF_Live_e; - } else { - mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n"); - return ASF_Prerecorded_e; - } - } else { - // 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 - if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) { - if( asf_header_check( http_hdr )==0 ) { - mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n"); - return ASF_PlainText_e; - } else if( (!strcasecmp(content_type, "text/html")) ) { - mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n"); - return ASF_Unknown_e; - } else { - mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n"); - return ASF_Redirector_e; - } - } else { - 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")) ) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"=====> ASF Redirector\n"); - return ASF_Redirector_e; - } else if( !strcasecmp(content_type, "text/plain") ) { - mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n"); - return ASF_PlainText_e; - } else { - mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type ); - return ASF_Unknown_e; - } - } - } - return ASF_Unknown_e; -} - -static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) { - HTTP_header_t *http_hdr; - URL_t *url = NULL; - URL_t *server_url = NULL; - asf_http_streaming_ctrl_t *asf_http_ctrl; - char str[250]; - char *ptr; - int i, enable; - - int offset_hi=0, offset_lo=0, length=0; - int asf_nb_stream=0, stream_id; - - // 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; - - // 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" ); - http_add_basic_authentication( http_hdr, url->username, url->password ); - - // Check if we are using a proxy - if( !strcasecmp( url->protocol, "mp_http_proxy" ) ) { - server_url = url_new( (url->file)+1 ); - if( server_url==NULL ) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"invalid proxy URL\n"); - http_free( http_hdr ); - return NULL; - } - http_set_uri( http_hdr, server_url->url ); - sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port ); - url_free( server_url ); - } else { - http_set_uri( http_hdr, url->file ); - sprintf( str, "Host: %.220s:%d", url->hostname, url->port ); - } - - http_set_field( http_hdr, str ); - http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" ); - sprintf(str, - "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u", - offset_hi, offset_lo, asf_http_ctrl->request, length ); - http_set_field( http_hdr, str ); - - switch( asf_http_ctrl->streaming_type ) { - case ASF_Live_e: - case ASF_Prerecorded_e: - http_set_field( http_hdr, "Pragma: xPlayStrm=1" ); - ptr = str; - ptr += sprintf( ptr, "Pragma: stream-switch-entry="); - if(asf_http_ctrl->n_audio > 0) { - for( i=0; in_audio ; i++ ) { - stream_id = asf_http_ctrl->audio_streams[i]; - if(stream_id == asf_http_ctrl->audio_id) { - enable = 0; - } else { - enable = 2; - continue; - } - asf_nb_stream++; - ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable); - } - } - if(asf_http_ctrl->n_video > 0) { - for( i=0; in_video ; i++ ) { - stream_id = asf_http_ctrl->video_streams[i]; - if(stream_id == asf_http_ctrl->video_id) { - enable = 0; - } else { - enable = 2; - continue; - } - asf_nb_stream++; - ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable); - } - } - http_set_field( http_hdr, str ); - 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: - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF stream type\n"); - } - - http_set_field( http_hdr, "Connection: Close" ); - http_build_request( http_hdr ); - - return http_hdr; -} - -static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) { - char *content_type, *pragma; - char features[64] = "\0"; - size_t len; - if( http_response_parse(http_hdr)<0 ) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse HTTP response.\n"); - return -1; - } - switch( http_hdr->status_code ) { - case 200: - break; - case 401: // Authentication required - return ASF_Authenticate_e; - default: - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returned %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase); - return -1; - } - - content_type = http_get_field( http_hdr, "Content-Type"); -//printf("Content-Type: [%s]\n", content_type); - - pragma = http_get_field( http_hdr, "Pragma"); - while( pragma!=NULL ) { - char *comma_ptr=NULL; - char *end; -//printf("Pragma: [%s]\n", pragma ); - // The pragma line can get severals attributes - // separeted with a comma ','. - do { - if( !strncasecmp( pragma, "features=", 9) ) { - pragma += 9; - end = strstr( pragma, "," ); - if( end==NULL ) { - len = strlen(pragma); - } else { - len = (unsigned int)(end-pragma); - } - if(len > sizeof(features) - 1) { - mp_tmsg(MSGT_NETWORK,MSGL_WARN,"ASF HTTP PARSE WARNING : Pragma %s cut from %zd bytes to %zd\n",pragma,len,sizeof(features) - 1); - len = sizeof(features) - 1; - } - 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 ); - } - asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr ); - return 0; -} - -static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) { - HTTP_header_t *http_hdr=NULL; - URL_t *url = stream->streaming_ctrl->url; - asf_http_streaming_ctrl_t *asf_http_ctrl; - char buffer[BUFFER_SIZE]; - int i, ret; - int fd = stream->fd; - int done; - int auth_retry = 0; - - asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t)); - if( asf_http_ctrl==NULL ) { - mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n"); - return -1; - } - asf_http_ctrl->streaming_type = ASF_Unknown_e; - asf_http_ctrl->request = 1; - asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL; - asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0; - stream->streaming_ctrl->data = (void*)asf_http_ctrl; - - do { - done = 1; - if( fd>0 ) closesocket( fd ); - - if( !strcasecmp( url->protocol, "mp_http_proxy" ) ) { - if( url->port==0 ) url->port = 8080; - } else { - if( url->port==0 ) url->port = 80; - } - fd = connect2Server( url->hostname, url->port, 1); - if( fd<0 ) return fd; - - http_hdr = asf_http_request( stream->streaming_ctrl ); - mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer ); - for(i=0; i < (int)http_hdr->buffer_size ; ) { - int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, DEFAULT_SEND_FLAGS ); - if(r <0) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"socket write error: %s\n",strerror(errno)); - goto err_out; - } - i += r; - } - http_free( http_hdr ); - http_hdr = http_new_header(); - do { - i = recv( fd, buffer, BUFFER_SIZE, 0 ); -//printf("read: %d\n", i ); - if( i<=0 ) { - perror("read"); - goto err_out; - } - http_response_append( http_hdr, buffer, i ); - } while( !http_is_header_entire( http_hdr ) ); - if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { - http_hdr->buffer[http_hdr->buffer_size]='\0'; - mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer ); - } - ret = asf_http_parse_response(asf_http_ctrl, http_hdr); - if( ret<0 ) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse header.\n"); - goto err_out; - } - switch( asf_http_ctrl->streaming_type ) { - case ASF_Live_e: - case ASF_Prerecorded_e: - case ASF_PlainText_e: - if( http_hdr->body_size>0 ) { - if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { - goto err_out; - } - } - if( asf_http_ctrl->request==1 ) { - if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) { - // First request, we only got the ASF header. - ret = asf_streaming_parse_header(stream,fd); - if(ret < 0) goto err_out; - if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) { - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"No stream found.\n"); - goto err_out; - } - asf_http_ctrl->request++; - done = 0; - } else { - done = 1; - } - } - break; - case ASF_Redirector_e: - if( http_hdr->body_size>0 ) { - if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { - goto err_out; - } - } - *demuxer_type = DEMUXER_TYPE_PLAYLIST; - done = 1; - break; - 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; - case ASF_Unknown_e: - default: - mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF streaming type\n"); - goto err_out; - } - // Check if we got a redirect. - } while(!done); - - stream->fd = fd; - if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) { - stream->streaming_ctrl->streaming_read = nop_streaming_read; - stream->streaming_ctrl->streaming_seek = nop_streaming_seek; - } else { - stream->streaming_ctrl->streaming_read = asf_http_streaming_read; - stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek; - stream->streaming = true; - } - stream->streaming_ctrl->status = streaming_playing_e; - stream->close = close_s; - - http_free( http_hdr ); - return 0; - -err_out: - if (fd > 0) - closesocket(fd); - stream->fd = -1; - http_free(http_hdr); - return -1; -} - -static int open_s(stream_t *stream,int mode, void* opts, int* file_format) { - stream->streaming_ctrl = streaming_ctrl_new(); - if( stream->streaming_ctrl==NULL ) { - return STREAM_ERROR; - } - stream->streaming_ctrl->audio_id_ptr = &stream->opts->audio_id; - stream->streaming_ctrl->video_id_ptr = &stream->opts->video_id; - stream->streaming_ctrl->bandwidth = network_bandwidth; - stream->streaming_ctrl->url = url_new_with_proxy(stream->url); - - mp_tmsg(MSGT_OPEN, MSGL_INFO, "STREAM_ASF, URL: %s\n", stream->url); - if((!strncmp(stream->url, "mp_http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) { - streaming_ctrl_free(stream->streaming_ctrl); - stream->streaming_ctrl = NULL; - return STREAM_UNSUPPORTED; - } - - if(asf_streaming_start(stream, file_format) < 0) { - mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed, exiting.\n"); - streaming_ctrl_free(stream->streaming_ctrl); - stream->streaming_ctrl = NULL; - return STREAM_UNSUPPORTED; - } - - if (*file_format != DEMUXER_TYPE_PLAYLIST) - *file_format = DEMUXER_TYPE_ASF; - stream->type = STREAMTYPE_STREAM; - return STREAM_OK; -} - -const stream_info_t stream_info_asf = { - "mms and mms over http streaming", - "null", - "Bertrand, Reimar Doeffinger, Albeu", - "originally based on work by Majormms (is that code still there?)", - open_s, - {"mp_mms", "mp_mmst", "mp_http", "mp_http_proxy", "mp_mmsh", "mp_mmshttp", NULL}, - NULL, - 0 // Urls are an option string -}; diff --git a/stream/cookies.c b/stream/cookies.c index a7f01ef420..4181458405 100644 --- a/stream/cookies.c +++ b/stream/cookies.c @@ -33,8 +33,8 @@ #include "osdep/io.h" +#include "core/options.h" #include "cookies.h" -#include "http.h" #include "core/mp_msg.h" #define MAX_COOKIES 20 @@ -72,24 +72,6 @@ static char *col_dup(const char *src) return dst; } -static int right_hand_strcmp(const char *cookie_domain, const char *url_domain) -{ - int c_l; - int u_l; - - c_l = strlen(cookie_domain); - u_l = strlen(url_domain); - - if (c_l > u_l) - return -1; - return strcmp(cookie_domain, url_domain + u_l - c_l); -} - -static int left_hand_strcmp(const char *cookie_path, const char *url_path) -{ - return strncmp(cookie_path, url_path, strlen(cookie_path)); -} - /* Finds the start of all the columns */ static int parse_line(char **ptr, char *cols[6]) { @@ -196,72 +178,6 @@ static struct cookie_list_type *load_cookies(void) return NULL; } -/* Take an HTTP_header_t, and insert the correct headers. The cookie files are read if necessary. */ -void -cookies_set(HTTP_header_t * http_hdr, const char *domain, const char *url) -{ - int found_cookies = 0; - struct cookie_list_type *cookies[MAX_COOKIES]; - struct cookie_list_type *list, *start; - int i; - char *path; - char *buf; - - path = strchr(url, '/'); - if (!path) - path = ""; - - if (!cookie_list) - cookie_list = load_cookies(); - - - list = start = cookie_list; - - /* Find which cookies we want, removing duplicates. Cookies with the longest domain, then longest path take priority */ - while (list) { - /* Check the cookie domain and path. Also, we never send "secure" cookies. These should only be sent over HTTPS. */ - if ((right_hand_strcmp(list->domain, domain) == 0) - && (left_hand_strcmp(list->path, path) == 0) && !list->secure) { - int replacing = 0; - for (i = 0; i < found_cookies; i++) { - if (strcmp(list->name, cookies[i]->name) == 0) { - replacing = 0; - if (strlen(list->domain) <= strlen(cookies[i]->domain)) { - cookies[i] = list; - } else if (strlen(list->path) <= strlen(cookies[i]->path)) { - cookies[i] = list; - } - } - } - if (found_cookies > MAX_COOKIES) { - /* Cookie jar overflow! */ - break; - } - if (!replacing) - cookies[found_cookies++] = list; - } - list = list->next; - } - - - buf = strdup("Cookie:"); - - for (i = 0; i < found_cookies; i++) { - char *nbuf; - - nbuf = malloc(strlen(buf) + strlen(" ") + strlen(cookies[i]->name) + - strlen("=") + strlen(cookies[i]->value) + strlen(";") + 1); - sprintf(nbuf, "%s %s=%s;", buf, cookies[i]->name, - cookies[i]->value); - free(buf); - buf = nbuf; - } - - if (found_cookies) - http_set_field(http_hdr, buf); - free(buf); -} - // Return a cookies string as expected by lavf (libavformat/http.c). The format // is like a Set-Cookie header (http://curl.haxx.se/rfc/cookie_spec.html), // separated by newlines. diff --git a/stream/cookies.h b/stream/cookies.h index a24560631b..e8b548c112 100644 --- a/stream/cookies.h +++ b/stream/cookies.h @@ -24,10 +24,6 @@ #ifndef MPLAYER_COOKIES_H #define MPLAYER_COOKIES_H -#include "http.h" - -void cookies_set(HTTP_header_t * http_hdr, const char *hostname, - const char *url); char *cookies_lavf(void); #endif /* MPLAYER_COOKIES_H */ diff --git a/stream/http.c b/stream/http.c deleted file mode 100644 index d17261910f..0000000000 --- a/stream/http.c +++ /dev/null @@ -1,966 +0,0 @@ -/* - * HTTP Helper - * - * Copyright (C) 2001 Bertrand Baudet - * - * 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. - */ - -#include "config.h" - -#include -#include -#include -#include - -#if !HAVE_WINSOCK2_H -#else -#include -#include -#endif - -#include "http.h" -#include "url.h" -#include "core/options.h" -#include "core/mp_msg.h" - -#include "stream.h" -#include "demux/demux.h" -#include "network.h" - -#include "libavutil/base64.h" - -#include - -typedef struct { - unsigned metaint; - unsigned metapos; - int is_ultravox; -} scast_data_t; - -/** - * \brief first read any data from sc->buffer then from fd - * \param fd file descriptor to read data from - * \param buffer buffer to read into - * \param len how many bytes to read - * \param sc streaming control containing buffer to read from first - * \return len unless there is a read error or eof - */ -static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) { - unsigned pos = 0; - unsigned cp_len = sc->buffer_size - sc->buffer_pos; - if (cp_len > len) - cp_len = len; - memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len); - sc->buffer_pos += cp_len; - pos += cp_len; - while (pos < len) { - int ret = recv(fd, &buffer[pos], len - pos, 0); - if (ret <= 0) - break; - pos += ret; - } - return pos; -} - -/** - * \brief read and process (i.e. discard *g*) a block of ultravox metadata - * \param fd file descriptor to read from - * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd - * \return number of real data before next metadata block starts or 0 on error - * - * You can use unsv://samples.mplayerhq.hu/V-codecs/VP5/vp5_artefacts.nsv to - * test. - */ -static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) { - unsigned metaint; - unsigned char info[6] = {0, 0, 0, 0, 0, 0}; - int info_read; - do { - info_read = my_read(fd, info, 1, sc); - if (info[0] == 0x00) - info_read = my_read(fd, info, 6, sc); - else - info_read += my_read(fd, &info[1], 5, sc); - if (info_read != 6) // read error or eof - return 0; - // sync byte and reserved flags - if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) { - mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n"); - return 0; - } - if (info[1] & 0x01) - mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n"); - metaint = info[4] << 8 | info[5]; - if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense - char *metabuf = malloc(metaint); - my_read(fd, metabuf, metaint, sc); - free(metabuf); - } - } while ((info[3] & 0xf) < 0x07); - return metaint; -} - -/** - * \brief read one scast meta data entry and print it - * \param fd file descriptor to read from - * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd - */ -static void scast_meta_read(int fd, streaming_ctrl_t *sc) { - unsigned char tmp = 0; - unsigned metalen; - my_read(fd, &tmp, 1, sc); - metalen = tmp * 16; - if (metalen > 0) { - int i; - uint8_t *info = malloc(metalen + 1); - unsigned nlen = my_read(fd, info, metalen, sc); - // avoid breaking the user's terminal too much - if (nlen > 256) nlen = 256; - for (i = 0; i < nlen; i++) - if (info[i] && info[i] < 32) info[i] = '?'; - info[nlen] = 0; - mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info); - free(info); - } -} - -/** - * \brief read data from scast/ultravox stream without any metadata - * \param fd file descriptor to read from - * \param buffer buffer to read data into - * \param size number of bytes to read - * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd - */ -static int scast_streaming_read(int fd, char *buffer, int size, - streaming_ctrl_t *sc) { - scast_data_t *sd = (scast_data_t *)sc->data; - unsigned block, ret; - unsigned done = 0; - - // first read remaining data up to next metadata - block = sd->metaint - sd->metapos; - if (block > size) - block = size; - ret = my_read(fd, buffer, block, sc); - sd->metapos += ret; - done += ret; - if (ret != block) // read problems or eof - size = done; - - while (done < size) { // now comes the metadata - if (sd->is_ultravox) - { - sd->metaint = uvox_meta_read(fd, sc); - if (!sd->metaint) - size = done; - } - else - scast_meta_read(fd, sc); // read and display metadata - sd->metapos = 0; - block = size - done; - if (block > sd->metaint) - block = sd->metaint; - ret = my_read(fd, &buffer[done], block, sc); - sd->metapos += ret; - done += ret; - if (ret != block) // read problems or eof - size = done; - } - return done; -} - -static int scast_streaming_start(stream_t *stream) { - int metaint; - scast_data_t *scast_data; - HTTP_header_t *http_hdr = stream->streaming_ctrl->data; - if (!stream || stream->fd < 0 || !http_hdr) - return -1; - int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; - if (is_ultravox) - metaint = 0; - else { - metaint = -1; - char *h = http_get_field(http_hdr, "Icy-MetaInt"); - if (h) - metaint = atoi(h); - if (metaint <= 0) - return -1; - } - stream->streaming_ctrl->buffer = malloc(http_hdr->body_size); - stream->streaming_ctrl->buffer_size = http_hdr->body_size; - stream->streaming_ctrl->buffer_pos = 0; - memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size); - scast_data = malloc(sizeof(scast_data_t)); - scast_data->metaint = metaint; - scast_data->metapos = 0; - scast_data->is_ultravox = is_ultravox; - http_free(http_hdr); - stream->streaming_ctrl->data = scast_data; - stream->streaming_ctrl->streaming_read = scast_streaming_read; - stream->streaming_ctrl->streaming_seek = NULL; - stream->streaming_ctrl->status = streaming_playing_e; - stream->streaming = true; - return 0; -} - -static int nop_streaming_start( stream_t *stream ) { - HTTP_header_t *http_hdr = NULL; - char *next_url=NULL; - int fd,ret; - if( stream==NULL ) return -1; - - fd = stream->fd; - if( fd<0 ) { - fd = http_send_request( stream->streaming_ctrl->url, 0 ); - if( fd<0 ) return -1; - http_hdr = http_read_response( fd ); - if( http_hdr==NULL ) return -1; - - switch( http_hdr->status_code ) { - case 200: // OK - mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); - mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); - if( http_hdr->body_size>0 ) { - if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); - return -1; - } - } - break; - // Redirect - case 301: // Permanently - case 302: // Temporarily - case 303: // See Other - case 307: // Temporarily (since HTTP/1.1) - ret=-1; - next_url = http_get_field( http_hdr, "Location" ); - - if (next_url != NULL) { - mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url); - stream->streaming_ctrl->url=url_new_with_proxy(next_url); - ret=nop_streaming_start(stream); //recursively get streaming started - } else { - mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n"); - closesocket( fd ); - fd = -1; - } - return ret; - break; - case 401: //Authorization required - case 403: //Forbidden - case 404: //Not found - case 500: //Server Error - default: - mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); - closesocket( fd ); - fd = -1; - return -1; - break; - } - stream->fd = fd; - } else { - http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data; - if( http_hdr->body_size>0 ) { - if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); - stream->streaming_ctrl->data = NULL; - return -1; - } - } - } - - if( http_hdr ) { - http_free( http_hdr ); - stream->streaming_ctrl->data = NULL; - } - - stream->streaming_ctrl->streaming_read = nop_streaming_read; - stream->streaming_ctrl->streaming_seek = nop_streaming_seek; - stream->streaming_ctrl->status = streaming_playing_e; - stream->streaming = true; - return 0; -} - -HTTP_header_t * -http_new_header(void) { - HTTP_header_t *http_hdr; - - http_hdr = calloc(1, sizeof(*http_hdr)); - if( http_hdr==NULL ) return NULL; - - return http_hdr; -} - -void -http_free( HTTP_header_t *http_hdr ) { - HTTP_field_t *field, *field2free; - if( http_hdr==NULL ) return; - free(http_hdr->protocol); - free(http_hdr->uri); - free(http_hdr->reason_phrase); - free(http_hdr->field_search); - free(http_hdr->method); - free(http_hdr->buffer); - field = http_hdr->first_field; - while( field!=NULL ) { - field2free = field; - free(field->field_name); - field = field->next; - free( field2free ); - } - free( http_hdr ); - http_hdr = NULL; -} - -int -http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { - if( http_hdr==NULL || response==NULL || length<0 ) return -1; - - if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) { - mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n"); - return -1; - } - http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); - if( http_hdr->buffer==NULL ) { - mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n"); - return -1; - } - memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); - http_hdr->buffer_size += length; - http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! - return http_hdr->buffer_size; -} - -int -http_is_header_entire( HTTP_header_t *http_hdr ) { - if( http_hdr==NULL ) return -1; - if( http_hdr->buffer==NULL ) return 0; // empty - - if(http_hdr->buffer_size > 128*1024) return 1; - if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && - strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; - return 1; -} - -int -http_response_parse( HTTP_header_t *http_hdr ) { - char *hdr_ptr, *ptr; - char *field=NULL; - int pos_hdr_sep, hdr_sep_len; - size_t len; - if( http_hdr==NULL ) return -1; - if( http_hdr->is_parsed ) return 0; - - // Get the protocol - hdr_ptr = strstr( http_hdr->buffer, " " ); - if( hdr_ptr==NULL ) { - mp_msg(MSGT_NETWORK,MSGL