Gearbox updates, FLV parser rewrite, buffer fixes...
This commit is contained in:
parent
de701fc58e
commit
82d5ed69a2
2 changed files with 102 additions and 59 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../sockets/SocketW.h"
|
#include "../sockets/SocketW.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -47,17 +49,17 @@ int main( int argc, char * argv[] ) {
|
||||||
unsigned char packtype;
|
unsigned char packtype;
|
||||||
bool gotVideoInfo = false;
|
bool gotVideoInfo = false;
|
||||||
bool gotAudioInfo = false;
|
bool gotAudioInfo = false;
|
||||||
while(std::cin.good() && std::cout.good()) {
|
|
||||||
loopcount ++;
|
//set stdin to be nonblocking
|
||||||
|
int flags = fcntl(0, F_GETFL, 0);
|
||||||
|
flags |= O_NONBLOCK;
|
||||||
|
fcntl(0, F_SETFL, flags);
|
||||||
|
|
||||||
|
while(!feof(stdin) && !ferror(stdin) && !All_Hell_Broke_Loose){
|
||||||
//invalidate the current buffer
|
//invalidate the current buffer
|
||||||
ringbuf[current_buffer]->number = -1;
|
ringbuf[current_buffer]->number = -1;
|
||||||
if (std::cin.peek() == 'F') {
|
if (FLV_GetPacket(ringbuf[current_buffer]->FLV)){
|
||||||
//new FLV file, read the file header again.
|
loopcount ++;
|
||||||
FLV_Readheader();
|
|
||||||
} else {
|
|
||||||
if (!FLV_GetPacket(ringbuf[current_buffer]->FLV)){
|
|
||||||
break;//wrong packet? something bust be broken. End program!
|
|
||||||
}
|
|
||||||
packtype = ringbuf[current_buffer]->FLV->data[0];
|
packtype = ringbuf[current_buffer]->FLV->data[0];
|
||||||
//store metadata, if available
|
//store metadata, if available
|
||||||
if (packtype == 0x12){
|
if (packtype == 0x12){
|
||||||
|
@ -98,35 +100,39 @@ int main( int argc, char * argv[] ) {
|
||||||
if (packtype == 0x09){
|
if (packtype == 0x09){
|
||||||
if (((ringbuf[current_buffer]->FLV->data[11] & 0xf0) >> 4) == 1){lastproper = current_buffer;}
|
if (((ringbuf[current_buffer]->FLV->data[11] & 0xf0) >> 4) == 1){lastproper = current_buffer;}
|
||||||
}
|
}
|
||||||
incoming = listener.accept(&BError);
|
//keep track of buffers
|
||||||
if (incoming){
|
current_buffer++;
|
||||||
connectionList.push_back(user(incoming));
|
current_buffer %= buffers;
|
||||||
//send the FLV header
|
ringbuf[current_buffer]->number = loopcount;
|
||||||
connectionList.back().MyBuffer = lastproper;
|
}
|
||||||
connectionList.back().MyBuffer_num = -1;
|
|
||||||
//TODO: Do this more nicely?
|
//check for new connections, accept them if there are any
|
||||||
if (connectionList.back().Conn->send(FLVHeader,13,&BError) != 13){
|
incoming = listener.accept(&BError);
|
||||||
connectionList.back().disconnect("failed to receive the header!");
|
if (incoming){
|
||||||
}else{
|
connectionList.push_back(user(incoming));
|
||||||
if (connectionList.back().Conn->send(metabuffer,metabuflen,&BError) != metabuflen){
|
//send the FLV header
|
||||||
connectionList.back().disconnect("failed to receive metadata!");
|
connectionList.back().MyBuffer = lastproper;
|
||||||
}
|
connectionList.back().MyBuffer_num = -1;
|
||||||
}
|
//TODO: Do this more nicely?
|
||||||
if (BError != SWBaseSocket::ok){
|
if (connectionList.back().Conn->send(FLVHeader,13,&BError) != 13){
|
||||||
connectionList.back().disconnect("Socket error: " + BError.get_error());
|
connectionList.back().disconnect("failed to receive the header!");
|
||||||
|
}else{
|
||||||
|
if (connectionList.back().Conn->send(metabuffer,metabuflen,&BError) != metabuflen){
|
||||||
|
connectionList.back().disconnect("failed to receive metadata!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ringbuf[current_buffer]->number = loopcount;
|
if (BError != SWBaseSocket::ok){
|
||||||
//send all connections what they need, if and when they need it
|
connectionList.back().disconnect("Socket error: " + BError.get_error());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//send all connections what they need, if and when they need it
|
||||||
|
if (connectionList.size() > 0){
|
||||||
for (connIt = connectionList.begin(); connIt != connectionList.end(); connIt++){
|
for (connIt = connectionList.begin(); connIt != connectionList.end(); connIt++){
|
||||||
if (!(*connIt).is_connected){connectionList.erase(connIt);break;}
|
if (!(*connIt).is_connected){connectionList.erase(connIt);break;}
|
||||||
(*connIt).Send(ringbuf, buffers);
|
(*connIt).Send(ringbuf, buffers);
|
||||||
}
|
}
|
||||||
//keep track of buffers
|
|
||||||
current_buffer++;
|
|
||||||
current_buffer %= buffers;
|
|
||||||
}
|
}
|
||||||
}
|
}//main loop
|
||||||
|
|
||||||
// disconnect listener
|
// disconnect listener
|
||||||
std::cout << "Reached EOF of input" << std::endl;
|
std::cout << "Reached EOF of input" << std::endl;
|
||||||
|
|
93
util/flv.cpp
93
util/flv.cpp
|
@ -8,43 +8,80 @@ struct FLV_Pack {
|
||||||
};//FLV_Pack
|
};//FLV_Pack
|
||||||
|
|
||||||
char FLVHeader[13];
|
char FLVHeader[13];
|
||||||
|
bool All_Hell_Broke_Loose = false;
|
||||||
|
|
||||||
//reads full length from a file descriptor
|
//checks FLV Header for correctness
|
||||||
void Magic_Read(char * buf, int len, int file){
|
|
||||||
int i = 0;
|
|
||||||
while (i < len) i += read(file, buf, len-i);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//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(){
|
bool FLV_Checkheader(char * header){
|
||||||
fread(FLVHeader,1,13,stdin);
|
if (header[0] != 'F') return false;
|
||||||
if (FLVHeader[0] != 'F') return false;
|
if (header[1] != 'L') return false;
|
||||||
if (FLVHeader[1] != 'L') return false;
|
if (header[2] != 'V') return false;
|
||||||
if (FLVHeader[2] != 'V') return false;
|
if (header[8] != 0x09) return false;
|
||||||
if (FLVHeader[8] != 0x09) return false;
|
if (header[9] != 0) return false;
|
||||||
if (FLVHeader[9] != 0) return false;
|
if (header[10] != 0) return false;
|
||||||
if (FLVHeader[10] != 0) return false;
|
if (header[11] != 0) return false;
|
||||||
if (FLVHeader[11] != 0) return false;
|
if (header[12] != 0) return false;
|
||||||
if (FLVHeader[12] != 0) return false;
|
|
||||||
return true;
|
return true;
|
||||||
}//FLV_Readheader
|
}//FLV_Checkheader
|
||||||
|
|
||||||
|
//returns true if header is an FLV header
|
||||||
|
bool FLV_Isheader(char * header){
|
||||||
|
if (header[0] != 'F') return false;
|
||||||
|
if (header[1] != 'L') return false;
|
||||||
|
if (header[2] != 'V') return false;
|
||||||
|
return true;
|
||||||
|
}//FLV_Isheader
|
||||||
|
|
||||||
|
bool ReadUntil(char * buffer, unsigned int count, unsigned int & sofar){
|
||||||
|
if (sofar >= count){return true;}
|
||||||
|
int r = 0;
|
||||||
|
r = fread(buffer + sofar,1,count-sofar,stdin);
|
||||||
|
if (r < 0){All_Hell_Broke_Loose = true; return false;}
|
||||||
|
sofar += r;
|
||||||
|
if (sofar >= count){return true;}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//gets a packet, storing in given FLV_Pack pointer.
|
//gets a packet, storing in given FLV_Pack pointer.
|
||||||
//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!)
|
||||||
bool FLV_GetPacket(FLV_Pack *& p){
|
bool FLV_GetPacket(FLV_Pack *& p){
|
||||||
|
static bool done = true;
|
||||||
|
static unsigned int sofar = 0;
|
||||||
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;}
|
||||||
if (fread(p->data,1,11,stdin) != 11){return false;}
|
|
||||||
p->len = p->data[3] + 15;
|
if (done){
|
||||||
p->len += (p->data[2] << 8);
|
//read a header
|
||||||
p->len += (p->data[1] << 16);
|
if (ReadUntil(p->data, 11, sofar)){
|
||||||
if (p->buf < p->len){p->data = (char*)realloc(p->data, p->len);p->buf = p->len;}
|
//if its a correct FLV header, throw away and read tag header
|
||||||
if (fread(p->data+11,1,p->len-11,stdin) != (unsigned int)(p->len-11)){return false;}
|
if (FLV_Isheader(p->data)){
|
||||||
p->isKeyframe = false;
|
if (ReadUntil(p->data, 13, sofar)){
|
||||||
if ((p->data[0] == 0x09) && (((p->data[11] & 0xf0) >> 4) == 1)){p->isKeyframe = true;}
|
if (FLV_Checkheader(p->data)){
|
||||||
return true;
|
sofar = 0;
|
||||||
|
memcpy(FLVHeader, p->data, 13);
|
||||||
|
}else{All_Hell_Broke_Loose = true;}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
//if a tag header, calculate length and read tag body
|
||||||
|
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);p->buf = p->len;}
|
||||||
|
done = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
//read tag body
|
||||||
|
if (ReadUntil(p->data, p->len, sofar)){
|
||||||
|
//calculate keyframeness, next time read header again, return true
|
||||||
|
p->isKeyframe = false;
|
||||||
|
if ((p->data[0] == 0x09) && (((p->data[11] & 0xf0) >> 4) == 1)){p->isKeyframe = true;}
|
||||||
|
done = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}//FLV_GetPacket
|
}//FLV_GetPacket
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue