Rewrite of Comms

This commit is contained in:
Thulinma 2020-10-30 22:38:29 +01:00
parent c6265f6659
commit 7297336e46
14 changed files with 173 additions and 303 deletions

View file

@ -490,10 +490,7 @@ void Controller::SharedMemStats(void *config){
}else{/*LTS-START*/
if (Controller::killOnExit){
WARN_MSG("Killing all connected clients to force full shutdown");
for (uint32_t id = statComm.firstValid(); id != statComm.endValid(); id++){
if (statComm.getStatus(id) == COMM_STATUS_INVALID){continue;}
statComm.kill(id, true);
}
statComm.finishAll();
}
/*LTS-END*/
}

View file

@ -857,15 +857,14 @@ namespace Mist{
tid = thisPacket.getTrackId();
idx = M.trackIDToIndex(tid, getpid());
if (thisPacket && !userSelect.count(idx)){
userSelect[idx].reload(streamName, idx, COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
userSelect[idx].reload(streamName, idx, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
}
while (thisPacket && config->is_active && userSelect[idx].isAlive()){
while (thisPacket && config->is_active && userSelect[idx]){
if (userSelect[idx].getStatus() == COMM_STATUS_REQDISCONNECT){
Util::logExitReason("buffer requested shutdown");
break;
}
bufferLivePacket(thisPacket);
userSelect[idx].keepAlive();
getNext();
if (!thisPacket){
Util::logExitReason("invalid packet from getNext");
@ -874,14 +873,14 @@ namespace Mist{
tid = thisPacket.getTrackId();
idx = M.trackIDToIndex(tid, getpid());
if (thisPacket && !userSelect.count(idx)){
userSelect[idx].reload(streamName, idx, COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
userSelect[idx].reload(streamName, idx, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
}
if (Util::bootSecs() - statTimer > 1){
// Connect to stats for INPUT detection
if (!statComm){statComm.reload();}
if (statComm){
if (!statComm.isAlive()){
if (!statComm){
config->is_active = false;
Util::logExitReason("received shutdown request from controller");
return;
@ -896,7 +895,6 @@ namespace Mist{
statComm.setTime(now - startTime);
statComm.setLastSecond(0);
statComm.setHost(getConnectedBinHost());
statComm.keepAlive();
}
statTimer = Util::bootSecs();
@ -911,33 +909,31 @@ namespace Mist{
getNext();
if (thisPacket && !userSelect.count(thisPacket.getTrackId())){
size_t tid = thisPacket.getTrackId();
userSelect[tid].reload(streamName, tid, COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
userSelect[tid].reload(streamName, tid, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
}
while (thisPacket && config->is_active && userSelect[thisPacket.getTrackId()].isAlive()){
while (thisPacket && config->is_active && userSelect[thisPacket.getTrackId()]){
thisPacket.nullMember("bpos");
while (config->is_active && userSelect[thisPacket.getTrackId()].isAlive() &&
while (config->is_active && userSelect[thisPacket.getTrackId()] &&
Util::bootMS() + SIMULATED_LIVE_BUFFER < (thisPacket.getTime() + timeOffset) + simStartTime){
Util::sleep(std::min(((thisPacket.getTime() + timeOffset) + simStartTime) - (Util::getMS() + SIMULATED_LIVE_BUFFER),
(uint64_t)1000));
userSelect[thisPacket.getTrackId()].keepAlive();
}
uint64_t originalTime = thisPacket.getTime();
thisPacket.setTime(originalTime + timeOffset);
bufferLivePacket(thisPacket);
thisPacket.setTime(originalTime);
userSelect[thisPacket.getTrackId()].keepAlive();
getNext();
if (thisPacket && !userSelect.count(thisPacket.getTrackId())){
size_t tid = thisPacket.getTrackId();
userSelect[tid].reload(streamName, tid, COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
userSelect[tid].reload(streamName, tid, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
}
if (Util::bootSecs() - statTimer > 1){
// Connect to stats for INPUT detection
if (!statComm){statComm.reload();}
if (statComm){
if (!statComm.isAlive()){
if (statComm.getStatus() == COMM_STATUS_REQDISCONNECT){
config->is_active = false;
Util::logExitReason("received shutdown request from controller");
return;
@ -952,7 +948,6 @@ namespace Mist{
statComm.setTime(now - startTime);
statComm.setLastSecond(0);
statComm.setHost(getConnectedBinHost());
statComm.keepAlive();
}
statTimer = Util::bootSecs();
@ -961,7 +956,7 @@ namespace Mist{
if (!thisPacket){
Util::logExitReason("invalid packet from getNext");
}
if (thisPacket && !userSelect[thisPacket.getTrackId()].isAlive()){
if (thisPacket && !userSelect[thisPacket.getTrackId()]){
Util::logExitReason("buffer shutdown");
}
}
@ -1411,8 +1406,7 @@ namespace Mist{
bool isAlive = false;
for (std::map<size_t, Comms::Users>::iterator it = userSelect.begin(); it != userSelect.end(); it++){
if (it->second.isAlive()){isAlive = true;}
it->second.keepAlive();
if (it->second){isAlive = true;}
}
return isAlive && config->is_active;
}

View file

@ -155,9 +155,7 @@ namespace Mist{
{
Comms::Users cleanUsers;
cleanUsers.reload(streamName);
for (size_t i = cleanUsers.firstValid(); i < cleanUsers.endValid(); ++i){
cleanUsers.setStatus(COMM_STATUS_INVALID, i);
}
cleanUsers.finishAll();
cleanUsers.setMaster(true);
}
// Delete the live stream semaphore, if any.
@ -389,8 +387,8 @@ namespace Mist{
}
void inputBuffer::removeTrack(size_t tid){
size_t lastUser = users.endValid();
for (size_t i = users.firstValid(); i < lastUser; ++i){
size_t lastUser = users.recordCount();
for (size_t i = 0; i < lastUser; ++i){
if (users.getStatus(i) == COMM_STATUS_INVALID){continue;}
if (!(users.getStatus(i) & COMM_STATUS_SOURCE)){continue;}
if (users.getTrack(i) != tid){continue;}

View file

@ -169,7 +169,7 @@ namespace Mist{
std::set<size_t> validTracks = M.getMySourceTracks(getpid());
userSelect.clear();
for (std::set<size_t>::iterator it = validTracks.begin(); it != validTracks.end(); ++it){
userSelect[*it].reload(streamName, *it, COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
userSelect[*it].reload(streamName, *it, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
}
break;
}

View file

@ -212,7 +212,7 @@ namespace Mist{
// Connect to stats for INPUT detection
statComm.reload();
if (statComm){
if (!statComm.isAlive()){
if (statComm.getStatus() == COMM_STATUS_REQDISCONNECT){
config->is_active = false;
Util::logExitReason("received shutdown request from controller");
return;
@ -227,7 +227,6 @@ namespace Mist{
statComm.setTime(now - startTime);
statComm.setLastSecond(0);
statComm.setHost(getConnectedBinHost());
statComm.keepAlive();
}
}
}
@ -402,7 +401,7 @@ namespace Mist{
}else{
if (!userSelect.count(idx)){
WARN_MSG("Reloading track %zu, index %zu", pkt.getTrackId(), idx);
userSelect[idx].reload(streamName, idx, COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
userSelect[idx].reload(streamName, idx, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
}
if (userSelect[idx].getStatus() == COMM_STATUS_REQDISCONNECT){
Util::logExitReason("buffer requested shutdown");

View file

@ -58,8 +58,7 @@ void parseThread(void *mistIn){
threadTimer[tid] = Util::bootSecs();
}
while (Util::bootSecs() - threadTimer[tid] < THREAD_TIMEOUT && cfgPointer->is_active &&
(!dataTrack || (userConn ? userConn.isAlive() : true))){
if (dataTrack){userConn.keepAlive();}
(!dataTrack || (userConn ? userConn : true))){
liveStream.parse(tid);
if (!liveStream.hasPacket(tid)){
Util::sleep(100);
@ -95,7 +94,7 @@ void parseThread(void *mistIn){
idx = meta.trackIDToIndex(tid, getpid());
if (idx != INVALID_TRACK_ID){
//Successfully assigned a track index! Inform the buffer we're pushing
userConn.reload(globalStreamName, idx, COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
userConn.reload(globalStreamName, idx, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE | COMM_STATUS_DONOTTRACK);
}
//Any kind of failure? Retry later.
if (idx == INVALID_TRACK_ID || !meta.trackValid(idx)){
@ -137,7 +136,7 @@ void parseThread(void *mistIn){
std::string reason = "unknown reason";
if (!(Util::bootSecs() - threadTimer[tid] < THREAD_TIMEOUT)){reason = "thread timeout";}
if (!cfgPointer->is_active){reason = "input shutting down";}
if (!(!liveStream.isDataTrack(tid) || userConn.isAlive())){
if (!(!liveStream.isDataTrack(tid) || userConn)){
reason = "buffer disconnect";
cfgPointer->is_active = false;
}
@ -588,7 +587,7 @@ namespace Mist{
// Connect to stats for INPUT detection
statComm.reload();
if (statComm){
if (!statComm.isAlive()){
if (statComm.getStatus() == COMM_STATUS_REQDISCONNECT){
config->is_active = false;
Util::logExitReason("received shutdown request from controller");
return;
@ -603,7 +602,6 @@ namespace Mist{
statComm.setTime(now - startTime);
statComm.setLastSecond(0);
statComm.setHost(getConnectedBinHost());
statComm.keepAlive();
}
std::set<size_t> activeTracks = liveStream.getActiveTracks();

View file

@ -1605,7 +1605,7 @@ namespace Mist{
}
emptyCount = 0; // valid packet - reset empty counter
if (!userSelect[nxt.tid].isAlive()){
if (!userSelect[nxt.tid]){
INFO_MSG("Track %zu is not alive!", nxt.tid);
return false;
}
@ -1691,7 +1691,6 @@ namespace Mist{
statComm.setTime(now - myConn.connTime());
statComm.setLastSecond(thisPacket ? thisPacket.getTime() : 0);
statComm.setPid(getpid());
statComm.keepAlive();
/*LTS-START*/
// Tag the session with the user agent
@ -1710,21 +1709,16 @@ namespace Mist{
if (isPushing()){
for (std::map<size_t, Comms::Users>::iterator it = userSelect.begin(); it != userSelect.end(); it++){
it->second.keepAlive();
if (it->second.getStatus() == COMM_STATUS_REQDISCONNECT){
if (dropPushTrack(it->second.getTrack(), "disconnect request from buffer")){break;}
}
if (!it->second.isAlive()){
if (!it->second){
if (dropPushTrack(it->second.getTrack(), "track mapping no longer valid")){break;}
}
//if (Util::bootSecs() - M.getLastUpdated(it->first) > 5){
// if (dropPushTrack(it->second.getTrack(), "track updates being ignored by buffer")){break;}
//}
}
}else{
for (std::map<size_t, Comms::Users>::iterator it = userSelect.begin(); it != userSelect.end(); it++){
it->second.keepAlive();
}
}
}

View file

@ -811,12 +811,7 @@ namespace Mist{
}
Util::wait(100);
//Make sure we don't accidentally timeout while waiting - runs approximately every second.
if (i % 10 == 0){
for (std::map<size_t, Comms::Users>::iterator it = userSelect.begin(); it != userSelect.end(); ++it){
it->second.keepAlive();
stats();
}
}
if (i % 10 == 0){stats();}
}
return (keys.getEndValid() > currentKey + 1 && M.getLastms(thisIdx) > M.getTimeForKeyIndex(getMainSelectedTrack(), currentKey+1));
}

View file

@ -1406,7 +1406,7 @@ namespace Mist{
}
uint64_t idx = reTrackToID[reTrack];
if (idx != INVALID_TRACK_ID && !userSelect.count(idx)){
userSelect[idx].reload(streamName, idx, COMM_STATUS_SOURCE);
userSelect[idx].reload(streamName, idx, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE);
}
if (M.getCodec(idx) == "PCM" && M.getSize(idx) == 16){
char *ptr = F.getData();

View file

@ -812,7 +812,7 @@ namespace Mist{
videoTrack.rtpToDTSC.setCallbacks(onDTSCConverterHasPacketCallback, onDTSCConverterHasInitDataCallback);
videoTrack.sorter.setCallback(M.getID(vIdx), onRTPSorterHasPacketCallback);
userSelect[vIdx].reload(streamName, vIdx, COMM_STATUS_SOURCE);
userSelect[vIdx].reload(streamName, vIdx, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE);
INFO_MSG("Video push received on track %zu", vIdx);
}
@ -834,7 +834,7 @@ namespace Mist{
audioTrack.rtpToDTSC.setCallbacks(onDTSCConverterHasPacketCallback, onDTSCConverterHasInitDataCallback);
audioTrack.sorter.setCallback(M.getID(aIdx), onRTPSorterHasPacketCallback);
userSelect[aIdx].reload(streamName, aIdx, COMM_STATUS_SOURCE);
userSelect[aIdx].reload(streamName, aIdx, COMM_STATUS_ACTIVE | COMM_STATUS_SOURCE);
INFO_MSG("Audio push received on track %zu", aIdx);
}

View file

@ -135,10 +135,10 @@ int main(int argc, char **argv){
Comms::Users cleanUsers;
cleanUsers.reload(Util::streamName, true);
std::set<pid_t> checkPids;
for (size_t i = cleanUsers.firstValid(); i < cleanUsers.endValid(); ++i){
for (size_t i = 0; i < cleanUsers.recordCount(); ++i){
uint8_t status = cleanUsers.getStatus(i);
cleanUsers.setStatus(COMM_STATUS_INVALID, i);
if (status != COMM_STATUS_INVALID && status != COMM_STATUS_DISCONNECT && cleanUsers.getTimer(i) < 126){
if (status != COMM_STATUS_INVALID && status != COMM_STATUS_DISCONNECT){
pid_t pid = cleanUsers.getPid(i);
if (pid > 1){
Util::Procs::Stop(pid);