diff options
-rw-r--r-- | orkaudio/CapturePort.cpp | 81 | ||||
-rw-r--r-- | orkaudio/CapturePort.h | 9 |
2 files changed, 75 insertions, 15 deletions
diff --git a/orkaudio/CapturePort.cpp b/orkaudio/CapturePort.cpp index 044520f..14e0e8a 100644 --- a/orkaudio/CapturePort.cpp +++ b/orkaudio/CapturePort.cpp @@ -20,12 +20,13 @@ #include "Reporting.h" #include "ConfigManager.h" -CapturePort::CapturePort(CStdString& Id) +CapturePort::CapturePort(CStdString& id) { - m_Id = Id; + m_id = id; m_vadBelowThresholdSec = 0.0; m_vadUp = false; m_capturing = false; + m_lastUpdated = 0; } CStdString CapturePort::ToString() @@ -34,9 +35,16 @@ CStdString CapturePort::ToString() return ret; } +CStdString CapturePort::GetId() +{ + return m_id; +} + + void CapturePort::AddAudioChunk(AudioChunkRef chunkRef) { time_t now = time(NULL); + m_lastUpdated = now; if(CONFIG.m_audioSegmentation) { @@ -51,7 +59,7 @@ void CapturePort::AddAudioChunk(AudioChunkRef chunkRef) AddCaptureEvent(eventRef); // create new tape - m_audioTapeRef.reset(new AudioTape(m_Id)); + m_audioTapeRef.reset(new AudioTape(m_id)); // signal new tape start event eventRef.reset(new CaptureEvent); @@ -63,7 +71,7 @@ void CapturePort::AddAudioChunk(AudioChunkRef chunkRef) else { // create new tape - m_audioTapeRef.reset(new AudioTape(m_Id)); + m_audioTapeRef.reset(new AudioTape(m_id)); // signal new tape start event CaptureEventRef eventRef(new CaptureEvent); @@ -112,7 +120,7 @@ void CapturePort::AddAudioChunk(AudioChunkRef chunkRef) m_vadUp = true; // create new tape - m_audioTapeRef.reset(new AudioTape(m_Id)); + m_audioTapeRef.reset(new AudioTape(m_id)); // signal new tape start event CaptureEventRef eventRef(new CaptureEvent); @@ -139,6 +147,8 @@ void CapturePort::AddAudioChunk(AudioChunkRef chunkRef) void CapturePort::AddCaptureEvent(CaptureEventRef eventRef) { + m_lastUpdated = time(NULL); + AudioTapeRef audioTapeRef = m_audioTapeRef; // First of all, handle tape start @@ -149,16 +159,16 @@ void CapturePort::AddCaptureEvent(CaptureEventRef eventRef) { audioTapeRef->SetShouldStop(); // force stop of previous tape } - audioTapeRef.reset(new AudioTape(m_Id)); // Create a new tape + audioTapeRef.reset(new AudioTape(m_id)); // Create a new tape audioTapeRef->AddCaptureEvent(eventRef, false); //Reporting::GetInstance()->AddAudioTape(audioTapeRef); m_audioTapeRef = audioTapeRef; - LOG4CXX_INFO(LOG.portLog, "#" + m_Id + ": start"); + LOG4CXX_INFO(LOG.portLog, "#" + m_id + ": start"); } if (!audioTapeRef.get()) { - LOG4CXX_WARN(LOG.portLog, "#" + m_Id + ": received unexpected capture event:" + LOG4CXX_WARN(LOG.portLog, "#" + m_id + ": received unexpected capture event:" + CaptureEvent::EventTypeToString(eventRef->m_type)); } else @@ -169,7 +179,7 @@ void CapturePort::AddCaptureEvent(CaptureEventRef eventRef) case CaptureEvent::EtStop: m_capturing = false; - LOG4CXX_INFO(LOG.portLog, "#" + m_Id + ": stop"); + LOG4CXX_INFO(LOG.portLog, "#" + m_id + ": stop"); audioTapeRef->AddCaptureEvent(eventRef, true); if (m_audioTapeRef->GetAudioFileRef().get()) @@ -182,7 +192,7 @@ void CapturePort::AddCaptureEvent(CaptureEventRef eventRef) else { // Received a stop but there is no valid audio file associated with the tape - LOG4CXX_WARN(LOG.portLog, "#" + m_Id + ": no audio reported between last start and stop"); + LOG4CXX_WARN(LOG.portLog, "#" + m_id + ": no audio reported between last start and stop"); } break; case CaptureEvent::EtDirection: @@ -195,16 +205,27 @@ void CapturePort::AddCaptureEvent(CaptureEventRef eventRef) } } +bool CapturePort::IsExpired(time_t now) +{ + if((now - m_lastUpdated) > 60) // 1 minute + { + return true; + } + return false; +} + //======================================= - -void CapturePorts::Initialize() +CapturePorts::CapturePorts() { m_ports.clear(); + m_lastHooveringTime = time(NULL); } CapturePortRef CapturePorts::GetPort(CStdString & portId) { + Hoover(); + std::map<CStdString, CapturePortRef>::iterator pair; pair = m_ports.find(portId); @@ -222,7 +243,7 @@ CapturePortRef CapturePorts::GetPort(CStdString & portId) CapturePortRef CapturePorts::AddAndReturnPort(CStdString & portId) { - //MutexGuard mutexGuard(m_mutex); // To make sure a channel cannot be created twice + //MutexGuard mutexGuard(m_mutex); // To make sure a channel cannot be created twice - not used for now. CapturePorts only ever gets interaction from capture single thread CapturePortRef portRef = GetPort(portId); if (portRef.get() == NULL) @@ -238,4 +259,38 @@ CapturePortRef CapturePorts::AddAndReturnPort(CStdString & portId) } } +void CapturePorts::Hoover() +{ + CStdString logMsg; + time_t now = time(NULL); + if( (now - m_lastHooveringTime) > 10) // Hoover every 10 seconds + { + m_lastHooveringTime = now; + int numPorts = m_ports.size(); + + // Go round and detect inactive ports + std::map<CStdString, CapturePortRef>::iterator pair; + std::list<CapturePortRef> toDismiss; + + for(pair = m_ports.begin(); pair != m_ports.end(); pair++) + { + CapturePortRef port = pair->second; + if(port->IsExpired(now)) + { + toDismiss.push_back(port); + } + } + + // Discard inactive ports + for (std::list<CapturePortRef>::iterator it = toDismiss.begin(); it != toDismiss.end() ; it++) + { + CapturePortRef port = *it; + m_ports.erase(port->GetId()); + LOG4CXX_DEBUG(LOG.portLog, port->GetId() + ": Expired"); + } + logMsg.Format("Hoovered %d ports. New number:%d", (numPorts - m_ports.size()), m_ports.size()); + LOG4CXX_DEBUG(LOG.portLog, logMsg); + } +} + diff --git a/orkaudio/CapturePort.h b/orkaudio/CapturePort.h index bd62731..69b4c0a 100644 --- a/orkaudio/CapturePort.h +++ b/orkaudio/CapturePort.h @@ -33,16 +33,19 @@ class CapturePort public: CapturePort(CStdString& Id); CStdString ToString(); + CStdString GetId(); void AddAudioChunk(AudioChunkRef chunkRef); void AddCaptureEvent(CaptureEventRef eventRef); + bool IsExpired(time_t now); private: - CStdString m_Id; + CStdString m_id; AudioTapeRef m_audioTapeRef; ACE_Thread_Mutex m_mutex; bool m_capturing; double m_vadBelowThresholdSec; bool m_vadUp; + time_t m_lastUpdated; }; typedef boost::shared_ptr<CapturePort> CapturePortRef; @@ -51,13 +54,15 @@ typedef boost::shared_ptr<CapturePort> CapturePortRef; class CapturePorts { public: - void Initialize(); + CapturePorts(); CapturePortRef GetPort(CStdString & portId); /** Tries to find a capture port from its ID. If unsuccessful, creates a new one and returns it */ CapturePortRef AddAndReturnPort(CStdString & portId); + void Hoover(); private: std::map<CStdString, CapturePortRef> m_ports; ACE_Thread_Mutex m_mutex; + time_t m_lastHooveringTime; }; typedef ACE_Singleton<CapturePorts, ACE_Thread_Mutex> CapturePortsSingleton; |