- statistics improved + allow to use virtual microphone AND/OR speaker + minor cleanups

This commit is contained in:
2026-05-18 10:46:21 +03:00
parent 7cb3b4334f
commit 97c4c3aef0
12 changed files with 381 additions and 308 deletions
+15 -18
View File
@@ -26,16 +26,6 @@ AudioManager::~AudioManager()
// stop();
}
AudioManager& AudioManager::instance()
{
static std::shared_ptr<AudioManager> GAudioManager;
if (!GAudioManager)
GAudioManager = std::make_shared<AudioManager>();
return *GAudioManager;
}
void AudioManager::setTerminal(MT::Terminal* terminal)
{
mTerminal = terminal;
@@ -67,6 +57,7 @@ void AudioManager::start(int usageId)
if (mUsage.obtain(usageId) > 1)
return;
// Maybe it is time to initialize global audio support
if (Audio::OsEngine::instance())
Audio::OsEngine::instance()->open();
@@ -89,14 +80,15 @@ void AudioManager::start(int usageId)
enumerator->open(Audio::myMicrophone);
int inputIndex = enumerator->indexOfDefaultDevice();
// Construct and set to terminal's audio pair input device
// Construct default platform input device
if (usageId != atNull)
mAudioInput = Audio::PInputDevice(Audio::InputDevice::make(enumerator->idAt(inputIndex)));
else
mAudioInput = Audio::PInputDevice(new Audio::NullInputDevice());
mTerminal->audio()->setInput(mAudioInput);
}
// Bind input to the terminal's device pair regardless of whether it was
// just constructed or externally injected via setAudioInput().
mTerminal->audio()->setInput(mAudioInput);
if (!mAudioOutput)
{
@@ -104,7 +96,7 @@ void AudioManager::start(int usageId)
enumerator->open(Audio::mySpeaker);
int outputIndex = enumerator->indexOfDefaultDevice();
// Construct and set terminal's audio pair output device
// Construct default platform output device
if (usageId != atNull)
{
if (outputIndex >= enumerator->count())
@@ -115,9 +107,8 @@ void AudioManager::start(int usageId)
}
else
mAudioOutput = Audio::POutputDevice(new Audio::NullOutputDevice());
mTerminal->audio()->setOutput(mAudioOutput);
}
mTerminal->audio()->setOutput(mAudioOutput);
}
// Open audio
@@ -173,6 +164,12 @@ void AudioManager::setAudioInput(Audio::PInputDevice input)
mAudioInput = std::move(input);
}
void AudioManager::setAudioOutput(Audio::POutputDevice output)
{
LOCK_MANAGER;
mAudioOutput = std::move(output);
}
void AudioManager::startPlayFile(int usageId, const std::string& path, AudioTarget target, LoopMode lm, int timelimit)
{
// Check if file exists
@@ -208,6 +205,6 @@ void AudioManager::process()
mPlayer.releasePlayed();
std::vector<int> ids;
mTerminal->audio()->player().retrieveUsageIds(ids);
for (unsigned i=0; i<ids.size(); i++)
stop(ids[i]);
for (int id : ids)
stop(id);
}
+2 -1
View File
@@ -37,7 +37,7 @@ public:
AudioManager();
virtual ~AudioManager();
static AudioManager& instance();
// static AudioManager& instance();
// Enforces to close audio devices. Used to shutdown AudioManager on exit from application
void close();
@@ -57,6 +57,7 @@ public:
// start() skips construction of the default platform microphone. Pass an
// empty pointer to clear the override.
void setAudioInput(Audio::PInputDevice input);
void setAudioOutput(Audio::POutputDevice output);
enum AudioTarget
{
+114 -110
View File
@@ -3,10 +3,11 @@
#include "helper/HL_String.h"
#include "helper/HL_StreamState.h"
#include "helper/HL_VariantMap.h"
#include "helper/HL_CsvReader.h"
#include "helper/HL_Base64.h"
// #include "helper/HL_CsvReader.h"
// #include "helper/HL_Base64.h"
#include "media/MT_CodecList.h"
#include <fstream>
#include "audio/Audio_Null.h"
// #include <fstream>
const std::string Status_Ok = "ok";
@@ -37,19 +38,19 @@ AgentImpl::~AgentImpl()
// Get access to internal audio manager. Value can be nullptr.
const std::shared_ptr<AudioManager>& AgentImpl::audioManager() const
{
return mAudioManager;
return mAudioManager;
}
void AgentImpl::setAudioMonitoring(Audio::DataConnection* monitoring)
{
mAudioMonitoring = monitoring;
if (mAudioManager)
mAudioManager->setAudioMonitoring(monitoring);
mAudioMonitoring = monitoring;
if (mAudioManager)
mAudioManager->setAudioMonitoring(monitoring);
}
Audio::DataConnection* AgentImpl::monitoring() const
{
return mAudioMonitoring;
return mAudioMonitoring;
}
void AgentImpl::run()
@@ -84,73 +85,73 @@ std::string AgentImpl::command(const std::string& command)
if (cmd == "config")
processConfig(d, answer);
else
if (cmd == "start")
processStart(d, answer);
else
if (cmd == "stop")
processStop(d, answer);
else
if (cmd == "account_create")
processCreateAccount(d, answer);
else
if (cmd == "account_start")
processStartAccount(d, answer);
else
if (cmd == "account_setuserinfo")
processSetUserInfoToAccount(d, answer);
else
if (cmd == "session_create") {
// For Bugsnag test
// int* v = nullptr;
// *v = 0;
processCreateSession(d, answer);
}
else
if (cmd == "session_start")
processStartSession(d, answer);
else
if (cmd == "session_stop")
processStopSession(d, answer);
else
if (cmd == "session_accept")
processAcceptSession(d, answer);
else
if (cmd == "session_destroy")
processDestroySession(d, answer);
else
if (cmd == "session_use_stream")
processUseStreamForSession(d, answer);
else
if (cmd == "wait_for_event")
processWaitForEvent(d, answer);
else
if (cmd == "session_get_media_stats")
processGetMediaStats(d, answer);
else
if (cmd == "agent_network_changed")
processNetworkChanged(d, answer);
else
if (cmd == "agent_add_root_cert")
processAddRootCert(d, answer);
else
if (cmd == "detach_log")
{
GLogger.closeFile();
answer["status"] = Status_Ok;
}
else
if (cmd == "attach_log")
{
GLogger.openFile();
answer["status"] = Status_Ok;
}
else
if (cmd == "log_message")
processLogMessage(d, answer);
else
{
answer["status"] = Status_NoCommand;
}
if (cmd == "start")
processStart(d, answer);
else
if (cmd == "stop")
processStop(d, answer);
else
if (cmd == "account_create")
processCreateAccount(d, answer);
else
if (cmd == "account_start")
processStartAccount(d, answer);
else
if (cmd == "account_setuserinfo")
processSetUserInfoToAccount(d, answer);
else
if (cmd == "session_create") {
// For Bugsnag test
// int* v = nullptr;
// *v = 0;
processCreateSession(d, answer);
}
else
if (cmd == "session_start")
processStartSession(d, answer);
else
if (cmd == "session_stop")
processStopSession(d, answer);
else
if (cmd == "session_accept")
processAcceptSession(d, answer);
else
if (cmd == "session_destroy")
processDestroySession(d, answer);
else
if (cmd == "session_use_stream")
processUseStreamForSession(d, answer);
else
if (cmd == "wait_for_event")
processWaitForEvent(d, answer);
else
if (cmd == "session_get_media_stats")
processGetMediaStats(d, answer);
else
if (cmd == "agent_network_changed")
processNetworkChanged(d, answer);
else
if (cmd == "agent_add_root_cert")
processAddRootCert(d, answer);
else
if (cmd == "detach_log")
{
GLogger.closeFile();
answer["status"] = Status_Ok;
}
else
if (cmd == "attach_log")
{
GLogger.openFile();
answer["status"] = Status_Ok;
}
else
if (cmd == "log_message")
processLogMessage(d, answer);
else
{
answer["status"] = Status_NoCommand;
}
}
catch(std::exception& e)
{
@@ -226,19 +227,13 @@ void AgentImpl::processStart(JsonCpp::Value& request, JsonCpp::Value &answer)
for (int i=0; i<cl.count(); i++)
priorityConfig->at(i) = i;
// Disable dynamic payload codec types - commented for now
// if (cl.codecAt(i).payloadType() < 96)
// priorityConfig->at(i) = i;
// else
// priorityConfig->at(i) = -1;
config()[CONFIG_CODEC_PRIORITY] = priorityConfig;
// Enable audio
mAudioManager = std::make_shared<AudioManager>();
mAudioManager->setTerminal(mTerminal.get());
if (mAudioMonitoring)
mAudioManager->setAudioMonitoring(mAudioMonitoring);
mAudioManager->setAudioMonitoring(mAudioMonitoring);
// Do not start audio manager here. Start right before call.
@@ -341,6 +336,11 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
return;
}
if (request["use_null_mic"].asBool())
mAudioManager->setAudioInput(std::make_shared<Audio::NullInputDevice>());
if (request["use_null_spk"].asBool())
mAudioManager->setAudioOutput(std::make_shared<Audio::NullOutputDevice>());
mAudioManager->start(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull);
auto sessionIter = mSessionMap.find(request["session_id"].asInt());
@@ -351,7 +351,7 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
PDataProvider audioProvider = std::make_shared<AudioProvider>(*this, *mTerminal);
audioProvider->setState(audioProvider->state() | static_cast<int>(StreamState::Grabbing) | static_cast<int>(StreamState::Playing));
/*#if defined(USE_AQUA_LIBRARY)
/*#if defined(USE_AQUA_LIBRARY)
std::string path_faults = request["path_faults"].asString();
sevana::aqua::config config = {
@@ -440,6 +440,10 @@ void AgentImpl::processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& an
else
{
// Ensure audio manager is here
if (request["use_null_mic"].asBool())
mAudioManager->setAudioInput(std::make_shared<Audio::NullInputDevice>());
if (request["use_null_spk"].asBool())
mAudioManager->setAudioOutput(std::make_shared<Audio::NullOutputDevice>());
mAudioManager->start(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull);
// Accept session on SIP level
@@ -471,9 +475,9 @@ void AgentImpl::processDestroySession(JsonCpp::Value& request, JsonCpp::Value& a
auto sessionIter = mSessionMap.find(sessionId);
if (sessionIter != mSessionMap.end())
mSessionMap.erase(sessionIter);
//#if defined(USE_AQUA_LIBRARY)
// closeAqua(sessionId);
//#endif
//#if defined(USE_AQUA_LIBRARY)
// closeAqua(sessionId);
//#endif
answer["status"] = Status_Ok;
}
@@ -637,7 +641,7 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
// Parse command
std::string actionText = request["media_action"].asString(),
directionText = request["media_direction"].asString();
directionText = request["media_direction"].asString();
MT::Stream::MediaDirection direction = directionText == "incoming" ? MT::Stream::MediaDirection::Incoming
: MT::Stream::MediaDirection::Outgoing;
@@ -668,34 +672,34 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
}
}
else
if (actionText == "write")
{
if (path.empty())
if (actionText == "write")
{
// Turn off recording from the stream
prov->writeFile(Audio::PWavFileWriter(), direction);
answer["status"] = Status_Ok;
}
else
{
Audio::PWavFileWriter writer = std::make_shared<Audio::WavFileWriter>();
if (!writer->open(strx::makeTstring(path), AUDIO_SAMPLERATE, AUDIO_CHANNELS))
answer["status"] = Status_FailedToOpenFile;
else
if (path.empty())
{
prov->writeFile(writer, direction);
// Turn off recording from the stream
prov->writeFile(Audio::PWavFileWriter(), direction);
answer["status"] = Status_Ok;
}
else
{
Audio::PWavFileWriter writer = std::make_shared<Audio::WavFileWriter>();
if (!writer->open(strx::makeTstring(path), AUDIO_SAMPLERATE, AUDIO_CHANNELS))
answer["status"] = Status_FailedToOpenFile;
else
{
prov->writeFile(writer, direction);
answer["status"] = Status_Ok;
}
}
}
}
else
if (actionText == "mirror")
{
prov->setupMirror(request["enable"].asBool());
answer["status"] = Status_Ok;
}
else
answer["status"] = Status_NoCommand;
else
if (actionText == "mirror")
{
prov->setupMirror(request["enable"].asBool());
answer["status"] = Status_Ok;
}
else
answer["status"] = Status_NoCommand;
}
else
answer["status"] = Status_NoMediaAction;
+78 -78
View File
@@ -18,111 +18,111 @@
class AgentImpl: public UserAgent, public MT::Stream::MediaObserver
{
protected:
std::recursive_mutex mAgentMutex;
std::mutex mEventListMutex;
std::condition_variable mEventListChangeCondVar;
std::vector<JsonCpp::Value> mEventList;
bool mUseNativeAudio = false;
std::recursive_mutex mAgentMutex;
std::mutex mEventListMutex;
std::condition_variable mEventListChangeCondVar;
std::vector<JsonCpp::Value> mEventList;
bool mUseNativeAudio = false;
typedef std::map<int, PAccount> AccountMap;
AccountMap mAccountMap;
typedef std::map<int, PAccount> AccountMap;
AccountMap mAccountMap;
typedef std::map<int, PSession> SessionMap;
SessionMap mSessionMap;
typedef std::map<int, PSession> SessionMap;
SessionMap mSessionMap;
std::shared_ptr<std::thread> mThread;
volatile bool mShutdown;
std::shared_ptr<MT::Terminal> mTerminal;
std::shared_ptr<AudioManager> mAudioManager;
Audio::DataConnection* mAudioMonitoring = nullptr;
std::shared_ptr<std::thread> mThread;
volatile bool mShutdown;
std::shared_ptr<MT::Terminal> mTerminal;
std::shared_ptr<AudioManager> mAudioManager;
Audio::DataConnection* mAudioMonitoring = nullptr;
void run();
void addEvent(const JsonCpp::Value& v);
void processConfig(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStart(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStop(JsonCpp::Value& request, JsonCpp::Value& answer);
void processCreateAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStartAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processSetUserInfoToAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processCreateSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStartSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStopSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processDestroySession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processWaitForEvent(JsonCpp::Value& request, JsonCpp::Value& answer);
void processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& answer);
void processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processNetworkChanged(JsonCpp::Value& request, JsonCpp::Value& answer);
void processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answer);
void processLogMessage(JsonCpp::Value& request, JsonCpp::Value& answer);
void stopAgentAndThread();
void run();
void addEvent(const JsonCpp::Value& v);
void processConfig(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStart(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStop(JsonCpp::Value& request, JsonCpp::Value& answer);
void processCreateAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStartAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processSetUserInfoToAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processCreateSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStartSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStopSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processDestroySession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processWaitForEvent(JsonCpp::Value& request, JsonCpp::Value& answer);
void processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& answer);
void processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processNetworkChanged(JsonCpp::Value& request, JsonCpp::Value& answer);
void processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answer);
void processLogMessage(JsonCpp::Value& request, JsonCpp::Value& answer);
void stopAgentAndThread();
public:
AgentImpl();
~AgentImpl();
AgentImpl();
~AgentImpl();
std::string command(const std::string& command);
bool waitForData(int milliseconds);
std::string read();
std::string command(const std::string& command);
bool waitForData(int milliseconds);
std::string read();
// Get access to internal audio manager. Value can be nullptr.
const std::shared_ptr<AudioManager>& audioManager() const;
// Get access to internal audio manager. Value can be nullptr.
const std::shared_ptr<AudioManager>& audioManager() const;
void setAudioMonitoring(Audio::DataConnection* monitoring);
Audio::DataConnection* monitoring() const;
void setAudioMonitoring(Audio::DataConnection* monitoring);
Audio::DataConnection* monitoring() const;
// UserAgent overrides
// Called on new incoming session; providers shoukld
PDataProvider onProviderNeeded(const std::string& name) override;
// UserAgent overrides
// Called on new incoming session; providers shoukld
PDataProvider onProviderNeeded(const std::string& name) override;
// Called on new session offer
void onNewSession(PSession s) override;
// Called on new session offer
void onNewSession(PSession s) override;
// Called when session is terminated
void onSessionTerminated(PSession s, int responsecode, int reason) override;
// Called when session is terminated
void onSessionTerminated(PSession s, int responsecode, int reason) override;
// Called when session is established ok i.e. after all ICE signalling is finished
// Conntype is type of establish event - EV_SIP or EV_ICE
void onSessionEstablished(PSession s, int conntype, const RtpPair<InternetAddress>& p) override;
// Called when session is established ok i.e. after all ICE signalling is finished
// Conntype is type of establish event - EV_SIP or EV_ICE
void onSessionEstablished(PSession s, int conntype, const RtpPair<InternetAddress>& p) override;
void onSessionProvisional(PSession s, int code) override;
void onSessionProvisional(PSession s, int code) override;
// Called when user agent started
void onStart(int errorcode) override;
// Called when user agent started
void onStart(int errorcode) override;
// Called when user agent stopped
void onStop() override;
// Called when user agent stopped
void onStop() override;
// Called when account registered
void onAccountStart(PAccount account) override;
// Called when account registered
void onAccountStart(PAccount account) override;
// Called when account removed or failed (non zero error code)
void onAccountStop(PAccount account, int error) override;
// Called when account removed or failed (non zero error code)
void onAccountStop(PAccount account, int error) override;
// Called when connectivity checks failed.
void onConnectivityFailed(PSession s) override;
// Called when connectivity checks failed.
void onConnectivityFailed(PSession s) override;
// Called when new candidate is gathered
void onCandidateGathered(PSession s, const char* address) override;
// Called when new candidate is gathered
void onCandidateGathered(PSession s, const char* address) override;
// Called when network change detected
void onNetworkChange(PSession s) override;
// Called when network change detected
void onNetworkChange(PSession s) override;
// Called when all candidates are gathered
void onGathered(PSession s) override;
// Called when all candidates are gathered
void onGathered(PSession s) override;
// Called when new connectivity check is finished
void onCheckFinished(PSession s, const char* description) override;
// Called when new connectivity check is finished
void onCheckFinished(PSession s, const char* description) override;
// Called when log message must be recorded
void onLog(const char* msg) override;
// Called when log message must be recorded
void onLog(const char* msg) override;
// Called when problem with SIP connection(s) detected
void onSipConnectionFailed() override;
// Called when problem with SIP connection(s) detected
void onSipConnectionFailed() override;
// Called on incoming & outgoing audio for voice sessions
void onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context, void* userTag) override;
// Called on incoming & outgoing audio for voice sessions
void onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context, void* userTag) override;
};
#endif