Implemented triggers.

Merged from code by Wouter Spruit, with additions by yours truly.
This commit is contained in:
Thulinma 2015-10-07 13:23:27 +02:00
parent eb6b98b219
commit 279add438a
18 changed files with 597 additions and 6 deletions

View file

@ -3,6 +3,7 @@
#include <sys/stat.h>
#include <mist/stream.h>
#include <mist/triggers.h>
#include <mist/defines.h>
#include "input.h"
#include <sstream>
@ -185,7 +186,21 @@ namespace Mist {
}
}
void Input::serve() {
/// The main loop for inputs in stream serving mode.
///
/// \triggers
/// The `"STREAM_READY"` trigger is stream-specific, and is ran whenever an input finished loading and started serving a stream. If cancelled, the input is immediately shut down again. Its payload is:
/// ~~~~~~~~~~~~~~~
/// streamname
/// input name
/// ~~~~~~~~~~~~~~~
/// The `"STREAM_UNLOAD"` trigger is stream-specific, and is ran right before an input shuts down and stops serving a stream. If cancelled, the shut down is delayed. Its payload is:
/// ~~~~~~~~~~~~~~~
/// streamname
/// input name
/// ~~~~~~~~~~~~~~~
//
void Input::serve(){
char userPageName[NAME_BUFFER_SIZE];
snprintf(userPageName, NAME_BUFFER_SIZE, SHM_USERS, streamName.c_str());
#ifdef INPUT_LIVE
@ -219,6 +234,14 @@ namespace Mist {
}
userClient.finish();
#else
/*LTS-START*/
if(Triggers::shouldTrigger("STREAM_READY", config->getString("streamname"))){
std::string payload = config->getString("streamname")+"\n" +capa["name"].asStringRef()+"\n";
if (!Triggers::doTrigger("STREAM_READY", payload, config->getString("streamname"))){
config->is_active = false;
}
}
/*LTS-END*/
userPage.init(userPageName, PLAY_EX_SIZE, true);
if (!isBuffer) {
for (std::map<unsigned int, DTSC::Track>::iterator it = myMeta.tracks.begin(); it != myMeta.tracks.end(); it++) {
@ -239,6 +262,17 @@ namespace Mist {
} else {
DEBUG_MSG(DLVL_INSANE, "Timer running");
}
/*LTS-START*/
if ((Util::bootSecs() - activityCounter) >= 10 || !config->is_active){//10 second timeout
if(Triggers::shouldTrigger("STREAM_UNLOAD", config->getString("streamname"))){
std::string payload = config->getString("streamname")+"\n" +capa["name"].asStringRef()+"\n";
if (!Triggers::doTrigger("STREAM_UNLOAD", payload, config->getString("streamname"))){
activityCounter = Util::bootSecs();
config->is_active = true;
}
}
}
/*LTS-END*/
}
#endif
finish();

View file

@ -9,6 +9,7 @@
#include <string>
#include <mist/stream.h>
#include <mist/defines.h>
#include <mist/triggers.h>
#include "input_buffer.h"
@ -299,6 +300,12 @@ namespace Mist {
}
}
/// \triggers
/// The `"STREAM_TRACK_REMOVE"` trigger is stream-specific, and is ran whenever a track is fully removed from a live strean buffer. It cannot be cancelled. Its payload is:
/// ~~~~~~~~~~~~~~~
/// streamname
/// trackID
/// ~~~~~~~~~~~~~~~
void inputBuffer::removeUnused(){
//first remove all tracks that have not been updated for too long
bool changed = true;
@ -335,6 +342,12 @@ namespace Mist {
}else{
INFO_MSG("Erasing inactive track %u because it was inactive for 5+ seconds and contains data (%us - %us), while active tracks are (%us - %us), which is more than %us seconds apart.", it->first, it->second.firstms / 1000, it->second.lastms / 1000, compareFirst/1000, compareLast/1000, bufferTime / 1000);
}
/*LTS-START*/
if(Triggers::shouldTrigger("STREAM_TRACK_REMOVE")){
std::string payload = config->getString("streamname")+"\n"+JSON::Value((long long)it->first).asString()+"\n";
Triggers::doTrigger("STREAM_TRACK_REMOVE", payload, config->getString("streamname"));
}
/*LTS-END*/
lastUpdated.erase(tid);
/// \todo Consider replacing with eraseTrackDataPages(it->first)?
while (bufferLocations[tid].size()){
@ -397,6 +410,12 @@ namespace Mist {
updateMeta();
}
/// \triggers
/// The `"STREAM_TRACK_ADD"` trigger is stream-specific, and is ran whenever a new track is added to a live strean buffer. It cannot be cancelled. Its payload is:
/// ~~~~~~~~~~~~~~~
/// streamname
/// trackID
/// ~~~~~~~~~~~~~~~
void inputBuffer::userCallback(char * data, size_t len, unsigned int id){
/*LTS-START*/
//Reload the configuration to make sure we stay up to date with changes through the api
@ -532,6 +551,10 @@ namespace Mist {
//No collision has been detected, assign a new final number
finalMap = (myMeta.tracks.size() ? myMeta.tracks.rbegin()->first : 0) + 1;
DEBUG_MSG(DLVL_DEVEL, "No colision detected for temporary track %lu from user %u, assigning final track number %lu", value, id, finalMap);
if(Triggers::shouldTrigger("STREAM_TRACK_ADD")){
std::string payload = config->getString("streamname")+"\n"+JSON::Value((long long)finalMap).asString()+"\n";
Triggers::doTrigger("STREAM_TRACK_ADD", payload, config->getString("streamname"));
}
}
/*LTS-END*/
//Resume either if we have more than 1 keyframe on the replacement track (assume it was already pushing before the track "dissapeared")

View file

@ -13,6 +13,21 @@
#include <mist/mp4_generic.h>
#include "input_ts.h"
/// \todo Implement this trigger equivalent...
/*
if(Triggers::shouldTrigger("STREAM_PUSH", smp)){
std::string payload = streamName+"\n" + myConn.getHost() +"\n"+capa["name"].asStringRef()+"\n"+reqUrl;
if (!Triggers::doTrigger("STREAM_PUSH", payload, smp)){
DEBUG_MSG(DLVL_FAIL, "Push from %s to %s rejected - STREAM_PUSH trigger denied the push", myConn.getHost().c_str(), streamName.c_str());
myConn.close();
configLock.post();
configLock.close();
return;
}
}
*/
namespace Mist {