📄 real.c
字号:
/*********************************************************************** * real.c : for downloading from real server using rtsp *********************************************************************** * Copyright (C) 2007 metro <me_t_ro@yahoo.com> * * This file is part of msdl, media stream downloader * * This file is based on rtsp (real) implementation of * mplayer and xine, and includes code from both projects. * * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. * ***********************************************************************//* * Copyright notice of MPlayer project * which some part of msdl is based on. * (from MPlayer-1.0rc2/stream/realrtsp/real.c) *//* * This file was ported to MPlayer from xine CVS real.c,v 1.8 2003/03/30 17:11:50 *//* * Copyright (C) 2002 the xine project * * This file is part of xine, a free video player. * * xine 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. * * xine 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * * * special functions for real streams. * adopted from joschkas real tools. * */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include "msdl.h"#include "msdllib.h"#include "display.h"#include "network.h"#include "rtsp.h"#include "real.h"#include "realchallenge.h"#include "rmff.h"#include "sdpreal.h"#include "asmrule.h"/* realplayer specific stuff */const char real_clientid[] = "ClientID: Linux_2.6_10.0.0.0_play32_RN01_EN_586";const char real_useragent[] = "User-Agent: RealMedia Player Version 10.0.0.0 (linux-2.6-libc6-gcc4.1-i586)";const char real_companyid[] = "CompanyID: X160hPo6JmsD6Vger24oAg==";const char real_clientchallenge[] = "ClientChallenge: cd7cb1ac431e8d1ad4d2fadb8cf762d1";const char real_playerstarttime[] = "PlayerStarttime: [19/03/2007:21:42:56 00:00]";const char real_transport[] = "Transport: x-pn-tng/tcp;mode=play,rtp/avp/tcp;unicast;mode=play";static int real_process_header(struct stream_t *stream);static int real_process_media_packet(struct stream_t *stream,uint8_t *header, uint8_t *buffer,size_t max_size);int real_prepare_resuming(struct stream_t *stream);int real_get_last_npt_of_file(char *local_filename,uint32_t *send_time,uint64_t *seek_offset);/* * count rules * >>>> CAUTION <<<< * starting '#' is not necessary, but ending ';' is necessary, so * couting ; is correct. */static int count_rules(char *asmrule){ int count = 0; while(*asmrule) if(*(asmrule++) == ';') count++; return count;}static char *next_rule(char *data){ data = strchr(data,';'); return data + 1;}/* * finds rule which matches bandwidth. * return value: number of matched rules * rulearray contains its rule numbers. */int find_asmrule_match(char *asmrule,int **matched,int bw){ int i; int rules; int matchedrules = 0; rules = count_rules(asmrule); *matched = (int *)xmalloc(sizeof(int) * rules); for(i = 0; i < rules ;i++) { /* copy one rule to string*/ if(asmrule_match(asmrule,bw)) { /* match!! */ display(MSDL_DBG,"rule[%d] = %d matched\n",matchedrules,i); (*matched)[matchedrules] = i; matchedrules++; } asmrule = next_rule(asmrule); } return matchedrules;}/* * copy mlti_data('type specific data') to it's buffer. * * *buf is malloc()ed in this function. don't forget to free later */static int select_mlti_data(uint8_t *mlti_data,int mlti_data_size, int match,uint8_t **buf){ int numrules,codec,size; int i; if(!mlti_data) return 0; /* just to make sure */ if(memcmp(mlti_data,"MLTI",4)) { display(MSDL_DBG, "'MLTI' not found on OpaqueData base64 decoded string\n"); *buf = (uint8_t *)xmalloc(mlti_data_size); memcpy(*buf,mlti_data,mlti_data_size); return mlti_data_size; } mlti_data += 4; numrules = get16_be(mlti_data); if(match >= numrules) { display(MSDL_ERR,"matched rule >= number of rules .. strange ...\n"); return 0; } mlti_data += (match + 1) * 2; codec = get16_be(mlti_data); mlti_data += (numrules - match) * 2; numrules = get16_be(mlti_data); if(codec >= numrules) { display(MSDL_ERR,"number of codecs >= number of codecs .. strange ...\n"); return 0; } mlti_data += 2; for(i = 0; i < codec ; i++) { size = get32_be(mlti_data); mlti_data += size + 4; } size = get32_be(mlti_data); *buf = (uint8_t *)xmalloc(size); memcpy(*buf,mlti_data + 4,size); return size;}/* * parse sdp(stream description) lines. * * subscribe is "Subscribe: " which is send with SET_PARAMETER request. * */struct rmff_header_t *real_parse_sdp(char *data,char **subscribe,int bw){ struct sdpreal_t *desc; struct list_h *p; struct rmff_header_t *rmff_header; uint8_t *buf; int *matched; /* array of matched rules. malloced in find_asmrule_match */ int matched_rules; int i,j; int mlti_data_len; int subscribe_len = BUFSIZE_1K; char subbuf[32]; int duration =0; int max_bit_rate = 0; int avg_bit_rate = 0; int max_packet_size = 0; int avg_packet_size = 0; desc = sdpreal_parse(data); rmff_header = new_rmff_header_t(); rmff_header->fileheader = new_rmff_fileheader(4 + desc->stream_count); rmff_header->cont = new_rmff_cont(desc->title,desc->author, desc->copyright,desc->abstract); rmff_header->data = new_rmff_dataheader(0,0); rmff_header->streams = xmalloc(sizeof(struct rmff_mdpr_t*) * (desc->stream_count + 1)); display(MSDL_VER,"number of streams: %u\n",desc->stream_count); *subscribe = (char *)xmalloc(subscribe_len); strcpy(*subscribe,"Subscribe: "); for(i = 0,p = desc->streams; i < desc->stream_count; i++,p = p->next) { struct sdpreal_stream_t *stream = p->p; /* Subscribe: stream=0;rule=16,stream=0;rule=17 */ matched_rules = find_asmrule_match(stream->asm_rule_book, &matched,bw); if((strlen(*subscribe) + 32 * matched_rules) > subscribe_len) { /* subscribe should be longer */ subscribe_len = (subscribe_len * 2 > strlen(*subscribe) + 32 * matched_rules) ? subscribe_len * 2 : strlen(*subscribe) + 32 * matched_rules; *subscribe = (char *)xrealloc(*subscribe,subscribe_len); } for(j = 0; j < matched_rules ; j++) { snprintf(subbuf,sizeof(subbuf),"stream=%u;rule=%u,",i,matched[j]); strcat(*subscribe,subbuf); } buf = NULL; if(!stream->mlti_data) { mlti_data_len = 0; } else { /* buf is malloc()ed inside select_mlti_data */ mlti_data_len = select_mlti_data(stream->mlti_data, stream->mlti_data_size, matched[0],&buf); } rmff_header->streams[i] = new_rmff_mdpr(stream->stream_id, stream->max_bit_rate, stream->avg_bit_rate, stream->max_packet_size, stream->avg_packet_size, stream->start_time, stream->preroll, stream->duration, stream->stream_name, stream->mime_type, mlti_data_len, buf); duration = (duration > stream->duration) ? duration : stream->duration; max_bit_rate += stream->max_bit_rate; avg_bit_rate += stream->avg_bit_rate; max_packet_size = (max_packet_size > stream->max_packet_size) ? max_packet_size : stream->max_packet_size; if(avg_packet_size) { avg_packet_size = (avg_packet_size + stream->avg_packet_size) / 2; } else { avg_packet_size = stream->avg_packet_size; } if(matched) { free(matched); matched = NULL; } if(buf) { free(buf); buf = NULL; } } if((*subscribe)[strlen(*subscribe) -1 ] == ',') { (*subscribe)[strlen(*subscribe) -1 ] = '\0'; /* delete last comma */ } rmff_header->prop = new_rmff_prop(max_bit_rate, avg_bit_rate, max_packet_size, avg_packet_size, 0, duration, 0,0,0, desc->stream_count, desc->flags); rmff_fix_header(rmff_header); free_sdpreal_t(desc); rmff_print_header(rmff_header); return rmff_header;}/* * send OPTIONS request, this is used as very first trial to server * we will need rtsp_hdr later, so return that. * return value: -1: failure status_code: success, rtsp_hdr_ret(malloc) */int real_rtsp_options(struct stream_t *stream,struct rtsp_header_t **rtsp_hdr_ret){ int ret = 0; struct rtsp_ctrl_t *rtsp_ctrl = stream->stream_ctrl->rtsp_ctrl; struct rtsp_header_t *rtsp_hdr = NULL; char *options_uri = NULL; int options_uri_len = 0; /* default is rtsp-real (becasue OPTIONS req is supported) */ rtsp_hdr = new_rtsp_header_with_standard_fields(rtsp_ctrl); rtsp_set_field(rtsp_hdr,real_useragent); rtsp_set_field(rtsp_hdr,"GUID: 00000000-0000-0000-0000-000000000000"); rtsp_set_field(rtsp_hdr,real_clientid); rtsp_set_field(rtsp_hdr,"Pragma: initiate-session"); rtsp_set_field(rtsp_hdr,"RegionData: 0"); rtsp_set_field(rtsp_hdr,real_clientchallenge); rtsp_set_field(rtsp_hdr,real_companyid); rtsp_set_field(rtsp_hdr,real_playerstarttime); options_uri_len = strlen(stream->serverinfo->host) + 20; options_uri = (char *)xmalloc(options_uri_len); snprintf(options_uri,options_uri_len,"rtsp://%s:%i", stream->serverinfo->host,stream->serverinfo->port); rtsp_request_options(rtsp_hdr,options_uri); rtsp_send_request_and_free(stream,rtsp_hdr); rtsp_hdr = new_rtsp_header(); ret = rtsp_recv_header(stream,rtsp_hdr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -