Fix local file targets when pushing to non-full-path

This commit is contained in:
Thulinma 2023-04-28 23:46:51 +02:00
parent 53d14376c3
commit d7ec340d7e
4 changed files with 46 additions and 38 deletions

View file

@ -8,6 +8,7 @@
#include "timing.h" #include "timing.h"
#include "util.h" #include "util.h"
#include "url.h" #include "url.h"
#include "urireader.h"
#include <errno.h> // errno, ENOENT, EEXIST #include <errno.h> // errno, ENOENT, EEXIST
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
@ -215,7 +216,7 @@ namespace Util{
/// \param outFile file descriptor which will be used to send data /// \param outFile file descriptor which will be used to send data
/// \param append whether to open this connection in truncate or append mode /// \param append whether to open this connection in truncate or append mode
bool externalWriter(const std::string & uri, int &outFile, bool append){ bool externalWriter(const std::string & uri, int &outFile, bool append){
HTTP::URL target(uri); HTTP::URL target = HTTP::localURIResolver().link(uri);
// If it is a remote target, we might need to spawn an external binary // If it is a remote target, we might need to spawn an external binary
if (!target.isLocalPath()){ if (!target.isLocalPath()){
bool matchedProtocol = false; bool matchedProtocol = false;

View file

@ -227,23 +227,28 @@ namespace Mist{
lastStats = 0; lastStats = 0;
} }
void Input::checkHeaderTimes(std::string streamFile){ void Input::checkHeaderTimes(const HTTP::URL & streamFile){
/// \TODO Implement remote URLs?
if (streamFile.isLocalPath()){
const std::string & f = streamFile.getFilePath();
struct stat bufStream; struct stat bufStream;
struct stat bufHeader; struct stat bufHeader;
struct stat srtStream; struct stat srtStream;
std::string srtFile = f + ".srt";
std::string srtFile = streamFile + ".srt";
if (stat(srtFile.c_str(), &srtStream) == 0){ if (stat(srtFile.c_str(), &srtStream) == 0){
hasSrt = true; hasSrt = true;
srtSource.open(srtFile.c_str()); srtSource.open(srtFile.c_str());
INFO_MSG("File %s opened as srt source", srtFile.c_str()); INFO_MSG("File %s opened as srt source", srtFile.c_str());
} }
if (stat(streamFile.c_str(), &bufStream) != 0){ if (stat(f.c_str(), &bufStream) != 0){
INSANE_MSG("Source is not a file - ignoring header check"); INSANE_MSG("Source is not a file - ignoring header check");
return; return;
} }
std::string headerFile = streamFile + ".dtsh"; std::string headerFile = f + ".dtsh";
if (stat(headerFile.c_str(), &bufHeader) != 0){ if (stat(headerFile.c_str(), &bufHeader) != 0){
INSANE_MSG("No header exists to compare - ignoring header check"); INSANE_MSG("No header exists to compare - ignoring header check");
return; return;
@ -261,6 +266,8 @@ namespace Mist{
} }
} }
}
void Input::readSrtHeader(){ void Input::readSrtHeader(){
if (!hasSrt){return;} if (!hasSrt){return;}
if (!srtSource.good()){return;} if (!srtSource.good()){return;}
@ -586,7 +593,7 @@ namespace Mist{
int Input::run(){ int Input::run(){
Comms::sessionConfigCache(); Comms::sessionConfigCache();
if (streamStatus){streamStatus.mapped[0] = STRMSTAT_BOOT;} if (streamStatus){streamStatus.mapped[0] = STRMSTAT_BOOT;}
checkHeaderTimes(config->getString("input")); checkHeaderTimes(HTTP::localURIResolver().link(config->getString("input")));
//needHeader internally calls readExistingHeader which in turn attempts to read header cache //needHeader internally calls readExistingHeader which in turn attempts to read header cache
if (needHeader()){ if (needHeader()){
uint64_t timer = Util::getMicros(); uint64_t timer = Util::getMicros();
@ -597,7 +604,7 @@ namespace Mist{
timer = Util::getMicros(timer); timer = Util::getMicros(timer);
INFO_MSG("Created header in %.3f ms (%zu tracks)", (double)timer/1000.0, M?M.trackCount():(size_t)0); INFO_MSG("Created header in %.3f ms (%zu tracks)", (double)timer/1000.0, M?M.trackCount():(size_t)0);
//Write header to file for caching purposes //Write header to file for caching purposes
M.toFile(config->getString("input") + ".dtsh"); M.toFile(HTTP::localURIResolver().link(config->getString("input") + ".dtsh").getUrl());
} }
postHeader(); postHeader();
if (config->getBool("headeronly")){return 0;} if (config->getBool("headeronly")){return 0;}

View file

@ -9,6 +9,7 @@
#include <mist/json.h> #include <mist/json.h>
#include <mist/shared_memory.h> #include <mist/shared_memory.h>
#include <mist/timing.h> #include <mist/timing.h>
#include <mist/url.h>
#include <set> #include <set>
#include "../io.h" #include "../io.h"
@ -73,7 +74,7 @@ namespace Mist{
virtual bool openStreamSource(){return readHeader();} virtual bool openStreamSource(){return readHeader();}
virtual void closeStreamSource(){} virtual void closeStreamSource(){}
virtual void parseStreamHeader(){} virtual void parseStreamHeader(){}
void checkHeaderTimes(std::string streamFile); void checkHeaderTimes(const HTTP::URL & streamFile);
virtual void removeUnused(); virtual void removeUnused();
virtual void convert(); virtual void convert();
virtual void serve(); virtual void serve();

View file

@ -1417,7 +1417,7 @@ namespace Mist{
if (!plsConn){ if (!plsConn){
std::string plsRel = targetParams["m3u8"]; std::string plsRel = targetParams["m3u8"];
Util::streamVariables(plsRel, streamName); Util::streamVariables(plsRel, streamName);
playlistLocation = HTTP::URL(config->getString("target")).link(plsRel); playlistLocation = HTTP::localURIResolver().link(config->getString("target")).link(plsRel);
if (playlistLocation.isLocalPath()){ if (playlistLocation.isLocalPath()){
playlistLocationString = playlistLocation.getFilePath(); playlistLocationString = playlistLocation.getFilePath();
INFO_MSG("Segmenting to local playlist '%s'", playlistLocationString.c_str()); INFO_MSG("Segmenting to local playlist '%s'", playlistLocationString.c_str());
@ -1551,7 +1551,6 @@ namespace Mist{
Util::replace(newTarget, "$segmentCounter", JSON::Value(segmentCount).asString()); Util::replace(newTarget, "$segmentCounter", JSON::Value(segmentCount).asString());
Util::streamVariables(newTarget, streamName); Util::streamVariables(newTarget, streamName);
currentTarget = newTarget; currentTarget = newTarget;
config->getOption("target", true).append(currentTarget);
if (newTarget == "-"){ if (newTarget == "-"){
INFO_MSG("Outputting %s to stdout with %s format", streamName.c_str(), INFO_MSG("Outputting %s to stdout with %s format", streamName.c_str(),
capa["name"].asString().c_str()); capa["name"].asString().c_str());
@ -1668,7 +1667,7 @@ namespace Mist{
Util::logExitReason("Lost connection to playlist file `%s` during segmenting", playlistLocationString.c_str()); Util::logExitReason("Lost connection to playlist file `%s` during segmenting", playlistLocationString.c_str());
break; break;
} }
std::string segment = HTTP::URL(currentTarget).getLinkFrom(playlistLocation); std::string segment = HTTP::localURIResolver().link(currentTarget).getLinkFrom(playlistLocation);
if (M.getLive()){ if (M.getLive()){
uint64_t unixMs = M.getBootMsOffset() + systemBoot + currentStartTime; uint64_t unixMs = M.getBootMsOffset() + systemBoot + currentStartTime;
playlistBuffer += "#EXT-X-PROGRAM-DATE-TIME:" + Util::getUTCStringMillis(unixMs) + "\n"; playlistBuffer += "#EXT-X-PROGRAM-DATE-TIME:" + Util::getUTCStringMillis(unixMs) + "\n";
@ -1800,7 +1799,7 @@ namespace Mist{
// If this is a non-live source, we can finally open up the connection to the playlist file // If this is a non-live source, we can finally open up the connection to the playlist file
if (!M.getLive()){connectToFile(playlistLocationString, false, &plsConn);} if (!M.getLive()){connectToFile(playlistLocationString, false, &plsConn);}
if (plsConn){ if (plsConn){
std::string segment = HTTP::URL(currentTarget).getLinkFrom(playlistLocation); std::string segment = HTTP::localURIResolver().link(currentTarget).getLinkFrom(playlistLocation);
if (M.getLive()){ if (M.getLive()){
uint64_t unixMs = M.getBootMsOffset() + systemBoot + currentStartTime; uint64_t unixMs = M.getBootMsOffset() + systemBoot + currentStartTime;
playlistBuffer += "#EXT-X-PROGRAM-DATE-TIME:" + Util::getUTCStringMillis(unixMs) + "\n"; playlistBuffer += "#EXT-X-PROGRAM-DATE-TIME:" + Util::getUTCStringMillis(unixMs) + "\n";
@ -2296,7 +2295,7 @@ namespace Mist{
bool Output::connectToFile(std::string file, bool append, Socket::Connection *conn){ bool Output::connectToFile(std::string file, bool append, Socket::Connection *conn){
int outFile = -1; int outFile = -1;
if (!conn) {conn = &myConn;} if (!conn) {conn = &myConn;}
bool isFileTarget = HTTP::URL(file).isLocalPath(); bool isFileTarget = HTTP::localURIResolver().link(file).isLocalPath();
if (!Util::externalWriter(file, outFile, append)){return false;} if (!Util::externalWriter(file, outFile, append)){return false;}
if (*conn && isFileTarget) { if (*conn && isFileTarget) {
flock(conn->getSocket(), LOCK_UN | LOCK_NB); flock(conn->getSocket(), LOCK_UN | LOCK_NB);