#include "path.h" #include "../recorder/recorder.h" namespace mtx { Path::Path(const PathConf & conf, const Conf & global) : LogParent("[path " + conf.name + "] "), m_conf(conf), m_global(global) {} Path::~Path() { stop(); } void Path::start() { m_pSource.reset(new RtspSource(m_conf, m_global, this)); m_pSource->start(); } void Path::stop() { if (m_pSource) { m_pSource->stop(); // calls sourceSetNotReady() if it was ready m_pSource.reset(); } } PathInfo Path::info() const { std::lock_guard lock(m_mutex); PathInfo tI; tI.name = m_conf.name; tI.source = m_conf.source; tI.record = m_conf.record; tI.ready = (m_pStream != nullptr); tI.readySinceUs = m_tReadySinceUs; tI.readers = m_readers.size(); if (m_pStream) { tI.desc = m_pStream->desc(); tI.bytesReceived = m_pStream->bytesReceived(); } return tI; } bool Path::addReader(PathReader * r, StreamDesc & desc, std::string & err) { std::lock_guard lock(m_mutex); if (! m_pStream) { err = "no stream is available on path '" + m_conf.name + "'"; return false; } desc = m_pStream->desc(); m_readers.insert(r); m_pStream->addReader(r); return true; } void Path::removeReader(PathReader * r) { std::lock_guard lock(m_mutex); if (m_readers.erase(r) > 0 && m_pStream) { m_pStream->removeReader(r); } } void Path::sourceSetReady(const StreamDesc & desc) { std::lock_guard lock(m_mutex); m_pStream = std::make_shared(desc); m_tReadySinceUs = nowUs(); log(LogLevel::Info, "stream is available and online, 1 track (H264 %dx%d)", desc.width, desc.height); if (m_conf.record) { m_pRecorder.reset(new Recorder(m_conf.name, m_global, m_pStream, this)); m_pRecorder->start(); } } void Path::sourceSetNotReady() { std::unique_ptr tRec; { std::lock_guard lock(m_mutex); if (! m_pStream) { return; } for (PathReader * r : m_readers) { m_pStream->removeReader(r); r->pathNotReady(); } m_readers.clear(); tRec = std::move(m_pRecorder); m_pStream.reset(); m_tReadySinceUs = 0; } tRec.reset(); // closes the segment log(LogLevel::Info, "stream is not available anymore"); } void Path::sourceWriteUnit(const UnitPtr & unit) { std::shared_ptr tStream; { std::lock_guard lock(m_mutex); tStream = m_pStream; } if (tStream) { tStream->writeUnit(unit); } } } // namespace mtx