Converted to new libmist JSON support, added dtscfix for metadata rewriting.
This commit is contained in:
parent
61abc0e0a1
commit
b54ee2dcd2
6 changed files with 185 additions and 37 deletions
|
@ -10,7 +10,8 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <mist/dtsc.h> //DTSC support
|
#include <mist/dtsc.h>
|
||||||
|
#include <mist/json.h>
|
||||||
#include <mist/config.h>
|
#include <mist/config.h>
|
||||||
|
|
||||||
/// Reads DTSC from stdin and outputs human-readable information to stderr.
|
/// Reads DTSC from stdin and outputs human-readable information to stderr.
|
||||||
|
@ -50,6 +51,9 @@ int main(int argc, char ** argv) {
|
||||||
if (!doneheader){
|
if (!doneheader){
|
||||||
doneheader = true;
|
doneheader = true;
|
||||||
Strm.metadata.Print();
|
Strm.metadata.Print();
|
||||||
|
JSON::Value mdata = JSON::fromDTMI(Strm.metadata.Pack(false));
|
||||||
|
std::cout << mdata.toString() << std::endl;
|
||||||
|
std::cout << mdata.toPrettyString() << std::endl;
|
||||||
}
|
}
|
||||||
Strm.getPacket().Print();
|
Strm.getPacket().Print();
|
||||||
//get current timestamp
|
//get current timestamp
|
||||||
|
|
|
@ -25,14 +25,14 @@
|
||||||
namespace Connector_HTTP{
|
namespace Connector_HTTP{
|
||||||
|
|
||||||
/// Returns a F4M-format manifest file
|
/// Returns a F4M-format manifest file
|
||||||
std::string BuildManifest(std::string MovieId) {
|
std::string BuildManifest(std::string MovieId, JSON::Value & metadata) {
|
||||||
std::string Result="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
|
std::string Result="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
|
||||||
"<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n"
|
"<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n"
|
||||||
"<id>" + MovieId + "</id>\n"
|
"<id>" + MovieId + "</id>\n"
|
||||||
"<mimeType>video/mp4</mimeType>\n"
|
"<mimeType>video/mp4</mimeType>\n"
|
||||||
"<streamType>live</streamType>\n"
|
"<streamType>live</streamType>\n"
|
||||||
"<deliveryType>streaming</deliveryType>\n"
|
"<deliveryType>streaming</deliveryType>\n"
|
||||||
"<bootstrapInfo profile=\"named\" id=\"bootstrap1\">" + Base64::encode(MP4::GenerateLiveBootstrap(1)) + "</bootstrapInfo>\n"
|
"<bootstrapInfo profile=\"named\" id=\"bootstrap1\">" + Base64::encode(MP4::GenerateLiveBootstrap(metadata)) + "</bootstrapInfo>\n"
|
||||||
"<media streamId=\"1\" bootstrapInfoId=\"bootstrap1\" url=\"" + MovieId + "/\">\n"
|
"<media streamId=\"1\" bootstrapInfoId=\"bootstrap1\" url=\"" + MovieId + "/\">\n"
|
||||||
"</media>\n"
|
"</media>\n"
|
||||||
"</manifest>\n";
|
"</manifest>\n";
|
||||||
|
@ -49,6 +49,7 @@ namespace Connector_HTTP{
|
||||||
HTTP::Parser HTTP_R, HTTP_S;//HTTP Receiver en HTTP Sender.
|
HTTP::Parser HTTP_R, HTTP_S;//HTTP Receiver en HTTP Sender.
|
||||||
|
|
||||||
bool ready4data = false;//Set to true when streaming is to begin.
|
bool ready4data = false;//Set to true when streaming is to begin.
|
||||||
|
bool pending_manifest = false;
|
||||||
bool inited = false;
|
bool inited = false;
|
||||||
Socket::Connection ss(-1);
|
Socket::Connection ss(-1);
|
||||||
std::string streamname;
|
std::string streamname;
|
||||||
|
@ -84,15 +85,7 @@ namespace Connector_HTTP{
|
||||||
Flash_RequestPending++;
|
Flash_RequestPending++;
|
||||||
}else{
|
}else{
|
||||||
streamname = HTTP_R.url.substr(1,HTTP_R.url.find("/",1)-1);
|
streamname = HTTP_R.url.substr(1,HTTP_R.url.find("/",1)-1);
|
||||||
HTTP_S.Clean();
|
pending_manifest = true;
|
||||||
HTTP_S.SetHeader("Content-Type","text/xml");
|
|
||||||
HTTP_S.SetHeader("Cache-Control","no-cache");
|
|
||||||
std::string manifest = BuildManifest(Movie);
|
|
||||||
HTTP_S.SetBody(manifest);
|
|
||||||
conn.Send(HTTP_S.BuildResponse("200", "OK"));
|
|
||||||
#if DEBUG >= 3
|
|
||||||
printf("Sent manifest\n");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
ready4data = true;
|
ready4data = true;
|
||||||
HTTP_R.Clean(); //clean for any possible next requests
|
HTTP_R.Clean(); //clean for any possible next requests
|
||||||
|
@ -141,6 +134,19 @@ namespace Connector_HTTP{
|
||||||
if (ss.spool() || ss.Received() != ""){
|
if (ss.spool() || ss.Received() != ""){
|
||||||
if (Strm.parsePacket(ss.Received())){
|
if (Strm.parsePacket(ss.Received())){
|
||||||
tag.DTSCLoader(Strm);
|
tag.DTSCLoader(Strm);
|
||||||
|
if (pending_manifest){
|
||||||
|
JSON::Value meta = JSON::fromDTMI(Strm.metadata.Pack(false));
|
||||||
|
HTTP_S.Clean();
|
||||||
|
HTTP_S.SetHeader("Content-Type","text/xml");
|
||||||
|
HTTP_S.SetHeader("Cache-Control","no-cache");
|
||||||
|
std::string manifest = BuildManifest(Movie, meta);
|
||||||
|
HTTP_S.SetBody(manifest);
|
||||||
|
conn.Send(HTTP_S.BuildResponse("200", "OK"));
|
||||||
|
#if DEBUG >= 3
|
||||||
|
printf("Sent manifest\n");
|
||||||
|
#endif
|
||||||
|
pending_manifest = false;
|
||||||
|
}
|
||||||
if (Strm.getPacket(0).getContentP("keyframe")){
|
if (Strm.getPacket(0).getContentP("keyframe")){
|
||||||
if (FlashBuf != ""){
|
if (FlashBuf != ""){
|
||||||
Flash_FragBuffer.push(FlashBuf);
|
Flash_FragBuffer.push(FlashBuf);
|
||||||
|
|
|
@ -140,7 +140,7 @@ int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
|
||||||
/// Tries to get and parse one RTMP chunk at a time.
|
/// Tries to get and parse one RTMP chunk at a time.
|
||||||
void Connector_RTMP::parseChunk(std::string & inbuffer){
|
void Connector_RTMP::parseChunk(std::string & inbuffer){
|
||||||
//for DTSC conversion
|
//for DTSC conversion
|
||||||
static DTSC::DTMI meta_out;
|
static JSON::Value meta_out;
|
||||||
static std::stringstream prebuffer; // Temporary buffer before sending real data
|
static std::stringstream prebuffer; // Temporary buffer before sending real data
|
||||||
static bool sending = false;
|
static bool sending = false;
|
||||||
static unsigned int counter = 0;
|
static unsigned int counter = 0;
|
||||||
|
@ -222,23 +222,37 @@ void Connector_RTMP::parseChunk(std::string & inbuffer){
|
||||||
case 18://meta data
|
case 18://meta data
|
||||||
if (SS.connected()){
|
if (SS.connected()){
|
||||||
F.ChunkLoader(next);
|
F.ChunkLoader(next);
|
||||||
DTSC::DTMI pack_out = F.toDTSC(meta_out);
|
JSON::Value pack_out = F.toJSON(meta_out);
|
||||||
if (!pack_out.isEmpty()){
|
if (!pack_out.isNull()){
|
||||||
if (!sending){
|
if (!sending){
|
||||||
counter++;
|
counter++;
|
||||||
if (counter > 8){
|
if (counter > 8){
|
||||||
sending = true;
|
sending = true;
|
||||||
meta_out.Pack(true);//pack metadata
|
std::string packed = meta_out.toPacked();
|
||||||
meta_out.packed.replace(0, 4, DTSC::Magic_Header);//prepare proper header
|
unsigned int size = htonl(packed.size());
|
||||||
SS.write(meta_out.packed);//write header/metadata
|
SS.write(std::string(DTSC::Magic_Header, 4));
|
||||||
|
SS.write(std::string((char*)&size, 4));
|
||||||
|
SS.write(packed);
|
||||||
SS.write(prebuffer.str());//write buffer
|
SS.write(prebuffer.str());//write buffer
|
||||||
prebuffer.str("");//clear buffer
|
prebuffer.str("");//clear buffer
|
||||||
SS.write(pack_out.Pack(true));//simply write
|
packed = pack_out.toPacked();
|
||||||
|
size = htonl(packed.size());
|
||||||
|
SS.write(std::string(DTSC::Magic_Packet, 4));
|
||||||
|
SS.write(std::string((char*)&size, 4));
|
||||||
|
SS.write(packed);
|
||||||
}else{
|
}else{
|
||||||
prebuffer << pack_out.Pack(true);//buffer
|
std::string packed = pack_out.toPacked();
|
||||||
|
unsigned int size = htonl(packed.size());
|
||||||
|
prebuffer << std::string(DTSC::Magic_Packet, 4);
|
||||||
|
prebuffer << std::string((char*)&size, 4);
|
||||||
|
prebuffer << packed;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
SS.write(pack_out.Pack(true));//simple write
|
std::string packed = pack_out.toPacked();
|
||||||
|
unsigned int size = htonl(packed.size());
|
||||||
|
SS.write(std::string(DTSC::Magic_Packet, 4));
|
||||||
|
SS.write(std::string((char*)&size, 4));
|
||||||
|
SS.write(packed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
AM_CPPFLAGS = $(MIST_CFLAGS)
|
AM_CPPFLAGS = $(MIST_CFLAGS)
|
||||||
LDADD = $(MIST_LIBS)
|
LDADD = $(MIST_LIBS)
|
||||||
bin_PROGRAMS=MistDTSC2FLV MistFLV2DTSC
|
bin_PROGRAMS=MistDTSC2FLV MistFLV2DTSC MistDTSCFix
|
||||||
MistDTSC2FLV_SOURCES=dtsc2flv.cpp
|
MistDTSC2FLV_SOURCES=dtsc2flv.cpp
|
||||||
MistFLV2DTSC_SOURCES=flv2dtsc.cpp
|
MistFLV2DTSC_SOURCES=flv2dtsc.cpp
|
||||||
|
MistDTSCFix_SOURCES=dtscfix.cpp
|
||||||
|
|
117
src/converters/dtscfix.cpp
Normal file
117
src/converters/dtscfix.cpp
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
/// \file dtscfix.cpp
|
||||||
|
/// Contains the code that will attempt to fix the metadata contained in an DTSC file.
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <mist/dtsc.h>
|
||||||
|
#include <mist/json.h>
|
||||||
|
#include <mist/config.h>
|
||||||
|
|
||||||
|
/// Holds all code that converts filetypes to/from to DTSC.
|
||||||
|
namespace Converters{
|
||||||
|
|
||||||
|
/// Reads an DTSC file and attempts to fix the metadata in it.
|
||||||
|
int DTSCFix(Util::Config & conf) {
|
||||||
|
DTSC::File F(conf.getString("filename"));
|
||||||
|
std::string loader = F.getHeader();
|
||||||
|
JSON::Value meta = JSON::fromDTMI(loader);
|
||||||
|
JSON::Value pack;
|
||||||
|
|
||||||
|
long long unsigned int firstpack = 0;
|
||||||
|
long long unsigned int nowpack = 0;
|
||||||
|
long long unsigned int lastaudio = 0;
|
||||||
|
long long unsigned int lastvideo = 0;
|
||||||
|
long long unsigned int lastkey = 0;
|
||||||
|
long long unsigned int totalvideo = 0;
|
||||||
|
long long unsigned int totalaudio = 0;
|
||||||
|
long long unsigned int keyframes = 0;
|
||||||
|
long long unsigned int key_min = 0xffffffff;
|
||||||
|
long long unsigned int key_max = 0;
|
||||||
|
long long unsigned int vid_min = 0xffffffff;
|
||||||
|
long long unsigned int vid_max = 0;
|
||||||
|
long long unsigned int aud_min = 0xffffffff;
|
||||||
|
long long unsigned int aud_max = 0;
|
||||||
|
long long unsigned int bfrm_min = 0xffffffff;
|
||||||
|
long long unsigned int bfrm_max = 0;
|
||||||
|
long long unsigned int bps = 0;
|
||||||
|
|
||||||
|
while (loader.size() != 0){
|
||||||
|
loader = F.getPacket();
|
||||||
|
if (loader.size() != 0){
|
||||||
|
pack = JSON::fromDTMI(loader);
|
||||||
|
nowpack = pack["time"].asInt();
|
||||||
|
if (firstpack == 0){firstpack = nowpack;}
|
||||||
|
if (pack["datatype"].asString() == "audio"){
|
||||||
|
if (lastaudio != 0 && (nowpack - lastaudio) != 0){
|
||||||
|
bps = pack["data"].asString().size() / (nowpack - lastaudio);
|
||||||
|
if (bps < aud_min){aud_min = bps;}
|
||||||
|
if (bps > aud_max){aud_max = bps;}
|
||||||
|
}
|
||||||
|
totalaudio += pack["data"].asString().size();
|
||||||
|
lastaudio = nowpack;
|
||||||
|
}
|
||||||
|
if (pack["datatype"].asString() == "video"){
|
||||||
|
if (lastvideo != 0 && (nowpack - lastvideo) != 0){
|
||||||
|
bps = pack["data"].asString().size() / (nowpack - lastvideo);
|
||||||
|
if (bps < vid_min){vid_min = bps;}
|
||||||
|
if (bps > vid_max){vid_max = bps;}
|
||||||
|
}
|
||||||
|
if (pack["keyframe"].asInt() != 0){
|
||||||
|
if (lastkey != 0){
|
||||||
|
bps = nowpack - lastkey;
|
||||||
|
if (bps < key_min){key_min = bps;}
|
||||||
|
if (bps > key_max){key_max = bps;}
|
||||||
|
}
|
||||||
|
keyframes++;
|
||||||
|
lastkey = nowpack;
|
||||||
|
}
|
||||||
|
if (pack["offset"].asInt() != 0){
|
||||||
|
bps = pack["offset"].asInt();
|
||||||
|
if (bps < bfrm_min){bfrm_min = bps;}
|
||||||
|
if (bps > bfrm_max){bfrm_max = bps;}
|
||||||
|
}
|
||||||
|
totalvideo += pack["data"].asString().size();
|
||||||
|
lastvideo = nowpack;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl << "Summary:" << std::endl;
|
||||||
|
meta["length"] = (long long int)((nowpack - firstpack)/1000);
|
||||||
|
if (meta.isMember("audio")){
|
||||||
|
meta["audio"]["bps"] = (long long int)(totalaudio / ((lastaudio - firstpack) / 1000));
|
||||||
|
std::cout << " Audio: " << meta["audio"]["codec"].asString() << std::endl;
|
||||||
|
std::cout << " Bitrate: " << meta["audio"]["bps"].asInt() << std::endl;
|
||||||
|
}
|
||||||
|
if (meta.isMember("video")){
|
||||||
|
meta["video"]["bps"] = (long long int)(totalvideo / ((lastvideo - firstpack) / 1000));
|
||||||
|
meta["video"]["keyms"] = (long long int)((lastvideo - firstpack) / keyframes);
|
||||||
|
if (meta["video"]["keyms"].asInt() - key_min > key_max - meta["video"]["keyms"].asInt()){
|
||||||
|
meta["video"]["keyvar"] = (long long int)(meta["video"]["keyms"].asInt() - key_min);
|
||||||
|
}else{
|
||||||
|
meta["video"]["keyvar"] = (long long int)(key_max - meta["video"]["keyms"].asInt());
|
||||||
|
}
|
||||||
|
std::cout << " Video: " << meta["video"]["codec"].asString() << std::endl;
|
||||||
|
std::cout << " Bitrate: " << meta["video"]["bps"].asInt() << std::endl;
|
||||||
|
std::cout << " Keyframes: " << meta["video"]["keyms"].asInt() << "~" << meta["video"]["keyvar"].asInt() << std::endl;
|
||||||
|
std::cout << " B-frames: " << bfrm_min << " - " << bfrm_max << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cerr << "Re-writing header..." << std::endl;
|
||||||
|
|
||||||
|
loader = meta.toPacked();
|
||||||
|
if (F.writeHeader(loader)){
|
||||||
|
return 0;
|
||||||
|
}else{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}//DTSCFix
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Entry point for FLV2DTSC, simply calls Converters::FLV2DTSC().
|
||||||
|
int main(int argc, char ** argv){
|
||||||
|
Util::Config conf = Util::Config(argv[0], PACKAGE_VERSION);
|
||||||
|
conf.addOption("filename", JSON::fromString("{\"arg_num\":1, \"arg\":\"string\", \"help\":\"Filename of the file to attempt to fix.\"}"));
|
||||||
|
conf.parseArgs(argc, argv);
|
||||||
|
return Converters::DTSCFix(conf);
|
||||||
|
}//main
|
|
@ -10,9 +10,10 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <mist/flv_tag.h> //FLV support
|
#include <mist/flv_tag.h>
|
||||||
#include <mist/dtsc.h> //DTSC support
|
#include <mist/dtsc.h>
|
||||||
#include <mist/amf.h> //AMF support
|
#include <mist/json.h>
|
||||||
|
#include <mist/amf.h>
|
||||||
#include <mist/config.h>
|
#include <mist/config.h>
|
||||||
|
|
||||||
/// Holds all code that converts filetypes to/from to DTSC.
|
/// Holds all code that converts filetypes to/from to DTSC.
|
||||||
|
@ -21,41 +22,46 @@ namespace Converters{
|
||||||
/// Reads FLV from STDIN, outputs DTSC to STDOUT.
|
/// Reads FLV from STDIN, outputs DTSC to STDOUT.
|
||||||
int FLV2DTSC() {
|
int FLV2DTSC() {
|
||||||
FLV::Tag FLV_in; // Temporary storage for incoming FLV data.
|
FLV::Tag FLV_in; // Temporary storage for incoming FLV data.
|
||||||
DTSC::DTMI meta_out; // Storage for outgoing DTMI header data.
|
JSON::Value meta_out; // Storage for outgoing header data.
|
||||||
DTSC::DTMI pack_out; // Storage for outgoing DTMI data.
|
JSON::Value pack_out; // Storage for outgoing data.
|
||||||
std::stringstream prebuffer; // Temporary buffer before sending real data
|
std::stringstream prebuffer; // Temporary buffer before sending real data
|
||||||
bool sending = false;
|
bool sending = false;
|
||||||
unsigned int counter = 0;
|
unsigned int counter = 0;
|
||||||
|
|
||||||
while (!feof(stdin)){
|
while (!feof(stdin)){
|
||||||
if (FLV_in.FileLoader(stdin)){
|
if (FLV_in.FileLoader(stdin)){
|
||||||
pack_out = FLV_in.toDTSC(meta_out);
|
pack_out = FLV_in.toJSON(meta_out);
|
||||||
if (pack_out.isEmpty()){continue;}
|
if (pack_out.isNull()){continue;}
|
||||||
if (!sending){
|
if (!sending){
|
||||||
counter++;
|
counter++;
|
||||||
if (counter > 8){
|
if (counter > 8){
|
||||||
sending = true;
|
sending = true;
|
||||||
meta_out.Pack(true);
|
std::string packed_header = meta_out.toPacked();
|
||||||
meta_out.packed.replace(0, 4, DTSC::Magic_Header);
|
unsigned int size = htonl(packed_header.size());
|
||||||
std::cout << meta_out.packed;
|
std::cout << std::string(DTSC::Magic_Header, 4) << std::string((char*)&size, 4) << packed_header;
|
||||||
std::cout << prebuffer.rdbuf();
|
std::cout << prebuffer.rdbuf();
|
||||||
prebuffer.str("");
|
prebuffer.str("");
|
||||||
std::cerr << "Buffer done, starting real-time output..." << std::endl;
|
std::cerr << "Buffer done, starting real-time output..." << std::endl;
|
||||||
}else{
|
}else{
|
||||||
prebuffer << pack_out.Pack(true);//buffer
|
std::string packed_out = pack_out.toPacked();
|
||||||
|
unsigned int size = htonl(packed_out.size());
|
||||||
|
prebuffer << std::string(DTSC::Magic_Packet, 4) << std::string((char*)&size, 4) << packed_out;
|
||||||
continue;//don't also write
|
continue;//don't also write
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << pack_out.Pack(true);//simply write
|
//simply write
|
||||||
|
std::string packed_out = pack_out.toPacked();
|
||||||
|
unsigned int size = htonl(packed_out.size());
|
||||||
|
std::cout << std::string(DTSC::Magic_Packet, 4) << std::string((char*)&size, 4) << packed_out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the FLV input is very short, do output it correctly...
|
// if the FLV input is very short, do output it correctly...
|
||||||
if (!sending){
|
if (!sending){
|
||||||
std::cerr << "EOF - outputting buffer..." << std::endl;
|
std::cerr << "EOF - outputting buffer..." << std::endl;
|
||||||
meta_out.Pack(true);
|
std::string packed_header = meta_out.toPacked();
|
||||||
meta_out.packed.replace(0, 4, DTSC::Magic_Header);
|
unsigned int size = htonl(packed_header.size());
|
||||||
std::cout << meta_out.packed;
|
std::cout << std::string(DTSC::Magic_Header, 4) << std::string((char*)&size, 4) << packed_header;
|
||||||
std::cout << prebuffer.rdbuf();
|
std::cout << prebuffer.rdbuf();
|
||||||
}
|
}
|
||||||
std::cerr << "Done!" << std::endl;
|
std::cerr << "Done!" << std::endl;
|
||||||
|
|
Loading…
Add table
Reference in a new issue