Stabiliteitsfixes en in theorie werkende H.264 en AAC, maar je weet maar nooit...

This commit is contained in:
Thulinma 2010-08-24 00:13:52 +02:00 committed by Thulinma
parent a34a9b69cc
commit 94f587563c
10 changed files with 144 additions and 54 deletions

View file

@ -2,5 +2,6 @@
struct buffer{
int number;
bool iskeyframe;
FLV_Pack * FLV;
};//buffer

View file

@ -37,9 +37,11 @@ int main( int argc, char * argv[] ) {
unlink("/tmp/shared_socket");
listener.bind("/tmp/shared_socket");
listener.listen();
listener.listen(50);
listener.set_timeout(0,50000);
unsigned char packtype;
bool gotVideoInfo = false;
bool gotAudioInfo = false;
while(std::cin.good()) {
loopcount ++;
//invalidate the current buffer
@ -49,25 +51,62 @@ int main( int argc, char * argv[] ) {
FLV_Readheader();
} else {
FLV_GetPacket(ringbuf[current_buffer]->FLV);
//if video frame? (id 9) check for incoming connections
if (ringbuf[current_buffer]->FLV->data[0] == 0x12){
packtype = ringbuf[current_buffer]->FLV->data[0];
//store metadata, if available
if (packtype == 0x12){
metabuflen = ringbuf[current_buffer]->FLV->len;
metabuffer = (char*)realloc(metabuffer, metabuflen);
memcpy(metabuffer, ringbuf[current_buffer]->FLV->data, metabuflen);
std::cout << "Received metadata!" << std::endl;
gotVideoInfo = false;
gotAudioInfo = false;
}
if (!gotVideoInfo && ringbuf[current_buffer]->FLV->isKeyframe){
if ((ringbuf[current_buffer]->FLV->data[11] & 0x0f) == 7){//avc packet
if (ringbuf[current_buffer]->FLV->data[12] == 0){
ringbuf[current_buffer]->FLV->data[4] = 0;//timestamp to zero
ringbuf[current_buffer]->FLV->data[5] = 0;//timestamp to zero
ringbuf[current_buffer]->FLV->data[6] = 0;//timestamp to zero
metabuffer = (char*)realloc(metabuffer, metabuflen + ringbuf[current_buffer]->FLV->len);
memcpy(metabuffer+metabuflen, ringbuf[current_buffer]->FLV->data, ringbuf[current_buffer]->FLV->len);
metabuflen += ringbuf[current_buffer]->FLV->len;
gotVideoInfo = true;
std::cout << "Received video configuration!" << std::endl;
}
}else{gotVideoInfo = true;}//non-avc = no config...
}
if (!gotAudioInfo && (packtype == 0x08)){
if (((ringbuf[current_buffer]->FLV->data[11] & 0xf0) >> 4) == 10){//aac packet
ringbuf[current_buffer]->FLV->data[4] = 0;//timestamp to zero
ringbuf[current_buffer]->FLV->data[5] = 0;//timestamp to zero
ringbuf[current_buffer]->FLV->data[6] = 0;//timestamp to zero
metabuffer = (char*)realloc(metabuffer, metabuflen + ringbuf[current_buffer]->FLV->len);
memcpy(metabuffer+metabuflen, ringbuf[current_buffer]->FLV->data, ringbuf[current_buffer]->FLV->len);
metabuflen += ringbuf[current_buffer]->FLV->len;
gotAudioInfo = true;
std::cout << "Received audio configuration!" << std::endl;
}else{gotAudioInfo = true;}//no aac = no config...
}
//on keyframe set start point
if (packtype == 0x09){
if (((ringbuf[current_buffer]->FLV->data[11] & 0xf0) >> 4) == 1){lastproper = current_buffer;}
}
incoming = listener.accept(&BError);
if (incoming){
connectionList.push_back(user(incoming));
//send the FLV header
std::cout << "Client connected." << std::endl;
connectionList.back().MyBuffer = lastproper;
connectionList.back().MyBuffer_num = ringbuf[lastproper]->number;
connectionList.back().MyBuffer_num = -1;
//TODO: Do this more nicely?
if (connectionList.back().Conn->send(FLVHeader,13,0) != 13){
if (connectionList.back().Conn->send(FLVHeader,13,&BError) != 13){
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!");
}
}
if (connectionList.back().Conn->send(metabuffer,metabuflen,0) != metabuflen){
connectionList.back().disconnect("failed to receive metadata!");
if (BError != SWBaseSocket::ok){
connectionList.back().disconnect("Socket error: " + BError.get_error());
}
}
ringbuf[current_buffer]->number = loopcount;
@ -77,7 +116,6 @@ int main( int argc, char * argv[] ) {
(*connIt).Send(ringbuf, buffers);
}
//keep track of buffers
lastproper = current_buffer;
current_buffer++;
current_buffer %= buffers;
}

View file

@ -1,3 +1,4 @@
#!/bin/bash
ffmpeg -re -i "$1" -b 1024000 -ar 11025 -f flv - | ./Server_PLS 5000 5
ffmpeg -re -i "$1" -b 1024000 -ar 11025 -f flv - 2> /dev/null | ./Server_PLS 500

View file

@ -11,19 +11,28 @@ class user{
SWUnixSocket * Conn;
int MyBuffer;
int MyBuffer_num;
int MyBuffer_len;
int MyNum;
void * lastpointer;
static int UserCount;
static SWBaseSocket::SWBaseError err;
};//user
int user::UserCount = 0;
SWBaseSocket::SWBaseError user::err;
user::user(SWBaseSocket * newConn) {
Conn = (SWUnixSocket*)newConn;
is_connected = (Conn != 0);
MyNum = UserCount++;
std::cout << "User " << MyNum << " connected" << std::endl;
}
void user::disconnect(std::string reason) {
if (Conn) {
Conn->disconnect();
Conn->disconnect(&err);
Conn = NULL;
std::cout << "Disconnected user: " << reason << std::endl;
std::cout << "Disconnected user " << MyNum << ": " << reason << std::endl;
}
is_connected = false;
}
@ -32,24 +41,38 @@ void user::Send(buffer ** ringbuf, int buffers){
//not connected? cancel
if (!is_connected){return;}
//still waiting for next buffer? check it
if (MyBuffer_num < 0){MyBuffer_num = ringbuf[MyBuffer]->number;}
//still waiting? don't crash - wait longer.
if (MyBuffer_num < 0){return;}
//buffer number changed? disconnect
if ((ringbuf[MyBuffer]->number != MyBuffer_num)){
disconnect("Buffer number changed (connection too slow)");
if (MyBuffer_num < 0){
MyBuffer_num = ringbuf[MyBuffer]->number;
//still waiting? don't crash - wait longer.
if (MyBuffer_num < 0){
return;
}else{
MyBuffer_len = ringbuf[MyBuffer]->FLV->len;
lastpointer = ringbuf[MyBuffer]->FLV->data;
}
}
if (lastpointer != ringbuf[MyBuffer]->FLV->data){
disconnect("Buffer resize at wrong time... had to disconnect");
return;
}
SWBaseSocket::SWBaseError err;
int ret = Conn->fsend(ringbuf[MyBuffer]->FLV->data, ringbuf[MyBuffer]->FLV->len, &err);
int ret = Conn->fsend(ringbuf[MyBuffer]->FLV->data, MyBuffer_len, &err);
if ((err != SWBaseSocket::ok) && (err != SWBaseSocket::notReady)){
disconnect("Socket error");
disconnect("Socket error: " + err.get_error());
return;
}
if (ret == ringbuf[MyBuffer]->FLV->len){
if (ret == MyBuffer_len){
//completed a send - switch to next buffer
MyBuffer++;
MyBuffer %= buffers;
if ((ringbuf[MyBuffer]->number != MyBuffer_num)){
std::cout << "Warning: User " << MyNum << " was send corrupt video data and send to the next keyframe!" << std::endl;
do{
MyBuffer++;
MyBuffer %= buffers;
}while(!ringbuf[MyBuffer]->FLV->isKeyframe);
}else{
MyBuffer++;
MyBuffer %= buffers;
}
MyBuffer_num = -1;
lastpointer = 0;
}
}
}