make docs now works (needs doxygen installed), added documentation to a LOT of stuff, refactored most utility functions, complete rewrites of DDV sockets and the FLV parsers, updated HTTP_Box_Parser and Connector_HTTP to be able to cope with these new changes - Connector_RTMP, Buffer and Connector_RAW are currently broken. Need to be fixed ASAP.
This commit is contained in:
parent
1ea85a27df
commit
cc78697ba2
19 changed files with 1470 additions and 918 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -7,6 +7,7 @@ Connector_RTMP/Connector_RTMP
|
|||
Connector_RTSP/Connector_RTSP
|
||||
*~
|
||||
bin/*
|
||||
docs
|
||||
gearbox/plugins/001_putserversup.sh
|
||||
AMF_Tester/AMFtest
|
||||
AMF_Creator/AMFtest
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
SRC = main.cpp
|
||||
SRC = main.cpp ../util/ddv_socket.cpp ../util/http_parser.cpp ../util/flv_tag.cpp
|
||||
OBJ = $(SRC:.cpp=.o)
|
||||
OUT = Connector_HTTP
|
||||
INCLUDES =
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
//debugging level 0 = nothing
|
||||
//debugging level 1 = critical errors
|
||||
//debugging level 2 = errors
|
||||
//debugging level 3 = status information
|
||||
//debugging level 4 = extremely verbose status information
|
||||
/// Sets the global debugging level.
|
||||
/// debugging level 0 = nothing
|
||||
/// debugging level 1 = critical errors
|
||||
/// debugging level 2 = errors
|
||||
/// debugging level 3 = status information
|
||||
/// debugging level 4 = extremely verbose status information
|
||||
#define DEBUG 4
|
||||
|
||||
#include <iostream>
|
||||
|
@ -16,325 +17,341 @@
|
|||
#include <sys/epoll.h>
|
||||
#include <getopt.h>
|
||||
#include <ctime>
|
||||
|
||||
enum {HANDLER_NONE, HANDLER_PROGRESSIVE, HANDLER_FLASH, HANDLER_APPLE, HANDLER_MICRO};
|
||||
|
||||
#define DEFAULT_PORT 8080
|
||||
#include "../util/server_setup.cpp"
|
||||
#include "../util/http_parser.cpp"
|
||||
#include "../util/ddv_socket.h"
|
||||
#include "../util/http_parser.h"
|
||||
#include "../util/flv_tag.h"
|
||||
#include "../util/MP4/interface.cpp"
|
||||
#include "amf.cpp"
|
||||
|
||||
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
std::string base64_encode(std::string const input) {
|
||||
std::string ret;
|
||||
unsigned int in_len = input.size();
|
||||
char quad[4], triple[3];
|
||||
unsigned int i, x, n = 3;
|
||||
for (x = 0; x < in_len; x = x + 3){
|
||||
if ((in_len - x) / 3 == 0){n = (in_len - x) % 3;}
|
||||
for (i=0; i < 3; i++){triple[i] = '0';}
|
||||
for (i=0; i < n; i++){triple[i] = input[x + i];}
|
||||
quad[0] = base64_chars[(triple[0] & 0xFC) >> 2]; // FC = 11111100
|
||||
quad[1] = base64_chars[((triple[0] & 0x03) << 4) | ((triple[1] & 0xF0) >> 4)]; // 03 = 11
|
||||
quad[2] = base64_chars[((triple[1] & 0x0F) << 2) | ((triple[2] & 0xC0) >> 6)]; // 0F = 1111, C0=11110
|
||||
quad[3] = base64_chars[triple[2] & 0x3F]; // 3F = 111111
|
||||
if (n < 3){quad[3] = '=';}
|
||||
if (n < 2){quad[2] = '=';}
|
||||
for(i=0; i < 4; i++){ret += quad[i];}
|
||||
}
|
||||
return ret;
|
||||
}//base64_encode
|
||||
/// Holds everything unique to HTTP Connector.
|
||||
namespace Connector_HTTP{
|
||||
|
||||
std::string GetMetaData( ) {
|
||||
AMFType amfreply("container", (unsigned char)AMF0_DDV_CONTAINER);
|
||||
amfreply.addContent(AMFType("onMetaData",(unsigned char)AMF0_STRING));
|
||||
amfreply.addContent(AMFType("",(unsigned char)AMF0_ECMA_ARRAY));
|
||||
amfreply.getContentP(1)->addContent(AMFType("trackinfo", (unsigned char)AMF0_STRICT_ARRAY));
|
||||
amfreply.getContentP(1)->getContentP(0)->addContent(AMFType("arrVal"));
|
||||
// amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("timescale",(double)1000));
|
||||
// amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("length",(double)59641700));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("language","eng"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("sampledescription", (unsigned char)AMF0_STRICT_ARRAY));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->getContentP(1)->addContent(AMFType("arrVal"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->getContentP(1)->getContentP(0)->addContent(AMFType("sampletype","avc1"));
|
||||
amfreply.getContentP(1)->getContentP(0)->addContent(AMFType("arrVal"));
|
||||
// amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("timescale",(double)44100));
|
||||
// amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("length",(double)28630000));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("language","eng"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("sampledescription", (unsigned char)AMF0_STRICT_ARRAY));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->getContentP(1)->addContent(AMFType("arrVal"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->getContentP(1)->getContentP(0)->addContent(AMFType("sampletype","mp4a"));
|
||||
amfreply.getContentP(1)->addContent(AMFType("audiochannels",(double)2));
|
||||
amfreply.getContentP(1)->addContent(AMFType("audiosamplerate",(double)44100));
|
||||
amfreply.getContentP(1)->addContent(AMFType("videoframerate",(double)25));
|
||||
amfreply.getContentP(1)->addContent(AMFType("aacaot",(double)2));
|
||||
amfreply.getContentP(1)->addContent(AMFType("avclevel",(double)12));
|
||||
amfreply.getContentP(1)->addContent(AMFType("avcprofile",(double)77));
|
||||
amfreply.getContentP(1)->addContent(AMFType("audiocodecid","mp4a"));
|
||||
amfreply.getContentP(1)->addContent(AMFType("videocodecid","avc1"));
|
||||
amfreply.getContentP(1)->addContent(AMFType("width",(double)1280));
|
||||
amfreply.getContentP(1)->addContent(AMFType("height",(double)720));
|
||||
amfreply.getContentP(1)->addContent(AMFType("frameWidth",(double)1280));
|
||||
amfreply.getContentP(1)->addContent(AMFType("frameHeight",(double)720));
|
||||
amfreply.getContentP(1)->addContent(AMFType("displayWidth",(double)1280));
|
||||
amfreply.getContentP(1)->addContent(AMFType("displayHeight",(double)720));
|
||||
// amfreply.getContentP(1)->addContent(AMFType("moovposition",(double)35506700));
|
||||
// amfreply.getContentP(1)->addContent(AMFType("duration",(double)596.458));
|
||||
return amfreply.Pack( );
|
||||
}
|
||||
/// Defines the type of handler used to process this request.
|
||||
enum {HANDLER_NONE, HANDLER_PROGRESSIVE, HANDLER_FLASH, HANDLER_APPLE, HANDLER_MICRO};
|
||||
|
||||
std::string BuildManifest( std::string MetaData, std::string MovieId, int CurrentMediaTime ) {
|
||||
Interface * temp = new Interface;
|
||||
std::string Result="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n";
|
||||
Result += "<id>";
|
||||
Result += MovieId;
|
||||
Result += "</id>\n<mimeType>video/mp4</mimeType>\n";
|
||||
Result += "<streamType>live</streamType>\n";
|
||||
Result += "<deliveryType>streaming</deliveryType>\n";
|
||||
Result += "<bootstrapInfo profile=\"named\" id=\"bootstrap1\">";
|
||||
Result += base64_encode(temp->GenerateLiveBootstrap(1));
|
||||
Result += "</bootstrapInfo>\n";
|
||||
Result += "<media streamId=\"1\" bootstrapInfoId=\"bootstrap1\" url=\"";
|
||||
Result += MovieId;
|
||||
Result += "/\">\n";
|
||||
Result += "<metadata>";
|
||||
Result += base64_encode(GetMetaData());
|
||||
Result += "</metadata>\n";
|
||||
Result += "</media>\n";
|
||||
Result += "</manifest>\n";
|
||||
delete temp;
|
||||
return Result;
|
||||
}
|
||||
/// Needed for base64_encode function
|
||||
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
int mainHandler(int CONN_fd){
|
||||
// GetMetaData( ); return 0;
|
||||
int handler = HANDLER_PROGRESSIVE;
|
||||
bool ready4data = false;//set to true when streaming starts
|
||||
bool inited = false;
|
||||
bool progressive_has_sent_header = false;
|
||||
int ss;
|
||||
std::string streamname;
|
||||
std::string FlashBuf;
|
||||
std::string FlashMeta;
|
||||
bool Flash_ManifestSent = false;
|
||||
int Flash_RequestPending = 0;
|
||||
unsigned int Flash_StartTime;
|
||||
std::queue<std::string> Flash_FragBuffer;
|
||||
FLV_Pack * tag = 0;
|
||||
FLV_Pack Audio_Init;
|
||||
FLV_Pack Video_Init;
|
||||
bool FlashFirstVideo = false;
|
||||
bool FlashFirstAudio = false;
|
||||
HTTPReader HTTP_R, HTTP_S;//HTTP Receiver en HTTP Sender.
|
||||
/// Used to base64 encode data. Input is the plaintext as std::string, output is the encoded data as std::string.
|
||||
/// \param input Plaintext data to encode.
|
||||
/// \returns Base64 encoded data.
|
||||
std::string base64_encode(std::string const input) {
|
||||
std::string ret;
|
||||
unsigned int in_len = input.size();
|
||||
char quad[4], triple[3];
|
||||
unsigned int i, x, n = 3;
|
||||
for (x = 0; x < in_len; x = x + 3){
|
||||
if ((in_len - x) / 3 == 0){n = (in_len - x) % 3;}
|
||||
for (i=0; i < 3; i++){triple[i] = '0';}
|
||||
for (i=0; i < n; i++){triple[i] = input[x + i];}
|
||||
quad[0] = base64_chars[(triple[0] & 0xFC) >> 2]; // FC = 11111100
|
||||
quad[1] = base64_chars[((triple[0] & 0x03) << 4) | ((triple[1] & 0xF0) >> 4)]; // 03 = 11
|
||||
quad[2] = base64_chars[((triple[1] & 0x0F) << 2) | ((triple[2] & 0xC0) >> 6)]; // 0F = 1111, C0=11110
|
||||
quad[3] = base64_chars[triple[2] & 0x3F]; // 3F = 111111
|
||||
if (n < 3){quad[3] = '=';}
|
||||
if (n < 2){quad[2] = '=';}
|
||||
for(i=0; i < 4; i++){ret += quad[i];}
|
||||
}
|
||||
return ret;
|
||||
}//base64_encode
|
||||
|
||||
int retval;
|
||||
int poller = epoll_create(1);
|
||||
int sspoller = epoll_create(1);
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = CONN_fd;
|
||||
epoll_ctl(poller, EPOLL_CTL_ADD, CONN_fd, &ev);
|
||||
struct epoll_event events[1];
|
||||
/// Returns AMF-format metadata for Adobe HTTP Dynamic Streaming.
|
||||
std::string GetMetaData( ) {
|
||||
AMFType amfreply("container", (unsigned char)AMF0_DDV_CONTAINER);
|
||||
amfreply.addContent(AMFType("onMetaData",(unsigned char)AMF0_STRING));
|
||||
amfreply.addContent(AMFType("",(unsigned char)AMF0_ECMA_ARRAY));
|
||||
amfreply.getContentP(1)->addContent(AMFType("trackinfo", (unsigned char)AMF0_STRICT_ARRAY));
|
||||
amfreply.getContentP(1)->getContentP(0)->addContent(AMFType("arrVal"));
|
||||
//amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("timescale",(double)1000));
|
||||
//amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("length",(double)59641700));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("language","eng"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->addContent(AMFType("sampledescription", (unsigned char)AMF0_STRICT_ARRAY));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->getContentP(1)->addContent(AMFType("arrVal"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(0)->getContentP(1)->getContentP(0)->addContent(AMFType("sampletype","avc1"));
|
||||
amfreply.getContentP(1)->getContentP(0)->addContent(AMFType("arrVal"));
|
||||
//amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("timescale",(double)44100));
|
||||
//amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("length",(double)28630000));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("language","eng"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->addContent(AMFType("sampledescription", (unsigned char)AMF0_STRICT_ARRAY));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->getContentP(1)->addContent(AMFType("arrVal"));
|
||||
amfreply.getContentP(1)->getContentP(0)->getContentP(1)->getContentP(1)->getContentP(0)->addContent(AMFType("sampletype","mp4a"));
|
||||
amfreply.getContentP(1)->addContent(AMFType("audiochannels",(double)2));
|
||||
amfreply.getContentP(1)->addContent(AMFType("audiosamplerate",(double)44100));
|
||||
amfreply.getContentP(1)->addContent(AMFType("videoframerate",(double)25));
|
||||
amfreply.getContentP(1)->addContent(AMFType("aacaot",(double)2));
|
||||
amfreply.getContentP(1)->addContent(AMFType("avclevel",(double)12));
|
||||
amfreply.getContentP(1)->addContent(AMFType("avcprofile",(double)77));
|
||||
amfreply.getContentP(1)->addContent(AMFType("audiocodecid","mp4a"));
|
||||
amfreply.getContentP(1)->addContent(AMFType("videocodecid","avc1"));
|
||||
amfreply.getContentP(1)->addContent(AMFType("width",(double)1280));
|
||||
amfreply.getContentP(1)->addContent(AMFType("height",(double)720));
|
||||
amfreply.getContentP(1)->addContent(AMFType("frameWidth",(double)1280));
|
||||
amfreply.getContentP(1)->addContent(AMFType("frameHeight",(double)720));
|
||||
amfreply.getContentP(1)->addContent(AMFType("displayWidth",(double)1280));
|
||||
amfreply.getContentP(1)->addContent(AMFType("displayHeight",(double)720));
|
||||
//amfreply.getContentP(1)->addContent(AMFType("moovposition",(double)35506700));
|
||||
//amfreply.getContentP(1)->addContent(AMFType("duration",(double)596.458));
|
||||
return amfreply.Pack( );
|
||||
}//getMetaData
|
||||
|
||||
std::string Movie = "";
|
||||
std::string Quality = "";
|
||||
int Segment = -1;
|
||||
int ReqFragment = -1;
|
||||
int temp;
|
||||
//int CurrentFragment = -1; later herbruiken?
|
||||
/// Returns a F4M-format manifest file for Adobe HTTP Dynamic Streaming.
|
||||
std::string BuildManifest( std::string MetaData, std::string MovieId, int CurrentMediaTime ) {
|
||||
Interface * temp = new Interface;
|
||||
std::string Result="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n";
|
||||
Result += "<id>";
|
||||
Result += MovieId;
|
||||
Result += "</id>\n<mimeType>video/mp4</mimeType>\n";
|
||||
Result += "<streamType>live</streamType>\n";
|
||||
Result += "<deliveryType>streaming</deliveryType>\n";
|
||||
Result += "<bootstrapInfo profile=\"named\" id=\"bootstrap1\">";
|
||||
Result += base64_encode(temp->GenerateLiveBootstrap(1));
|
||||
Result += "</bootstrapInfo>\n";
|
||||
Result += "<media streamId=\"1\" bootstrapInfoId=\"bootstrap1\" url=\"";
|
||||
Result += MovieId;
|
||||
Result += "/\">\n";
|
||||
Result += "<metadata>";
|
||||
Result += base64_encode(GetMetaData());
|
||||
Result += "</metadata>\n";
|
||||
Result += "</media>\n";
|
||||
Result += "</manifest>\n";
|
||||
delete temp;
|
||||
return Result;
|
||||
}//BuildManifest
|
||||
|
||||
while (!socketError && !All_Hell_Broke_Loose){
|
||||
//only parse input if available or not yet init'ed
|
||||
if (HTTP_R.ReadSocket(CONN_fd)){
|
||||
handler = HANDLER_PROGRESSIVE;
|
||||
if ((HTTP_R.url.find("Seg") != std::string::npos) && (HTTP_R.url.find("Frag") != std::string::npos)){handler = HANDLER_FLASH;}
|
||||
if (HTTP_R.url.find("f4m") != std::string::npos){handler = HANDLER_FLASH;}
|
||||
if (HTTP_R.url == "/crossdomain.xml"){
|
||||
handler = HANDLER_NONE;
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetHeader("Content-Type", "text/xml");
|
||||
HTTP_S.SetBody("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" /><site-control permitted-cross-domain-policies=\"all\"/></cross-domain-policy>");
|
||||
HTTP_S.SendResponse(CONN_fd, "200", "OK");//geen SetBody = unknown length! Dat willen we hier.
|
||||
#if DEBUG >= 3
|
||||
printf("Sending crossdomain.xml file\n");
|
||||
#endif
|
||||
}
|
||||
if(handler == HANDLER_FLASH){
|
||||
if (HTTP_R.url.find("f4m") == std::string::npos){
|
||||
Movie = HTTP_R.url.substr(1);
|
||||
Movie = Movie.substr(0,Movie.find("/"));
|
||||
Quality = HTTP_R.url.substr( HTTP_R.url.find("/",1)+1 );
|
||||
Quality = Quality.substr(0, Quality.find("Seg"));
|
||||
temp = HTTP_R.url.find("Seg") + 3;
|
||||
Segment = atoi( HTTP_R.url.substr(temp,HTTP_R.url.find("-",temp)-temp).c_str());
|
||||
temp = HTTP_R.url.find("Frag") + 4;
|
||||
ReqFragment = atoi( HTTP_R.url.substr(temp).c_str() );
|
||||
#if DEBUG >= 4
|
||||
printf( "URL: %s\n", HTTP_R.url.c_str());
|
||||
printf( "Movie: %s, Quality: %s, Seg %d Frag %d\n", Movie.c_str(), Quality.c_str(), Segment, ReqFragment);
|
||||
/// Main function for Connector_HTTP
|
||||
int Connector_HTTP(DDV::Socket conn){
|
||||
int handler = HANDLER_PROGRESSIVE;///< The handler used for processing this request.
|
||||
bool ready4data = false;///< Set to true when streaming is to begin.
|
||||
bool inited = false;
|
||||
bool progressive_has_sent_header = false;
|
||||
DDV::Socket ss(-1);
|
||||
std::string streamname;
|
||||
std::string FlashBuf;
|
||||
std::string FlashMeta;
|
||||
bool Flash_ManifestSent = false;
|
||||
int Flash_RequestPending = 0;
|
||||
unsigned int Flash_StartTime;
|
||||
std::queue<std::string> Flash_FragBuffer;
|
||||
FLV::Tag tag;///< Temporary tag buffer for incoming video data.
|
||||
FLV::Tag Audio_Init;///< Audio initialization data, if available.
|
||||
FLV::Tag Video_Init;///< Video initialization data, if available.
|
||||
bool FlashFirstVideo = false;
|
||||
bool FlashFirstAudio = false;
|
||||
HTTPReader HTTP_R, HTTP_S;//HTTP Receiver en HTTP Sender.
|
||||
|
||||
int retval;
|
||||
int poller = epoll_create(1);
|
||||
int sspoller = epoll_create(1);
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = conn.getSocket();
|
||||
epoll_ctl(poller, EPOLL_CTL_ADD, conn.getSocket(), &ev);
|
||||
struct epoll_event events[1];
|
||||
|
||||
std::string Movie = "";
|
||||
std::string Quality = "";
|
||||
int Segment = -1;
|
||||
int ReqFragment = -1;
|
||||
int temp;
|
||||
//int CurrentFragment = -1; later herbruiken?
|
||||
|
||||
while (conn.connected() && !FLV::Parse_Error){
|
||||
//only parse input if available or not yet init'ed
|
||||
if (HTTP_R.Read(conn)){
|
||||
handler = HANDLER_PROGRESSIVE;
|
||||
if ((HTTP_R.url.find("Seg") != std::string::npos) && (HTTP_R.url.find("Frag") != std::string::npos)){handler = HANDLER_FLASH;}
|
||||
if (HTTP_R.url.find("f4m") != std::string::npos){handler = HANDLER_FLASH;}
|
||||
if (HTTP_R.url == "/crossdomain.xml"){
|
||||
handler = HANDLER_NONE;
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetHeader("Content-Type", "text/xml");
|
||||
HTTP_S.SetBody("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" /><site-control permitted-cross-domain-policies=\"all\"/></cross-domain-policy>");
|
||||
HTTP_S.SendResponse(conn, "200", "OK");//geen SetBody = unknown length! Dat willen we hier.
|
||||
#if DEBUG >= 3
|
||||
printf("Sending crossdomain.xml file\n");
|
||||
#endif
|
||||
Flash_RequestPending++;
|
||||
}else{
|
||||
Movie = HTTP_R.url.substr(1);
|
||||
Movie = Movie.substr(0,Movie.find("/"));
|
||||
}
|
||||
streamname = "/tmp/shared_socket_";
|
||||
for (std::string::iterator i=Movie.end()-1; i>=Movie.begin(); --i){
|
||||
if (!isalpha(*i) && !isdigit(*i)){
|
||||
Movie.erase(i);
|
||||
if(handler == HANDLER_FLASH){
|
||||
if (HTTP_R.url.find("f4m") == std::string::npos){
|
||||
Movie = HTTP_R.url.substr(1);
|
||||
Movie = Movie.substr(0,Movie.find("/"));
|
||||
Quality = HTTP_R.url.substr( HTTP_R.url.find("/",1)+1 );
|
||||
Quality = Quality.substr(0, Quality.find("Seg"));
|
||||
temp = HTTP_R.url.find("Seg") + 3;
|
||||
Segment = atoi( HTTP_R.url.substr(temp,HTTP_R.url.find("-",temp)-temp).c_str());
|
||||
temp = HTTP_R.url.find("Frag") + 4;
|
||||
ReqFragment = atoi( HTTP_R.url.substr(temp).c_str() );
|
||||
#if DEBUG >= 4
|
||||
printf( "URL: %s\n", HTTP_R.url.c_str());
|
||||
printf( "Movie: %s, Quality: %s, Seg %d Frag %d\n", Movie.c_str(), Quality.c_str(), Segment, ReqFragment);
|
||||
#endif
|
||||
Flash_RequestPending++;
|
||||
}else{
|
||||
*i=tolower(*i);
|
||||
}//strip nonalphanumeric
|
||||
}
|
||||
streamname += Movie;
|
||||
ready4data = true;
|
||||
}//FLASH handler
|
||||
if (handler == HANDLER_PROGRESSIVE){
|
||||
//in het geval progressive nemen we aan dat de URL de streamname is, met .flv erachter
|
||||
streamname = HTTP_R.url.substr(0, HTTP_R.url.size()-4);//strip de .flv
|
||||
for (std::string::iterator i=streamname.end()-1; i>=streamname.begin(); --i){
|
||||
if (!isalpha(*i) && !isdigit(*i)){streamname.erase(i);}else{*i=tolower(*i);}//strip nonalphanumeric
|
||||
}
|
||||
streamname = "/tmp/shared_socket_" + streamname;//dit is dan onze shared_socket
|
||||
//normaal zouden we ook een position uitlezen uit de URL, maar bij LIVE streams is dat zinloos
|
||||
printf("Streamname: %s\n", streamname.c_str());
|
||||
ready4data = true;
|
||||
}//PROGRESSIVE handler
|
||||
HTTP_R.CleanForNext(); //maak schoon na verwerken voor eventuele volgende requests...
|
||||
}
|
||||
if (ready4data){
|
||||
if (!inited){
|
||||
//we are ready, connect the socket!
|
||||
ss = DDV_OpenUnix(streamname);
|
||||
if (ss <= 0){
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not connect to server!\n");
|
||||
#endif
|
||||
socketError = 1;
|
||||
break;
|
||||
}
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = ss;
|
||||
epoll_ctl(sspoller, EPOLL_CTL_ADD, ss, &ev);
|
||||
#if DEBUG >= 3
|
||||
fprintf(stderr, "Everything connected, starting to send video data...\n");
|
||||
#endif
|
||||
inited = true;
|
||||
}
|
||||
if ((Flash_RequestPending > 0) && !Flash_FragBuffer.empty()){
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetHeader("Content-Type","video/mp4");
|
||||
HTTP_S.SetBody(Interface::mdatFold(Flash_FragBuffer.front()));
|
||||
Flash_FragBuffer.pop();
|
||||
HTTP_S.SendResponse(CONN_fd, "200", "OK");//schrijf de HTTP response header
|
||||
Flash_RequestPending--;
|
||||
#if DEBUG >= 3
|
||||
fprintf(stderr, "Sending a video fragment. %i left in buffer, %i requested\n", (int)Flash_FragBuffer.size(), Flash_RequestPending);
|
||||
#endif
|
||||
}
|
||||
retval = epoll_wait(sspoller, events, 1, 1);
|
||||
switch (DDV_ready(ss)){
|
||||
case 0:
|
||||
socketError = true;
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Source socket is disconnected.\n");
|
||||
#endif
|
||||
break;
|
||||
case -1: break;//not ready yet
|
||||
default:
|
||||
if (FLV_GetPacket(tag, ss)){//able to read a full packet?f
|
||||
if (handler == HANDLER_FLASH){
|
||||
if (tag->tagTime() > 0){
|
||||
if (Flash_StartTime == 0){
|
||||
Flash_StartTime = tag->tagTime();
|
||||
}
|
||||
tag->tagTime(tag->tagTime() - Flash_StartTime);
|
||||
}
|
||||
if (tag->data[0] != 0x12 ) {
|
||||
if ((tag->isKeyframe) && (Video_Init.len == 0)){
|
||||
if (((tag->data[11] & 0x0f) == 7) && (tag->data[12] == 0)){
|
||||
tag->tagTime(0);//timestamp to zero
|
||||
Video_Init = *tag;
|
||||
}
|
||||
}
|
||||
if ((tag->data[0] == 0x08) && (Audio_Init.len == 0)){
|
||||
if (((tag->data[11] & 0xf0) >> 4) == 10){//aac packet
|
||||
tag->tagTime(0);//timestamp to zero
|
||||
Audio_Init = *tag;
|
||||
}
|
||||
}
|
||||
if (tag->isKeyframe){
|
||||
if (FlashBuf != ""){
|
||||
Flash_FragBuffer.push(FlashBuf);
|
||||
#if DEBUG >= 4
|
||||
fprintf(stderr, "Received a fragment. Now %i in buffer.\n", (int)Flash_FragBuffer.size());
|
||||
#endif
|
||||
}
|
||||
FlashBuf.clear();
|
||||
FlashFirstVideo = true;
|
||||
FlashFirstAudio = true;
|
||||
}
|
||||
if (FlashFirstVideo && (tag->data[0] == 0x09) && (Video_Init.len > 0)){
|
||||
Video_Init.tagTime(tag->tagTime());
|
||||
FlashBuf.append(Video_Init.data, Video_Init.len);
|
||||
FlashFirstVideo = false;
|
||||
}
|
||||
if (FlashFirstAudio && (tag->data[0] == 0x08) && (Audio_Init.len > 0)){
|
||||
Audio_Init.tagTime(tag->tagTime());
|
||||
FlashBuf.append(Audio_Init.data, Audio_Init.len);
|
||||
FlashFirstAudio = false;
|
||||
}
|
||||
#if DEBUG >= 5
|
||||
fprintf(stderr, "Received a tag of type %2hhu and length %i\n", tag->data[0], tag->len);
|
||||
#endif
|
||||
FlashBuf.append(tag->data,tag->len);
|
||||
} else {
|
||||
FlashMeta = "";
|
||||
FlashMeta.append(tag->data+11,tag->len-15);
|
||||
if( !Flash_ManifestSent ) {
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetHeader("Content-Type","text/xml");
|
||||
HTTP_S.SetHeader("Cache-Control","no-cache");
|
||||
HTTP_S.SetBody(BuildManifest(FlashMeta, Movie, tag->tagTime()));
|
||||
HTTP_S.SendResponse(CONN_fd, "200", "OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (handler == HANDLER_PROGRESSIVE){
|
||||
if (!progressive_has_sent_header){
|
||||
HTTP_S.Clean();//troep opruimen die misschien aanwezig is...
|
||||
HTTP_S.SetHeader("Content-Type", "video/x-flv");//FLV files hebben altijd dit content-type.
|
||||
//HTTP_S.SetHeader("Transfer-Encoding", "chunked");
|
||||
HTTP_S.protocol = "HTTP/1.0";
|
||||
HTTP_S.SendResponse(CONN_fd, "200", "OK");//geen SetBody = unknown length! Dat willen we hier.
|
||||
//HTTP_S.SendBodyPart(CONN_fd, FLVHeader, 13);//schrijf de FLV header
|
||||
DDV_write(FLVHeader, 13, CONN_fd);
|
||||
progressive_has_sent_header = true;
|
||||
}
|
||||
//HTTP_S.SendBodyPart(CONN_fd, tag->data, tag->len);//schrijf deze FLV tag onbewerkt weg
|
||||
DDV_write(tag->data, tag->len, CONN_fd);
|
||||
}//PROGRESSIVE handler
|
||||
Movie = HTTP_R.url.substr(1);
|
||||
Movie = Movie.substr(0,Movie.find("/"));
|
||||
}
|
||||
break;
|
||||
streamname = "/tmp/shared_socket_";
|
||||
for (std::string::iterator i=Movie.end()-1; i>=Movie.begin(); --i){
|
||||
if (!isalpha(*i) && !isdigit(*i)){
|
||||
Movie.erase(i);
|
||||
}else{
|
||||
*i=tolower(*i);
|
||||
}//strip nonalphanumeric
|
||||
}
|
||||
streamname += Movie;
|
||||
ready4data = true;
|
||||
}//FLASH handler
|
||||
if (handler == HANDLER_PROGRESSIVE){
|
||||
//in het geval progressive nemen we aan dat de URL de streamname is, met .flv erachter
|
||||
streamname = HTTP_R.url.substr(0, HTTP_R.url.size()-4);//strip de .flv
|
||||
for (std::string::iterator i=streamname.end()-1; i>=streamname.begin(); --i){
|
||||
if (!isalpha(*i) && !isdigit(*i)){streamname.erase(i);}else{*i=tolower(*i);}//strip nonalphanumeric
|
||||
}
|
||||
streamname = "/tmp/shared_socket_" + streamname;//dit is dan onze shared_socket
|
||||
//normaal zouden we ook een position uitlezen uit de URL, maar bij LIVE streams is dat zinloos
|
||||
printf("Streamname: %s\n", streamname.c_str());
|
||||
ready4data = true;
|
||||
}//PROGRESSIVE handler
|
||||
HTTP_R.CleanForNext(); //maak schoon na verwerken voor eventuele volgende requests...
|
||||
}
|
||||
if (ready4data){
|
||||
if (!inited){
|
||||
//we are ready, connect the socket!
|
||||
ss = DDV::Socket(streamname);
|
||||
if (!ss.connected()){
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not connect to server!\n");
|
||||
#endif
|
||||
conn.close();
|
||||
break;
|
||||
}
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = ss.getSocket();
|
||||
epoll_ctl(sspoller, EPOLL_CTL_ADD, ss.getSocket(), &ev);
|
||||
#if DEBUG >= 3
|
||||
fprintf(stderr, "Everything connected, starting to send video data...\n");
|
||||
#endif
|
||||
inited = true;
|
||||
}
|
||||
if ((Flash_RequestPending > 0) && !Flash_FragBuffer.empty()){
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetHeader("Content-Type","video/mp4");
|
||||
HTTP_S.SetBody(Interface::mdatFold(Flash_FragBuffer.front()));
|
||||
Flash_FragBuffer.pop();
|
||||
HTTP_S.SendResponse(conn, "200", "OK");//schrijf de HTTP response header
|
||||
Flash_RequestPending--;
|
||||
#if DEBUG >= 3
|
||||
fprintf(stderr, "Sending a video fragment. %i left in buffer, %i requested\n", (int)Flash_FragBuffer.size(), Flash_RequestPending);
|
||||
#endif
|
||||
}
|
||||
retval = epoll_wait(sspoller, events, 1, 1);
|
||||
switch (ss.ready()){
|
||||
case -1:
|
||||
conn.close();
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Source socket is disconnected.\n");
|
||||
#endif
|
||||
break;
|
||||
case 0: break;//not ready yet
|
||||
default:
|
||||
if (tag.SockLoader(ss)){//able to read a full packet?f
|
||||
if (handler == HANDLER_FLASH){
|
||||
if (tag.tagTime() > 0){
|
||||
if (Flash_StartTime == 0){
|
||||
Flash_StartTime = tag.tagTime();
|
||||
}
|
||||
tag.tagTime(tag.tagTime() - Flash_StartTime);
|
||||
}
|
||||
if (tag.data[0] != 0x12 ) {
|
||||
if ((tag.isKeyframe) && (Video_Init.len == 0)){
|
||||
if (((tag.data[11] & 0x0f) == 7) && (tag.data[12] == 0)){
|
||||
tag.tagTime(0);//timestamp to zero
|
||||
Video_Init = tag;
|
||||
}
|
||||
}
|
||||
if ((tag.data[0] == 0x08) && (Audio_Init.len == 0)){
|
||||
if (((tag.data[11] & 0xf0) >> 4) == 10){//aac packet
|
||||
tag.tagTime(0);//timestamp to zero
|
||||
Audio_Init = tag;
|
||||
}
|
||||
}
|
||||
if (tag.isKeyframe){
|
||||
if (FlashBuf != ""){
|
||||
Flash_FragBuffer.push(FlashBuf);
|
||||
#if DEBUG >= 4
|
||||
fprintf(stderr, "Received a fragment. Now %i in buffer.\n", (int)Flash_FragBuffer.size());
|
||||
#endif
|
||||
}
|
||||
FlashBuf.clear();
|
||||
FlashFirstVideo = true;
|
||||
FlashFirstAudio = true;
|
||||
}
|
||||
if (FlashFirstVideo && (tag.data[0] == 0x09) && (Video_Init.len > 0)){
|
||||
Video_Init.tagTime(tag.tagTime());
|
||||
FlashBuf.append(Video_Init.data, Video_Init.len);
|
||||
FlashFirstVideo = false;
|
||||
}
|
||||
if (FlashFirstAudio && (tag.data[0] == 0x08) && (Audio_Init.len > 0)){
|
||||
Audio_Init.tagTime(tag.tagTime());
|
||||
FlashBuf.append(Audio_Init.data, Audio_Init.len);
|
||||
FlashFirstAudio = false;
|
||||
}
|
||||
#if DEBUG >= 5
|
||||
fprintf(stderr, "Received a tag of type %2hhu and length %i\n", tag.data[0], tag.len);
|
||||
#endif
|
||||
FlashBuf.append(tag.data,tag.len);
|
||||
} else {
|
||||
FlashMeta = "";
|
||||
FlashMeta.append(tag.data+11,tag.len-15);
|
||||
if( !Flash_ManifestSent ) {
|
||||
HTTP_S.Clean();
|
||||
HTTP_S.SetHeader("Content-Type","text/xml");
|
||||
HTTP_S.SetHeader("Cache-Control","no-cache");
|
||||
HTTP_S.SetBody(BuildManifest(FlashMeta, Movie, tag.tagTime()));
|
||||
HTTP_S.SendResponse(conn, "200", "OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (handler == HANDLER_PROGRESSIVE){
|
||||
if (!progressive_has_sent_header){
|
||||
HTTP_S.Clean();//troep opruimen die misschien aanwezig is...
|
||||
HTTP_S.SetHeader("Content-Type", "video/x-flv");//FLV files hebben altijd dit content-type.
|
||||
//HTTP_S.SetHeader("Transfer-Encoding", "chunked");
|
||||
HTTP_S.protocol = "HTTP/1.0";
|
||||
HTTP_S.SendResponse(conn, "200", "OK");//geen SetBody = unknown length! Dat willen we hier.
|
||||
//HTTP_S.SendBodyPart(CONN_fd, FLVHeader, 13);//schrijf de FLV header
|
||||
conn.write(FLV::Header, 13);
|
||||
progressive_has_sent_header = true;
|
||||
}
|
||||
//HTTP_S.SendBodyPart(CONN_fd, tag->data, tag->len);//schrijf deze FLV tag onbewerkt weg
|
||||
conn.write(tag.data, tag.len);
|
||||
}//PROGRESSIVE handler
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
close(CONN_fd);
|
||||
if (inited) close(ss);
|
||||
#if DEBUG >= 1
|
||||
if (All_Hell_Broke_Loose){fprintf(stderr, "All Hell Broke Loose\n");}
|
||||
fprintf(stderr, "User %i disconnected.\n", CONN_fd);
|
||||
if (inited){
|
||||
fprintf(stderr, "Status was: inited\n");
|
||||
}else{
|
||||
if (ready4data){
|
||||
fprintf(stderr, "Status was: ready4data\n");
|
||||
conn.close();
|
||||
if (inited) ss.close();
|
||||
#if DEBUG >= 1
|
||||
if (FLV::Parse_Error){fprintf(stderr, "FLV Parser Error\n");}
|
||||
fprintf(stderr, "User %i disconnected.\n", conn.getSocket());
|
||||
if (inited){
|
||||
fprintf(stderr, "Status was: inited\n");
|
||||
}else{
|
||||
fprintf(stderr, "Status was: connected\n");
|
||||
if (ready4data){
|
||||
fprintf(stderr, "Status was: ready4data\n");
|
||||
}else{
|
||||
fprintf(stderr, "Status was: connected\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}//Connector_HTTP main function
|
||||
|
||||
};//Connector_HTTP namespace
|
||||
|
||||
// Load main server setup file, default port 8080, handler is Connector_HTTP::Connector_HTTP
|
||||
#define DEFAULT_PORT 8080
|
||||
#define MAINHANDLER Connector_HTTP::Connector_HTTP
|
||||
#include "../util/server_setup.cpp"
|
||||
|
|
296
Doxyfile
Normal file
296
Doxyfile
Normal file
|
@ -0,0 +1,296 @@
|
|||
# Doxyfile 1.6.3
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Project related configuration options
|
||||
#---------------------------------------------------------------------------
|
||||
DOXYFILE_ENCODING = UTF-8
|
||||
PROJECT_NAME = DDVTECH Streaming Server
|
||||
PROJECT_NUMBER = 1
|
||||
OUTPUT_DIRECTORY = ./docs
|
||||
CREATE_SUBDIRS = NO
|
||||
OUTPUT_LANGUAGE = English
|
||||
BRIEF_MEMBER_DESC = YES
|
||||
REPEAT_BRIEF = YES
|
||||
ABBREVIATE_BRIEF =
|
||||
ALWAYS_DETAILED_SEC = NO
|
||||
INLINE_INHERITED_MEMB = YES
|
||||
FULL_PATH_NAMES = YES
|
||||
STRIP_FROM_PATH =
|
||||
STRIP_FROM_INC_PATH =
|
||||
SHORT_NAMES = NO
|
||||
JAVADOC_AUTOBRIEF = YES
|
||||
QT_AUTOBRIEF = NO
|
||||
MULTILINE_CPP_IS_BRIEF = NO
|
||||
INHERIT_DOCS = YES
|
||||
SEPARATE_MEMBER_PAGES = NO
|
||||
TAB_SIZE = 2
|
||||
ALIASES =
|
||||
OPTIMIZE_OUTPUT_FOR_C = YES
|
||||
OPTIMIZE_OUTPUT_JAVA = NO
|
||||
OPTIMIZE_FOR_FORTRAN = NO
|
||||
OPTIMIZE_OUTPUT_VHDL = NO
|
||||
EXTENSION_MAPPING =
|
||||
BUILTIN_STL_SUPPORT = YES
|
||||
CPP_CLI_SUPPORT = NO
|
||||
SIP_SUPPORT = NO
|
||||
IDL_PROPERTY_SUPPORT = NO
|
||||
DISTRIBUTE_GROUP_DOC = NO
|
||||
SUBGROUPING = YES
|
||||
TYPEDEF_HIDES_STRUCT = NO
|
||||
SYMBOL_CACHE_SIZE = 0
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Build related configuration options
|
||||
#---------------------------------------------------------------------------
|
||||
EXTRACT_ALL = YES
|
||||
EXTRACT_PRIVATE = NO
|
||||
EXTRACT_STATIC = YES
|
||||
EXTRACT_LOCAL_CLASSES = YES
|
||||
EXTRACT_LOCAL_METHODS = NO
|
||||
EXTRACT_ANON_NSPACES = NO
|
||||
HIDE_UNDOC_MEMBERS = NO
|
||||
HIDE_UNDOC_CLASSES = NO
|
||||
HIDE_FRIEND_COMPOUNDS = NO
|
||||
HIDE_IN_BODY_DOCS = NO
|
||||
INTERNAL_DOCS = NO
|
||||
CASE_SENSE_NAMES = YES
|
||||
HIDE_SCOPE_NAMES = NO
|
||||
SHOW_INCLUDE_FILES = YES
|
||||
FORCE_LOCAL_INCLUDES = NO
|
||||
INLINE_INFO = YES
|
||||
SORT_MEMBER_DOCS = YES
|
||||
SORT_BRIEF_DOCS = NO
|
||||
SORT_MEMBERS_CTORS_1ST = NO
|
||||
SORT_GROUP_NAMES = NO
|
||||
SORT_BY_SCOPE_NAME = NO
|
||||
GENERATE_TODOLIST = YES
|
||||
GENERATE_TESTLIST = YES
|
||||
GENERATE_BUGLIST = YES
|
||||
GENERATE_DEPRECATEDLIST= YES
|
||||
ENABLED_SECTIONS =
|
||||
MAX_INITIALIZER_LINES = 30
|
||||
SHOW_USED_FILES = YES
|
||||
SHOW_DIRECTORIES = YES
|
||||
SHOW_FILES = YES
|
||||
SHOW_NAMESPACES = YES
|
||||
FILE_VERSION_FILTER =
|
||||
LAYOUT_FILE =
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to warning and progress messages
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
QUIET = NO
|
||||
WARNINGS = YES
|
||||
WARN_IF_UNDOCUMENTED = YES
|
||||
WARN_IF_DOC_ERROR = YES
|
||||
WARN_NO_PARAMDOC = NO
|
||||
WARN_FORMAT = "$file:$line: $text"
|
||||
WARN_LOGFILE =
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the input files
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
INPUT = .
|
||||
INPUT_ENCODING = UTF-8
|
||||
FILE_PATTERNS =
|
||||
RECURSIVE = YES
|
||||
EXCLUDE =
|
||||
EXCLUDE_SYMLINKS = NO
|
||||
EXCLUDE_PATTERNS = */.git/*
|
||||
EXCLUDE_SYMBOLS =
|
||||
EXAMPLE_PATH =
|
||||
EXAMPLE_PATTERNS =
|
||||
EXAMPLE_RECURSIVE = NO
|
||||
IMAGE_PATH =
|
||||
INPUT_FILTER =
|
||||
FILTER_PATTERNS =
|
||||
FILTER_SOURCE_FILES = NO
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to source browsing
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
SOURCE_BROWSER = NO
|
||||
INLINE_SOURCES = NO
|
||||
STRIP_CODE_COMMENTS = YES
|
||||
REFERENCED_BY_RELATION = NO
|
||||
REFERENCES_RELATION = NO
|
||||
REFERENCES_LINK_SOURCE = YES
|
||||
USE_HTAGS = NO
|
||||
VERBATIM_HEADERS = YES
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the alphabetical class index
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
ALPHABETICAL_INDEX = NO
|
||||
COLS_IN_ALPHA_INDEX = 5
|
||||
IGNORE_PREFIX =
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the HTML output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
GENERATE_HTML = YES
|
||||
HTML_OUTPUT = html
|
||||
HTML_FILE_EXTENSION = .html
|
||||
HTML_HEADER =
|
||||
HTML_FOOTER =
|
||||
HTML_STYLESHEET =
|
||||
HTML_TIMESTAMP = YES
|
||||
HTML_ALIGN_MEMBERS = YES
|
||||
HTML_DYNAMIC_SECTIONS = NO
|
||||
|
||||
GENERATE_DOCSET = NO
|
||||
DOCSET_FEEDNAME = "Doxygen generated docs"
|
||||
DOCSET_BUNDLE_ID = org.doxygen.Project
|
||||
|
||||
GENERATE_HTMLHELP = NO
|
||||
CHM_FILE =
|
||||
HHC_LOCATION =
|
||||
GENERATE_CHI = NO
|
||||
CHM_INDEX_ENCODING =
|
||||
BINARY_TOC = NO
|
||||
TOC_EXPAND = NO
|
||||
|
||||
GENERATE_QHP = NO
|
||||
QCH_FILE =
|
||||
QHP_NAMESPACE = org.doxygen.Project
|
||||
QHP_VIRTUAL_FOLDER = doc
|
||||
QHP_CUST_FILTER_NAME =
|
||||
QHP_CUST_FILTER_ATTRS =
|
||||
QHP_SECT_FILTER_ATTRS =
|
||||
QHG_LOCATION =
|
||||
|
||||
GENERATE_ECLIPSEHELP = NO
|
||||
ECLIPSE_DOC_ID = org.doxygen.Project
|
||||
DISABLE_INDEX = NO
|
||||
ENUM_VALUES_PER_LINE = 4
|
||||
GENERATE_TREEVIEW = NO
|
||||
USE_INLINE_TREES = NO
|
||||
TREEVIEW_WIDTH = 250
|
||||
FORMULA_FONTSIZE = 10
|
||||
SEARCHENGINE = YES
|
||||
SERVER_BASED_SEARCH = NO
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the LaTeX output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
GENERATE_LATEX = YES
|
||||
LATEX_OUTPUT = latex
|
||||
LATEX_CMD_NAME = latex
|
||||
MAKEINDEX_CMD_NAME = makeindex
|
||||
COMPACT_LATEX = NO
|
||||
PAPER_TYPE = a4wide
|
||||
EXTRA_PACKAGES =
|
||||
LATEX_HEADER =
|
||||
PDF_HYPERLINKS = YES
|
||||
USE_PDFLATEX = YES
|
||||
LATEX_BATCHMODE = NO
|
||||
LATEX_HIDE_INDICES = NO
|
||||
LATEX_SOURCE_CODE = NO
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the RTF output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
GENERATE_RTF = NO
|
||||
RTF_OUTPUT = rtf
|
||||
COMPACT_RTF = NO
|
||||
RTF_HYPERLINKS = NO
|
||||
RTF_STYLESHEET_FILE =
|
||||
RTF_EXTENSIONS_FILE =
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the man page output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
GENERATE_MAN = NO
|
||||
MAN_OUTPUT = man
|
||||
MAN_EXTENSION = .3
|
||||
MAN_LINKS = NO
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the XML output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
GENERATE_XML = NO
|
||||
XML_OUTPUT = xml
|
||||
XML_SCHEMA =
|
||||
XML_DTD =
|
||||
XML_PROGRAMLISTING = YES
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options for the AutoGen Definitions output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
GENERATE_AUTOGEN_DEF = NO
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the Perl module output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
GENERATE_PERLMOD = NO
|
||||
PERLMOD_LATEX = NO
|
||||
PERLMOD_PRETTY = YES
|
||||
PERLMOD_MAKEVAR_PREFIX =
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Configuration options related to the preprocessor
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
ENABLE_PREPROCESSING = YES
|
||||
MACRO_EXPANSION = NO
|
||||
EXPAND_ONLY_PREDEF = NO
|
||||
SEARCH_INCLUDES = YES
|
||||
INCLUDE_PATH =
|
||||
INCLUDE_FILE_PATTERNS =
|
||||
PREDEFINED =
|
||||
EXPAND_AS_DEFINED =
|
||||
SKIP_FUNCTION_MACROS = YES
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Configuration::additions related to external references
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
TAGFILES =
|
||||
GENERATE_TAGFILE =
|
||||
ALLEXTERNALS = NO
|
||||
EXTERNAL_GROUPS = YES
|
||||
PERL_PATH = /usr/bin/perl
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Configuration options related to the dot tool
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
CLASS_DIAGRAMS = YES
|
||||
MSCGEN_PATH =
|
||||
HIDE_UNDOC_RELATIONS = YES
|
||||
HAVE_DOT = NO
|
||||
DOT_FONTNAME = FreeSans
|
||||
DOT_FONTSIZE = 10
|
||||
DOT_FONTPATH =
|
||||
CLASS_GRAPH = YES
|
||||
COLLABORATION_GRAPH = YES
|
||||
GROUP_GRAPHS = YES
|
||||
UML_LOOK = NO
|
||||
TEMPLATE_RELATIONS = NO
|
||||
INCLUDE_GRAPH = YES
|
||||
INCLUDED_BY_GRAPH = YES
|
||||
CALL_GRAPH = NO
|
||||
CALLER_GRAPH = NO
|
||||
GRAPHICAL_HIERARCHY = YES
|
||||
DIRECTORY_GRAPH = YES
|
||||
DOT_IMAGE_FORMAT = png
|
||||
DOT_PATH =
|
||||
DOTFILE_DIRS =
|
||||
DOT_GRAPH_MAX_NODES = 50
|
||||
MAX_DOT_GRAPH_DEPTH = 0
|
||||
DOT_TRANSPARENT = NO
|
||||
DOT_MULTI_TARGETS = NO
|
||||
GENERATE_LEGEND = YES
|
||||
DOT_CLEANUP = YES
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
SRC = main.cpp
|
||||
SRC = main.cpp ../util/flv_tag.cpp ../util/http_parser.cpp ../util/ddv_socket.cpp
|
||||
OBJ = $(SRC:.cpp=.o)
|
||||
OUT = Box_Parser
|
||||
INCLUDES =
|
||||
|
|
|
@ -2,25 +2,25 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include "../util/http_parser.cpp"
|
||||
#include "../util/http_parser.h"
|
||||
#include "../util/MP4/box_includes.h"
|
||||
#include "../util/flv_data.cpp"
|
||||
#include "../util/flv_tag.h"
|
||||
|
||||
int main(){
|
||||
HTTPReader H;
|
||||
FLV_Pack * F = 0;
|
||||
FLV::Tag F;
|
||||
unsigned int P = 0;
|
||||
char * Payload = 0;
|
||||
|
||||
while (H.ReadSocket(stdin) || H.CleanForNext()){
|
||||
while (H.Read(stdin) || H.CleanForNext()){
|
||||
if (H.body.size() > 10000){
|
||||
Box * TestBox = new Box((uint8_t*)H.body.c_str(), H.body.size());
|
||||
Payload = (char*)TestBox->GetPayload();
|
||||
printf("First bytes: %2hhu %2hhu %2hhu %2hhu\n", Payload[0], Payload[1], Payload[2], Payload[3]);
|
||||
P = 0;
|
||||
while (TestBox->GetPayloadSize() > P){
|
||||
if (FLV_GetPacket(F, Payload, TestBox->GetPayloadSize(), P)){
|
||||
std::cout << "Got a " << F->len << " bytes " << F->tagType() << " FLV tag of time " << F->tagTime() << "." << std::endl;
|
||||
if (F.MemLoader(Payload, TestBox->GetPayloadSize(), P)){
|
||||
std::cout << "Got a " << F.len << " bytes " << F.tagType() << " FLV tag of time " << F.tagTime() << "." << std::endl;
|
||||
}
|
||||
}
|
||||
delete TestBox;
|
||||
|
|
3
Makefile
3
Makefile
|
@ -1,4 +1,5 @@
|
|||
default: client-install
|
||||
.PHONY: client client-clean clean client-install docs
|
||||
|
||||
client:
|
||||
cd Connector_HTTP; $(MAKE)
|
||||
|
@ -16,4 +17,6 @@ client-install: client-clean client
|
|||
cd Connector_HTTP; $(MAKE) install
|
||||
cd Connector_RAW; $(MAKE) install
|
||||
cd Buffer; $(MAKE) install
|
||||
docs:
|
||||
doxygen ./Doxyfile
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <stdint.h>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
|
|
|
@ -1,141 +1,112 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include "ddv_socket.h"
|
||||
|
||||
bool socketError = false;
|
||||
bool socketBlocking = false;
|
||||
/// Create a new base socket. This is a basic constructor for converting any valid socket to a DDV::Socket.
|
||||
/// \param sockNo Integer representing the socket to convert.
|
||||
DDV::Socket::Socket(int sockNo){
|
||||
sock = sockNo;
|
||||
Error = false;
|
||||
Blocking = false;
|
||||
}//DDV::Socket basic constructor
|
||||
|
||||
int DDV_OpenUnix(std::string adres, bool nonblock = false){
|
||||
int s = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||
/// Close connection. The internal socket is closed and then set to -1.
|
||||
void DDV::Socket::close(){
|
||||
#if DEBUG >= 3
|
||||
fprintf(stderr, "Socket closed.\n");
|
||||
#endif
|
||||
::close(sock);
|
||||
sock = -1;
|
||||
}//DDV::Socket::close
|
||||
|
||||
/// Returns internal socket number.
|
||||
int DDV::Socket::getSocket(){return sock;}
|
||||
|
||||
/// Create a new Unix Socket. This socket will (try to) connect to the given address right away.
|
||||
/// \param address String containing the location of the Unix socket to connect to.
|
||||
/// \param nonblock Whether the socket should be nonblocking. False by default.
|
||||
DDV::Socket::Socket(std::string address, bool nonblock){
|
||||
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock < 0){
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not create socket! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
Error = false;
|
||||
Blocking = false;
|
||||
sockaddr_un addr;
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, adres.c_str(), adres.size()+1);
|
||||
int r = connect(s, (sockaddr*)&addr, sizeof(addr));
|
||||
strncpy(addr.sun_path, address.c_str(), address.size()+1);
|
||||
int r = connect(sock, (sockaddr*)&addr, sizeof(addr));
|
||||
if (r == 0){
|
||||
if (nonblock){
|
||||
int flags = fcntl(s, F_GETFL, 0);
|
||||
int flags = fcntl(sock, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(s, F_SETFL, flags);
|
||||
}
|
||||
return s;
|
||||
}else{
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int DDV_Listen(int port){
|
||||
int s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
int on = 1;
|
||||
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);//port 8888
|
||||
inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr);//listen on all interfaces
|
||||
int ret = bind(s, (sockaddr*)&addr, sizeof(addr));//bind to all interfaces, chosen port
|
||||
if (ret == 0){
|
||||
ret = listen(s, 100);//start listening, backlog of 100 allowed
|
||||
if (ret == 0){
|
||||
return s;
|
||||
}else{
|
||||
fprintf(stderr, "Listen failed! Error: %s\n", strerror(errno));
|
||||
close(s);
|
||||
return 0;
|
||||
fcntl(sock, F_SETFL, flags);
|
||||
}
|
||||
}else{
|
||||
fprintf(stderr, "Binding failed! Error: %s\n", strerror(errno));
|
||||
close(s);
|
||||
return 0;
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not connect to %s! Error: %s\n", address.c_str(), strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
}
|
||||
}
|
||||
}//DDV::Socket Unix Contructor
|
||||
|
||||
int DDV_UnixListen(std::string adres, bool nonblock = false){
|
||||
unlink(adres.c_str());
|
||||
int s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (nonblock){
|
||||
int flags = fcntl(s, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(s, F_SETFL, flags);
|
||||
}
|
||||
sockaddr_un addr;
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, adres.c_str(), adres.size()+1);
|
||||
int ret = bind(s, (sockaddr*)&addr, sizeof(addr));
|
||||
if (ret == 0){
|
||||
ret = listen(s, 100);//start listening, backlog of 100 allowed
|
||||
if (ret == 0){
|
||||
return s;
|
||||
}else{
|
||||
fprintf(stderr, "Listen failed! Error: %s\n", strerror(errno));
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
}else{
|
||||
fprintf(stderr, "Binding failed! Error: %s\n", strerror(errno));
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int DDV_Accept(int sock, bool nonblock = false){
|
||||
int r = accept(sock, 0, 0);
|
||||
if ((r >= 0) && nonblock){
|
||||
int flags = fcntl(r, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(r, F_SETFL, flags);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
bool DDV_write(const void * buffer, int todo, int sock){
|
||||
int sofar = 0;
|
||||
socketBlocking = false;
|
||||
while (sofar != todo){
|
||||
int r = send(sock, (char*)buffer + sofar, todo-sofar, 0);
|
||||
if (r <= 0){
|
||||
switch (errno){
|
||||
case EWOULDBLOCK: socketBlocking = true; break;
|
||||
default:
|
||||
socketError = true;
|
||||
fprintf(stderr, "Could not write! %s\n", strerror(errno));
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
sofar += r;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
signed int DDV_ready(int sock){
|
||||
/// Returns the ready-state for this socket.
|
||||
/// \returns 1 if data is waiting to be read, -1 if not connected, 0 otherwise.
|
||||
signed int DDV::Socket::ready(){
|
||||
if (sock < 0) return -1;
|
||||
char tmp;
|
||||
int preflags = fcntl(sock, F_GETFL, 0);
|
||||
int postflags = preflags | O_NONBLOCK;
|
||||
fcntl(sock, F_SETFL, postflags);
|
||||
int r = recv(sock, &tmp, 1, MSG_PEEK);
|
||||
fcntl(sock, F_SETFL, preflags);
|
||||
if (r < 0){
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK){
|
||||
return 0;
|
||||
}else{
|
||||
#if DEBUG >= 2
|
||||
fprintf(stderr, "Socket ready error! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (r == 0){
|
||||
close(); return -1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
bool DDV_read(void * buffer, int todo, int sock){
|
||||
/// Returns the connected-state for this socket.
|
||||
/// Note that this function might be slightly behind the real situation.
|
||||
/// The connection status is updated after every read/write attempt, when errors occur
|
||||
/// and when the socket is closed manually.
|
||||
/// \returns True if socket is connected, false otherwise.
|
||||
bool DDV::Socket::connected(){
|
||||
return (sock >= 0);
|
||||
}
|
||||
|
||||
/// Writes data to socket. This function blocks if the socket is blocking and all data cannot be written right away.
|
||||
/// If the socket is nonblocking and not all data can be written, this function sets internal variable Blocking to true
|
||||
/// and returns false.
|
||||
/// \param buffer Location of the buffer to write from.
|
||||
/// \param len Amount of bytes to write.
|
||||
/// \returns True if the whole write was succesfull, false otherwise.
|
||||
bool DDV::Socket::write(const void * buffer, int len){
|
||||
int sofar = 0;
|
||||
socketBlocking = false;
|
||||
while (sofar != todo){
|
||||
int r = recv(sock, (char*)buffer + sofar, todo-sofar, 0);
|
||||
Blocking = false;
|
||||
while (sofar != len){
|
||||
int r = send(sock, (char*)buffer + sofar, len-sofar, 0);
|
||||
if (r <= 0){
|
||||
switch (errno){
|
||||
case EWOULDBLOCK: socketBlocking = true; break;
|
||||
case EWOULDBLOCK: Blocking = true; break;
|
||||
default:
|
||||
socketError = true;
|
||||
fprintf(stderr, "Could not read! %s\n", strerror(errno));
|
||||
Error = true;
|
||||
#if DEBUG >= 2
|
||||
fprintf(stderr, "Could not write data! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
@ -143,41 +114,218 @@ bool DDV_read(void * buffer, int todo, int sock){
|
|||
sofar += r;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}//DDv::Socket::write
|
||||
|
||||
|
||||
bool DDV_read(void * buffer, int width, int count, int sock){return DDV_read(buffer, width*count, sock);}
|
||||
bool DDV_write(void * buffer, int width, int count, int sock){return DDV_write(buffer, width*count, sock);}
|
||||
/// Reads data from socket. This function blocks if the socket is blocking and all data cannot be read right away.
|
||||
/// If the socket is nonblocking and not all data can be read, this function sets internal variable Blocking to true
|
||||
/// and returns false.
|
||||
/// \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 DDV::Socket::read(void * buffer, int len){
|
||||
int sofar = 0;
|
||||
Blocking = false;
|
||||
while (sofar != len){
|
||||
int r = recv(sock, (char*)buffer + sofar, len-sofar, 0);
|
||||
if (r <= 0){
|
||||
switch (errno){
|
||||
case EWOULDBLOCK: Blocking = true; break;
|
||||
default:
|
||||
Error = true;
|
||||
#if DEBUG >= 2
|
||||
fprintf(stderr, "Could not read data! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
sofar += r;
|
||||
}
|
||||
return true;
|
||||
}//DDV::Socket::read
|
||||
|
||||
/// Read call that is compatible with file access syntax. This function simply calls the other read function.
|
||||
bool DDV::Socket::read(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 DDV::Socket::write(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 DDV::Socket::write(const std::string data){return write(data.c_str(), data.size());}
|
||||
|
||||
int DDV_iwrite(void * buffer, int todo, int sock){
|
||||
int r = send(sock, buffer, todo, 0);
|
||||
/// Incremental write call. This function tries to write len bytes to the socket from the buffer,
|
||||
/// returning the amount of bytes it actually wrote.
|
||||
/// \param buffer Location of the buffer to write from.
|
||||
/// \param len Amount of bytes to write.
|
||||
/// \returns The amount of bytes actually written.
|
||||
int DDV::Socket::iwrite(void * buffer, int len){
|
||||
int r = send(sock, buffer, len, 0);
|
||||
if (r < 0){
|
||||
switch (errno){
|
||||
case EWOULDBLOCK: return 0; break;
|
||||
default:
|
||||
socketError = true;
|
||||
fprintf(stderr, "Could not write! %s\n", strerror(errno));
|
||||
Error = true;
|
||||
#if DEBUG >= 2
|
||||
fprintf(stderr, "Could not iwrite data! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}//DDV::Socket::iwrite
|
||||
|
||||
int DDV_iread(void * buffer, int todo, int sock){
|
||||
int r = recv(sock, buffer, todo, 0);
|
||||
/// Incremental read call. This function tries to read len bytes to the buffer from the socket,
|
||||
/// returning the amount of bytes it actually read.
|
||||
/// \param buffer Location of the buffer to read to.
|
||||
/// \param len Amount of bytes to read.
|
||||
/// \returns The amount of bytes actually read.
|
||||
int DDV::Socket::iread(void * buffer, int len){
|
||||
int r = recv(sock, buffer, len, 0);
|
||||
if (r < 0){
|
||||
switch (errno){
|
||||
case EWOULDBLOCK: break;
|
||||
case EWOULDBLOCK: return 0; break;
|
||||
default:
|
||||
socketError = true;
|
||||
fprintf(stderr, "Could not read! %s\n", strerror(errno));
|
||||
Error = true;
|
||||
#if DEBUG >= 2
|
||||
fprintf(stderr, "Could not iread data! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}//DDV::Socket::iread
|
||||
|
||||
/// Create a new base ServerSocket. The socket is never connected, and a placeholder for later connections.
|
||||
DDV::ServerSocket::ServerSocket(){
|
||||
sock = -1;
|
||||
}//DDV::ServerSocket base Constructor
|
||||
|
||||
/// Create a new TCP ServerSocket. The socket is immediately bound and set to listen.
|
||||
/// A maximum of 100 connections will be accepted between accept() calls.
|
||||
/// Any further connections coming in will be dropped.
|
||||
/// \param port The TCP port to listen on
|
||||
/// \param hostname (optional) The interface to bind to. The default is 0.0.0.0 (all interfaces).
|
||||
/// \param nonblock (optional) Whether accept() calls will be nonblocking. Default is false (blocking).
|
||||
DDV::ServerSocket::ServerSocket(int port, std::string hostname, bool nonblock){
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0){
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not create socket! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
int on = 1;
|
||||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
||||
if (nonblock){
|
||||
int flags = fcntl(sock, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(sock, F_SETFL, flags);
|
||||
}
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);//set port
|
||||
inet_pton(AF_INET, hostname.c_str(), &addr.sin_addr);//set interface, 0.0.0.0 (default) is all
|
||||
int ret = bind(sock, (sockaddr*)&addr, sizeof(addr));//do the actual bind
|
||||
if (ret == 0){
|
||||
ret = listen(sock, 100);//start listening, backlog of 100 allowed
|
||||
if (ret == 0){
|
||||
return;
|
||||
}else{
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Listen failed! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return;
|
||||
}
|
||||
}else{
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Binding failed! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return;
|
||||
}
|
||||
}//DDV::ServerSocket TCP Constructor
|
||||
|
||||
/// Create a new Unix ServerSocket. The socket is immediately bound and set to listen.
|
||||
/// A maximum of 100 connections will be accepted between accept() calls.
|
||||
/// Any further connections coming in will be dropped.
|
||||
/// The address used will first be unlinked - so it succeeds if the Unix socket already existed. Watch out for this behaviour - it will delete any file located at address!
|
||||
/// \param address The location of the Unix socket to bind to.
|
||||
/// \param nonblock (optional) Whether accept() calls will be nonblocking. Default is false (blocking).
|
||||
DDV::ServerSocket::ServerSocket(std::string address, bool nonblock){
|
||||
unlink(address.c_str());
|
||||
sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock < 0){
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Could not create socket! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
if (nonblock){
|
||||
int flags = fcntl(sock, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(sock, F_SETFL, flags);
|
||||
}
|
||||
sockaddr_un addr;
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, address.c_str(), address.size()+1);
|
||||
int ret = bind(sock, (sockaddr*)&addr, sizeof(addr));
|
||||
if (ret == 0){
|
||||
ret = listen(sock, 100);//start listening, backlog of 100 allowed
|
||||
if (ret == 0){
|
||||
return;
|
||||
}else{
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Listen failed! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return;
|
||||
}
|
||||
}else{
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "Binding failed! Error: %s\n", strerror(errno));
|
||||
#endif
|
||||
close();
|
||||
return;
|
||||
}
|
||||
}//DDV::ServerSocket Unix Constructor
|
||||
|
||||
/// Accept any waiting connections. If the DDV::ServerSocket is blocking, this function will block until there is an incoming connection.
|
||||
/// If the DDV::ServerSocket is nonblocking, it might return a DDV::Socket that is not connected, so check for this.
|
||||
/// \param nonblock (optional) Whether the newly connected socket should be nonblocking. Default is false (blocking).
|
||||
/// \returns A DDV::Socket, which may or may not be connected, depending on settings and circumstances.
|
||||
DDV::Socket DDV::ServerSocket::accept(bool nonblock){
|
||||
if (sock < 0){return DDV::Socket(-1);}
|
||||
int r = ::accept(sock, 0, 0);
|
||||
if ((r >= 0) && nonblock){
|
||||
int flags = fcntl(r, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(r, F_SETFL, flags);
|
||||
}
|
||||
if (r < 0){
|
||||
if (errno != EWOULDBLOCK && errno != EAGAIN){close();}
|
||||
}
|
||||
return DDV::Socket(r);
|
||||
}
|
||||
|
||||
/// Close connection. The internal socket is closed and then set to -1.
|
||||
void DDV::ServerSocket::close(){
|
||||
::close(sock);
|
||||
sock = -1;
|
||||
}//DDV::ServerSocket::close
|
||||
|
||||
/// Returns the connected-state for this socket.
|
||||
/// Note that this function might be slightly behind the real situation.
|
||||
/// The connection status is updated after every accept attempt, when errors occur
|
||||
/// and when the socket is closed manually.
|
||||
/// \returns True if socket is connected, false otherwise.
|
||||
bool DDV::ServerSocket::connected(){
|
||||
return (sock >= 0);
|
||||
}//DDV::ServerSocket::connected
|
||||
|
||||
/// Returns internal socket number.
|
||||
int DDV::ServerSocket::getSocket(){return sock;}
|
||||
|
|
53
util/ddv_socket.h
Normal file
53
util/ddv_socket.h
Normal file
|
@ -0,0 +1,53 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
///Holds DDV Socket tools.
|
||||
namespace DDV{
|
||||
|
||||
/// This class is for easy communicating through sockets, either TCP or Unix.
|
||||
class Socket{
|
||||
private:
|
||||
int sock; ///< Internally saved socket number.
|
||||
public:
|
||||
Socket(int sockNo); ///< Create a new base socket.
|
||||
Socket(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
|
||||
bool Error; ///< Set to true if a socket error happened.
|
||||
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
|
||||
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 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 std::string data); ///< Write call that is compatible with std::string.
|
||||
int iwrite(void * buffer, int len); ///< Incremental write call.
|
||||
int iread(void * buffer, int len); ///< Incremental read call.
|
||||
void close(); ///< Close connection.
|
||||
int getSocket(); ///< Returns internal socket number.
|
||||
};
|
||||
|
||||
/// This class is for easily setting up listening socket, either TCP or Unix.
|
||||
class ServerSocket{
|
||||
private:
|
||||
int sock; ///< Internally saved socket number.
|
||||
public:
|
||||
ServerSocket(); ///< Create a new base ServerSocket.
|
||||
ServerSocket(int port, std::string hostname = "0.0.0.0", bool nonblock = false); ///< Create a new TCP ServerSocket.
|
||||
ServerSocket(std::string adres, bool nonblock = false); ///< Create a new Unix ServerSocket.
|
||||
Socket accept(bool nonblock = false); ///< Accept any waiting connections.
|
||||
bool connected(); ///< Returns the connected-state for this socket.
|
||||
void close(); ///< Close connection.
|
||||
int getSocket(); ///< Returns internal socket number.
|
||||
};
|
||||
|
||||
};
|
88
util/flv.cpp
88
util/flv.cpp
|
@ -1,88 +0,0 @@
|
|||
#include <unistd.h> //for read()
|
||||
#include <fcntl.h>
|
||||
#include "flv_pack.cpp"
|
||||
|
||||
char FLVHeader[13];
|
||||
bool All_Hell_Broke_Loose = false;
|
||||
|
||||
//checks FLV Header for correctness
|
||||
//returns true if everything is alright, false otherwise
|
||||
bool FLV_Checkheader(char * header){
|
||||
if (header[0] != 'F') return false;
|
||||
if (header[1] != 'L') return false;
|
||||
if (header[2] != 'V') return false;
|
||||
if (header[8] != 0x09) return false;
|
||||
if (header[9] != 0) return false;
|
||||
if (header[10] != 0) return false;
|
||||
if (header[11] != 0) return false;
|
||||
if (header[12] != 0) return false;
|
||||
return true;
|
||||
}//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.
|
||||
//will assign pointer if null
|
||||
//resizes FLV_Pack data field bigger if data doesn't fit
|
||||
// (does not auto-shrink for speed!)
|
||||
bool FLV_GetPacket(FLV_Pack *& p){
|
||||
int preflags = fcntl(fileno(stdin), F_GETFL, 0);
|
||||
int postflags = preflags | O_NONBLOCK;
|
||||
fcntl(fileno(stdin), F_SETFL, postflags);
|
||||
static bool done = true;
|
||||
static unsigned int sofar = 0;
|
||||
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 (done){
|
||||
//read a header
|
||||
if (ReadUntil(p->data, 11, sofar)){
|
||||
//if its a correct FLV header, throw away and read tag header
|
||||
if (FLV_Isheader(p->data)){
|
||||
if (ReadUntil(p->data, 13, sofar)){
|
||||
if (FLV_Checkheader(p->data)){
|
||||
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;
|
||||
sofar = 0;
|
||||
fcntl(fileno(stdin), F_SETFL, preflags);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
fcntl(fileno(stdin), F_SETFL, preflags);
|
||||
return false;
|
||||
}//FLV_GetPacket
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
#include <unistd.h> //for read()
|
||||
#include <fcntl.h>
|
||||
#include "flv_pack.cpp"
|
||||
|
||||
char FLVHeader[13];
|
||||
bool All_Hell_Broke_Loose = false;
|
||||
|
||||
//checks FLV Header for correctness
|
||||
//returns true if everything is alright, false otherwise
|
||||
bool FLV_Checkheader(char * header){
|
||||
if (header[0] != 'F') return false;
|
||||
if (header[1] != 'L') return false;
|
||||
if (header[2] != 'V') return false;
|
||||
if (header[8] != 0x09) return false;
|
||||
if (header[9] != 0) return false;
|
||||
if (header[10] != 0) return false;
|
||||
if (header[11] != 0) return false;
|
||||
if (header[12] != 0) return false;
|
||||
return true;
|
||||
}//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, char * D, unsigned int S, unsigned int & P){
|
||||
if (sofar >= count){return true;}
|
||||
int r = 0;
|
||||
if (P+(count-sofar) > S){r = S-P;}else{r = count-sofar;}
|
||||
memcpy(buffer+sofar, D+P, r);
|
||||
P += r;
|
||||
sofar += r;
|
||||
if (sofar >= count){return true;}
|
||||
return false;
|
||||
}
|
||||
|
||||
//gets a packet, storing in given FLV_Pack pointer.
|
||||
//will assign pointer if null
|
||||
//resizes FLV_Pack data field bigger if data doesn't fit
|
||||
// (does not auto-shrink for speed!)
|
||||
bool FLV_GetPacket(FLV_Pack *& p, char * D, unsigned int S, unsigned int & P){
|
||||
static bool done = true;
|
||||
static unsigned int sofar = 0;
|
||||
if (!p){p = (FLV_Pack*)calloc(1, sizeof(FLV_Pack));}
|
||||
if (p->buf < 15){p->data = (char*)realloc(p->data, 15000000); p->buf = 15000000;}
|
||||
|
||||
if (done){
|
||||
//read a header
|
||||
if (ReadUntil(p->data, 11, sofar, D, S, P)){
|
||||
//if its a correct FLV header, throw away and read tag header
|
||||
if (FLV_Isheader(p->data)){
|
||||
if (ReadUntil(p->data, 13, sofar, D, S, P)){
|
||||
if (FLV_Checkheader(p->data)){
|
||||
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;}
|
||||
if (p->data[0] > 0x12){
|
||||
printf("Invalid data: %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n", p->data[0], p->data[1], p->data[2], p->data[3], p->data[4], p->data[5], p->data[6]);
|
||||
}
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
//read tag body
|
||||
if (ReadUntil(p->data, p->len, sofar, D, S, P)){
|
||||
//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;
|
||||
sofar = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}//FLV_GetPacket
|
||||
|
|
@ -1,123 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
class FLV_Pack {
|
||||
public:
|
||||
int len;
|
||||
int buf;
|
||||
bool isKeyframe;
|
||||
char * data;
|
||||
std::string tagType(){
|
||||
std::string R = "";
|
||||
switch (data[0]){
|
||||
case 0x09:
|
||||
switch (data[11] & 0x0F){
|
||||
case 1: R += "JPEG"; break;
|
||||
case 2: R += "H263"; break;
|
||||
case 3: R += "ScreenVideo1"; break;
|
||||
case 4: R += "VP6"; break;
|
||||
case 5: R += "VP6Alpha"; break;
|
||||
case 6: R += "ScreenVideo2"; break;
|
||||
case 7: R += "AVC"; break;
|
||||
default: R += "unknown"; break;
|
||||
}
|
||||
R += " video ";
|
||||
switch (data[11] & 0xF0){
|
||||
case 0x10: R += "keyframe"; break;
|
||||
case 0x20: R += "iframe"; break;
|
||||
case 0x30: R += "disposableiframe"; break;
|
||||
case 0x40: R += "generatedkeyframe"; break;
|
||||
case 0x50: R += "videoinfo"; break;
|
||||
}
|
||||
if ((data[11] & 0x0F) == 7){
|
||||
switch (data[12]){
|
||||
case 0: R += " header"; break;
|
||||
case 1: R += " NALU"; break;
|
||||
case 2: R += " endofsequence"; break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 0x08:
|
||||
switch (data[11] & 0xF0){
|
||||
case 0x00: R += "linear PCM PE"; break;
|
||||
case 0x10: R += "ADPCM"; break;
|
||||
case 0x20: R += "MP3"; break;
|
||||
case 0x30: R += "linear PCM LE"; break;
|
||||
case 0x40: R += "Nelly16kHz"; break;
|
||||
case 0x50: R += "Nelly8kHz"; break;
|
||||
case 0x60: R += "Nelly"; break;
|
||||
case 0x70: R += "G711A-law"; break;
|
||||
case 0x80: R += "G711mu-law"; break;
|
||||
case 0x90: R += "reserved"; break;
|
||||
case 0xA0: R += "AAC"; break;
|
||||
case 0xB0: R += "Speex"; break;
|
||||
case 0xE0: R += "MP38kHz"; break;
|
||||
case 0xF0: R += "DeviceSpecific"; break;
|
||||
default: R += "unknown"; break;
|
||||
}
|
||||
switch (data[11] & 0x0C){
|
||||
case 0x0: R += " 5.5kHz"; break;
|
||||
case 0x4: R += " 11kHz"; break;
|
||||
case 0x8: R += " 22kHz"; break;
|
||||
case 0xC: R += " 44kHz"; break;
|
||||
}
|
||||
switch (data[11] & 0x02){
|
||||
case 0: R += " 8bit"; break;
|
||||
case 2: R += " 16bit"; break;
|
||||
}
|
||||
switch (data[11] & 0x01){
|
||||
case 0: R += " mono"; break;
|
||||
case 1: R += " stereo"; break;
|
||||
}
|
||||
R += " audio";
|
||||
if ((data[12] == 0) && ((data[11] & 0xF0) == 0xA0)){
|
||||
R += " initdata";
|
||||
}
|
||||
break;
|
||||
case 0x12:
|
||||
R += "(meta)data";
|
||||
break;
|
||||
default:
|
||||
R += "unknown";
|
||||
break;
|
||||
}
|
||||
return R;
|
||||
};//tagtype
|
||||
unsigned int tagTime(){
|
||||
return (data[4] << 16) + (data[5] << 8) + data[6] + (data[7] << 24);
|
||||
}//tagTime getter
|
||||
void tagTime(unsigned int T){
|
||||
data[4] = ((T >> 16) & 0xFF);
|
||||
data[5] = ((T >> 8) & 0xFF);
|
||||
data[6] = (T & 0xFF);
|
||||
data[7] = ((T >> 24) & 0xFF);
|
||||
}//tagTime setter
|
||||
FLV_Pack(){
|
||||
len = 0; buf = 0; data = 0; isKeyframe = false;
|
||||
}//empty constructor
|
||||
FLV_Pack(const FLV_Pack& O){
|
||||
buf = O.len;
|
||||
len = buf;
|
||||
if (len > 0){
|
||||
data = (char*)malloc(len);
|
||||
memcpy(data, O.data, len);
|
||||
}else{
|
||||
data = 0;
|
||||
}
|
||||
isKeyframe = O.isKeyframe;
|
||||
}//copy constructor
|
||||
FLV_Pack & operator= (const FLV_Pack& O){
|
||||
if (this != &O){//no self-assignment
|
||||
if (data != 0){free(data);}
|
||||
buf = O.len;
|
||||
len = buf;
|
||||
if (len > 0){
|
||||
data = (char*)malloc(len);
|
||||
memcpy(data, O.data, len);
|
||||
}else{
|
||||
data = 0;
|
||||
}
|
||||
isKeyframe = O.isKeyframe;
|
||||
}
|
||||
return *this;
|
||||
}//assignment operator
|
||||
};//FLV_Pack
|
|
@ -1,106 +0,0 @@
|
|||
#include "flv_pack.cpp"
|
||||
|
||||
char FLVHeader[13];
|
||||
bool All_Hell_Broke_Loose = false;
|
||||
|
||||
//checks FLV Header for correctness
|
||||
//returns true if everything is alright, false otherwise
|
||||
bool FLV_Checkheader(char * header){
|
||||
if (header[0] != 'F') return false;
|
||||
if (header[1] != 'L') return false;
|
||||
if (header[2] != 'V') return false;
|
||||
if (header[8] != 0x09) return false;
|
||||
if (header[9] != 0) return false;
|
||||
if (header[10] != 0) return false;
|
||||
if (header[11] != 0) return false;
|
||||
if (header[12] != 0) return false;
|
||||
return true;
|
||||
}//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, int sock){
|
||||
if (sofar == count){return true;}
|
||||
int r = DDV_iread(buffer + sofar,count-sofar,sock);
|
||||
if (r < 0){
|
||||
if (errno != EWOULDBLOCK){
|
||||
All_Hell_Broke_Loose = true;
|
||||
fprintf(stderr, "ReadUntil fail: %s. All Hell Broke Loose!\n", strerror(errno));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
sofar += r;
|
||||
if (sofar == count){return true;}
|
||||
if (sofar > count){
|
||||
All_Hell_Broke_Loose = true;
|
||||
fprintf(stderr, "ReadUntil fail: %s. Read too much. All Hell Broke Loose!\n", strerror(errno));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//gets a packet, storing in given FLV_Pack pointer.
|
||||
//will assign pointer if null
|
||||
//resizes FLV_Pack data field bigger if data doesn't fit
|
||||
// (does not auto-shrink for speed!)
|
||||
bool FLV_GetPacket(FLV_Pack *& p, int sock){
|
||||
static bool done = true;
|
||||
static unsigned int sofar = 0;
|
||||
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 (done){
|
||||
//read a header
|
||||
if (ReadUntil(p->data, 11, sofar, sock)){
|
||||
//if its a correct FLV header, throw away and read tag header
|
||||
if (FLV_Isheader(p->data)){
|
||||
if (ReadUntil(p->data, 13, sofar, sock)){
|
||||
if (FLV_Checkheader(p->data)){
|
||||
sofar = 0;
|
||||
memcpy(FLVHeader, p->data, 13);
|
||||
//fwrite(p->data, 13, 1, stdout);//output raw stream
|
||||
}else{
|
||||
All_Hell_Broke_Loose = true;
|
||||
fprintf(stderr, "Invalid FLV header. All Hell Broke Loose!\n");
|
||||
}
|
||||
}
|
||||
}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);
|
||||
//fprintf(stderr, "Tag of len %i\n", p->len);
|
||||
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, sock)){
|
||||
//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;}
|
||||
int testlen = p->data[p->len-1] + 4;
|
||||
testlen += (p->data[p->len-2] << 8);
|
||||
testlen += (p->data[p->len-3] << 16);
|
||||
testlen += (p->data[p->len-4] << 24);
|
||||
//fwrite(p->data, p->len, 1, stdout);//output raw stream
|
||||
if (p->len != testlen){
|
||||
fprintf(stderr, "Len: %i, testlen: %i\n", p->len, testlen);
|
||||
All_Hell_Broke_Loose = true;
|
||||
fprintf(stderr, "ReadUntil fail: Wrong size tag? All Hell Broke Loose!\n");
|
||||
return false;
|
||||
}
|
||||
done = true;
|
||||
sofar = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}//FLV_GetPacket
|
||||
|
391
util/flv_tag.cpp
Normal file
391
util/flv_tag.cpp
Normal file
|
@ -0,0 +1,391 @@
|
|||
#include "flv_tag.h"
|
||||
#include <stdio.h> //for Tag::FileLoader
|
||||
#include <unistd.h> //for Tag::FileLoader
|
||||
#include <fcntl.h> //for Tag::FileLoader
|
||||
#include <stdlib.h> //malloc
|
||||
#include <string.h> //memcpy
|
||||
#include "ddv_socket.h" //socket functions
|
||||
|
||||
char FLV::Header[13]; ///< Holds the last FLV header parsed.
|
||||
bool FLV::Parse_Error = false; ///< This variable is set to true if a problem is encountered while parsing the FLV.
|
||||
|
||||
/// Checks a FLV Header for validness. Returns true if the header is valid, false
|
||||
/// if the header is not. Not valid can mean:
|
||||
/// - Not starting with the string "FLV".
|
||||
/// - The DataOffset is not 9 bytes.
|
||||
/// - The PreviousTagSize is not 0 bytes.
|
||||
///
|
||||
/// Note that we see PreviousTagSize as part of the FLV header, not part of the tag header!
|
||||
bool FLV::check_header(char * header){
|
||||
if (header[0] != 'F') return false;
|
||||
if (header[1] != 'L') return false;
|
||||
if (header[2] != 'V') return false;
|
||||
if (header[5] != 0) return false;
|
||||
if (header[6] != 0) return false;
|
||||
if (header[7] != 0) return false;
|
||||
if (header[8] != 0x09) return false;
|
||||
if (header[9] != 0) return false;
|
||||
if (header[10] != 0) return false;
|
||||
if (header[11] != 0) return false;
|
||||
if (header[12] != 0) return false;
|
||||
return true;
|
||||
}//FLV::check_header
|
||||
|
||||
/// Checks the first 3 bytes for the string "FLV". Implementing a basic FLV header check,
|
||||
/// returning true if it is, false if not.
|
||||
bool FLV::is_header(char * header){
|
||||
if (header[0] != 'F') return false;
|
||||
if (header[1] != 'L') return false;
|
||||
if (header[2] != 'V') return false;
|
||||
return true;
|
||||
}//FLV::is_header
|
||||
|
||||
|
||||
/// Returns a std::string describing the tag in detail.
|
||||
/// The string includes information about whether the tag is
|
||||
/// audio, video or metadata, what encoding is used, and the details
|
||||
/// of the encoding itself.
|
||||
std::string FLV::Tag::tagType(){
|
||||
std::string R = "";
|
||||
switch (data[0]){
|
||||
case 0x09:
|
||||
switch (data[11] & 0x0F){
|
||||
case 1: R += "JPEG"; break;
|
||||
case 2: R += "H263"; break;
|
||||
case 3: R += "ScreenVideo1"; break;
|
||||
case 4: R += "VP6"; break;
|
||||
case 5: R += "VP6Alpha"; break;
|
||||
case 6: R += "ScreenVideo2"; break;
|
||||
case 7: R += "AVC"; break;
|
||||
default: R += "unknown"; break;
|
||||
}
|
||||
R += " video ";
|
||||
switch (data[11] & 0xF0){
|
||||
case 0x10: R += "keyframe"; break;
|
||||
case 0x20: R += "iframe"; break;
|
||||
case 0x30: R += "disposableiframe"; break;
|
||||
case 0x40: R += "generatedkeyframe"; break;
|
||||
case 0x50: R += "videoinfo"; break;
|
||||
}
|
||||
if ((data[11] & 0x0F) == 7){
|
||||
switch (data[12]){
|
||||
case 0: R += " header"; break;
|
||||
case 1: R += " NALU"; break;
|
||||
case 2: R += " endofsequence"; break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 0x08:
|
||||
switch (data[11] & 0xF0){
|
||||
case 0x00: R += "linear PCM PE"; break;
|
||||
case 0x10: R += "ADPCM"; break;
|
||||
case 0x20: R += "MP3"; break;
|
||||
case 0x30: R += "linear PCM LE"; break;
|
||||
case 0x40: R += "Nelly16kHz"; break;
|
||||
case 0x50: R += "Nelly8kHz"; break;
|
||||
case 0x60: R += "Nelly"; break;
|
||||
case 0x70: R += "G711A-law"; break;
|
||||
case 0x80: R += "G711mu-law"; break;
|
||||
case 0x90: R += "reserved"; break;
|
||||
case 0xA0: R += "AAC"; break;
|
||||
case 0xB0: R += "Speex"; break;
|
||||
case 0xE0: R += "MP38kHz"; break;
|
||||
case 0xF0: R += "DeviceSpecific"; break;
|
||||
default: R += "unknown"; break;
|
||||
}
|
||||
switch (data[11] & 0x0C){
|
||||
case 0x0: R += " 5.5kHz"; break;
|
||||
case 0x4: R += " 11kHz"; break;
|
||||
case 0x8: R += " 22kHz"; break;
|
||||
case 0xC: R += " 44kHz"; break;
|
||||
}
|
||||
switch (data[11] & 0x02){
|
||||
case 0: R += " 8bit"; break;
|
||||
case 2: R += " 16bit"; break;
|
||||
}
|
||||
switch (data[11] & 0x01){
|
||||
case 0: R += " mono"; break;
|
||||
case 1: R += " stereo"; break;
|
||||
}
|
||||
R += " audio";
|
||||
if ((data[12] == 0) && ((data[11] & 0xF0) == 0xA0)){
|
||||
R += " initdata";
|
||||
}
|
||||
break;
|
||||
case 0x12:
|
||||
R += "(meta)data";
|
||||
break;
|
||||
default:
|
||||
R += "unknown";
|
||||
break;
|
||||
}
|
||||
return R;
|
||||
}//FLV::Tag::tagtype
|
||||
|
||||
/// Returns the 32-bit timestamp of this tag.
|
||||
unsigned int FLV::Tag::tagTime(){
|
||||
return (data[4] << 16) + (data[5] << 8) + data[6] + (data[7] << 24);
|
||||
}//tagTime getter
|
||||
|
||||
/// Sets the 32-bit timestamp of this tag.
|
||||
void FLV::Tag::tagTime(unsigned int T){
|
||||
data[4] = ((T >> 16) & 0xFF);
|
||||
data[5] = ((T >> 8) & 0xFF);
|
||||
data[6] = (T & 0xFF);
|
||||
data[7] = ((T >> 24) & 0xFF);
|
||||
}//tagTime setter
|
||||
|
||||
/// Constructor for a new, empty, tag.
|
||||
/// The buffer length is initialized to 0, and later automatically
|
||||
/// increased if neccesary.
|
||||
FLV::Tag::Tag(){
|
||||
len = 0; buf = 0; data = 0; isKeyframe = false;
|
||||
}//empty constructor
|
||||
|
||||
/// Copy constructor, copies the contents of an existing tag.
|
||||
/// The buffer length is initialized to the actual size of the tag
|
||||
/// that is being copied, and later automaticallt increased if
|
||||
/// neccesary.
|
||||
FLV::Tag::Tag(const Tag& O){
|
||||
buf = O.len;
|
||||
len = buf;
|
||||
if (len > 0){
|
||||
data = (char*)malloc(len);
|
||||
memcpy(data, O.data, len);
|
||||
}else{
|
||||
data = 0;
|
||||
}
|
||||
isKeyframe = O.isKeyframe;
|
||||
}//copy constructor
|
||||
|
||||
/// Assignment operator - works exactly like the copy constructor.
|
||||
/// This operator checks for self-assignment.
|
||||
FLV::Tag & FLV::Tag::operator= (const FLV::Tag& O){
|
||||
if (this != &O){//no self-assignment
|
||||
if (data != 0){free(data);}
|
||||
buf = O.len;
|
||||
len = buf;
|
||||
if (len > 0){
|
||||
data = (char*)malloc(len);
|
||||
memcpy(data, O.data, len);
|
||||
}else{
|
||||
data = 0;
|
||||
}
|
||||
isKeyframe = O.isKeyframe;
|
||||
}
|
||||
return *this;
|
||||
}//assignment operator
|
||||
|
||||
/// Helper function for FLV::MemLoader.
|
||||
/// This function will try to read count bytes from data buffer D into buffer.
|
||||
/// This function should be called repeatedly until true.
|
||||
/// P and sofar are not the same value, because D may not start with the current tag.
|
||||
/// \param buffer The target buffer.
|
||||
/// \param count Amount of bytes to read.
|
||||
/// \param sofar Current amount read.
|
||||
/// \param D The location of the data buffer.
|
||||
/// \param S The size of the data buffer.
|
||||
/// \param P The current position in the data buffer. Will be updated to reflect new position.
|
||||
/// \return True if count bytes are read succesfully, false otherwise.
|
||||
bool FLV::Tag::MemReadUntil(char * buffer, unsigned int count, unsigned int & sofar, char * D, unsigned int S, unsigned int & P){
|
||||
if (sofar >= count){return true;}
|
||||
int r = 0;
|
||||
if (P+(count-sofar) > S){r = S-P;}else{r = count-sofar;}
|
||||
memcpy(buffer+sofar, D+P, r);
|
||||
P += r;
|
||||
sofar += r;
|
||||
if (sofar >= count){return true;}
|
||||
return false;
|
||||
}//Tag::MemReadUntil
|
||||
|
||||
|
||||
/// Try to load a tag from a data buffer in memory.
|
||||
/// This is a stateful function - if fed incorrect data, it will most likely never return true again!
|
||||
/// While this function returns false, the Tag might not contain valid data.
|
||||
/// \param D The location of the data buffer.
|
||||
/// \param S The size of the data buffer.
|
||||
/// \param P The current position in the data buffer. Will be updated to reflect new position.
|
||||
/// \return True if a whole tag is succesfully read, false otherwise.
|
||||
bool FLV::Tag::MemLoader(char * D, unsigned int S, unsigned int & P){
|
||||
static bool done = true;
|
||||
static unsigned int sofar = 0;
|
||||
if (buf < 15){data = (char*)realloc(data, 15); buf = 15;}
|
||||
if (done){
|
||||
//read a header
|
||||
if (MemReadUntil(data, 11, sofar, D, S, P)){
|
||||
//if its a correct FLV header, throw away and read tag header
|
||||
if (FLV::is_header(data)){
|
||||
if (MemReadUntil(data, 13, sofar, D, S, P)){
|
||||
if (FLV::check_header(data)){
|
||||
sofar = 0;
|
||||
memcpy(FLV::Header, data, 13);
|
||||
}else{FLV::Parse_Error = true; return false;}
|
||||
}
|
||||
}else{
|
||||
//if a tag header, calculate length and read tag body
|
||||
len = data[3] + 15;
|
||||
len += (data[2] << 8);
|
||||
len += (data[1] << 16);
|
||||
if (buf < len){data = (char*)realloc(data, len); buf = len;}
|
||||
if (data[0] > 0x12){FLV::Parse_Error = true; return false;}
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
//read tag body
|
||||
if (MemReadUntil(data, len, sofar, D, S, P)){
|
||||
//calculate keyframeness, next time read header again, return true
|
||||
if ((data[0] == 0x09) && (((data[11] & 0xf0) >> 4) == 1)){isKeyframe = true;}else{isKeyframe = false;}
|
||||
done = true;
|
||||
sofar = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}//Tag::MemLoader
|
||||
|
||||
|
||||
/// Helper function for FLV::SockLoader.
|
||||
/// This function will try to read count bytes from socket sock into buffer.
|
||||
/// This function should be called repeatedly until true.
|
||||
/// \param buffer The target buffer.
|
||||
/// \param count Amount of bytes to read.
|
||||
/// \param sofar Current amount read.
|
||||
/// \param sock Socket to read from.
|
||||
/// \return True if count bytes are read succesfully, false otherwise.
|
||||
bool FLV::Tag::SockReadUntil(char * buffer, unsigned int count, unsigned int & sofar, DDV::Socket & sock){
|
||||
if (sofar == count){return true;}
|
||||
int r = sock.read(buffer + sofar,count-sofar);
|
||||
if (r < 0){
|
||||
if (errno != EWOULDBLOCK){
|
||||
FLV::Parse_Error = true;
|
||||
fprintf(stderr, "ReadUntil fail: %s. All Hell Broke Loose!\n", strerror(errno));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
sofar += r;
|
||||
if (sofar == count){return true;}
|
||||
if (sofar > count){
|
||||
FLV::Parse_Error = true;
|
||||
fprintf(stderr, "ReadUntil fail: %s. Read too much. All Hell Broke Loose!\n", strerror(errno));
|
||||
}
|
||||
return false;
|
||||
}//Tag::SockReadUntil
|
||||
|
||||
/// Try to load a tag from a socket.
|
||||
/// This is a stateful function - if fed incorrect data, it will most likely never return true again!
|
||||
/// While this function returns false, the Tag might not contain valid data.
|
||||
/// \param sock The socket to read from.
|
||||
/// \return True if a whole tag is succesfully read, false otherwise.
|
||||
bool FLV::Tag::SockLoader(DDV::Socket sock){
|
||||
static bool done = true;
|
||||
static unsigned int sofar = 0;
|
||||
if (buf < 15){data = (char*)realloc(data, 15); buf = 15;}
|
||||
if (done){
|
||||
if (SockReadUntil(data, 11, sofar, sock)){
|
||||
//if its a correct FLV header, throw away and read tag header
|
||||
if (FLV::is_header(data)){
|
||||
if (SockReadUntil(data, 13, sofar, sock)){
|
||||
if (FLV::check_header(data)){
|
||||
sofar = 0;
|
||||
memcpy(FLV::Header, data, 13);
|
||||
}else{FLV::Parse_Error = true; return false;}
|
||||
}
|
||||
}else{
|
||||
//if a tag header, calculate length and read tag body
|
||||
len = data[3] + 15;
|
||||
len += (data[2] << 8);
|
||||
len += (data[1] << 16);
|
||||
if (buf < len){data = (char*)realloc(data, len); buf = len;}
|
||||
if (data[0] > 0x12){FLV::Parse_Error = true; return false;}
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
//read tag body
|
||||
if (SockReadUntil(data, len, sofar, sock)){
|
||||
//calculate keyframeness, next time read header again, return true
|
||||
if ((data[0] == 0x09) && (((data[11] & 0xf0) >> 4) == 1)){isKeyframe = true;}else{isKeyframe = false;}
|
||||
done = true;
|
||||
sofar = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}//Tag::SockLoader
|
||||
|
||||
/// Try to load a tag from a socket.
|
||||
/// This is a stateful function - if fed incorrect data, it will most likely never return true again!
|
||||
/// While this function returns false, the Tag might not contain valid data.
|
||||
/// \param sock The socket to read from.
|
||||
/// \return True if a whole tag is succesfully read, false otherwise.
|
||||
bool FLV::Tag::SockLoader(int sock){
|
||||
return SockLoader(DDV::Socket(sock));
|
||||
}//Tag::SockLoader
|
||||
|
||||
/// Helper function for FLV::FileLoader.
|
||||
/// This function will try to read count bytes from file f into buffer.
|
||||
/// This function should be called repeatedly until true.
|
||||
/// \param buffer The target buffer.
|
||||
/// \param count Amount of bytes to read.
|
||||
/// \param sofar Current amount read.
|
||||
/// \param f File to read from.
|
||||
/// \return True if count bytes are read succesfully, false otherwise.
|
||||
bool FLV::Tag::FileReadUntil(char * buffer, unsigned int count, unsigned int & sofar, FILE * f){
|
||||
if (sofar >= count){return true;}
|
||||
int r = 0;
|
||||
r = fread(buffer + sofar,1,count-sofar,f);
|
||||
if (r < 0){FLV::Parse_Error = true; return false;}
|
||||
sofar += r;
|
||||
if (sofar >= count){return true;}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Try to load a tag from a file.
|
||||
/// This is a stateful function - if fed incorrect data, it will most likely never return true again!
|
||||
/// While this function returns false, the Tag might not contain valid data.
|
||||
/// \param f The file to read from.
|
||||
/// \return True if a whole tag is succesfully read, false otherwise.
|
||||
bool FLV::Tag::FileLoader(FILE * f){
|
||||
int preflags = fcntl(fileno(f), F_GETFL, 0);
|
||||
int postflags = preflags | O_NONBLOCK;
|
||||
fcntl(fileno(f), F_SETFL, postflags);
|
||||
static bool done = true;
|
||||
static unsigned int sofar = 0;
|
||||
if (buf < 15){data = (char*)realloc(data, 15); buf = 15;}
|
||||
|
||||
if (done){
|
||||
//read a header
|
||||
if (FileReadUntil(data, 11, sofar, f)){
|
||||
//if its a correct FLV header, throw away and read tag header
|
||||
if (FLV::is_header(data)){
|
||||
if (FileReadUntil(data, 13, sofar, f)){
|
||||
if (FLV::check_header(data)){
|
||||
sofar = 0;
|
||||
memcpy(FLV::Header, data, 13);
|
||||
}else{FLV::Parse_Error = true;}
|
||||
}
|
||||
}else{
|
||||
//if a tag header, calculate length and read tag body
|
||||
len = data[3] + 15;
|
||||
len += (data[2] << 8);
|
||||
len += (data[1] << 16);
|
||||
if (buf < len){data = (char*)realloc(data, len); buf = len;}
|
||||
if (data[0] > 0x12){FLV::Parse_Error = true;}
|
||||
done = false;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
//read tag body
|
||||
if (FileReadUntil(data, len, sofar, f)){
|
||||
//calculate keyframeness, next time read header again, return true
|
||||
if ((data[0] == 0x09) && (((data[11] & 0xf0) >> 4) == 1)){isKeyframe = true;}else{isKeyframe = false;}
|
||||
done = true;
|
||||
sofar = 0;
|
||||
fcntl(fileno(f), F_SETFL, preflags);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
fcntl(fileno(f), F_SETFL, preflags);
|
||||
return false;
|
||||
}//FLV_GetPacket
|
40
util/flv_tag.h
Normal file
40
util/flv_tag.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
#include "ddv_socket.h"
|
||||
#include <string>
|
||||
|
||||
/// This namespace holds all FLV-parsing related functionality.
|
||||
namespace FLV {
|
||||
//variables
|
||||
extern char Header[13]; ///< Holds the last FLV header parsed.
|
||||
extern bool Parse_Error; ///< This variable is set to true if a problem is encountered while parsing the FLV.
|
||||
|
||||
//functions
|
||||
bool check_header(char * header); ///< Checks a FLV Header for validness.
|
||||
bool is_header(char * header); ///< Checks the first 3 bytes for the string "FLV".
|
||||
|
||||
/// This class is used to hold, work with and get information about a single FLV tag.
|
||||
class Tag {
|
||||
public:
|
||||
int len; ///< Actual length of tag.
|
||||
bool isKeyframe; ///< True if current tag is a video keyframe.
|
||||
char * data; ///< Pointer to tag buffer.
|
||||
std::string tagType(); ///< Returns a std::string describing the tag in detail.
|
||||
unsigned int tagTime(); ///< Returns the 32-bit timestamp of this tag.
|
||||
void tagTime(unsigned int T); ///< Sets the 32-bit timestamp of this tag.
|
||||
Tag(); ///< Constructor for a new, empty, tag.
|
||||
Tag(const Tag& O); ///< Copy constructor, copies the contents of an existing tag.
|
||||
Tag & operator= (const Tag& O); ///< Assignment operator - works exactly like the copy constructor.
|
||||
//loader functions
|
||||
bool MemLoader(char * D, unsigned int S, unsigned int & P);
|
||||
bool SockLoader(int sock);
|
||||
bool SockLoader(DDV::Socket sock);
|
||||
bool FileLoader(FILE * f);
|
||||
protected:
|
||||
int buf; ///< Maximum length of buffer space.
|
||||
//loader helper functions
|
||||
bool MemReadUntil(char * buffer, unsigned int count, unsigned int & sofar, char * D, unsigned int S, unsigned int & P);
|
||||
bool SockReadUntil(char * buffer, unsigned int count, unsigned int & sofar, DDV::Socket & sock);
|
||||
bool FileReadUntil(char * buffer, unsigned int count, unsigned int & sofar, FILE * f);
|
||||
};//Tag
|
||||
|
||||
};//FLV namespace
|
|
@ -1,42 +1,5 @@
|
|||
#pragma once
|
||||
#include "ddv_socket.cpp"
|
||||
#include <map>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class HTTPReader{
|
||||
public:
|
||||
HTTPReader();
|
||||
bool ReadSocket(int CONN_fd);
|
||||
bool ReadSocket(FILE * F);
|
||||
std::string GetHeader(std::string i);
|
||||
std::string GetVar(std::string i);
|
||||
void SetHeader(std::string i, std::string v);
|
||||
void SetHeader(std::string i, int v);
|
||||
void SetVar(std::string i, std::string v);
|
||||
void SetBody(std::string s);
|
||||
void SetBody(char * buffer, int len);
|
||||
std::string BuildRequest();
|
||||
std::string BuildResponse(std::string code, std::string message);
|
||||
void SendResponse(int conn, std::string code, std::string message);
|
||||
void SendBodyPart(int conn, char * buffer, int len);
|
||||
void SendBodyPart(int conn, std::string bodypart);
|
||||
void Clean();
|
||||
bool CleanForNext();
|
||||
std::string body;
|
||||
std::string method;
|
||||
std::string url;
|
||||
std::string protocol;
|
||||
unsigned int length;
|
||||
private:
|
||||
bool seenHeaders;
|
||||
bool seenReq;
|
||||
bool parse();
|
||||
std::string HTTPbuffer;
|
||||
std::map<std::string, std::string> headers;
|
||||
std::map<std::string, std::string> vars;
|
||||
void Trim(std::string & s);
|
||||
};//HTTPReader
|
||||
#include "http_parser.h"
|
||||
#include "ddv_socket.h"
|
||||
|
||||
HTTPReader::HTTPReader(){Clean();}
|
||||
void HTTPReader::Clean(){
|
||||
|
@ -127,29 +90,28 @@ void HTTPReader::SetVar(std::string i, std::string v){
|
|||
vars[i] = v;
|
||||
}
|
||||
|
||||
bool HTTPReader::ReadSocket(int CONN_fd){
|
||||
bool HTTPReader::Read(DDV::Socket & sock){
|
||||
//returned true als hele http packet gelezen is
|
||||
int r = 0;
|
||||
int b = 0;
|
||||
char buffer[500];
|
||||
while (true){
|
||||
r = DDV_ready(CONN_fd);
|
||||
r = sock.ready();
|
||||
if (r < 1){
|
||||
if (r == 0){
|
||||
socketError = true;
|
||||
if (r == -1){
|
||||
#if DEBUG >= 1
|
||||
fprintf(stderr, "User socket is disconnected.\n");
|
||||
#endif
|
||||
}
|
||||
return parse();
|
||||
}
|
||||
b = DDV_iread(buffer, 500, CONN_fd);
|
||||
b = sock.iread(buffer, 500);
|
||||
HTTPbuffer.append(buffer, b);
|
||||
}
|
||||
return false;
|
||||
}//HTTPReader::ReadSocket
|
||||
|
||||
bool HTTPReader::ReadSocket(FILE * F){
|
||||
bool HTTPReader::Read(FILE * F){
|
||||
//returned true als hele http packet gelezen is
|
||||
int b = 1;
|
||||
char buffer[500];
|
||||
|
@ -210,23 +172,23 @@ bool HTTPReader::parse(){
|
|||
return false; //we should never get here...
|
||||
}//HTTPReader::parse
|
||||
|
||||
void HTTPReader::SendResponse(int conn, std::string code, std::string message){
|
||||
void HTTPReader::SendResponse(DDV::Socket & conn, std::string code, std::string message){
|
||||
std::string tmp = BuildResponse(code, message);
|
||||
DDV_write(tmp.c_str(), tmp.size(), conn);
|
||||
conn.write(tmp);
|
||||
}
|
||||
|
||||
void HTTPReader::SendBodyPart(int conn, char * buffer, int len){
|
||||
void HTTPReader::SendBodyPart(DDV::Socket & conn, char * buffer, int len){
|
||||
std::string tmp;
|
||||
tmp.append(buffer, len);
|
||||
SendBodyPart(conn, tmp);
|
||||
}
|
||||
|
||||
void HTTPReader::SendBodyPart(int conn, std::string bodypart){
|
||||
void HTTPReader::SendBodyPart(DDV::Socket & conn, std::string bodypart){
|
||||
static char len[10];
|
||||
int sizelen;
|
||||
sizelen = snprintf(len, 10, "%x\r\n", (unsigned int)bodypart.size());
|
||||
DDV_write(len, sizelen, conn);
|
||||
DDV_write(bodypart.c_str(), bodypart.size(), conn);
|
||||
DDV_write(len+sizelen-2, 2, conn);
|
||||
conn.write(len, sizelen);
|
||||
conn.write(bodypart);
|
||||
conn.write(len+sizelen-2, 2);
|
||||
}
|
||||
|
||||
|
|
41
util/http_parser.h
Normal file
41
util/http_parser.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "ddv_socket.h"
|
||||
|
||||
class HTTPReader{
|
||||
public:
|
||||
HTTPReader();
|
||||
bool Read(DDV::Socket & sock);
|
||||
bool Read(FILE * F);
|
||||
std::string GetHeader(std::string i);
|
||||
std::string GetVar(std::string i);
|
||||
void SetHeader(std::string i, std::string v);
|
||||
void SetHeader(std::string i, int v);
|
||||
void SetVar(std::string i, std::string v);
|
||||
void SetBody(std::string s);
|
||||
void SetBody(char * buffer, int len);
|
||||
std::string BuildRequest();
|
||||
std::string BuildResponse(std::string code, std::string message);
|
||||
void SendResponse(DDV::Socket & conn, std::string code, std::string message);
|
||||
void SendBodyPart(DDV::Socket & conn, char * buffer, int len);
|
||||
void SendBodyPart(DDV::Socket & conn, std::string bodypart);
|
||||
void Clean();
|
||||
bool CleanForNext();
|
||||
std::string body;
|
||||
std::string method;
|
||||
std::string url;
|
||||
std::string protocol;
|
||||
unsigned int length;
|
||||
private:
|
||||
bool seenHeaders;
|
||||
bool seenReq;
|
||||
bool parse();
|
||||
std::string HTTPbuffer;
|
||||
std::map<std::string, std::string> headers;
|
||||
std::map<std::string, std::string> vars;
|
||||
void Trim(std::string & s);
|
||||
};//HTTPReader
|
||||
|
|
@ -1,23 +1,21 @@
|
|||
int mainHandler(int CONN_fd);//define this function in your own code!
|
||||
#include <signal.h>
|
||||
#include "ddv_socket.cpp" //DDVTech Socket wrapper
|
||||
#include "flv_sock.cpp" //FLV parsing with DDVTech Socket wrapper
|
||||
int server_socket = 0;
|
||||
#include "ddv_socket.h" //DDVTech Socket wrapper
|
||||
#include "flv_tag.h" //FLV parsing with DDVTech Socket wrapper
|
||||
DDV::ServerSocket server_socket(-1);
|
||||
|
||||
void termination_handler (int signum){
|
||||
if (server_socket == 0) return;
|
||||
if (!server_socket.connected()) return;
|
||||
switch (signum){
|
||||
case SIGINT: break;
|
||||
case SIGHUP: break;
|
||||
case SIGTERM: break;
|
||||
default: return; break;
|
||||
}
|
||||
close(server_socket);
|
||||
server_socket = 0;
|
||||
server_socket.close();
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv){
|
||||
int CONN_fd = 0;
|
||||
DDV::Socket CONN_fd(-1);
|
||||
|
||||
//setup signal handler
|
||||
struct sigaction new_action;
|
||||
|
@ -31,12 +29,14 @@ int main(int argc, char ** argv){
|
|||
|
||||
int listen_port = DEFAULT_PORT;
|
||||
bool daemon_mode = true;
|
||||
std::string interface = "0.0.0.0";
|
||||
|
||||
int opt = 0;
|
||||
static const char *optString = "np:h?";
|
||||
static const char *optString = "np:i:h?";
|
||||
static const struct option longOpts[] = {
|
||||
{"help",0,0,'h'},
|
||||
{"port",1,0,'p'},
|
||||
{"interface",1,0,'i'},
|
||||
{"no-daemon",0,0,'n'}
|
||||
};
|
||||
while ((opt = getopt_long(argc, argv, optString, longOpts, 0)) != -1){
|
||||
|
@ -44,6 +44,9 @@ int main(int argc, char ** argv){
|
|||
case 'p':
|
||||
listen_port = atoi(optarg);
|
||||
break;
|
||||
case 'i':
|
||||
interface = optarg;
|
||||
break;
|
||||
case 'n':
|
||||
daemon_mode = false;
|
||||
break;
|
||||
|
@ -55,11 +58,11 @@ int main(int argc, char ** argv){
|
|||
}
|
||||
}
|
||||
|
||||
server_socket = DDV_Listen(listen_port);
|
||||
server_socket = DDV::ServerSocket(listen_port, interface);
|
||||
#if DEBUG >= 3
|
||||
fprintf(stderr, "Made a listening socket on port %i...\n", listen_port);
|
||||
fprintf(stderr, "Made a listening socket on %s:%i...\n", interface.c_str(), listen_port);
|
||||
#endif
|
||||
if (server_socket > 0){
|
||||
if (server_socket.connected()){
|
||||
if (daemon_mode){
|
||||
daemon(1, 0);
|
||||
#if DEBUG >= 3
|
||||
|
@ -73,22 +76,22 @@ int main(int argc, char ** argv){
|
|||
return 1;
|
||||
}
|
||||
int status;
|
||||
while (server_socket > 0){
|
||||
while (server_socket.connected()){
|
||||
waitpid((pid_t)-1, &status, WNOHANG);
|
||||
CONN_fd = DDV_Accept(server_socket);
|
||||
if (CONN_fd > 0){
|
||||
CONN_fd = server_socket.accept();
|
||||
if (CONN_fd.connected()){
|
||||
pid_t myid = fork();
|
||||
if (myid == 0){
|
||||
break;
|
||||
}else{
|
||||
#if DEBUG >= 3
|
||||
fprintf(stderr, "Spawned new process %i for handling socket %i\n", (int)myid, CONN_fd);
|
||||
fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, CONN_fd.getSocket());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
if (server_socket <= 0){
|
||||
if (!server_socket.connected()){
|
||||
return 0;
|
||||
}
|
||||
return mainHandler(CONN_fd);
|
||||
return MAINHANDLER(CONN_fd);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue