Restructuring + installer scripts

This commit is contained in:
Thulinma 2010-10-20 03:23:28 +02:00
parent 006da11595
commit ce6843044c
11 changed files with 34 additions and 9 deletions

18
Buffer/Makefile Normal file
View file

@ -0,0 +1,18 @@
SRC = main.cpp ../sockets/sw_base.cpp ../sockets/sw_inet.cpp ../sockets/sw_unix.cpp
OBJ = $(SRC:.cpp=.o)
OUT = Buffer
INCLUDES =
CCFLAGS = -Wall -Wextra -funsigned-char -g
CC = $(CROSS)g++
LD = $(CROSS)ld
AR = $(CROSS)ar
LIBS =
.SUFFIXES: .cpp
.PHONY: clean default
default: $(OUT)
.cpp.o:
$(CC) $(INCLUDES) $(CCFLAGS) $(LIBS) -c $< -o $@
$(OUT): $(OBJ)
$(CC) $(LIBS) -o $(OUT) $(OBJ)
clean:
rm -rf $(OBJ) $(OUT) Makefile.bak *~

7
Buffer/buffer.h Normal file
View file

@ -0,0 +1,7 @@
#pragma once
struct buffer{
int number;
bool iskeyframe;
FLV_Pack * FLV;
};//buffer

133
Buffer/main.cpp Normal file
View file

@ -0,0 +1,133 @@
#include <iostream>
#include "../sockets/SocketW.h"
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <string.h>
#include <unistd.h>
#include "../util/flv.cpp" //FLV format parser
#include "user.cpp"
int get_empty( user ** list, int amount ) {
for (int i = 0; i < amount; i++ ){
if (!list[i]->is_connected){return i;}
}
return -1;
}
int main( int argc, char * argv[] ) {
if (argc < 2) {
std::cout << "usage: " << argv[0] << " buffers_count [streamname]" << std::endl;
return 1;
}
int metabuflen = 0;
char * metabuffer = 0;
int buffers = atoi(argv[1]);
buffer ** ringbuf = (buffer**) calloc (buffers,sizeof(buffer*));
std::vector<user> connectionList;
std::vector<user>::iterator connIt;
for (int i = 0; i < buffers; ++i) ringbuf[i] = new buffer;
int current_buffer = 0;
int lastproper = 0;//last properly finished buffer number
unsigned int loopcount = 0;
SWUnixSocket listener(SWBaseSocket::nonblocking);
SWBaseSocket * incoming = 0;
SWBaseSocket::SWBaseError BError;
std::string shared_socket = "/tmp/shared_socket";
if (argc > 2){
shared_socket = argv[2];
shared_socket = "/tmp/shared_socket_" + shared_socket;
}
unlink(shared_socket.c_str());
listener.bind(shared_socket.c_str());
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
ringbuf[current_buffer]->number = -1;
if (std::cin.peek() == 'F') {
//new FLV file, read the file header again.
FLV_Readheader();
} else {
FLV_GetPacket(ringbuf[current_buffer]->FLV);
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
connectionList.back().MyBuffer = lastproper;
connectionList.back().MyBuffer_num = -1;
//TODO: Do this more nicely?
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 (BError != SWBaseSocket::ok){
connectionList.back().disconnect("Socket error: " + BError.get_error());
}
}
ringbuf[current_buffer]->number = loopcount;
//send all connections what they need, if and when they need it
for (connIt = connectionList.begin(); connIt != connectionList.end(); connIt++){
if (!(*connIt).is_connected){connectionList.erase(connIt);break;}
(*connIt).Send(ringbuf, buffers);
}
//keep track of buffers
current_buffer++;
current_buffer %= buffers;
}
}
// disconnect listener
std::cout << "Reached EOF of input" << std::endl;
listener.disconnect(&BError);
return 0;
}

4
Buffer/play1000kbit.sh Executable file
View file

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

9
Buffer/playh264.sh Executable file
View file

@ -0,0 +1,9 @@
#!/bin/bash
#ffmpeg -y -i "$1" -acodec libfaac -ar 44100 -vcodec libx264 -b 1000k -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -i_qfactor 0.71 -keyint_min 25 -b_strategy 1 -g 150 -r 20 -f flv - 2> /dev/null | ./Server_PLS 500
#ffmpeg -y -i "$1" -ar 44100 -vcodec libx264 -b 1000k -g 150 -r 20 -f flv - | ./Buffer 500
ffmpeg -i "$1" -re -acodec aac -ar 11025 -vcodec libx264 -b 700k -vpre ultrafast -refs 1 -bf 0 -g 150 -f flv - 2> /dev/null | ./Buffer 500

78
Buffer/user.cpp Normal file
View file

@ -0,0 +1,78 @@
#include "buffer.h"
#include "../sockets/SocketW.h"
#include <iostream>
class user{
public:
user(SWBaseSocket * newConn);
void disconnect(std::string reason);
void Send(buffer ** ringbuf, int buffers);
bool is_connected;
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(&err);
Conn = NULL;
std::cout << "Disconnected user " << MyNum << ": " << reason << std::endl;
}
is_connected = false;
}
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;
}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;
}
int ret = Conn->fsend(ringbuf[MyBuffer]->FLV->data, MyBuffer_len, &err);
if ((err != SWBaseSocket::ok) && (err != SWBaseSocket::notReady)){
disconnect("Socket error: " + err.get_error());
return;
}
if (ret == MyBuffer_len){
//completed a send - switch to next buffer
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;
}
}