First compiling version of DTSC-powered Buffer. Merged DTSC and DTMI into DTSC for ease of use. Todo: FLV2DTSC executable, DTSC support in all connectors...

This commit is contained in:
Thulinma 2011-09-13 00:32:35 +02:00
parent 7ee6500c3b
commit f9f13a1fa1
8 changed files with 520 additions and 500 deletions

View file

@ -1,4 +1,4 @@
SRC = main.cpp ../util/json/json_reader.cpp ../util/json/json_value.cpp ../util/json/json_writer.cpp ../util/socket.cpp ../util/flv_tag.cpp
SRC = main.cpp ../util/json/json_reader.cpp ../util/json/json_value.cpp ../util/json/json_writer.cpp ../util/socket.cpp ../util/dtsc.cpp
OBJ = $(SRC:.cpp=.o)
OUT = DDV_Buffer
INCLUDES =

View file

@ -10,8 +10,7 @@
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sstream>
#include "../util/flv_tag.h" //FLV format parser
#include "../util/dtsc.h" //DTSC support
#include "../util/socket.h" //Socket lib
#include "../util/json/json.h"
@ -28,11 +27,7 @@ namespace Buffer{
}
}
///holds FLV::Tag objects and their numbers
struct buffer{
int number;
FLV::Tag FLV;
};//buffer
DTSC::Stream * Strm = 0;
/// Converts a stats line to up, down, host, connector and conntime values.
class Stats{
@ -76,9 +71,7 @@ namespace Buffer{
/// Keeps track of what buffer users are using and the connection status.
class user{
public:
int MyBuffer; ///< Index of currently used buffer.
int MyBuffer_num; ///< Number of currently used buffer.
int MyBuffer_len; ///< Length in bytes of currently used buffer.
DTSC::Ring * myRing; ///< Ring of the buffer for this user.
int MyNum; ///< User ID of this user.
std::string MyStr; ///< User ID of this user as a string.
int currsend; ///< Current amount of bytes sent.
@ -97,11 +90,16 @@ namespace Buffer{
std::stringstream st;
st << MyNum;
MyStr = st.str();
gotproperaudio = false;
curr_up = 0;
curr_down = 0;
currsend = 0;
myRing = Strm->getRing();
std::cout << "User " << MyNum << " connected" << std::endl;
}//constructor
/// Drops held DTSC::Ring class, if one is held.
~user(){
Strm->dropRing(myRing);
}//destructor
/// Disconnects the current user. Doesn't do anything if already disconnected.
/// Prints "Disconnected user" to stdout if disconnect took place.
void Disconnect(std::string reason) {
@ -119,70 +117,41 @@ namespace Buffer{
}//Disconnect
/// Tries to send the current buffer, returns true if success, false otherwise.
/// Has a side effect of dropping the connection if send will never complete.
bool doSend(){
int r = S.iwrite((char*)lastpointer+currsend, MyBuffer_len-currsend);
bool doSend(const char * ptr, int len){
int r = S.iwrite(ptr+currsend, len-currsend);
if (r <= 0){
if (errno == EWOULDBLOCK){return false;}
Disconnect(S.getError());
return false;
}
currsend += r;
return (currsend == MyBuffer_len);
return (currsend == len);
}//doSend
/// Try to send data to this user. Disconnects if any problems occur.
/// \param ringbuf Array of buffers (FLV:Tag with ID attached)
/// \param buffers Count of elements in ringbuf
void Send(buffer ** ringbuf, int buffers){
/// \todo For MP3: gotproperaudio - if false, only send if first byte is 0xFF and set to true
void Send(){
if (!S.connected()){return;}//cancel if not connected
//still waiting for next buffer? check it
if (MyBuffer_num < 0){
MyBuffer_num = ringbuf[MyBuffer]->number;
if (MyBuffer_num < 0){
return; //still waiting? don't crash - wait longer.
}else{
MyBuffer_len = ringbuf[MyBuffer]->FLV.len;
lastpointer = ringbuf[MyBuffer]->FLV.data;
}
}
//do check for buffer resizes
if (lastpointer != ringbuf[MyBuffer]->FLV.data){
Disconnect("Buffer resize at wrong time... had to disconnect");
return;
}
if (myRing->waiting){return;}//still waiting for next buffer?
//try to complete a send
if (doSend()){
if (doSend(Strm->outPacket(myRing->b).c_str(), Strm->outPacket(myRing->b).length())){
//switch to next buffer
if ((ringbuf[MyBuffer]->number != MyBuffer_num)){
//if corrupt data, warn and find keyframe
std::cout << "Warning: User " << MyNum << " was send corrupt video data and send to the next keyframe!" << std::endl;
int nocrashcount = 0;
do{
MyBuffer++;
nocrashcount++;
MyBuffer %= buffers;
}while(!ringbuf[MyBuffer]->FLV.isKeyframe && (nocrashcount < buffers));
//if keyframe not available, try again later
if (nocrashcount >= buffers){
std::cout << "Warning: No keyframe found in buffers! Skipping search for now..." << std::endl;
return;
}
}else{
MyBuffer++;
MyBuffer %= buffers;
if (myRing->b <= 0){myRing->waiting = true; return;}//no next buffer? go in waiting mode.
myRing->b--;
if (myRing->starved){
//if corrupt data, warn and get new DTSC::Ring
std::cout << "Warning: User was send corrupt video data and send to the next keyframe!" << std::endl;
Strm->dropRing(myRing);
myRing = Strm->getRing();
}
MyBuffer_num = -1;
lastpointer = 0;
currsend = 0;
}//completed a send
}//send
};
int user::UserCount = 0;
/// Starts a loop, waiting for connections to send video data to.
/// Starts a loop, waiting for connections to send data to.
int Start(int argc, char ** argv) {
//first make sure no segpipe signals will kill us
struct sigaction new_action;
@ -193,31 +162,26 @@ namespace Buffer{
//then check and parse the commandline
if (argc < 3) {
std::cout << "usage: " << argv[0] << " buffers_count streamname [awaiting_IP]" << std::endl;
std::cout << "usage: " << argv[0] << " streamname [awaiting_IP]" << std::endl;
return 1;
}
std::string waiting_ip = "";
bool ip_waiting = false;
Socket::Connection ip_input;
if (argc >= 4){
waiting_ip += argv[3];
waiting_ip += argv[2];
ip_waiting = true;
}
std::string shared_socket = "/tmp/shared_socket_";
shared_socket += argv[2];
shared_socket += argv[1];
Socket::Server SS(shared_socket, true);
FLV::Tag metadata;
FLV::Tag video_init;
FLV::Tag audio_init;
int buffers = atoi(argv[1]);
buffer ** ringbuf = (buffer**) calloc (buffers,sizeof(buffer*));
Strm = new DTSC::Stream(5);
std::vector<user> users;
std::vector<user>::iterator usersIt;
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;
std::string inBuffer;
char charBuffer[1024*10];
unsigned int charCount;
unsigned int stattimer = 0;
Socket::Connection incoming;
Socket::Connection std_input(fileno(stdin));
@ -226,11 +190,7 @@ namespace Buffer{
Storage["log"] = Json::Value(Json::objectValue);
Storage["curr"] = Json::Value(Json::objectValue);
Storage["totals"] = Json::Value(Json::objectValue);
unsigned char packtype;
bool gotVideoInfo = false;
bool gotAudioInfo = false;
bool gotData = false;
while((!feof(stdin) || ip_waiting) && !FLV::Parse_Error){
usleep(1000); //sleep for 1 ms, to prevent 100% CPU time
@ -259,89 +219,21 @@ namespace Buffer{
}
}
//invalidate the current buffer
ringbuf[current_buffer]->number = -1;
if (
(!ip_waiting &&
(std_input.canRead()) && ringbuf[current_buffer]->FLV.FileLoader(stdin)
) || (ip_waiting && (ip_input.connected()) &&
ringbuf[current_buffer]->FLV.SockLoader(ip_input)
)
){
loopcount++;
packtype = ringbuf[current_buffer]->FLV.data[0];
//store metadata, if available
if (packtype == 0x12){
metadata = ringbuf[current_buffer]->FLV;
std::cout << "Received metadata!" << std::endl;
if (gotVideoInfo && gotAudioInfo){
FLV::Parse_Error = true;
std::cout << "... after proper video and audio? Cancelling broadcast!" << std::endl;
}
gotVideoInfo = false;
gotAudioInfo = false;
}
//store video init data, if available
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.tagTime(0);//timestamp to zero
video_init = ringbuf[current_buffer]->FLV;
gotVideoInfo = true;
std::cout << "Received video configuration!" << std::endl;
}
}else{gotVideoInfo = true;}//non-avc = no config...
}
//store audio init data, if available
if (!gotAudioInfo && (packtype == 0x08)){
if (((ringbuf[current_buffer]->FLV.data[11] & 0xf0) >> 4) == 10){//aac packet
ringbuf[current_buffer]->FLV.tagTime(0);//timestamp to zero
audio_init = ringbuf[current_buffer]->FLV;
gotAudioInfo = true;
std::cout << "Received audio configuration!" << std::endl;
}else{gotAudioInfo = true;}//no aac = no config...
}
//on keyframe set possible start point
if (packtype == 0x09){
if (((ringbuf[current_buffer]->FLV.data[11] & 0xf0) >> 4) == 1){
lastproper = current_buffer;
}
}
if (loopcount > 5){gotData = true;}
//keep track of buffers
ringbuf[current_buffer]->number = loopcount;
current_buffer++;
current_buffer %= buffers;
if ( (!ip_waiting && std_input.canRead()) || (ip_waiting && ip_input.connected()) ){
std::cin.read(charBuffer, 1024*10);
charCount = std::cin.gcount();
inBuffer.append(charBuffer, charCount);
Strm->parsePacket(inBuffer);
}
//check for new connections, accept them if there are any
incoming = SS.accept(true);
if (incoming.connected()){
users.push_back(incoming);
//send the FLV header
users.back().currsend = 0;
users.back().MyBuffer = lastproper;
users.back().MyBuffer_num = -1;
/// \todo Do this more nicely?
if (gotData){
if (!users.back().S.write(FLV::Header, 13)){
users.back().Disconnect("failed to receive the header!");
}else{
if (metadata.len > 0){
if (!users.back().S.write(metadata.data, metadata.len)){
users.back().Disconnect("failed to receive metadata!");
}
}
if (audio_init.len > 0){
if (!users.back().S.write(audio_init.data, audio_init.len)){
users.back().Disconnect("failed to receive audio init!");
}
}
if (video_init.len > 0){
if (!users.back().S.write(video_init.data, video_init.len)){
users.back().Disconnect("failed to receive video init!");
}
}
}
//send the header
if (!users.back().S.write(Strm->outHeader())){
/// \todo Do this more nicely?
users.back().Disconnect("failed to receive the header!");
}
}
@ -391,18 +283,15 @@ namespace Buffer{
}
}
}
(*usersIt).Send(ringbuf, buffers);
(*usersIt).Send();
}
}
}
}//main loop
// disconnect listener
if (FLV::Parse_Error){
std::cout << "FLV parse error" << std::endl;
}else{
std::cout << "Reached EOF of input" << std::endl;
}
/// \todo Deal with EOF more nicely - doesn't send the end of the stream to all users!
std::cout << "Reached EOF of input" << std::endl;
SS.close();
while (users.size() > 0){
for (usersIt = users.begin(); usersIt != users.end(); usersIt++){

View file

@ -1,248 +0,0 @@
/// \file dtmi.cpp
/// Holds all code for DDVTECH MediaInfo parsing/generation.
#include "dtmi.h"
#include <cstdio> //needed for stderr only
/// Returns the std::string Indice for the current object, if available.
/// Returns an empty string if no indice exists.
std::string DTSC::DTMI::Indice(){return myIndice;};
/// Returns the DTSC::DTMItype AMF0 object type for this object.
DTSC::DTMItype DTSC::DTMI::GetType(){return myType;};
/// Returns the numeric value of this object, if available.
/// If this object holds no numeric value, 0 is returned.
uint64_t DTSC::DTMI::NumValue(){return numval;};
/// Returns the std::string value of this object, if available.
/// If this object holds no string value, an empty string is returned.
std::string DTSC::DTMI::StrValue(){return strval;};
/// Returns the C-string value of this object, if available.
/// If this object holds no string value, an empty C-string is returned.
const char * DTSC::DTMI::Str(){return strval.c_str();};
/// Returns a count of the amount of objects this object currently holds.
/// If this object is not a container type, this function will always return 0.
int DTSC::DTMI::hasContent(){return contents.size();};
/// Adds an DTSC::DTMI to this object. Works for all types, but only makes sense for container types.
void DTSC::DTMI::addContent(DTSC::DTMI c){contents.push_back(c);};
/// Returns a pointer to the object held at indice i.
/// Returns AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param i The indice of the object in this container.
DTSC::DTMI* DTSC::DTMI::getContentP(int i){return &contents.at(i);};
/// Returns a copy of the object held at indice i.
/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param i The indice of the object in this container.
DTSC::DTMI DTSC::DTMI::getContent(int i){return contents.at(i);};
/// Returns a pointer to the object held at indice s.
/// Returns NULL if no object is held at this indice.
/// \param s The indice of the object in this container.
DTSC::DTMI* DTSC::DTMI::getContentP(std::string s){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
if (it->Indice() == s){return &(*it);}
}
return 0;
};
/// Returns a copy of the object held at indice s.
/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param s The indice of the object in this container.
DTSC::DTMI DTSC::DTMI::getContent(std::string s){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
if (it->Indice() == s){return *it;}
}
return DTSC::DTMI("error", DTMI::DTMI_ROOT);
};
/// Default constructor.
/// Simply fills the data with DTSC::DTMI("error", AMF0_DDV_CONTAINER)
DTSC::DTMI::DTMI(){
*this = DTSC::DTMI("error", DTMI::DTMI_ROOT);
};//default constructor
/// Constructor for numeric objects.
/// The object type is by default AMF::AMF0_NUMBER, but this can be forced to a different value.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param val The numeric value of this object. Numeric AMF0 objects only support double-type values.
/// \param setType The object type to force this object to.
DTSC::DTMI::DTMI(std::string indice, double val, DTSC::DTMItype setType){//num type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = val;
};
/// Constructor for string objects.
/// The object type is by default AMF::AMF0_STRING, but this can be forced to a different value.
/// There is no need to manually change the type to AMF::AMF0_LONGSTRING, this will be done automatically.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param val The string value of this object.
/// \param setType The object type to force this object to.
DTSC::DTMI::DTMI(std::string indice, std::string val, DTSC::DTMItype setType){//str type initializer
myIndice = indice;
myType = setType;
strval = val;
numval = 0;
};
/// Constructor for container objects.
/// The object type is by default AMF::AMF0_OBJECT, but this can be forced to a different value.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param setType The object type to force this object to.
DTSC::DTMI::DTMI(std::string indice, DTSC::DTMItype setType){//object type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = 0;
};
/// Prints the contents of this object to std::cerr.
/// If this object contains other objects, it will call itself recursively
/// and print all nested content in a nice human-readable format.
void DTSC::DTMI::Print(std::string indent){
std::cerr << indent;
// print my type
switch (myType){
case DTMItype::DTMI_INT: std::cerr << "Integer"; break;
case DTMItype::DTMI_STRING: std::cerr << "String"; break;
case DTMItype::DTMI_OBJECT: std::cerr << "Object"; break;
case DTMItype::DTMI_OBJ_END: std::cerr << "Object end"; break;
case DTMItype::DTMI_ROOT: std::cerr << "Root Node"; break;
}
// print my string indice, if available
std::cerr << " " << myIndice << " ";
// print my numeric or string contents
switch (myType){
case DTMItype::DTMI_INT: std::cerr << numval; break;
case DTMItype::DTMI_STRING: std::cerr << strval; break;
default: break;//we don't care about the rest, and don't want a compiler warning...
}
std::cerr << std::endl;
// if I hold other objects, print those too, recursively.
if (contents.size() > 0){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){it->Print(indent+" ");}
}
};//print
/// Packs the AMF object to a std::string for transfer over the network.
/// If the object is a container type, this function will call itself recursively and contain all contents.
std::string DTSC::DTMI::Pack(){
std::string r = "";
//skip output of DDV container types, they do not exist. Only output their contents.
if (myType != DTMItype::DTMI_ROOT){r += myType;}
//output the properly formatted data stream for this object's contents.
switch (myType){
case DTMItype::DTMI_INT:
r += *(((char*)&numval)+7); r += *(((char*)&numval)+6);
r += *(((char*)&numval)+5); r += *(((char*)&numval)+4);
r += *(((char*)&numval)+3); r += *(((char*)&numval)+2);
r += *(((char*)&numval)+1); r += *(((char*)&numval));
break;
case DTMItype::DTMI_STRING:
r += strval.size() / (256*256*256);
r += strval.size() / (256*256);
r += strval.size() / 256;
r += strval.size() % 256;
r += strval;
break;
case DTMItype::DTMI_OBJECT:
if (contents.size() > 0){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Indice().size() / 256;
r += it->Indice().size() % 256;
r += it->Indice();
r += it->Pack();
}
}
r += (char)0; r += (char)0; r += (char)9;
break;
case DTMItype::DTMI_ROOT://only send contents
if (contents.size() > 0){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Pack();
}
}
break;
}
return r;
};//pack
/// Parses a single AMF0 type - used recursively by the AMF::parse() functions.
/// This function updates i every call with the new position in the data.
/// \param data The raw data to parse.
/// \param len The size of the raw data.
/// \param i Current parsing position in the raw data.
/// \param name Indice name for any new object created.
/// \returns A single DTSC::DTMI, parsed from the raw data.
DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){
std::string tmpstr;
unsigned int tmpi = 0;
unsigned char tmpdbl[8];
#if DEBUG >= 10
fprintf(stderr, "Note: AMF type %hhx found. %i bytes left\n", data[i], len-i);
#endif
switch (data[i]){
case DTMI::DTMI_INT:
tmpdbl[7] = data[i+1];
tmpdbl[6] = data[i+2];
tmpdbl[5] = data[i+3];
tmpdbl[4] = data[i+4];
tmpdbl[3] = data[i+5];
tmpdbl[2] = data[i+6];
tmpdbl[1] = data[i+7];
tmpdbl[0] = data[i+8];
i+=9;//skip 8(a double)+1 forwards
return DTSC::DTMI(name, *(uint64_t*)tmpdbl, AMF::AMF0_NUMBER);
break;
case DTMI::DTMI_STRING:
tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to UTF-8-long length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char *)data+i+5, (size_t)tmpi);//add the string data
i += tmpi + 5;//skip length+size+1 forwards
return DTSC::DTMI(name, tmpstr, AMF::AMF0_LONGSTRING);
break;
case DTMI::DTMI_OBJECT:{
++i;
DTSC::DTMI ret(name, DTMI::DTMI_OBJECT);
while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009)
tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data
i += tmpi + 2;//skip length+size forwards
ret.addContent(AMF::parseOne(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
}
i += 3;//skip 0x000009
return ret;
} break;
}
#if DEBUG >= 2
fprintf(stderr, "Error: Unimplemented DTMI type %hhx - returning.\n", data[i]);
#endif
return DTSC::DTMI("error", DTMI::DTMI_ROOT);
}//parseOne
/// Parses a C-string to a valid DTSC::DTMI.
/// This function will find all AMF objects in the string and return
/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI.
DTSC::DTMI DTSC::parseDTMI(const unsigned char * data, unsigned int len){
DTSC::DTMI ret("returned", DTMI::DTMI_ROOT);//container type
unsigned int i = 0, j = 0;
while (i < len){
ret.addContent(AMF::parseOne(data, len, i, ""));
if (i > j){j = i;}else{return ret;}
}
return ret;
}//parse
/// Parses a std::string to a valid DTSC::DTMI.
/// This function will find all AMF objects in the string and return
/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI.
DTSC::DTMI DTSC::parseDTMI(std::string data){
return parse((const unsigned char*)data.c_str(), data.size());
}//parse

View file

@ -1,58 +0,0 @@
/// \file dtmi.h
/// Holds all headers for DDVTECH MediaInfo parsing/generation.
#pragma once
#include <vector>
#include <iostream>
//#include <string.h>
#include <string>
/// Holds all DDVTECH Stream Container classes and parsers.
namespace DTSC{
/// Enumerates all possible DTMI types.
enum DTMItype {
DTMI_INT = 0x01, ///< Unsigned 64-bit integer.
DTMI_STRING = 0x02, ///< String, equivalent to the AMF longstring type.
DTMI_OBJECT = 0xE0, ///< Object, equivalent to the AMF object type.
DTMI_OBJ_END = 0xEE, ///< End of object marker.
DTMI_ROOT = 0xFF ///< Root node for all DTMI data.
};
/// Recursive class that holds DDVTECH MediaInfo.
class DTMI {
public:
std::string Indice();
DTMItype GetType();
uint64_t NumValue();
std::string StrValue();
const char * Str();
int hasContent();
void addContent(DTMI c);
DTMI* getContentP(int i);
DTMI getContent(int i);
DTMI* getContentP(std::string s);
DTMI getContent(std::string s);
DTMI();
DTMI(std::string indice, double val, DTMItype setType = DTMI_INT);
DTMI(std::string indice, std::string val, DTMItype setType = DTMI_STRING);
DTMI(std::string indice, DTMItype setType = DTMI_OBJECT);
void Print(std::string indent = "");
std::string Pack();
protected:
std::string myIndice; ///< Holds this objects indice, if any.
DTMItype myType; ///< Holds this objects AMF0 type.
std::string strval; ///< Holds this objects string value, if any.
uint64_t numval; ///< Holds this objects numeric value, if any.
std::vector<DTMI> contents; ///< Holds this objects contents, if any (for container types).
};//AMFType
/// Parses a C-string to a valid DTSC::DTMI.
DTMI parseDTMI(const unsigned char * data, unsigned int len);
/// Parses a std::string to a valid DTSC::DTMI.
DTMI parseDTMI(std::string data);
/// Parses a single DTMI type - used recursively by the DTSC::parseDTMI() functions.
DTMI parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name);
};//DTSC namespace

