#include "lrs.h"
char *BUF=NULL;
char *pEXTINF=NULL;
char *pDOT=NULL;
int i=0,j=0,k=0;
char pTime[1000][20]={'\0'},pTs[1000][50]={'\0'};
int countTs=0,countRes=0;
char *tempBuf;
char *pVodURL=NULL,*pHost=NULL;
char *tempStr=NULL,SHost[50]={'\0'},*pSURL=NULL,psurl[200]={'\0'};
int port=80;
char getTs[2000]="GET ";
char *a=NULL,*b=NULL,*c=NULL,*d=NULL,*e=NULL,*f=NULL,*g=NULL,*h=NULL;
//char *VTime[50],*VTs[50];
int vtime=0,vts=0,hot=0;
float waitTime=0;
double atof(const char * string );
char sock[10]={'\0'},buf[10]={'\0'};
char *high,*mid,*low;
Action()
{
lrs_startup(257);
/*
for(countRes=0;countRes<10;countRes++){
for(hot=0;hot<=9;hot++){
lr_save_int(port,"PORT");
pVodURL="/vod/_definst_/mp4:mr/mr/2013/06/24/20130624_19de4644ff19477ea7ba082c727e9185_1_1.mp4/playlist.m3u8";
high=lr_eval_string("<high>");
mid=lr_eval_string("<mid>");
low=lr_eval_string("<low>");
if(hot<=5&&countRes<5)
pVodURL=high;
else if(hot<=8&&countRes<5)
pVodURL=mid;
else if(hot<=9&&countRes<5)
pVodURL=low;
else if(hot<=5&&countRes>=5)
pVodURL=low;
else if(hot<=8&&countRes>=5)
pVodURL=high;
else
pVodURL=mid;
*/
lr_save_int(port,"PORT");
pVodURL=lr_eval_string("<movieid>");
lr_save_string(pVodURL,"VODURL");
pHost="192.168.7.35";
lr_save_string(pHost,"HOST");
lrs_create_socket("socket0", "TCP", "RemoteHost=<HOST>:<PORT>", LrsLastArg);
lrs_send("socket0", "buf0", LrsLastArg);
lrs_receive("socket0", "buf1", LrsLastArg);
lrs_save_searched_string("socket0",LRS_LAST_RECEIVED,"DEST","LB=Location: http://","RB=playlist.m3u8",1,0,-1);
tempStr=lr_eval_string("<DEST>");
pSURL=(char *)strstr(tempStr,"/");
k=0;
while(1){
if(tempStr[k]=='/'){
break;
}
SHost[k]=tempStr[k];
k=k+1;
}
lr_save_string(pSURL,"SURL");
lr_save_string(SHost,"SHOST");
lrs_close_socket("socket0");
lrs_create_socket("socket1", "TCP", "RemoteHost=<SHOST>:<PORT>", LrsLastArg);
lrs_send("socket1", "buf2", LrsLastArg);
lrs_receive("socket1", "buf3", LrsLastArg);
lrs_close_socket("socket1");
lrs_create_socket("socket2", "TCP", "RemoteHost=<SHOST>:<PORT>", LrsLastArg);
lrs_send("socket2", "buf4", LrsLastArg);
lrs_receive("socket2", "buf5", LrsLastArg);
BUF=lrs_get_received_buffer("socket2", 0,-1,"ascii");
tempBuf=BUF;
// strcpy(tempBuf,BUF);
vtime=0;
vts=0;
while(1){
pEXTINF=(char *)strstr(tempBuf,"#EXTINF:");
tempBuf=pEXTINF+8;
if (pEXTINF==0) {
break;
}
countTs=countTs+1;
i=0;
while(1){
if(pEXTINF[i+8]==','){
break;
}
pTime[vtime][i]=pEXTINF[i+8];
i=i+1;
}
tempBuf=pEXTINF;
pDOT=(char *)strstr(tempBuf,",");
j=0;
while(1){
if(pDOT[j+3]=='#'){
break;
}
pTs[vts][j]=pDOT[j+2];
j=j+1;
}
strcpy(tempBuf,pDOT);
strcpy(psurl,pSURL);
strcpy(getTs,"GET ");
a=(char *)strcat(psurl,pTs[vts]);
b=" HTTP/1.1\r\n";
c="Host: 192.168.7.83\r\n";
d="User-Agent: VLC/2.0.7 LibVLC/2.0.7\r\n";
e="Range: bytes=0-\r\n";
f="Connection: close\r\n";
g="Icy-MetaData: 1\r\n";
h="\r\n";
(char *)strcat(getTs,a);
(char *)strcat(getTs,b);
(char *)strcat(getTs,c);
(char *)strcat(getTs,d);
(char *)strcat(getTs,e);
(char *)strcat(getTs,f);
(char *)strcat(getTs,g);
(char *)strcat(getTs,h);
lr_save_string(getTs,"PARAM");
sprintf(sock,"socket%d",vts+3);
lr_save_string(sock,"SOCK");
sprintf(buf,"buf%d",(vts+3)*2);
lr_save_string(buf,"BUFn");
lr_output_message("pTime=%s\n",pTime[vtime]);
waitTime=atof(pTime[vtime]);
lrs_create_socket(lr_eval_string("<SOCK>"), "TCP", "RemoteHost=<SHOST>:<PORT>", LrsLastArg);
lrs_send(lr_eval_string("<SOCK>"), lr_eval_string("<BUFn>"), LrsLastArg);
sprintf(buf,"buf%d",(vts+3)*2+1);
lr_save_string(buf,"BUFn");
lr_think_time (waitTime-1);
lrs_receive(lr_eval_string("<SOCK>"), lr_eval_string("<BUFn>"), LrsLastArg);
lrs_close_socket(lr_eval_string("<SOCK>"));
lr_output_message ("vts=%d\n",vts);
vtime=vtime+1;
vts=vts+1;
}
lrs_close_socket("socket2");
// }
// }
return 0;
}