From 1451b64e000df5b6b46adf415bad62c75899bf7d Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 4 Jul 2018 11:19:02 +0200 Subject: [PATCH] Improved track selection logic for JSON output --- src/output/output.cpp | 56 +++++++++++++++++++++----------------- src/output/output_json.cpp | 16 +---------- src/output/output_raw.cpp | 2 +- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/src/output/output.cpp b/src/output/output.cpp index b988cd78..87bdd79b 100644 --- a/src/output/output.cpp +++ b/src/output/output.cpp @@ -277,28 +277,22 @@ namespace Mist{ unsigned int bestSoFarCount = 0; unsigned int index = 0; jsonForEach(capa["codecs"], it){ - unsigned int genCounter = 0; unsigned int selCounter = 0; if ((*it).size() > 0){ jsonForEach((*it), itb){ if ((*itb).size() > 0){ - bool found = false; jsonForEach(*itb, itc){ + const std::string & strRef = (*itc).asStringRef(); + bool byType = false; + bool multiSel = false; + uint8_t shift = 0; + if (strRef[shift] == '@'){byType = true; ++shift;} + if (strRef[shift] == '+'){multiSel = true; ++shift;} for (std::set::iterator itd = selectedTracks.begin(); itd != selectedTracks.end(); itd++){ - if (myMeta.tracks[*itd].codec == (*itc).asStringRef()){ + if ((!byType && myMeta.tracks[*itd].codec == strRef.substr(shift)) || (byType && myMeta.tracks[*itd].type == strRef.substr(shift)) || strRef.substr(shift) == "*"){ selCounter++; - found = true; - break; - } - } - } - if (!found){ - jsonForEach(*itb, itc){ - for (std::map::iterator trit = myMeta.tracks.begin(); trit != myMeta.tracks.end(); trit++){ - if (trit->second.codec == (*itc).asStringRef() || (*itc).asStringRef() == "*"){ - genCounter++; - found = true; - if ((*itc).asStringRef() != "*"){break;} + if (!multiSel){ + break; } } } @@ -306,10 +300,10 @@ namespace Mist{ } } if (selCounter == selectedTracks.size()){ - if (selCounter + genCounter > bestSoFarCount){ - bestSoFarCount = selCounter + genCounter; + if (selCounter > bestSoFarCount){ + bestSoFarCount = selCounter; bestSoFar = index; - HIGH_MSG("Match (%u/%u): %s", selCounter, selCounter + genCounter, (*it).toString().c_str()); + HIGH_MSG("Matched %u: %s", selCounter, (*it).toString().c_str()); } }else{ VERYHIGH_MSG("Not a match for currently selected tracks: %s", (*it).toString().c_str()); @@ -324,31 +318,43 @@ namespace Mist{ jsonForEach(capa["codecs"][bestSoFar], itb){ if ((*itb).size() && myMeta.tracks.size()){ bool found = false; + bool multiFind = false; jsonForEach((*itb), itc){ + const std::string & strRef = (*itc).asStringRef(); + bool byType = false; + uint8_t shift = 0; + if (strRef[shift] == '@'){byType = true; ++shift;} + if (strRef[shift] == '+'){multiFind = true; ++shift;} for (std::set::iterator itd = selectedTracks.begin(); itd != selectedTracks.end(); itd++){ - if (myMeta.tracks[*itd].codec == (*itc).asStringRef()){ + if ((!byType && myMeta.tracks[*itd].codec == strRef.substr(shift)) || (byType && myMeta.tracks[*itd].type == strRef.substr(shift)) || strRef.substr(shift) == "*"){ found = true; break; } } } - if (!found){ + if (!found || multiFind){ jsonForEach((*itb), itc){ - if (found){break;} + const std::string & strRef = (*itc).asStringRef(); + bool byType = false; + bool multiSel = false; + uint8_t shift = 0; + if (strRef[shift] == '@'){byType = true; ++shift;} + if (strRef[shift] == '+'){multiSel = true; ++shift;} + if (found && !multiSel){continue;} if (myMeta.live){ for (std::map::reverse_iterator trit = myMeta.tracks.rbegin(); trit != myMeta.tracks.rend(); trit++){ - if (trit->second.codec == (*itc).asStringRef() || (*itc).asStringRef() == "*"){ + if ((!byType && trit->second.codec == strRef.substr(shift)) || (byType && trit->second.type == strRef.substr(shift)) || strRef.substr(shift) == "*"){ selectedTracks.insert(trit->first); found = true; - if ((*itc).asStringRef() != "*"){break;} + if (!multiSel){break;} } } }else{ for (std::map::iterator trit = myMeta.tracks.begin(); trit != myMeta.tracks.end(); trit++){ - if (trit->second.codec == (*itc).asStringRef() || (*itc).asStringRef() == "*"){ + if ((!byType && trit->second.codec == strRef.substr(shift)) || (byType && trit->second.type == strRef.substr(shift)) || strRef.substr(shift) == "*"){ selectedTracks.insert(trit->first); found = true; - if ((*itc).asStringRef() != "*"){break;} + if (!multiSel){break;} } } } diff --git a/src/output/output_json.cpp b/src/output/output_json.cpp index b2fa08f6..1add0a8c 100644 --- a/src/output/output_json.cpp +++ b/src/output/output_json.cpp @@ -18,8 +18,7 @@ namespace Mist { capa["name"] = "JSON"; capa["desc"] = "Enables HTTP protocol JSON streaming."; capa["url_match"] = "/$.json"; - capa["codecs"][0u][0u].append("srt"); - capa["codecs"][0u][0u].append("TTXT"); + capa["codecs"][0u][0u].append("@+meta"); capa["methods"][0u]["handler"] = "http"; capa["methods"][0u]["type"] = "html5/text/javascript"; capa["methods"][0u]["priority"] = 0ll; @@ -74,10 +73,6 @@ namespace Mist { jsonp = ""; if (H.GetVar("callback") != ""){jsonp = H.GetVar("callback");} if (H.GetVar("jsonp") != ""){jsonp = H.GetVar("jsonp");} - if (H.GetVar("track") != ""){ - selectedTracks.clear(); - selectedTracks.insert(JSON::Value(H.GetVar("track")).asInt()); - } if (H.GetHeader("Upgrade") == "websocket"){ ws = new HTTP::Websocket(myConn, H); @@ -101,16 +96,7 @@ namespace Mist { H.Clean(); return; } - first = true; - initialize(); - if (!selectedTracks.size()){ - for (std::map::iterator it = myMeta.tracks.begin(); it != myMeta.tracks.end(); it++){ - if (it->second.type == "meta" ){ - selectedTracks.insert(it->first); - } - } - } parseData = true; wantRequest = false; } diff --git a/src/output/output_raw.cpp b/src/output/output_raw.cpp index 571cf29b..2d98b3b3 100644 --- a/src/output/output_raw.cpp +++ b/src/output/output_raw.cpp @@ -46,7 +46,7 @@ namespace Mist { capa["optional"]["seek"]["help"] = "The time in milliseconds to seek to, 0 by default."; capa["optional"]["seek"]["type"] = "int"; capa["optional"]["seek"]["option"] = "--seek"; - capa["codecs"][0u][0u].append("*"); + capa["codecs"][0u][0u].append("+*"); cfg->addOption("streamname", JSON::fromString("{\"arg\":\"string\",\"short\":\"s\",\"long\":\"stream\",\"help\":\"The name of the stream that this connector will transmit.\"}")); cfg->addOption("tracks",