View file

@ -2,33 +2,50 @@
/// Holds all code for DDVTECH Stream Container parsing/generation.
#include "dtsc.h"
#include "string.h" //for memcmp
#include "arpa/inet.h" //for htonl/ntohl
#include <string.h> //for memcmp
#include <arpa/inet.h> //for htonl/ntohl
#include <stdio.h> //for fprint, stderr
char * DTSC::Magic_Header = "DTSC";
char * DTSC::Magic_Packet = "DTPD";
char DTSC::Magic_Header[] = "DTSC";
char DTSC::Magic_Packet[] = "DTPD";
/// Initializes a DTSC::Stream with only one packet buffer.
DTSC::Stream::Stream(){
datapointer = 0;
buffercount = 1;
}
/// Initializes a DTSC::Stream with a minimum of rbuffers packet buffers.
/// The actual buffer count may not at all times be the requested amount.
DTSC::Stream::Stream(unsigned int rbuffers){
datapointer = 0;
if (rbuffers < 1){rbuffers = 1;}
buffercount = rbuffers;
}
/// Attempts to parse a packet from the given std::string buffer.
/// Returns true if successful, removing the parsed part from the buffer string.
/// Returns false if invalid or not enough data is in the buffer.
/// \arg buffer The std::string buffer to attempt to parse.
bool DTSC::Stream::parsePacket(std::string & buffer){
uint32_t len;
if (buffer.length() > 8){
if (memcmp(buffer.c_str(), DTSC::Magic_Header, 4) == 0){
len = ntohl(((uint32_t *)buffer.c_str())[1]);
if (buffer.length() < len+8){return false;}
metadata = DTSC::parseDTMI(buffer.c_str() + 8, len);
metadata = DTSC::parseDTMI((unsigned char*)buffer.c_str() + 8, len);
buffer.erase(0, len+8);
}
if (memcmp(buffer.c_str(), DTSC::Magic_Packet, 4) == 0){
len = ntohl(((uint32_t *)buffer.c_str())[1]);
if (buffer.length() < len+8){return false;}
lastPacket = DTSC::parseDTMI(buffer.c_str() + 8, len);
buffers.push_front(DTSC::DTMI("empty", DTMI_ROOT));
buffers.front() = DTSC::parseDTMI((unsigned char*)buffer.c_str() + 8, len);
datapointertype = INVALID;
if (lastPacket.getContentP("data")){
datapointer = lastPacket.getContentP("data")->StrValue.c_str();
if (lastPacket.getContentP("datatype")){
std::string tmp = lastPacket.getContentP("datatype")->StrValue();
if (buffers.front().getContentP("data")){
datapointer = buffers.front().getContentP("data")->StrValue().c_str();
if (buffers.front().getContentP("datatype")){
std::string tmp = buffers.front().getContentP("datatype")->StrValue();
if (tmp == "video"){datapointertype = VIDEO;}
if (tmp == "audio"){datapointertype = AUDIO;}
if (tmp == "meta"){datapointertype = META;}
@ -36,23 +53,372 @@ bool DTSC::Stream::parsePacket(std::string & buffer){
}else{
datapointer = 0;
}
buffer.erase(0, len+8);
while (buffers.size() > buffercount){buffers.pop_back();}
advanceRings();
}
#if DEBUG >= 2
std::cerr << "Error: Invalid DTMI data! I *will* get stuck!" << std::endl;
#endif
}
return false;
}
char * DTSC::Stream::lastData(){
/// Returns a direct pointer to the data attribute of the last received packet, if available.
/// Returns NULL if no valid pointer or packet is available.
const char * DTSC::Stream::lastData(){
return datapointer;
}
/// Returns the packed in this buffer number.
/// \arg num Buffer number.
DTSC::DTMI & DTSC::Stream::getPacket(unsigned int num){
return buffers[num];
}
/// Returns the type of the last received packet.
DTSC::datatype DTSC::Stream::lastType(){
return datapointertype;
}
/// Returns true if the current stream contains at least one video track.
bool DTSC::Stream::hasVideo(){
return (metadata.getContentP("video") != 0);
}
/// Returns true if the current stream contains at least one audio track.
bool DTSC::Stream::hasAudio(){
return (metadata.getContentP("audio") != 0);
}
/// Returns a packed DTSC packet, ready to sent over the network.
std::string DTSC::Stream::outPacket(unsigned int num){
std::string tmp;
unsigned int size;
tmp = Magic_Packet;
size = htonl(buffers[num].Pack().length());
tmp.append((char*)&size, 4);
tmp.append(buffers[num].Pack());
return tmp;
}
/// Returns a packed DTSC header, ready to sent over the network.
std::string DTSC::Stream::outHeader(){
std::string tmp;
unsigned int size;
tmp = Magic_Header;
size = htonl(metadata.Pack().length());
tmp.append((char*)&size, 4);
tmp.append(metadata.Pack());
return tmp;
}
/// advances all given out and internal Ring classes to point to the new buffer, after one has been added.
/// Also updates the internal keyframes ring, as well as marking rings as starved if they are.
/// Unsets waiting rings, updating them with their new buffer number.
void DTSC::Stream::advanceRings(){
std::deque<DTSC::Ring>::iterator dit;
std::set<DTSC::Ring *>::iterator sit;
for (sit = rings.begin(); sit != rings.end(); sit++){
(*sit)->b++;
if ((*sit)->waiting){(*sit)->waiting = false; (*sit)->b = 0;}
if ((*sit)->b >= buffers.size()){(*sit)->starved = true;}
}
for (dit = keyframes.begin(); dit != keyframes.end(); dit++){
dit->b++;
if (dit->b >= buffers.size()){keyframes.erase(dit); break;}
}
if ((lastType() == VIDEO) && (buffers.front().getContentP("keyframe"))){
keyframes.push_front(DTSC::Ring(0));
}
//increase buffer size if no keyframes available
if ((buffercount > 1) && (keyframes.size() < 1)){buffercount++;}
}
/// Constructs a new Ring, at the given buffer position.
/// \arg v Position for buffer.
DTSC::Ring::Ring(unsigned int v){
b = v;
waiting = false;
starved = false;
}
/// Requests a new Ring, which will be created and added to the internal Ring list.
/// This Ring will be kept updated so it always points to valid data or has the starved boolean set.
/// Don't forget to call dropRing() for all requested Ring classes that are no longer neccessary!
DTSC::Ring * DTSC::Stream::getRing(){
DTSC::Ring * tmp;
if (keyframes.size() == 0){
tmp = new DTSC::Ring(0);
}else{
tmp = new DTSC::Ring(keyframes[0].b);
}
rings.insert(tmp);
return tmp;
}
/// Deletes a given out Ring class from memory and internal Ring list.
/// Checks for NULL pointers and invalid pointers, silently discarding them.
void DTSC::Stream::dropRing(DTSC::Ring * ptr){
if (rings.find(ptr) != rings.end()){
rings.erase(ptr);
delete ptr;
}
}
/// Properly cleans up the object for erasing.
/// Drops all Ring classes that have been given out.
DTSC::Stream::~Stream(){
std::set<DTSC::Ring *>::iterator sit;
for (sit = rings.begin(); sit != rings.end(); sit++){delete (*sit);}
}
/// Returns the std::string Indice for the current object, if available.
/// Returns an empty string if no indice exists.
std::string DTSC::DTMI::Indice(){return myIndice;};
/// Returns the DTSC::DTMItype AMF0 object type for this object.
DTSC::DTMItype DTSC::DTMI::GetType(){return myType;};
/// Returns the numeric value of this object, if available.
/// If this object holds no numeric value, 0 is returned.
uint64_t DTSC::DTMI::NumValue(){return numval;};
/// Returns the std::string value of this object, if available.
/// If this object holds no string value, an empty string is returned.
std::string DTSC::DTMI::StrValue(){return strval;};
/// Returns the C-string value of this object, if available.
/// If this object holds no string value, an empty C-string is returned.
const char * DTSC::DTMI::Str(){return strval.c_str();};
/// Returns a count of the amount of objects this object currently holds.
/// If this object is not a container type, this function will always return 0.
int DTSC::DTMI::hasContent(){return contents.size();};
/// Adds an DTSC::DTMI to this object. Works for all types, but only makes sense for container types.
/// This function resets DTMI::packed to an empty string, forcing a repack on the next call to DTMI::Pack.
void DTSC::DTMI::addContent(DTSC::DTMI c){contents.push_back(c); packed = "";};
/// Returns a pointer to the object held at indice i.
/// Returns AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param i The indice of the object in this container.
DTSC::DTMI* DTSC::DTMI::getContentP(int i){return &contents.at(i);};
/// Returns a copy of the object held at indice i.
/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param i The indice of the object in this container.
DTSC::DTMI DTSC::DTMI::getContent(int i){return contents.at(i);};
/// Returns a pointer to the object held at indice s.
/// Returns NULL if no object is held at this indice.
/// \param s The indice of the object in this container.
DTSC::DTMI* DTSC::DTMI::getContentP(std::string s){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
if (it->Indice() == s){return &(*it);}
}
return 0;
};
/// Returns a copy of the object held at indice s.
/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param s The indice of the object in this container.
DTSC::DTMI DTSC::DTMI::getContent(std::string s){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
if (it->Indice() == s){return *it;}
}
return DTSC::DTMI("error", DTMI_ROOT);
};
/// Default constructor.
/// Simply fills the data with DTSC::DTMI("error", AMF0_DDV_CONTAINER)
DTSC::DTMI::DTMI(){
*this = DTSC::DTMI("error", DTMI_ROOT);
};//default constructor
/// Constructor for numeric objects.
/// The object type is by default AMF::AMF0_NUMBER, but this can be forced to a different value.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param val The numeric value of this object. Numeric AMF0 objects only support double-type values.
/// \param setType The object type to force this object to.
DTSC::DTMI::DTMI(std::string indice, double val, DTSC::DTMItype setType){//num type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = val;
};
/// Constructor for string objects.
/// The object type is by default AMF::AMF0_STRING, but this can be forced to a different value.
/// There is no need to manually change the type to AMF::AMF0_LONGSTRING, this will be done automatically.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param val The string value of this object.
/// \param setType The object type to force this object to.
DTSC::DTMI::DTMI(std::string indice, std::string val, DTSC::DTMItype setType){//str type initializer
myIndice = indice;
myType = setType;
strval = val;
numval = 0;
};
/// Constructor for container objects.
/// The object type is by default AMF::AMF0_OBJECT, but this can be forced to a different value.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param setType The object type to force this object to.
DTSC::DTMI::DTMI(std::string indice, DTSC::DTMItype setType){//object type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = 0;
};
/// Prints the contents of this object to std::cerr.
/// If this object contains other objects, it will call itself recursively
/// and print all nested content in a nice human-readable format.
void DTSC::DTMI::Print(std::string indent){
std::cerr << indent;
// print my type
switch (myType){
case DTMI_INT: std::cerr << "Integer"; break;
case DTMI_STRING: std::cerr << "String"; break;
case DTMI_OBJECT: std::cerr << "Object"; break;
case DTMI_OBJ_END: std::cerr << "Object end"; break;
case DTMI_ROOT: std::cerr << "Root Node"; break;
}
// print my string indice, if available
std::cerr << " " << myIndice << " ";
// print my numeric or string contents
switch (myType){
case DTMI_INT: std::cerr << numval; break;
case DTMI_STRING: std::cerr << strval; break;
default: break;//we don't care about the rest, and don't want a compiler warning...
}
std::cerr << std::endl;
// if I hold other objects, print those too, recursively.
if (contents.size() > 0){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){it->Print(indent+" ");}
}
};//print
/// Packs the DTMI to a std::string for transfer over the network.
/// If a packed version already exists, does not regenerate it.
/// If the object is a container type, this function will call itself recursively and contain all contents.
std::string DTSC::DTMI::Pack(){
if (packed != ""){return packed;}
std::string r = "";
//skip output of DDV container types, they do not exist. Only output their contents.
if (myType != DTMI_ROOT){r += myType;}
//output the properly formatted data stream for this object's contents.
switch (myType){
case DTMI_INT:
r += *(((char*)&numval)+7); r += *(((char*)&numval)+6);
r += *(((char*)&numval)+5); r += *(((char*)&numval)+4);
r += *(((char*)&numval)+3); r += *(((char*)&numval)+2);
r += *(((char*)&numval)+1); r += *(((char*)&numval));
break;
case DTMI_STRING:
r += strval.size() / (256*256*256);
r += strval.size() / (256*256);
r += strval.size() / 256;
r += strval.size() % 256;
r += strval;
break;
case DTMI_OBJECT:
if (contents.size() > 0){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Indice().size() / 256;
r += it->Indice().size() % 256;
r += it->Indice();
r += it->Pack();
}
}
r += (char)0; r += (char)0; r += (char)9;
break;
case DTMI_ROOT://only send contents
if (contents.size() > 0){
for (std::vector<DTSC::DTMI>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Pack();
}
}
break;
case DTMI_OBJ_END:
break;
}
packed = r;
return r;
};//pack
/// Parses a single AMF0 type - used recursively by the AMF::parse() functions.
/// This function updates i every call with the new position in the data.
/// \param data The raw data to parse.
/// \param len The size of the raw data.
/// \param i Current parsing position in the raw data.
/// \param name Indice name for any new object created.
/// \returns A single DTSC::DTMI, parsed from the raw data.
DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){
std::string tmpstr;
unsigned int tmpi = 0;
unsigned char tmpdbl[8];
#if DEBUG >= 10
fprintf(stderr, "Note: AMF type %hhx found. %i bytes left\n", data[i], len-i);
#endif
switch (data[i]){
case DTMI_INT:
tmpdbl[7] = data[i+1];
tmpdbl[6] = data[i+2];
tmpdbl[5] = data[i+3];
tmpdbl[4] = data[i+4];
tmpdbl[3] = data[i+5];
tmpdbl[2] = data[i+6];
tmpdbl[1] = data[i+7];
tmpdbl[0] = data[i+8];
i+=9;//skip 8(a double)+1 forwards
return DTSC::DTMI(name, *(uint64_t*)tmpdbl, DTMI_INT);
break;
case DTMI_STRING:
tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to UTF-8-long length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char *)data+i+5, (size_t)tmpi);//add the string data
i += tmpi + 5;//skip length+size+1 forwards
return DTSC::DTMI(name, tmpstr, DTMI_STRING);
break;
case DTMI_OBJECT:{
++i;
DTSC::DTMI ret(name, DTMI_OBJECT);
while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009)
tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data
i += tmpi + 2;//skip length+size forwards
ret.addContent(parseOneDTMI(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
}
i += 3;//skip 0x000009
return ret;
} break;
}
#if DEBUG >= 2
fprintf(stderr, "Error: Unimplemented DTMI type %hhx - returning.\n", data[i]);
#endif
return DTSC::DTMI("error", DTMI_ROOT);
}//parseOne
/// Parses a C-string to a valid DTSC::DTMI.
/// This function will find all AMF objects in the string and return
/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI.
DTSC::DTMI DTSC::parseDTMI(const unsigned char * data, unsigned int len){
DTSC::DTMI ret("returned", DTMI_ROOT);//container type
unsigned int i = 0, j = 0;
while (i < len){
ret.addContent(parseOneDTMI(data, len, i, ""));
if (i > j){j = i;}else{return ret;}
}
ret.packed = std::string((char*)data, (size_t)len);
return ret;
}//parse
/// Parses a std::string to a valid DTSC::DTMI.
/// This function will find all AMF objects in the string and return
/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI.
DTSC::DTMI DTSC::parseDTMI(std::string data){
return parseDTMI((const unsigned char*)data.c_str(), data.size());
}//parse

View file

@ -2,46 +2,117 @@
/// Holds all headers for DDVTECH Stream Container parsing/generation.
#pragma once
#include "dtmi.h"
#include <vector>
#include <iostream>
#include <stdint.h> //for uint64_t
#include <string>
#include <deque>
#include <set>
// Video:
// Codec (string)
// video
// codec (string)
// Audio:
// Codec (string)
// Samping rate (int, Hz)
// Sample Size (int, bytesize)
// Channels (int, channelcount)
// audio
// codec (string)
// sampingrate (int, Hz)
// samplesize (int, bytesize)
// channels (int, channelcount)
/// Holds all DDVTECH Stream Container classes and parsers.
namespace DTSC{
/// Enumerates all possible DTMI types.
enum DTMItype {
DTMI_INT = 0x01, ///< Unsigned 64-bit integer.
DTMI_STRING = 0x02, ///< String, equivalent to the AMF longstring type.
DTMI_OBJECT = 0xE0, ///< Object, equivalent to the AMF object type.
DTMI_OBJ_END = 0xEE, ///< End of object marker.
DTMI_ROOT = 0xFF ///< Root node for all DTMI data.
};
/// Recursive class that holds DDVTECH MediaInfo.
class DTMI {
public:
std::string Indice();
DTMItype GetType();
uint64_t NumValue();
std::string StrValue();
const char * Str();
int hasContent();
void addContent(DTMI c);
DTMI* getContentP(int i);
DTMI getContent(int i);
DTMI* getContentP(std::string s);
DTMI getContent(std::string s);
DTMI();
DTMI(std::string indice, double val, DTMItype setType = DTMI_INT);
DTMI(std::string indice, std::string val, DTMItype setType = DTMI_STRING);
DTMI(std::string indice, DTMItype setType = DTMI_OBJECT);
void Print(std::string indent = "");
std::string Pack();
std::string packed;
protected:
std::string myIndice; ///< Holds this objects indice, if any.
DTMItype myType; ///< Holds this objects AMF0 type.
std::string strval; ///< Holds this objects string value, if any.
uint64_t numval; ///< Holds this objects numeric value, if any.
std::vector<DTMI> contents; ///< Holds this objects contents, if any (for container types).
};//AMFType
/// Parses a C-string to a valid DTSC::DTMI.
DTMI parseDTMI(const unsigned char * data, unsigned int len);
/// Parses a std::string to a valid DTSC::DTMI.
DTMI parseDTMI(std::string data);
/// Parses a single DTMI type - used recursively by the DTSC::parseDTMI() functions.
DTMI parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name);
/// This enum holds all possible datatypes for DTSC packets.
enum datatype {
AUDIO, ///< Stream Audio data
VIDEO, ///< Stream Video data
META, ///< Stream Metadata
INVALID ///< Anything else or no data available.
}
};
char * Magic_Header; ///< The magic bytes for a DTSC header
char * Magic_Packet; ///< The magic bytes for a DTSC packet
extern char Magic_Header[]; ///< The magic bytes for a DTSC header
extern char Magic_Packet[]; ///< The magic bytes for a DTSC packet
/// Holds temporary data for a DTSC stream and provides functions to access/store it.
/// A part from the DTSC::Stream ringbuffer.
/// Holds information about a buffer that will stay consistent
class Ring {
public:
Ring(unsigned int v);
unsigned int b; ///< Holds current number of buffer. May and is intended to change unexpectedly!
bool waiting; ///< If true, this Ring is currently waiting for a buffer fill.
bool starved; ///< If true, this Ring can no longer receive valid data.
};
/// Holds temporary data for a DTSC stream and provides functions to utilize it.
/// Optionally also acts as a ring buffer of a certain requested size.
/// If ring buffering mode is enabled, it will automatically grow in size to always contain at least one keyframe.
class Stream {
public:
Stream();
~Stream();
Stream(unsigned int buffers);
DTSC::DTMI metadata;
DRSC::DTMI lastPacket;
DTSC::DTMI & getPacket(unsigned int num = 0);
datatype lastType();
char * lastData();
const char * lastData();
bool hasVideo();
bool hasAudio();
bool parsePacket(std::string & buffer);
private:
char * datapointer;
std::string outPacket(unsigned int num);
std::string outHeader();
Ring * getRing();
void dropRing(Ring * ptr);
private:
std::deque<DTSC::DTMI> buffers;
std::set<DTSC::Ring *> rings;
std::deque<DTSC::Ring> keyframes;
void advanceRings();
const char * datapointer;
datatype datapointertype;
}
unsigned int buffercount;
};
};

