summaryrefslogtreecommitdiff
path: root/orkaudio/CapturePort.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'orkaudio/CapturePort.cpp')
-rw-r--r--orkaudio/CapturePort.cpp81
1 files changed, 68 insertions, 13 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);
+ }
+}
+