Alles werkend - maar echte flash clients zijn het daar niet meer eens, raar genoeg...
This commit is contained in:
parent
2d0aafc179
commit
e8246efc91
8 changed files with 234 additions and 99 deletions
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
unsigned int chunk_rec_max = 128;
|
unsigned int chunk_rec_max = 128;
|
||||||
unsigned int chunk_snd_max = 128;
|
unsigned int chunk_snd_max = 128;
|
||||||
unsigned int rec_window_size = 1024*500;
|
unsigned int rec_window_size = 0xFA00;
|
||||||
unsigned int snd_window_size = 1024*500;
|
unsigned int snd_window_size = 1024*500;
|
||||||
unsigned int rec_window_at = 0;
|
unsigned int rec_window_at = 0;
|
||||||
unsigned int snd_window_at = 0;
|
unsigned int snd_window_at = 0;
|
||||||
|
@ -14,6 +14,7 @@ unsigned int rec_cnt = 0;
|
||||||
unsigned int snd_cnt = 0;
|
unsigned int snd_cnt = 0;
|
||||||
|
|
||||||
struct chunkinfo {
|
struct chunkinfo {
|
||||||
|
unsigned int cs_id;
|
||||||
unsigned int timestamp;
|
unsigned int timestamp;
|
||||||
unsigned int len;
|
unsigned int len;
|
||||||
unsigned int real_len;
|
unsigned int real_len;
|
||||||
|
@ -41,64 +42,150 @@ void scrubChunk(struct chunkpack c){
|
||||||
c.real_len = 0;
|
c.real_len = 0;
|
||||||
}//scrubChunk
|
}//scrubChunk
|
||||||
|
|
||||||
|
|
||||||
|
//ugly global, but who cares...
|
||||||
|
std::map<unsigned int, chunkinfo> prevmap;
|
||||||
|
//return previous packet of this cs_id
|
||||||
|
chunkinfo GetPrev(unsigned int cs_id){
|
||||||
|
return prevmap[cs_id];
|
||||||
|
}//GetPrev
|
||||||
|
//store packet information of last packet of this cs_id
|
||||||
|
void PutPrev(chunkpack prev){
|
||||||
|
prevmap[prev.cs_id].timestamp = prev.timestamp;
|
||||||
|
prevmap[prev.cs_id].len = prev.len;
|
||||||
|
prevmap[prev.cs_id].real_len = prev.real_len;
|
||||||
|
prevmap[prev.cs_id].len_left = prev.len_left;
|
||||||
|
prevmap[prev.cs_id].msg_type_id = prev.msg_type_id;
|
||||||
|
prevmap[prev.cs_id].msg_stream_id = prev.msg_stream_id;
|
||||||
|
}//PutPrev
|
||||||
|
|
||||||
|
//ugly global, but who cares...
|
||||||
|
std::map<unsigned int, chunkinfo> sndprevmap;
|
||||||
|
//return previous packet of this cs_id
|
||||||
|
chunkinfo GetSndPrev(unsigned int cs_id){
|
||||||
|
return sndprevmap[cs_id];
|
||||||
|
}//GetPrev
|
||||||
|
//store packet information of last packet of this cs_id
|
||||||
|
void PutSndPrev(chunkpack prev){
|
||||||
|
sndprevmap[prev.cs_id].cs_id = prev.cs_id;
|
||||||
|
sndprevmap[prev.cs_id].timestamp = prev.timestamp;
|
||||||
|
sndprevmap[prev.cs_id].len = prev.len;
|
||||||
|
sndprevmap[prev.cs_id].real_len = prev.real_len;
|
||||||
|
sndprevmap[prev.cs_id].len_left = prev.len_left;
|
||||||
|
sndprevmap[prev.cs_id].msg_type_id = prev.msg_type_id;
|
||||||
|
sndprevmap[prev.cs_id].msg_stream_id = prev.msg_stream_id;
|
||||||
|
}//PutPrev
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//sends the chunk over the network
|
//sends the chunk over the network
|
||||||
void SendChunk(chunkpack ch){
|
void SendChunk(chunkpack ch){
|
||||||
unsigned char tmp;
|
unsigned char tmp;
|
||||||
unsigned int tmpi;
|
unsigned int tmpi;
|
||||||
|
unsigned char chtype = 0x00;
|
||||||
|
chunkinfo prev = GetSndPrev(ch.cs_id);
|
||||||
|
if (prev.cs_id == ch.cs_id){
|
||||||
|
if (ch.msg_stream_id == prev.msg_stream_id){
|
||||||
|
chtype = 0x40;//do not send msg_stream_id
|
||||||
|
if (ch.len == prev.len){
|
||||||
|
if (ch.msg_type_id == prev.msg_type_id){
|
||||||
|
chtype = 0x80;//do not send len and msg_type_id
|
||||||
|
if (ch.timestamp == prev.timestamp){
|
||||||
|
chtype = 0xC0;//do not send timestamp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (ch.cs_id <= 63){
|
if (ch.cs_id <= 63){
|
||||||
tmp = ch.cs_id; fwrite(&tmp, 1, 1, stdout);
|
tmp = chtype | ch.cs_id; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=1;
|
||||||
}else{
|
}else{
|
||||||
if (ch.cs_id <= 255+64){
|
if (ch.cs_id <= 255+64){
|
||||||
tmp = 0; fwrite(&tmp, 1, 1, stdout);
|
tmp = chtype | 0; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = ch.cs_id - 64; fwrite(&tmp, 1, 1, stdout);
|
tmp = ch.cs_id - 64; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=2;
|
||||||
}else{
|
}else{
|
||||||
tmp = 1; fwrite(&tmp, 1, 1, stdout);
|
tmp = chtype | 1; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmpi = ch.cs_id - 64;
|
tmpi = ch.cs_id - 64;
|
||||||
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//timestamp
|
unsigned int ntime = 0;
|
||||||
//TODO: support for > 0x00ffffff timestamps!
|
if (chtype != 0xC0){
|
||||||
tmpi = ch.timestamp;
|
//timestamp or timestamp diff
|
||||||
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, stdout);
|
if (chtype == 0x00){
|
||||||
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
tmpi = ch.timestamp;
|
||||||
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
if (tmpi >= 0x00ffffff){ntime = tmpi; tmpi = 0x00ffffff;}
|
||||||
//len
|
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, stdout);
|
||||||
tmpi = ch.len;
|
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, stdout);
|
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
snd_cnt+=3;
|
||||||
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
}else{
|
||||||
//msg type id
|
tmpi = ch.timestamp - prev.timestamp;
|
||||||
tmp = ch.msg_type_id; fwrite(&tmp, 1, 1, stdout);
|
if (tmpi >= 0x00ffffff){ntime = tmpi; tmpi = 0x00ffffff;}
|
||||||
//msg stream id
|
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = ch.msg_stream_id / (256*256*256); fwrite(&tmp, 1, 1, stdout);
|
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = ch.msg_stream_id / (256*256); fwrite(&tmp, 1, 1, stdout);
|
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = ch.msg_stream_id / 256; fwrite(&tmp, 1, 1, stdout);
|
snd_cnt+=3;
|
||||||
tmp = ch.msg_stream_id % 256; fwrite(&tmp, 1, 1, stdout);
|
}
|
||||||
|
if (chtype != 0x80){
|
||||||
|
//len
|
||||||
|
tmpi = ch.len;
|
||||||
|
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=3;
|
||||||
|
//msg type id
|
||||||
|
tmp = ch.msg_type_id; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=1;
|
||||||
|
if (chtype != 0x40){
|
||||||
|
//msg stream id
|
||||||
|
tmp = ch.msg_stream_id / (256*256*256); fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = ch.msg_stream_id / (256*256); fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = ch.msg_stream_id / 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = ch.msg_stream_id % 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//support for 0x00ffffff timestamps
|
||||||
|
if (ntime){
|
||||||
|
tmp = ntime / (256*256*256); fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = ntime / (256*256); fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = ntime / 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
tmp = ntime % 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=4;
|
||||||
|
}
|
||||||
ch.len_left = 0;
|
ch.len_left = 0;
|
||||||
while (ch.len_left < ch.len){
|
while (ch.len_left < ch.len){
|
||||||
tmpi = ch.len - ch.len_left;
|
tmpi = ch.len - ch.len_left;
|
||||||
if (tmpi > chunk_snd_max){tmpi = chunk_snd_max;}
|
if (tmpi > chunk_snd_max){tmpi = chunk_snd_max;}
|
||||||
fwrite((ch.data + ch.len_left), 1, tmpi, stdout);
|
fwrite((ch.data + ch.len_left), 1, tmpi, stdout);
|
||||||
|
snd_cnt+=tmpi;
|
||||||
ch.len_left += tmpi;
|
ch.len_left += tmpi;
|
||||||
if (ch.len_left < ch.len){
|
if (ch.len_left < ch.len){
|
||||||
if (ch.cs_id <= 63){
|
if (ch.cs_id <= 63){
|
||||||
tmp = 0xC + ch.cs_id; fwrite(&tmp, 1, 1, stdout);
|
tmp = 0xC0 + ch.cs_id; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=1;
|
||||||
}else{
|
}else{
|
||||||
if (ch.cs_id <= 255+64){
|
if (ch.cs_id <= 255+64){
|
||||||
tmp = 0xC0; fwrite(&tmp, 1, 1, stdout);
|
tmp = 0xC0; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = ch.cs_id - 64; fwrite(&tmp, 1, 1, stdout);
|
tmp = ch.cs_id - 64; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=2;
|
||||||
}else{
|
}else{
|
||||||
tmp = 0xC1; fwrite(&tmp, 1, 1, stdout);
|
tmp = 0xC1; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmpi = ch.cs_id - 64;
|
tmpi = ch.cs_id - 64;
|
||||||
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
tmp = tmpi % 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
tmp = tmpi / 256; fwrite(&tmp, 1, 1, stdout);
|
||||||
|
snd_cnt+=4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
PutSndPrev(ch);
|
||||||
}//SendChunk
|
}//SendChunk
|
||||||
|
|
||||||
//sends a chunk
|
//sends a chunk
|
||||||
|
@ -107,7 +194,7 @@ void SendChunk(unsigned int cs_id, unsigned char msg_type_id, unsigned int msg_s
|
||||||
timeval t;
|
timeval t;
|
||||||
gettimeofday(&t, 0);
|
gettimeofday(&t, 0);
|
||||||
ch.cs_id = cs_id;
|
ch.cs_id = cs_id;
|
||||||
ch.timestamp = t.tv_sec * 10 + t.tv_usec / 1000000;
|
ch.timestamp = t.tv_sec;
|
||||||
ch.len = data.size();
|
ch.len = data.size();
|
||||||
ch.real_len = data.size();
|
ch.real_len = data.size();
|
||||||
ch.len_left = 0;
|
ch.len_left = 0;
|
||||||
|
@ -126,7 +213,7 @@ void SendMedia(unsigned char msg_type_id, unsigned char * data, int len){
|
||||||
timeval t;
|
timeval t;
|
||||||
gettimeofday(&t, 0);
|
gettimeofday(&t, 0);
|
||||||
ch.cs_id = msg_type_id;
|
ch.cs_id = msg_type_id;
|
||||||
ch.timestamp = t.tv_sec * 10 + t.tv_usec / 1000000;
|
ch.timestamp = t.tv_sec;
|
||||||
ch.len = len;
|
ch.len = len;
|
||||||
ch.real_len = len;
|
ch.real_len = len;
|
||||||
ch.len_left = 0;
|
ch.len_left = 0;
|
||||||
|
@ -144,7 +231,7 @@ void SendCTL(unsigned char type, unsigned int data){
|
||||||
timeval t;
|
timeval t;
|
||||||
gettimeofday(&t, 0);
|
gettimeofday(&t, 0);
|
||||||
ch.cs_id = 2;
|
ch.cs_id = 2;
|
||||||
ch.timestamp = t.tv_sec * 10 + t.tv_usec / 1000000;
|
ch.timestamp = t.tv_sec;
|
||||||
ch.len = 4;
|
ch.len = 4;
|
||||||
ch.real_len = 4;
|
ch.real_len = 4;
|
||||||
ch.len_left = 0;
|
ch.len_left = 0;
|
||||||
|
@ -163,7 +250,7 @@ void SendCTL(unsigned char type, unsigned int data, unsigned char data2){
|
||||||
timeval t;
|
timeval t;
|
||||||
gettimeofday(&t, 0);
|
gettimeofday(&t, 0);
|
||||||
ch.cs_id = 2;
|
ch.cs_id = 2;
|
||||||
ch.timestamp = t.tv_sec * 10 + t.tv_usec / 1000000;
|
ch.timestamp = t.tv_sec;
|
||||||
ch.len = 5;
|
ch.len = 5;
|
||||||
ch.real_len = 5;
|
ch.real_len = 5;
|
||||||
ch.len_left = 0;
|
ch.len_left = 0;
|
||||||
|
@ -183,7 +270,7 @@ void SendUSR(unsigned char type, unsigned int data){
|
||||||
timeval t;
|
timeval t;
|
||||||
gettimeofday(&t, 0);
|
gettimeofday(&t, 0);
|
||||||
ch.cs_id = 2;
|
ch.cs_id = 2;
|
||||||
ch.timestamp = t.tv_sec * 10 + t.tv_usec / 1000000;
|
ch.timestamp = t.tv_sec;
|
||||||
ch.len = 6;
|
ch.len = 6;
|
||||||
ch.real_len = 6;
|
ch.real_len = 6;
|
||||||
ch.len_left = 0;
|
ch.len_left = 0;
|
||||||
|
@ -204,7 +291,7 @@ void SendUSR(unsigned char type, unsigned int data, unsigned int data2){
|
||||||
timeval t;
|
timeval t;
|
||||||
gettimeofday(&t, 0);
|
gettimeofday(&t, 0);
|
||||||
ch.cs_id = 2;
|
ch.cs_id = 2;
|
||||||
ch.timestamp = t.tv_sec * 10 + t.tv_usec / 1000000;
|
ch.timestamp = t.tv_sec;
|
||||||
ch.len = 10;
|
ch.len = 10;
|
||||||
ch.real_len = 10;
|
ch.real_len = 10;
|
||||||
ch.len_left = 0;
|
ch.len_left = 0;
|
||||||
|
@ -221,33 +308,18 @@ void SendUSR(unsigned char type, unsigned int data, unsigned int data2){
|
||||||
free(ch.data);
|
free(ch.data);
|
||||||
}//SendUSR
|
}//SendUSR
|
||||||
|
|
||||||
//ugly global, but who cares...
|
|
||||||
std::map<unsigned int, chunkinfo> prevmap;
|
|
||||||
|
|
||||||
//return previous packet of this cs_id
|
|
||||||
chunkinfo GetPrev(unsigned int cs_id){
|
|
||||||
return prevmap[cs_id];
|
|
||||||
}//GetPrev
|
|
||||||
|
|
||||||
//store packet information of last packet of this cs_id
|
|
||||||
void PutPrev(chunkpack prev){
|
|
||||||
prevmap[prev.cs_id].timestamp = prev.timestamp;
|
|
||||||
prevmap[prev.cs_id].len = prev.len;
|
|
||||||
prevmap[prev.cs_id].real_len = prev.real_len;
|
|
||||||
prevmap[prev.cs_id].len_left = prev.len_left;
|
|
||||||
prevmap[prev.cs_id].msg_type_id = prev.msg_type_id;
|
|
||||||
prevmap[prev.cs_id].msg_stream_id = prev.msg_stream_id;
|
|
||||||
}//PutPrev
|
|
||||||
|
|
||||||
//get a chunk from standard input
|
//get a chunk from standard input
|
||||||
struct chunkpack getChunk(){
|
struct chunkpack getChunk(){
|
||||||
|
gettimeofday(&lastrec, 0);
|
||||||
struct chunkpack ret;
|
struct chunkpack ret;
|
||||||
unsigned char temp;
|
unsigned char temp;
|
||||||
fread(&(ret.chunktype), 1, 1, stdin);
|
fread(&(ret.chunktype), 1, 1, stdin);
|
||||||
|
rec_cnt++;
|
||||||
//read the chunkstream ID properly
|
//read the chunkstream ID properly
|
||||||
switch (ret.chunktype & 0x3F){
|
switch (ret.chunktype & 0x3F){
|
||||||
case 0:
|
case 0:
|
||||||
fread(&temp, 1, 1, stdin);
|
fread(&temp, 1, 1, stdin);
|
||||||
|
rec_cnt++;
|
||||||
ret.cs_id = temp + 64;
|
ret.cs_id = temp + 64;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
|
@ -255,6 +327,7 @@ struct chunkpack getChunk(){
|
||||||
ret.cs_id = temp + 64;
|
ret.cs_id = temp + 64;
|
||||||
fread(&temp, 1, 1, stdin);
|
fread(&temp, 1, 1, stdin);
|
||||||
ret.cs_id += temp * 256;
|
ret.cs_id += temp * 256;
|
||||||
|
rec_cnt+=2;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ret.cs_id = ret.chunktype & 0x3F;
|
ret.cs_id = ret.chunktype & 0x3F;
|
||||||
|
@ -287,6 +360,7 @@ struct chunkpack getChunk(){
|
||||||
ret.msg_stream_id += temp*256;
|
ret.msg_stream_id += temp*256;
|
||||||
fread(&temp, 1, 1, stdin);
|
fread(&temp, 1, 1, stdin);
|
||||||
ret.msg_stream_id += temp;
|
ret.msg_stream_id += temp;
|
||||||
|
rec_cnt+=11;
|
||||||
break;
|
break;
|
||||||
case 0x40:
|
case 0x40:
|
||||||
fread(&temp, 1, 1, stdin);
|
fread(&temp, 1, 1, stdin);
|
||||||
|
@ -306,6 +380,7 @@ struct chunkpack getChunk(){
|
||||||
fread(&temp, 1, 1, stdin);
|
fread(&temp, 1, 1, stdin);
|
||||||
ret.msg_type_id = temp;
|
ret.msg_type_id = temp;
|
||||||
ret.msg_stream_id = prev.msg_stream_id;
|
ret.msg_stream_id = prev.msg_stream_id;
|
||||||
|
rec_cnt+=7;
|
||||||
break;
|
break;
|
||||||
case 0x80:
|
case 0x80:
|
||||||
fread(&temp, 1, 1, stdin);
|
fread(&temp, 1, 1, stdin);
|
||||||
|
@ -319,6 +394,7 @@ struct chunkpack getChunk(){
|
||||||
ret.len_left = prev.len_left;
|
ret.len_left = prev.len_left;
|
||||||
ret.msg_type_id = prev.msg_type_id;
|
ret.msg_type_id = prev.msg_type_id;
|
||||||
ret.msg_stream_id = prev.msg_stream_id;
|
ret.msg_stream_id = prev.msg_stream_id;
|
||||||
|
rec_cnt+=3;
|
||||||
break;
|
break;
|
||||||
case 0xC0:
|
case 0xC0:
|
||||||
ret.timestamp = prev.timestamp;
|
ret.timestamp = prev.timestamp;
|
||||||
|
@ -349,11 +425,13 @@ struct chunkpack getChunk(){
|
||||||
ret.timestamp += temp*256;
|
ret.timestamp += temp*256;
|
||||||
fread(&temp, 1, 1, stdin);
|
fread(&temp, 1, 1, stdin);
|
||||||
ret.timestamp += temp;
|
ret.timestamp += temp;
|
||||||
|
rec_cnt+=4;
|
||||||
}
|
}
|
||||||
//read data if length > 0, and allocate it
|
//read data if length > 0, and allocate it
|
||||||
if (ret.real_len > 0){
|
if (ret.real_len > 0){
|
||||||
ret.data = (unsigned char*)malloc(ret.real_len);
|
ret.data = (unsigned char*)malloc(ret.real_len);
|
||||||
fread(ret.data, 1, ret.real_len, stdin);
|
fread(ret.data, 1, ret.real_len, stdin);
|
||||||
|
rec_cnt+=ret.real_len;
|
||||||
}else{
|
}else{
|
||||||
ret.data = 0;
|
ret.data = 0;
|
||||||
}
|
}
|
||||||
|
|
28
Connector_RTMP/flv_sock.cpp
Normal file
28
Connector_RTMP/flv_sock.cpp
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
SWBaseSocket::SWBaseError SWBerr;
|
||||||
|
char * FLVbuffer;
|
||||||
|
int FLV_len;
|
||||||
|
int FLVbs = 0;
|
||||||
|
|
||||||
|
void FLV_Readheader(SWUnixSocket & ss){
|
||||||
|
static char header[13];
|
||||||
|
while (ss.frecv(header, 13, &SWBerr) != 13){
|
||||||
|
//wait
|
||||||
|
}
|
||||||
|
}//FLV_Readheader
|
||||||
|
|
||||||
|
bool FLV_GetPacket(SWUnixSocket & ss){
|
||||||
|
if (FLVbs < 15){FLVbuffer = (char*)realloc(FLVbuffer, 15); FLVbs = 15;}
|
||||||
|
//if received a whole header, receive a whole packet
|
||||||
|
//if not, retry header next pass
|
||||||
|
if (ss.frecv(FLVbuffer, 11, &SWBerr) == 11){
|
||||||
|
FLV_len = FLVbuffer[3] + 15;
|
||||||
|
FLV_len += (FLVbuffer[2] << 8);
|
||||||
|
FLV_len += (FLVbuffer[1] << 16);
|
||||||
|
if (FLVbs < FLV_len){FLVbuffer = (char*)realloc(FLVbuffer, FLV_len);FLVbs = FLV_len;}
|
||||||
|
while (ss.frecv(FLVbuffer+11, FLV_len-11, &SWBerr) != FLV_len-11){
|
||||||
|
//wait
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}//FLV_GetPacket
|
|
@ -4,6 +4,8 @@ struct Handshake {
|
||||||
char Random[1528];
|
char Random[1528];
|
||||||
};//Handshake
|
};//Handshake
|
||||||
|
|
||||||
|
char * versionstring = "PLSRTMPServer";
|
||||||
|
|
||||||
void doHandshake(){
|
void doHandshake(){
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
char Version;
|
char Version;
|
||||||
|
@ -15,10 +17,13 @@ void doHandshake(){
|
||||||
fread(Client.Time, 1, 4, stdin);
|
fread(Client.Time, 1, 4, stdin);
|
||||||
fread(Client.Zero, 1, 4, stdin);
|
fread(Client.Zero, 1, 4, stdin);
|
||||||
fread(Client.Random, 1, 1528, stdin);
|
fread(Client.Random, 1, 1528, stdin);
|
||||||
|
rec_cnt+=1537;
|
||||||
/** Build S1 Packet **/
|
/** Build S1 Packet **/
|
||||||
Server.Time[0] = 0; Server.Time[1] = 0; Server.Time[2] = 0; Server.Time[3] = 0;
|
Server.Time[0] = 0; Server.Time[1] = 0; Server.Time[2] = 0; Server.Time[3] = 0;
|
||||||
Server.Zero[0] = 0; Server.Zero[1] = 0; Server.Zero[2] = 0; Server.Zero[3] = 0;
|
Server.Zero[0] = 0; Server.Zero[1] = 0; Server.Zero[2] = 0; Server.Zero[3] = 0;
|
||||||
for (int i = 0; i < 1528; i++){Server.Random[i] = (rand() % 256);}
|
for (int i = 0; i < 1528; i++){
|
||||||
|
Server.Random[i] = versionstring[i%13];
|
||||||
|
}
|
||||||
/** Send S0 **/
|
/** Send S0 **/
|
||||||
fwrite(&(Version), 1, 1, stdout);
|
fwrite(&(Version), 1, 1, stdout);
|
||||||
/** Send S1 **/
|
/** Send S1 **/
|
||||||
|
@ -27,14 +32,17 @@ void doHandshake(){
|
||||||
fwrite(Server.Random, 1, 1528, stdout);
|
fwrite(Server.Random, 1, 1528, stdout);
|
||||||
/** Flush output, just for certainty **/
|
/** Flush output, just for certainty **/
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
snd_cnt+=1537;
|
||||||
/** Send S2 **/
|
/** Send S2 **/
|
||||||
fwrite(Client.Time, 1, 4, stdout);
|
fwrite(Client.Time, 1, 4, stdout);
|
||||||
fwrite(Client.Time, 1, 4, stdout);
|
fwrite(Client.Time, 1, 4, stdout);
|
||||||
fwrite(Client.Random, 1, 1528, stdout);
|
fwrite(Client.Random, 1, 1528, stdout);
|
||||||
|
snd_cnt+=1536;
|
||||||
/** Flush, necessary in order to work **/
|
/** Flush, necessary in order to work **/
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
/** Read and discard C2 **/
|
/** Read and discard C2 **/
|
||||||
fread(Client.Time, 1, 4, stdin);
|
fread(Client.Time, 1, 4, stdin);
|
||||||
fread(Client.Zero, 1, 4, stdin);
|
fread(Client.Zero, 1, 4, stdin);
|
||||||
fread(Client.Random, 1, 1528, stdin);
|
fread(Client.Random, 1, 1528, stdin);
|
||||||
|
rec_cnt+=1536;
|
||||||
}//doHandshake
|
}//doHandshake
|
|
@ -14,45 +14,52 @@
|
||||||
#include "../sockets/SocketW.h"
|
#include "../sockets/SocketW.h"
|
||||||
bool ready4data = false;//set to true when streaming starts
|
bool ready4data = false;//set to true when streaming starts
|
||||||
bool inited = false;
|
bool inited = false;
|
||||||
#include "../util/flv.cpp" //FLV format parser
|
timeval lastrec;
|
||||||
#include "handshake.cpp" //handshaking
|
|
||||||
#include "parsechunks.cpp" //chunkstream parsing
|
#include "parsechunks.cpp" //chunkstream parsing
|
||||||
|
#include "handshake.cpp" //handshaking
|
||||||
|
#include "flv_sock.cpp" //FLV parsing with SocketW
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
SWUnixSocket ss;
|
SWUnixSocket ss;
|
||||||
FLV_Pack * FLV = 0;
|
|
||||||
int ssfd = 0;
|
|
||||||
fd_set pollset;
|
fd_set pollset;
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
|
struct timeval now;
|
||||||
//0 timeout - return immediately after select call
|
//0 timeout - return immediately after select call
|
||||||
timeout.tv_sec = 1; timeout.tv_usec = 0;
|
timeout.tv_sec = 1; timeout.tv_usec = 0;
|
||||||
FD_ZERO(&pollset);//clear the polling set
|
FD_ZERO(&pollset);//clear the polling set
|
||||||
FD_SET(0, &pollset);//add stdin to polling set
|
FD_SET(0, &pollset);//add stdin to polling set
|
||||||
|
|
||||||
|
fprintf(stderr, "Doing handshake...\n");
|
||||||
doHandshake();
|
doHandshake();
|
||||||
|
fprintf(stderr, "Starting processing...\n");
|
||||||
while (!feof(stdin)){
|
while (!feof(stdin)){
|
||||||
select(1, &pollset, 0, 0, &timeout);
|
select(1, &pollset, 0, 0, &timeout);
|
||||||
//only parse input from stdin if available
|
//only parse input from stdin if available or not yet init'ed
|
||||||
if (FD_ISSET(0, &pollset)){parseChunk();}
|
if (FD_ISSET(0, &pollset) || !ready4data){parseChunk();fflush(stdout);}// || !ready4data?
|
||||||
if (ready4data){
|
if (ready4data){
|
||||||
if (!inited){
|
if (!inited){
|
||||||
//we are ready, connect the socket!
|
//we are ready, connect the socket!
|
||||||
ss.connect("../shared_socket");
|
if (!ss.connect("../shared_socket")){
|
||||||
ssfd = ss.get_fd(0);
|
fprintf(stderr, "Could not connect to server!\n");
|
||||||
if (ssfd > 0){FD_SET(ssfd, &pollset);}else{return 1;}
|
return 1;
|
||||||
FLV_Readheader(ssfd);//read the header, we don't want it
|
}
|
||||||
fprintf(stderr, "Header read\n");
|
FLV_Readheader(ss);//read the header, we don't want it
|
||||||
|
fprintf(stderr, "Header read, starting to send video data...\n");
|
||||||
inited = true;
|
inited = true;
|
||||||
}
|
}
|
||||||
//only deal with FLV packets if we have any to receive
|
//only send data if previous data has been ACK'ed...
|
||||||
if (FD_ISSET(ssfd, &pollset)){
|
if (snd_cnt - snd_window_at < snd_window_size){
|
||||||
fprintf(stderr, "Getting packet...\n");
|
if (FLV_GetPacket(ss)){//able to read a full packet?
|
||||||
FLV_GetPacket(FLV, ssfd);//read a full packet
|
SendMedia((unsigned char)FLVbuffer[0], (unsigned char *)FLVbuffer+11, FLV_len-15);
|
||||||
fprintf(stderr, "Sending a type %hhx packet...\n", (unsigned char)FLV->data[0]);
|
}
|
||||||
SendMedia((unsigned char)FLV->data[0], (unsigned char *)FLV->data+11, FLV->len-15);
|
|
||||||
fprintf(stderr, "Packet sent.\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//send ACK if we received a whole window
|
||||||
|
if (rec_cnt - rec_window_at > rec_window_size){
|
||||||
|
rec_window_at = rec_cnt;
|
||||||
|
SendCTL(3, rec_cnt);//send ack (msg 3)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}//main
|
}//main
|
||||||
|
|
|
@ -34,6 +34,8 @@ void parseChunk(){
|
||||||
case 5://window size of other end
|
case 5://window size of other end
|
||||||
fprintf(stderr, "CTRL: Window size\n");
|
fprintf(stderr, "CTRL: Window size\n");
|
||||||
rec_window_size = ntohl(*(int*)next.data);
|
rec_window_size = ntohl(*(int*)next.data);
|
||||||
|
rec_window_at = rec_cnt;
|
||||||
|
SendCTL(3, rec_cnt);//send ack (msg 3)
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
fprintf(stderr, "CTRL: Set peer bandwidth\n");
|
fprintf(stderr, "CTRL: Set peer bandwidth\n");
|
||||||
|
@ -73,19 +75,30 @@ void parseChunk(){
|
||||||
tmpint = amfdata.getContentP(2)->getContentP("audioCodecs")->NumValue();
|
tmpint = amfdata.getContentP(2)->getContentP("audioCodecs")->NumValue();
|
||||||
if (tmpint & 0x04){fprintf(stderr, "MP3 audio support detected\n");}
|
if (tmpint & 0x04){fprintf(stderr, "MP3 audio support detected\n");}
|
||||||
if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");}
|
if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");}
|
||||||
|
SendCTL(6, rec_window_size, 0);//send peer bandwidth (msg 6)
|
||||||
|
//SendCTL(5, snd_window_size);//send window acknowledgement size (msg 5)
|
||||||
|
SendUSR(0, 0);//send UCM StreamBegin (0), stream 0
|
||||||
//send a _result reply
|
//send a _result reply
|
||||||
AMFType amfreply("container", (unsigned char)0xFF);
|
AMFType amfreply("container", (unsigned char)0xFF);
|
||||||
amfreply.addContent(AMFType("", "_result"));//result success
|
amfreply.addContent(AMFType("", "_result"));//result success
|
||||||
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
||||||
amfreply.addContent(AMFType("", (double)0, 0x05));//null - properties (none?)
|
// amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info
|
||||||
|
amfreply.addContent(AMFType(""));//server properties
|
||||||
|
amfreply.getContentP(2)->addContent(AMFType("fmsVer", "FMS/3,0,1,123"));//stolen from examples
|
||||||
|
amfreply.getContentP(2)->addContent(AMFType("capabilities", (double)31));//stolen from examples
|
||||||
amfreply.addContent(AMFType(""));//info
|
amfreply.addContent(AMFType(""));//info
|
||||||
amfreply.getContentP(3)->addContent(AMFType("level", "status"));
|
amfreply.getContentP(3)->addContent(AMFType("level", "status"));
|
||||||
amfreply.getContentP(3)->addContent(AMFType("code", "NetConnection.Connect.Sucess"));
|
amfreply.getContentP(3)->addContent(AMFType("code", "NetConnection.Connect.Sucess"));
|
||||||
amfreply.getContentP(3)->addContent(AMFType("description", "Connection succeeded."));
|
amfreply.getContentP(3)->addContent(AMFType("description", "Connection succeeded."));
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("capabilities", (double)33));//from red5 server
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("fmsVer", "RED5/1,0,0,0"));//from red5 server
|
||||||
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
||||||
SendCTL(5, snd_window_size);//send window acknowledgement size (msg 5)
|
//send onBWDone packet
|
||||||
SendCTL(5, rec_window_size, 1);//send peer bandwidth (msg 6)
|
//amfreply = AMFType("container", (unsigned char)0xFF);
|
||||||
SendUSR(0, 10);//send UCM StreamBegin (0), stream 10 (we use this number)
|
//amfreply.addContent(AMFType("", "onBWDone"));//result success
|
||||||
|
//amfreply.addContent(AMFType("", (double)0));//zero
|
||||||
|
//amfreply.addContent(AMFType("", (double)0, 0x05));//null
|
||||||
|
//SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
||||||
}//connect
|
}//connect
|
||||||
if (amfdata.getContentP(0)->StrValue() == "createStream"){
|
if (amfdata.getContentP(0)->StrValue() == "createStream"){
|
||||||
//send a _result reply
|
//send a _result reply
|
||||||
|
@ -96,12 +109,36 @@ void parseChunk(){
|
||||||
amfreply.addContent(AMFType("", (double)10));//stream ID - we use 10
|
amfreply.addContent(AMFType("", (double)10));//stream ID - we use 10
|
||||||
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
||||||
}//createStream
|
}//createStream
|
||||||
|
if (amfdata.getContentP(0)->StrValue() == "getMovLen"){
|
||||||
|
//send a _result reply
|
||||||
|
AMFType amfreply("container", (unsigned char)0xFF);
|
||||||
|
amfreply.addContent(AMFType("", "_result"));//result success
|
||||||
|
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
||||||
|
amfreply.addContent(AMFType("", (double)6000));//null - command info
|
||||||
|
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
||||||
|
}//createStream
|
||||||
if ((amfdata.getContentP(0)->StrValue() == "play") || (amfdata.getContentP(0)->StrValue() == "play2")){
|
if ((amfdata.getContentP(0)->StrValue() == "play") || (amfdata.getContentP(0)->StrValue() == "play2")){
|
||||||
//send a status reply
|
//send a status reply
|
||||||
AMFType amfreply("container", (unsigned char)0xFF);
|
AMFType amfreply("container", (unsigned char)0xFF);
|
||||||
amfreply.addContent(AMFType("", "onStatus"));//status reply
|
amfreply.addContent(AMFType("", "onStatus"));//status reply
|
||||||
amfreply.addContent(AMFType("", "NetStream.Play.Start"));//result success
|
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
||||||
|
amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info
|
||||||
|
amfreply.addContent(AMFType(""));//info
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("level", "status"));
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("code", "NetStream.Play.Reset"));
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("description", "Playing and resetting..."));
|
||||||
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
||||||
|
amfreply = AMFType("container", (unsigned char)0xFF);
|
||||||
|
amfreply.addContent(AMFType("", "onStatus"));//status reply
|
||||||
|
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
||||||
|
amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info
|
||||||
|
amfreply.addContent(AMFType(""));//info
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("level", "status"));
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("code", "NetStream.Play.Start"));
|
||||||
|
amfreply.getContentP(3)->addContent(AMFType("description", "Playing!"));
|
||||||
|
SendChunk(3, 20, next.msg_stream_id, amfreply.Pack());
|
||||||
|
chunk_snd_max = 1024*1024;
|
||||||
|
SendCTL(1, chunk_snd_max);//send chunk size max (msg 1)
|
||||||
ready4data = true;//start sending video data!
|
ready4data = true;//start sending video data!
|
||||||
}//createStream
|
}//createStream
|
||||||
} break;
|
} break;
|
||||||
|
|
|
@ -31,7 +31,7 @@ int main( int argc, char * argv[] ) {
|
||||||
int open_connection = -1;
|
int open_connection = -1;
|
||||||
int lastproper = 0;//last properly finished buffer number
|
int lastproper = 0;//last properly finished buffer number
|
||||||
unsigned int loopcount = 0;
|
unsigned int loopcount = 0;
|
||||||
SWUnixSocket listener;
|
SWUnixSocket listener(SWBaseSocket::nonblocking);
|
||||||
SWBaseSocket * incoming = 0;
|
SWBaseSocket * incoming = 0;
|
||||||
SWBaseSocket::SWBaseError BError;
|
SWBaseSocket::SWBaseError BError;
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,7 @@ public:
|
||||||
// noWait - operations block but only once
|
// noWait - operations block but only once
|
||||||
// useful with blocking w. select()
|
// useful with blocking w. select()
|
||||||
// nonblocking - don't block (you should use select())
|
// nonblocking - don't block (you should use select())
|
||||||
enum block_type{blocking, noWait, nonblocking};
|
enum block_type{nonblocking, noWait, blocking};
|
||||||
|
|
||||||
|
|
||||||
// Connection methods
|
// Connection methods
|
||||||
|
|
25
util/flv.cpp
25
util/flv.cpp
|
@ -17,18 +17,6 @@ void Magic_Read(char * buf, int len, int file){
|
||||||
|
|
||||||
//reads a FLV header and checks for correctness
|
//reads a FLV header and checks for correctness
|
||||||
//returns true if everything is alright, false otherwise
|
//returns true if everything is alright, false otherwise
|
||||||
bool FLV_Readheader(int file){
|
|
||||||
Magic_Read(FLVHeader,13,file);
|
|
||||||
if (FLVHeader[0] != 'F') return false;
|
|
||||||
if (FLVHeader[1] != 'L') return false;
|
|
||||||
if (FLVHeader[2] != 'V') return false;
|
|
||||||
if (FLVHeader[8] != 0x09) return false;
|
|
||||||
if (FLVHeader[9] != 0) return false;
|
|
||||||
if (FLVHeader[10] != 0) return false;
|
|
||||||
if (FLVHeader[11] != 0) return false;
|
|
||||||
if (FLVHeader[12] != 0) return false;
|
|
||||||
return true;
|
|
||||||
}//FLV_Readheader
|
|
||||||
bool FLV_Readheader(){
|
bool FLV_Readheader(){
|
||||||
fread(FLVHeader,1,13,stdin);
|
fread(FLVHeader,1,13,stdin);
|
||||||
if (FLVHeader[0] != 'F') return false;
|
if (FLVHeader[0] != 'F') return false;
|
||||||
|
@ -46,17 +34,6 @@ bool FLV_Readheader(){
|
||||||
//will assign pointer if null
|
//will assign pointer if null
|
||||||
//resizes FLV_Pack data field bigger if data doesn't fit
|
//resizes FLV_Pack data field bigger if data doesn't fit
|
||||||
// (does not auto-shrink for speed!)
|
// (does not auto-shrink for speed!)
|
||||||
void FLV_GetPacket(FLV_Pack *& p, int file){
|
|
||||||
if (!p){p = (FLV_Pack*)calloc(1, sizeof(FLV_Pack));}
|
|
||||||
if (p->buf < 15){p->data = (char*)realloc(p->data, 15); p->buf = 15;}
|
|
||||||
Magic_Read(p->data,11,file);
|
|
||||||
p->len = p->data[3] + 15;
|
|
||||||
p->len += (p->data[2] << 8);
|
|
||||||
p->len += (p->data[1] << 16);
|
|
||||||
if (p->buf < p->len){p->data = (char*)realloc(p->data, p->len);}
|
|
||||||
Magic_Read(p->data+11,p->len-11,file);
|
|
||||||
}//FLV_GetPacket
|
|
||||||
|
|
||||||
void FLV_GetPacket(FLV_Pack *& p){
|
void FLV_GetPacket(FLV_Pack *& p){
|
||||||
if (!p){p = (FLV_Pack*)calloc(1, sizeof(FLV_Pack));}
|
if (!p){p = (FLV_Pack*)calloc(1, sizeof(FLV_Pack));}
|
||||||
if (p->buf < 15){p->data = (char*)realloc(p->data, 15); p->buf = 15;}
|
if (p->buf < 15){p->data = (char*)realloc(p->data, 15); p->buf = 15;}
|
||||||
|
@ -64,6 +41,6 @@ void FLV_GetPacket(FLV_Pack *& p){
|
||||||
p->len = p->data[3] + 15;
|
p->len = p->data[3] + 15;
|
||||||
p->len += (p->data[2] << 8);
|
p->len += (p->data[2] << 8);
|
||||||
p->len += (p->data[1] << 16);
|
p->len += (p->data[1] << 16);
|
||||||
if (p->buf < p->len){p->data = (char*)realloc(p->data, p->len);}
|
if (p->buf < p->len){p->data = (char*)realloc(p->data, p->len);p->buf = p->len;}
|
||||||
fread(p->data+11,1,p->len-11,stdin);
|
fread(p->data+11,1,p->len-11,stdin);
|
||||||
}//FLV_GetPacket
|
}//FLV_GetPacket
|
||||||
|
|
Loading…
Add table
Reference in a new issue