View file

@ -270,7 +270,7 @@ bool Socket::Connection::write(const void * buffer, int len){
/// \param buffer Location of the buffer to read to.
/// \param len Amount of bytes to read.
/// \returns True if the whole read was succesfull, false otherwise.
bool Socket::Connection::read(void * buffer, int len){
bool Socket::Connection::read(const void * buffer, int len){
int sofar = 0;
if (sock < 0){return false;}
while (sofar != len){
@ -309,9 +309,9 @@ bool Socket::Connection::read(void * buffer, int len){
}//Socket::Connection::read
/// Read call that is compatible with file access syntax. This function simply calls the other read function.
bool Socket::Connection::read(void * buffer, int width, int count){return read(buffer, width*count);}
bool Socket::Connection::read(const void * buffer, int width, int count){return read(buffer, width*count);}
/// Write call that is compatible with file access syntax. This function simply calls the other write function.
bool Socket::Connection::write(void * buffer, int width, int count){return write(buffer, width*count);}
bool Socket::Connection::write(const void * buffer, int width, int count){return write(buffer, width*count);}
/// Write call that is compatible with std::string. This function simply calls the other write function.
bool Socket::Connection::write(const std::string data){return write(data.c_str(), data.size());}
@ -320,7 +320,7 @@ bool Socket::Connection::write(const std::string data){return write(data.c_str()
/// \param buffer Location of the buffer to write from.
/// \param len Amount of bytes to write.
/// \returns The amount of bytes actually written.
int Socket::Connection::iwrite(void * buffer, int len){
int Socket::Connection::iwrite(const void * buffer, int len){
if (sock < 0){return 0;}
int r = send(sock, buffer, len, 0);
if (r < 0){

View file

@ -37,12 +37,12 @@ namespace Socket{
bool canWrite(); ///< Calls poll() on the socket, checking if data can be written.
signed int ready(); ///< Returns the ready-state for this socket.
bool connected(); ///< Returns the connected-state for this socket.
bool read(void * buffer, int len); ///< Reads data from socket.
bool read(void * buffer, int width, int count); ///< Read call that is compatible with file access syntax.
bool read(const void * buffer, int len); ///< Reads data from socket.
bool read(const void * buffer, int width, int count); ///< Read call that is compatible with file access syntax.
bool write(const void * buffer, int len); ///< Writes data to socket.
bool write(void * buffer, int width, int count); ///< Write call that is compatible with file access syntax.
bool write(const void * buffer, int width, int count); ///< Write call that is compatible with file access syntax.
bool write(const std::string data); ///< Write call that is compatible with std::string.
int iwrite(void * buffer, int len); ///< Incremental write call.
int iwrite(const void * buffer, int len); ///< Incremental write call.
int iread(void * buffer, int len); ///< Incremental read call.
bool read(std::string & buffer); ///< Read call that is compatible with std::string.
bool swrite(std::string & buffer); ///< Read call that is compatible with std::string.