- clang format + naming improved

This commit is contained in:
2026-09-03 10:03:42 +03:00
parent 249e614def
commit e166bae1ea
204 changed files with 20621 additions and 21019 deletions
+6 -10
View File
@@ -9,14 +9,13 @@
#include "HL_String.h" #include "HL_String.h"
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
# include "../engine/audio/Audio_Android.h" #include "../engine/audio/Audio_Android.h"
#endif #endif
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "audio"
AudioManager::AudioManager() AudioManager::AudioManager() : mTerminal(nullptr), mAudioMonitoring(nullptr)
:mTerminal(nullptr), mAudioMonitoring(nullptr)
{ {
mPlayer.setDelegate(this); mPlayer.setDelegate(this);
} }
@@ -92,7 +91,7 @@ void AudioManager::start(int usageId)
if (!mAudioOutput) if (!mAudioOutput)
{ {
Audio::Enumerator *enumerator = Audio::Enumerator::make(usageId == atNull); Audio::Enumerator* enumerator = Audio::Enumerator::make(usageId == atNull);
enumerator->open(Audio::mySpeaker); enumerator->open(Audio::mySpeaker);
int outputIndex = enumerator->indexOfDefaultDevice(); int outputIndex = enumerator->indexOfDefaultDevice();
@@ -102,8 +101,7 @@ void AudioManager::start(int usageId)
if (outputIndex >= enumerator->count()) if (outputIndex >= enumerator->count())
outputIndex = 0; outputIndex = 0;
mAudioOutput = Audio::POutputDevice( mAudioOutput = Audio::POutputDevice(Audio::OutputDevice::make(enumerator->idAt(outputIndex)));
Audio::OutputDevice::make(enumerator->idAt(outputIndex)));
} }
else else
mAudioOutput = Audio::POutputDevice(new Audio::NullOutputDevice()); mAudioOutput = Audio::POutputDevice(new Audio::NullOutputDevice());
@@ -139,7 +137,7 @@ void AudioManager::stop(int usageId)
{ {
LOCK_MANAGER; LOCK_MANAGER;
ICELogInfo( << "Stop main audio with usage id " << usageId); ICELogInfo(<< "Stop main audio with usage id " << usageId);
if (mTerminal) if (mTerminal)
{ {
if (mTerminal->audio()) if (mTerminal->audio())
@@ -196,9 +194,7 @@ void AudioManager::stopPlayFile(int usageId)
mPlayer.release(usageId); mPlayer.release(usageId);
} }
void AudioManager::onFilePlayed(Audio::Player::PlaylistItem& item) void AudioManager::onFilePlayed(Audio::Player::PlaylistItem& item) {}
{
}
void AudioManager::process() void AudioManager::process()
{ {
+1 -2
View File
@@ -11,7 +11,6 @@
#include "../engine/media/MT_Box.h" #include "../engine/media/MT_Box.h"
enum enum
{ {
AudioPrefix_Ring = 1, AudioPrefix_Ring = 1,
@@ -31,7 +30,7 @@ enum
#define AudioSessionCoeff 64 #define AudioSessionCoeff 64
class AudioManager: public Audio::Player::EndOfAudioDelegate class AudioManager : public Audio::Player::EndOfAudioDelegate
{ {
public: public:
AudioManager(); AudioManager();
+59 -70
View File
@@ -21,8 +21,7 @@ const std::string Status_NoAudioManager = "no audio manager";
#define LOG_SUBSYSTEM "agent" #define LOG_SUBSYSTEM "agent"
AgentImpl::AgentImpl() AgentImpl::AgentImpl() : mShutdown(false), mEventListChangeCondVar()
:mShutdown(false), mEventListChangeCondVar()
{ {
#if defined(TARGET_ANDROID) || defined(TARGET_WIN) #if defined(TARGET_ANDROID) || defined(TARGET_WIN)
ice::GLogger.useDebugWindow(true); ice::GLogger.useDebugWindow(true);
@@ -84,76 +83,59 @@ std::string AgentImpl::command(const std::string& command)
} }
if (cmd == "config") if (cmd == "config")
processConfig(d, answer); processConfig(d, answer);
else else if (cmd == "start")
if (cmd == "start")
processStart(d, answer); processStart(d, answer);
else else if (cmd == "stop")
if (cmd == "stop")
processStop(d, answer); processStop(d, answer);
else else if (cmd == "account_create")
if (cmd == "account_create")
processCreateAccount(d, answer); processCreateAccount(d, answer);
else else if (cmd == "account_start")
if (cmd == "account_start")
processStartAccount(d, answer); processStartAccount(d, answer);
else else if (cmd == "account_setuserinfo")
if (cmd == "account_setuserinfo")
processSetUserInfoToAccount(d, answer); processSetUserInfoToAccount(d, answer);
else else if (cmd == "session_create")
if (cmd == "session_create") { {
// For Bugsnag test // For Bugsnag test
// int* v = nullptr; // int* v = nullptr;
// *v = 0; // *v = 0;
processCreateSession(d, answer); processCreateSession(d, answer);
} }
else else if (cmd == "session_start")
if (cmd == "session_start")
processStartSession(d, answer); processStartSession(d, answer);
else else if (cmd == "session_stop")
if (cmd == "session_stop")
processStopSession(d, answer); processStopSession(d, answer);
else else if (cmd == "session_accept")
if (cmd == "session_accept")
processAcceptSession(d, answer); processAcceptSession(d, answer);
else else if (cmd == "session_destroy")
if (cmd == "session_destroy")
processDestroySession(d, answer); processDestroySession(d, answer);
else else if (cmd == "session_use_stream")
if (cmd == "session_use_stream")
processUseStreamForSession(d, answer); processUseStreamForSession(d, answer);
else else if (cmd == "wait_for_event")
if (cmd == "wait_for_event")
processWaitForEvent(d, answer); processWaitForEvent(d, answer);
else else if (cmd == "session_get_media_stats")
if (cmd == "session_get_media_stats")
processGetMediaStats(d, answer); processGetMediaStats(d, answer);
else else if (cmd == "agent_network_changed")
if (cmd == "agent_network_changed")
processNetworkChanged(d, answer); processNetworkChanged(d, answer);
else else if (cmd == "agent_add_root_cert")
if (cmd == "agent_add_root_cert")
processAddRootCert(d, answer); processAddRootCert(d, answer);
else else if (cmd == "detach_log")
if (cmd == "detach_log")
{ {
GLogger.closeFile(); GLogger.closeFile();
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
else else if (cmd == "attach_log")
if (cmd == "attach_log")
{ {
GLogger.openFile(); GLogger.openFile();
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
else else if (cmd == "log_message")
if (cmd == "log_message")
processLogMessage(d, answer); processLogMessage(d, answer);
else else
{ {
answer["status"] = Status_NoCommand; answer["status"] = Status_NoCommand;
} }
} }
catch(std::exception& e) catch (std::exception& e)
{ {
answer["status"] = e.what(); answer["status"] = e.what();
} }
@@ -172,12 +154,15 @@ std::string AgentImpl::read()
return ""; return "";
} }
void AgentImpl::processConfig(JsonCpp::Value &d, JsonCpp::Value &answer) void AgentImpl::processConfig(JsonCpp::Value& d, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
std::string transport = d["transport"].asString(); std::string transport = d["transport"].asString();
config()[CONFIG_TRANSPORT] = (transport == "any") ? TransportType_Any : (transport == "udp" ? TransportType_Udp : (transport == "tcp" ? TransportType_Tcp : TransportType_Tls)); config()[CONFIG_TRANSPORT] =
(transport == "any")
? TransportType_Any
: (transport == "udp" ? TransportType_Udp : (transport == "tcp" ? TransportType_Tcp : TransportType_Tls));
config()[CONFIG_IPV4] = d["ipv4"].asBool(); config()[CONFIG_IPV4] = d["ipv4"].asBool();
config()[CONFIG_IPV6] = d["ipv6"].asBool(); config()[CONFIG_IPV6] = d["ipv6"].asBool();
@@ -199,7 +184,7 @@ void AgentImpl::processConfig(JsonCpp::Value &d, JsonCpp::Value &answer)
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processStart(JsonCpp::Value& request, JsonCpp::Value &answer) void AgentImpl::processStart(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
if (mThread) if (mThread)
@@ -222,7 +207,7 @@ void AgentImpl::processStart(JsonCpp::Value& request, JsonCpp::Value &answer)
// Enable/disable codecs // Enable/disable codecs
PVariantMap priorityConfig = std::make_shared<VariantMap>(); PVariantMap priorityConfig = std::make_shared<VariantMap>();
MT::CodecList& cl = mTerminal->codeclist(); MT::CodecList& cl = mTerminal->codeclist();
for (int i=0; i<cl.count(); i++) for (int i = 0; i < cl.count(); i++)
priorityConfig->at(i) = i; priorityConfig->at(i) = i;
config()[CONFIG_CODEC_PRIORITY] = priorityConfig; config()[CONFIG_CODEC_PRIORITY] = priorityConfig;
@@ -250,7 +235,7 @@ void AgentImpl::processStop(JsonCpp::Value& /*request*/, JsonCpp::Value& answer)
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processCreateAccount(JsonCpp::Value &d, JsonCpp::Value& answer) void AgentImpl::processCreateAccount(JsonCpp::Value& d, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
PVariantMap c = std::make_shared<VariantMap>(); PVariantMap c = std::make_shared<VariantMap>();
@@ -286,7 +271,7 @@ void AgentImpl::processStartAccount(JsonCpp::Value& request, JsonCpp::Value& ans
answer["status"] = Status_AccountNotFound; answer["status"] = Status_AccountNotFound;
} }
void AgentImpl::processSetUserInfoToAccount(JsonCpp::Value &request, JsonCpp::Value &answer) void AgentImpl::processSetUserInfoToAccount(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
// Locate account in map // Locate account in map
@@ -296,7 +281,7 @@ void AgentImpl::processSetUserInfoToAccount(JsonCpp::Value &request, JsonCpp::Va
Account::UserInfo info; Account::UserInfo info;
JsonCpp::Value& arg = request["userinfo"]; JsonCpp::Value& arg = request["userinfo"];
std::vector<std::string> keys = arg.getMemberNames(); std::vector<std::string> keys = arg.getMemberNames();
for (const std::string& k: keys) for (const std::string& k : keys)
info[k] = arg[k].asString(); info[k] = arg[k].asString();
accountIter->second->setUserInfo(info); accountIter->second->setUserInfo(info);
@@ -306,7 +291,7 @@ void AgentImpl::processSetUserInfoToAccount(JsonCpp::Value &request, JsonCpp::Va
answer["status"] = Status_AccountNotFound; answer["status"] = Status_AccountNotFound;
} }
void AgentImpl::processCreateSession(JsonCpp::Value &request, JsonCpp::Value &answer) void AgentImpl::processCreateSession(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
auto accountIter = mAccountMap.find(request["account_id"].asInt()); auto accountIter = mAccountMap.find(request["account_id"].asInt());
@@ -347,7 +332,8 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
// Ensure audio provider is here // Ensure audio provider is here
PSession session = sessionIter->second; PSession session = sessionIter->second;
PDataProvider audioProvider = std::make_shared<AudioProvider>(*this, *mTerminal); PDataProvider audioProvider = std::make_shared<AudioProvider>(*this, *mTerminal);
audioProvider->setState(audioProvider->state() | static_cast<int>(StreamState::Grabbing) | static_cast<int>(StreamState::Playing)); 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(); std::string path_faults = request["path_faults"].asString();
@@ -372,7 +358,8 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
{ "specp", "32"} { "specp", "32"}
}; };
// std::string config = "-avlp on -smtnrm on -decor off -mprio off -npnt auto -voip off -enorm off -g711 on -spfrcor off -grad off -tmc on -miter 1 -trim a 10 -output json"; // std::string config = "-avlp on -smtnrm on -decor off -mprio off -npnt auto -voip off -enorm off -g711 on
-spfrcor off -grad off -tmc on -miter 1 -trim a 10 -output json";
// if (temp_path.size()) // if (temp_path.size())
// config += " -fau " + temp_path; // config += " -fau " + temp_path;
@@ -395,7 +382,7 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
Session::UserHeaders info; Session::UserHeaders info;
JsonCpp::Value& arg = request["userinfo"]; JsonCpp::Value& arg = request["userinfo"];
std::vector<std::string> keys = arg.getMemberNames(); std::vector<std::string> keys = arg.getMemberNames();
for (const std::string& k: keys) for (const std::string& k : keys)
info[k] = arg[k].asString(); info[k] = arg[k].asString();
session->setUserHeaders(info); session->setUserHeaders(info);
@@ -451,7 +438,7 @@ void AgentImpl::processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& an
Session::UserHeaders info; Session::UserHeaders info;
JsonCpp::Value& arg = request["userinfo"]; JsonCpp::Value& arg = request["userinfo"];
std::vector<std::string> keys = arg.getMemberNames(); std::vector<std::string> keys = arg.getMemberNames();
for (const std::string& k: keys) for (const std::string& k : keys)
info[k] = arg[k].asString(); info[k] = arg[k].asString();
session->setUserHeaders(info); session->setUserHeaders(info);
@@ -473,13 +460,13 @@ void AgentImpl::processDestroySession(JsonCpp::Value& request, JsonCpp::Value& a
auto sessionIter = mSessionMap.find(sessionId); auto sessionIter = mSessionMap.find(sessionId);
if (sessionIter != mSessionMap.end()) if (sessionIter != mSessionMap.end())
mSessionMap.erase(sessionIter); mSessionMap.erase(sessionIter);
//#if defined(USE_AQUA_LIBRARY) // #if defined(USE_AQUA_LIBRARY)
// closeAqua(sessionId); // closeAqua(sessionId);
//#endif // #endif
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processWaitForEvent(JsonCpp::Value &request, JsonCpp::Value &answer) void AgentImpl::processWaitForEvent(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
// Deliberately does NOT take mAgentMutex: events are produced by the worker // Deliberately does NOT take mAgentMutex: events are produced by the worker
// thread inside process(), which needs mAgentMutex. Holding it here would // thread inside process(), which needs mAgentMutex. Holding it here would
@@ -512,8 +499,7 @@ void AgentImpl::processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& an
{ {
PSession session = sessionIter->second; PSession session = sessionIter->second;
VariantMap result; VariantMap result;
session->getSessionInfo(Session::InfoOptions::Detailed, session->getSessionInfo(Session::InfoOptions::Detailed, result);
result);
if (result.exists(SessionInfo_AudioCodec)) if (result.exists(SessionInfo_AudioCodec))
answer["codec"] = result[SessionInfo_AudioCodec].asStdString(); answer["codec"] = result[SessionInfo_AudioCodec].asStdString();
@@ -566,11 +552,13 @@ void AgentImpl::processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answ
std::string pem = request["cert"].asString(); std::string pem = request["cert"].asString();
std::string::size_type pb = 0, pe = 0; std::string::size_type pb = 0, pe = 0;
while (pb != std::string::npos && pe != std::string::npos) { while (pb != std::string::npos && pe != std::string::npos)
{
pb = pem.find(BeginCertificate, pb); pb = pem.find(BeginCertificate, pb);
pe = pem.find(EndCertificate, pe); pe = pem.find(EndCertificate, pe);
if (pb != std::string::npos && pe != std::string::npos && pe > pb) { if (pb != std::string::npos && pe != std::string::npos && pe > pb)
{
std::string cert = pem.substr(pb, pe - pb + EndCertificate.size()); std::string cert = pem.substr(pb, pe - pb + EndCertificate.size());
addRootCert(ByteBuffer(cert.c_str(), cert.size())); addRootCert(ByteBuffer(cert.c_str(), cert.size()));
@@ -581,7 +569,7 @@ void AgentImpl::processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answ
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processLogMessage(JsonCpp::Value &request, JsonCpp::Value &answer) void AgentImpl::processLogMessage(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
int level = request["level"].asInt(); int level = request["level"].asInt();
std::string message = request["message"].asString(); std::string message = request["message"].asString();
@@ -600,7 +588,8 @@ void AgentImpl::stopAgentAndThread()
stop(); stop();
} }
catch (...) catch (...)
{} {
}
// Stop worker thread // Stop worker thread
if (mThread) if (mThread)
@@ -644,8 +633,8 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
std::string actionText = request["media_action"].asString(), 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 direction =
: MT::Stream::MediaDirection::Outgoing; directionText == "incoming" ? MT::Stream::MediaDirection::Incoming : MT::Stream::MediaDirection::Outgoing;
std::string path = request["path"].asString(); std::string path = request["path"].asString();
// Try to open file // Try to open file
@@ -672,8 +661,7 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
} }
} }
} }
else else if (actionText == "write")
if (actionText == "write")
{ {
if (path.empty()) if (path.empty())
{ {
@@ -693,8 +681,7 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
} }
} }
} }
else else if (actionText == "mirror")
if (actionText == "mirror")
{ {
prov->setupMirror(request["enable"].asBool()); prov->setupMirror(request["enable"].asBool());
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
@@ -707,7 +694,8 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
} }
} }
void AgentImpl::onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context, void* userTag) void AgentImpl::onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context,
void* userTag)
{ {
/*switch (direction) /*switch (direction)
{ {
@@ -718,7 +706,9 @@ void AgentImpl::onMedia(const void* data, int length, MT::Stream::MediaDirection
// Called on new incoming session; providers shoukld // Called on new incoming session; providers shoukld
#define EVENT_WITH_NAME(X) JsonCpp::Value v; v["event_name"] = X; #define EVENT_WITH_NAME(X) \
JsonCpp::Value v; \
v["event_name"] = X;
PDataProvider AgentImpl::onProviderNeeded(const std::string& name) PDataProvider AgentImpl::onProviderNeeded(const std::string& name)
{ {
@@ -856,8 +846,7 @@ void AgentImpl::onCheckFinished(PSession s, const char* description)
} }
// Called when log message must be recorded // Called when log message must be recorded
void AgentImpl::onLog(const char* /*msg*/) void AgentImpl::onLog(const char* /*msg*/) {}
{}
// Called when problem with SIP connection(s) detected // Called when problem with SIP connection(s) detected
void AgentImpl::onSipConnectionFailed() void AgentImpl::onSipConnectionFailed()
+3 -2
View File
@@ -16,7 +16,7 @@
#include <atomic> #include <atomic>
class AgentImpl: public UserAgent, public MT::Stream::MediaObserver class AgentImpl : public UserAgent, public MT::Stream::MediaObserver
{ {
protected: protected:
std::recursive_mutex mAgentMutex; std::recursive_mutex mAgentMutex;
@@ -123,7 +123,8 @@ public:
void onSipConnectionFailed() override; void onSipConnectionFailed() override;
// Called on incoming & outgoing audio for voice sessions // Called on incoming & outgoing audio for voice sessions
void onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context, void* userTag) override; void onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context,
void* userTag) override;
}; };
#endif #endif
+4 -15
View File
@@ -1,27 +1,16 @@
#include "Agent_Interface.h" #include "Agent_Interface.h"
#include "Agent_Impl.h" #include "Agent_Impl.h"
Agent::Agent() Agent::Agent() : mContext(new AgentImpl());
:mContext(new AgentImpl());
{ {
} }
Agent::~Agent() Agent::~Agent() {}
{
} void Agent::write(const std::string& command) {}
void Agent::write(const std::string& command)
{
}
bool Agent::waitForData(int milliseconds) bool Agent::waitForData(int milliseconds)
{ {
return false; return false;
} }
std::string Agent::read() std::string Agent::read() {}
{
}
+87 -93
View File
@@ -14,11 +14,9 @@ using namespace Audio;
// -------------------- AndroidEnumerator ----------------------------- // -------------------- AndroidEnumerator -----------------------------
AndroidEnumerator::AndroidEnumerator() AndroidEnumerator::AndroidEnumerator() {}
{}
AndroidEnumerator::~AndroidEnumerator() AndroidEnumerator::~AndroidEnumerator() {}
{}
int AndroidEnumerator::indexOfDefaultDevice() int AndroidEnumerator::indexOfDefaultDevice()
{ {
@@ -40,26 +38,21 @@ std::string AndroidEnumerator::nameAt(int index)
return "Audio"; return "Audio";
} }
void AndroidEnumerator::open(int direction) void AndroidEnumerator::open(int direction) {}
{}
void AndroidEnumerator::close() void AndroidEnumerator::close() {}
{}
// ----------------------- // -----------------------
OpenSLEngine::OpenSLEngine() OpenSLEngine::OpenSLEngine() {}
{}
OpenSLEngine::~OpenSLEngine() OpenSLEngine::~OpenSLEngine() {}
{}
void OpenSLEngine::open() void OpenSLEngine::open()
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
if (++mUsageCounter == 1) if (++mUsageCounter == 1)
internalOpen(); internalOpen();
} }
void OpenSLEngine::close() void OpenSLEngine::close()
@@ -72,7 +65,9 @@ void OpenSLEngine::close()
internalClose(); internalClose();
} }
#define CHECK_OPENSLES_ERROR if (resultCode != SL_RESULT_SUCCESS) throw Exception(ERR_OPENSLES, (int)resultCode) #define CHECK_OPENSLES_ERROR \
if (resultCode != SL_RESULT_SUCCESS) \
throw Exception(ERR_OPENSLES, (int)resultCode)
void OpenSLEngine::internalOpen() void OpenSLEngine::internalOpen()
{ {
@@ -118,25 +113,15 @@ OpenSLEngine& OpenSLEngine::instance()
} }
// --------------- Input implementation ---------------- // --------------- Input implementation ----------------
AndroidInputDevice::AndroidInputDevice(int devId) AndroidInputDevice::AndroidInputDevice(int devId) {}
{}
AndroidInputDevice::~AndroidInputDevice() AndroidInputDevice::~AndroidInputDevice() {}
{}
static int RateToProbe[12][2] = { static int RateToProbe[12][2] = {
{ SL_SAMPLINGRATE_16, 16000 }, {SL_SAMPLINGRATE_16, 16000}, {SL_SAMPLINGRATE_8, 8000}, {SL_SAMPLINGRATE_32, 32000},
{ SL_SAMPLINGRATE_8, 8000 }, {SL_SAMPLINGRATE_44_1, 44100}, {SL_SAMPLINGRATE_11_025, 10025}, {SL_SAMPLINGRATE_22_05, 22050},
{ SL_SAMPLINGRATE_32, 32000 }, {SL_SAMPLINGRATE_24, 24000}, {SL_SAMPLINGRATE_48, 48000}, {SL_SAMPLINGRATE_64, 64000},
{ SL_SAMPLINGRATE_44_1, 44100 }, {SL_SAMPLINGRATE_88_2, 88200}, {SL_SAMPLINGRATE_96, 96000}, {SL_SAMPLINGRATE_192, 192000}};
{ SL_SAMPLINGRATE_11_025, 10025 },
{ SL_SAMPLINGRATE_22_05, 22050 },
{ SL_SAMPLINGRATE_24, 24000 },
{ SL_SAMPLINGRATE_48, 48000 },
{ SL_SAMPLINGRATE_64, 64000 },
{ SL_SAMPLINGRATE_88_2, 88200 },
{ SL_SAMPLINGRATE_96, 96000 },
{ SL_SAMPLINGRATE_192, 192000} };
bool AndroidInputDevice::open() bool AndroidInputDevice::open()
{ {
@@ -158,7 +143,7 @@ bool AndroidInputDevice::open()
if (!opened) if (!opened)
internalClose(); internalClose();
} }
catch(...) catch (...)
{ {
opened = false; opened = false;
internalClose(); internalClose();
@@ -192,8 +177,7 @@ bool AndroidInputDevice::fakeMode()
return false; return false;
} }
void AndroidInputDevice::setFakeMode(bool fakemode) void AndroidInputDevice::setFakeMode(bool fakemode) {}
{}
int AndroidInputDevice::readBuffer(void* buffer) int AndroidInputDevice::readBuffer(void* buffer)
{ {
@@ -206,7 +190,13 @@ int AndroidInputDevice::readBuffer(void* buffer)
return mSdkRateCache.read(buffer, AUDIO_MIC_BUFFER_SIZE); return mSdkRateCache.read(buffer, AUDIO_MIC_BUFFER_SIZE);
} }
#define CHECK_SL_INTERFACE(INTF, ERR) {if (!INTF) throw Exception(ERR_OPENSLES, ERR); if (!(*INTF)) throw Exception(ERR_OPENSLES, ERR);} #define CHECK_SL_INTERFACE(INTF, ERR) \
{ \
if (!INTF) \
throw Exception(ERR_OPENSLES, ERR); \
if (!(*INTF)) \
throw Exception(ERR_OPENSLES, ERR); \
}
void AndroidInputDevice::internalOpen(int rateCode, int rate) void AndroidInputDevice::internalOpen(int rateCode, int rate)
{ {
@@ -214,33 +204,37 @@ void AndroidInputDevice::internalOpen(int rateCode, int rate)
SLuint32 nrOfChannels = 1; SLuint32 nrOfChannels = 1;
// Prepare audio source // Prepare audio source
SLDataLocator_IODevice devDescription = { SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, NULL}; SLDataLocator_IODevice devDescription = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
SLDataSource audioSource = { &devDescription, NULL }; SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
SLDataSource audioSource = {&devDescription, NULL};
// Source flags // Source flags
SLuint32 speakersFlags = nrOfChannels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER; SLuint32 speakersFlags =
nrOfChannels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER;
// Buffer queue // Buffer queue
SLDataLocator_AndroidSimpleBufferQueue queueDescription = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 }; SLDataLocator_AndroidSimpleBufferQueue queueDescription = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
// Audio format // Audio format
SLDataFormat_PCM formatDescription = { SL_DATAFORMAT_PCM, nrOfChannels, (SLuint32)rateCode, SL_PCMSAMPLEFORMAT_FIXED_16, SLDataFormat_PCM formatDescription = {SL_DATAFORMAT_PCM, nrOfChannels,
SL_PCMSAMPLEFORMAT_FIXED_16, (SLuint32)speakersFlags, SL_BYTEORDER_LITTLEENDIAN }; (SLuint32)rateCode, SL_PCMSAMPLEFORMAT_FIXED_16,
SL_PCMSAMPLEFORMAT_FIXED_16, (SLuint32)speakersFlags,
SL_BYTEORDER_LITTLEENDIAN};
SLDataSink audioSink = { &queueDescription, &formatDescription }; SLDataSink audioSink = {&queueDescription, &formatDescription};
// Create recorder // Create recorder
// Do not forget about RECORD_AUDIO permission // Do not forget about RECORD_AUDIO permission
const SLInterfaceID interfacesList[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION }; const SLInterfaceID interfacesList[2] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION};
const SLboolean interfacesRequirements[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE }; const SLboolean interfacesRequirements[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
// Get access to OpenSL engine // Get access to OpenSL engine
SLEngineItf engine_interface = OpenSLEngine::instance().getNativeEngine(); SLEngineItf engine_interface = OpenSLEngine::instance().getNativeEngine();
CHECK_SL_INTERFACE(engine_interface, -1); CHECK_SL_INTERFACE(engine_interface, -1);
resultCode = (*engine_interface)->CreateAudioRecorder( resultCode = (*engine_interface)
OpenSLEngine::instance().getNativeEngine(), ->CreateAudioRecorder(OpenSLEngine::instance().getNativeEngine(), &mRecorderObject, &audioSource,
&mRecorderObject, &audioSource, &audioSink, 2, interfacesList, interfacesRequirements); &audioSink, 2, interfacesList, interfacesRequirements);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
CHECK_SL_INTERFACE(mRecorderObject, -2); CHECK_SL_INTERFACE(mRecorderObject, -2);
@@ -258,7 +252,8 @@ void AndroidInputDevice::internalOpen(int rateCode, int rate)
CHECK_SL_INTERFACE(mRecorderInterface, -3); CHECK_SL_INTERFACE(mRecorderInterface, -3);
// Now buffer queue interface... // Now buffer queue interface...
resultCode = (*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &mRecorderBufferInterface); resultCode =
(*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &mRecorderBufferInterface);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
CHECK_SL_INTERFACE(mRecorderBufferInterface, -4); CHECK_SL_INTERFACE(mRecorderBufferInterface, -4);
@@ -276,8 +271,9 @@ void AndroidInputDevice::internalOpen(int rateCode, int rate)
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Setup buffers // Setup buffers
for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++)
(*mRecorderBufferInterface)->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + i * mBufferSize, mBufferSize); (*mRecorderBufferInterface)
->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + i * mBufferSize, mBufferSize);
// Start finally // Start finally
resultCode = (*mRecorderInterface)->SetRecordState(mRecorderInterface, SL_RECORDSTATE_RECORDING); resultCode = (*mRecorderInterface)->SetRecordState(mRecorderInterface, SL_RECORDSTATE_RECORDING);
@@ -302,8 +298,7 @@ void AndroidInputDevice::internalClose()
{ {
(*mRecorderInterface)->GetRecordState(mRecorderInterface, &state); (*mRecorderInterface)->GetRecordState(mRecorderInterface, &state);
SyncHelper::delay(1); SyncHelper::delay(1);
} } while (state == SL_RECORDSTATE_RECORDING);
while (state == SL_RECORDSTATE_RECORDING);
} }
(*mRecorderObject)->Destroy(mRecorderObject); (*mRecorderObject)->Destroy(mRecorderObject);
} }
@@ -331,9 +326,8 @@ void AndroidInputDevice::handleCallback(SLAndroidSimpleBufferQueueItf bq)
{ {
char* resampled = (char*)alloca(Format().sizeFromTime(10)); char* resampled = (char*)alloca(Format().sizeFromTime(10));
int processed = 0; int processed = 0;
int outlen = mResampler->processBuffer(mDeviceRateCache.data(), tenMsSize, processed, resampled, Format().sizeFromTime(10)); int outlen = mResampler->processBuffer(mDeviceRateCache.data(), tenMsSize, processed, resampled,
if (outlen > 0) Format().sizeFromTime(10)); if (outlen > 0) mSdkRateCache.add(resampled, (int)Format().sizeFromTime(10));
mSdkRateCache.add(resampled, (int)Format().sizeFromTime(10));
mDeviceRateCache.erase(tenMsSize); mDeviceRateCache.erase(tenMsSize);
} }
@@ -346,20 +340,22 @@ void AndroidInputDevice::handleCallback(SLAndroidSimpleBufferQueueItf bq)
} }
*/ */
// Re-enqueue used buffer // Re-enqueue used buffer
(*mRecorderBufferInterface)->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize); (*mRecorderBufferInterface)
->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize);
mRecorderBufferIndex++; mRecorderBufferIndex++;
mRecorderBufferIndex %= AUDIO_MIC_BUFFER_COUNT; mRecorderBufferIndex %= AUDIO_MIC_BUFFER_COUNT;
} }
void AndroidInputDevice::DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void *context) void AndroidInputDevice::DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context)
{ {
try try
{ {
if (context) if (context)
reinterpret_cast<AndroidInputDevice*>(context)->handleCallback(bq); reinterpret_cast<AndroidInputDevice*>(context)->handleCallback(bq);
} }
catch(...) catch (...)
{} {
}
} }
// ------------ AndroidOutputDevice ----------------- // ------------ AndroidOutputDevice -----------------
@@ -387,7 +383,7 @@ bool AndroidOutputDevice::open()
mDeviceRate = RateToProbe[rateIndex][1]; mDeviceRate = RateToProbe[rateIndex][1];
ICELogCritical(<< "Output opened with rate " << mDeviceRate << " and index " << rateIndex); ICELogCritical(<< "Output opened with rate " << mDeviceRate << " and index " << rateIndex);
} }
catch(...) catch (...)
{ {
opened = false; opened = false;
} }
@@ -414,9 +410,7 @@ bool AndroidOutputDevice::fakeMode()
return false; return false;
} }
void AndroidOutputDevice::setFakeMode(bool fakemode) void AndroidOutputDevice::setFakeMode(bool fakemode) {}
{
}
void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice) void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice)
{ {
@@ -426,12 +420,12 @@ void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice)
SLuint32 channels = 1; SLuint32 channels = 1;
// Configure audio source // Configure audio source
SLDataLocator_AndroidSimpleBufferQueue queue_desc = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 }; SLDataLocator_AndroidSimpleBufferQueue queue_desc = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
const SLInterfaceID interfacesList[] = { SL_IID_VOLUME }; const SLInterfaceID interfacesList[] = {SL_IID_VOLUME};
const SLboolean interfaceRequirements[] = { SL_BOOLEAN_FALSE }; const SLboolean interfaceRequirements[] = {SL_BOOLEAN_FALSE};
resultCode = (*OpenSLEngine::instance().getNativeEngine())->CreateOutputMix( resultCode = (*OpenSLEngine::instance().getNativeEngine())
OpenSLEngine::instance().getNativeEngine(), &mMixer, 1, interfacesList, ->CreateOutputMix(OpenSLEngine::instance().getNativeEngine(), &mMixer, 1, interfacesList,
interfaceRequirements); interfaceRequirements);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
@@ -440,30 +434,28 @@ void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice)
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Prepare mixer configuration // Prepare mixer configuration
SLuint32 speakers = SLuint32 speakers = channels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER;
channels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER;
// Describe audio format // Describe audio format
SLDataFormat_PCM pcm_format = {SL_DATAFORMAT_PCM, channels, (SLuint32) rateId, SLDataFormat_PCM pcm_format = {
SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_DATAFORMAT_PCM, channels, (SLuint32)rateId, SL_PCMSAMPLEFORMAT_FIXED_16,
speakers, SL_BYTEORDER_LITTLEENDIAN}; SL_PCMSAMPLEFORMAT_FIXED_16, speakers, SL_BYTEORDER_LITTLEENDIAN};
// Describe audio source - buffers + audio format // Describe audio source - buffers + audio format
SLDataSource audio_source = { &queue_desc, &pcm_format }; SLDataSource audio_source = {&queue_desc, &pcm_format};
// Describe audio sink // Describe audio sink
SLDataLocator_OutputMix mixer_desc = { SL_DATALOCATOR_OUTPUTMIX, mMixer }; SLDataLocator_OutputMix mixer_desc = {SL_DATALOCATOR_OUTPUTMIX, mMixer};
SLDataSink audio_sink = { &mixer_desc, NULL }; SLDataSink audio_sink = {&mixer_desc, NULL};
// Create player instance // Create player instance
const SLInterfaceID playerInterfaces[] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, const SLInterfaceID playerInterfaces[] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_VOLUME,
SL_IID_VOLUME, SL_IID_ANDROIDCONFIGURATION};
SL_IID_ANDROIDCONFIGURATION }; const SLboolean playerInterfacesReqs[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
const SLboolean playerInterfacesReqs[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
resultCode = (*OpenSLEngine::instance().getNativeEngine())->CreateAudioPlayer( resultCode = (*OpenSLEngine::instance().getNativeEngine())
OpenSLEngine::instance().getNativeEngine(), &mPlayer, ->CreateAudioPlayer(OpenSLEngine::instance().getNativeEngine(), &mPlayer, &audio_source,
&audio_source, &audio_sink, 3, playerInterfaces, playerInterfacesReqs); &audio_sink, 3, playerInterfaces, playerInterfacesReqs);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Get android config interface // Get android config interface
@@ -472,8 +464,8 @@ void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice)
if (resultCode == SL_RESULT_SUCCESS) if (resultCode == SL_RESULT_SUCCESS)
{ {
SLint32 streamType = voice ? SL_ANDROID_STREAM_VOICE : SL_ANDROID_STREAM_MEDIA; SLint32 streamType = voice ? SL_ANDROID_STREAM_VOICE : SL_ANDROID_STREAM_MEDIA;
resultCode = (*mAndroidConfig)->SetConfiguration(mAndroidConfig, SL_ANDROID_KEY_STREAM_TYPE, resultCode = (*mAndroidConfig)
&streamType, sizeof(SLint32)); ->SetConfiguration(mAndroidConfig, SL_ANDROID_KEY_STREAM_TYPE, &streamType, sizeof(SLint32));
if (resultCode != SL_RESULT_SUCCESS) if (resultCode != SL_RESULT_SUCCESS)
ICELogCritical(<< "Failed to set audio destination with error " << (unsigned)resultCode); ICELogCritical(<< "Failed to set audio destination with error " << (unsigned)resultCode);
} }
@@ -489,8 +481,7 @@ void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice)
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Get the buffer queue interface // Get the buffer queue interface
resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &mBufferQueue);
&mBufferQueue);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Setup callback // Setup callback
@@ -503,8 +494,7 @@ void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice)
mBufferIndex = 0; mBufferIndex = 0;
for (int i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++)
(*mBufferQueue)->Enqueue(mBufferQueue, mPlayBuffer.data() + i * mBufferSize, (*mBufferQueue)->Enqueue(mBufferQueue, mPlayBuffer.data() + i * mBufferSize, (SLuint32)mBufferSize);
(SLuint32)mBufferSize);
// Set the player's state to playing // Set the player's state to playing
resultCode = (*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_PLAYING); resultCode = (*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_PLAYING);
@@ -521,12 +511,15 @@ void AndroidOutputDevice::internalClose()
{ {
mInShutdown = true; mInShutdown = true;
ICELogInfo(<< "Stop player"); ICELogInfo(<< "Stop player");
if (mPlayerControl) { if (mPlayerControl)
if (*mPlayerControl) { {
if (*mPlayerControl)
{
SLuint32 state = SL_PLAYSTATE_PLAYING; SLuint32 state = SL_PLAYSTATE_PLAYING;
(*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_STOPPED); (*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_STOPPED);
while (state != SL_PLAYSTATE_STOPPED) { while (state != SL_PLAYSTATE_STOPPED)
{
(*mPlayerControl)->GetPlayState(mPlayerControl, &state); (*mPlayerControl)->GetPlayState(mPlayerControl, &state);
SyncHelper::delay(1); SyncHelper::delay(1);
} }
@@ -592,8 +585,9 @@ void AndroidOutputDevice::DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void*
{ {
reinterpret_cast<AndroidOutputDevice*>(context)->handleCallback(bq); reinterpret_cast<AndroidOutputDevice*>(context)->handleCallback(bq);
} }
catch(...) catch (...)
{} {
}
} }
#endif // TARGET_ANDROID #endif // TARGET_ANDROID
+21 -22
View File
@@ -25,9 +25,9 @@
namespace Audio namespace Audio
{ {
class AndroidEnumerator: public Enumerator class AndroidEnumerator : public Enumerator
{ {
public: public:
AndroidEnumerator(); AndroidEnumerator();
~AndroidEnumerator(); ~AndroidEnumerator();
@@ -39,12 +39,12 @@ namespace Audio
int idAt(int index); int idAt(int index);
int indexOfDefaultDevice(); int indexOfDefaultDevice();
protected: protected:
}; };
class AndroidInputDevice: public InputDevice class AndroidInputDevice : public InputDevice
{ {
public: public:
AndroidInputDevice(int devId); AndroidInputDevice(int devId);
~AndroidInputDevice(); ~AndroidInputDevice();
@@ -57,7 +57,7 @@ namespace Audio
int readBuffer(void* buffer); int readBuffer(void* buffer);
bool active() const; bool active() const;
protected: protected:
bool mActive = false; bool mActive = false;
SLObjectItf mRecorderObject = nullptr; SLObjectItf mRecorderObject = nullptr;
SLRecordItf mRecorderInterface = nullptr; SLRecordItf mRecorderInterface = nullptr;
@@ -77,11 +77,11 @@ namespace Audio
void internalClose(); void internalClose();
void handleCallback(SLAndroidSimpleBufferQueueItf bq); void handleCallback(SLAndroidSimpleBufferQueueItf bq);
static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context); static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context);
}; };
class AndroidOutputDevice: public OutputDevice class AndroidOutputDevice : public OutputDevice
{ {
public: public:
AndroidOutputDevice(int devId); AndroidOutputDevice(int devId);
~AndroidOutputDevice(); ~AndroidOutputDevice();
@@ -92,7 +92,7 @@ namespace Audio
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
protected: protected:
std::mutex mMutex; std::mutex mMutex;
int mDeviceRate = 0; int mDeviceRate = 0;
SLObjectItf mMixer = nullptr; SLObjectItf mMixer = nullptr;
@@ -111,12 +111,11 @@ namespace Audio
void handleCallback(SLAndroidSimpleBufferQueueItf bq); void handleCallback(SLAndroidSimpleBufferQueueItf bq);
static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context); static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context);
};
}; class OpenSLEngine : public OsEngine
{
class OpenSLEngine: public OsEngine public:
{
public:
OpenSLEngine(); OpenSLEngine();
~OpenSLEngine(); ~OpenSLEngine();
@@ -130,7 +129,7 @@ namespace Audio
static OpenSLEngine& instance(); static OpenSLEngine& instance();
protected: protected:
std::mutex mMutex; std::mutex mMutex;
int mUsageCounter = 0; int mUsageCounter = 0;
SLObjectItf mEngineObject = nullptr; SLObjectItf mEngineObject = nullptr;
@@ -138,8 +137,8 @@ namespace Audio
void internalOpen(); void internalOpen();
void internalClose(); void internalClose();
}; };
} } // namespace Audio
#endif // TARGET_ANDROID #endif // TARGET_ANDROID
+22 -27
View File
@@ -18,11 +18,9 @@ using namespace Audio;
// -------------------- AndroidEnumerator ----------------------------- // -------------------- AndroidEnumerator -----------------------------
AndroidEnumerator::AndroidEnumerator() AndroidEnumerator::AndroidEnumerator() {}
{}
AndroidEnumerator::~AndroidEnumerator() AndroidEnumerator::~AndroidEnumerator() {}
{}
int AndroidEnumerator::indexOfDefaultDevice() int AndroidEnumerator::indexOfDefaultDevice()
{ {
@@ -44,15 +42,12 @@ std::string AndroidEnumerator::nameAt(int index)
return "Audio"; return "Audio";
} }
void AndroidEnumerator::open(int direction) void AndroidEnumerator::open(int direction) {}
{}
void AndroidEnumerator::close() void AndroidEnumerator::close() {}
{}
// --------------- Input implementation ---------------- // --------------- Input implementation ----------------
AndroidInputDevice::AndroidInputDevice(int devId) AndroidInputDevice::AndroidInputDevice(int devId) {}
{}
AndroidInputDevice::~AndroidInputDevice() AndroidInputDevice::~AndroidInputDevice()
{ {
@@ -94,13 +89,14 @@ void AndroidInputDevice::close()
if (mRecordingStream != nullptr) if (mRecordingStream != nullptr)
{ {
mRecordingStream->close(); mRecordingStream->close();
delete mRecordingStream; mRecordingStream = nullptr; delete mRecordingStream;
mRecordingStream = nullptr;
} }
mActive = false; mActive = false;
} }
oboe::DataCallbackResult oboe::DataCallbackResult AndroidInputDevice::onAudioReady(oboe::AudioStream* audioStream, void* audioData,
AndroidInputDevice::onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames) int32_t numFrames)
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
@@ -126,8 +122,7 @@ bool AndroidInputDevice::fakeMode()
return false; return false;
} }
void AndroidInputDevice::setFakeMode(bool fakemode) void AndroidInputDevice::setFakeMode(bool fakemode) {}
{}
int AndroidInputDevice::readBuffer(void* buffer) int AndroidInputDevice::readBuffer(void* buffer)
{ {
@@ -172,7 +167,7 @@ bool AndroidOutputDevice::open()
builder.setContentType(oboe::ContentType::Speech); builder.setContentType(oboe::ContentType::Speech);
// builder.setDataCallback(this); // builder.setDataCallback(this);
builder.setCallback(this); builder.setCallback(this);
//builder.setErrorCallback(this) // builder.setErrorCallback(this)
oboe::Result rescode = builder.openStream(&mPlayingStream); oboe::Result rescode = builder.openStream(&mPlayingStream);
if (rescode != oboe::Result::OK) if (rescode != oboe::Result::OK)
@@ -211,7 +206,8 @@ void AndroidOutputDevice::close()
if (mPlayingStream != nullptr) if (mPlayingStream != nullptr)
{ {
mPlayingStream->close(); mPlayingStream->close();
delete mPlayingStream; mPlayingStream = nullptr; delete mPlayingStream;
mPlayingStream = nullptr;
} }
mEndTime = now_ms(); mEndTime = now_ms();
mActive = false; mActive = false;
@@ -230,11 +226,10 @@ bool AndroidOutputDevice::fakeMode()
return false; return false;
} }
void AndroidOutputDevice::setFakeMode(bool /*fakemode*/) void AndroidOutputDevice::setFakeMode(bool /*fakemode*/) {}
{
}
oboe::DataCallbackResult AndroidOutputDevice::onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames) oboe::DataCallbackResult AndroidOutputDevice::onAudioReady(oboe::AudioStream* audioStream, void* audioData,
int32_t numFrames)
{ {
if (mInShutdown) if (mInShutdown)
return oboe::DataCallbackResult::Stop; return oboe::DataCallbackResult::Stop;
@@ -276,10 +271,8 @@ oboe::DataCallbackResult AndroidOutputDevice::onAudioReady(oboe::AudioStream *au
mHeartbeatLast = t; mHeartbeatLast = t;
else if (t - mHeartbeatLast >= 5000.0f) else if (t - mHeartbeatLast >= 5000.0f)
{ {
ICELogInfo(<< "[spk-heartbeat] xruns=" << mXRunLast ICELogInfo(<< "[spk-heartbeat] xruns=" << mXRunLast << " bufFrames=" << audioStream->getBufferSizeInFrames()
<< " bufFrames=" << audioStream->getBufferSizeInFrames() << " burst=" << mBurstFrames << " rate=" << mDeviceRate);
<< " burst=" << mBurstFrames
<< " rate=" << mDeviceRate);
mHeartbeatLast = t; mHeartbeatLast = t;
} }
@@ -290,8 +283,10 @@ oboe::DataCallbackResult AndroidOutputDevice::onAudioReady(oboe::AudioStream *au
// stream down and calls this on its own thread after the stream is closed. Rebuild // stream down and calls this on its own thread after the stream is closed. Rebuild
// on the new default route so audio doesn't silently die mid-call. // on the new default route so audio doesn't silently die mid-call.
// See https://github.com/google/oboe/blob/master/docs/notes/disconnect.md // See https://github.com/google/oboe/blob/master/docs/notes/disconnect.md
void AndroidOutputDevice::onErrorAfterClose(oboe::AudioStream *stream, oboe::Result result) { void AndroidOutputDevice::onErrorAfterClose(oboe::AudioStream* stream, oboe::Result result)
if (result == oboe::Result::ErrorDisconnected && !mInShutdown) { {
if (result == oboe::Result::ErrorDisconnected && !mInShutdown)
{
ICELogInfo(<< "Output stream disconnected; restarting on the new route"); ICELogInfo(<< "Output stream disconnected; restarting on the new route");
// close() and open() each take mMutex internally; this callback holds none. // close() and open() each take mMutex internally; this callback holds none.
close(); close();
+19 -20
View File
@@ -25,9 +25,9 @@
namespace Audio namespace Audio
{ {
class AndroidEnumerator: public Enumerator class AndroidEnumerator : public Enumerator
{ {
public: public:
AndroidEnumerator(); AndroidEnumerator();
~AndroidEnumerator(); ~AndroidEnumerator();
@@ -39,12 +39,12 @@ namespace Audio
int idAt(int index); int idAt(int index);
int indexOfDefaultDevice(); int indexOfDefaultDevice();
protected: protected:
}; };
class AndroidInputDevice: public InputDevice, public oboe::AudioStreamCallback class AndroidInputDevice : public InputDevice, public oboe::AudioStreamCallback
{ {
public: public:
AndroidInputDevice(int devId); AndroidInputDevice(int devId);
~AndroidInputDevice(); ~AndroidInputDevice();
@@ -57,11 +57,10 @@ namespace Audio
int readBuffer(void* buffer); int readBuffer(void* buffer);
bool active() const; bool active() const;
oboe::DataCallbackResult oboe::DataCallbackResult onAudioReady(oboe::AudioStream* audioStream, void* audioData, int32_t numFrames);
onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames);
protected: protected:
bool mActive = false; bool mActive = false;
oboe::AudioStream* mRecordingStream = nullptr; oboe::AudioStream* mRecordingStream = nullptr;
PResampler mResampler; PResampler mResampler;
@@ -72,11 +71,11 @@ namespace Audio
std::condition_variable mDataCondVar; std::condition_variable mDataCondVar;
int mRecorderBufferIndex; int mRecorderBufferIndex;
std::mutex mMutex; std::mutex mMutex;
}; };
class AndroidOutputDevice: public OutputDevice, public oboe::AudioStreamCallback class AndroidOutputDevice : public OutputDevice, public oboe::AudioStreamCallback
{ {
public: public:
AndroidOutputDevice(int devId); AndroidOutputDevice(int devId);
~AndroidOutputDevice(); ~AndroidOutputDevice();
@@ -87,10 +86,10 @@ class AndroidOutputDevice: public OutputDevice, public oboe::AudioStreamCallback
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
oboe::DataCallbackResult onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames); oboe::DataCallbackResult onAudioReady(oboe::AudioStream* audioStream, void* audioData, int32_t numFrames);
void onErrorAfterClose(oboe::AudioStream *stream, oboe::Result result); void onErrorAfterClose(oboe::AudioStream* stream, oboe::Result result);
protected: protected:
std::mutex mMutex; std::mutex mMutex;
int mDeviceRate = 0; int mDeviceRate = 0;
oboe::AudioStream* mPlayingStream = nullptr; oboe::AudioStream* mPlayingStream = nullptr;
@@ -106,8 +105,8 @@ class AndroidOutputDevice: public OutputDevice, public oboe::AudioStreamCallback
// Statistics // Statistics
float mRequestedFrames = 0.0, mStartTime = 0.0, mEndTime = 0.0; float mRequestedFrames = 0.0, mStartTime = 0.0, mEndTime = 0.0;
}; };
} } // namespace Audio
#endif // TARGET_ANDROID #endif // TARGET_ANDROID
+84 -149
View File
@@ -7,7 +7,7 @@
#include "Audio_CoreAudio.h" #include "Audio_CoreAudio.h"
#include "../Helper/HL_Log.h" #include "../Helper/HL_Log.h"
//#include <qdebug.h> // #include <qdebug.h>
#include <AudioUnit/AudioUnit.h> #include <AudioUnit/AudioUnit.h>
#include <CoreAudio/CoreAudio.h> #include <CoreAudio/CoreAudio.h>
@@ -23,12 +23,11 @@ enum
static inline short Float32ToInt16(Float32 v) static inline short Float32ToInt16(Float32 v)
{ {
//assert (v <= 1.0); // assert (v <= 1.0);
int r = int(v * 32768); int r = int(v * 32768);
if (r >= 32768) if (r >= 32768)
return (short)32767; return (short)32767;
else else if (r < -32768)
if (r < -32768)
return (short)-32768; return (short)-32768;
else else
return (short)r; return (short)r;
@@ -43,7 +42,7 @@ static inline Float32 Int16ToFloat32(short v)
static inline Float32 StereoToMono(bool interleaved, Float32* buffer, int samples, int index, int channels) static inline Float32 StereoToMono(bool interleaved, Float32* buffer, int samples, int index, int channels)
{ {
Float32 sum = 0; Float32 sum = 0;
for (int i = 0; i<channels; i++) for (int i = 0; i < channels; i++)
{ {
if (!interleaved) if (!interleaved)
sum += buffer[index * channels + i]; sum += buffer[index * channels + i];
@@ -55,7 +54,7 @@ static inline Float32 StereoToMono(bool interleaved, Float32* buffer, int sample
static inline void MonoToStereo(bool interleaved, Float32 sample, Float32* buffer, int samples, int index, int channels) static inline void MonoToStereo(bool interleaved, Float32 sample, Float32* buffer, int samples, int index, int channels)
{ {
for (int i=0; i<channels; i++) for (int i = 0; i < channels; i++)
{ {
if (!interleaved) if (!interleaved)
buffer[index * channels + i] = sample; buffer[index * channels + i] = sample;
@@ -64,7 +63,7 @@ static inline void MonoToStereo(bool interleaved, Float32 sample, Float32* buffe
} }
} }
static void propertyListenerCallback(void *inUserData, AudioQueueRef queueObject, AudioQueuePropertyID propertyID) static void propertyListenerCallback(void* inUserData, AudioQueueRef queueObject, AudioQueuePropertyID propertyID)
{ {
// AudioPlayer *player = (AudioPlayer *) inUserData; // AudioPlayer *player = (AudioPlayer *) inUserData;
// gets a reference to the playback object // gets a reference to the playback object
@@ -73,10 +72,7 @@ static void propertyListenerCallback(void *inUserData, AudioQueueRef queueObject
} }
CoreAudioUnit::CoreAudioUnit() CoreAudioUnit::CoreAudioUnit() : mUnit(0) {}
:mUnit(0)
{
}
void CoreAudioUnit::open(bool voice) void CoreAudioUnit::open(bool voice)
{ {
@@ -84,9 +80,7 @@ void CoreAudioUnit::open(bool voice)
#ifdef TARGET_IOS #ifdef TARGET_IOS
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord; UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
/* We want to be able to open playback and recording streams */ /* We want to be able to open playback and recording streams */
ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);
sizeof(audioCategory),
&audioCategory);
if (ostatus != kAudioSessionNoError) if (ostatus != kAudioSessionNoError)
{ {
ICELogError(<< "Cannot set audio session to PlaybackAndRecord category, error" << ostatus); ICELogError(<< "Cannot set audio session to PlaybackAndRecord category, error" << ostatus);
@@ -101,7 +95,8 @@ void CoreAudioUnit::open(bool voice)
#ifdef TARGET_IOS #ifdef TARGET_IOS
desc.componentSubType = voice ? kAudioUnitSubType_VoiceProcessingIO : kAudioUnitSubType_RemoteIO; desc.componentSubType = voice ? kAudioUnitSubType_VoiceProcessingIO : kAudioUnitSubType_RemoteIO;
#else #else
desc.componentSubType = kAudioUnitSubType_HALOutput;//voice ? kAudioUnitSubType_VoiceProcessingIO : kAudioUnitSubType_DefaultOutput; desc.componentSubType =
kAudioUnitSubType_HALOutput; // voice ? kAudioUnitSubType_VoiceProcessingIO : kAudioUnitSubType_DefaultOutput;
#endif #endif
desc.componentManufacturer = kAudioUnitManufacturer_Apple; desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0; desc.componentFlags = 0;
@@ -153,12 +148,8 @@ AudioStreamBasicDescription CoreAudioUnit::getFormat(int scope, int bus)
void CoreAudioUnit::setFormat(AudioStreamBasicDescription& format, int scope, int bus) void CoreAudioUnit::setFormat(AudioStreamBasicDescription& format, int scope, int bus)
{ {
OSStatus ostatus = AudioUnitSetProperty(mUnit, OSStatus ostatus =
kAudioUnitProperty_StreamFormat, AudioUnitSetProperty(mUnit, kAudioUnitProperty_StreamFormat, scope, bus, &format, sizeof(format));
scope,
bus,
&format,
sizeof(format));
if (ostatus != noErr) if (ostatus != noErr)
{ {
ICELogError(<< "Cannot set stream format, error " << int(ostatus)); ICELogError(<< "Cannot set stream format, error " << int(ostatus));
@@ -181,28 +172,20 @@ bool CoreAudioUnit::getEnabled(int scope, int bus)
void CoreAudioUnit::setEnabled(bool enabled, int scope, int bus) void CoreAudioUnit::setEnabled(bool enabled, int scope, int bus)
{ {
UInt32 enable = enabled ? 1 : 0; UInt32 enable = enabled ? 1 : 0;
OSStatus ostatus = AudioUnitSetProperty(mUnit, OSStatus ostatus =
kAudioOutputUnitProperty_EnableIO, AudioUnitSetProperty(mUnit, kAudioOutputUnitProperty_EnableIO, scope, bus, &enable, sizeof(enable));
scope,
bus,
&enable,
sizeof(enable));
if (ostatus != noErr) if (ostatus != noErr)
{ {
ICELogError(<< "Cannot enable input on audio device , error " << int(ostatus)); ICELogError(<< "Cannot enable input on audio device , error " << int(ostatus));
//throw AudioException(ERR_COREAUDIO, ostatus); // throw AudioException(ERR_COREAUDIO, ostatus);
} }
} }
void CoreAudioUnit::makeCurrent(AudioDeviceID deviceId, int scope, int bus) void CoreAudioUnit::makeCurrent(AudioDeviceID deviceId, int scope, int bus)
{ {
OSStatus ostatus = AudioUnitSetProperty(mUnit, OSStatus ostatus =
kAudioOutputUnitProperty_CurrentDevice, AudioUnitSetProperty(mUnit, kAudioOutputUnitProperty_CurrentDevice, scope, bus, &deviceId, sizeof(deviceId));
scope,
bus,
&deviceId,
sizeof(deviceId));
if (ostatus != noErr) if (ostatus != noErr)
{ {
ICELogError(<< "Cannot make device " << int(deviceId) << " current, error " << ostatus); ICELogError(<< "Cannot make device " << int(deviceId) << " current, error " << ostatus);
@@ -213,12 +196,7 @@ void CoreAudioUnit::makeCurrent(AudioDeviceID deviceId, int scope, int bus)
void CoreAudioUnit::setCallback(AURenderCallbackStruct cb, int callbackType, int scope, int bus) void CoreAudioUnit::setCallback(AURenderCallbackStruct cb, int callbackType, int scope, int bus)
{ {
OSStatus ostatus = AudioUnitSetProperty(mUnit, OSStatus ostatus = AudioUnitSetProperty(mUnit, callbackType, scope, bus, &cb, sizeof(cb));
callbackType,
scope,
bus,
&cb,
sizeof(cb));
if (ostatus != noErr) if (ostatus != noErr)
{ {
ICELogError(<< "Cannot set callback pointer, error " << int(ostatus)); ICELogError(<< "Cannot set callback pointer, error " << int(ostatus));
@@ -230,7 +208,8 @@ void CoreAudioUnit::setBufferFrameSizeInMilliseconds(int ms)
{ {
#ifdef TARGET_IOS #ifdef TARGET_IOS
Float32 preferredBufferSize = Float32(ms) / 1000; // in seconds Float32 preferredBufferSize = Float32(ms) / 1000; // in seconds
OSStatus ostatus = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferSize), &preferredBufferSize); OSStatus ostatus = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration,
sizeof(preferredBufferSize), &preferredBufferSize);
if (ostatus != noErr) if (ostatus != noErr)
{ {
ICELogError(<< "Cannot set audio buffer length to " << ms << " milliseconds"); ICELogError(<< "Cannot set audio buffer length to " << ms << " milliseconds");
@@ -245,12 +224,8 @@ int CoreAudioUnit::getBufferFrameSize()
{ {
UInt32 bufsize = 0; UInt32 bufsize = 0;
UInt32 size = sizeof(UInt32); UInt32 size = sizeof(UInt32);
OSStatus ostatus = AudioUnitGetProperty(mUnit, OSStatus ostatus = AudioUnitGetProperty(mUnit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global,
kAudioDevicePropertyBufferFrameSize, Bus_Speaker, &bufsize, &size);
kAudioUnitScope_Global,
Bus_Speaker,
&bufsize,
&size);
if (ostatus != noErr) if (ostatus != noErr)
{ {
ICELogError(<< "Cannot obtain input buffer size , error " << int(ostatus)); ICELogError(<< "Cannot obtain input buffer size , error " << int(ostatus));
@@ -274,12 +249,9 @@ AudioUnit CoreAudioUnit::getHandle()
return mUnit; return mUnit;
} }
OSStatus MacDevice::outputCallback( void *inRefCon, OSStatus MacDevice::outputCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags,
AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames,
const AudioTimeStamp *inTimeStamp, AudioBufferList* ioData)
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData )
{ {
MacDevice* d = reinterpret_cast<MacDevice*>(inRefCon); MacDevice* d = reinterpret_cast<MacDevice*>(inRefCon);
if (!d) if (!d)
@@ -293,20 +265,18 @@ OSStatus MacDevice::outputCallback( void *inRefCon,
AudioBuffer& ab = ioData->mBuffers[0]; AudioBuffer& ab = ioData->mBuffers[0];
if (ab.mNumberChannels == 1) if (ab.mNumberChannels == 1)
{ {
ICELogMedia(<< "CoreAudio output callback for mono " << (int)ab.mDataByteSize << ICELogMedia(<< "CoreAudio output callback for mono " << (int)ab.mDataByteSize << " bytes");
" bytes");
memset(ab.mData, 0, ab.mDataByteSize); memset(ab.mData, 0, ab.mDataByteSize);
d->provideAudioToSpeaker(ab.mNumberChannels, ab.mData, ab.mDataByteSize); d->provideAudioToSpeaker(ab.mNumberChannels, ab.mData, ab.mDataByteSize);
for (int i=1; i<ioData->mNumberBuffers; i++) for (int i = 1; i < ioData->mNumberBuffers; i++)
memcpy(ioData->mBuffers[i].mData, ab.mData, ab.mDataByteSize); memcpy(ioData->mBuffers[i].mData, ab.mData, ab.mDataByteSize);
} }
else else
{ {
ICELogMedia(<< "CoreAudio output callback for stereo " << (int)ab.mDataByteSize << ICELogMedia(<< "CoreAudio output callback for stereo " << (int)ab.mDataByteSize << " bytes")
" bytes")
// Iterate requested buffers // Iterate requested buffers
for (unsigned i=0; i<ioData->mNumberBuffers; i++) for (unsigned i = 0; i < ioData->mNumberBuffers; i++)
{ {
unsigned channels = ioData->mBuffers[i].mNumberChannels; unsigned channels = ioData->mBuffers[i].mNumberChannels;
short* dataPtr = (short*)ioData->mBuffers[i].mData; short* dataPtr = (short*)ioData->mBuffers[i].mData;
@@ -325,32 +295,25 @@ OSStatus MacDevice::outputCallback( void *inRefCon,
static char GlobalInputBuffer[AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE]; static char GlobalInputBuffer[AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE];
OSStatus MacDevice::inputCallback(void *inRefCon, OSStatus MacDevice::inputCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags,
AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames,
const AudioTimeStamp *inTimeStamp, AudioBufferList* ioData)
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{ {
//ICELogDebug(<< "CoreAudio input callback"); // ICELogDebug(<< "CoreAudio input callback");
MacDevice* d = reinterpret_cast<MacDevice*>(inRefCon); MacDevice* d = reinterpret_cast<MacDevice*>(inRefCon);
if (!d) if (!d)
return 0; return 0;
OSStatus ostatus; OSStatus ostatus;
AudioBuffer& b = d->mInputBufferList->mBuffers[0]; AudioBuffer& b = d->mInputBufferList->mBuffers[0];
//b.mDataByteSize = 65536; // b.mDataByteSize = 65536;
b.mNumberChannels = d->mStreamFormat.mChannelsPerFrame; b.mNumberChannels = d->mStreamFormat.mChannelsPerFrame;
b.mData = NULL; b.mData = NULL;
b.mDataByteSize = inNumberFrames * d->mStreamFormat.mChannelsPerFrame; b.mDataByteSize = inNumberFrames * d->mStreamFormat.mChannelsPerFrame;
// Render the unit to get input data // Render the unit to get input data
ostatus = AudioUnitRender(d->mAudioUnit.getHandle(), ostatus = AudioUnitRender(d->mAudioUnit.getHandle(), ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames,
ioActionFlags,
inTimeStamp,
inBusNumber,
inNumberFrames,
d->mInputBufferList); d->mInputBufferList);
if (ostatus != noErr) if (ostatus != noErr)
@@ -365,10 +328,7 @@ OSStatus MacDevice::inputCallback(void *inRefCon,
} }
#ifdef TARGET_IOS #ifdef TARGET_IOS
void MacDevice::propListener(void *inClientData, void MacDevice::propListener(void* inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void* inData)
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void * inData)
{ {
MacDevice* d = reinterpret_cast<MacDevice*>(inClientData); MacDevice* d = reinterpret_cast<MacDevice*>(inClientData);
CFDictionaryRef routeDictionary; CFDictionaryRef routeDictionary;
@@ -388,10 +348,9 @@ void MacDevice::propListener(void *inClientData,
} }
// Audio route changed. Nothing to do in this implementation. // Audio route changed. Nothing to do in this implementation.
} }
void MacDevice::interruptionListener(void *inClientData, UInt32 inInterruption) void MacDevice::interruptionListener(void* inClientData, UInt32 inInterruption)
{ {
MacDevice* d = reinterpret_cast<MacDevice*>(inClientData); MacDevice* d = reinterpret_cast<MacDevice*>(inClientData);
@@ -409,19 +368,16 @@ void MacDevice::interruptionListener(void *inClientData, UInt32 inInterruption)
*/ */
/* Make sure we set the correct audio category before restarting */ /* Make sure we set the correct audio category before restarting */
audioCategory = kAudioSessionCategory_PlayAndRecord; audioCategory = kAudioSessionCategory_PlayAndRecord;
ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);
sizeof(audioCategory),
&audioCategory);
if (ostatus != kAudioSessionNoError) if (ostatus != kAudioSessionNoError)
{ {
ICELogError(<<"Cannot set the audio session category, error " << ostatus); ICELogError(<< "Cannot set the audio session category, error " << ostatus);
} }
// Start stream // Start stream
d->startStream(); d->startStream();
} }
else else if (inInterruption == kAudioSessionBeginInterruption)
if (inInterruption == kAudioSessionBeginInterruption)
{ {
d->stopStream(); d->stopStream();
} }
@@ -429,28 +385,25 @@ void MacDevice::interruptionListener(void *inClientData, UInt32 inInterruption)
#endif #endif
MacDevice::MacDevice(int devId) MacDevice::MacDevice(int devId)
:mDeviceId(devId), mCapture(false), mRender(false), mActive(false), : mDeviceId(devId), mCapture(false), mRender(false), mActive(false), mConnection(nullptr), mUsageCount(0)
mConnection(nullptr), mUsageCount(0)
{ {
mInputBuffer.setCapacity(AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE); mInputBuffer.setCapacity(AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE);
mOutputBuffer.setCapacity(AUDIO_SPK_BUFFER_SIZE); mOutputBuffer.setCapacity(AUDIO_SPK_BUFFER_SIZE);
} }
MacDevice::~MacDevice() MacDevice::~MacDevice() {}
{
}
DataConnection* MacDevice::connection() DataConnection* MacDevice::connection()
{ {
return mConnection; return mConnection;
} }
void MacDevice::setConnection(DataConnection *c) void MacDevice::setConnection(DataConnection* c)
{ {
mConnection = c; mConnection = c;
} }
void MacDevice::provideAudioToSpeaker(int channels, void *buffer, int length) void MacDevice::provideAudioToSpeaker(int channels, void* buffer, int length)
{ {
if (!mConnection) if (!mConnection)
return; return;
@@ -459,7 +412,7 @@ void MacDevice::provideAudioToSpeaker(int channels, void *buffer, int length)
return; return;
} }
void MacDevice::obtainAudioFromMic(int channels, const void *buffer, int length) void MacDevice::obtainAudioFromMic(int channels, const void* buffer, int length)
{ {
if (!mConnection) if (!mConnection)
return; return;
@@ -539,13 +492,13 @@ bool MacDevice::createUnit(bool voice)
mAudioUnit.open(voice); mAudioUnit.open(voice);
//if (mCapture != mAudioUnit.getEnabled(kAudioUnitScope_Input, Bus_Microphone)) // if (mCapture != mAudioUnit.getEnabled(kAudioUnitScope_Input, Bus_Microphone))
mAudioUnit.setEnabled(mCapture, kAudioUnitScope_Input, Bus_Microphone); mAudioUnit.setEnabled(mCapture, kAudioUnitScope_Input, Bus_Microphone);
//mAudioUnit.setEnabled(mCapture, kAudioUnitScope_Output, Bus_Microphone); // mAudioUnit.setEnabled(mCapture, kAudioUnitScope_Output, Bus_Microphone);
//if (mRender != mAudioUnit.getEnabled(kAudioUnitScope_Output, Bus_Speaker)) // if (mRender != mAudioUnit.getEnabled(kAudioUnitScope_Output, Bus_Speaker))
mAudioUnit.setEnabled(mRender, kAudioUnitScope_Output, Bus_Speaker); mAudioUnit.setEnabled(mRender, kAudioUnitScope_Output, Bus_Speaker);
//mAudioUnit.setEnabled(mRender, kAudioUnitScope_Input, Bus_Speaker); // mAudioUnit.setEnabled(mRender, kAudioUnitScope_Input, Bus_Speaker);
#ifdef TARGET_OSX #ifdef TARGET_OSX
@@ -582,8 +535,9 @@ bool MacDevice::createUnit(bool voice)
// Start resample // Start resample
mRenderResampler.start(mStreamFormat.mChannelsPerFrame, AUDIO_SAMPLERATE, mStreamFormat.mSampleRate); mRenderResampler.start(mStreamFormat.mChannelsPerFrame, AUDIO_SAMPLERATE, mStreamFormat.mSampleRate);
// Set current render format - it is format required by unit from application; scope is Input and bus is 0 (speaker) // Set current render format - it is format required by unit from application; scope is Input and bus is 0
//mAudioUnit.setFormat(mRenderInputFormat, kAudioUnitScope_Input, Bus_Speaker); // (speaker)
// mAudioUnit.setFormat(mRenderInputFormat, kAudioUnitScope_Input, Bus_Speaker);
// Configure callback // Configure callback
AURenderCallbackStruct cb; AURenderCallbackStruct cb;
@@ -597,9 +551,10 @@ bool MacDevice::createUnit(bool voice)
AURenderCallbackStruct cb; AURenderCallbackStruct cb;
cb.inputProc = inputCallback; cb.inputProc = inputCallback;
cb.inputProcRefCon = this; cb.inputProcRefCon = this;
mAudioUnit.setCallback(cb, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, Bus_Microphone); //!!! mAudioUnit.setCallback(cb, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global,
Bus_Microphone); //!!!
#ifdef TARGET_OSX #ifdef TARGET_OSX
AudioBuffer *ab; AudioBuffer* ab;
UInt32 size, bufsize; UInt32 size, bufsize;
mAudioUnit.setBufferFrameSizeInMilliseconds(COREAUDIO_BUFFER_TIME); mAudioUnit.setBufferFrameSizeInMilliseconds(COREAUDIO_BUFFER_TIME);
@@ -615,11 +570,11 @@ bool MacDevice::createUnit(bool voice)
mInputBufferList->mNumberBuffers = 1; mInputBufferList->mNumberBuffers = 1;
ab = &mInputBufferList->mBuffers[0]; ab = &mInputBufferList->mBuffers[0];
ab->mNumberChannels = mStreamFormat.mChannelsPerFrame; ab->mNumberChannels = mStreamFormat.mChannelsPerFrame;
ab->mDataByteSize = 0;//bufsize * ab->mNumberChannels * mCaptureOutputFormat.mBitsPerChannel / 8 * 8; ab->mDataByteSize = 0; // bufsize * ab->mNumberChannels * mCaptureOutputFormat.mBitsPerChannel / 8 * 8;
ab->mData = NULL;//malloc(ab->mDataByteSize); ab->mData = NULL; // malloc(ab->mDataByteSize);
if (!ab->mData) if (!ab->mData)
{ {
//ICELogError(<< "No memory for capture buffer"); // ICELogError(<< "No memory for capture buffer");
} }
#endif #endif
#ifdef TARGET_IOS #ifdef TARGET_IOS
@@ -662,8 +617,9 @@ void MacDevice::startStream()
} }
// Share audio chain // Share audio chain
//UInt32 allowMixing = YES; // UInt32 allowMixing = YES;
//AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing); // AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing),
// &allowMixing);
// Activate audio chain // Activate audio chain
AudioSessionSetActive(true); AudioSessionSetActive(true);
@@ -721,15 +677,9 @@ protected:
MacDeviceList* MacDeviceList::mInstance = NULL; MacDeviceList* MacDeviceList::mInstance = NULL;
MacDeviceList::MacDeviceList() MacDeviceList::MacDeviceList() {}
{
} MacDeviceList::~MacDeviceList() {}
MacDeviceList::~MacDeviceList()
{
}
MacDeviceList& MacDeviceList::instance() MacDeviceList& MacDeviceList::instance()
{ {
@@ -741,7 +691,7 @@ MacDeviceList& MacDeviceList::instance()
PMacDevice MacDeviceList::findDevice(int devId) PMacDevice MacDeviceList::findDevice(int devId)
{ {
Lock l(mGuard); Lock l(mGuard);
for (unsigned i=0; i<mDeviceList.size(); i++) for (unsigned i = 0; i < mDeviceList.size(); i++)
{ {
PMacDevice& d = mDeviceList[i]; PMacDevice& d = mDeviceList[i];
if (d->getId() == devId) if (d->getId() == devId)
@@ -754,8 +704,7 @@ PMacDevice MacDeviceList::findDevice(int devId)
} }
// Share list of opened devices // Share list of opened devices
MacInputDevice::MacInputDevice(int devId) MacInputDevice::MacInputDevice(int devId) : InputDevice()
:InputDevice()
{ {
// Look for MacDevice // Look for MacDevice
mDevice = MacDeviceList::instance().findDevice(devId); mDevice = MacDeviceList::instance().findDevice(devId);
@@ -784,8 +733,7 @@ Format MacInputDevice::getFormat()
return mDevice->getFormat(); return mDevice->getFormat();
} }
MacOutputDevice::MacOutputDevice(int devId) MacOutputDevice::MacOutputDevice(int devId) : OutputDevice()
:OutputDevice()
{ {
// Look for MacDevice // Look for MacDevice
mDevice = MacDeviceList::instance().findDevice(devId); mDevice = MacDeviceList::instance().findDevice(devId);
@@ -814,16 +762,9 @@ Format MacOutputDevice::getFormat()
return mDevice->getFormat(); return mDevice->getFormat();
} }
MacEnumerator::MacEnumerator() MacEnumerator::MacEnumerator() : mDefaultInput(0), mDefaultOutput(0) {}
:mDefaultInput(0), mDefaultOutput(0)
{
} MacEnumerator::~MacEnumerator() {}
MacEnumerator::~MacEnumerator()
{
}
void MacEnumerator::open(int direction) void MacEnumerator::open(int direction)
{ {
@@ -841,8 +782,7 @@ void MacEnumerator::open(int direction)
addr.mSelector = kAudioHardwarePropertyDevices; addr.mSelector = kAudioHardwarePropertyDevices;
addr.mScope = kAudioObjectPropertyScopeGlobal; addr.mScope = kAudioObjectPropertyScopeGlobal;
addr.mElement = kAudioObjectPropertyElementMaster; addr.mElement = kAudioObjectPropertyElementMaster;
osstatus = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, osstatus = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, 0, NULL, &devSize);
0, NULL, &devSize);
if (osstatus != noErr) if (osstatus != noErr)
{ {
devSize = 0; devSize = 0;
@@ -854,11 +794,11 @@ void MacEnumerator::open(int direction)
deviceIds.resize(devCount); deviceIds.resize(devCount);
// Get actual list // Get actual list
osstatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, osstatus =
0, NULL, &devSize, (void *)&deviceIds.front()); AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL, &devSize, (void*)&deviceIds.front());
if (osstatus != noErr) if (osstatus != noErr)
return; return;
for (unsigned i=0; i<deviceIds.size(); i++) for (unsigned i = 0; i < deviceIds.size(); i++)
{ {
DeviceInfo di; DeviceInfo di;
di.mId = deviceIds[i]; di.mId = deviceIds[i];
@@ -887,13 +827,12 @@ void MacEnumerator::open(int direction)
addr.mElement = kAudioObjectPropertyElementMaster; addr.mElement = kAudioObjectPropertyElementMaster;
size = sizeof(devId); size = sizeof(devId);
osstatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, osstatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL, &size, (void*)&devId);
&addr, 0, NULL, &size, (void *)&devId);
if (osstatus == noErr) if (osstatus == noErr)
{ {
std::vector<DeviceInfo>::iterator r = std::find_if(mDeviceList.begin(), mDeviceList.end(), std::vector<DeviceInfo>::iterator r = std::find_if(
[devId] (const DeviceInfo& di) { return di.mId == devId;}); mDeviceList.begin(), mDeviceList.end(), [devId](const DeviceInfo& di) { return di.mId == devId; });
if (r != mDeviceList.end()) if (r != mDeviceList.end())
mDefaultInput = r - mDeviceList.begin(); mDefaultInput = r - mDeviceList.begin();
} }
@@ -903,13 +842,11 @@ void MacEnumerator::open(int direction)
if (direction == mySpeaker) if (direction == mySpeaker)
{ {
addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice; addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
osstatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, osstatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL, &size, (void*)&devId);
&addr, 0, NULL,
&size, (void *)&devId);
if (osstatus == noErr) if (osstatus == noErr)
{ {
std::vector<DeviceInfo>::iterator r = std::find_if(mDeviceList.begin(), mDeviceList.end(), std::vector<DeviceInfo>::iterator r = std::find_if(
[devId](const DeviceInfo& di) { return di.mId == devId;}); mDeviceList.begin(), mDeviceList.end(), [devId](const DeviceInfo& di) { return di.mId == devId; });
if (r != mDeviceList.end()) if (r != mDeviceList.end())
mDefaultOutput = r - mDeviceList.begin(); mDefaultOutput = r - mDeviceList.begin();
} }
@@ -918,7 +855,7 @@ void MacEnumerator::open(int direction)
#endif #endif
} }
void MacEnumerator::getInfo(DeviceInfo &di) void MacEnumerator::getInfo(DeviceInfo& di)
{ {
UInt32 size; UInt32 size;
OSStatus osstatus; OSStatus osstatus;
@@ -930,7 +867,7 @@ void MacEnumerator::getInfo(DeviceInfo &di)
addr.mElement = kAudioObjectPropertyElementMaster; addr.mElement = kAudioObjectPropertyElementMaster;
char name[256]; char name[256];
size = sizeof(name); size = sizeof(name);
AudioObjectGetPropertyData(di.mId, &addr, 0, NULL, &size, (void *)name); AudioObjectGetPropertyData(di.mId, &addr, 0, NULL, &size, (void*)name);
di.mName = name; di.mName = name;
// Get the number of input channels // Get the number of input channels
@@ -984,7 +921,8 @@ void MacEnumerator::getInfo(DeviceInfo &di)
di.mOutputCount += buf->mBuffers[idx].mNumberChannels; di.mOutputCount += buf->mBuffers[idx].mNumberChannels;
} }
} }
free(buf); buf = NULL; free(buf);
buf = NULL;
} }
/* Get default sample rate */ /* Get default sample rate */
@@ -992,7 +930,7 @@ void MacEnumerator::getInfo(DeviceInfo &di)
addr.mScope = kAudioObjectPropertyScopeGlobal; addr.mScope = kAudioObjectPropertyScopeGlobal;
size = sizeof(Float64); size = sizeof(Float64);
Float64 sampleRate; Float64 sampleRate;
osstatus = AudioObjectGetPropertyData (di.mId, &addr, 0, NULL, &size, &sampleRate); osstatus = AudioObjectGetPropertyData(di.mId, &addr, 0, NULL, &size, &sampleRate);
if (osstatus == noErr) if (osstatus == noErr)
di.mDefaultRate = int(sampleRate); di.mDefaultRate = int(sampleRate);
@@ -1009,10 +947,7 @@ void MacEnumerator::getInfo(DeviceInfo &di)
} }
} }
void MacEnumerator::close() void MacEnumerator::close() {}
{
}
int MacEnumerator::count() int MacEnumerator::count()
{ {
+25 -32
View File
@@ -25,18 +25,16 @@
namespace Audio namespace Audio
{ {
class AudioException: public Exception class AudioException : public Exception
{ {
public: public:
AudioException(int code, OSStatus subcode) AudioException(int code, OSStatus subcode) : Exception(code, int(subcode)) {}
:Exception(code, int(subcode))
{}
}; };
//#ifndef AudioDeviceID // #ifndef AudioDeviceID
//# define AudioDeviceID unsigned // # define AudioDeviceID unsigned
//#endif // #endif
class MacEnumerator: public Enumerator class MacEnumerator : public Enumerator
{ {
public: public:
MacEnumerator(); MacEnumerator();
@@ -59,7 +57,11 @@ protected:
bool mCanChangeInputVolume; bool mCanChangeInputVolume;
int mInputCount, mOutputCount; int mInputCount, mOutputCount;
int mDefaultRate; int mDefaultRate;
DeviceInfo(): mId(0), mCanChangeOutputVolume(false), mCanChangeInputVolume(false), mInputCount(0), mOutputCount(0), mDefaultRate(16000) {} DeviceInfo()
: mId(0), mCanChangeOutputVolume(false), mCanChangeInputVolume(false), mInputCount(0), mOutputCount(0),
mDefaultRate(16000)
{
}
}; };
std::vector<DeviceInfo> mDeviceList; std::vector<DeviceInfo> mDeviceList;
unsigned mDefaultInput, mDefaultOutput; unsigned mDefaultInput, mDefaultOutput;
@@ -117,7 +119,8 @@ protected:
CoreAudioUnit mAudioUnit; CoreAudioUnit mAudioUnit;
AudioComponent mComponent; AudioComponent mComponent;
AudioStreamBasicDescription mCaptureInputFormat, mCaptureOutputFormat, mRenderInputFormat, mRenderOutputFormat, mStreamFormat; AudioStreamBasicDescription mCaptureInputFormat, mCaptureOutputFormat, mRenderInputFormat, mRenderOutputFormat,
mStreamFormat;
AudioBufferList* mInputBufferList; AudioBufferList* mInputBufferList;
DataConnection* mConnection; DataConnection* mConnection;
SpeexResampler mCaptureResampler, mRenderResampler; SpeexResampler mCaptureResampler, mRenderResampler;
@@ -130,32 +133,22 @@ protected:
void setupStreamFormat(); void setupStreamFormat();
bool createResampleUnit(AudioStreamBasicDescription format); bool createResampleUnit(AudioStreamBasicDescription format);
static OSStatus outputCallback( void *inRefCon, static OSStatus outputCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags,
AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames,
const AudioTimeStamp *inTimeStamp, AudioBufferList* ioData);
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData );
static OSStatus inputCallback(void *inRefCon, static OSStatus inputCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags,
AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames,
const AudioTimeStamp *inTimeStamp, AudioBufferList* ioData);
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData);
#ifdef TARGET_IOS #ifdef TARGET_IOS
static void propListener(void *inClientData, static void propListener(void* inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void* inData);
AudioSessionPropertyID inID, static void interruptionListener(void* inClientData, UInt32 inInterruption);
UInt32 inDataSize,
const void * inData);
static void interruptionListener(void *inClientData, UInt32 inInterruption);
#endif #endif
}; };
typedef std::shared_ptr<MacDevice> PMacDevice; typedef std::shared_ptr<MacDevice> PMacDevice;
class MacInputDevice: public InputDevice class MacInputDevice : public InputDevice
{ {
public: public:
MacInputDevice(int devId); MacInputDevice(int devId);
@@ -168,12 +161,12 @@ public:
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
int readBuffer(void* buffer); int readBuffer(void* buffer);
protected: protected:
PMacDevice mDevice; PMacDevice mDevice;
}; };
class MacOutputDevice: public OutputDevice class MacOutputDevice : public OutputDevice
{ {
public: public:
MacOutputDevice(int devId); MacOutputDevice(int devId);
@@ -190,7 +183,7 @@ protected:
PMacDevice mDevice; PMacDevice mDevice;
}; };
} } // namespace Audio
#endif // TARGET_OSX #endif // TARGET_OSX
+6 -7
View File
@@ -8,8 +8,7 @@
using namespace Audio; using namespace Audio;
DataWindow::DataWindow() DataWindow::DataWindow() {}
{}
DataWindow::~DataWindow() DataWindow::~DataWindow()
{ {
@@ -145,7 +144,7 @@ size_t DataWindow::read(void* buffer, size_t length)
if (buffer) if (buffer)
memcpy(buffer, mData, length); memcpy(buffer, mData, length);
if (length < mFilled) if (length < mFilled)
memmove(mData, mData+length, mFilled - length); memmove(mData, mData + length, mFilled - length);
mFilled -= length; mFilled -= length;
} }
return length; return length;
@@ -195,18 +194,18 @@ size_t DataWindow::moveTo(DataWindow& dst, size_t size)
std::chrono::milliseconds DataWindow::getTimeLength(const Audio::Format& fmt) const std::chrono::milliseconds DataWindow::getTimeLength(const Audio::Format& fmt) const
{ {
Lock l(mMutex); Lock l(mMutex);
return std::chrono::milliseconds(mFilled / sizeof(short) / fmt.channels() / (fmt.rate()/ 1000)); return std::chrono::milliseconds(mFilled / sizeof(short) / fmt.channels() / (fmt.rate() / 1000));
} }
void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src) void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src)
{ {
Lock lockDst(dst.mMutex), lockSrc(src.mMutex); Lock lockDst(dst.mMutex), lockSrc(src.mMutex);
dst.setCapacity(src.filled()*2); dst.setCapacity(src.filled() * 2);
short* input = (short*)src.mutableData(); short* input = (short*)src.mutableData();
short* output = (short*)dst.mutableData(); short* output = (short*)dst.mutableData();
for (int i=0; i<src.filled()/2; i++) for (int i = 0; i < src.filled() / 2; i++)
output[i*2] = output[i*2+1] = input[i]; output[i * 2] = output[i * 2 + 1] = input[i];
dst.mFilled = src.filled() * 2; dst.mFilled = src.filled() * 2;
} }
+2 -2
View File
@@ -35,7 +35,7 @@ public:
short shortAt(size_t index) const; short shortAt(size_t index) const;
void setShortAt(short value, size_t index); void setShortAt(short value, size_t index);
void zero(size_t length); void zero(size_t length);
size_t moveTo(DataWindow& dst, size_t size /* in bytes*/ ); size_t moveTo(DataWindow& dst, size_t size /* in bytes*/);
std::chrono::milliseconds getTimeLength(const Format& fmt) const; std::chrono::milliseconds getTimeLength(const Format& fmt) const;
@@ -47,5 +47,5 @@ protected:
size_t mFilled = 0; size_t mFilled = 0;
size_t mCapacity = 0; size_t mCapacity = 0;
}; };
} } // namespace Audio
#endif #endif
+7 -11
View File
@@ -13,7 +13,8 @@ using namespace Audio;
// --- DevicePair --- // --- DevicePair ---
DevicePair::DevicePair() DevicePair::DevicePair()
:mConfig(nullptr), mDelegate(nullptr), mAec(false), mAgc(false), mAecFilter(AUDIO_MIC_BUFFER_LENGTH*10, AUDIO_MIC_BUFFER_LENGTH, AUDIO_SAMPLERATE), mAgcFilter(AUDIO_CHANNELS), : mConfig(nullptr), mDelegate(nullptr), mAec(false), mAgc(false),
mAecFilter(AUDIO_MIC_BUFFER_LENGTH * 10, AUDIO_MIC_BUFFER_LENGTH, AUDIO_SAMPLERATE), mAgcFilter(AUDIO_CHANNELS),
mMonitoring(nullptr) mMonitoring(nullptr)
{ {
mInputBuffer.setCapacity(AUDIO_MIC_BUFFER_SIZE * (AUDIO_MIC_BUFFER_COUNT + 1)); mInputBuffer.setCapacity(AUDIO_MIC_BUFFER_SIZE * (AUDIO_MIC_BUFFER_COUNT + 1));
@@ -176,7 +177,6 @@ void DevicePair::onMicData(const Format& f, const void* buffer, int length)
for (int blockIndex = 0; blockIndex < blocks; blockIndex++) for (int blockIndex = 0; blockIndex < blocks; blockIndex++)
{ {
size_t wasProcessed = 0; size_t wasProcessed = 0;
size_t wasProduced = mMicResampler.resample(f.mRate, // Source rate size_t wasProduced = mMicResampler.resample(f.mRate, // Source rate
@@ -193,12 +193,11 @@ void DevicePair::onMicData(const Format& f, const void* buffer, int length)
mInputBuffer.erase((int)Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH)); mInputBuffer.erase((int)Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH));
} }
} }
void DevicePair::onSpkData(const Format& f, void* buffer, int length) void DevicePair::onSpkData(const Format& f, void* buffer, int length)
{ {
//ICELogMedia(<< "Audio::DevicePair::onSpkData() begin"); // ICELogMedia(<< "Audio::DevicePair::onSpkData() begin");
#ifdef DUMP_NATIVEOUTPUT #ifdef DUMP_NATIVEOUTPUT
if (!mNativeOutputDump) if (!mNativeOutputDump)
{ {
@@ -215,7 +214,6 @@ void DevicePair::onSpkData(const Format& f, void* buffer, int length)
int required = length - mOutputNativeData.filled(); int required = length - mOutputNativeData.filled();
if (required > 0) if (required > 0)
{ {
// Find how much blocks must be received from RTP/decoder side // Find how much blocks must be received from RTP/decoder side
int nativeBufferSize = (int)nativeFormat.sizeFromTime(AUDIO_SPK_BUFFER_LENGTH); int nativeBufferSize = (int)nativeFormat.sizeFromTime(AUDIO_SPK_BUFFER_LENGTH);
int blocks = required / nativeBufferSize; int blocks = required / nativeBufferSize;
@@ -240,10 +238,8 @@ void DevicePair::onSpkData(const Format& f, void* buffer, int length)
// Resample these 10 milliseconds it to native format // Resample these 10 milliseconds it to native format
size_t wasProcessed = 0; size_t wasProcessed = 0;
size_t wasProduced = mSpkResampler.resample(Format().mRate, size_t wasProduced = mSpkResampler.resample(Format().mRate, mOutput10msBuffer.data(),
mOutput10msBuffer.data(), mOutput10msBuffer.capacity(), wasProcessed, f.mRate,
mOutput10msBuffer.capacity(),
wasProcessed, f.mRate,
mOutputNativeData.mutableData() + mOutputNativeData.filled(), mOutputNativeData.mutableData() + mOutputNativeData.filled(),
mOutputNativeData.capacity() - mOutputNativeData.filled()); mOutputNativeData.capacity() - mOutputNativeData.filled());
mOutputNativeData.setFilled(mOutputNativeData.filled() + wasProduced); mOutputNativeData.setFilled(mOutputNativeData.filled() + wasProduced);
@@ -265,13 +261,13 @@ void DevicePair::onSpkData(const Format& f, void* buffer, int length)
if (mMonitoring) if (mMonitoring)
mMonitoring->onSpkData(f, buffer, length); mMonitoring->onSpkData(f, buffer, length);
#define AEC_FRAME_SIZE (AUDIO_CHANNELS * (AUDIO_SAMPLERATE / 1000) * AEC_FRAME_TIME * sizeof(short)) #define AEC_FRAME_SIZE (AUDIO_CHANNELS * (AUDIO_SAMPLERATE / 1000) * AEC_FRAME_TIME * sizeof(short))
// AEC filter wants frames. // AEC filter wants frames.
if (mAec) if (mAec)
{ {
int nrOfFrames = mAecSpkBuffer.filled() / AEC_FRAME_SIZE; int nrOfFrames = mAecSpkBuffer.filled() / AEC_FRAME_SIZE;
for (int frameIndex=0; frameIndex < nrOfFrames; frameIndex++) for (int frameIndex = 0; frameIndex < nrOfFrames; frameIndex++)
mAecFilter.toSpeaker(mAecSpkBuffer.mutableData() + AEC_FRAME_SIZE * frameIndex); mAecFilter.toSpeaker(mAecSpkBuffer.mutableData() + AEC_FRAME_SIZE * frameIndex);
mAecSpkBuffer.erase(nrOfFrames * AEC_FRAME_SIZE); mAecSpkBuffer.erase(nrOfFrames * AEC_FRAME_SIZE);
} }
+10 -10
View File
@@ -11,16 +11,16 @@
#include "Audio_Resampler.h" #include "Audio_Resampler.h"
#include "Audio_DataWindow.h" #include "Audio_DataWindow.h"
//#define DUMP_NATIVEOUTPUT // #define DUMP_NATIVEOUTPUT
//#define DUMP_NATIVEINPUT // #define DUMP_NATIVEINPUT
namespace Audio namespace Audio
{ {
class DevicePair: protected DataConnection class DevicePair : protected DataConnection
{ {
public: public:
class Delegate: public DataConnection class Delegate : public DataConnection
{ {
public: public:
virtual void deviceChanged(DevicePair* dpair) = 0; virtual void deviceChanged(DevicePair* dpair) = 0;
@@ -54,7 +54,7 @@ namespace Audio
Player& player(); Player& player();
protected: protected:
VariantMap* mConfig; VariantMap* mConfig;
PInputDevice mInput; PInputDevice mInput;
POutputDevice mOutput; POutputDevice mOutput;
@@ -77,9 +77,9 @@ namespace Audio
void onMicData(const Format& f, const void* buffer, int length); void onMicData(const Format& f, const void* buffer, int length);
void onSpkData(const Format& f, void* buffer, int length); void onSpkData(const Format& f, void* buffer, int length);
void processMicData(const Format& f, void* buffer, int length); void processMicData(const Format& f, void* buffer, int length);
}; };
typedef std::shared_ptr<DevicePair> PDevicePair; typedef std::shared_ptr<DevicePair> PDevicePair;
} } // namespace Audio
#endif #endif
+115 -132
View File
@@ -39,14 +39,14 @@ public:
{ {
HINSTANCE mInstance; HINSTANCE mInstance;
HRESULT (WINAPI *DirectSoundCreate8)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN); HRESULT(WINAPI* DirectSoundCreate8)(LPGUID, LPDIRECTSOUND8*, LPUNKNOWN);
HRESULT (WINAPI *DirectSoundEnumerateW)(LPDSENUMCALLBACKW, LPVOID); HRESULT(WINAPI* DirectSoundEnumerateW)(LPDSENUMCALLBACKW, LPVOID);
HRESULT (WINAPI *DirectSoundEnumerateA)(LPDSENUMCALLBACKA, LPVOID); HRESULT(WINAPI* DirectSoundEnumerateA)(LPDSENUMCALLBACKA, LPVOID);
HRESULT (WINAPI *DirectSoundCaptureCreate8)(LPGUID, LPDIRECTSOUNDCAPTURE8* , LPUNKNOWN); HRESULT(WINAPI* DirectSoundCaptureCreate8)(LPGUID, LPDIRECTSOUNDCAPTURE8*, LPUNKNOWN);
HRESULT (WINAPI *DirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW, LPVOID); HRESULT(WINAPI* DirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW, LPVOID);
HRESULT (WINAPI *DirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA, LPVOID); HRESULT(WINAPI* DirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA, LPVOID);
HRESULT (WINAPI *GetDeviceID)(LPCGUID src, LPGUID dst); HRESULT(WINAPI* GetDeviceID)(LPCGUID src, LPGUID dst);
} mRoutines; } mRoutines;
protected: protected:
@@ -57,14 +57,11 @@ protected:
DSoundInit gDSoundInit; DSoundInit gDSoundInit;
DSoundInit::DSoundInit() DSoundInit::DSoundInit() : mRefCount(0) {}
:mRefCount(0)
{
}
DSoundInit::~DSoundInit() DSoundInit::~DSoundInit()
{ {
//Unload(); // Unload();
} }
void DSoundInit::load() void DSoundInit::load()
@@ -77,16 +74,21 @@ void DSoundInit::load()
hr = ::CoInitialize(NULL); hr = ::CoInitialize(NULL);
//load the DirectSound DLL // load the DirectSound DLL
mRoutines.mInstance = ::LoadLibraryW(L"dsound.dll"); mRoutines.mInstance = ::LoadLibraryW(L"dsound.dll");
if (!mRoutines.mInstance) if (!mRoutines.mInstance)
throw std::logic_error("Cannot load dsound.dll"); throw std::logic_error("Cannot load dsound.dll");
mRoutines.DirectSoundCaptureCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUNDCAPTURE8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureCreate8"); mRoutines.DirectSoundCaptureCreate8 =
mRoutines.DirectSoundCaptureEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureEnumerateW"); (HRESULT(WINAPI*)(LPGUID, LPDIRECTSOUNDCAPTURE8*, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance,
mRoutines.DirectSoundCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCreate8"); "DirectSoundCaptureCreate8");
mRoutines.DirectSoundEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundEnumerateW"); mRoutines.DirectSoundCaptureEnumerateW = (HRESULT(WINAPI*)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(
mRoutines.GetDeviceID = (HRESULT (WINAPI*) (LPCGUID, LPGUID)) GetProcAddress(mRoutines.mInstance, "GetDeviceID"); mRoutines.mInstance, "DirectSoundCaptureEnumerateW");
mRoutines.DirectSoundCreate8 = (HRESULT(WINAPI*)(LPGUID, LPDIRECTSOUND8*, LPUNKNOWN))::GetProcAddress(
mRoutines.mInstance, "DirectSoundCreate8");
mRoutines.DirectSoundEnumerateW =
(HRESULT(WINAPI*)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundEnumerateW");
mRoutines.GetDeviceID = (HRESULT(WINAPI*)(LPCGUID, LPGUID))GetProcAddress(mRoutines.mInstance, "GetDeviceID");
} }
} }
@@ -106,10 +108,7 @@ void DSoundInit::unload()
} }
// --------------- VistaEnumerator --------------------- // --------------- VistaEnumerator ---------------------
VistaEnumerator::VistaEnumerator() VistaEnumerator::VistaEnumerator() : mCollection(NULL), mDefaultDevice(NULL), mEnumerator(NULL), mDirection(eCapture) {}
:mCollection(NULL), mDefaultDevice(NULL), mEnumerator(NULL), mDirection(eCapture)
{
}
VistaEnumerator::~VistaEnumerator() VistaEnumerator::~VistaEnumerator()
{ {
@@ -123,10 +122,8 @@ void VistaEnumerator::open(int direction)
mDirection = (direction == myMicrophone) ? eCapture : eRender; mDirection = (direction == myMicrophone) ? eCapture : eRender;
HRESULT hr = CoCreateInstance( HRESULT hr =
CLSID_MMDeviceEnumerator, NULL, CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&mEnumerator);
CLSCTX_ALL, IID_IMMDeviceEnumerator,
(void**)&mEnumerator);
if (!mEnumerator) if (!mEnumerator)
return; return;
@@ -152,7 +149,7 @@ void VistaEnumerator::close()
if (mDefaultDevice) if (mDefaultDevice)
{ {
//mDefaultDevice->Release(); // mDefaultDevice->Release();
mDefaultDevice = NULL; mDefaultDevice = NULL;
} }
@@ -162,7 +159,7 @@ void VistaEnumerator::close()
mEnumerator = NULL; mEnumerator = NULL;
} }
} }
catch(...) catch (...)
{ {
} }
} }
@@ -185,7 +182,7 @@ IMMDevice* VistaEnumerator::mapIndexToInterface(int index)
if (mmres != MMSYSERR_NOERROR) if (mmres != MMSYSERR_NOERROR)
return NULL; return NULL;
id = (WCHAR*)_alloca(idSize*sizeof(WCHAR)); id = (WCHAR*)_alloca(idSize * sizeof(WCHAR));
mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize);
} }
else else
@@ -195,7 +192,7 @@ IMMDevice* VistaEnumerator::mapIndexToInterface(int index)
if (mmres != MMSYSERR_NOERROR) if (mmres != MMSYSERR_NOERROR)
return NULL; return NULL;
id = (WCHAR*)_alloca(idSize*sizeof(WCHAR)); id = (WCHAR*)_alloca(idSize * sizeof(WCHAR));
mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize);
} }
@@ -213,7 +210,7 @@ void VistaEnumerator::enumerate()
mNameList.clear(); mNameList.clear();
int res = (int)count(); int res = (int)count();
for (int i=0; i<res; i++) for (int i = 0; i < res; i++)
{ {
IMMDevice* dev = mapIndexToInterface(i); IMMDevice* dev = mapIndexToInterface(i);
if (dev) if (dev)
@@ -258,26 +255,23 @@ int VistaEnumerator::indexOfDefaultDevice()
if (mDirection == mySpeaker) if (mDirection == mySpeaker)
{ {
if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID,
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
else else
{ {
if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID,
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
return devID; return devID;
} }
// -------------- XpEnumerator --------------- // -------------- XpEnumerator ---------------
XpEnumerator::XpEnumerator() XpEnumerator::XpEnumerator() : mDirection(-1) {}
:mDirection(-1)
{
}
XpEnumerator::~XpEnumerator() XpEnumerator::~XpEnumerator() {}
{
}
void XpEnumerator::open(int direction) void XpEnumerator::open(int direction)
{ {
@@ -285,7 +279,7 @@ void XpEnumerator::open(int direction)
if (direction == myMicrophone) if (direction == myMicrophone)
{ {
int count = waveInGetNumDevs(); int count = waveInGetNumDevs();
for (int i=0; i<count; i++) for (int i = 0; i < count; i++)
{ {
WAVEINCAPSW caps; WAVEINCAPSW caps;
if (waveInGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR) if (waveInGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR)
@@ -297,7 +291,7 @@ void XpEnumerator::open(int direction)
else else
{ {
int count = waveOutGetNumDevs(); int count = waveOutGetNumDevs();
for (int i=0; i<count; i++) for (int i = 0; i < count; i++)
{ {
WAVEOUTCAPSW caps; WAVEOUTCAPSW caps;
if (waveOutGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR) if (waveOutGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR)
@@ -308,9 +302,7 @@ void XpEnumerator::open(int direction)
} }
} }
void XpEnumerator::close() void XpEnumerator::close() {}
{
}
int XpEnumerator::count() int XpEnumerator::count()
{ {
@@ -333,12 +325,14 @@ int XpEnumerator::indexOfDefaultDevice()
if (mDirection == mySpeaker) if (mDirection == mySpeaker)
{ {
if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID,
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
else else
{ {
if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR) if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID,
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
return devID; return devID;
@@ -346,11 +340,12 @@ int XpEnumerator::indexOfDefaultDevice()
// -------- DSoundInputDevice --------------- // -------- DSoundInputDevice ---------------
DSoundInputDevice::DSoundInputDevice(GUID deviceId) DSoundInputDevice::DSoundInputDevice(GUID deviceId)
:mSimulate(false), mBufferIndex(0), mGUID(deviceId), mThreadHandle(0), mDenoiser(AUDIO_SAMPLERATE), mEnableDenoiser(true), : mSimulate(false), mBufferIndex(0), mGUID(deviceId), mThreadHandle(0), mDenoiser(AUDIO_SAMPLERATE),
mNullAudio(AUDIO_MIC_BUFFER_LENGTH, AUDIO_MIC_BUFFER_COUNT) mEnableDenoiser(true), mNullAudio(AUDIO_MIC_BUFFER_LENGTH, AUDIO_MIC_BUFFER_COUNT)
#ifdef AUDIO_DUMPINPUT #ifdef AUDIO_DUMPINPUT
,mDump(AUDIO_SAMPLERATE) ,
#endif mDump(AUDIO_SAMPLERATE)
#endif
{ {
gDSoundInit.load(); gDSoundInit.load();
@@ -359,7 +354,7 @@ DSoundInputDevice::DSoundInputDevice(GUID deviceId)
mDevice = NULL; mDevice = NULL;
mBuffer = NULL; mBuffer = NULL;
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
for (unsigned i=0; i<AUDIO_MIC_BUFFER_COUNT; i++) for (unsigned i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++)
{ {
mEventArray[i].dwOffset = (i + 1) * AUDIO_MIC_BUFFER_SIZE - 1; mEventArray[i].dwOffset = (i + 1) * AUDIO_MIC_BUFFER_SIZE - 1;
mEventSignals[i] = mEventArray[i].hEventNotify = ::CreateEvent(NULL, TRUE, FALSE, NULL); mEventSignals[i] = mEventArray[i].hEventNotify = ::CreateEvent(NULL, TRUE, FALSE, NULL);
@@ -371,7 +366,7 @@ DSoundInputDevice::~DSoundInputDevice()
{ {
close(); close();
::CloseHandle(mShutdownSignal); ::CloseHandle(mShutdownSignal);
for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++)
::CloseHandle(mEventArray[i].hEventNotify); ::CloseHandle(mEventArray[i].hEventNotify);
gDSoundInit.unload(); gDSoundInit.unload();
@@ -389,13 +384,12 @@ bool DSoundInputDevice::isSimulate() const
void DSoundInputDevice::openDevice() void DSoundInputDevice::openDevice()
{ {
ICELogInfo(<< "Open DirectSound audio input.") ICELogInfo(<< "Open DirectSound audio input.")::CoInitialize(NULL);
::CoInitialize(NULL);
Lock l(mGuard); Lock l(mGuard);
// Ensure if GUID is not null // Ensure if GUID is not null
if (IsEqualGUID(mGUID, GUID_NULL)) if (IsEqualGUID(mGUID, GUID_NULL))
{ {
setSimulate( true ); setSimulate(true);
return; return;
} }
@@ -404,24 +398,28 @@ void DSoundInputDevice::openDevice()
mDump.open(L"audioinput.wav"); mDump.open(L"audioinput.wav");
#endif #endif
mNextBuffer = 0; mDevice = NULL; IUnknown* unk = NULL; mBuffer = NULL; mNextBuffer = 0;
mDevice = NULL;
IUnknown* unk = NULL;
mBuffer = NULL;
DSoundHelper::checkComResult(gDSoundInit.mRoutines.DirectSoundCaptureCreate8(&mGUID, &mDevice, NULL)); DSoundHelper::checkComResult(gDSoundInit.mRoutines.DirectSoundCaptureCreate8(&mGUID, &mDevice, NULL));
WAVEFORMATEX wfx; WAVEFORMATEX wfx;
memset(&wfx, 0, sizeof(wfx)); memset(&wfx, 0, sizeof(wfx));
//wfx.cbSize = sizeof(wfx); // wfx.cbSize = sizeof(wfx);
wfx.nChannels = AUDIO_CHANNELS; wfx.nChannels = AUDIO_CHANNELS;
wfx.nSamplesPerSec = AUDIO_SAMPLERATE; wfx.nSamplesPerSec = AUDIO_SAMPLERATE;
wfx.wBitsPerSample = 16; wfx.wBitsPerSample = 16;
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;; wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
;
wfx.nAvgBytesPerSec = AUDIO_SAMPLERATE * 2 * AUDIO_CHANNELS; wfx.nAvgBytesPerSec = AUDIO_SAMPLERATE * 2 * AUDIO_CHANNELS;
wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.wFormatTag = WAVE_FORMAT_PCM;
DSCBUFFERDESC dsbd; DSCBUFFERDESC dsbd;
ZeroMemory(&dsbd, sizeof(dsbd)); ZeroMemory(&dsbd, sizeof(dsbd));
dsbd.dwSize = sizeof(DSCBUFFERDESC); dsbd.dwSize = sizeof(DSCBUFFERDESC);
dsbd.dwFlags = 0;//DSBCAPS_CTRLPOSITIONNOTIFY; dsbd.dwFlags = 0; // DSBCAPS_CTRLPOSITIONNOTIFY;
dsbd.dwBufferBytes = AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE; dsbd.dwBufferBytes = AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE;
dsbd.lpwfxFormat = &wfx; dsbd.lpwfxFormat = &wfx;
dsbd.dwFXCount = 0; dsbd.dwFXCount = 0;
@@ -434,7 +432,7 @@ void DSoundInputDevice::openDevice()
DSoundHelper::checkComResult(mNotifications->SetNotificationPositions(AUDIO_MIC_BUFFER_COUNT, mEventArray)); DSoundHelper::checkComResult(mNotifications->SetNotificationPositions(AUDIO_MIC_BUFFER_COUNT, mEventArray));
DSoundHelper::checkComResult(mBuffer->Start(DSCBSTART_LOOPING)); DSoundHelper::checkComResult(mBuffer->Start(DSCBSTART_LOOPING));
dscb->Release(); dscb->Release();
setSimulate( false ); setSimulate(false);
} }
bool DSoundInputDevice::open() bool DSoundInputDevice::open()
@@ -452,7 +450,7 @@ bool DSoundInputDevice::open()
void DSoundInputDevice::closeDevice() void DSoundInputDevice::closeDevice()
{ {
ICELogInfo(<<"Close DirectSound audio input"); ICELogInfo(<< "Close DirectSound audio input");
Lock l(mGuard); Lock l(mGuard);
#ifdef AUDIO_DUMPINPUT #ifdef AUDIO_DUMPINPUT
@@ -505,7 +503,7 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
// Ensure device exists // Ensure device exists
if (!mDevice) if (!mDevice)
{ {
setSimulate( true ); setSimulate(true);
return false; return false;
} }
@@ -524,9 +522,10 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
try try
{ {
if (::WaitForSingleObject(mEventArray[mNextBuffer].hEventNotify, AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_LENGTH * 4) != WAIT_OBJECT_0) if (::WaitForSingleObject(mEventArray[mNextBuffer].hEventNotify,
AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_LENGTH * 4) != WAIT_OBJECT_0)
{ {
setSimulate( true ); setSimulate(true);
return false; return false;
} }
@@ -534,7 +533,7 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
if (::WaitForMultipleObjects(AUDIO_MIC_BUFFER_COUNT, mEventSignals, TRUE, 0) != WAIT_TIMEOUT) if (::WaitForMultipleObjects(AUDIO_MIC_BUFFER_COUNT, mEventSignals, TRUE, 0) != WAIT_TIMEOUT)
{ {
// Possible overflow. Consider current buffer resulting. Reset ALL events. // Possible overflow. Consider current buffer resulting. Reset ALL events.
for (int i = 0; i<AUDIO_MIC_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++)
ResetEvent(mEventArray[i].hEventNotify); ResetEvent(mEventArray[i].hEventNotify);
} }
else else
@@ -543,11 +542,12 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
// Find the buffer start offset // Find the buffer start offset
mReadOffset = mNextBuffer * AUDIO_MIC_BUFFER_SIZE; mReadOffset = mNextBuffer * AUDIO_MIC_BUFFER_SIZE;
//increase the buffer's index // increase the buffer's index
if (++mNextBuffer == AUDIO_MIC_BUFFER_COUNT) if (++mNextBuffer == AUDIO_MIC_BUFFER_COUNT)
mNextBuffer = 0; mNextBuffer = 0;
LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0; LPVOID ptr1 = NULL, ptr2 = NULL;
DWORD len1 = 0, len2 = 0;
DSoundHelper::checkComResult(mBuffer->Lock(mReadOffset, AUDIO_MIC_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); DSoundHelper::checkComResult(mBuffer->Lock(mReadOffset, AUDIO_MIC_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0));
// Copy&Enqueue captured data to mQueue // Copy&Enqueue captured data to mQueue
@@ -574,9 +574,9 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
return true; return true;
} }
catch(...) catch (...)
{ {
setSimulate( true ); setSimulate(true);
} }
return false; return false;
} }
@@ -585,8 +585,7 @@ void DSoundInputDevice::setSimulate(bool s)
{ {
if (!mSimulate && s) if (!mSimulate && s)
mNullAudio.start(); mNullAudio.start();
else else if (mSimulate && !s)
if (mSimulate && !s)
mNullAudio.stop(); mNullAudio.stop();
mSimulate = s; mSimulate = s;
@@ -599,7 +598,7 @@ Format DSoundInputDevice::getFormat()
int DSoundInputDevice::readBuffer(void* buffer) int DSoundInputDevice::readBuffer(void* buffer)
{ {
//Lock lock(mGuard); // Lock lock(mGuard);
if (mRefCount <= 0 || isSimulate()) if (mRefCount <= 0 || isSimulate())
return 0; return 0;
@@ -642,10 +641,8 @@ void DSoundInputDevice::threadProc(void* arg)
DSoundOutputDevice::DSoundOutputDevice(GUID deviceId) DSoundOutputDevice::DSoundOutputDevice(GUID deviceId)
:mDevice(NULL), mPrimaryBuffer(NULL), mBuffer(NULL), : mDevice(NULL), mPrimaryBuffer(NULL), mBuffer(NULL), mWriteOffset(0), mPlayedSamples(0), mTotalPlayed(0), mTail(0),
mWriteOffset(0), mPlayedSamples(0), mTotalPlayed(0), mTail(0), mThreadHandle(0), mSimulate(false), mGUID(deviceId), mNullAudio(AUDIO_SPK_BUFFER_LENGTH, AUDIO_SPK_BUFFER_COUNT)
mThreadHandle(0), mSimulate(false), mGUID(deviceId),
mNullAudio(AUDIO_SPK_BUFFER_LENGTH, AUDIO_SPK_BUFFER_COUNT)
{ {
gDSoundInit.load(); gDSoundInit.load();
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
@@ -700,7 +697,7 @@ void DSoundOutputDevice::openDevice()
ICELogInfo(<< "Open DirectSound audio output"); ICELogInfo(<< "Open DirectSound audio output");
if (IsEqualGUID(mGUID, GUID_NULL)) if (IsEqualGUID(mGUID, GUID_NULL))
{ {
setSimulate( true ); setSimulate(true);
return; return;
} }
@@ -729,10 +726,10 @@ void DSoundOutputDevice::openDevice()
dsbd.dwSize = sizeof(DSBUFFERDESC); dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwBufferBytes = 0; dsbd.dwBufferBytes = 0;
dsbd.lpwfxFormat = NULL;//&wfx; dsbd.lpwfxFormat = NULL; //&wfx;
dsbd.guid3DAlgorithm = DS3DALG_DEFAULT; dsbd.guid3DAlgorithm = DS3DALG_DEFAULT;
DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mPrimaryBuffer, NULL )); DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mPrimaryBuffer, NULL));
DSBCAPS caps; DSBCAPS caps;
caps.dwSize = sizeof(caps); caps.dwSize = sizeof(caps);
caps.dwFlags = 0; caps.dwFlags = 0;
@@ -751,14 +748,16 @@ void DSoundOutputDevice::openDevice()
DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mBuffer, NULL)); DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mBuffer, NULL));
// Fill the buffer with silence // Fill the buffer with silence
LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0; LPVOID ptr1 = NULL, ptr2 = NULL;
DSoundHelper::checkComResult(mBuffer->Lock(0, AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT, &ptr1, &len1, &ptr2, &len2, 0)); DWORD len1 = 0, len2 = 0;
DSoundHelper::checkComResult(
mBuffer->Lock(0, AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT, &ptr1, &len1, &ptr2, &len2, 0));
if (len1 && ptr1) if (len1 && ptr1)
memset(ptr1, 0, len1); memset(ptr1, 0, len1);
if (len2 && ptr2) if (len2 && ptr2)
memset(ptr2, 0, len2); memset(ptr2, 0, len2);
DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2)); DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2));
DSoundHelper::checkComResult(mBuffer->Play(0,0,DSBPLAY_LOOPING)); DSoundHelper::checkComResult(mBuffer->Play(0, 0, DSBPLAY_LOOPING));
mBuffer->GetCurrentPosition(NULL, &mWriteCursor); mBuffer->GetCurrentPosition(NULL, &mWriteCursor);
} }
@@ -804,8 +803,9 @@ bool DSoundOutputDevice::getMediaFrame()
if (mConnection) if (mConnection)
mConnection->onSpkData(getFormat(), mMediaFrame, sizeof mMediaFrame); mConnection->onSpkData(getFormat(), mMediaFrame, sizeof mMediaFrame);
} }
catch(...) catch (...)
{} {
}
return true; return true;
} }
@@ -833,7 +833,8 @@ bool DSoundOutputDevice::process()
offset %= mBufferSize; offset %= mBufferSize;
// See what we can write // See what we can write
LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0; LPVOID ptr1 = NULL, ptr2 = NULL;
DWORD len1 = 0, len2 = 0;
DSoundHelper::checkComResult(mBuffer->Lock(offset, AUDIO_SPK_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); DSoundHelper::checkComResult(mBuffer->Lock(offset, AUDIO_SPK_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0));
assert(ptr2 == NULL); assert(ptr2 == NULL);
@@ -884,12 +885,12 @@ void DSoundOutputDevice::threadProc(void* arg)
impl->restoreBuffer(); impl->restoreBuffer();
impl->process(); impl->process();
} }
catch(const Exception& e) catch (const Exception& e)
{ {
ICELogError(<< "DirectSound output failed with code = " << e.code() << ", subcode = " << e.subcode()); ICELogError(<< "DirectSound output failed with code = " << e.code() << ", subcode = " << e.subcode());
impl->setSimulate(true); impl->setSimulate(true);
} }
catch(...) catch (...)
{ {
ICELogError(<< "DirectSound output failed due to unexpected exception."); ICELogError(<< "DirectSound output failed due to unexpected exception.");
impl->setSimulate(true); impl->setSimulate(true);
@@ -925,9 +926,9 @@ bool DSoundOutputDevice::closing()
return false; return false;
} }
typedef WINUSERAPI HRESULT (WINAPI *LPFNDLLGETCLASSOBJECT) (const CLSID &, const IID &, void **); typedef WINUSERAPI HRESULT(WINAPI* LPFNDLLGETCLASSOBJECT)(const CLSID&, const IID&, void**);
HRESULT DirectSoundPrivateCreate (OUT LPKSPROPERTYSET * ppKsPropertySet) HRESULT DirectSoundPrivateCreate(OUT LPKSPROPERTYSET* ppKsPropertySet)
{ {
HMODULE hLibDsound = NULL; HMODULE hLibDsound = NULL;
LPFNDLLGETCLASSOBJECT pfnDllGetClassObject = NULL; LPFNDLLGETCLASSOBJECT pfnDllGetClassObject = NULL;
@@ -938,49 +939,48 @@ HRESULT DirectSoundPrivateCreate (OUT LPKSPROPERTYSET * ppKsPropertySet)
// Load dsound.dll // Load dsound.dll
hLibDsound = LoadLibrary(TEXT("dsound.dll")); hLibDsound = LoadLibrary(TEXT("dsound.dll"));
if(!hLibDsound) if (!hLibDsound)
{ {
hr = DSERR_GENERIC; hr = DSERR_GENERIC;
} }
// Find DllGetClassObject // Find DllGetClassObject
if(SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
pfnDllGetClassObject = pfnDllGetClassObject = (LPFNDLLGETCLASSOBJECT)GetProcAddress(hLibDsound, "DllGetClassObject");
(LPFNDLLGETCLASSOBJECT)GetProcAddress ( hLibDsound, "DllGetClassObject" );
if(!pfnDllGetClassObject) if (!pfnDllGetClassObject)
{ {
hr = DSERR_GENERIC; hr = DSERR_GENERIC;
} }
} }
// Create a class factory object // Create a class factory object
if(SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = pfnDllGetClassObject (CLSID_DirectSoundPrivate, IID_IClassFactory, (LPVOID *)&pClassFactory ); hr = pfnDllGetClassObject(CLSID_DirectSoundPrivate, IID_IClassFactory, (LPVOID*)&pClassFactory);
} }
// Create the DirectSoundPrivate object and query for an IKsPropertySet // Create the DirectSoundPrivate object and query for an IKsPropertySet
// interface // interface
if(SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = pClassFactory->CreateInstance ( NULL, IID_IKsPropertySet, (LPVOID *)&pKsPropertySet ); hr = pClassFactory->CreateInstance(NULL, IID_IKsPropertySet, (LPVOID*)&pKsPropertySet);
} }
// Release the class factory // Release the class factory
if(pClassFactory) if (pClassFactory)
{ {
pClassFactory->Release(); pClassFactory->Release();
} }
// Handle final success or failure // Handle final success or failure
if(SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
*ppKsPropertySet = pKsPropertySet; *ppKsPropertySet = pKsPropertySet;
} }
else if(pKsPropertySet) else if (pKsPropertySet)
{ {
pKsPropertySet->Release(); pKsPropertySet->Release();
} }
@@ -990,7 +990,7 @@ HRESULT DirectSoundPrivateCreate (OUT LPKSPROPERTYSET * ppKsPropertySet)
return hr; return hr;
} }
BOOL GetInfoFromDSoundGUID( GUID i_sGUID, int &dwWaveID) BOOL GetInfoFromDSoundGUID(GUID i_sGUID, int& dwWaveID)
{ {
LPKSPROPERTYSET pKsPropertySet = NULL; LPKSPROPERTYSET pKsPropertySet = NULL;
HRESULT hr; HRESULT hr;
@@ -999,45 +999,34 @@ BOOL GetInfoFromDSoundGUID( GUID i_sGUID, int &dwWaveID)
PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA psDirectSoundDeviceDescription = NULL; PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA psDirectSoundDeviceDescription = NULL;
DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA sDirectSoundDeviceDescription; DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA sDirectSoundDeviceDescription;
memset(&sDirectSoundDeviceDescription,0,sizeof(sDirectSoundDeviceDescription)); memset(&sDirectSoundDeviceDescription, 0, sizeof(sDirectSoundDeviceDescription));
hr = DirectSoundPrivateCreate( &pKsPropertySet ); hr = DirectSoundPrivateCreate(&pKsPropertySet);
if(SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
ULONG ulBytesReturned = 0; ULONG ulBytesReturned = 0;
sDirectSoundDeviceDescription.DeviceId = i_sGUID; sDirectSoundDeviceDescription.DeviceId = i_sGUID;
// On the first call the final size is unknown so pass the size of the struct in order to receive // On the first call the final size is unknown so pass the size of the struct in order to receive
// "Type" and "DataFlow" values, ulBytesReturned will be populated with bytes required for struct+strings. // "Type" and "DataFlow" values, ulBytesReturned will be populated with bytes required for struct+strings.
hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, NULL, 0,
DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, &sDirectSoundDeviceDescription, sizeof(sDirectSoundDeviceDescription),
NULL, &ulBytesReturned);
0,
&sDirectSoundDeviceDescription,
sizeof(sDirectSoundDeviceDescription),
&ulBytesReturned
);
if (ulBytesReturned) if (ulBytesReturned)
{ {
// On the first call it notifies us of the required amount of memory in order to receive the strings. // On the first call it notifies us of the required amount of memory in order to receive the strings.
// Allocate the required memory, the strings will be pointed to the memory space directly after the struct. // Allocate the required memory, the strings will be pointed to the memory space directly after the struct.
psDirectSoundDeviceDescription = (PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA)new BYTE[ulBytesReturned]; psDirectSoundDeviceDescription = (PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA) new BYTE[ulBytesReturned];
*psDirectSoundDeviceDescription = sDirectSoundDeviceDescription; *psDirectSoundDeviceDescription = sDirectSoundDeviceDescription;
hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, NULL, 0,
DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, psDirectSoundDeviceDescription, ulBytesReturned, &ulBytesReturned);
NULL,
0,
psDirectSoundDeviceDescription,
ulBytesReturned,
&ulBytesReturned
);
dwWaveID = psDirectSoundDeviceDescription->WaveDeviceId; dwWaveID = psDirectSoundDeviceDescription->WaveDeviceId;
/*Description = psDirectSoundDeviceDescription->Description; /*Description = psDirectSoundDeviceDescription->Description;
Module = psDirectSoundDeviceDescription->Module; Module = psDirectSoundDeviceDescription->Module;
Interface = psDirectSoundDeviceDescription->Interface;*/ Interface = psDirectSoundDeviceDescription->Interface;*/
delete [] psDirectSoundDeviceDescription; delete[] psDirectSoundDeviceDescription;
retval = TRUE; retval = TRUE;
} }
@@ -1053,16 +1042,10 @@ struct EnumResult
GUID mGuid; GUID mGuid;
}; };
BOOL CALLBACK DSEnumCallback( BOOL CALLBACK DSEnumCallback(LPGUID lpGuid, LPCTSTR lpcstrDescription, LPCTSTR lpcstrModule, LPVOID lpContext)
LPGUID lpGuid,
LPCTSTR lpcstrDescription,
LPCTSTR lpcstrModule,
LPVOID lpContext
)
{ {
if (lpGuid) if (lpGuid)
{ {
int devId = -1; int devId = -1;
GetInfoFromDSoundGUID(*lpGuid, devId); GetInfoFromDSoundGUID(*lpGuid, devId);
EnumResult* er = (EnumResult*)lpContext; EnumResult* er = (EnumResult*)lpContext;
+7 -7
View File
@@ -22,7 +22,7 @@
#include <EndpointVolume.h> #include <EndpointVolume.h>
#include <MMDeviceAPI.h> #include <MMDeviceAPI.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
# include <Functiondiscoverykeys_devpkey.h> #include <Functiondiscoverykeys_devpkey.h>
#endif #endif
#include <vector> #include <vector>
#include <string> #include <string>
@@ -31,7 +31,7 @@
namespace Audio namespace Audio
{ {
class VistaEnumerator: public Enumerator class VistaEnumerator : public Enumerator
{ {
public: public:
VistaEnumerator(); VistaEnumerator();
@@ -56,7 +56,7 @@ protected:
IMMDevice* mapIndexToInterface(int index); IMMDevice* mapIndexToInterface(int index);
}; };
class XpEnumerator: public Enumerator class XpEnumerator : public Enumerator
{ {
public: public:
XpEnumerator(); XpEnumerator();
@@ -83,10 +83,10 @@ public:
}; };
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
typedef struct IDirectSoundNotify8 *LPDIRECTSOUNDNOTIFY8; typedef struct IDirectSoundNotify8* LPDIRECTSOUNDNOTIFY8;
#endif #endif
class DSoundInputDevice: public InputDevice class DSoundInputDevice : public InputDevice
{ {
public: public:
DSoundInputDevice(GUID deviceId); DSoundInputDevice(GUID deviceId);
@@ -136,7 +136,7 @@ protected:
static void threadProc(void* arg); static void threadProc(void* arg);
}; };
class DSoundOutputDevice: public OutputDevice class DSoundOutputDevice : public OutputDevice
{ {
public: public:
DSoundOutputDevice(GUID deviceId); DSoundOutputDevice(GUID deviceId);
@@ -182,6 +182,6 @@ protected:
static void threadProc(void* arg); static void threadProc(void* arg);
}; };
} } // namespace Audio
#endif #endif
+9 -10
View File
@@ -4,7 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef TARGET_WIN #ifdef TARGET_WIN
# include <WinSock2.h> #include <WinSock2.h>
#endif #endif
#include <assert.h> #include <assert.h>
@@ -39,9 +39,7 @@ void TimeSource::start()
#endif #endif
} }
void TimeSource::stop() void TimeSource::stop() {}
{
}
unsigned TimeSource::time() unsigned TimeSource::time()
{ {
@@ -50,15 +48,15 @@ unsigned TimeSource::time()
if (!QueryPerformanceCounter(&c)) if (!QueryPerformanceCounter(&c))
throw Exception(ERR_QPC, GetLastError()); throw Exception(ERR_QPC, GetLastError());
//find the f // find the f
double f = (double)mFreq.QuadPart / 1000.0; double f = (double)mFreq.QuadPart / 1000.0;
//find the difference // find the difference
unsigned __int64 diff = c.QuadPart - mCounter.QuadPart; unsigned __int64 diff = c.QuadPart - mCounter.QuadPart;
mCounter.QuadPart = c.QuadPart; mCounter.QuadPart = c.QuadPart;
diff = (unsigned __int64)((double)diff / f + 0.5); //get ms diff = (unsigned __int64)((double)diff / f + 0.5); // get ms
diff += mTailTime; diff += mTailTime;
if (diff > mDepthTime) if (diff > mDepthTime)
@@ -68,8 +66,8 @@ unsigned TimeSource::time()
} }
else else
{ {
mTailTime = (unsigned )(diff % (unsigned __int64)mQuantTime); mTailTime = (unsigned)(diff % (unsigned __int64)mQuantTime);
unsigned int t = (unsigned )(diff / (unsigned __int64)mQuantTime); unsigned int t = (unsigned)(diff / (unsigned __int64)mQuantTime);
return t * mQuantTime; return t * mQuantTime;
} }
#endif #endif
@@ -103,7 +101,8 @@ unsigned TimeSource::time()
// --- StubTimer --- // --- StubTimer ---
StubTimer::StubTimer(int bufferTime, int bufferCount) StubTimer::StubTimer(int bufferTime, int bufferCount)
:mBufferTime(bufferTime), mBufferCount(bufferCount), mTimeSource(bufferTime, bufferCount), mActive(false), mCurrentTime(0) : mBufferTime(bufferTime), mBufferCount(bufferCount), mTimeSource(bufferTime, bufferCount), mActive(false),
mCurrentTime(0)
{ {
#ifdef TARGET_WIN #ifdef TARGET_WIN
mStubSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mStubSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
+16 -16
View File
@@ -10,15 +10,15 @@
#include <EndpointVolume.h> #include <EndpointVolume.h>
#include <MMDeviceAPI.h> #include <MMDeviceAPI.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
# include <Functiondiscoverykeys_devpkey.h> #include <Functiondiscoverykeys_devpkey.h>
#endif #endif
#endif #endif
#if defined(TARGET_OSX) || defined(TARGET_IOS) #if defined(TARGET_OSX) || defined(TARGET_IOS)
# include <AudioUnit/AudioUnit.h> #include <AudioUnit/AudioUnit.h>
# include <AudioToolbox/AudioConverter.h> #include <AudioToolbox/AudioConverter.h>
# include <AudioToolbox/AudioServices.h> #include <AudioToolbox/AudioServices.h>
# include <mach/mach_time.h> #include <mach/mach_time.h>
#endif #endif
#include <vector> #include <vector>
@@ -27,9 +27,9 @@
namespace Audio namespace Audio
{ {
class TimeSource class TimeSource
{ {
protected: protected:
#ifdef TARGET_WIN #ifdef TARGET_WIN
LARGE_INTEGER mCounter; /// Current value from QPC. LARGE_INTEGER mCounter; /// Current value from QPC.
LARGE_INTEGER mFreq; /// Current frequency from QPC. LARGE_INTEGER mFreq; /// Current frequency from QPC.
@@ -45,25 +45,25 @@ namespace Audio
unsigned mDepthTime; /// Number of available time quants. unsigned mDepthTime; /// Number of available time quants.
unsigned mTailTime; /// Not-accounted milliseconds. unsigned mTailTime; /// Not-accounted milliseconds.
public: public:
TimeSource(int quantTime, int nrOfQuants); TimeSource(int quantTime, int nrOfQuants);
~TimeSource() = default; ~TimeSource() = default;
void start(); void start();
void stop(); void stop();
unsigned time(); unsigned time();
}; };
class StubTimer class StubTimer
{ {
public: public:
StubTimer(int bufferTime, int bufferCount); StubTimer(int bufferTime, int bufferCount);
~StubTimer(); ~StubTimer();
void start(); void start();
void stop(); void stop();
void waitForBuffer(); void waitForBuffer();
protected: protected:
unsigned mBufferTime; unsigned mBufferTime;
unsigned mBufferCount; unsigned mBufferCount;
unsigned mCurrentTime; unsigned mCurrentTime;
@@ -72,7 +72,7 @@ namespace Audio
HANDLE mStubSignal; HANDLE mStubSignal;
#endif #endif
bool mActive; bool mActive;
}; };
} } // namespace Audio
#endif #endif
+20 -38
View File
@@ -7,16 +7,16 @@
#include "../helper/HL_OsVersion.h" #include "../helper/HL_OsVersion.h"
#if !defined(USE_NULL_AUDIO) #if !defined(USE_NULL_AUDIO)
# ifdef TARGET_WIN #ifdef TARGET_WIN
# include "Audio_Wmme.h" #include "Audio_Wmme.h"
# include "Audio_DirectSound.h" #include "Audio_DirectSound.h"
# endif #endif
# ifdef TARGET_OSX #ifdef TARGET_OSX
# include "Audio_CoreAudio.h" #include "Audio_CoreAudio.h"
# endif #endif
# ifdef TARGET_ANDROID #ifdef TARGET_ANDROID
# include "Audio_Android.h" #include "Audio_Android.h"
# endif #endif
#endif #endif
#include "Audio_Helper.h" #include "Audio_Helper.h"
@@ -24,14 +24,9 @@
using namespace Audio; using namespace Audio;
Device::Device() Device::Device() : mConnection(nullptr) {}
:mConnection(nullptr)
{
}
Device::~Device() Device::~Device() {}
{
}
void Device::setConnection(DataConnection* connection) void Device::setConnection(DataConnection* connection)
@@ -44,13 +39,9 @@ DataConnection* Device::connection()
return mConnection; return mConnection;
} }
InputDevice::InputDevice() InputDevice::InputDevice() {}
{
}
InputDevice::~InputDevice() InputDevice::~InputDevice() {}
{
}
InputDevice* InputDevice::make(int devId) InputDevice* InputDevice::make(int devId)
{ {
@@ -71,13 +62,9 @@ InputDevice* InputDevice::make(int devId)
return nullptr; return nullptr;
} }
OutputDevice::OutputDevice() OutputDevice::OutputDevice() {}
{
}
OutputDevice::~OutputDevice() OutputDevice::~OutputDevice() {}
{
}
OutputDevice* OutputDevice::make(int devId) OutputDevice* OutputDevice::make(int devId)
{ {
@@ -85,7 +72,7 @@ OutputDevice* OutputDevice::make(int devId)
return new NullOutputDevice(); return new NullOutputDevice();
#else #else
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
//return new WmmeOutputDevice(index); // return new WmmeOutputDevice(index);
return new DSoundOutputDevice(DSoundHelper::deviceId2Guid(devId, false)); return new DSoundOutputDevice(DSoundHelper::deviceId2Guid(devId, false));
#endif #endif
#ifdef TARGET_OSX #ifdef TARGET_OSX
@@ -100,13 +87,9 @@ OutputDevice* OutputDevice::make(int devId)
// --- Enumerator --- // --- Enumerator ---
Enumerator::Enumerator() Enumerator::Enumerator() {}
{
}
Enumerator::~Enumerator() Enumerator::~Enumerator() {}
{
}
int Enumerator::nameToIndex(const std::tstring& name) int Enumerator::nameToIndex(const std::tstring& name)
{ {
@@ -119,7 +102,6 @@ int Enumerator::nameToIndex(const std::tstring& name)
Enumerator* Enumerator::make(bool useNull) Enumerator* Enumerator::make(bool useNull)
{ {
if (useNull) if (useNull)
return new NullEnumerator(); return new NullEnumerator();
#ifndef USE_NULL_AUDIO #ifndef USE_NULL_AUDIO
@@ -147,7 +129,7 @@ OsEngine* OsEngine::instance()
#ifdef TARGET_ANDROID #ifdef TARGET_ANDROID
return nullptr; // As we use Oboe library for now return nullptr; // As we use Oboe library for now
//return &OpenSLEngine::instance(); // return &OpenSLEngine::instance();
#endif #endif
return nullptr; return nullptr;
+14 -42
View File
@@ -27,34 +27,18 @@ struct Format
int mRate; int mRate;
int mChannels; int mChannels;
Format() Format() : mRate(AUDIO_SAMPLERATE), mChannels(AUDIO_CHANNELS) {}
:mRate(AUDIO_SAMPLERATE), mChannels(AUDIO_CHANNELS)
{}
Format(int rate, int channels) Format(int rate, int channels) : mRate(rate), mChannels(channels) {}
:mRate(rate), mChannels(channels)
{}
size_t samplesFromSize(size_t length) const size_t samplesFromSize(size_t length) const { return length / 2 / mChannels; }
{
return length / 2 / mChannels;
}
// Returns milliseconds // Returns milliseconds
float timeFromSize(size_t length) const float timeFromSize(size_t length) const { return float(samplesFromSize(length) / (mRate / 1000.0)); }
{
return float(samplesFromSize(length) / (mRate / 1000.0));
}
float sizeFromTime(size_t milliseconds) const float sizeFromTime(size_t milliseconds) const { return float((milliseconds * mRate) / 500.0 * mChannels); }
{
return float((milliseconds * mRate) / 500.0 * mChannels);
}
size_t sizeFromTime(std::chrono::milliseconds ms) const size_t sizeFromTime(std::chrono::milliseconds ms) const { return sizeFromTime(ms.count()); }
{
return sizeFromTime(ms.count());
}
std::string toString() std::string toString()
{ {
@@ -63,26 +47,13 @@ struct Format
return std::string(buffer); return std::string(buffer);
} }
bool operator == (const Format& rhs) const bool operator==(const Format& rhs) const { return mRate == rhs.mRate && mChannels == rhs.mChannels; }
{
return mRate == rhs.mRate && mChannels == rhs.mChannels;
}
bool operator != (const Format& rhs) const bool operator!=(const Format& rhs) const { return mRate != rhs.mRate || mChannels != rhs.mChannels; }
{
return mRate != rhs.mRate || mChannels != rhs.mChannels;
}
int rate() const int rate() const { return mRate; }
{
return mRate;
}
int channels() const
{
return mChannels;
}
int channels() const { return mChannels; }
}; };
class DataConnection class DataConnection
@@ -105,12 +76,13 @@ public:
virtual bool open() = 0; virtual bool open() = 0;
virtual void close() = 0; virtual void close() = 0;
virtual Format getFormat() = 0; virtual Format getFormat() = 0;
protected: protected:
DataConnection* mConnection; DataConnection* mConnection;
}; };
class InputDevice: public Device class InputDevice : public Device
{ {
public: public:
InputDevice(); InputDevice();
@@ -120,7 +92,7 @@ public:
}; };
typedef std::shared_ptr<InputDevice> PInputDevice; typedef std::shared_ptr<InputDevice> PInputDevice;
class OutputDevice: public Device class OutputDevice : public Device
{ {
public: public:
OutputDevice(); OutputDevice();
@@ -156,6 +128,6 @@ public:
static OsEngine* instance(); static OsEngine* instance();
}; };
}; }; // namespace Audio
#endif #endif
+23 -30
View File
@@ -28,9 +28,7 @@ Mixer::Stream::Stream()
mData.setCapacity(AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT); mData.setCapacity(AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT);
} }
Mixer::Stream::~Stream() Mixer::Stream::~Stream() {}
{
}
void Mixer::Stream::setSsrc(unsigned ssrc) void Mixer::Stream::setSsrc(unsigned ssrc)
{ {
@@ -73,7 +71,9 @@ void Mixer::Stream::addPcm(int rate, const void* input, int length)
if (mTempBuffer.size() < outputSize) if (mTempBuffer.size() < outputSize)
mTempBuffer.resize(outputSize); mTempBuffer.resize(outputSize);
Resampler* resampler = (rate == 8000) ? &mResampler8 : ((rate == 16000) ? &mResampler16 : ((rate == 32000) ? &mResampler32 : &mResampler48)); Resampler* resampler = (rate == 8000)
? &mResampler8
: ((rate == 16000) ? &mResampler16 : ((rate == 32000) ? &mResampler32 : &mResampler48));
size_t inputProcessed = 0; size_t inputProcessed = 0;
resampler->processBuffer(input, length, inputProcessed, mTempBuffer.mutableData(), outputSize); resampler->processBuffer(input, length, inputProcessed, mTempBuffer.mutableData(), outputSize);
// inputProcessed result value is ignored here - rate will be 8/16/32/48k, inputProcessed is equal to length // inputProcessed result value is ignored here - rate will be 8/16/32/48k, inputProcessed is equal to length
@@ -88,14 +88,12 @@ Mixer::Mixer()
mOutput.setCapacity(32768); mOutput.setCapacity(32768);
} }
Mixer::~Mixer() Mixer::~Mixer() {}
{
}
void Mixer::unregisterChannel(void* channel) void Mixer::unregisterChannel(void* channel)
{ {
Lock l(mMutex); Lock l(mMutex);
for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT; i++) for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++)
{ {
Stream& c = mChannelList[i]; Stream& c = mChannelList[i];
if (c.active() && c.context() == channel) if (c.active() && c.context() == channel)
@@ -110,7 +108,7 @@ void Mixer::unregisterChannel(void* channel)
void Mixer::clear(void* context, unsigned ssrc) void Mixer::clear(void* context, unsigned ssrc)
{ {
Lock l(mMutex); Lock l(mMutex);
for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT; i++) for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++)
{ {
Stream& c = mChannelList[i]; Stream& c = mChannelList[i];
if (c.active() && c.context() == context && c.ssrc() == ssrc) if (c.active() && c.context() == context && c.ssrc() == ssrc)
@@ -127,7 +125,7 @@ Mixer::Stream* Mixer::allocateChannel(void* context, unsigned ssrc)
// Allocate new channel // Allocate new channel
Lock l(mMutex); Lock l(mMutex);
Stream* channel; Stream* channel;
for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT;i++) for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++)
{ {
channel = &mChannelList[i]; channel = &mChannelList[i];
if (!channel->active()) if (!channel->active())
@@ -143,9 +141,7 @@ Mixer::Stream* Mixer::allocateChannel(void* context, unsigned ssrc)
return NULL; return NULL;
} }
void Mixer::addPcm(void* context, unsigned ssrc, void Mixer::addPcm(void* context, unsigned ssrc, const void* inputData, int inputLength, int inputRate, bool fadeOut)
const void* inputData, int inputLength,
int inputRate, bool fadeOut)
{ {
assert(inputRate == 8000 || inputRate == 16000 || inputRate == 32000); assert(inputRate == 8000 || inputRate == 16000 || inputRate == 32000);
@@ -155,7 +151,7 @@ void Mixer::addPcm(void* context, unsigned ssrc,
// Locate a channel // Locate a channel
Stream* channel = NULL; Stream* channel = NULL;
for (i=0; i<AUDIO_MIX_CHANNEL_COUNT && !channel; i++) for (i = 0; i < AUDIO_MIX_CHANNEL_COUNT && !channel; i++)
{ {
Stream& c = mChannelList[i]; Stream& c = mChannelList[i];
if (c.active() && c.context() == context && c.ssrc() == ssrc) if (c.active() && c.context() == context && c.ssrc() == ssrc)
@@ -181,7 +177,7 @@ void Mixer::addPcm(void* context, unsigned ssrc, Audio::DataWindow& w, int rate,
// Locate a channel // Locate a channel
Stream* channel = NULL; Stream* channel = NULL;
for (i=0; i<AUDIO_MIX_CHANNEL_COUNT && !channel; i++) for (i = 0; i < AUDIO_MIX_CHANNEL_COUNT && !channel; i++)
{ {
Stream& c = mChannelList[i]; Stream& c = mChannelList[i];
if (c.active() && c.context() == context && c.ssrc() == ssrc) if (c.active() && c.context() == context && c.ssrc() == ssrc)
@@ -195,7 +191,7 @@ void Mixer::addPcm(void* context, unsigned ssrc, Audio::DataWindow& w, int rate,
} }
channel->addPcm(rate, w.data(), w.filled()); channel->addPcm(rate, w.data(), w.filled());
//ICELogSpecial(<<"Mixer stream " << int(this) << " has " << w.filled() << " bytes"); // ICELogSpecial(<<"Mixer stream " << int(this) << " has " << w.filled() << " bytes");
} }
void Mixer::mix() void Mixer::mix()
@@ -217,7 +213,7 @@ void Mixer::mix()
// Build active channel map // Build active channel map
Stream* channelList[AUDIO_MIX_CHANNEL_COUNT]; Stream* channelList[AUDIO_MIX_CHANNEL_COUNT];
int activeCounter = 0; int activeCounter = 0;
for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT; i++) for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++)
if (mChannelList[i].active()) if (mChannelList[i].active())
channelList[activeCounter++] = &mChannelList[i]; channelList[activeCounter++] = &mChannelList[i];
@@ -239,10 +235,9 @@ void Mixer::mix()
// Erase copied audio samples // Erase copied audio samples
audio.data().erase(audio.data().filled()); audio.data().erase(audio.data().filled());
//ICELogSpecial(<<"Length of mixer stream " << audio.data().filled()); // ICELogSpecial(<<"Length of mixer stream " << audio.data().filled());
} }
else else if (activeCounter == 2)
if (activeCounter == 2)
{ {
Stream& audio1 = *channelList[0]; Stream& audio1 = *channelList[0];
Stream& audio2 = *channelList[1]; Stream& audio2 = *channelList[1];
@@ -257,7 +252,7 @@ void Mixer::mix()
available = maxsize - filled; available = maxsize - filled;
short sample = 0; short sample = 0;
for (int i=0; i<available; i++) for (int i = 0; i < available; i++)
{ {
short sample1 = filled1 > i ? audio1.data().shortAt(i) : 0; short sample1 = filled1 > i ? audio1.data().shortAt(i) : 0;
short sample2 = filled2 > i ? audio2.data().shortAt(i) : 0; short sample2 = filled2 > i ? audio2.data().shortAt(i) : 0;
@@ -265,8 +260,8 @@ void Mixer::mix()
mOutput.add(sample); mOutput.add(sample);
} }
audio1.data().erase(available*2); audio1.data().erase(available * 2);
audio2.data().erase(available*2); audio2.data().erase(available * 2);
} }
else else
{ {
@@ -275,7 +270,7 @@ void Mixer::mix()
sample = 0; sample = 0;
sourceCounter = 0; sourceCounter = 0;
processed = 0; processed = 0;
for (int i=0; i<activeCounter; i++) for (int i = 0; i < activeCounter; i++)
{ {
Stream& audio = *channelList[i]; Stream& audio = *channelList[i];
processed++; processed++;
@@ -301,14 +296,13 @@ void Mixer::mix()
mOutput.add(outputBuffer, outputCounter * 2); mOutput.add(outputBuffer, outputCounter * 2);
outputCounter = 0; outputCounter = 0;
} }
} } while (sourceCounter);
while (sourceCounter);
processed = 0; processed = 0;
for (int i=0; i<activeCounter; i++) for (int i = 0; i < activeCounter; i++)
{ {
Stream& audio = *channelList[i]; Stream& audio = *channelList[i];
audio.data().erase(sampleCounter*2); audio.data().erase(sampleCounter * 2);
} }
} }
} }
@@ -321,7 +315,7 @@ int Mixer::getPcm(void* outputData, int outputLength)
if (mOutput.filled() < outputLength) if (mOutput.filled() < outputLength)
mix(); mix();
//ICELogSpecial(<<"Mixer has " << mOutput.filled() << " available bytes"); // ICELogSpecial(<<"Mixer has " << mOutput.filled() << " available bytes");
memset(outputData, 0, outputLength); memset(outputData, 0, outputLength);
return mOutput.read(outputData, outputLength); return mOutput.read(outputData, outputLength);
} }
@@ -354,4 +348,3 @@ int Mixer::available()
{ {
return mOutput.filled(); return mOutput.filled();
} }
+7 -10
View File
@@ -16,17 +16,14 @@
namespace Audio namespace Audio
{ {
class Mixer class Mixer
{ {
protected: protected:
class Stream class Stream
{ {
protected: protected:
DataWindow mData; DataWindow mData;
Resampler mResampler8, Resampler mResampler8, mResampler16, mResampler32, mResampler48;
mResampler16,
mResampler32,
mResampler48;
bool mActive; bool mActive;
void* mContext; void* mContext;
unsigned mSSRC; unsigned mSSRC;
@@ -55,7 +52,7 @@ namespace Audio
void mix(); void mix();
Stream* allocateChannel(void* context, unsigned ssrc); Stream* allocateChannel(void* context, unsigned ssrc);
public: public:
Mixer(); Mixer();
~Mixer(); ~Mixer();
@@ -66,7 +63,7 @@ namespace Audio
int getPcm(void* outputData, int outputLength); int getPcm(void* outputData, int outputLength);
int mixAndGetPcm(Audio::DataWindow& output); int mixAndGetPcm(Audio::DataWindow& output);
int available(); int available();
}; };
} //end of namespace } // namespace Audio
#endif #endif
+20 -27
View File
@@ -7,8 +7,8 @@
using namespace Audio; using namespace Audio;
using namespace std::chrono_literals; using namespace std::chrono_literals;
NullTimer::NullTimer(std::chrono::milliseconds interval, Delegate *delegate, const char* name) NullTimer::NullTimer(std::chrono::milliseconds interval, Delegate* delegate, const char* name)
:mShutdown(false), mDelegate(delegate), mInterval(interval), mThreadName(name) : mShutdown(false), mDelegate(delegate), mInterval(interval), mThreadName(name)
{ {
start(); start();
} }
@@ -49,15 +49,13 @@ void NullTimer::run()
// Sleep for mInterval - mTail milliseconds // Sleep for mInterval - mTail milliseconds
std::this_thread::sleep_for(mInterval - mTail); std::this_thread::sleep_for(mInterval - mTail);
mTail = mTail + std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - timestamp); mTail =
mTail + std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - timestamp);
} }
} }
// --------------------- NullInputDevice ------------------------- // --------------------- NullInputDevice -------------------------
NullInputDevice::NullInputDevice() NullInputDevice::NullInputDevice() : mBuffer(nullptr) {}
:mBuffer(nullptr)
{
}
NullInputDevice::~NullInputDevice() NullInputDevice::~NullInputDevice()
{ {
@@ -69,7 +67,8 @@ bool NullInputDevice::open()
ICELogInfo(<< "Starting NullInputDevice for " << AUDIO_MIC_BUFFER_LENGTH << "ms buffers"); ICELogInfo(<< "Starting NullInputDevice for " << AUDIO_MIC_BUFFER_LENGTH << "ms buffers");
mBuffer = malloc(AUDIO_MIC_BUFFER_SIZE); mBuffer = malloc(AUDIO_MIC_BUFFER_SIZE);
memset(mBuffer, 0, AUDIO_MIC_BUFFER_SIZE); memset(mBuffer, 0, AUDIO_MIC_BUFFER_SIZE);
mTimeCounter = 0; mDataCounter = 0; mTimeCounter = 0;
mDataCounter = 0;
// Creation of timer starts it also. So first onTimerSignal can come even before open() returns. // Creation of timer starts it also. So first onTimerSignal can come even before open() returns.
mTimer = std::make_shared<NullTimer>(std::chrono::milliseconds(AUDIO_MIC_BUFFER_LENGTH), this, "null_mic"); mTimer = std::make_shared<NullTimer>(std::chrono::milliseconds(AUDIO_MIC_BUFFER_LENGTH), this, "null_mic");
@@ -85,7 +84,7 @@ void NullInputDevice::internalClose()
free(mBuffer); free(mBuffer);
mBuffer = nullptr; mBuffer = nullptr;
} }
ICELogInfo( << "Pseudocaptured " << mTimeCounter << " milliseconds , " << mDataCounter << " bytes."); ICELogInfo(<< "Pseudocaptured " << mTimeCounter << " milliseconds , " << mDataCounter << " bytes.");
} }
void NullInputDevice::close() void NullInputDevice::close()
@@ -95,7 +94,7 @@ void NullInputDevice::close()
Format NullInputDevice::getFormat() Format NullInputDevice::getFormat()
{ {
assert (Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH) == AUDIO_MIC_BUFFER_SIZE); assert(Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH) == AUDIO_MIC_BUFFER_SIZE);
return {}; // Return library-define default format return {}; // Return library-define default format
} }
@@ -109,10 +108,7 @@ void NullInputDevice::onTimerSignal(NullTimer& timer)
} }
// --------------------- NullOutputDevice -------------------------- // --------------------- NullOutputDevice --------------------------
NullOutputDevice::NullOutputDevice() NullOutputDevice::NullOutputDevice() : mBuffer(nullptr) {}
:mBuffer(nullptr)
{
}
NullOutputDevice::~NullOutputDevice() NullOutputDevice::~NullOutputDevice()
{ {
@@ -122,7 +118,8 @@ NullOutputDevice::~NullOutputDevice()
bool NullOutputDevice::open() bool NullOutputDevice::open()
{ {
mTimeCounter = 0; mDataCounter = 0; mTimeCounter = 0;
mDataCounter = 0;
mBuffer = malloc(AUDIO_SPK_BUFFER_SIZE); mBuffer = malloc(AUDIO_SPK_BUFFER_SIZE);
// Creation of timer starts it also. So first onSpkData() can come before open() returns even. // Creation of timer starts it also. So first onSpkData() can come before open() returns even.
mTimer = std::make_shared<NullTimer>(std::chrono::milliseconds(AUDIO_SPK_BUFFER_LENGTH), this, "null_spk"); mTimer = std::make_shared<NullTimer>(std::chrono::milliseconds(AUDIO_SPK_BUFFER_LENGTH), this, "null_spk");
@@ -132,7 +129,8 @@ bool NullOutputDevice::open()
void NullOutputDevice::internalClose() void NullOutputDevice::internalClose()
{ {
mTimer.reset(); mTimer.reset();
free(mBuffer); mBuffer = nullptr; free(mBuffer);
mBuffer = nullptr;
ICELogInfo(<< "Pseudoplayed " << mTimeCounter << " milliseconds, " << mDataCounter << " bytes."); ICELogInfo(<< "Pseudoplayed " << mTimeCounter << " milliseconds, " << mDataCounter << " bytes.");
} }
@@ -143,11 +141,11 @@ void NullOutputDevice::close()
Format NullOutputDevice::getFormat() Format NullOutputDevice::getFormat()
{ {
assert (Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH) == AUDIO_SPK_BUFFER_SIZE); assert(Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH) == AUDIO_SPK_BUFFER_SIZE);
return Format(); return Format();
} }
void NullOutputDevice::onTimerSignal(NullTimer &timer) void NullOutputDevice::onTimerSignal(NullTimer& timer)
{ {
mTimeCounter += AUDIO_SPK_BUFFER_LENGTH; mTimeCounter += AUDIO_SPK_BUFFER_LENGTH;
mDataCounter += AUDIO_SPK_BUFFER_SIZE; mDataCounter += AUDIO_SPK_BUFFER_SIZE;
@@ -156,17 +154,13 @@ void NullOutputDevice::onTimerSignal(NullTimer &timer)
} }
// ---------------------- NullEnumerator -------------------------- // ---------------------- NullEnumerator --------------------------
NullEnumerator::NullEnumerator() NullEnumerator::NullEnumerator() {}
{}
NullEnumerator::~NullEnumerator() NullEnumerator::~NullEnumerator() {}
{}
void NullEnumerator::open(int direction) void NullEnumerator::open(int direction) {}
{}
void NullEnumerator::close() void NullEnumerator::close() {}
{}
int NullEnumerator::count() int NullEnumerator::count()
{ {
@@ -191,4 +185,3 @@ int NullEnumerator::indexOfDefaultDevice()
{ {
return 0; return 0;
} }
+6 -4
View File
@@ -27,13 +27,14 @@ protected:
void start(); void start();
void stop(); void stop();
void run(); void run();
public: public:
/* Interval is in milliseconds. */ /* Interval is in milliseconds. */
NullTimer(std::chrono::milliseconds interval, Delegate* delegate, const char* name = nullptr); NullTimer(std::chrono::milliseconds interval, Delegate* delegate, const char* name = nullptr);
~NullTimer(); ~NullTimer();
}; };
class NullInputDevice: public InputDevice, public NullTimer::Delegate class NullInputDevice : public InputDevice, public NullTimer::Delegate
{ {
protected: protected:
void* mBuffer = nullptr; void* mBuffer = nullptr;
@@ -52,7 +53,7 @@ public:
void onTimerSignal(NullTimer& timer) override; void onTimerSignal(NullTimer& timer) override;
}; };
class NullOutputDevice: public OutputDevice, public NullTimer::Delegate class NullOutputDevice : public OutputDevice, public NullTimer::Delegate
{ {
protected: protected:
std::shared_ptr<NullTimer> mTimer; std::shared_ptr<NullTimer> mTimer;
@@ -60,6 +61,7 @@ protected:
int64_t mDataCounter = 0, mTimeCounter = 0; int64_t mDataCounter = 0, mTimeCounter = 0;
void internalClose(); void internalClose();
public: public:
NullOutputDevice(); NullOutputDevice();
virtual ~NullOutputDevice(); virtual ~NullOutputDevice();
@@ -71,7 +73,7 @@ public:
void onTimerSignal(NullTimer& timer) override; void onTimerSignal(NullTimer& timer) override;
}; };
class NullEnumerator: public Enumerator class NullEnumerator : public Enumerator
{ {
public: public:
NullEnumerator(); NullEnumerator();
@@ -86,6 +88,6 @@ public:
int indexOfDefaultDevice() override; int indexOfDefaultDevice() override;
}; };
} } // namespace Audio
#endif #endif
+6 -11
View File
@@ -11,14 +11,9 @@
using namespace Audio; using namespace Audio;
// -------------- Player ----------- // -------------- Player -----------
Player::Player() Player::Player() : mDelegate(nullptr), mPlayedTime(0) {}
:mDelegate(nullptr), mPlayedTime(0)
{
}
Player::~Player() Player::~Player() {}
{
}
void Player::setDelegate(EndOfAudioDelegate* d) void Player::setDelegate(EndOfAudioDelegate* d)
{ {
@@ -68,15 +63,15 @@ void Player::onSpkData(const Format& f, void* buffer, int length)
continue; continue;
} }
int wasread = item.mFile->read((char*)buffer+produced, length-produced); int wasread = item.mFile->read((char*)buffer + produced, length - produced);
mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND; mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND;
produced += wasread; produced += wasread;
if (wasread < length-produced) if (wasread < length - produced)
{ {
if (item.mLoop) if (item.mLoop)
{ {
item.mFile->rewind(); item.mFile->rewind();
wasread = item.mFile->read((char*)buffer+produced, (length - produced)); wasread = item.mFile->read((char*)buffer + produced, (length - produced));
mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND; mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND;
produced += wasread; produced += wasread;
} }
@@ -124,7 +119,7 @@ void Player::release(int usage)
if (!usageIter->second) if (!usageIter->second)
mUsage.erase(usageIter); mUsage.erase(usageIter);
for (unsigned i=0; i<mPlaylist.size(); i++) for (unsigned i = 0; i < mPlaylist.size(); i++)
if (mPlaylist[i].mUsageId == usage) if (mPlaylist[i].mUsageId == usage)
mPlaylist.erase(mPlaylist.begin() + i); mPlaylist.erase(mPlaylist.begin() + i);
+8 -7
View File
@@ -16,10 +16,11 @@
namespace Audio namespace Audio
{ {
class Player: public DataConnection class Player : public DataConnection
{ {
friend class DevicePair; friend class DevicePair;
public:
public:
struct PlaylistItem struct PlaylistItem
{ {
PWavFileReader mFile; PWavFileReader mFile;
@@ -35,7 +36,7 @@ namespace Audio
virtual void onFilePlayed(PlaylistItem& item) = 0; virtual void onFilePlayed(PlaylistItem& item) = 0;
}; };
protected: protected:
typedef std::map<int, int> UsageMap; typedef std::map<int, int> UsageMap;
Audio::POutputDevice mOutput; Audio::POutputDevice mOutput;
UsageMap mUsage; // References map UsageMap mUsage; // References map
@@ -51,7 +52,7 @@ namespace Audio
void onFilePlayed(); void onFilePlayed();
void obtain(int usageId); void obtain(int usageId);
public: public:
Player(); Player();
~Player(); ~Player();
@@ -66,6 +67,6 @@ namespace Audio
void clear(); void clear();
int releasePlayed(); int releasePlayed();
void retrieveUsageIds(std::vector<int>& ids); void retrieveUsageIds(std::vector<int>& ids);
}; };
} } // namespace Audio
#endif #endif
+28 -32
View File
@@ -10,7 +10,7 @@
#include "speex/speex_preprocess.h" #include "speex/speex_preprocess.h"
#ifdef WIN32 #ifdef WIN32
# include <malloc.h> #include <malloc.h>
#endif #endif
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@@ -18,14 +18,14 @@
using namespace Audio; using namespace Audio;
#ifndef SHRT_MAX #ifndef SHRT_MAX
# define SHRT_MAX 32767 /* maximum (signed) short value */ #define SHRT_MAX 32767 /* maximum (signed) short value */
#endif #endif
AgcFilter::AgcFilter(int channels) AgcFilter::AgcFilter(int channels)
{ {
static const float DefaultLevel = 0.8f; static const float DefaultLevel = 0.8f;
for (int i=0; i<channels; i++) for (int i = 0; i < channels; i++)
{ {
Channel c; Channel c;
float level = DefaultLevel; float level = DefaultLevel;
@@ -34,8 +34,7 @@ AgcFilter::AgcFilter(int channels)
c.mIgain = 65536; c.mIgain = 65536;
if (level > 1.0f) if (level > 1.0f)
level = 1.0f; level = 1.0f;
else else if (level < 0.5f)
if (level < 0.5f)
level = 0.5f; level = 0.5f;
c.mIpeak = (int)(SHRT_MAX * level * 65536); c.mIpeak = (int)(SHRT_MAX * level * 65536);
@@ -45,13 +44,11 @@ AgcFilter::AgcFilter(int channels)
} }
} }
AgcFilter::~AgcFilter() AgcFilter::~AgcFilter() {}
{
}
void AgcFilter::process(void *pcm, int length) void AgcFilter::process(void* pcm, int length)
{ {
for (size_t i=0; i<mChannelList.size(); i++) for (size_t i = 0; i < mChannelList.size(); i++)
processChannel((short*)pcm, length / (sizeof(short) * mChannelList.size()), i); processChannel((short*)pcm, length / (sizeof(short) * mChannelList.size()), i);
} }
@@ -59,7 +56,7 @@ void AgcFilter::processChannel(short* pcm, int nrOfSamples, int channelIndex)
{ {
int i; int i;
for(i=0; i<nrOfSamples; i++) for (i = 0; i < nrOfSamples; i++)
{ {
long gain_new; long gain_new;
int sample; int sample;
@@ -68,14 +65,14 @@ void AgcFilter::processChannel(short* pcm, int nrOfSamples, int channelIndex)
/* get the abs of buffer[i] */ /* get the abs of buffer[i] */
sample = pcm[sampleIndex]; sample = pcm[sampleIndex];
sample = (sample < 0 ? -(sample):sample); sample = (sample < 0 ? -(sample) : sample);
if(sample > (int)channel.mSampleMax) if (sample > (int)channel.mSampleMax)
{ {
/* update the max */ /* update the max */
channel.mSampleMax = (unsigned int)sample; channel.mSampleMax = (unsigned int)sample;
} }
channel.mCounter ++; channel.mCounter++;
/* Will we get an overflow with the current gain factor? */ /* Will we get an overflow with the current gain factor? */
if (((sample * channel.mIgain) >> 16) > channel.mIpeak) if (((sample * channel.mIgain) >> 16) > channel.mIpeak)
@@ -83,7 +80,7 @@ void AgcFilter::processChannel(short* pcm, int nrOfSamples, int channelIndex)
/* Yes: Calculate new gain. */ /* Yes: Calculate new gain. */
channel.mIgain = ((channel.mIpeak / channel.mSampleMax) * 62259) >> 16; channel.mIgain = ((channel.mIpeak / channel.mSampleMax) * 62259) >> 16;
channel.mSilenceCounter = 0; channel.mSilenceCounter = 0;
pcm[sampleIndex] = (short) ((pcm[sampleIndex] * channel.mIgain) >> 16); pcm[sampleIndex] = (short)((pcm[sampleIndex] * channel.mIgain) >> 16);
continue; continue;
} }
@@ -112,23 +109,23 @@ void AgcFilter::processChannel(short* pcm, int nrOfSamples, int channelIndex)
channel.mCounter = 0; channel.mCounter = 0;
channel.mSampleMax = 1; channel.mSampleMax = 1;
} }
pcm[sampleIndex] = (short) ((pcm[sampleIndex] * channel.mIgain) >> 16); pcm[sampleIndex] = (short)((pcm[sampleIndex] * channel.mIgain) >> 16);
} }
} }
// --- AecFilter --- // --- AecFilter ---
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
# include "speex/speex_echo.h" #include "speex/speex_echo.h"
#include "Audio_Interface.h" #include "Audio_Interface.h"
#if !defined(TARGET_WIN) #if !defined(TARGET_WIN)
# include <alloca.h> #include <alloca.h>
#endif #endif
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
# include "aec/echo_cancellation.h" #include "aec/echo_cancellation.h"
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
@@ -139,12 +136,12 @@ static void CheckWRACode(unsigned errorcode)
} }
#endif #endif
AecFilter::AecFilter(int tailTime, int frameTime, int rate) AecFilter::AecFilter(int tailTime, int frameTime, int rate) : mCtx(nullptr), mFrameTime(frameTime), mRate(rate)
:mCtx(nullptr), mFrameTime(frameTime), mRate(rate)
{ {
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
if (AUDIO_CHANNELS == 2) if (AUDIO_CHANNELS == 2)
mCtx = speex_echo_state_init_mc(frameTime * (mRate / 1000), tailTime * (mRate / 1000), AUDIO_CHANNELS, AUDIO_CHANNELS ); mCtx = speex_echo_state_init_mc(frameTime * (mRate / 1000), tailTime * (mRate / 1000), AUDIO_CHANNELS,
AUDIO_CHANNELS);
else else
mCtx = speex_echo_state_init(frameTime * (mRate / 1000), tailTime * (mRate / 1000)); mCtx = speex_echo_state_init(frameTime * (mRate / 1000), tailTime * (mRate / 1000));
int tmp = rate; int tmp = rate;
@@ -162,7 +159,7 @@ AecFilter::~AecFilter()
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
if (mCtx) if (mCtx)
{ {
//speex_echo_state_destroy((SpeexEchoState*)mCtx); // speex_echo_state_destroy((SpeexEchoState*)mCtx);
mCtx = nullptr; mCtx = nullptr;
} }
#endif #endif
@@ -173,7 +170,7 @@ AecFilter::~AecFilter()
#endif #endif
} }
void AecFilter::fromMic(void *data) void AecFilter::fromMic(void* data)
{ {
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
short* output = (short*)alloca(Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH)); short* output = (short*)alloca(Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH));
@@ -183,12 +180,13 @@ void AecFilter::fromMic(void *data)
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
short* inputframe = (short*)ALLOCA(framesize); short* inputframe = (short*)ALLOCA(framesize);
memcpy(inputframe, (char*)data+framesize*i, framesize); memcpy(inputframe, (char*)data + framesize * i, framesize);
CheckWRACode(WebRtcAec_Process(mCtx, (short*)inputframe, NULL, (short*)data+framesize/2*i, NULL, mFrameTime * mRate / 1000, 0,0)); CheckWRACode(WebRtcAec_Process(mCtx, (short*)inputframe, NULL, (short*)data + framesize / 2 * i, NULL,
mFrameTime * mRate / 1000, 0, 0));
#endif #endif
} }
void AecFilter::toSpeaker(void *data) void AecFilter::toSpeaker(void* data)
{ {
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
speex_echo_playback((SpeexEchoState*)mCtx, (short*)data); speex_echo_playback((SpeexEchoState*)mCtx, (short*)data);
@@ -205,10 +203,9 @@ int AecFilter::frametime()
} }
DenoiseFilter::DenoiseFilter(int rate) DenoiseFilter::DenoiseFilter(int rate) : mRate(rate)
:mRate(rate)
{ {
mCtx = speex_preprocess_state_init(mRate/100, mRate); mCtx = speex_preprocess_state_init(mRate / 100, mRate);
} }
DenoiseFilter::~DenoiseFilter() DenoiseFilter::~DenoiseFilter()
@@ -224,7 +221,7 @@ void DenoiseFilter::fromMic(void* data, int timelength)
// Process by 10-ms blocks // Process by 10-ms blocks
spx_int16_t* in = (spx_int16_t*)data; spx_int16_t* in = (spx_int16_t*)data;
for (int blockIndex=0; blockIndex<timelength/10; blockIndex++) for (int blockIndex = 0; blockIndex < timelength / 10; blockIndex++)
{ {
spx_int16_t* block = in + blockIndex * (mRate / 100) * AUDIO_CHANNELS; spx_int16_t* block = in + blockIndex * (mRate / 100) * AUDIO_CHANNELS;
speex_preprocess_run((SpeexPreprocessState*)mCtx, block); speex_preprocess_run((SpeexPreprocessState*)mCtx, block);
@@ -235,4 +232,3 @@ int DenoiseFilter::rate()
{ {
return mRate; return mRate;
} }
+17 -16
View File
@@ -11,9 +11,9 @@
namespace Audio namespace Audio
{ {
class AgcFilter class AgcFilter
{ {
protected: protected:
struct Channel struct Channel
{ {
unsigned int mSampleMax; unsigned int mSampleMax;
@@ -24,16 +24,17 @@ namespace Audio
}; };
std::vector<Channel> mChannelList; std::vector<Channel> mChannelList;
void processChannel(short* pcm, int nrOfSamples, int channelIndex); void processChannel(short* pcm, int nrOfSamples, int channelIndex);
public:
public:
AgcFilter(int channels); AgcFilter(int channels);
~AgcFilter(); ~AgcFilter();
void process(void* pcm, int length); void process(void* pcm, int length);
}; };
class AecFilter class AecFilter
{ {
public: public:
AecFilter(int tailTime, int frameTime, int rate); AecFilter(int tailTime, int frameTime, int rate);
~AecFilter(); ~AecFilter();
@@ -42,28 +43,28 @@ namespace Audio
void fromMic(void* data); void fromMic(void* data);
int frametime(); int frametime();
protected: protected:
void* mCtx; /// The echo canceller context's pointer. void* mCtx; /// The echo canceller context's pointer.
Mutex mGuard; /// Mutex to protect this instance. Mutex mGuard; /// Mutex to protect this instance.
int mFrameTime; /// Duration of single audio frame (in milliseconds) int mFrameTime; /// Duration of single audio frame (in milliseconds)
int mRate; int mRate;
}; };
class DenoiseFilter class DenoiseFilter
{ {
public: public:
DenoiseFilter(int rate); DenoiseFilter(int rate);
~DenoiseFilter(); ~DenoiseFilter();
void fromMic(void* data, int timelength); void fromMic(void* data, int timelength);
int rate(); int rate();
protected: protected:
Mutex mGuard; /// Mutex to protect this instance. Mutex mGuard; /// Mutex to protect this instance.
void* mCtx; /// The denoiser context pointer. void* mCtx; /// The denoiser context pointer.
int mRate; /// Duration of single audio frame (in milliseconds) int mRate; /// Duration of single audio frame (in milliseconds)
}; };
} } // namespace Audio
#endif #endif
+31 -42
View File
@@ -17,8 +17,7 @@ namespace Audio
{ {
SpeexResampler::SpeexResampler() SpeexResampler::SpeexResampler() {}
{}
void SpeexResampler::start(int channels, int sourceRate, int destRate) void SpeexResampler::start(int channels, int sourceRate, int destRate)
{ {
@@ -35,8 +34,8 @@ void SpeexResampler::start(int channels, int sourceRate, int destRate)
if (sourceRate != destRate) if (sourceRate != destRate)
{ {
// Defer context creation until first request // Defer context creation until first request
//mContext = speex_resampler_init(channels, sourceRate, destRate, AUDIO_RESAMPLER_QUALITY, &mErrorCode); // mContext = speex_resampler_init(channels, sourceRate, destRate, AUDIO_RESAMPLER_QUALITY, &mErrorCode);
//assert(mContext != NULL); // assert(mContext != NULL);
} }
} }
@@ -59,8 +58,8 @@ SpeexResampler::~SpeexResampler()
stop(); stop();
} }
size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_t& sourceProcessed, size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_t& sourceProcessed, void* dest,
void* dest, size_t destCapacity) size_t destCapacity)
{ {
assert(mSourceRate != 0 && mDestRate != 0); assert(mSourceRate != 0 && mDestRate != 0);
@@ -77,8 +76,7 @@ size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_
if (!mContext) if (!mContext)
{ {
mContext = speex_resampler_init(mChannels, mSourceRate, mDestRate, mContext = speex_resampler_init(mChannels, mSourceRate, mDestRate, AUDIO_RESAMPLER_QUALITY, &mErrorCode);
AUDIO_RESAMPLER_QUALITY, &mErrorCode);
if (!mContext) if (!mContext)
return 0; return 0;
} }
@@ -103,11 +101,9 @@ size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_
unsigned inLen = sourceLength / (sizeof(short) * mChannels); unsigned inLen = sourceLength / (sizeof(short) * mChannels);
outLen /= sizeof(short) * mChannels; outLen /= sizeof(short) * mChannels;
assert(mContext != NULL); assert(mContext != NULL);
spx_uint32_t in_len = static_cast<spx_uint32_t>(inLen), spx_uint32_t in_len = static_cast<spx_uint32_t>(inLen), out_len = static_cast<spx_uint32_t>(outLen);
out_len = static_cast<spx_uint32_t>(outLen);
int speexCode = speex_resampler_process_interleaved_int((SpeexResamplerState *)mContext, int speexCode = speex_resampler_process_interleaved_int((SpeexResamplerState*)mContext, (spx_int16_t*)src, &in_len,
(spx_int16_t*)src, &in_len,
(spx_int16_t*)dest, &out_len); (spx_int16_t*)dest, &out_len);
assert(speexCode == RESAMPLER_ERR_SUCCESS); assert(speexCode == RESAMPLER_ERR_SUCCESS);
@@ -147,27 +143,27 @@ size_t SpeexResampler::getSize() const
} }
// -------------------------- ChannelConverter -------------------- // -------------------------- ChannelConverter --------------------
int ChannelConverter::stereoToMono(const void *source, int sourceLength, void *dest, int destLength) int ChannelConverter::stereoToMono(const void* source, int sourceLength, void* dest, int destLength)
{ {
assert(destLength == sourceLength / 2); assert(destLength == sourceLength / 2);
const short* input = (const short*)source; const short* input = (const short*)source;
short* output = (short*)dest; short* output = (short*)dest;
for (int sampleIndex = 0; sampleIndex < destLength/2; sampleIndex++) for (int sampleIndex = 0; sampleIndex < destLength / 2; sampleIndex++)
{ {
output[sampleIndex] = (input[sampleIndex*2] + input[sampleIndex*2+1]) >> 1; output[sampleIndex] = (input[sampleIndex * 2] + input[sampleIndex * 2 + 1]) >> 1;
} }
return sourceLength / 2; return sourceLength / 2;
} }
int ChannelConverter::monoToStereo(const void *source, int sourceLength, void *dest, int destLength) int ChannelConverter::monoToStereo(const void* source, int sourceLength, void* dest, int destLength)
{ {
assert (destLength == sourceLength * 2); assert(destLength == sourceLength * 2);
const short* input = (const short*)source; const short* input = (const short*)source;
short* output = (short*)dest; short* output = (short*)dest;
// Convert starting from the end of buffer to allow inplace conversion // Convert starting from the end of buffer to allow inplace conversion
for (int sampleIndex = sourceLength/2 - 1; sampleIndex >= 0; sampleIndex--) for (int sampleIndex = sourceLength / 2 - 1; sampleIndex >= 0; sampleIndex--)
{ {
output[2*sampleIndex] = output[2*sampleIndex+1] = input[sampleIndex]; output[2 * sampleIndex] = output[2 * sampleIndex + 1] = input[sampleIndex];
} }
return sourceLength * 2; return sourceLength * 2;
} }
@@ -184,13 +180,14 @@ Resampler48kTo16k::~Resampler48kTo16k()
WebRtcSpl_ResetResample48khzTo16khz(&mContext); WebRtcSpl_ResetResample48khzTo16khz(&mContext);
} }
int Resampler48kTo16k::process(const void *source, int sourceLen, void *dest, int destLen) int Resampler48kTo16k::process(const void* source, int sourceLen, void* dest, int destLen)
{ {
const short* input = (const short*)source; int inputLen = sourceLen / 2; const short* input = (const short*)source;
short* output = (short*)dest; //int outputCapacity = destLen / 2; int inputLen = sourceLen / 2;
short* output = (short*)dest; // int outputCapacity = destLen / 2;
assert(inputLen % 480 == 0); assert(inputLen % 480 == 0);
int frames = inputLen / 480; int frames = inputLen / 480;
for (int i=0; i<frames; i++) for (int i = 0; i < frames; i++)
WebRtcSpl_Resample48khzTo16khz(input + i * 480, output + i * 160, &mContext, mTemp); WebRtcSpl_Resample48khzTo16khz(input + i * 480, output + i * 160, &mContext, mTemp);
return sourceLen / 3; return sourceLen / 3;
@@ -207,13 +204,14 @@ Resampler16kto48k::~Resampler16kto48k()
WebRtcSpl_ResetResample16khzTo48khz(&mContext); WebRtcSpl_ResetResample16khzTo48khz(&mContext);
} }
int Resampler16kto48k::process(const void *source, int sourceLen, void *dest, int destLen) int Resampler16kto48k::process(const void* source, int sourceLen, void* dest, int destLen)
{ {
const WebRtc_Word16* input = (const WebRtc_Word16*)source; int inputLen = sourceLen / 2; const WebRtc_Word16* input = (const WebRtc_Word16*)source;
WebRtc_Word16* output = (WebRtc_Word16*)dest; //int outputCapacity = destLen / 2; int inputLen = sourceLen / 2;
WebRtc_Word16* output = (WebRtc_Word16*)dest; // int outputCapacity = destLen / 2;
assert(inputLen % 160 == 0); assert(inputLen % 160 == 0);
int frames = inputLen / 160; int frames = inputLen / 160;
for (int i=0; i<frames; i++) for (int i = 0; i < frames; i++)
WebRtcSpl_Resample16khzTo48khz(input + i * 160, output + i * 480, &mContext, mTemp); WebRtcSpl_Resample16khzTo48khz(input + i * 160, output + i * 480, &mContext, mTemp);
return sourceLen * 3; return sourceLen * 3;
@@ -222,18 +220,12 @@ int Resampler16kto48k::process(const void *source, int sourceLen, void *dest, in
#endif #endif
// ---------------- UniversalResampler ------------------- // ---------------- UniversalResampler -------------------
UniversalResampler::UniversalResampler() UniversalResampler::UniversalResampler() {}
{
} UniversalResampler::~UniversalResampler() {}
UniversalResampler::~UniversalResampler() size_t UniversalResampler::resample(int sourceRate, const void* sourceBuffer, size_t sourceLength,
{ size_t& sourceProcessed, int destRate, void* destBuffer, size_t destCapacity)
}
size_t UniversalResampler::resample(int sourceRate, const void *sourceBuffer, size_t sourceLength,
size_t& sourceProcessed, int destRate, void *destBuffer, size_t destCapacity)
{ {
assert(destBuffer && sourceBuffer); assert(destBuffer && sourceBuffer);
size_t result; size_t result;
@@ -252,10 +244,7 @@ size_t UniversalResampler::resample(int sourceRate, const void *sourceBuffer, si
return result; return result;
} }
void UniversalResampler::preload() void UniversalResampler::preload() {}
{
}
size_t UniversalResampler::getDestLength(int sourceRate, int destRate, size_t sourceLength) size_t UniversalResampler::getDestLength(int sourceRate, int destRate, size_t sourceLength)
{ {
@@ -289,4 +278,4 @@ PResampler UniversalResampler::findResampler(int sourceRate, int destRate)
return r; return r;
} }
} // end of namespace } // namespace Audio
+36 -37
View File
@@ -7,7 +7,7 @@
#define __AUDIO_RESAMPLER_H #define __AUDIO_RESAMPLER_H
#ifdef USE_WEBRTC_RESAMPLER #ifdef USE_WEBRTC_RESAMPLER
# include "signal_processing_library/signal_processing_library.h" #include "signal_processing_library/signal_processing_library.h"
#endif #endif
#include <vector> #include <vector>
@@ -16,9 +16,9 @@
namespace Audio namespace Audio
{ {
class SpeexResampler class SpeexResampler
{ {
public: public:
SpeexResampler(); SpeexResampler();
~SpeexResampler(); ~SpeexResampler();
@@ -26,8 +26,8 @@ namespace Audio
void stop(); void stop();
bool isOpened() const; bool isOpened() const;
size_t processBuffer(const void* source, size_t sourceLength, size_t& sourceProcessed, size_t processBuffer(const void* source, size_t sourceLength, size_t& sourceProcessed, void* dest,
void* dest, size_t destCapacity); size_t destCapacity);
int sourceRate() const; int sourceRate() const;
int destRate() const; int destRate() const;
size_t getDestLength(size_t sourceLen) const; size_t getDestLength(size_t sourceLen) const;
@@ -36,29 +36,27 @@ namespace Audio
// Returns instance + speex encoder size in bytes // Returns instance + speex encoder size in bytes
size_t getSize() const; size_t getSize() const;
protected: protected:
void* mContext = nullptr; void* mContext = nullptr;
int mErrorCode = 0; int mErrorCode = 0;
int mSourceRate = 0, int mSourceRate = 0, mDestRate = 0, mChannels = 0;
mDestRate = 0,
mChannels = 0;
short mLastSample = 0; short mLastSample = 0;
}; };
typedef SpeexResampler Resampler; typedef SpeexResampler Resampler;
typedef std::shared_ptr<Resampler> PResampler; typedef std::shared_ptr<Resampler> PResampler;
class ChannelConverter class ChannelConverter
{ {
public: public:
static int stereoToMono(const void* source, int sourceLength, void* dest, int destLength); static int stereoToMono(const void* source, int sourceLength, void* dest, int destLength);
static int monoToStereo(const void* source, int sourceLength, void* dest, int destLength); static int monoToStereo(const void* source, int sourceLength, void* dest, int destLength);
}; };
// Operates with AUDIO_CHANNELS number of channels // Operates with AUDIO_CHANNELS number of channels
class UniversalResampler class UniversalResampler
{ {
public: public:
UniversalResampler(); UniversalResampler();
~UniversalResampler(); ~UniversalResampler();
@@ -67,40 +65,41 @@ namespace Audio
size_t getDestLength(int sourceRate, int destRate, size_t sourceLength); size_t getDestLength(int sourceRate, int destRate, size_t sourceLength);
size_t getSourceLength(int sourceRate, int destRate, size_t destLength); size_t getSourceLength(int sourceRate, int destRate, size_t destLength);
protected: protected:
typedef std::pair<int, int> RatePair; typedef std::pair<int, int> RatePair;
typedef std::map<RatePair, PResampler> ResamplerMap; typedef std::map<RatePair, PResampler> ResamplerMap;
ResamplerMap mResamplerMap; ResamplerMap mResamplerMap;
PResampler findResampler(int sourceRate, int destRate); PResampler findResampler(int sourceRate, int destRate);
void preload(); void preload();
}; };
#ifdef USE_WEBRTC_RESAMPLER #ifdef USE_WEBRTC_RESAMPLER
// n*10 milliseconds buffers required! // n*10 milliseconds buffers required!
class Resampler48kTo16k class Resampler48kTo16k
{ {
public: public:
Resampler48kTo16k(); Resampler48kTo16k();
~Resampler48kTo16k(); ~Resampler48kTo16k();
int process(const void* source, int sourceLen, void* dest, int destLen); int process(const void* source, int sourceLen, void* dest, int destLen);
protected:
protected:
WebRtc_Word32 mTemp[496]; WebRtc_Word32 mTemp[496];
WebRtcSpl_State48khzTo16khz mContext; WebRtcSpl_State48khzTo16khz mContext;
}; };
class Resampler16kto48k class Resampler16kto48k
{ {
public: public:
Resampler16kto48k(); Resampler16kto48k();
~Resampler16kto48k(); ~Resampler16kto48k();
int process(const void* source, int sourceLen, void* dest, int destLen); int process(const void* source, int sourceLen, void* dest, int destLen);
protected: protected:
WebRtc_Word32 mTemp[336]; WebRtc_Word32 mTemp[336];
WebRtcSpl_State16khzTo48khz mContext; WebRtcSpl_State16khzTo48khz mContext;
}; };
#endif #endif
} // end of namespace } // namespace Audio
#endif #endif
+13 -19
View File
@@ -13,13 +13,14 @@
#include <assert.h> #include <assert.h>
#ifndef WORD #ifndef WORD
# define WORD unsigned short #define WORD unsigned short
#endif #endif
#ifndef DWORD #ifndef DWORD
# define DWORD unsigned int #define DWORD unsigned int
#endif #endif
typedef struct { typedef struct
{
WORD wFormatTag; WORD wFormatTag;
WORD nChannels; WORD nChannels;
DWORD nSamplesPerSec; DWORD nSamplesPerSec;
@@ -27,8 +28,7 @@ typedef struct {
WORD nBlockAlign; WORD nBlockAlign;
WORD wBitsPerSample; WORD wBitsPerSample;
WORD cbSize; WORD cbSize;
} } WaveFormatEx;
WaveFormatEx;
#define WAVE_FORMAT_PCM 1 #define WAVE_FORMAT_PCM 1
@@ -39,15 +39,12 @@ WaveFormatEx;
using namespace Audio; using namespace Audio;
// ---------------------- WavFileReader ------------------------- // ---------------------- WavFileReader -------------------------
WavFileReader::WavFileReader() WavFileReader::WavFileReader() : mSamplerate(0), mLastError(0), mChannels(0), mBits(0), mDataLength(0)
:mSamplerate(0), mLastError(0), mChannels(0), mBits(0), mDataLength(0)
{ {
mDataOffset = 0; mDataOffset = 0;
} }
WavFileReader::~WavFileReader() WavFileReader::~WavFileReader() {}
{
}
#define THROW_READERROR throw Exception(ERR_WAVFILE_FAILED); #define THROW_READERROR throw Exception(ERR_WAVFILE_FAILED);
@@ -127,7 +124,7 @@ bool WavFileReader::open(const std::filesystem::path& p)
uint16_t formattag = 0; uint16_t formattag = 0;
readBuffer(&formattag, sizeof(formattag)); readBuffer(&formattag, sizeof(formattag));
if (formattag != 1/*WAVE_FORMAT_PCM*/) if (formattag != 1 /*WAVE_FORMAT_PCM*/)
THROW_READERROR; THROW_READERROR;
mChannels = 0; mChannels = 0;
@@ -168,7 +165,7 @@ bool WavFileReader::open(const std::filesystem::path& p)
mDataOffset = mInput->tellg(); mDataOffset = mInput->tellg();
mResampler.start(AUDIO_CHANNELS, mSamplerate, AUDIO_SAMPLERATE); mResampler.start(AUDIO_CHANNELS, mSamplerate, AUDIO_SAMPLERATE);
} }
catch(...) catch (...)
{ {
mInput.reset(); mInput.reset();
mLastError = static_cast<unsigned>(-1); mLastError = static_cast<unsigned>(-1);
@@ -234,8 +231,7 @@ size_t WavFileReader::read(short* buffer, size_t samples)
size_t readBytes = tryReadBuffer(temp, requiredBytes); size_t readBytes = tryReadBuffer(temp, requiredBytes);
size_t processedBytes = 0; size_t processedBytes = 0;
size_t result = mResampler.processBuffer(temp, readBytes, processedBytes, size_t result = mResampler.processBuffer(temp, readBytes, processedBytes, buffer, samples * 2 * AUDIO_CHANNELS);
buffer, samples * 2 * AUDIO_CHANNELS);
if (useHeap) if (useHeap)
free(temp); free(temp);
@@ -304,9 +300,7 @@ unsigned WavFileReader::lastError() const
#define BITS_PER_CHANNEL 16 #define BITS_PER_CHANNEL 16
WavFileWriter::WavFileWriter() WavFileWriter::WavFileWriter() : mLengthOffset(0), mSamplerate(AUDIO_SAMPLERATE), mChannels(1), mWritten(0) {}
:mLengthOffset(0), mSamplerate(AUDIO_SAMPLERATE), mChannels(1), mWritten(0)
{}
WavFileWriter::~WavFileWriter() WavFileWriter::~WavFileWriter()
{ {
@@ -358,7 +352,8 @@ bool WavFileWriter::open(const std::filesystem::path& p, int samplerate, int cha
writeBuffer(wavefmt, 8); writeBuffer(wavefmt, 8);
// Set the format description // Set the format description
uint32_t dwFmtSize = 16; /*= 16L*/; uint32_t dwFmtSize = 16; /*= 16L*/
;
writeBuffer(&dwFmtSize, sizeof(dwFmtSize)); writeBuffer(&dwFmtSize, sizeof(dwFmtSize));
WaveFormatEx format; WaveFormatEx format;
@@ -435,4 +430,3 @@ std::filesystem::path WavFileWriter::path() const
LOCK; LOCK;
return mPath; return mPath;
} }
+3 -3
View File
@@ -73,11 +73,11 @@ protected:
mutable std::recursive_mutex mFileMtx; /// Mutex to protect this instance. mutable std::recursive_mutex mFileMtx; /// Mutex to protect this instance.
size_t mWritten = 0; /// Amount of written data (in bytes) size_t mWritten = 0; /// Amount of written data (in bytes)
size_t mLengthOffset = 0; /// Position of length field. size_t mLengthOffset = 0; /// Position of length field.
int mSamplerate = 0, int mSamplerate = 0, mChannels = 0;
mChannels = 0;
void checkWriteResult(int result); void checkWriteResult(int result);
void writeBuffer(const void* buffer, size_t sz); void writeBuffer(const void* buffer, size_t sz);
public: public:
WavFileWriter(); WavFileWriter();
~WavFileWriter(); ~WavFileWriter();
@@ -91,6 +91,6 @@ public:
typedef std::shared_ptr<WavFileWriter> PWavFileWriter; typedef std::shared_ptr<WavFileWriter> PWavFileWriter;
} } // namespace Audio
#endif #endif
+30 -31
View File
@@ -44,7 +44,6 @@ WmmeInputDevice::Buffer::~Buffer()
GlobalUnlock(mHeaderHandle); GlobalUnlock(mHeaderHandle);
GlobalFree(mHeaderHandle); GlobalFree(mHeaderHandle);
} }
} }
bool WmmeInputDevice::Buffer::prepare(HWAVEIN device) bool WmmeInputDevice::Buffer::prepare(HWAVEIN device)
@@ -55,7 +54,7 @@ bool WmmeInputDevice::Buffer::prepare(HWAVEIN device)
mHeader->lpData = (LPSTR)mData; mHeader->lpData = (LPSTR)mData;
resCode = waveInPrepareHeader(device, mHeader, sizeof *mHeader); resCode = waveInPrepareHeader(device, mHeader, sizeof *mHeader);
//if (resCode != MMSYSERR_NOERROR) // if (resCode != MMSYSERR_NOERROR)
// LogCritical("Audio", << "Failed to prepare source header. Error code " << resCode << "."); // LogCritical("Audio", << "Failed to prepare source header. Error code " << resCode << ".");
return resCode == MMSYSERR_NOERROR; return resCode == MMSYSERR_NOERROR;
@@ -66,7 +65,7 @@ bool WmmeInputDevice::Buffer::unprepare(HWAVEIN device)
if (mHeader->dwFlags & WHDR_PREPARED) if (mHeader->dwFlags & WHDR_PREPARED)
{ {
MMRESULT resCode = waveInUnprepareHeader(device, mHeader, sizeof *mHeader); MMRESULT resCode = waveInUnprepareHeader(device, mHeader, sizeof *mHeader);
//if (resCode != MMSYSERR_NOERROR) // if (resCode != MMSYSERR_NOERROR)
// LogCritical("Audio", << "Failed to unprepare source header. Error code " << resCode << "."); // LogCritical("Audio", << "Failed to unprepare source header. Error code " << resCode << ".");
return resCode == MMSYSERR_NOERROR; return resCode == MMSYSERR_NOERROR;
} }
@@ -81,7 +80,7 @@ bool WmmeInputDevice::Buffer::isFinished()
bool WmmeInputDevice::Buffer::addToDevice(HWAVEIN device) bool WmmeInputDevice::Buffer::addToDevice(HWAVEIN device)
{ {
MMRESULT resCode = waveInAddBuffer(device, mHeader, sizeof(*mHeader)); MMRESULT resCode = waveInAddBuffer(device, mHeader, sizeof(*mHeader));
//if (resCode != MMSYSERR_NOERROR) // if (resCode != MMSYSERR_NOERROR)
// LogCritical("Audio", << "Failed to add buffer to source audio device. Error code is " << resCode << "."); // LogCritical("Audio", << "Failed to add buffer to source audio device. Error code is " << resCode << ".");
return resCode == MMSYSERR_NOERROR; return resCode == MMSYSERR_NOERROR;
} }
@@ -93,8 +92,8 @@ void* WmmeInputDevice::Buffer::data()
WmmeInputDevice::WmmeInputDevice(int deviceId) WmmeInputDevice::WmmeInputDevice(int deviceId)
:mDevHandle(NULL), mDoneSignal(INVALID_HANDLE_VALUE), mFakeMode(false), : mDevHandle(NULL), mDoneSignal(INVALID_HANDLE_VALUE), mFakeMode(false), mBufferIndex(0), mDeviceIndex(deviceId),
mBufferIndex(0), mDeviceIndex(deviceId), mThreadHandle(0) mThreadHandle(0)
{ {
mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
@@ -113,10 +112,11 @@ bool WmmeInputDevice::fakeMode()
return mFakeMode; return mFakeMode;
} }
void CALLBACK WmmeInputDevice::callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) void CALLBACK WmmeInputDevice::callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1,
DWORD_PTR dwParam2)
{ {
WmmeInputDevice* impl; WmmeInputDevice* impl;
switch(uMsg) switch (uMsg)
{ {
case WIM_DATA: case WIM_DATA:
impl = (WmmeInputDevice*)dwInstance; impl = (WmmeInputDevice*)dwInstance;
@@ -147,7 +147,8 @@ void WmmeInputDevice::openDevice()
// Open wavein // Open wavein
MMRESULT mmres = waveInOpen(&mDevHandle, mDeviceIndex, &wfx, (DWORD_PTR)callbackProc, (DWORD_PTR)this, CALLBACK_FUNCTION); MMRESULT mmres =
waveInOpen(&mDevHandle, mDeviceIndex, &wfx, (DWORD_PTR)callbackProc, (DWORD_PTR)this, CALLBACK_FUNCTION);
if (mmres != MMSYSERR_NOERROR) if (mmres != MMSYSERR_NOERROR)
{ {
mFakeMode = true; mFakeMode = true;
@@ -158,13 +159,13 @@ void WmmeInputDevice::openDevice()
// Create the buffers for running // Create the buffers for running
mBufferIndex = 0; mBufferIndex = 0;
for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++)
mBufferList[i].prepare(mDevHandle); mBufferList[i].prepare(mDevHandle);
for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++)
mBufferList[i].addToDevice(mDevHandle); mBufferList[i].addToDevice(mDevHandle);
/*mmres = */waveInStart(mDevHandle); /*mmres = */ waveInStart(mDevHandle);
} }
bool WmmeInputDevice::open() bool WmmeInputDevice::open()
@@ -190,7 +191,7 @@ void WmmeInputDevice::closeDevice()
} }
// Close buffers // Close buffers
for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++)
mBufferList[i].unprepare(mDevHandle); mBufferList[i].unprepare(mDevHandle);
// Close device // Close device
@@ -216,7 +217,6 @@ void WmmeInputDevice::close()
::SetEvent(mShutdownSignal); ::SetEvent(mShutdownSignal);
::WaitForSingleObject(mThreadHandle, INFINITE); ::WaitForSingleObject(mThreadHandle, INFINITE);
mThreadHandle = 0; mThreadHandle = 0;
} }
bool WmmeInputDevice::tryReadBuffer(void* buffer) bool WmmeInputDevice::tryReadBuffer(void* buffer)
@@ -244,7 +244,7 @@ void WmmeInputDevice::setFakeMode(bool fakeMode)
int WmmeInputDevice::readBuffer(void* buffer) int WmmeInputDevice::readBuffer(void* buffer)
{ {
//Lock lock(mGuard); // Lock lock(mGuard);
if (mRefCount <= 0 || mFakeMode) if (mRefCount <= 0 || mFakeMode)
return 0; return 0;
@@ -287,8 +287,7 @@ void WmmeInputDevice::threadProc(void* arg)
} }
// --- WmmeOutputDevice --- // --- WmmeOutputDevice ---
WmmeOutputDevice::Buffer::Buffer() WmmeOutputDevice::Buffer::Buffer() : mHeaderHandle(NULL), mDataHandle(NULL), mData(NULL), mHeader(NULL)
:mHeaderHandle(NULL), mDataHandle(NULL), mData(NULL), mHeader(NULL)
{ {
mHeaderHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_SPK_BUFFER_SIZE); mHeaderHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_SPK_BUFFER_SIZE);
if (!mHeaderHandle) if (!mHeaderHandle)
@@ -341,8 +340,8 @@ bool WmmeOutputDevice::Buffer::write(HWAVEOUT device)
} }
WmmeOutputDevice::WmmeOutputDevice(int index) WmmeOutputDevice::WmmeOutputDevice(int index)
:mDevice(NULL), mDeviceIndex(index), mPlayedTime(0), mPlayedCount(0), mBufferIndex(0), mThreadHandle(NULL), : mDevice(NULL), mDeviceIndex(index), mPlayedTime(0), mPlayedCount(0), mBufferIndex(0), mThreadHandle(NULL),
mFailed(false), mShutdownMarker(false) mFailed(false), mShutdownMarker(false)
{ {
mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
@@ -354,7 +353,8 @@ WmmeOutputDevice::~WmmeOutputDevice()
close(); close();
// Destroy used signals // Destroy used signals
CloseHandle(mDoneSignal); CloseHandle(mShutdownSignal); CloseHandle(mDoneSignal);
CloseHandle(mShutdownSignal);
} }
bool WmmeOutputDevice::open() bool WmmeOutputDevice::open()
@@ -395,10 +395,10 @@ void WmmeOutputDevice::openDevice()
throw Exception(ERR_WMME_FAILED, mmres); throw Exception(ERR_WMME_FAILED, mmres);
// Prebuffer silence // Prebuffer silence
for (unsigned i=0; i<AUDIO_SPK_BUFFER_COUNT; i++) for (unsigned i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++)
{ {
//bool dumb = false; // bool dumb = false;
//mCallback(mBufferList[i].mData, SPK_BUFFER_SIZE, dumb, dumb); // mCallback(mBufferList[i].mData, SPK_BUFFER_SIZE, dumb, dumb);
memset(mBufferList[i].mData, 0, AUDIO_SPK_BUFFER_SIZE); memset(mBufferList[i].mData, 0, AUDIO_SPK_BUFFER_SIZE);
mBufferList[i].prepare(mDevice); mBufferList[i].prepare(mDevice);
mBufferList[i].write(mDevice); mBufferList[i].write(mDevice);
@@ -430,13 +430,12 @@ bool WmmeOutputDevice::areBuffersFinished()
{ {
Lock l(mGuard); Lock l(mGuard);
bool result = true; bool result = true;
for (unsigned i=0; i<AUDIO_SPK_BUFFER_COUNT && result; i++) for (unsigned i = 0; i < AUDIO_SPK_BUFFER_COUNT && result; i++)
{ {
bool finished = mBufferList[i].mHeader->dwFlags & WHDR_DONE || bool finished = mBufferList[i].mHeader->dwFlags & WHDR_DONE || !mBufferList[i].mHeader->dwFlags;
!mBufferList[i].mHeader->dwFlags;
if (finished) if (finished)
{ {
/* if (mBufferList[i].mHeader->dwFlags & WHDR_PREPARED) /* if (mBufferList[i].mHeader->dwFlags & WHDR_PREPARED)
mBufferList[i].Unprepare(mDevice); */ mBufferList[i].Unprepare(mDevice); */
} }
result &= finished; result &= finished;
@@ -465,7 +464,7 @@ void WmmeOutputDevice::threadProc(void* arg)
WaitForSingleObject(impl->mDoneSignal, 500); WaitForSingleObject(impl->mDoneSignal, 500);
// Iterate buffers to find played // Iterate buffers to find played
for (i=0; i<AUDIO_SPK_BUFFER_COUNT; i++) for (i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++)
{ {
index = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT; index = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT;
Buffer& buffer = impl->mBufferList[index]; Buffer& buffer = impl->mBufferList[index];
@@ -488,8 +487,7 @@ void WmmeOutputDevice::threadProc(void* arg)
exitCount++; exitCount++;
} }
impl->mBufferIndex = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT; impl->mBufferIndex = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT;
} } while (!exitSignal || exitCount < AUDIO_SPK_BUFFER_COUNT);
while (!exitSignal || exitCount < AUDIO_SPK_BUFFER_COUNT);
impl->closeDevice(); impl->closeDevice();
} }
@@ -540,7 +538,8 @@ bool WmmeOutputDevice::closing()
return mClosing; return mClosing;
} }
void CALLBACK WmmeOutputDevice::callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) void CALLBACK WmmeOutputDevice::callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1,
DWORD_PTR dwParam2)
{ {
WmmeOutputDevice* impl; WmmeOutputDevice* impl;
+18 -18
View File
@@ -30,9 +30,9 @@
namespace Audio namespace Audio
{ {
class WmmeInputDevice: public InputDevice class WmmeInputDevice : public InputDevice
{ {
public: public:
WmmeInputDevice(int index); WmmeInputDevice(int index);
~WmmeInputDevice(); ~WmmeInputDevice();
@@ -45,7 +45,7 @@ namespace Audio
int readBuffer(void* buffer); int readBuffer(void* buffer);
HWAVEIN handle(); HWAVEIN handle();
protected: protected:
class Buffer class Buffer
{ {
public: public:
@@ -79,13 +79,14 @@ namespace Audio
void openDevice(); void openDevice();
void closeDevice(); void closeDevice();
static void CALLBACK callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2); static void CALLBACK callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1,
DWORD_PTR dwParam2);
static void threadProc(void* arg); static void threadProc(void* arg);
}; };
class WmmeOutputDevice: public OutputDevice class WmmeOutputDevice : public OutputDevice
{ {
public: public:
WmmeOutputDevice(int index); WmmeOutputDevice(int index);
~WmmeOutputDevice(); ~WmmeOutputDevice();
@@ -98,16 +99,18 @@ namespace Audio
bool fakeMode(); bool fakeMode();
bool closing(); bool closing();
protected: protected:
class Buffer class Buffer
{ {
friend class WmmeOutputDevice; friend class WmmeOutputDevice;
public: public:
Buffer(); Buffer();
~Buffer(); ~Buffer();
bool prepare(HWAVEOUT device); bool prepare(HWAVEOUT device);
bool unprepare(HWAVEOUT device); bool unprepare(HWAVEOUT device);
bool write(HWAVEOUT device); bool write(HWAVEOUT device);
protected: protected:
WAVEHDR* mHeader; WAVEHDR* mHeader;
void* mData; void* mData;
@@ -121,9 +124,7 @@ namespace Audio
Buffer mBufferList[AUDIO_SPK_BUFFER_COUNT]; Buffer mBufferList[AUDIO_SPK_BUFFER_COUNT];
unsigned mPlayedTime; /// Amount of played time in milliseconds unsigned mPlayedTime; /// Amount of played time in milliseconds
bool mClosing; bool mClosing;
HANDLE mDoneSignal, HANDLE mDoneSignal, mShutdownSignal, mThreadHandle;
mShutdownSignal,
mThreadHandle;
volatile bool mShutdownMarker; volatile bool mShutdownMarker;
volatile LONG mPlayedCount; volatile LONG mPlayedCount;
@@ -134,14 +135,13 @@ namespace Audio
void closeDevice(); void closeDevice();
bool areBuffersFinished(); bool areBuffersFinished();
static void CALLBACK callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2); static void CALLBACK callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1,
DWORD_PTR dwParam2);
static void threadProc(void* arg); static void threadProc(void* arg);
}; };
} // namespace Audio
}
#endif #endif
-1
View File
@@ -1,2 +1 @@
#include "Audio_iOS.h" #include "Audio_iOS.h"
+2 -4
View File
@@ -1,21 +1,19 @@
#ifndef __AUDIO_IOS #ifndef __AUDIO_IOS
#define __AUDIO_IOS #define __AUDIO_IOS
class IosInputDevice: public InputDevice class IosInputDevice : public InputDevice
{ {
protected: protected:
public: public:
IosInputDevice(); IosInputDevice();
~IosInputDevice(); ~IosInputDevice();
void open(); void open();
void close(); void close();
}; };
class IosOutputDevice: public OutputDevice class IosOutputDevice : public OutputDevice
{ {
protected: protected:
public: public:
+74 -78
View File
@@ -15,10 +15,10 @@
#define CONFIG(X) mConfig->at(X) #define CONFIG(X) mConfig->at(X)
#define CONFIG_EXISTS(X) mConfig->exists(X) #define CONFIG_EXISTS(X) mConfig->exists(X)
//#define MODIFY_VIA_BEHIND_NAT // #define MODIFY_VIA_BEHIND_NAT
// NAT decorator // NAT decorator
class NATDecorator: public resip::MessageDecorator class NATDecorator : public resip::MessageDecorator
{ {
protected: protected:
UserAgent& mUserAgent; UserAgent& mUserAgent;
@@ -35,29 +35,26 @@ public:
NATDecorator(UserAgent& endpoint); NATDecorator(UserAgent& endpoint);
virtual ~NATDecorator(); virtual ~NATDecorator();
virtual void decorateMessage(resip::SipMessage &msg, const resip::Tuple &source, const resip::Tuple &destination, const resip::Data& sigcompId); virtual void decorateMessage(resip::SipMessage& msg, const resip::Tuple& source, const resip::Tuple& destination,
const resip::Data& sigcompId);
virtual void rollbackMessage(resip::SipMessage& msg); virtual void rollbackMessage(resip::SipMessage& msg);
virtual MessageDecorator* clone() const; virtual MessageDecorator* clone() const;
}; };
NATDecorator::NATDecorator(UserAgent& ua) NATDecorator::NATDecorator(UserAgent& ua) : mUserAgent(ua), mViaPort(0), mContactsPort(0) {}
:mUserAgent(ua), mViaPort(0), mContactsPort(0)
{
}
NATDecorator::~NATDecorator() NATDecorator::~NATDecorator() {}
{
}
void NATDecorator::decorateMessage(resip::SipMessage &msg, const resip::Tuple &source, const resip::Tuple &destination, const resip::Data& sigcompId) void NATDecorator::decorateMessage(resip::SipMessage& msg, const resip::Tuple& source, const resip::Tuple& destination,
const resip::Data& sigcompId)
{ {
// Make a copy to allow rollback // Make a copy to allow rollback
mMessage = msg; mMessage = msg;
std::stringstream dump; std::stringstream dump;
mMessage.encode(dump); mMessage.encode(dump);
//ICELogDebug(<< "Decorating message: \n" << dump.str()); // ICELogDebug(<< "Decorating message: \n" << dump.str());
// Check From: header and find the account // Check From: header and find the account
resip::NameAddr from; resip::NameAddr from;
@@ -101,11 +98,11 @@ void NATDecorator::decorateMessage(resip::SipMessage &msg, const resip::Tuple &s
uri.port() = account->mExternalAddress.port(); uri.port() = account->mExternalAddress.port();
if (account->mConfig->at(CONFIG_SIPS).asBool()) if (account->mConfig->at(CONFIG_SIPS).asBool())
{ {
//uri.scheme() = "sips"; // uri.scheme() = "sips";
//uri.param(resip::p_transport) = "tls"; // uri.param(resip::p_transport) = "tls";
} }
//uri.scheme() = account->mConfig->at(CONFIG_SIPS).asBool() ? "sips" : "sip"; // uri.scheme() = account->mConfig->at(CONFIG_SIPS).asBool() ? "sips" : "sip";
} }
} }
} }
@@ -141,7 +138,7 @@ void NATDecorator::rollbackMessage(resip::SipMessage& msg)
{ {
uri.host() = mContactsHost; uri.host() = mContactsHost;
uri.port() = mContactsPort; uri.port() = mContactsPort;
//uri.scheme() = mContactsScheme; // uri.scheme() = mContactsScheme;
} }
} }
} }
@@ -153,19 +150,16 @@ resip::MessageDecorator* NATDecorator::clone() const
} }
Account::Account(PVariantMap config, UserAgent& agent) Account::Account(PVariantMap config, UserAgent& agent)
:mAgent(agent), mId(0), mConfig(config), mRegistrationState(RegistrationState::None), : mAgent(agent), mId(0), mConfig(config), mRegistrationState(RegistrationState::None), mRegistration(NULL)
mRegistration(NULL)
{ {
mProfile = std::make_shared<resip::UserProfile>(agent.mProfile); mProfile = std::make_shared<resip::UserProfile>(agent.mProfile);
mId = Account::generateId(); mId = Account::generateId();
setup(*config); setup(*config);
} }
Account::~Account() Account::~Account() {}
{
}
void Account::setup(VariantMap &config) void Account::setup(VariantMap& config)
{ {
// Credentials // Credentials
@@ -176,8 +170,8 @@ void Account::setup(VariantMap &config)
mProfile->setDigestCredential(resip::Data(config[CONFIG_DOMAIN].asStdString()), mProfile->setDigestCredential(resip::Data(config[CONFIG_DOMAIN].asStdString()),
resip::Data(config[CONFIG_USERNAME].asStdString()), resip::Data(config[CONFIG_USERNAME].asStdString()),
resip::Data(config[CONFIG_PASSWORD].asStdString())); resip::Data(config[CONFIG_PASSWORD].asStdString()));
ICELogInfo( << "Credentials are set to domain " << config[CONFIG_DOMAIN].asStdString() << ICELogInfo(<< "Credentials are set to domain " << config[CONFIG_DOMAIN].asStdString() << ", username to "
", username to " << config[CONFIG_USERNAME].asStdString()); << config[CONFIG_USERNAME].asStdString());
// Proxy // Proxy
mProfile->unsetOutboundProxy(); mProfile->unsetOutboundProxy();
@@ -234,7 +228,7 @@ void Account::setup(VariantMap &config)
if (config[CONFIG_DOMAINPORT].asInt() != 0) if (config[CONFIG_DOMAINPORT].asInt() != 0)
from.uri().port() = config[CONFIG_DOMAINPORT].asInt(); from.uri().port() = config[CONFIG_DOMAINPORT].asInt();
else else
from.uri().port();// = 5060; from.uri().port(); // = 5060;
from.uri().user() = resip::Data(config[CONFIG_USERNAME].asStdString()); from.uri().user() = resip::Data(config[CONFIG_USERNAME].asStdString());
from.uri().host() = resip::Data(config[CONFIG_DOMAIN].asStdString()); from.uri().host() = resip::Data(config[CONFIG_DOMAIN].asStdString());
@@ -266,10 +260,12 @@ void Account::start()
// Create registration // Create registration
mRegistration = new ResipSession(*mAgent.mDum); mRegistration = new ResipSession(*mAgent.mDum);
auto regmessage = mAgent.mDum->makeRegistration(mProfile->getDefaultFrom(), mProfile, mConfig->at(CONFIG_REGISTERDURATION).asInt(), mRegistration); auto regmessage = mAgent.mDum->makeRegistration(mProfile->getDefaultFrom(), mProfile,
mConfig->at(CONFIG_REGISTERDURATION).asInt(), mRegistration);
for (UserInfo::const_iterator iter = mUserInfo.begin(); iter != mUserInfo.end(); iter++) for (UserInfo::const_iterator iter = mUserInfo.begin(); iter != mUserInfo.end(); iter++)
regmessage->header(resip::ExtensionHeader(iter->first.c_str())).push_back(resip::StringCategory(iter->second.c_str())); regmessage->header(resip::ExtensionHeader(iter->first.c_str()))
.push_back(resip::StringCategory(iter->second.c_str()));
mRegistrationState = RegistrationState::Registering; mRegistrationState = RegistrationState::Registering;
@@ -278,10 +274,10 @@ void Account::start()
// Check if STUN IP is required // Check if STUN IP is required
bool noStunServerIp = !CONFIG_EXISTS(CONFIG_STUNSERVER_IP); bool noStunServerIp = !CONFIG_EXISTS(CONFIG_STUNSERVER_IP);
//bool hasStunServerName = !CONFIG(CONFIG_STUNSERVER_NAME).asStdString().empty(); // bool hasStunServerName = !CONFIG(CONFIG_STUNSERVER_NAME).asStdString().empty();
if (noStunServerIp) if (noStunServerIp)
{ {
ICELogInfo(<<"No STUN server name or IP is not specified. Has to resolve/discover STUN server IP."); ICELogInfo(<< "No STUN server name or IP is not specified. Has to resolve/discover STUN server IP.");
mRefreshStunServerIpTimer.start(CONFIG(CONFIG_DNS_CACHE_TIME).asInt() * 1000); mRefreshStunServerIpTimer.start(CONFIG(CONFIG_DNS_CACHE_TIME).asInt() * 1000);
mRefreshStunServerIpTimer.isTimeToSend(); mRefreshStunServerIpTimer.isTimeToSend();
queryStunServerIp(); queryStunServerIp();
@@ -305,8 +301,7 @@ void Account::stop()
mRegistrationHandle->removeAll(); mRegistrationHandle->removeAll();
mRegistrationHandle = resip::ClientRegistrationHandle(); mRegistrationHandle = resip::ClientRegistrationHandle();
} }
else else if (mRegistration)
if (mRegistration)
{ {
mRegistration->end(); mRegistration->end();
} }
@@ -361,7 +356,8 @@ void Account::publishPresence(bool online, const std::string& content, int secon
if (mPublication.isValid()) if (mPublication.isValid())
mPublication->update(&p); mPublication->update(&p);
else else
mAgent.mDum->send(mAgent.mDum->makePublication(contact(SecureScheme::TlsOnly), mProfile, p, resip::Symbols::Presence, seconds)); mAgent.mDum->send(mAgent.mDum->makePublication(contact(SecureScheme::TlsOnly), mProfile, p,
resip::Symbols::Presence, seconds));
} }
void Account::stopPublish() void Account::stopPublish()
@@ -387,8 +383,8 @@ PClientObserver Account::observe(const std::string& target, const std::string& p
if (mConfig->exists(CONFIG_SUBSCRIPTION_REFRESHTIME)) if (mConfig->exists(CONFIG_SUBSCRIPTION_REFRESHTIME))
refresh = CONFIG(CONFIG_SUBSCRIPTION_REFRESHTIME).asInt(); refresh = CONFIG(CONFIG_SUBSCRIPTION_REFRESHTIME).asInt();
msg = mAgent.mDum->makeSubscription(resip::NameAddr(resip::Data(target)), mProfile, msg = mAgent.mDum->makeSubscription(resip::NameAddr(resip::Data(target)), mProfile, resip::Data(package), expires,
resip::Data(package), expires, refresh, observer->mSession); refresh, observer->mSession);
msg->header(resip::h_Accepts) = mAgent.mDum->getMasterProfile()->getSupportedMimeTypes(resip::NOTIFY); msg->header(resip::h_Accepts) = mAgent.mDum->getMasterProfile()->getSupportedMimeTypes(resip::NOTIFY);
mAgent.mClientObserverMap[observer->mSessionId] = observer; mAgent.mClientObserverMap[observer->mSessionId] = observer;
@@ -409,12 +405,14 @@ int Account::sendMsg(const std::string& peer, const void* ptr, unsigned length,
resip::Mime type; resip::Mime type;
std::string::size_type p = mime.find('/'); std::string::size_type p = mime.find('/');
if (p != std::string::npos) if (p != std::string::npos)
type = resip::Mime(resip::Data(mime.substr(0, p)), resip::Data(mime.substr(p+1))); type = resip::Mime(resip::Data(mime.substr(0, p)), resip::Data(mime.substr(p + 1)));
else else
type = resip::Mime(resip::Data(mime), resip::Data()); type = resip::Mime(resip::Data(mime), resip::Data());
resip::ClientPagerMessageHandle msgHandle = mAgent.mDum->makePagerMessage(resip::NameAddr(resip::Data(peer)), mProfile, s); resip::ClientPagerMessageHandle msgHandle =
unique_ptr<resip::Contents> contentPtr(new resip::PlainContents(resip::Data(std::string((const char*)ptr, length)),type)); mAgent.mDum->makePagerMessage(resip::NameAddr(resip::Data(peer)), mProfile, s);
unique_ptr<resip::Contents> contentPtr(
new resip::PlainContents(resip::Data(std::string((const char*)ptr, length)), type));
int result = s->sessionId(); int result = s->sessionId();
msgHandle->page(std::move(contentPtr)); msgHandle->page(std::move(contentPtr));
@@ -451,12 +449,13 @@ resip::NameAddr Account::contact(SecureScheme ss)
void Account::queryStunServerIp() void Account::queryStunServerIp()
{ {
ICELogInfo(<<"Looking for STUN/TURN server IP"); ICELogInfo(<< "Looking for STUN/TURN server IP");
if (!mConfig->exists(CONFIG_STUNSERVER_NAME)) if (!mConfig->exists(CONFIG_STUNSERVER_NAME))
{ {
// Send request to find STUN or TURN service // Send request to find STUN or TURN service
std::string target = std::string(mConfig->at(CONFIG_RELAY).asBool() ? "_turn" : "_stun") + "._udp." + mConfig->at(CONFIG_DOMAIN).asStdString(); std::string target = std::string(mConfig->at(CONFIG_RELAY).asBool() ? "_turn" : "_stun") + "._udp." +
mConfig->at(CONFIG_DOMAIN).asStdString();
// Start lookup // Start lookup
mAgent.mStack->getDnsStub().lookup<resip::RR_SRV>(resip::Data(target), this); mAgent.mStack->getDnsStub().lookup<resip::RR_SRV>(resip::Data(target), this);
@@ -475,7 +474,7 @@ void Account::queryStunServerIp()
} }
} }
void Account::prepareIceStack(Session *session, ice::AgentRole icerole) void Account::prepareIceStack(Session* session, ice::AgentRole icerole)
{ {
ice::ServerConfig config; ice::ServerConfig config;
ice::NetworkAddress addr; ice::NetworkAddress addr;
@@ -495,8 +494,8 @@ void Account::prepareIceStack(Session *session, ice::AgentRole icerole)
config.mUseIPv4 = mAgent.config()[CONFIG_IPV4].asBool(); config.mUseIPv4 = mAgent.config()[CONFIG_IPV4].asBool();
config.mUseIPv6 = mAgent.config()[CONFIG_IPV6].asBool(); config.mUseIPv6 = mAgent.config()[CONFIG_IPV6].asBool();
//config.mDetectNetworkChange = true; // config.mDetectNetworkChange = true;
//config.mNetworkCheckInterval = 5000; // config.mNetworkCheckInterval = 5000;
session->mIceStack = std::shared_ptr<ice::Stack>(ice::Stack::makeICEBox(config)); session->mIceStack = std::shared_ptr<ice::Stack>(ice::Stack::makeICEBox(config));
session->mIceStack->setEventHandler(session, this); session->mIceStack->setEventHandler(session, this);
@@ -509,7 +508,7 @@ void Account::process()
queryStunServerIp(); queryStunServerIp();
} }
void Account::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessage &response) void Account::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessage& response)
{ {
// Save registration handle // Save registration handle
mRegistrationHandle = h; mRegistrationHandle = h;
@@ -522,7 +521,7 @@ void Account::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessa
const resip::Via& via = response.header(resip::h_Vias).front(); const resip::Via& via = response.header(resip::h_Vias).front();
// Get the sent host // Get the sent host
const resip::Data& sentHost = via.sentHost();//response.header(h_Contacts).front().uri().host(); const resip::Data& sentHost = via.sentHost(); // response.header(h_Contacts).front().uri().host();
// Get the sentPort // Get the sentPort
int sentPort = via.sentPort(); int sentPort = via.sentPort();
@@ -553,12 +552,13 @@ void Account::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessa
} }
mUsedTransport = response.getReceivedTransportTuple().getType(); mUsedTransport = response.getReceivedTransportTuple().getType();
//bool streamTransport = mUsedTransport == resip::TCP || mUsedTransport == resip::TLS; // bool streamTransport = mUsedTransport == resip::TCP || mUsedTransport == resip::TLS;
// Retry registration for stream based transport too // Retry registration for stream based transport too
if ( (hostChanged || portChanged) && mRegistrationState == RegistrationState::Registering /*&& !streamTransport*/ && mConfig->at(CONFIG_EXTERNALIP).asBool()) if ((hostChanged || portChanged) && mRegistrationState == RegistrationState::Registering /*&& !streamTransport*/ &&
mConfig->at(CONFIG_EXTERNALIP).asBool())
{ {
//mRegistrationHandle->requestRefresh(); // mRegistrationHandle->requestRefresh();
// Unregister at first // Unregister at first
mRegistrationHandle->removeAll(); mRegistrationHandle->removeAll();
mRegistrationState = RegistrationState::Reregistering; mRegistrationState = RegistrationState::Reregistering;
@@ -570,12 +570,12 @@ void Account::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessa
mAgent.onAccountStart(mAgent.getAccount(this)); mAgent.onAccountStart(mAgent.getAccount(this));
} }
void Account::onRemoved(resip::ClientRegistrationHandle h, const resip::SipMessage &response) void Account::onRemoved(resip::ClientRegistrationHandle h, const resip::SipMessage& response)
{ {
// Check if this unregistering is a part of rport pr // Check if this unregistering is a part of rport pr
if (mRegistrationState == RegistrationState::Reregistering) if (mRegistrationState == RegistrationState::Reregistering)
{ {
//if (/*this->mUseExternalIP && */response.getSource().getType() == resip::UDP) // if (/*this->mUseExternalIP && */response.getSource().getType() == resip::UDP)
{ {
resip::Uri hostport(contact(SecureScheme::TlsOnly).uri()); resip::Uri hostport(contact(SecureScheme::TlsOnly).uri());
hostport.host() = resip::Data(mExternalAddress.ip()); hostport.host() = resip::Data(mExternalAddress.ip());
@@ -585,19 +585,24 @@ void Account::onRemoved(resip::ClientRegistrationHandle h, const resip::SipMessa
const char* transportName = nullptr; const char* transportName = nullptr;
switch (mUsedTransport) switch (mUsedTransport)
{ {
case resip::TCP: transportName = "tcp"; break; case resip::TCP:
case resip::TLS: transportName = "tls"; break; transportName = "tcp";
break;
case resip::TLS:
transportName = "tls";
break;
} }
hostport.param(resip::p_transport) = resip::Data(transportName); hostport.param(resip::p_transport) = resip::Data(transportName);
} }
mProfile->setOverrideHostAndPort(hostport); mProfile->setOverrideHostAndPort(hostport);
//mProfile->setDefaultFrom(from); // mProfile->setDefaultFrom(from);
} }
mProfile->setRegId(mConfig->at(CONFIG_REGID).asInt()); mProfile->setRegId(mConfig->at(CONFIG_REGID).asInt());
auto regmessage = mAgent.mDum->makeRegistration(mProfile->getDefaultFrom(), mProfile, UA_REGISTRATION_TIME); auto regmessage = mAgent.mDum->makeRegistration(mProfile->getDefaultFrom(), mProfile, UA_REGISTRATION_TIME);
for (UserInfo::const_iterator iter = mUserInfo.begin(); iter != mUserInfo.end(); iter++) for (UserInfo::const_iterator iter = mUserInfo.begin(); iter != mUserInfo.end(); iter++)
regmessage->header(resip::ExtensionHeader(iter->first.c_str())).push_back(resip::StringCategory(iter->second.c_str())); regmessage->header(resip::ExtensionHeader(iter->first.c_str()))
.push_back(resip::StringCategory(iter->second.c_str()));
mAgent.mDum->send(regmessage); mAgent.mDum->send(regmessage);
return; return;
@@ -625,16 +630,17 @@ void Account::onDnsResult(const resip::DNSResult<resip::DnsHostRecord>& result)
if (result.status == 0) if (result.status == 0)
{ {
resip::Data foundAddress = result.records.front().host(); resip::Data foundAddress = result.records.front().host();
ICELogInfo( << "Success to resolve STUN/TURN address to " << foundAddress.c_str()); ICELogInfo(<< "Success to resolve STUN/TURN address to " << foundAddress.c_str());
mConfig->at(CONFIG_STUNSERVER_IP) = std::string(foundAddress.c_str()); mConfig->at(CONFIG_STUNSERVER_IP) = std::string(foundAddress.c_str());
// Here the IP address of STUN/TURN server is found. If account is registered already - it means account is ready. // Here the IP address of STUN/TURN server is found. If account is registered already - it means account is
// ready.
if (mRegistrationState == RegistrationState::Registered) if (mRegistrationState == RegistrationState::Registered)
mAgent.onAccountStart(mAgent.getAccount(this)); mAgent.onAccountStart(mAgent.getAccount(this));
} }
else else
{ {
ICELogError( << "Failed to resolve STUN or TURN server IP address."); ICELogError(<< "Failed to resolve STUN or TURN server IP address.");
if (mRegistrationState == RegistrationState::Registered) if (mRegistrationState == RegistrationState::Registered)
{ {
int startCode = mConfig->at(CONFIG_STUNSERVER_NAME).asStdString().empty() ? 0 : 503; int startCode = mConfig->at(CONFIG_STUNSERVER_NAME).asStdString().empty() ? 0 : 503;
@@ -643,10 +649,7 @@ void Account::onDnsResult(const resip::DNSResult<resip::DnsHostRecord>& result)
} }
} }
void Account::onDnsResult(const resip::DNSResult<resip::DnsAAAARecord>&) void Account::onDnsResult(const resip::DNSResult<resip::DnsAAAARecord>&) {}
{
}
void Account::onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>& result) void Account::onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>& result)
{ {
@@ -654,14 +657,14 @@ void Account::onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>& result)
{ {
// Find lowest priority // Find lowest priority
int priority = 0x7FFFFFFF; int priority = 0x7FFFFFFF;
for (size_t i=0; i<result.records.size(); i++) for (size_t i = 0; i < result.records.size(); i++)
if (result.records[i].priority() < priority) if (result.records[i].priority() < priority)
priority = result.records[i].priority(); priority = result.records[i].priority();
size_t index = 0; size_t index = 0;
int weight = 0; int weight = 0;
for (size_t i=0; i<result.records.size(); i++) for (size_t i = 0; i < result.records.size(); i++)
{ {
if (result.records[i].priority() == priority && result.records[i].weight() >= weight) if (result.records[i].priority() == priority && result.records[i].weight() >= weight)
{ {
@@ -674,15 +677,15 @@ void Account::onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>& result)
const char* host = result.records[index].target().c_str(); const char* host = result.records[index].target().c_str();
ICELogInfo( << "Success to find STUN/TURN server on " << result.records[index].target().c_str() << ICELogInfo(<< "Success to find STUN/TURN server on " << result.records[index].target().c_str() << ":"
":" << (int)result.records[index].port()); << (int)result.records[index].port());
if (inet_addr(host) == INADDR_NONE) if (inet_addr(host) == INADDR_NONE)
{ {
// Try to resolve domain name now // Try to resolve domain name now
mAgent.mStack->getDnsStub().lookup<resip::RR_A>(result.records[index].target(), this); mAgent.mStack->getDnsStub().lookup<resip::RR_A>(result.records[index].target(), this);
//mStack->getDnsStub().lookup<resip::RR_AAAA>(result.records[index].target(), this); // mStack->getDnsStub().lookup<resip::RR_AAAA>(result.records[index].target(), this);
} }
else else
{ {
@@ -691,23 +694,16 @@ void Account::onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>& result)
} }
else else
{ {
ICELogError( << "Failed to find STUN or TURN service for specified domain."); ICELogError(<< "Failed to find STUN or TURN service for specified domain.");
//mAgent::shutdown(); // mAgent::shutdown();
} }
} }
void Account::onDnsResult(const resip::DNSResult<resip::DnsNaptrRecord>&) void Account::onDnsResult(const resip::DNSResult<resip::DnsNaptrRecord>&) {}
{
} void Account::onDnsResult(const resip::DNSResult<resip::DnsCnameRecord>&) {}
void Account::onDnsResult(const resip::DNSResult<resip::DnsCnameRecord>&) bool Account::isResponsibleFor(const resip::NameAddr& addr)
{
}
bool Account::isResponsibleFor(const resip::NameAddr &addr)
{ {
std::string user = addr.uri().user().c_str(); std::string user = addr.uri().user().c_str();
std::string domain = addr.uri().host().c_str(); std::string domain = addr.uri().host().c_str();
@@ -724,7 +720,7 @@ bool Account::isResponsibleFor(const resip::NameAddr &addr)
return false; return false;
} }
void Account::setUserInfo(const UserInfo &info) void Account::setUserInfo(const UserInfo& info)
{ {
mUserInfo = info; mUserInfo = info;
if (mRegistrationHandle.isValid()) if (mRegistrationHandle.isValid())
+2 -1
View File
@@ -22,10 +22,11 @@
class UserAgent; class UserAgent;
class Session; class Session;
class Account: public resip::DnsResultSink class Account : public resip::DnsResultSink
{ {
friend class UserAgent; friend class UserAgent;
friend class NATDecorator; friend class NATDecorator;
public: public:
Account(PVariantMap config, UserAgent& agent); Account(PVariantMap config, UserAgent& agent);
~Account(); ~Account();
+33 -21
View File
@@ -17,8 +17,7 @@
#define LOG_SUBSYSTEM "engine" #define LOG_SUBSYSTEM "engine"
AudioProvider::AudioProvider(UserAgent& agent, MT::Terminal& terminal) AudioProvider::AudioProvider(UserAgent& agent, MT::Terminal& terminal)
:mUserAgent(agent), mTerminal(terminal), mState(0), : mUserAgent(agent), mTerminal(terminal), mState(0), mRemoteTelephoneCodec(0), mRemoteNoSdp(false)
mRemoteTelephoneCodec(0), mRemoteNoSdp(false)
{ {
mActive = mfActive; mActive = mfActive;
mRemoteState = msSendRecv; mRemoteState = msSendRecv;
@@ -26,12 +25,11 @@ AudioProvider::AudioProvider(UserAgent& agent, MT::Terminal& terminal)
if (mUserAgent.config().exists(CONFIG_CODEC_PRIORITY)) if (mUserAgent.config().exists(CONFIG_CODEC_PRIORITY))
mCodecPriority.setupFrom(mUserAgent.config()[CONFIG_CODEC_PRIORITY].asVMap()); mCodecPriority.setupFrom(mUserAgent.config()[CONFIG_CODEC_PRIORITY].asVMap());
mSrtpSuite = SRTP_NONE; mSrtpSuite = SRTP_NONE;
setStateImpl((int)StreamState::SipRecv | (int)StreamState::SipSend | (int)StreamState::Receiving | (int)StreamState::Sending); setStateImpl((int)StreamState::SipRecv | (int)StreamState::SipSend | (int)StreamState::Receiving |
(int)StreamState::Sending);
} }
AudioProvider::~AudioProvider() AudioProvider::~AudioProvider() {}
{
}
std::string AudioProvider::streamName() std::string AudioProvider::streamName()
{ {
@@ -55,7 +53,7 @@ void AudioProvider::setDestinationAddress(const RtpPair<InternetAddress>& addr)
mActiveStream->setDestination(addr); mActiveStream->setDestination(addr);
} }
void AudioProvider::configureMediaObserver(MT::Stream::MediaObserver *observer, void* userTag) void AudioProvider::configureMediaObserver(MT::Stream::MediaObserver* observer, void* userTag)
{ {
mMediaObserver = observer; mMediaObserver = observer;
mMediaObserverTag = userTag; mMediaObserverTag = userTag;
@@ -71,13 +69,14 @@ void AudioProvider::processData(const PDatagramSocket& s, const void* dataBuffe
if (RtpHelper::isRtpOrRtcp(dataBuffer, dataSize)) if (RtpHelper::isRtpOrRtcp(dataBuffer, dataSize))
{ {
ICELogMedia(<<"Adding new data to stream processing"); ICELogMedia(<< "Adding new data to stream processing");
mActiveStream->dataArrived(s, dataBuffer, dataSize, source); mActiveStream->dataArrived(s, dataBuffer, dataSize, source);
} }
} }
// This method is called by user agent to send ICE packet from mediasocket // This method is called by user agent to send ICE packet from mediasocket
void AudioProvider::sendData(const PDatagramSocket& s, InternetAddress& destination, const void* buffer, unsigned int size) void AudioProvider::sendData(const PDatagramSocket& s, InternetAddress& destination, const void* buffer,
unsigned int size)
{ {
s->sendDatagram(destination, buffer, size); s->sendDatagram(destination, buffer, size);
} }
@@ -105,7 +104,7 @@ void AudioProvider::updateSdpOffer(resip::SdpContents::Session::Medium& sdp, Sdp
// Use CodecListPriority mCodecPriority adapter to work with codec priorities // Use CodecListPriority mCodecPriority adapter to work with codec priorities
if (mAvailableCodecs.empty()) if (mAvailableCodecs.empty())
{ {
for (int i=0; i<mCodecPriority.count(mTerminal.codeclist()); i++) for (int i = 0; i < mCodecPriority.count(mTerminal.codeclist()); i++)
mCodecPriority.codecAt(mTerminal.codeclist(), i).updateSdp(sdp.codecs(), direction); mCodecPriority.codecAt(mTerminal.codeclist(), i).updateSdp(sdp.codecs(), direction);
sdp.addCodec(resip::SdpContents::Session::Codec::TelephoneEvent); sdp.addCodec(resip::SdpContents::Session::Codec::TelephoneEvent);
} }
@@ -122,22 +121,35 @@ void AudioProvider::updateSdpOffer(resip::SdpContents::Session::Medium& sdp, Sdp
switch (mActive) switch (mActive)
{ {
case mfActive: case mfActive:
switch(mRemoteState) switch (mRemoteState)
{ {
case msSendonly: attr = "recvonly"; break; case msSendonly:
case msInactive: attr = "recvonly"; break; attr = "recvonly";
break;
case msInactive:
attr = "recvonly";
break;
case msRecvonly: case msRecvonly:
case msSendRecv: break; // Do nothing here case msSendRecv:
break; // Do nothing here
} }
break; break;
case mfPaused: case mfPaused:
switch (mRemoteState) switch (mRemoteState)
{ {
case msRecvonly: attr = "sendonly"; break; case msRecvonly:
case msSendonly: attr = "inactive"; break; attr = "sendonly";
case msInactive: attr = "inactive"; break; break;
case msSendRecv: attr = "sendonly"; break; case msSendonly:
attr = "inactive";
break;
case msInactive:
attr = "inactive";
break;
case msSendRecv:
attr = "sendonly";
break;
} }
break; break;
} }
@@ -227,7 +239,7 @@ bool AudioProvider::processSdpOffer(const resip::SdpContents::Session::Medium& m
// Use CodecListPriority mCodecPriority to work with codec priorities // Use CodecListPriority mCodecPriority to work with codec priorities
int pt; int pt;
for (int localIndex=0; localIndex<mCodecPriority.count(mTerminal.codeclist()); localIndex++) for (int localIndex = 0; localIndex < mCodecPriority.count(mTerminal.codeclist()); localIndex++)
{ {
MT::Codec::Factory& factory = mCodecPriority.codecAt(mTerminal.codeclist(), localIndex); MT::Codec::Factory& factory = mCodecPriority.codecAt(mTerminal.codeclist(), localIndex);
if ((pt = factory.processSdp(media.codecs(), sdpDirection)) != -1) if ((pt = factory.processSdp(media.codecs(), sdpDirection)) != -1)
@@ -367,9 +379,9 @@ void AudioProvider::setupMirror(bool enable)
mActiveStream->setupMirror(enable); mActiveStream->setupMirror(enable);
} }
void AudioProvider::setStateImpl(unsigned int state) { void AudioProvider::setStateImpl(unsigned int state)
{
mState = state; mState = state;
if (mActiveStream) if (mActiveStream)
mActiveStream->setState(state); mActiveStream->setState(state);
} }
+6 -7
View File
@@ -18,10 +18,9 @@
class UserAgent; class UserAgent;
class AudioProvider: public DataProvider class AudioProvider : public DataProvider
{ {
public: public:
AudioProvider(UserAgent& agent, MT::Terminal& terminal); AudioProvider(UserAgent& agent, MT::Terminal& terminal);
virtual ~AudioProvider(); virtual ~AudioProvider();
@@ -38,7 +37,8 @@ public:
void processData(const PDatagramSocket& s, const void* dataBuffer, int dataSize, InternetAddress& source) override; void processData(const PDatagramSocket& s, const void* dataBuffer, int dataSize, InternetAddress& source) override;
// This method is called by user agent to send ICE packet from mediasocket // This method is called by user agent to send ICE packet from mediasocket
void sendData(const PDatagramSocket& s, InternetAddress& destination, const void* dataBuffer, unsigned int datasize) override; void sendData(const PDatagramSocket& s, InternetAddress& destination, const void* dataBuffer,
unsigned int datasize) override;
// Updates SDP offer // Updates SDP offer
void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) override; void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) override;
@@ -96,9 +96,9 @@ protected:
int mSrtpTag = 1; // RFC 4568 tag of the negotiated crypto attribute int mSrtpTag = 1; // RFC 4568 tag of the negotiated crypto attribute
struct RemoteCodec struct RemoteCodec
{ {
RemoteCodec(MT::Codec::Factory* factory, int payloadType) RemoteCodec(MT::Codec::Factory* factory, int payloadType) : mFactory(factory), mRemotePayloadType(payloadType)
:mFactory(factory), mRemotePayloadType(payloadType) {
{ } }
MT::Codec::Factory* mFactory; MT::Codec::Factory* mFactory;
int mRemotePayloadType; int mRemotePayloadType;
@@ -115,7 +115,6 @@ protected:
// Implements setState() logic. This allows to be called from constructor (it is not virtual function) // Implements setState() logic. This allows to be called from constructor (it is not virtual function)
void setStateImpl(unsigned state); void setStateImpl(unsigned state);
}; };
#endif #endif
+8 -10
View File
@@ -10,7 +10,7 @@ bool DataProvider::isSupported(const char* name)
{ {
return !strcmp(name, "audio"); return !strcmp(name, "audio");
//return (!strcmp(name, "screen") || !strcmp(name, "data") || !strcmp(name, "audio") || !strcmp(name, "video")); // return (!strcmp(name, "screen") || !strcmp(name, "data") || !strcmp(name, "audio") || !strcmp(name, "video"));
} }
void DataProvider::pause() void DataProvider::pause()
@@ -20,7 +20,7 @@ void DataProvider::pause()
// Stop receive RTP stream // Stop receive RTP stream
if (state() & (int)StreamState::Receiving) if (state() & (int)StreamState::Receiving)
setState( state() & ~(int)StreamState::Receiving ); setState(state() & ~(int)StreamState::Receiving);
mActive = mfPaused; mActive = mfPaused;
} }
@@ -28,10 +28,10 @@ void DataProvider::pause()
void DataProvider::resume() void DataProvider::resume()
{ {
// Tell remote peer about resumed receiving in SDP // Tell remote peer about resumed receiving in SDP
//setState( state() | STATE_SIPRECV ); // setState( state() | STATE_SIPRECV );
// Start receive RTP stream // Start receive RTP stream
setState( state() | (int)StreamState::Receiving ); setState(state() | (int)StreamState::Receiving);
mActive = mfActive; mActive = mfActive;
} }
@@ -44,17 +44,15 @@ bool DataProvider::processSdpOffer(const resip::SdpContents::Session::Medium& me
mRemoteState = msSendonly; mRemoteState = msSendonly;
setState(state() & ~(int)StreamState::Sending); setState(state() & ~(int)StreamState::Sending);
} }
else else if (media.exists("recvonly"))
if (media.exists("recvonly"))
{ {
mRemoteState = msRecvonly; mRemoteState = msRecvonly;
setState(state() & ~(int)StreamState::Receiving); setState(state() & ~(int)StreamState::Receiving);
} }
else else if (media.exists("inactive"))
if (media.exists("inactive"))
{ {
mRemoteState = msInactive; mRemoteState = msInactive;
setState(state() & ~((int)StreamState::Sending | (int)StreamState::Receiving) ); setState(state() & ~((int)StreamState::Sending | (int)StreamState::Receiving));
} }
else else
{ {
@@ -66,7 +64,7 @@ bool DataProvider::processSdpOffer(const resip::SdpContents::Session::Medium& me
break; break;
case mfPaused: case mfPaused:
setState(state() | (int)StreamState::Sending ); setState(state() | (int)StreamState::Sending);
break; break;
} }
} }
+4 -2
View File
@@ -45,10 +45,12 @@ public:
virtual void setDestinationAddress(const RtpPair<InternetAddress>& addr) = 0; virtual void setDestinationAddress(const RtpPair<InternetAddress>& addr) = 0;
// Processes incoming data // Processes incoming data
virtual void processData(const PDatagramSocket& s, const void* dataBuffer, int dataSize, InternetAddress& address) = 0; virtual void processData(const PDatagramSocket& s, const void* dataBuffer, int dataSize,
InternetAddress& address) = 0;
// This method is called by user agent to send ICE packet from mediasocket // This method is called by user agent to send ICE packet from mediasocket
virtual void sendData(const PDatagramSocket& s, InternetAddress& destination, const void* dataBuffer, unsigned int datasize) = 0; virtual void sendData(const PDatagramSocket& s, InternetAddress& destination, const void* dataBuffer,
unsigned int datasize) = 0;
// Updates SDP offer // Updates SDP offer
virtual void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) = 0; virtual void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) = 0;
+188 -215
View File
@@ -30,27 +30,32 @@
#include "resip/dum/InviteSession.hxx" #include "resip/dum/InviteSession.hxx"
#if defined(TARGET_OSX) #if defined(TARGET_OSX)
# include "resip/stack/ssl/MacSecurity.hxx" #include "resip/stack/ssl/MacSecurity.hxx"
#endif #endif
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
# include "resip/stack/ssl/WinSecurity.hxx" #include "resip/stack/ssl/WinSecurity.hxx"
#endif #endif
#define LOG_SUBSYSTEM "engine" #define LOG_SUBSYSTEM "engine"
#define LOCK Lock l(mGuard) #define LOCK Lock l(mGuard)
#define CAST2RESIPSESSION(x) (x.isValid() ? (x->getAppDialogSet().isValid() ? dynamic_cast<ResipSession*>(x->getAppDialogSet().get()) : NULL) : NULL) #define CAST2RESIPSESSION(x) \
(x.isValid() ? (x->getAppDialogSet().isValid() ? dynamic_cast<ResipSession*>(x->getAppDialogSet().get()) : NULL) \
: NULL)
typedef resip::SdpContents::Session::Medium Medium; typedef resip::SdpContents::Session::Medium Medium;
typedef resip::SdpContents::Session::MediumContainer MediumContainer; typedef resip::SdpContents::Session::MediumContainer MediumContainer;
class TransportLogger: public resip::Transport::SipMessageLoggingHandler class TransportLogger : public resip::Transport::SipMessageLoggingHandler
{ {
public: public:
void outboundMessage(const resip::Tuple &source, const resip::Tuple &destination, const resip::SipMessage &msg) override void outboundMessage(const resip::Tuple& source, const resip::Tuple& destination,
const resip::SipMessage& msg) override
{ {
std::ostringstream dest_buffer; dest_buffer << destination; std::ostringstream dest_buffer;
std::ostringstream msg_buffer; msg_buffer << msg; dest_buffer << destination;
std::ostringstream msg_buffer;
msg_buffer << msg;
std::string msg_text = msg_buffer.str(); std::string msg_text = msg_buffer.str();
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
if (msg_text.size() > 512) if (msg_text.size() > 512)
@@ -59,7 +64,7 @@ public:
msg_text = strx::prefixLines(msg_text, "<---"); msg_text = strx::prefixLines(msg_text, "<---");
auto lines = strx::split(msg_text); auto lines = strx::split(msg_text);
for (const auto& l: lines) for (const auto& l : lines)
ICELogDebug(<< l); ICELogDebug(<< l);
} }
else else
@@ -72,13 +77,18 @@ public:
// Note: retransmissions store already encoded messages, so callback doesn't send SipMessage it sends // Note: retransmissions store already encoded messages, so callback doesn't send SipMessage it sends
// the encoded version of the SipMessage instead. If you need a SipMessage you will need to // the encoded version of the SipMessage instead. If you need a SipMessage you will need to
// re-parse back into a SipMessage in the callback handler. // re-parse back into a SipMessage in the callback handler.
void outboundRetransmit(const resip::Tuple &source, const resip::Tuple &destination, const resip::SendData &data) override void outboundRetransmit(const resip::Tuple& source, const resip::Tuple& destination,
{} const resip::SendData& data) override
void inboundMessage(const resip::Tuple& source, const resip::Tuple& destination, const resip::SipMessage &msg) override
{ {
std::ostringstream source_buffer; source_buffer << source; }
std::ostringstream msg_buffer; msg_buffer << msg;
void inboundMessage(const resip::Tuple& source, const resip::Tuple& destination,
const resip::SipMessage& msg) override
{
std::ostringstream source_buffer;
source_buffer << source;
std::ostringstream msg_buffer;
msg_buffer << msg;
std::string msg_text = msg_buffer.str(); std::string msg_text = msg_buffer.str();
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
if (msg_text.size() > 512) if (msg_text.size() > 512)
@@ -86,7 +96,7 @@ public:
ICELogDebug(<< "Received from " << source_buffer.str() << " :"); ICELogDebug(<< "Received from " << source_buffer.str() << " :");
msg_text = strx::prefixLines(msg_text, "--->"); msg_text = strx::prefixLines(msg_text, "--->");
auto lines = strx::split(msg_text); auto lines = strx::split(msg_text);
for (const auto& l: lines) for (const auto& l : lines)
ICELogDebug(<< l); ICELogDebug(<< l);
} }
else else
@@ -95,7 +105,6 @@ public:
ICELogDebug(<< "Received from " << source_buffer.str() << "\n" << strx::prefixLines(msg_buffer.str(), "--->")); ICELogDebug(<< "Received from " << source_buffer.str() << "\n" << strx::prefixLines(msg_buffer.str(), "--->"));
#endif #endif
} }
}; };
//-------------- UserAgent ----------------------- //-------------- UserAgent -----------------------
@@ -142,7 +151,7 @@ void UserAgent::start()
LOCK; LOCK;
if (mStack) if (mStack)
{ {
ICELogError(<<"Endpoint is started already."); ICELogError(<< "Endpoint is started already.");
return; return;
} }
@@ -160,11 +169,16 @@ void UserAgent::start()
while (std::getline(ss, line)) while (std::getline(ss, line))
{ {
line = strx::trim(line); line = strx::trim(line);
ice::NetworkAddress addr(line.c_str(), 0); addr.setPort(80); // Fake port to make ICEAddress initialized ice::NetworkAddress addr(line.c_str(), 0);
addr.setPort(80); // Fake port to make ICEAddress initialized
switch (addr.family()) switch (addr.family())
{ {
case AF_INET: nslist.push_back(resip::GenericIPAddress(*addr.sockaddr4())); break; case AF_INET:
case AF_INET6: nslist.push_back(resip::GenericIPAddress(*addr.sockaddr6())); break; nslist.push_back(resip::GenericIPAddress(*addr.sockaddr4()));
break;
case AF_INET6:
nslist.push_back(resip::GenericIPAddress(*addr.sockaddr6()));
break;
} }
} }
} }
@@ -190,7 +204,7 @@ void UserAgent::start()
resip::Data cert = resip::Data(mConfig[CONFIG_ROOTCERT].asStdString()); resip::Data cert = resip::Data(mConfig[CONFIG_ROOTCERT].asStdString());
mStack->getSecurity()->addRootCertPEM(cert); mStack->getSecurity()->addRootCertPEM(cert);
} }
catch(resip::BaseException& /*e*/) catch (resip::BaseException& /*e*/)
{ {
ICELogError(<< "Failed to preload root certificate"); ICELogError(<< "Failed to preload root certificate");
} }
@@ -202,8 +216,16 @@ void UserAgent::start()
mTransportList.clear(); mTransportList.clear();
resip::InternalTransport* t; resip::InternalTransport* t;
#define ADD_TRANSPORT4(X) if ((t = dynamic_cast<resip::InternalTransport*>(mStack->addTransport(X, 0, resip::V4)))) { /*t->setTransportLogger(this);*/ mTransportList.push_back(t);} #define ADD_TRANSPORT4(X) \
#define ADD_TRANSPORT6(X) if ((t = dynamic_cast<resip::InternalTransport*>(mStack->addTransport(X, 0, resip::V6)))) { /*t->setTransportLogger(this);*/ mTransportList.push_back(t);} if ((t = dynamic_cast<resip::InternalTransport*>(mStack->addTransport(X, 0, resip::V4)))) \
{ /*t->setTransportLogger(this);*/ \
mTransportList.push_back(t); \
}
#define ADD_TRANSPORT6(X) \
if ((t = dynamic_cast<resip::InternalTransport*>(mStack->addTransport(X, 0, resip::V6)))) \
{ /*t->setTransportLogger(this);*/ \
mTransportList.push_back(t); \
}
switch (mConfig[CONFIG_TRANSPORT].asInt()) switch (mConfig[CONFIG_TRANSPORT].asInt())
{ {
@@ -314,20 +336,20 @@ void UserAgent::shutdown()
if (!mStack) if (!mStack)
return; return;
ICELogInfo( << "Attempt to stop endpoint."); ICELogInfo(<< "Attempt to stop endpoint.");
{ {
LOCK; LOCK;
for (auto& observerIter: mClientObserverMap) for (auto& observerIter : mClientObserverMap)
observerIter.second->stop(); observerIter.second->stop();
for (auto& observerIter: mServerObserverMap) for (auto& observerIter : mServerObserverMap)
observerIter.second->stop(); observerIter.second->stop();
for (auto& sessionIter: mSessionMap) for (auto& sessionIter : mSessionMap)
sessionIter.second->stop(); sessionIter.second->stop();
for (auto& accountIter: mAccountSet) for (auto& accountIter : mAccountSet)
accountIter->stop(); accountIter->stop();
} }
} }
@@ -342,17 +364,19 @@ void UserAgent::refresh()
{ {
LOCK; LOCK;
for (auto& acc: mAccountSet) for (auto& acc : mAccountSet)
acc->refresh(); acc->refresh();
for (auto& observer: mClientObserverMap) for (auto& observer : mClientObserverMap)
observer.second->refresh(); observer.second->refresh();
} }
void UserAgent::onDumCanBeDeleted() void UserAgent::onDumCanBeDeleted()
{ {
delete mDum; mDum = nullptr; delete mDum;
delete mStack; mStack = nullptr; mDum = nullptr;
delete mStack;
mStack = nullptr;
mClientObserverMap.clear(); mClientObserverMap.clear();
mServerObserverMap.clear(); mServerObserverMap.clear();
@@ -371,14 +395,12 @@ void UserAgent::stop()
mTransportList.clear(); mTransportList.clear();
// Dump statistics here // Dump statistics here
ICELogInfo(<< "Remaining " ICELogInfo(<< "Remaining " << Session::InstanceCounter.load() << " session(s), "
<< Session::InstanceCounter.load() << " session(s), "
<< ResipSession::InstanceCounter.load() << " resip DialogSet(s), " << ResipSession::InstanceCounter.load() << " resip DialogSet(s), "
<< resip::ClientRegistration::InstanceCounter.load() << " ClientRegistration(s)"); << resip::ClientRegistration::InstanceCounter.load() << " ClientRegistration(s)");
mDum->shutdown(this); mDum->shutdown(this);
onDumCanBeDeleted(); onDumCanBeDeleted();
} }
@@ -390,12 +412,12 @@ void UserAgent::process()
bool connectionFailed = false; bool connectionFailed = false;
mStack->buildFdSet(fdset); mStack->buildFdSet(fdset);
//unsigned int t1 = mStack->getTimeTillNextProcessMS(); // unsigned int t1 = mStack->getTimeTillNextProcessMS();
int ret = fdset.selectMilliSeconds(0); int ret = fdset.selectMilliSeconds(0);
if (ret >= 0) // Got any results or time to send new packets? if (ret >= 0) // Got any results or time to send new packets?
{ {
Lock l(mGuard); Lock l(mGuard);
//ICELogDebug(<< "Smth on SIP socket(s)"); // ICELogDebug(<< "Smth on SIP socket(s)");
mStack->process(fdset); mStack->process(fdset);
// Check if there failed connections // Check if there failed connections
@@ -417,7 +439,8 @@ void UserAgent::process()
this->onSipConnectionFailed(); this->onSipConnectionFailed();
} }
// Erase one terminated session. The rule is : seession must not have references from resiprocate and reference count has to be 1. // Erase one terminated session. The rule is : seession must not have references from resiprocate and reference
// count has to be 1.
{ {
Lock l(mGuard); Lock l(mGuard);
SessionMap::iterator sessionIter; SessionMap::iterator sessionIter;
@@ -456,21 +479,25 @@ void UserAgent::process()
session.processQueuedOffer(); session.processQueuedOffer();
// Generate outgoing data while available // Generate outgoing data while available
int iceStreamId = -1, iceComponentId = -1; void* iceTag = NULL; bool iceResponse; int iceStreamId = -1, iceComponentId = -1;
void* iceTag = NULL;
bool iceResponse;
ice::PByteBuffer buffer; ice::PByteBuffer buffer;
while ((buffer = session.mIceStack->generateOutgoingData(iceResponse, iceStreamId, iceComponentId, iceTag))) while ((buffer = session.mIceStack->generateOutgoingData(iceResponse, iceStreamId, iceComponentId, iceTag)))
{ {
// Find corresponding data provider // Find corresponding data provider
for (unsigned i=0; i < session.mStreamList.size(); ++i) for (unsigned i = 0; i < session.mStreamList.size(); ++i)
{ {
Session::Stream& stream = session.mStreamList[i]; Session::Stream& stream = session.mStreamList[i];
if (stream.provider() && stream.iceInfo().mStreamId == iceStreamId) if (stream.provider() && stream.iceInfo().mStreamId == iceStreamId)
{ {
// Send generated packet via provider's method to allow custom scheme of encryption // Send generated packet via provider's method to allow custom scheme of encryption
ICELogDebug(<<"Sending ICE packet to " << buffer->remoteAddress().toStdString() << " with " << buffer->comment()); ICELogDebug(<< "Sending ICE packet to " << buffer->remoteAddress().toStdString() << " with "
<< buffer->comment());
RtpPair<PDatagramSocket>& pair = buffer->remoteAddress().family() == AF_INET6 ? stream.socket6() : stream.socket4(); RtpPair<PDatagramSocket>& pair =
buffer->remoteAddress().family() == AF_INET6 ? stream.socket6() : stream.socket4();
PDatagramSocket s = iceComponentId == ICE_RTP_ID ? pair.mRtp : pair.mRtcp; PDatagramSocket s = iceComponentId == ICE_RTP_ID ? pair.mRtp : pair.mRtcp;
if (s) if (s)
stream.provider()->sendData(s, buffer->remoteAddress(), buffer->data(), buffer->size()); stream.provider()->sendData(s, buffer->remoteAddress(), buffer->data(), buffer->size());
@@ -491,10 +518,12 @@ void UserAgent::addRootCert(const ByteBuffer& data)
if (!mStack) if (!mStack)
return; return;
resip::Data b(data.data(), data.size()); resip::Data b(data.data(), data.size());
try { try
{
mStack->getSecurity()->addRootCertPEM(b); mStack->getSecurity()->addRootCertPEM(b);
} }
catch(...) { catch (...)
{
// Ignore silently // Ignore silently
} }
} }
@@ -538,8 +567,7 @@ std::string UserAgent::formatSipAddress(const std::string& sip)
{ {
if (sip.find("sip:") == std::string::npos && sip.find("sips:") == std::string::npos) if (sip.find("sip:") == std::string::npos && sip.find("sips:") == std::string::npos)
result = "<sip:" + sip + ">"; result = "<sip:" + sip + ">";
else else if (sip[0] != '<' && sip.find('<') == std::string::npos)
if (sip[0] != '<' && sip.find('<') == std::string::npos)
result = "<" + sip + ">"; result = "<" + sip + ">";
else else
result = sip; result = sip;
@@ -583,7 +611,7 @@ UserAgent::SipAddress UserAgent::parseSipAddress(const std::string& sip)
{ {
resip::Data d(formatSipAddress(sip)); resip::Data d(formatSipAddress(sip));
resip::NameAddr nameaddr(d); resip::NameAddr nameaddr(d);
//resip::Uri uri(d); // resip::Uri uri(d);
if (!nameaddr.isWellFormed()) if (!nameaddr.isWellFormed())
result.mValid = false; result.mValid = false;
else else
@@ -633,14 +661,9 @@ void UserAgent::onCandidateGathered(PSession s, const char* address)
// Called when new connectivity check is finished // Called when new connectivity check is finished
void UserAgent::onCheckFinished(PSession s, const char* description) void UserAgent::onCheckFinished(PSession s, const char* description) {}
{
} void UserAgent::onLog(const char* msg) {}
void UserAgent::onLog(const char* msg)
{
}
void UserAgent::sendOffer(Session* session) void UserAgent::sendOffer(Session* session)
{ {
@@ -653,10 +676,12 @@ void UserAgent::sendOffer(Session* session)
if (session->mOriginVersion == 1) if (session->mOriginVersion == 1)
{ {
// Construct INVITE session // Construct INVITE session
auto msg = mDum->makeInviteSession(session->mRemotePeer, session->account()->mProfile, &sdp, session->mResipSession); auto msg =
mDum->makeInviteSession(session->mRemotePeer, session->account()->mProfile, &sdp, session->mResipSession);
// Include user headers // Include user headers
for (Session::UserHeaders::const_iterator iter = session->mUserHeaders.begin(); iter != session->mUserHeaders.end(); iter++) for (Session::UserHeaders::const_iterator iter = session->mUserHeaders.begin();
iter != session->mUserHeaders.end(); iter++)
{ {
const std::string& name = iter->first; const std::string& name = iter->first;
const std::string& value = iter->second; const std::string& value = iter->second;
@@ -680,7 +705,7 @@ void UserAgent::sendOffer(Session* session)
void UserAgent::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessage& response) void UserAgent::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessage& response)
{ {
ICELogInfo (<< "Registration got 200 response."); ICELogInfo(<< "Registration got 200 response.");
Lock l(mGuard); Lock l(mGuard);
// Find account by registration handle // Find account by registration handle
@@ -692,7 +717,7 @@ void UserAgent::onSuccess(resip::ClientRegistrationHandle h, const resip::SipMes
// Called when all of my bindings have been removed // Called when all of my bindings have been removed
void UserAgent::onRemoved(resip::ClientRegistrationHandle h, const resip::SipMessage& response) void UserAgent::onRemoved(resip::ClientRegistrationHandle h, const resip::SipMessage& response)
{ {
ICELogInfo( << "Registration is removed."); ICELogInfo(<< "Registration is removed.");
Lock l(mGuard); Lock l(mGuard);
@@ -712,7 +737,7 @@ int UserAgent::onRequestRetry(resip::ClientRegistrationHandle h, int retrySecond
// Registration retry interval is enabled in the Profile) // Registration retry interval is enabled in the Profile)
void UserAgent::onFailure(resip::ClientRegistrationHandle h, const resip::SipMessage& response) void UserAgent::onFailure(resip::ClientRegistrationHandle h, const resip::SipMessage& response)
{ {
ICELogInfo (<< "Registration failed with code " << response.header(resip::h_StatusLine).statusCode()); ICELogInfo(<< "Registration failed with code " << response.header(resip::h_StatusLine).statusCode());
Lock l(mGuard); Lock l(mGuard);
PAccount account = getAccount(response.header(resip::h_From)); PAccount account = getAccount(response.header(resip::h_From));
@@ -723,41 +748,31 @@ void UserAgent::onFailure(resip::ClientRegistrationHandle h, const resip::SipMes
#pragma endregion #pragma endregion
bool UserAgent::operator()(resip::Log::Level level, bool UserAgent::operator()(resip::Log::Level level, const resip::Subsystem& subsystem, const resip::Data& appName,
const resip::Subsystem& subsystem, const char* file, int line, const resip::Data& message,
const resip::Data& appName, const resip::Data& messageWithHeaders, const resip::Data& instanceName)
const char* file,
int line,
const resip::Data& message,
const resip::Data& messageWithHeaders,
const resip::Data& instanceName)
{ {
std::string filename = file; std::string filename = file;
std::stringstream ss; std::stringstream ss;
ss << "File " << strx::extractFilename(filename).c_str() << ", line " << line << ": " << message.c_str(); ss << "File " << strx::extractFilename(filename).c_str() << ", line " << line << ": " << message.c_str();
if (level <= resip::Log::Crit) if (level <= resip::Log::Crit)
ICELogCritical(<< ss.str()) ICELogCritical(<< ss.str()) else if (level <= resip::Log::Warning)
else ICELogError(<< ss.str().c_str()) else if (level < resip::Log::Debug)
if (level <= resip::Log::Warning) ICELogInfo(<< ss.str().c_str()) else ICELogDebug(<< ss.str().c_str()) return false;
ICELogError(<< ss.str().c_str())
else
if (level < resip::Log::Debug)
ICELogInfo(<< ss.str().c_str())
else
ICELogDebug(<< ss.str().c_str())
return false;
} }
#pragma region INVITE handler #pragma region INVITE handler
/// called when an initial INVITE or the intial response to an outoing invite /// called when an initial INVITE or the intial response to an outoing invite
void UserAgent::onNewSession(resip::ClientInviteSessionHandle h, resip::InviteSession::OfferAnswerType oat, const resip::SipMessage& msg) void UserAgent::onNewSession(resip::ClientInviteSessionHandle h, resip::InviteSession::OfferAnswerType oat,
const resip::SipMessage& msg)
{ {
} }
void UserAgent::onNewSession(resip::ServerInviteSessionHandle h, resip::InviteSession::OfferAnswerType oat, const resip::SipMessage& msg) void UserAgent::onNewSession(resip::ServerInviteSessionHandle h, resip::InviteSession::OfferAnswerType oat,
const resip::SipMessage& msg)
{ {
ResipSession* rs = CAST2RESIPSESSION(h); ResipSession* rs = CAST2RESIPSESSION(h);
if (!rs) if (!rs)
@@ -783,7 +798,7 @@ void UserAgent::onNewSession(resip::ServerInviteSessionHandle h, resip::InviteSe
// Save remote address // Save remote address
s->setRemoteAddress(h->peerAddr().uri().getAor().c_str()); s->setRemoteAddress(h->peerAddr().uri().getAor().c_str());
ICELogInfo( << "Session " << s->sessionId() << ": incoming."); ICELogInfo(<< "Session " << s->sessionId() << ": incoming.");
h->provisional(100); h->provisional(100);
@@ -793,15 +808,11 @@ void UserAgent::onNewSession(resip::ServerInviteSessionHandle h, resip::InviteSe
} }
/// Received a failure response from UAS /// Received a failure response from UAS
void UserAgent::onFailure(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onFailure(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called when an in-dialog provisional response is received that contains an SDP body /// called when an in-dialog provisional response is received that contains an SDP body
void UserAgent::onEarlyMedia(resip::ClientInviteSessionHandle h, const resip::SipMessage&, const resip::SdpContents&) void UserAgent::onEarlyMedia(resip::ClientInviteSessionHandle h, const resip::SipMessage&, const resip::SdpContents&) {}
{
}
/// called when dialog enters the Early state - typically after getting 18x /// called when dialog enters the Early state - typically after getting 18x
@@ -819,7 +830,6 @@ void UserAgent::onProvisional(resip::ClientInviteSessionHandle h, const resip::S
int responseCode = msg.header(resip::h_StatusLine).statusCode(); int responseCode = msg.header(resip::h_StatusLine).statusCode();
onSessionProvisional(s, responseCode); onSessionProvisional(s, responseCode);
} }
} }
@@ -835,7 +845,7 @@ void UserAgent::onConnected(resip::ClientInviteSessionHandle h, const resip::Sip
if (!s->mOfferAnswerCounter) if (!s->mOfferAnswerCounter)
{ {
ICELogInfo (<< "Session " << s->sessionId() << ": connected."); ICELogInfo(<< "Session " << s->sessionId() << ": connected.");
// Transfer user headers // Transfer user headers
if (h.isValid()) if (h.isValid())
@@ -843,7 +853,7 @@ void UserAgent::onConnected(resip::ClientInviteSessionHandle h, const resip::Sip
onSessionEstablished(s, EV_SIP, RtpPair<InternetAddress>()); onSessionEstablished(s, EV_SIP, RtpPair<InternetAddress>());
for (unsigned i=0; i<s->mStreamList.size(); i++) for (unsigned i = 0; i < s->mStreamList.size(); i++)
{ {
if (s->mStreamList[i].provider()) if (s->mStreamList[i].provider())
s->mStreamList[i].provider()->sessionEstablished(EV_SIP); s->mStreamList[i].provider()->sessionEstablished(EV_SIP);
@@ -864,7 +874,7 @@ void UserAgent::onConnected(resip::InviteSessionHandle h, const resip::SipMessag
if (!s) if (!s)
return; return;
ICELogInfo (<< "Session " << s->mSessionId << ": connected."); ICELogInfo(<< "Session " << s->mSessionId << ": connected.");
// Transfer user headers // Transfer user headers
if (h.isValid()) if (h.isValid())
@@ -872,7 +882,7 @@ void UserAgent::onConnected(resip::InviteSessionHandle h, const resip::SipMessag
onSessionEstablished(s, EV_SIP, RtpPair<InternetAddress>()); onSessionEstablished(s, EV_SIP, RtpPair<InternetAddress>());
for (unsigned i=0; i<s->mStreamList.size(); i++) for (unsigned i = 0; i < s->mStreamList.size(); i++)
{ {
if (s->mStreamList[i].provider()) if (s->mStreamList[i].provider())
s->mStreamList[i].provider()->sessionEstablished(EV_SIP); s->mStreamList[i].provider()->sessionEstablished(EV_SIP);
@@ -880,7 +890,8 @@ void UserAgent::onConnected(resip::InviteSessionHandle h, const resip::SipMessag
} }
void UserAgent::onTerminated(resip::InviteSessionHandle h, resip::InviteSessionHandler::TerminatedReason reason, const resip::SipMessage* related) void UserAgent::onTerminated(resip::InviteSessionHandle h, resip::InviteSessionHandler::TerminatedReason reason,
const resip::SipMessage* related)
{ {
ResipSession* rs = CAST2RESIPSESSION(h); ResipSession* rs = CAST2RESIPSESSION(h);
if (!rs) if (!rs)
@@ -889,7 +900,7 @@ void UserAgent::onTerminated(resip::InviteSessionHandle h, resip::InviteSessionH
if (!s) if (!s)
return; return;
ICELogInfo( << "Session " << s->mSessionId << ": terminated."); ICELogInfo(<< "Session " << s->mSessionId << ": terminated.");
int errorcode = 0; int errorcode = 0;
if (related) if (related)
@@ -907,9 +918,7 @@ void UserAgent::onTerminated(resip::InviteSessionHandle h, resip::InviteSessionH
/// called when a fork that was created through a 1xx never receives a 2xx /// called when a fork that was created through a 1xx never receives a 2xx
/// because another fork answered and this fork was canceled by a proxy. /// because another fork answered and this fork was canceled by a proxy.
void UserAgent::onForkDestroyed(resip::ClientInviteSessionHandle) void UserAgent::onForkDestroyed(resip::ClientInviteSessionHandle) {}
{
}
/// called when a 3xx with valid targets is encountered in an early dialog /// called when a 3xx with valid targets is encountered in an early dialog
@@ -918,9 +927,7 @@ void UserAgent::onForkDestroyed(resip::ClientInviteSessionHandle)
/// Basically an onTermintated that conveys more information. /// Basically an onTermintated that conveys more information.
/// checking for 3xx respones in onTerminated will not work as there may /// checking for 3xx respones in onTerminated will not work as there may
/// be no valid targets. /// be no valid targets.
void UserAgent::onRedirected(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onRedirected(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// Called when an SDP answer is received - has nothing to do with user /// Called when an SDP answer is received - has nothing to do with user
/// answering the call /// answering the call
@@ -936,7 +943,7 @@ void UserAgent::onAnswer(resip::InviteSessionHandle h, const resip::SipMessage&
bool iceAvailable = true; bool iceAvailable = true;
ICELogInfo( << "Session " << s->mSessionId << ": got answer."); ICELogInfo(<< "Session " << s->mSessionId << ": got answer.");
// Check for remote ICE credentials // Check for remote ICE credentials
std::string icePwd, iceUfrag; std::string icePwd, iceUfrag;
@@ -947,7 +954,7 @@ void UserAgent::onAnswer(resip::InviteSessionHandle h, const resip::SipMessage&
if (s->mStreamList.size() < sdp.session().media().size()) if (s->mStreamList.size() < sdp.session().media().size())
{ {
ICELogError( << "SDP answer has wrong number of streams"); ICELogError(<< "SDP answer has wrong number of streams");
h->end(); h->end();
return; return;
} }
@@ -963,14 +970,14 @@ void UserAgent::onAnswer(resip::InviteSessionHandle h, const resip::SipMessage&
std::list<resip::SdpContents::Session::Medium>::const_iterator mediaIter; std::list<resip::SdpContents::Session::Medium>::const_iterator mediaIter;
unsigned streamIndex = 0; unsigned streamIndex = 0;
for (mediaIter = sdp.session().media().begin(), streamIndex = 0; for (mediaIter = sdp.session().media().begin(), streamIndex = 0;
mediaIter != sdp.session().media().end() && streamIndex < s->mStreamList.size(); mediaIter != sdp.session().media().end() && streamIndex < s->mStreamList.size(); ++mediaIter, ++streamIndex)
++mediaIter, ++streamIndex)
{ {
Session::Stream& stream = s->mStreamList[streamIndex]; Session::Stream& stream = s->mStreamList[streamIndex];
const resip::SdpContents::Session::Medium& remoteStream = *mediaIter; const resip::SdpContents::Session::Medium& remoteStream = *mediaIter;
// Update remote default ip if available // Update remote default ip if available
const std::list<resip::SdpContents::Session::Connection>& streamConnections = remoteStream.getMediumConnections(); const std::list<resip::SdpContents::Session::Connection>& streamConnections =
remoteStream.getMediumConnections();
if (streamConnections.size()) if (streamConnections.size())
remoteDefaultIP = streamConnections.front().getAddress().c_str(); remoteDefaultIP = streamConnections.front().getAddress().c_str();
@@ -985,10 +992,10 @@ void UserAgent::onAnswer(resip::InviteSessionHandle h, const resip::SipMessage&
if (stream.provider()) if (stream.provider())
{ {
stream.provider()->sessionTerminated(); // close corresponding media stream.provider()->sessionTerminated(); // close corresponding media
stream.setProvider( PDataProvider() ); // free provider stream.setProvider(PDataProvider()); // free provider
SocketHeap::instance().freeSocketPair( stream.socket4() ); // close provider's socket ip4 SocketHeap::instance().freeSocketPair(stream.socket4()); // close provider's socket ip4
SocketHeap::instance().freeSocketPair( stream.socket6() ); // close provider's socket ip6 SocketHeap::instance().freeSocketPair(stream.socket6()); // close provider's socket ip6
s->mIceStack->removeStream( stream.iceInfo().mStreamId ); // remove stream from ice stack s->mIceStack->removeStream(stream.iceInfo().mStreamId); // remove stream from ice stack
} }
continue; continue;
} }
@@ -1016,10 +1023,11 @@ void UserAgent::onAnswer(resip::InviteSessionHandle h, const resip::SipMessage&
// Corresponding turn allocation will be removed in this case. // Corresponding turn allocation will be removed in this case.
try try
{ {
if (!s->mIceStack->processSdpOffer(stream.iceInfo().mStreamId, candidateVector, remoteDefaultIP, remoteDefaultPort, mConfig[CONFIG_DEFERRELAYED].asBool())) if (!s->mIceStack->processSdpOffer(stream.iceInfo().mStreamId, candidateVector, remoteDefaultIP,
remoteDefaultPort, mConfig[CONFIG_DEFERRELAYED].asBool()))
iceAvailable = false; iceAvailable = false;
} }
catch(...) catch (...)
{ {
iceAvailable = false; iceAvailable = false;
} }
@@ -1027,16 +1035,15 @@ void UserAgent::onAnswer(resip::InviteSessionHandle h, const resip::SipMessage&
// Process media description with provider // Process media description with provider
if (stream.provider()) if (stream.provider())
{ {
if (stream.provider()->processSdpOffer( remoteStream, Sdp_Answer )) if (stream.provider()->processSdpOffer(remoteStream, Sdp_Answer))
{ {
InternetAddress addr(remoteDefaultIP, remoteDefaultPort), addr2(remoteDefaultIP, remoteDefaultPort+1); InternetAddress addr(remoteDefaultIP, remoteDefaultPort), addr2(remoteDefaultIP, remoteDefaultPort + 1);
// See if remote stream has "rtcp" or "rtcp-mux" attributes // See if remote stream has "rtcp" or "rtcp-mux" attributes
if (remoteStream.exists("rtcp")) if (remoteStream.exists("rtcp"))
addr2.setPort( strx::toInt(remoteStream.getValues("rtcp").front().c_str(), remoteDefaultPort+1) ); addr2.setPort(strx::toInt(remoteStream.getValues("rtcp").front().c_str(), remoteDefaultPort + 1));
else else if (remoteStream.exists("rtcp-mux"))
if (remoteStream.exists("rtcp-mux")) addr2.setPort(remoteDefaultPort);
addr2.setPort( remoteDefaultPort );
stream.provider()->setDestinationAddress(RtpPair<InternetAddress>(addr, addr2)); stream.provider()->setDestinationAddress(RtpPair<InternetAddress>(addr, addr2));
mediasupported = true; mediasupported = true;
} }
@@ -1101,7 +1108,7 @@ void UserAgent::onOffer(resip::InviteSessionHandle h, const resip::SipMessage& m
if (sdp.session().exists("ice-ufrag")) if (sdp.session().exists("ice-ufrag"))
iceUfrag = sdp.session().getValues("ice-ufrag").front().c_str(); iceUfrag = sdp.session().getValues("ice-ufrag").front().c_str();
//ice::Stack& ice = *s->mIceStack; // ice::Stack& ice = *s->mIceStack;
uint64_t version = sdp.session().origin().getVersion(); uint64_t version = sdp.session().origin().getVersion();
std::string remoteIp = sdp.session().connection().getAddress().c_str(); std::string remoteIp = sdp.session().connection().getAddress().c_str();
@@ -1111,13 +1118,12 @@ void UserAgent::onOffer(resip::InviteSessionHandle h, const resip::SipMessage& m
{ {
code = s->processSdp(version, iceAvailable, icePwd, iceUfrag, remoteIp, sdp.session().media()); code = s->processSdp(version, iceAvailable, icePwd, iceUfrag, remoteIp, sdp.session().media());
} }
else else if (version == s->mRemoteOriginVersion)
if (version == s->mRemoteOriginVersion)
{ {
// Timer, answer with previous SDP // Timer, answer with previous SDP
//session->processTimer(); // session->processTimer();
} }
if (version == s->mRemoteOriginVersion+1) if (version == s->mRemoteOriginVersion + 1)
{ {
// Updated offer. Here we must check if ICE has to be restarted. // Updated offer. Here we must check if ICE has to be restarted.
code = s->processSdp(version, iceAvailable, icePwd, iceUfrag, remoteIp, sdp.session().media()); code = s->processSdp(version, iceAvailable, icePwd, iceUfrag, remoteIp, sdp.session().media());
@@ -1149,64 +1155,43 @@ void UserAgent::onOffer(resip::InviteSessionHandle h, const resip::SipMessage& m
/// called when an Invite w/out SDP is sent, or any other context which /// called when an Invite w/out SDP is sent, or any other context which
/// requires an SDP offer from the user /// requires an SDP offer from the user
void UserAgent::onOfferRequired(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onOfferRequired(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called if an offer in a UPDATE or re-INVITE was rejected - not real /// called if an offer in a UPDATE or re-INVITE was rejected - not real
/// useful. A SipMessage is provided if one is available /// useful. A SipMessage is provided if one is available
void UserAgent::onOfferRejected(resip::InviteSessionHandle, const resip::SipMessage* msg) void UserAgent::onOfferRejected(resip::InviteSessionHandle, const resip::SipMessage* msg) {}
{
}
/// called when INFO message is received /// called when INFO message is received
void UserAgent::onInfo(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onInfo(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called when response to INFO message is received /// called when response to INFO message is received
void UserAgent::onInfoSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onInfoSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
void UserAgent::onInfoFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onInfoFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called when MESSAGE message is received /// called when MESSAGE message is received
void UserAgent::onMessage(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onMessage(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called when response to MESSAGE message is received /// called when response to MESSAGE message is received
void UserAgent::onMessageSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onMessageSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
void UserAgent::onMessageFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onMessageFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called when an REFER message is received. The refer is accepted or /// called when an REFER message is received. The refer is accepted or
/// rejected using the server subscription. If the offer is accepted, /// rejected using the server subscription. If the offer is accepted,
/// DialogUsageManager::makeInviteSessionFromRefer can be used to create an /// DialogUsageManager::makeInviteSessionFromRefer can be used to create an
/// InviteSession that will send notify messages using the ServerSubscription /// InviteSession that will send notify messages using the ServerSubscription
void UserAgent::onRefer(resip::InviteSessionHandle, resip::ServerSubscriptionHandle, const resip::SipMessage& msg) void UserAgent::onRefer(resip::InviteSessionHandle, resip::ServerSubscriptionHandle, const resip::SipMessage& msg) {}
{
}
void UserAgent::onReferNoSub(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onReferNoSub(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called when an REFER message receives a failure response /// called when an REFER message receives a failure response
void UserAgent::onReferRejected(resip::InviteSessionHandle, const resip::SipMessage& msg) void UserAgent::onReferRejected(resip::InviteSessionHandle, const resip::SipMessage& msg) {}
{
}
/// called when an REFER message receives an accepted response /// called when an REFER message receives an accepted response
void UserAgent::onReferAccepted(resip::InviteSessionHandle, resip::ClientSubscriptionHandle, const resip::SipMessage& msg) void UserAgent::onReferAccepted(resip::InviteSessionHandle, resip::ClientSubscriptionHandle,
const resip::SipMessage& msg)
{ {
} }
@@ -1215,25 +1200,21 @@ void UserAgent::onDnsResult(const resip::DNSResult<resip::DnsHostRecord>& result
if (result.status == 0) if (result.status == 0)
{ {
resip::Data foundAddress = result.records.front().host(); resip::Data foundAddress = result.records.front().host();
ICELogInfo( << "Success to resolve STUN/TURN address to " << foundAddress.c_str()); ICELogInfo(<< "Success to resolve STUN/TURN address to " << foundAddress.c_str());
mConfig[CONFIG_STUNSERVER_IP] = std::string(foundAddress.c_str()); mConfig[CONFIG_STUNSERVER_IP] = std::string(foundAddress.c_str());
onStart(0); onStart(0);
} }
else else
{ {
ICELogError( << "Failed to resolve STUN or TURN server IP address."); ICELogError(<< "Failed to resolve STUN or TURN server IP address.");
int startCode = mConfig[CONFIG_STUNSERVER_NAME].asStdString().empty() ? 0 : 503; int startCode = mConfig[CONFIG_STUNSERVER_NAME].asStdString().empty() ? 0 : 503;
onStart(startCode); onStart(startCode);
} }
} }
void UserAgent::onDnsResult(const resip::DNSResult<resip::DnsAAAARecord>& result) void UserAgent::onDnsResult(const resip::DNSResult<resip::DnsAAAARecord>& result) {}
{
}
void UserAgent::onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>& result) void UserAgent::onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>& result) {}
{
}
void UserAgent::onDnsResult(const resip::DNSResult<resip::DnsNaptrRecord>& result) void UserAgent::onDnsResult(const resip::DNSResult<resip::DnsNaptrRecord>& result)
{ {
@@ -1281,28 +1262,20 @@ int UserAgent::onRequestRetry(resip::ClientPublicationHandle, int retrySeconds,
} }
void UserAgent::onPublicationSuccess(PAccount account) {}
void UserAgent::onPublicationSuccess(PAccount account) void UserAgent::onPublicationTerminated(PAccount account, int code) {}
{
}
void UserAgent::onPublicationTerminated(PAccount account, int code)
{
}
#pragma endregion #pragma endregion
#pragma region Subscriptions #pragma region Subscriptions
void UserAgent::onClientObserverStart(PClientObserver observer) void UserAgent::onClientObserverStart(PClientObserver observer) {}
{
}
void UserAgent::onClientObserverStop(PClientObserver observer, int code) void UserAgent::onClientObserverStop(PClientObserver observer, int code) {}
{
}
void UserAgent::onPresenceUpdate(PClientObserver observer, const std::string& peer, bool online, const std::string& content) void UserAgent::onPresenceUpdate(PClientObserver observer, const std::string& peer, bool online,
const std::string& content)
{ {
} }
@@ -1351,13 +1324,9 @@ void UserAgent::onTerminated(resip::ServerSubscriptionHandle h)
} }
} }
void UserAgent::onServerObserverStart(PServerObserver observer) void UserAgent::onServerObserverStart(PServerObserver observer) {}
{
}
void UserAgent::onServerObserverStop(PServerObserver observer, int code) void UserAgent::onServerObserverStop(PServerObserver observer, int code) {}
{
}
void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMessage& notify) void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMessage& notify)
{ {
@@ -1393,14 +1362,13 @@ void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMess
bool online = pidf->getSimpleStatus(&body); bool online = pidf->getSimpleStatus(&body);
onPresenceUpdate(observer, observer->peer(), online, std::string(body.c_str(), body.size())); onPresenceUpdate(observer, observer->peer(), online, std::string(body.c_str(), body.size()));
} }
else else if (resip::MultipartRelatedContents* mr = dynamic_cast<resip::MultipartRelatedContents*>(contents))
if (resip::MultipartRelatedContents* mr = dynamic_cast<resip::MultipartRelatedContents*>(contents))
{ {
resip::MultipartRelatedContents::Parts& parts = mr->parts(); resip::MultipartRelatedContents::Parts& parts = mr->parts();
for( resip::MultipartRelatedContents::Parts::const_iterator i = parts.begin(); i != parts.end(); ++i) for (resip::MultipartRelatedContents::Parts::const_iterator i = parts.begin(); i != parts.end(); ++i)
{ {
resip::Contents* c = *i; resip::Contents* c = *i;
assert( c ); assert(c);
resip::Mime m = c->getType(); resip::Mime m = c->getType();
if (resip::Rlmi* rlmi = dynamic_cast<resip::Rlmi*>(c)) if (resip::Rlmi* rlmi = dynamic_cast<resip::Rlmi*>(c))
{ {
@@ -1414,7 +1382,7 @@ void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMess
resip::Data tag = c.getTag(); resip::Data tag = c.getTag();
if (tag != "list") if (tag != "list")
{ {
ICELogError( << "Failed to find <list> tag in rlmi"); ICELogError(<< "Failed to find <list> tag in rlmi");
} }
else else
{ {
@@ -1422,7 +1390,7 @@ void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMess
{ {
if (c.getTag() != "resource") if (c.getTag() != "resource")
{ {
ICELogError( << "Failed to find <resource> tag in rlmi"); ICELogError(<< "Failed to find <resource> tag in rlmi");
} }
else else
{ {
@@ -1442,14 +1410,13 @@ void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMess
// Save result // Save result
if (instance) if (instance)
availableContacts.push_back( attrIter->second ); availableContacts.push_back(attrIter->second);
} }
} }
} }
} }
} }
else else if (resip::Pidf* pidf = dynamic_cast<resip::Pidf*>(c))
if (resip::Pidf* pidf = dynamic_cast<resip::Pidf*>(c))
{ {
resip::Data body = pidf->getBodyData(); resip::Data body = pidf->getBodyData();
bool online = pidf->getSimpleStatus(&body); bool online = pidf->getSimpleStatus(&body);
@@ -1459,7 +1426,8 @@ void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMess
onPresenceUpdate(observer, entity.c_str(), online, std::string(body.c_str(), body.size())); onPresenceUpdate(observer, entity.c_str(), online, std::string(body.c_str(), body.size()));
// Drop corresponding record from availableContacts // Drop corresponding record from availableContacts
std::vector<resip::Data>::iterator ci = std::find(availableContacts.begin(), availableContacts.end(), entity); std::vector<resip::Data>::iterator ci =
std::find(availableContacts.begin(), availableContacts.end(), entity);
if (ci != availableContacts.end()) if (ci != availableContacts.end())
availableContacts.erase(ci); availableContacts.erase(ci);
} }
@@ -1467,7 +1435,7 @@ void UserAgent::onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMess
} }
} }
for (unsigned i=0; i<availableContacts.size(); i++) for (unsigned i = 0; i < availableContacts.size(); i++)
onPresenceUpdate(observer, availableContacts[i].c_str(), false, std::string()); onPresenceUpdate(observer, availableContacts[i].c_str(), false, std::string());
} }
@@ -1491,7 +1459,7 @@ int UserAgent::onRequestRetry(resip::ClientSubscriptionHandle, int retrySeconds,
return -1; return -1;
} }
//subscription can be ended through a notify or a failure response. // subscription can be ended through a notify or a failure response.
void UserAgent::onTerminated(resip::ClientSubscriptionHandle h, const resip::SipMessage* msg) void UserAgent::onTerminated(resip::ClientSubscriptionHandle h, const resip::SipMessage* msg)
{ {
// TODO - check for refer notication // TODO - check for refer notication
@@ -1531,22 +1499,16 @@ void UserAgent::onNewSubscription(resip::ClientSubscriptionHandle h, const resip
} }
/// called to allow app to adorn a message. /// called to allow app to adorn a message.
void UserAgent::onReadyToSend(resip::ClientSubscriptionHandle, resip::SipMessage& msg) void UserAgent::onReadyToSend(resip::ClientSubscriptionHandle, resip::SipMessage& msg) {}
{
}
void UserAgent::onNotifyNotReceived(resip::ClientSubscriptionHandle) void UserAgent::onNotifyNotReceived(resip::ClientSubscriptionHandle) {}
{
}
/// Called when a TCP or TLS flow to the server has terminated. This can be caused by socket /// Called when a TCP or TLS flow to the server has terminated. This can be caused by socket
/// errors, or missing CRLF keep alives pong responses from the server. /// errors, or missing CRLF keep alives pong responses from the server.
// Called only if clientOutbound is enabled on the UserProfile and the first hop server // Called only if clientOutbound is enabled on the UserProfile and the first hop server
/// supports RFC5626 (outbound). /// supports RFC5626 (outbound).
/// Default implementation is to re-form the subscription using a new flow /// Default implementation is to re-form the subscription using a new flow
void UserAgent::onFlowTerminated(resip::ClientSubscriptionHandle) void UserAgent::onFlowTerminated(resip::ClientSubscriptionHandle) {}
{
}
#pragma endregion #pragma endregion
#pragma region PagerHandler #pragma region PagerHandler
@@ -1560,14 +1522,16 @@ void UserAgent::onSuccess(resip::ClientPagerMessageHandle h, const resip::SipMes
onMessageSent(getAccount(status.header(resip::h_From)), s->sessionId(), s->remoteAddress(), s->tag()); onMessageSent(getAccount(status.header(resip::h_From)), s->sessionId(), s->remoteAddress(), s->tag());
} }
void UserAgent::onFailure(resip::ClientPagerMessageHandle h, const resip::SipMessage& status, std::unique_ptr<resip::Contents> contents) void UserAgent::onFailure(resip::ClientPagerMessageHandle h, const resip::SipMessage& status,
std::unique_ptr<resip::Contents> contents)
{ {
if (!h.isValid()) if (!h.isValid())
return; return;
ResipSession* s = CAST2RESIPSESSION(h); ResipSession* s = CAST2RESIPSESSION(h);
if (!s) if (!s)
return; return;
onMessageFailed(getAccount(status.header(resip::h_From)), s->sessionId(), s->remoteAddress(), status.header(resip::h_StatusLine).statusCode(), s->tag()); onMessageFailed(getAccount(status.header(resip::h_From)), s->sessionId(), s->remoteAddress(),
status.header(resip::h_StatusLine).statusCode(), s->tag());
} }
void UserAgent::onMessageArrived(resip::ServerPagerMessageHandle h, const resip::SipMessage& message) void UserAgent::onMessageArrived(resip::ServerPagerMessageHandle h, const resip::SipMessage& message)
@@ -1590,20 +1554,20 @@ void UserAgent::onMessageArrived(resip::ServerPagerMessageHandle h, const resip:
void UserAgent::updateInterfaceList() void UserAgent::updateInterfaceList()
{ {
//ICEImpl::ICENetworkHelper::instance().reload(); // ICEImpl::ICENetworkHelper::instance().reload();
} }
void UserAgent::onMessageArrived(PAccount /*account*/, const std::string& /*peer*/, const void* /*ptr*/, unsigned /*length*/) void UserAgent::onMessageArrived(PAccount /*account*/, const std::string& /*peer*/, const void* /*ptr*/,
unsigned /*length*/)
{ {
} }
void UserAgent::onMessageFailed(PAccount /*account*/, int /*id*/, const std::string& /*peer*/, int /*code*/, void* /*tag*/) void UserAgent::onMessageFailed(PAccount /*account*/, int /*id*/, const std::string& /*peer*/, int /*code*/,
void* /*tag*/)
{ {
} }
void UserAgent::onMessageSent(PAccount /*account*/, int /*id*/, const std::string& /*peer*/, void* /*tag*/) void UserAgent::onMessageSent(PAccount /*account*/, int /*id*/, const std::string& /*peer*/, void* /*tag*/) {}
{
}
VariantMap& UserAgent::config() VariantMap& UserAgent::config()
{ {
@@ -1678,7 +1642,8 @@ PSession UserAgent::getUserSession(int sessionId)
PAccount UserAgent::getAccount(const resip::NameAddr& myAddr) PAccount UserAgent::getAccount(const resip::NameAddr& myAddr)
{ {
PAccount acc; PAccount acc;
for (AccountSet::iterator accountIter = mAccountSet.begin(); accountIter != mAccountSet.end() && !acc; accountIter++) for (AccountSet::iterator accountIter = mAccountSet.begin(); accountIter != mAccountSet.end() && !acc;
accountIter++)
if ((*accountIter)->isResponsibleFor(myAddr)) if ((*accountIter)->isResponsibleFor(myAddr))
acc = *accountIter; acc = *accountIter;
@@ -1688,7 +1653,8 @@ PAccount UserAgent::getAccount(const resip::NameAddr& myAddr)
PAccount UserAgent::getAccount(Account* account) PAccount UserAgent::getAccount(Account* account)
{ {
PAccount acc; PAccount acc;
for (AccountSet::iterator accountIter = mAccountSet.begin(); accountIter != mAccountSet.end() && !acc; accountIter++) for (AccountSet::iterator accountIter = mAccountSet.begin(); accountIter != mAccountSet.end() && !acc;
accountIter++)
if (accountIter->get() == account) if (accountIter->get() == account)
acc = *accountIter; acc = *accountIter;
@@ -1697,7 +1663,14 @@ PAccount UserAgent::getAccount(Account* account)
PAccount UserAgent::getAccount(int sessionId) PAccount UserAgent::getAccount(int sessionId)
{ {
auto profileIter = std::find_if(mAccountSet.begin(), mAccountSet.end(), [=](const AccountSet::value_type& v) {if (v->mRegistration) return v->mRegistration->sessionId() == sessionId; else return false;}); auto profileIter = std::find_if(mAccountSet.begin(), mAccountSet.end(),
[=](const AccountSet::value_type& v)
{
if (v->mRegistration)
return v->mRegistration->sessionId() == sessionId;
else
return false;
});
return (profileIter != mAccountSet.end()) ? *profileIter : PAccount(); return (profileIter != mAccountSet.end()) ? *profileIter : PAccount();
} }
+35 -26
View File
@@ -87,7 +87,8 @@ enum
CONFIG_ICEUSERNAME, // Optional username for TURN server. String value. CONFIG_ICEUSERNAME, // Optional username for TURN server. String value.
CONFIG_ICEPASSWORD, // Optional password for TURN server. String value. CONFIG_ICEPASSWORD, // Optional password for TURN server. String value.
CONFIG_SIPS, // Marks if account credentials are sips: scheme. Boolean value. CONFIG_SIPS, // Marks if account credentials are sips: scheme. Boolean value.
CONFIG_STUNSERVER_IP, // Optional IP address of STUN/TURN server. String value. It is better to use CONFIG_STUNSERVER_NAME. CONFIG_STUNSERVER_IP, // Optional IP address of STUN/TURN server. String value. It is better to use
// CONFIG_STUNSERVER_NAME.
CONFIG_STUNSERVER_NAME, // Host name of STUN/TURN server. stun.xten.com for example. String value. CONFIG_STUNSERVER_NAME, // Host name of STUN/TURN server. stun.xten.com for example. String value.
CONFIG_STUNSERVER_PORT, // Port number of STUN/TURN server. Integer value. CONFIG_STUNSERVER_PORT, // Port number of STUN/TURN server. Integer value.
CONFIG_USERAGENT, // Name of user agent in SIP headers. String value. CONFIG_USERAGENT, // Name of user agent in SIP headers. String value.
@@ -98,7 +99,8 @@ enum
CONFIG_DNS_CACHE_TIME, // DNS cache time; default is 86400 seconds CONFIG_DNS_CACHE_TIME, // DNS cache time; default is 86400 seconds
CONFIG_PRESENCE_ID, // Tuple ID used in presence publishing; determines source device CONFIG_PRESENCE_ID, // Tuple ID used in presence publishing; determines source device
CONFIG_ROOTCERT, // Additional root cert in PEM format; string. CONFIG_ROOTCERT, // Additional root cert in PEM format; string.
CONFIG_CACHECREDENTIALS, // Attempt to cache credentials that comes in response from PBX. Use them when possible to reduce number of steps of SIP transaction CONFIG_CACHECREDENTIALS, // Attempt to cache credentials that comes in response from PBX. Use them when possible to
// reduce number of steps of SIP transaction
CONFIG_RTCP_ATTR, // Use "rtcp" attribute in sdp. Default value is true. CONFIG_RTCP_ATTR, // Use "rtcp" attribute in sdp. Default value is true.
CONFIG_MULTIPLEXING, // Do rtp/rtcp multiplexing CONFIG_MULTIPLEXING, // Do rtp/rtcp multiplexing
CONFIG_DEFERRELAYED, // Defer relayed media path CONFIG_DEFERRELAYED, // Defer relayed media path
@@ -139,11 +141,11 @@ enum
RemoteBye, RemoteBye,
LocalCancel, LocalCancel,
RemoteCancel, RemoteCancel,
Rejected, //Only as UAS, UAC has distinct onFailure callback Rejected, // Only as UAS, UAC has distinct onFailure callback
Referred Referred
}; };
class UserAgent: public resip::ClientRegistrationHandler, class UserAgent : public resip::ClientRegistrationHandler,
public resip::InviteSessionHandler, public resip::InviteSessionHandler,
public resip::DumShutdownHandler, public resip::DumShutdownHandler,
public resip::ExternalLogger, public resip::ExternalLogger,
@@ -153,15 +155,17 @@ class UserAgent: public resip::ClientRegistrationHandler,
public resip::ClientPagerMessageHandler, public resip::ClientPagerMessageHandler,
public resip::ServerPagerMessageHandler, public resip::ServerPagerMessageHandler,
public resip::ClientPublicationHandler public resip::ClientPublicationHandler
//public resip::InternalTransport::TransportLogger // public resip::InternalTransport::TransportLogger
{ {
friend class Account; friend class Account;
friend class Session; friend class Session;
friend class ResipSession; friend class ResipSession;
friend class NATDecorator; friend class NATDecorator;
friend class WatcherQueue; friend class WatcherQueue;
public: public:
/* Compares two sip addresses. Returns true if they represent the same entity - user and domain are the same. Otherwise returns false. */ /* Compares two sip addresses. Returns true if they represent the same entity - user and domain are the same.
* Otherwise returns false. */
static bool compareSipAddresses(const std::string& sip1, const std::string& sip2); static bool compareSipAddresses(const std::string& sip1, const std::string& sip2);
static std::string formatSipAddress(const std::string& sip); static std::string formatSipAddress(const std::string& sip);
static bool isSipAddressValid(const std::string& sip); static bool isSipAddressValid(const std::string& sip);
@@ -187,7 +191,8 @@ public:
This is asynchronous method. onStop() event will be called later */ This is asynchronous method. onStop() event will be called later */
void shutdown(); void shutdown();
/* Emergency stop. Please always call shutdown() before this. Kills registration, sessions & presence - everything. onStop() is called in context of this method. */ /* Emergency stop. Please always call shutdown() before this. Kills registration, sessions & presence - everything.
* onStop() is called in context of this method. */
void stop(); void stop();
/* Checks if user agent is active (started). */ /* Checks if user agent is active (started). */
@@ -268,7 +273,8 @@ public:
virtual void onClientObserverStop(PClientObserver observer, int code); virtual void onClientObserverStop(PClientObserver observer, int code);
virtual void onServerObserverStop(PServerObserver observer, int code); virtual void onServerObserverStop(PServerObserver observer, int code);
virtual void onPresenceUpdate(PClientObserver observer, const std::string& peer, bool online, const std::string& content); virtual void onPresenceUpdate(PClientObserver observer, const std::string& peer, bool online,
const std::string& content);
virtual void onMessageArrived(PAccount account, const std::string& peer, const void* ptr, unsigned length); virtual void onMessageArrived(PAccount account, const std::string& peer, const void* ptr, unsigned length);
virtual void onMessageFailed(PAccount account, int id, const std::string& peer, int code, void* tag); virtual void onMessageFailed(PAccount account, int id, const std::string& peer, int code, void* tag);
virtual void onMessageSent(PAccount account, int id, const std::string& peer, void* tag); virtual void onMessageSent(PAccount account, int id, const std::string& peer, void* tag);
@@ -280,14 +286,17 @@ public:
// InviteSessionHandler implementation // InviteSessionHandler implementation
#pragma region InviteSessionHandler implementation #pragma region InviteSessionHandler implementation
/// called when an initial INVITE or the intial response to an outoing invite /// called when an initial INVITE or the intial response to an outoing invite
virtual void onNewSession(resip::ClientInviteSessionHandle, resip::InviteSession::OfferAnswerType oat, const resip::SipMessage& msg) override; virtual void onNewSession(resip::ClientInviteSessionHandle, resip::InviteSession::OfferAnswerType oat,
virtual void onNewSession(resip::ServerInviteSessionHandle, resip::InviteSession::OfferAnswerType oat, const resip::SipMessage& msg) override; const resip::SipMessage& msg) override;
virtual void onNewSession(resip::ServerInviteSessionHandle, resip::InviteSession::OfferAnswerType oat,
const resip::SipMessage& msg) override;
/// Received a failure response from UAS /// Received a failure response from UAS
virtual void onFailure(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override; virtual void onFailure(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override;
/// called when an in-dialog provisional response is received that contains an SDP body /// called when an in-dialog provisional response is received that contains an SDP body
virtual void onEarlyMedia(resip::ClientInviteSessionHandle, const resip::SipMessage&, const resip::SdpContents&) override; virtual void onEarlyMedia(resip::ClientInviteSessionHandle, const resip::SipMessage&,
const resip::SdpContents&) override;
/// called when dialog enters the Early state - typically after getting 18x /// called when dialog enters the Early state - typically after getting 18x
virtual void onProvisional(resip::ClientInviteSessionHandle, const resip::SipMessage&) override; virtual void onProvisional(resip::ClientInviteSessionHandle, const resip::SipMessage&) override;
@@ -298,7 +307,8 @@ public:
/// called when a dialog initiated as a UAS enters the connected state /// called when a dialog initiated as a UAS enters the connected state
virtual void onConnected(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onConnected(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
virtual void onTerminated(resip::InviteSessionHandle, resip::InviteSessionHandler::TerminatedReason reason, const resip::SipMessage* related=0) override; virtual void onTerminated(resip::InviteSessionHandle, resip::InviteSessionHandler::TerminatedReason reason,
const resip::SipMessage* related = 0) override;
/// called when a fork that was created through a 1xx never receives a 2xx /// called when a fork that was created through a 1xx never receives a 2xx
/// because another fork answered and this fork was canceled by a proxy. /// because another fork answered and this fork was canceled by a proxy.
@@ -345,7 +355,8 @@ public:
/// rejected using the server subscription. If the offer is accepted, /// rejected using the server subscription. If the offer is accepted,
/// DialogUsageManager::makeInviteSessionFromRefer can be used to create an /// DialogUsageManager::makeInviteSessionFromRefer can be used to create an
/// InviteSession that will send notify messages using the ServerSubscription /// InviteSession that will send notify messages using the ServerSubscription
virtual void onRefer(resip::InviteSessionHandle, resip::ServerSubscriptionHandle, const resip::SipMessage& msg) override; virtual void onRefer(resip::InviteSessionHandle, resip::ServerSubscriptionHandle,
const resip::SipMessage& msg) override;
virtual void onReferNoSub(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onReferNoSub(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
@@ -353,7 +364,8 @@ public:
virtual void onReferRejected(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onReferRejected(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called when an REFER message receives an accepted response /// called when an REFER message receives an accepted response
virtual void onReferAccepted(resip::InviteSessionHandle, resip::ClientSubscriptionHandle, const resip::SipMessage& msg) override; virtual void onReferAccepted(resip::InviteSessionHandle, resip::ClientSubscriptionHandle,
const resip::SipMessage& msg) override;
#pragma endregion #pragma endregion
// ClientRegistrationHandler implementation // ClientRegistrationHandler implementation
@@ -377,14 +389,9 @@ public:
#pragma region ExternalLogger implementation #pragma region ExternalLogger implementation
/** return true to also do default logging, false to suppress default logging. */ /** return true to also do default logging, false to suppress default logging. */
virtual bool operator()(resip::Log::Level level, virtual bool operator()(resip::Log::Level level, const resip::Subsystem& subsystem, const resip::Data& appName,
const resip::Subsystem& subsystem, const char* file, int line, const resip::Data& message,
const resip::Data& appName, const resip::Data& messageWithHeaders, const resip::Data& instanceName) override;
const char* file,
int line,
const resip::Data& message,
const resip::Data& messageWithHeaders,
const resip::Data& instanceName) override;
#pragma endregion #pragma endregion
#pragma region DnsResultSink implementation #pragma region DnsResultSink implementation
@@ -413,13 +420,13 @@ public:
void onUpdatePending(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override; void onUpdatePending(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override;
void onUpdateActive(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override; void onUpdateActive(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override;
//unknown Subscription-State value // unknown Subscription-State value
void onUpdateExtension(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override; void onUpdateExtension(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override;
int onRequestRetry(resip::ClientSubscriptionHandle, int retrySeconds, const resip::SipMessage& notify) override; int onRequestRetry(resip::ClientSubscriptionHandle, int retrySeconds, const resip::SipMessage& notify) override;
//subscription can be ended through a notify or a failure response. // subscription can be ended through a notify or a failure response.
void onTerminated(resip::ClientSubscriptionHandle, const resip::SipMessage* msg) override; void onTerminated(resip::ClientSubscriptionHandle, const resip::SipMessage* msg) override;
//not sure if this has any value. // not sure if this has any value.
void onNewSubscription(resip::ClientSubscriptionHandle, const resip::SipMessage& notify) override; void onNewSubscription(resip::ClientSubscriptionHandle, const resip::SipMessage& notify) override;
/// called to allow app to adorn a message. /// called to allow app to adorn a message.
@@ -438,11 +445,13 @@ public:
#pragma region PagerHandler #pragma region PagerHandler
void onSuccess(resip::ClientPagerMessageHandle, const resip::SipMessage& status) override; void onSuccess(resip::ClientPagerMessageHandle, const resip::SipMessage& status) override;
void onFailure(resip::ClientPagerMessageHandle, const resip::SipMessage& status, std::unique_ptr<resip::Contents> contents) override; void onFailure(resip::ClientPagerMessageHandle, const resip::SipMessage& status,
std::unique_ptr<resip::Contents> contents) override;
void onMessageArrived(resip::ServerPagerMessageHandle, const resip::SipMessage& message) override; void onMessageArrived(resip::ServerPagerMessageHandle, const resip::SipMessage& message) override;
#pragma endregion #pragma endregion
void onDumCanBeDeleted() override; void onDumCanBeDeleted() override;
protected: protected:
// Mutex to protect this instance // Mutex to protect this instance
Mutex mGuard; Mutex mGuard;
+8 -11
View File
@@ -1,19 +1,16 @@
#include "EP_NetworkQueue.h" #include "EP_NetworkQueue.h"
#include "EP_Engine.h" #include "EP_Engine.h"
WatcherQueue::WatcherQueue(UserAgent& ua) WatcherQueue::WatcherQueue(UserAgent& ua) : mActiveId(0), mAgent(ua) {}
:mActiveId(0), mAgent(ua)
{}
WatcherQueue::~WatcherQueue() WatcherQueue::~WatcherQueue() {}
{}
int WatcherQueue::add(const std::string& peer, const std::string& package, void* tag) int WatcherQueue::add(const std::string& peer, const std::string& package, void* tag)
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
// Check if queue has similar item // Check if queue has similar item
for (unsigned i=0; i<mItemList.size(); i++) for (unsigned i = 0; i < mItemList.size(); i++)
{ {
Item& item = mItemList[i]; Item& item = mItemList[i];
if (item.mTarget == peer && item.mPackage == package && item.mState != Item::State_Deleting) if (item.mTarget == peer && item.mPackage == package && item.mState != Item::State_Deleting)
@@ -43,7 +40,7 @@ void WatcherQueue::remove(int id)
ice::Lock l(mGuard); ice::Lock l(mGuard);
// Check if queue has similar item // Check if queue has similar item
for (auto& item: mItemList) for (auto& item : mItemList)
{ {
if (item.mId == id && id) if (item.mId == id && id)
{ {
@@ -60,7 +57,7 @@ void WatcherQueue::refresh(int id)
ice::Lock l(mGuard); ice::Lock l(mGuard);
// Check if queue has similar item // Check if queue has similar item
for (auto& item: mItemList) for (auto& item : mItemList)
{ {
if (item.mId == id && id) if (item.mId == id && id)
{ {
@@ -77,7 +74,7 @@ void WatcherQueue::process()
{ {
// Find next item to process // Find next item to process
ItemList::iterator i = mItemList.begin(); ItemList::iterator i = mItemList.begin();
for (;i != mItemList.end() && !i->scheduled(); i++) for (; i != mItemList.end() && !i->scheduled(); i++)
; ;
if (i == mItemList.end()) if (i == mItemList.end())
return; return;
@@ -161,7 +158,7 @@ void WatcherQueue::onEstablished(int id, int code)
WatcherQueue::ItemList::iterator WatcherQueue::findById(int id) WatcherQueue::ItemList::iterator WatcherQueue::findById(int id)
{ {
for (ItemList::iterator i=mItemList.begin(); i != mItemList.end(); i++) for (ItemList::iterator i = mItemList.begin(); i != mItemList.end(); i++)
if (i->mId == id) if (i->mId == id)
return i; return i;
return mItemList.end(); return mItemList.end();
@@ -170,7 +167,7 @@ WatcherQueue::ItemList::iterator WatcherQueue::findById(int id)
void WatcherQueue::clear() void WatcherQueue::clear()
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
for (ItemList::iterator i=mItemList.begin(); i != mItemList.end(); i++) for (ItemList::iterator i = mItemList.begin(); i != mItemList.end(); i++)
{ {
if (i->mHandle.isValid()) if (i->mHandle.isValid())
i->mHandle->end(); i->mHandle->end();
+3 -3
View File
@@ -35,12 +35,12 @@ public:
void* mTag = nullptr; // User tag void* mTag = nullptr; // User tag
int mId = 0; // Related session ID - it is always non-zero (zero is here for initialization only) int mId = 0; // Related session ID - it is always non-zero (zero is here for initialization only)
Item() Item() {}
{}
bool scheduled() bool scheduled()
{ {
return mState == State_ScheduledToAdd || mState == State_ScheduledToDelete || mState == State_ScheduledToRefresh; return mState == State_ScheduledToAdd || mState == State_ScheduledToDelete ||
mState == State_ScheduledToRefresh;
} }
}; };
WatcherQueue(UserAgent& agent); WatcherQueue(UserAgent& agent);
+4 -13
View File
@@ -9,13 +9,9 @@
#include <resip/stack/Pidf.hxx> #include <resip/stack/Pidf.hxx>
#include <resip/dum/ClientSubscription.hxx> #include <resip/dum/ClientSubscription.hxx>
ClientObserver::ClientObserver() ClientObserver::ClientObserver() {}
{
}
ClientObserver::~ClientObserver() ClientObserver::~ClientObserver() {}
{
}
void ClientObserver::refresh() void ClientObserver::refresh()
{ {
@@ -27,8 +23,7 @@ void ClientObserver::stop()
{ {
if (mHandle.isValid()) if (mHandle.isValid())
mHandle->end(); mHandle->end();
else else if (mSession)
if (mSession)
{ {
mSession->runTerminatedEvent(ResipSession::Type_Subscription); mSession->runTerminatedEvent(ResipSession::Type_Subscription);
if (mSession) if (mSession)
@@ -42,11 +37,7 @@ std::string ClientObserver::peer()
return mPeer; return mPeer;
} }
ServerObserver::ServerObserver() ServerObserver::ServerObserver() : mState(State_Incoming) {}
:mState(State_Incoming)
{
}
ServerObserver::~ServerObserver() ServerObserver::~ServerObserver()
{ {
+2
View File
@@ -23,6 +23,7 @@ class ClientObserver
{ {
friend class Account; friend class Account;
friend class UserAgent; friend class UserAgent;
public: public:
ClientObserver(); ClientObserver();
~ClientObserver(); ~ClientObserver();
@@ -43,6 +44,7 @@ typedef std::shared_ptr<ClientObserver> PClientObserver;
class ServerObserver class ServerObserver
{ {
friend class UserAgent; friend class UserAgent;
public: public:
ServerObserver(); ServerObserver();
~ServerObserver(); ~ServerObserver();
+13 -12
View File
@@ -16,8 +16,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifdef _WIN32 #ifdef _WIN32
# include <winsock2.h> #include <winsock2.h>
# include <windows.h> #include <windows.h>
#endif #endif
#include <algorithm> #include <algorithm>
@@ -44,7 +44,6 @@ ReliableTunnel::ReliableTunnel(const char* streamname)
mBandwidth = 0; mBandwidth = 0;
mExitSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mExitSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
mDataSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mDataSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
} }
ReliableTunnel::~ReliableTunnel() ReliableTunnel::~ReliableTunnel()
@@ -141,7 +140,7 @@ DatagramSocket& ReliableTunnel::socket(int family)
bool ReliableTunnel::processSdpOffer(const resip::SdpContents::Session::Medium& media) bool ReliableTunnel::processSdpOffer(const resip::SdpContents::Session::Medium& media)
{ {
//check for default port number // check for default port number
mDestination.setPort(media.port()); mDestination.setPort(media.port());
return true; return true;
@@ -152,13 +151,13 @@ void ReliableTunnel::thread()
// Construct event array // Construct event array
while (true) while (true)
{ {
HANDLE eventarray[2] = { mDataSignal, mExitSignal }; HANDLE eventarray[2] = {mDataSignal, mExitSignal};
DWORD rescode = ::WaitForMultipleObjects(2, eventarray, FALSE, INFINITE); DWORD rescode = ::WaitForMultipleObjects(2, eventarray, FALSE, INFINITE);
if (rescode == WAIT_OBJECT_0) if (rescode == WAIT_OBJECT_0)
{ {
resip::Lock l(mNewQueuedGuard); resip::Lock l(mNewQueuedGuard);
for (unsigned i = 0; i<mNewQueued.size(); i++) for (unsigned i = 0; i < mNewQueued.size(); i++)
mStack.queueOutgoing(mNewQueued[i].c_str(), mNewQueued[i].size()); mStack.queueOutgoing(mNewQueued[i].c_str(), mNewQueued[i].size());
mNewQueued.clear(); mNewQueued.clear();
@@ -237,12 +236,13 @@ void ReliableTunnel::encrypt(void* dataPtr, int dataSize)
return; return;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
for (unsigned i=0; i<dataSize / blockSize(); i++) for (unsigned i = 0; i < dataSize / blockSize(); i++)
BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher, BF_ENCRYPT); BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher,
BF_ENCRYPT);
#endif #endif
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
for (unsigned i=0; i<dataSize / blockSize(); i++) for (unsigned i = 0; i < dataSize / blockSize(); i++)
mEncryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize()); mEncryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize());
#endif #endif
} }
@@ -253,12 +253,13 @@ void ReliableTunnel::decrypt(void* dataPtr, int dataSize)
if (mEncryptionKey.empty()) if (mEncryptionKey.empty())
return; return;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
for (unsigned i=0; i<dataSize / blockSize(); i++) for (unsigned i = 0; i < dataSize / blockSize(); i++)
BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher, BF_DECRYPT); BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher,
BF_DECRYPT);
#endif #endif
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
for (unsigned i=0; i<dataSize / blockSize(); i++) for (unsigned i = 0; i < dataSize / blockSize(); i++)
mDecryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize()); mDecryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize());
#endif #endif
} }
+4 -5
View File
@@ -26,13 +26,13 @@
#include "../ICE/ICEReliableTransport.h" #include "../ICE/ICEReliableTransport.h"
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
# include "../Libs/CryptoPP/blowfish.h" #include "../Libs/CryptoPP/blowfish.h"
#endif #endif
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
# include "../Libs/openssl/include/openssl/blowfish.h" #include "../Libs/openssl/include/openssl/blowfish.h"
#endif #endif
class ReliableTunnel: public DataProvider, public resip::ThreadIf, public ICEImpl::ReliableTransport::Encryption class ReliableTunnel : public DataProvider, public resip::ThreadIf, public ICEImpl::ReliableTransport::Encryption
{ {
public: public:
ReliableTunnel(const char* streamname); ReliableTunnel(const char* streamname);
@@ -111,8 +111,7 @@ protected:
// Mutex to protect queuing/sending outgoing data // Mutex to protect queuing/sending outgoing data
resip::Mutex mOutgoingMtx; resip::Mutex mOutgoingMtx;
std::vector<std::string> std::vector<std::string> mNewQueued;
mNewQueued;
resip::Mutex mNewQueuedGuard; resip::Mutex mNewQueuedGuard;
resip::Mutex mStackGuard; resip::Mutex mStackGuard;
+89 -83
View File
@@ -16,18 +16,15 @@
typedef resip::SdpContents::Session::Medium Medium; typedef resip::SdpContents::Session::Medium Medium;
typedef resip::SdpContents::Session::MediumContainer MediumContainer; typedef resip::SdpContents::Session::MediumContainer MediumContainer;
#define IS_MULTIPLEX() mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool() ? SocketHeap::DoMultiplexing : SocketHeap::DontMultiplexing #define IS_MULTIPLEX() \
mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool() ? SocketHeap::DoMultiplexing : SocketHeap::DontMultiplexing
//------------ ResipSessionAppDialog ------------ //------------ ResipSessionAppDialog ------------
#pragma region ResipSessionAppDialog #pragma region ResipSessionAppDialog
ResipSessionAppDialog::ResipSessionAppDialog(resip::HandleManager& ham) : AppDialog(ham) ResipSessionAppDialog::ResipSessionAppDialog(resip::HandleManager& ham) : AppDialog(ham) {}
{
}
ResipSessionAppDialog::~ResipSessionAppDialog() ResipSessionAppDialog::~ResipSessionAppDialog() {}
{
}
#pragma endregion #pragma endregion
@@ -54,7 +51,7 @@ ResipSession::~ResipSession()
mSession->mResipSession = nullptr; mSession->mResipSession = nullptr;
runTerminatedEvent(Type_Auto, 0, 0); runTerminatedEvent(Type_Auto, 0, 0);
} }
catch(...) catch (...)
{ {
} }
@@ -94,7 +91,8 @@ void ResipSession::runTerminatedEvent(Type type, int code, int reason)
case Type_Subscription: case Type_Subscription:
if (mSession) if (mSession)
{ {
UserAgent::ClientObserverMap::iterator observerIter = mUserAgent->mClientObserverMap.find(mSession->sessionId()); UserAgent::ClientObserverMap::iterator observerIter =
mUserAgent->mClientObserverMap.find(mSession->sessionId());
if (observerIter != mUserAgent->mClientObserverMap.end()) if (observerIter != mUserAgent->mClientObserverMap.end())
mUserAgent->onClientObserverStop(observerIter->second, code); mUserAgent->onClientObserverStop(observerIter->second, code);
} }
@@ -186,14 +184,9 @@ std::shared_ptr<resip::UserProfile> ResipSession::selectUASUserProfile(const res
#pragma endregion #pragma endregion
#pragma region Session::Stream #pragma region Session::Stream
Session::Stream::Stream() Session::Stream::Stream() : mRtcpAttr(false), mRtcpMuxAttr(false) {}
:mRtcpAttr(false), mRtcpMuxAttr(false)
{
}
Session::Stream::~Stream() Session::Stream::~Stream() {}
{
}
void Session::Stream::setProvider(PDataProvider provider) void Session::Stream::setProvider(PDataProvider provider)
{ {
@@ -290,14 +283,15 @@ Session::~Session()
mResipSession->setSession(NULL); mResipSession->setSession(NULL);
clearProvidersAndSockets(); clearProvidersAndSockets();
} }
catch(...) catch (...)
{} {
}
InstanceCounter--; InstanceCounter--;
} }
void Session::start(const std::string& peer) void Session::start(const std::string& peer)
{ {
ICELogInfo( << "Attempt to start session to " << peer); ICELogInfo(<< "Attempt to start session to " << peer);
Lock l(mGuard); Lock l(mGuard);
if (mResipSession) if (mResipSession)
@@ -334,7 +328,7 @@ void Session::stop()
{ {
ICELogInfo(<< "Stopping session " << mSessionId); ICELogInfo(<< "Stopping session " << mSessionId);
Lock l(mGuard); Lock l(mGuard);
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
{ {
Session::Stream& dataStream = mStreamList[i]; Session::Stream& dataStream = mStreamList[i];
@@ -344,8 +338,8 @@ void Session::stop()
dataStream.provider()->sessionTerminated(); dataStream.provider()->sessionTerminated();
// Free socket // Free socket
SocketHeap::instance().freeSocketPair( dataStream.socket4() ); SocketHeap::instance().freeSocketPair(dataStream.socket4());
SocketHeap::instance().freeSocketPair( dataStream.socket6() ); SocketHeap::instance().freeSocketPair(dataStream.socket6());
// Drop the references so the destructor's cleanup does not free them again // Drop the references so the destructor's cleanup does not free them again
dataStream.setSocket4(RtpPair<PDatagramSocket>()); dataStream.setSocket4(RtpPair<PDatagramSocket>());
@@ -366,7 +360,8 @@ void Session::accept()
Lock locksession(mGuard); Lock locksession(mGuard);
// If ICE candidate gathering is not finished - just mark session as accepted. It will be accepted in ICE handling code. // If ICE candidate gathering is not finished - just mark session as accepted. It will be accepted in ICE handling
// code.
mAcceptedByUser = true; mAcceptedByUser = true;
if (mGatheredCandidates || mIceStack->state() == ice::IceNone) if (mGatheredCandidates || mIceStack->state() == ice::IceNone)
@@ -405,7 +400,7 @@ void Session::accept()
void Session::reject(int code) void Session::reject(int code)
{ {
ICELogInfo( << "Attempt to reject session " << mSessionId); ICELogInfo(<< "Attempt to reject session " << mSessionId);
Lock l(mGuard); Lock l(mGuard);
@@ -450,7 +445,7 @@ void Session::getSessionInfo(Session::InfoOptions options, VariantMap& info)
// Iterate all session providers // Iterate all session providers
Stream* media = nullptr; Stream* media = nullptr;
for (Stream& stream: mStreamList) for (Stream& stream : mStreamList)
{ {
if (!stream.provider()) if (!stream.provider())
continue; continue;
@@ -473,7 +468,9 @@ void Session::getSessionInfo(Session::InfoOptions options, VariantMap& info)
info[SessionInfo_SentRtp] = static_cast<int>(stat.mSentRtp); info[SessionInfo_SentRtp] = static_cast<int>(stat.mSentRtp);
info[SessionInfo_SentRtcp] = static_cast<int>(stat.mSentRtcp); info[SessionInfo_SentRtcp] = static_cast<int>(stat.mSentRtcp);
if (stat.mFirstRtpTime) if (stat.mFirstRtpTime)
info[SessionInfo_Duration] = static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - *(stat.mFirstRtpTime)).count()); info[SessionInfo_Duration] = static_cast<int>(
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - *(stat.mFirstRtpTime))
.count());
else else
info[SessionInfo_Duration] = 0; info[SessionInfo_Duration] = 0;
@@ -481,7 +478,8 @@ void Session::getSessionInfo(Session::InfoOptions options, VariantMap& info)
info[SessionInfo_PacketLoss] = static_cast<int>((stat.mPacketLoss * 1000) / stat.mReceivedRtp); info[SessionInfo_PacketLoss] = static_cast<int>((stat.mPacketLoss * 1000) / stat.mReceivedRtp);
if (media && mIceStack) if (media && mIceStack)
info[SessionInfo_AudioPeer] = mIceStack->remoteAddress(media->iceInfo().mStreamId, media->iceInfo().mComponentId.mRtp).toStdString(); info[SessionInfo_AudioPeer] =
mIceStack->remoteAddress(media->iceInfo().mStreamId, media->iceInfo().mComponentId.mRtp).toStdString();
info[SessionInfo_Jitter] = stat.mJitter; info[SessionInfo_Jitter] = stat.mJitter;
if (stat.mRttDelay.is_initialized()) if (stat.mRttDelay.is_initialized())
@@ -505,7 +503,8 @@ PAccount Session::account()
return mAccount; return mAccount;
} }
void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr, unsigned receivedSize) void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr,
unsigned receivedSize)
{ {
Lock l(mGuard); Lock l(mGuard);
@@ -513,7 +512,7 @@ void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const
return; return;
// Check if it STUN packet and must be processed by ICE stack // Check if it STUN packet and must be processed by ICE stack
//ICELogDebug (<< "Received UDP packet from " << src.ip() << ":" << src.port()); // ICELogDebug (<< "Received UDP packet from " << src.ip() << ":" << src.port());
ice::ByteBuffer received(receivedPtr, receivedSize); ice::ByteBuffer received(receivedPtr, receivedSize);
received.setRemoteAddress(src); received.setRemoteAddress(src);
@@ -531,7 +530,7 @@ void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const
if (received.size() >= 4) if (received.size() >= 4)
{ {
bool turnPrefix = false; bool turnPrefix = false;
for (unsigned i=0; i<mTurnPrefixList.size() && !turnPrefix; i++) for (unsigned i = 0; i < mTurnPrefixList.size() && !turnPrefix; i++)
turnPrefix |= ice::Stack::isChannelData(received, mTurnPrefixList[i]); turnPrefix |= ice::Stack::isChannelData(received, mTurnPrefixList[i]);
if (turnPrefix) if (turnPrefix)
received.erase(0, 4); received.erase(0, 4);
@@ -546,7 +545,7 @@ void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const
{ {
ice::ByteBuffer buffer(receivedPtr, receivedSize); ice::ByteBuffer buffer(receivedPtr, receivedSize);
buffer.setRemoteAddress(src); buffer.setRemoteAddress(src);
/*bool processed = */mIceStack->processIncomingData(stream, component, buffer); /*bool processed = */ mIceStack->processIncomingData(stream, component, buffer);
} }
} }
else else
@@ -594,8 +593,7 @@ void Session::onGathered(ice::Stack* stack, void* tag)
if (mRole == Initiator) if (mRole == Initiator)
mUserAgent->sendOffer(this); mUserAgent->sendOffer(this);
else else if (mRole == Acceptor)
if (mRole == Acceptor)
{ {
// Mark session as gathered ICE candidates // Mark session as gathered ICE candidates
mGatheredCandidates = true; mGatheredCandidates = true;
@@ -616,7 +614,7 @@ void Session::onSuccess(ice::Stack* stack, void* tag)
RtpPair<InternetAddress> t; RtpPair<InternetAddress> t;
for (unsigned i=0; i<this->mStreamList.size(); i++) for (unsigned i = 0; i < this->mStreamList.size(); i++)
{ {
PDataProvider p = mStreamList[i].provider(); PDataProvider p = mStreamList[i].provider();
if (p) if (p)
@@ -638,8 +636,8 @@ void Session::onSuccess(ice::Stack* stack, void* tag)
mUserAgent->onSessionEstablished(mUserAgent->getUserSession(mSessionId), EV_ICE, t); mUserAgent->onSessionEstablished(mUserAgent->getUserSession(mSessionId), EV_ICE, t);
//time to resend updated media info over SIP // time to resend updated media info over SIP
//TODO: // TODO:
} }
void Session::onFailed(ice::Stack* stack, void* tag) void Session::onFailed(ice::Stack* stack, void* tag)
@@ -647,17 +645,17 @@ void Session::onFailed(ice::Stack* stack, void* tag)
ICELogError(<< "ICE connectivity check failed for session " << mSessionId); ICELogError(<< "ICE connectivity check failed for session " << mSessionId);
mUserAgent->onConnectivityFailed(mUserAgent->getUserSession(mSessionId)); mUserAgent->onConnectivityFailed(mUserAgent->getUserSession(mSessionId));
//if (mInviteHandle.isValid()) // if (mInviteHandle.isValid())
// mInviteHandle->end(); // mInviteHandle->end();
} }
void Session::onNetworkChange(ice::Stack *stack, void *tag) void Session::onNetworkChange(ice::Stack* stack, void* tag)
{ {
ICELogInfo(<< "Network change detected by ICE stack for session " << mSessionId); ICELogInfo(<< "Network change detected by ICE stack for session " << mSessionId);
mUserAgent->onNetworkChange(mUserAgent->getUserSession(mSessionId)); mUserAgent->onNetworkChange(mUserAgent->getUserSession(mSessionId));
} }
void Session::buildSdp(resip::SdpContents &sdp, SdpDirection sdpDirection) void Session::buildSdp(resip::SdpContents& sdp, SdpDirection sdpDirection)
{ {
sdp.session().name() = "ICE_UA"; sdp.session().name() = "ICE_UA";
sdp.session().origin().user() = "user"; sdp.session().origin().user() = "user";
@@ -675,8 +673,12 @@ void Session::buildSdp(resip::SdpContents &sdp, SdpDirection sdpDirection)
ice::NetworkAddress defaultAddr = mIceStack->defaultAddress(mStreamList.front().iceInfo().mStreamId, ICE_RTP_ID); ice::NetworkAddress defaultAddr = mIceStack->defaultAddress(mStreamList.front().iceInfo().mStreamId, ICE_RTP_ID);
// Set IP address for origin and connection // Set IP address for origin and connection
sdp.session().origin().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET ? resip::SdpContents::IP4 : resip::SdpContents::IP6); sdp.session().origin().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET
sdp.session().connection().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET ? resip::SdpContents::IP4 : resip::SdpContents::IP6); ? resip::SdpContents::IP4
: resip::SdpContents::IP6);
sdp.session().connection().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET
? resip::SdpContents::IP4
: resip::SdpContents::IP6);
// Add ICE credentials // Add ICE credentials
if (mIceStack->state() > ice::IceNone) if (mIceStack->state() > ice::IceNone)
@@ -686,7 +688,7 @@ void Session::buildSdp(resip::SdpContents &sdp, SdpDirection sdpDirection)
} }
// Iterate media streams // Iterate media streams
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
{ {
Stream& stream = mStreamList[i]; Stream& stream = mStreamList[i];
if (!stream.provider()) if (!stream.provider())
@@ -698,18 +700,18 @@ void Session::buildSdp(resip::SdpContents &sdp, SdpDirection sdpDirection)
rtcpPort = mIceStack->defaultAddress(mStreamList[i].iceInfo().mStreamId, ICE_RTCP_ID); rtcpPort = mIceStack->defaultAddress(mStreamList[i].iceInfo().mStreamId, ICE_RTCP_ID);
// Define media stream SDP's header // Define media stream SDP's header
resip::SdpContents::Session::Medium media(resip::Data(provider.streamName()), rtpPort.port(), 0, resip::Data(provider.streamProfile())); resip::SdpContents::Session::Medium media(resip::Data(provider.streamName()), rtpPort.port(), 0,
resip::Data(provider.streamProfile()));
// Add "rtcp" attribute // Add "rtcp" attribute
if (mUserAgent->mConfig[CONFIG_RTCP_ATTR].asBool()) if (mUserAgent->mConfig[CONFIG_RTCP_ATTR].asBool())
{ {
if (mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool()) if (mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool())
rtcpPort = rtpPort; rtcpPort = rtpPort;
else else if (rtcpPort.isEmpty())
if (rtcpPort.isEmpty())
{ {
rtcpPort = rtpPort; rtcpPort = rtpPort;
rtcpPort.setPort( rtpPort.port() + 1); rtcpPort.setPort(rtpPort.port() + 1);
} }
media.addAttribute("rtcp", resip::Data(rtcpPort.port())); media.addAttribute("rtcp", resip::Data(rtcpPort.port()));
@@ -732,7 +734,7 @@ void Session::buildSdp(resip::SdpContents &sdp, SdpDirection sdpDirection)
if (mIceStack->hasComponent(ii.mStreamId, ii.mComponentId.mRtcp)) if (mIceStack->hasComponent(ii.mStreamId, ii.mComponentId.mRtcp))
mIceStack->fillCandidateList(ii.mStreamId, ii.mComponentId.mRtcp, candidates); mIceStack->fillCandidateList(ii.mStreamId, ii.mComponentId.mRtcp, candidates);
for (unsigned c=0; c<candidates.size(); c++) for (unsigned c = 0; c < candidates.size(); c++)
media.addAttribute("candidate", candidates[c].c_str()); media.addAttribute("candidate", candidates[c].c_str());
} }
@@ -766,7 +768,7 @@ void Session::addProvider(PDataProvider provider)
return; return;
// Avoid duplicating providers // Avoid duplicating providers
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
if (mStreamList[i].provider() == provider) if (mStreamList[i].provider() == provider)
return; return;
@@ -777,18 +779,18 @@ void Session::addProvider(PDataProvider provider)
{ {
if (!streamIter->provider() && (streamIter->socket4().mRtp->isValid() || streamIter->socket6().mRtp->isValid())) if (!streamIter->provider() && (streamIter->socket4().mRtp->isValid() || streamIter->socket6().mRtp->isValid()))
{ {
streamIter->setProvider( provider ); streamIter->setProvider(provider);
provider->setSocket(streamIter->socket4(), streamIter->socket6()); provider->setSocket(streamIter->socket4(), streamIter->socket6());
return; return;
} }
} }
Stream s; Stream s;
s.setProvider( provider ); s.setProvider(provider);
// Allocate socket for provider // Allocate socket for provider
s.setSocket4( SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX()) ); s.setSocket4(SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX()));
s.setSocket6( SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX()) ); s.setSocket6(SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX()));
s.provider()->setSocket(s.socket4(), s.socket6()); s.provider()->setSocket(s.socket4(), s.socket6());
// Create ICE stream/component // Create ICE stream/component
@@ -797,10 +799,11 @@ void Session::addProvider(PDataProvider provider)
ii.mPort4 = s.socket4().mRtp->localport(); ii.mPort4 = s.socket4().mRtp->localport();
ii.mPort6 = s.socket6().mRtp->localport(); ii.mPort6 = s.socket6().mRtp->localport();
ii.mComponentId.mRtp = mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtp->localport(), ii.mComponentId.mRtp =
s.socket6().mRtp->localport()); mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtp->localport(), s.socket6().mRtp->localport());
if (!mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool()) if (!mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool())
ii.mComponentId.mRtcp = mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtcp->localport(), s.socket6().mRtcp->localport()); ii.mComponentId.mRtcp =
mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtcp->localport(), s.socket6().mRtcp->localport());
s.setIceInfo(ii); s.setIceInfo(ii);
@@ -857,7 +860,7 @@ void Session::setTag(void* tag)
void Session::pause() void Session::pause()
{ {
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
{ {
Stream& s = mStreamList[i]; Stream& s = mStreamList[i];
if (s.provider()) if (s.provider())
@@ -868,7 +871,7 @@ void Session::pause()
void Session::resume() void Session::resume()
{ {
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
{ {
Stream& s = mStreamList[i]; Stream& s = mStreamList[i];
if (s.provider()) if (s.provider())
@@ -880,9 +883,9 @@ void Session::resume()
void Session::refreshMediaPath() void Session::refreshMediaPath()
{ {
// Recreate media sockets // Recreate media sockets
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
{ {
Stream& s= mStreamList[i]; Stream& s = mStreamList[i];
PDataProvider p = s.provider(); PDataProvider p = s.provider();
if (!p) if (!p)
continue; continue;
@@ -891,7 +894,7 @@ void Session::refreshMediaPath()
SocketHeap::instance().freeSocketPair(p->socket(AF_INET)); SocketHeap::instance().freeSocketPair(p->socket(AF_INET));
// Bring new socket to provider and stream // Bring new socket to provider and stream
RtpPair<PDatagramSocket> s4 = SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX() ), RtpPair<PDatagramSocket> s4 = SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX()),
s6 = SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX()); s6 = SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX());
p->setSocket(s4, s6); p->setSocket(s4, s6);
@@ -929,7 +932,7 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
// Ask about provider if needed // Ask about provider if needed
if (!stream.provider()) if (!stream.provider())
stream.setProvider( mUserAgent->onProviderNeeded(remoteStream.name().c_str()) ); stream.setProvider(mUserAgent->onProviderNeeded(remoteStream.name().c_str()));
// Check the stream validity // Check the stream validity
if (!stream.provider()) if (!stream.provider())
@@ -938,8 +941,8 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
continue; continue;
// See for rtcp & rtcp-mux attribute // See for rtcp & rtcp-mux attribute
stream.setRtcpAttr( remoteStream.exists("rtcp") ); stream.setRtcpAttr(remoteStream.exists("rtcp"));
stream.setRtcpMuxAttr( remoteStream.exists("rtcp-mux") ); stream.setRtcpMuxAttr(remoteStream.exists("rtcp-mux"));
// Set destination address // Set destination address
if (!remoteStream.getConnections().empty()) if (!remoteStream.getConnections().empty())
@@ -951,12 +954,12 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
targetAddr.mRtcp.setIp(remoteIp); targetAddr.mRtcp.setIp(remoteIp);
if (stream.rtcpMuxAttr()) if (stream.rtcpMuxAttr())
targetAddr.mRtcp.setPort( remoteStream.port() ); targetAddr.mRtcp.setPort(remoteStream.port());
else if (stream.rtcpAttr())
targetAddr.mRtcp.setPort(
strx::toInt(remoteStream.getValues("rtcp").front().c_str(), remoteStream.port() + 1));
else else
if (stream.rtcpAttr()) targetAddr.mRtcp.setPort(remoteStream.port() + 1);
targetAddr.mRtcp.setPort( strx::toInt(remoteStream.getValues("rtcp").front().c_str(), remoteStream.port() + 1 ) );
else
targetAddr.mRtcp.setPort( remoteStream.port() + 1);
stream.provider()->setDestinationAddress(targetAddr); stream.provider()->setDestinationAddress(targetAddr);
@@ -974,9 +977,9 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
stream.setSocket4(SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX())); stream.setSocket4(SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX()));
stream.setSocket6(SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX())); stream.setSocket6(SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX()));
} }
catch(...) catch (...)
{ {
ICELogError( << "Cannot create media socket."); ICELogError(<< "Cannot create media socket.");
return 503; return 503;
} }
@@ -1005,13 +1008,15 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
// See what remote peer offers - offer only single ice component if it relies on multiplexing // See what remote peer offers - offer only single ice component if it relies on multiplexing
if (!targetAddr.multiplexed() && !mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool()) if (!targetAddr.multiplexed() && !mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool())
ii.mComponentId.mRtcp = mIceStack->addComponent(ii.mStreamId, NULL, stream.socket4().mRtcp->localport(), stream.socket6().mRtcp->localport()); ii.mComponentId.mRtcp = mIceStack->addComponent(ii.mStreamId, NULL, stream.socket4().mRtcp->localport(),
stream.socket6().mRtcp->localport());
stream.setIceInfo(ii); stream.setIceInfo(ii);
} }
if (iceAvailable) if (iceAvailable)
{ {
if (mIceStack->remotePassword(stream.iceInfo().mStreamId) != icePwd || mIceStack->remoteUfrag(stream.iceInfo().mStreamId) != iceUfrag) if (mIceStack->remotePassword(stream.iceInfo().mStreamId) != icePwd ||
mIceStack->remoteUfrag(stream.iceInfo().mStreamId) != iceUfrag)
{ {
iceRestart = true; iceRestart = true;
mIceStack->setRemotePassword(icePwd, stream.iceInfo().mStreamId); mIceStack->setRemotePassword(icePwd, stream.iceInfo().mStreamId);
@@ -1032,9 +1037,11 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
if (candidateVector.empty()) if (candidateVector.empty())
iceAvailable = false; iceAvailable = false;
// Ask ICE stack to process this information. This call will remove also second component if it is not defined in remote sdp. // Ask ICE stack to process this information. This call will remove also second component if it is not defined
// in remote sdp.
if (iceAvailable) if (iceAvailable)
iceAvailable = mIceStack->processSdpOffer(stream.iceInfo().mStreamId, candidateVector, remoteIp, remotePort, mUserAgent->mConfig[CONFIG_DEFERRELAYED].asBool()); iceAvailable = mIceStack->processSdpOffer(stream.iceInfo().mStreamId, candidateVector, remoteIp, remotePort,
mUserAgent->mConfig[CONFIG_DEFERRELAYED].asBool());
} }
// See if there are compatible media streams // See if there are compatible media streams
@@ -1081,22 +1088,22 @@ int Session::release()
void Session::clearProvidersAndSockets() void Session::clearProvidersAndSockets()
{ {
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
{ {
Session::Stream& ds = mStreamList[i]; Session::Stream& ds = mStreamList[i];
if (ds.provider()) if (ds.provider())
{ {
ds.provider()->sessionDeleted(); ds.provider()->sessionDeleted();
SocketHeap::instance().freeSocketPair( ds.socket4() ); SocketHeap::instance().freeSocketPair(ds.socket4());
SocketHeap::instance().freeSocketPair( ds.socket6() ); SocketHeap::instance().freeSocketPair(ds.socket6());
} }
} }
} }
void Session::clearProviders() void Session::clearProviders()
{ {
for (unsigned i=0; i<mStreamList.size(); i++) for (unsigned i = 0; i < mStreamList.size(); i++)
{ {
Session::Stream& ds = mStreamList[i]; Session::Stream& ds = mStreamList[i];
@@ -1132,14 +1139,13 @@ void Session::processQueuedOffer()
//-------------- ResipSessionFactory --------- //-------------- ResipSessionFactory ---------
#pragma region ResipSessionFactory #pragma region ResipSessionFactory
ResipSessionFactory::ResipSessionFactory(UserAgent* agent) ResipSessionFactory::ResipSessionFactory(UserAgent* agent) : mAgent(agent) {}
:mAgent(agent)
{}
resip::AppDialogSet* ResipSessionFactory::createAppDialogSet(resip::DialogUsageManager& dum, const resip::SipMessage& msg) resip::AppDialogSet* ResipSessionFactory::createAppDialogSet(resip::DialogUsageManager& dum,
const resip::SipMessage& msg)
{ {
ResipSession* s = new ResipSession(dum); ResipSession* s = new ResipSession(dum);
s->setUa( mAgent ); s->setUa(mAgent);
return s; return s;
} }
+6 -8
View File
@@ -77,9 +77,7 @@ enum SessionInfo
}; };
class Session : class Session : public SocketSink, public ice::StageHandler
public SocketSink,
public ice::StageHandler
{ {
public: public:
class Command class Command
@@ -91,8 +89,7 @@ public:
// Describes ice stream/component // Describes ice stream/component
struct IceInfo struct IceInfo
{ {
IceInfo() IceInfo() : mStreamId(-1)
:mStreamId(-1)
{ {
mPort4 = mPort6 = 0; mPort4 = mPort6 = 0;
mComponentId.mRtp = mComponentId.mRtcp = -1; mComponentId.mRtp = mComponentId.mRtcp = -1;
@@ -342,10 +339,10 @@ public:
virtual ~ResipSessionAppDialog(); virtual ~ResipSessionAppDialog();
}; };
class ResipSession: public resip::AppDialogSet class ResipSession : public resip::AppDialogSet
{ {
friend class UserAgent; friend class UserAgent;
friend class Account; friend class Account;
public: public:
enum Type enum Type
@@ -406,6 +403,7 @@ class ResipSessionFactory : public resip::AppDialogSetFactory
public: public:
ResipSessionFactory(UserAgent* agent); ResipSessionFactory(UserAgent* agent);
virtual resip::AppDialogSet* createAppDialogSet(resip::DialogUsageManager& dum, const resip::SipMessage& msg); virtual resip::AppDialogSet* createAppDialogSet(resip::DialogUsageManager& dum, const resip::SipMessage& msg);
protected: protected:
UserAgent* mAgent; UserAgent* mAgent;
}; };
+4 -4
View File
@@ -9,7 +9,7 @@
#define USE_SPEEX_AEC #define USE_SPEEX_AEC
// TODO: test implementation with webrtc aec; be careful - it needs fixes! // TODO: test implementation with webrtc aec; be careful - it needs fixes!
//#define USE_WEBRTC_AEC // #define USE_WEBRTC_AEC
#define USER #define USER
@@ -36,8 +36,8 @@
// Defined these two lines to get dumping of audio input/output // Defined these two lines to get dumping of audio input/output
//#define AUDIO_DUMPINPUT // #define AUDIO_DUMPINPUT
//#define AUDIO_DUMPOUTPUT // #define AUDIO_DUMPOUTPUT
#define UA_REGISTRATION_TIME 3600 #define UA_REGISTRATION_TIME 3600
@@ -107,7 +107,7 @@
#define MT_MIRROR_PREBUFFER (MT_SAMPLERATE / 10) #define MT_MIRROR_PREBUFFER (MT_SAMPLERATE / 10)
#if defined(TARGET_OSX) || defined(TARGET_LINUX) #if defined(TARGET_OSX) || defined(TARGET_LINUX)
# define TEXT(X) X #define TEXT(X) X
#endif #endif
// In milliseconds // In milliseconds
+2 -8
View File
@@ -5,12 +5,6 @@
#include "HL_AsyncCommand.h" #include "HL_AsyncCommand.h"
AsyncCommand::AsyncCommand() AsyncCommand::AsyncCommand() {}
{
} AsyncCommand::~AsyncCommand() {}
AsyncCommand::~AsyncCommand()
{
}
+119 -71
View File
@@ -7,9 +7,11 @@ const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"0123456789+/"; "0123456789+/";
class Base64 { class Base64
public: {
static bool Encode(const std::string &in, std::string *out) { public:
static bool Encode(const std::string& in, std::string* out)
{
int i = 0, j = 0; int i = 0, j = 0;
size_t enc_len = 0; size_t enc_len = 0;
unsigned char a3[3]; unsigned char a3[3];
@@ -20,12 +22,15 @@ class Base64 {
int input_len = in.size(); int input_len = in.size();
std::string::const_iterator input = in.begin(); std::string::const_iterator input = in.begin();
while (input_len--) { while (input_len--)
{
a3[i++] = *(input++); a3[i++] = *(input++);
if (i == 3) { if (i == 3)
{
a3_to_a4(a4, a3); a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++)
{
(*out)[enc_len++] = kBase64Alphabet[a4[i]]; (*out)[enc_len++] = kBase64Alphabet[a4[i]];
} }
@@ -33,18 +38,22 @@ class Base64 {
} }
} }
if (i) { if (i)
for (j = i; j < 3; j++) { {
for (j = i; j < 3; j++)
{
a3[j] = '\0'; a3[j] = '\0';
} }
a3_to_a4(a4, a3); a3_to_a4(a4, a3);
for (j = 0; j < i + 1; j++) { for (j = 0; j < i + 1; j++)
{
(*out)[enc_len++] = kBase64Alphabet[a4[j]]; (*out)[enc_len++] = kBase64Alphabet[a4[j]];
} }
while ((i++ < 3)) { while ((i++ < 3))
{
(*out)[enc_len++] = '='; (*out)[enc_len++] = '=';
} }
} }
@@ -52,22 +61,27 @@ class Base64 {
return (enc_len == out->size()); return (enc_len == out->size());
} }
static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) { static bool Encode(const char* input, size_t input_length, char* out, size_t out_length)
{
int i = 0, j = 0; int i = 0, j = 0;
char *out_begin = out; char* out_begin = out;
unsigned char a3[3]; unsigned char a3[3];
unsigned char a4[4]; unsigned char a4[4];
size_t encoded_length = EncodedLength(input_length); size_t encoded_length = EncodedLength(input_length);
if (out_length < encoded_length) return false; if (out_length < encoded_length)
return false;
while (input_length--) { while (input_length--)
{
a3[i++] = *input++; a3[i++] = *input++;
if (i == 3) { if (i == 3)
{
a3_to_a4(a4, a3); a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++)
{
*out++ = kBase64Alphabet[a4[i]]; *out++ = kBase64Alphabet[a4[i]];
} }
@@ -75,18 +89,22 @@ class Base64 {
} }
} }
if (i) { if (i)
for (j = i; j < 3; j++) { {
for (j = i; j < 3; j++)
{
a3[j] = '\0'; a3[j] = '\0';
} }
a3_to_a4(a4, a3); a3_to_a4(a4, a3);
for (j = 0; j < i + 1; j++) { for (j = 0; j < i + 1; j++)
{
*out++ = kBase64Alphabet[a4[j]]; *out++ = kBase64Alphabet[a4[j]];
} }
while ((i++ < 3)) { while ((i++ < 3))
{
*out++ = '='; *out++ = '=';
} }
} }
@@ -94,7 +112,8 @@ class Base64 {
return (out == (out_begin + encoded_length)); return (out == (out_begin + encoded_length));
} }
static bool Decode(const std::string &in, std::string *out) { static bool Decode(const std::string& in, std::string* out)
{
int i = 0, j = 0; int i = 0, j = 0;
size_t dec_len = 0; size_t dec_len = 0;
unsigned char a3[3]; unsigned char a3[3];
@@ -105,20 +124,25 @@ class Base64 {
out->resize(DecodedLength(in)); out->resize(DecodedLength(in));
while (input_len--) { while (input_len--)
if (*input == '=') { {
if (*input == '=')
{
break; break;
} }
a4[i++] = *(input++); a4[i++] = *(input++);
if (i == 4) { if (i == 4)
for (i = 0; i <4; i++) { {
for (i = 0; i < 4; i++)
{
a4[i] = b64_lookup(a4[i]); a4[i] = b64_lookup(a4[i]);
} }
a4_to_a3(a3,a4); a4_to_a3(a3, a4);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++)
{
(*out)[dec_len++] = a3[i]; (*out)[dec_len++] = a3[i];
} }
@@ -126,18 +150,22 @@ class Base64 {
} }
} }
if (i) { if (i)
for (j = i; j < 4; j++) { {
for (j = i; j < 4; j++)
{
a4[j] = '\0'; a4[j] = '\0';
} }
for (j = 0; j < 4; j++) { for (j = 0; j < 4; j++)
{
a4[j] = b64_lookup(a4[j]); a4[j] = b64_lookup(a4[j]);
} }
a4_to_a3(a3,a4); a4_to_a3(a3, a4);
for (j = 0; j < i - 1; j++) { for (j = 0; j < i - 1; j++)
{
(*out)[dec_len++] = a3[j]; (*out)[dec_len++] = a3[j];
} }
} }
@@ -145,30 +173,37 @@ class Base64 {
return (dec_len == out->size()); return (dec_len == out->size());
} }
static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) { static bool Decode(const char* input, size_t input_length, char* out, size_t out_length)
{
int i = 0, j = 0; int i = 0, j = 0;
char *out_begin = out; char* out_begin = out;
unsigned char a3[3]; unsigned char a3[3];
unsigned char a4[4]; unsigned char a4[4];
size_t decoded_length = DecodedLength(input, input_length); size_t decoded_length = DecodedLength(input, input_length);
if (out_length < decoded_length) return false; if (out_length < decoded_length)
return false;
while (input_length--) { while (input_length--)
if (*input == '=') { {
if (*input == '=')
{
break; break;
} }
a4[i++] = *(input++); a4[i++] = *(input++);
if (i == 4) { if (i == 4)
for (i = 0; i <4; i++) { {
for (i = 0; i < 4; i++)
{
a4[i] = b64_lookup(a4[i]); a4[i] = b64_lookup(a4[i]);
} }
a4_to_a3(a3,a4); a4_to_a3(a3, a4);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++)
{
*out++ = a3[i]; *out++ = a3[i];
} }
@@ -176,18 +211,22 @@ class Base64 {
} }
} }
if (i) { if (i)
for (j = i; j < 4; j++) { {
for (j = i; j < 4; j++)
{
a4[j] = '\0'; a4[j] = '\0';
} }
for (j = 0; j < 4; j++) { for (j = 0; j < 4; j++)
{
a4[j] = b64_lookup(a4[j]); a4[j] = b64_lookup(a4[j]);
} }
a4_to_a3(a3,a4); a4_to_a3(a3, a4);
for (j = 0; j < i - 1; j++) { for (j = 0; j < i - 1; j++)
{
*out++ = a3[j]; *out++ = a3[j];
} }
} }
@@ -195,62 +234,71 @@ class Base64 {
return (out == (out_begin + decoded_length)); return (out == (out_begin + decoded_length));
} }
static int DecodedLength(const char *in, size_t in_length) { static int DecodedLength(const char* in, size_t in_length)
{
int numEq = 0; int numEq = 0;
const char *in_end = in + in_length; const char* in_end = in + in_length;
while (*--in_end == '=') ++numEq; while (*--in_end == '=')
++numEq;
return ((6 * in_length) / 8) - numEq; return ((6 * in_length) / 8) - numEq;
} }
static int DecodedLength(const std::string &in) { static int DecodedLength(const std::string& in)
{
int numEq = 0; int numEq = 0;
int n = in.size(); int n = in.size();
for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) { for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it)
{
++numEq; ++numEq;
} }
return ((6 * n) / 8) - numEq; return ((6 * n) / 8) - numEq;
} }
inline static int EncodedLength(size_t length) { inline static int EncodedLength(size_t length) { return (length + 2 - ((length + 2) % 3)) / 3 * 4; }
return (length + 2 - ((length + 2) % 3)) / 3 * 4;
inline static int EncodedLength(const std::string& in) { return EncodedLength(in.length()); }
inline static void StripPadding(std::string* in)
{
while (!in->empty() && *(in->rbegin()) == '=')
in->resize(in->size() - 1);
} }
inline static int EncodedLength(const std::string &in) { private:
return EncodedLength(in.length()); static inline void a3_to_a4(unsigned char* a4, unsigned char* a3)
} {
inline static void StripPadding(std::string *in) {
while (!in->empty() && *(in->rbegin()) == '=') in->resize(in->size() - 1);
}
private:
static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
a4[0] = (a3[0] & 0xfc) >> 2; a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f); a4[3] = (a3[2] & 0x3f);
} }
static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) { static inline void a4_to_a3(unsigned char* a3, unsigned char* a4)
{
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4); a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2); a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3]; a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
} }
static inline unsigned char b64_lookup(unsigned char c) { static inline unsigned char b64_lookup(unsigned char c)
if(c >='A' && c <='Z') return c - 'A'; {
if(c >='a' && c <='z') return c - 71; if (c >= 'A' && c <= 'Z')
if(c >='0' && c <='9') return c + 4; return c - 'A';
if(c == '+') return 62; if (c >= 'a' && c <= 'z')
if(c == '/') return 63; return c - 71;
if (c >= '0' && c <= '9')
return c + 4;
if (c == '+')
return 62;
if (c == '/')
return 63;
return 255; return 255;
} }
}; };
#endif // HL_BASE64_H #endif // HL_BASE64_H
+168 -155
View File
@@ -11,11 +11,11 @@
namespace Calc namespace Calc
{ {
class Parser; class Parser;
namespace Ast namespace Ast
{ {
enum class Type enum class Type
{ {
None, None,
And, And,
Or, Or,
@@ -32,33 +32,22 @@ namespace Calc
Number, Number,
String, String,
Var Var
}; };
class Item; class Item;
typedef Item* PItem; typedef Item* PItem;
class Item class Item
{ {
friend class Calc::Parser; friend class Calc::Parser;
public:
bool isVariable() const
{
return mType == Type::Var;
}
bool isFixed() const
{
return mType == Type::Number || mType == Type::String;
}
bool isOperation() const public:
{ bool isVariable() const { return mType == Type::Var; }
return mType >= Type::And && mType <= Type::Div; bool isFixed() const { return mType == Type::Number || mType == Type::String; }
}
bool hasBrackets() const bool isOperation() const { return mType >= Type::And && mType <= Type::Div; }
{
return mHasBrackets; bool hasBrackets() const { return mHasBrackets; }
}
int getOperatorLevel() const int getOperatorLevel() const
{ {
@@ -94,25 +83,13 @@ namespace Calc
assert(0); assert(0);
} }
Type getType() const Type getType() const { return mType; }
{
return mType;
}
std::string getName() const std::string getName() const { return mName; }
{
return mName;
}
Variant& value() Variant& value() { return mValue; }
{
return mValue;
}
std::vector<PItem>& children() std::vector<PItem>& children() { return mChildren; }
{
return mChildren;
}
typedef std::map<std::string, std::string> NameMap; typedef std::map<std::string, std::string> NameMap;
@@ -126,21 +103,54 @@ namespace Calc
oss << " "; oss << " ";
switch (mType) switch (mType)
{ {
case Type::Number: oss << mValue.asStdString(); break; case Type::Number:
case Type::String: oss << '"' << mValue.asStdString() << '"'; break; oss << mValue.asStdString();
case Type::Var: { NameMap::const_iterator iter = nm.find(mName); oss << ((iter != nm.end()) ? iter->second : mName);} break; break;
case Type::Add: oss << "+"; break; case Type::String:
case Type::Mul: oss << "*"; break; oss << '"' << mValue.asStdString() << '"';
case Type::Div: oss << "/"; break; break;
case Type::Sub: oss << "-"; break; case Type::Var:
case Type::Equal: oss << "=="; break; {
case Type::NotEqual: oss << "!="; break; NameMap::const_iterator iter = nm.find(mName);
case Type::Less: oss << "<"; break; oss << ((iter != nm.end()) ? iter->second : mName);
case Type::LessOrEqual: oss << "<="; break; }
case Type::Greater: oss << ">"; break; break;
case Type::GreatorOrEqual: oss << ">="; break; case Type::Add:
case Type::Or: oss << "or"; break; oss << "+";
case Type::And: oss << "and"; break; break;
case Type::Mul:
oss << "*";
break;
case Type::Div:
oss << "/";
break;
case Type::Sub:
oss << "-";
break;
case Type::Equal:
oss << "==";
break;
case Type::NotEqual:
oss << "!=";
break;
case Type::Less:
oss << "<";
break;
case Type::LessOrEqual:
oss << "<=";
break;
case Type::Greater:
oss << ">";
break;
case Type::GreatorOrEqual:
oss << ">=";
break;
case Type::Or:
oss << "or";
break;
case Type::And:
oss << "and";
break;
default: default:
throw std::runtime_error("operator expected"); throw std::runtime_error("operator expected");
} }
@@ -167,23 +177,56 @@ namespace Calc
switch (mType) switch (mType)
{ {
case Type::Number: case Type::Number:
case Type::String: result = mValue; break; case Type::String:
result = mValue;
case Type::Var: { auto iter = vm.find(mName); if (iter != vm.end()) return iter->second; else throw std::runtime_error("Variable " + mName + " did not find."); }
break; break;
case Type::Add: result = left + right; break; case Type::Var:
case Type::Mul: result = left * right; break; {
case Type::Div: result = left / right; break; auto iter = vm.find(mName);
case Type::Sub: result = left - right; break; if (iter != vm.end())
case Type::Equal: result = left == right; break; return iter->second;
case Type::NotEqual: result = left != right; break; else
case Type::Less: result = left < right; break; throw std::runtime_error("Variable " + mName + " did not find.");
case Type::LessOrEqual: result = left <= right; break; }
case Type::Greater: result = left > right; break; break;
case Type::GreatorOrEqual: result = left >= right; break;
case Type::Or: result = left.asBool() || right.asBool(); break; case Type::Add:
case Type::And: result = left.asBool() && right.asBool(); break; result = left + right;
break;
case Type::Mul:
result = left * right;
break;
case Type::Div:
result = left / right;
break;
case Type::Sub:
result = left - right;
break;
case Type::Equal:
result = left == right;
break;
case Type::NotEqual:
result = left != right;
break;
case Type::Less:
result = left < right;
break;
case Type::LessOrEqual:
result = left <= right;
break;
case Type::Greater:
result = left > right;
break;
case Type::GreatorOrEqual:
result = left >= right;
break;
case Type::Or:
result = left.asBool() || right.asBool();
break;
case Type::And:
result = left.asBool() && right.asBool();
break;
default: default:
assert(0); assert(0);
} }
@@ -192,30 +235,30 @@ namespace Calc
~Item() ~Item()
{ {
for (auto node: mChildren) for (auto node : mChildren)
delete node; delete node;
mChildren.clear(); mChildren.clear();
} }
private: private:
Type mType = Type::None; Type mType = Type::None;
std::string mName; std::string mName;
Variant mValue; Variant mValue;
std::vector<PItem> mChildren; std::vector<PItem> mChildren;
bool mHasBrackets = false; bool mHasBrackets = false;
}; };
} } // namespace Ast
static bool ishex(int c) static bool ishex(int c)
{ {
if (isdigit(c)) if (isdigit(c))
return true; return true;
return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
} }
class Parser class Parser
{ {
private: private:
enum class LexemType enum class LexemType
{ {
None, None,
@@ -234,15 +277,9 @@ namespace Calc
LexemType mType = LexemType::None; LexemType mType = LexemType::None;
std::string mValue; std::string mValue;
operator bool () const operator bool() const { return mType != LexemType::None; }
{
return mType != LexemType::None;
}
std::string toString() const std::string toString() const { return std::to_string((int)mType) + " : " + mValue; }
{
return std::to_string((int)mType) + " : " + mValue;
}
}; };
Lexem mCurrentLexem; Lexem mCurrentLexem;
@@ -252,20 +289,16 @@ namespace Calc
if (c == '(') if (c == '(')
mCurrentLexem.mType = LexemType::OpenBracket; mCurrentLexem.mType = LexemType::OpenBracket;
else else if (c == ')')
if (c == ')')
mCurrentLexem.mType = LexemType::CloseBracket; mCurrentLexem.mType = LexemType::CloseBracket;
else else if (isdigit(c))
if (isdigit(c))
mCurrentLexem.mType = LexemType::Dec; mCurrentLexem.mType = LexemType::Dec;
else else if (isalpha(c))
if (isalpha(c))
mCurrentLexem.mType = LexemType::Var; mCurrentLexem.mType = LexemType::Var;
else else if (c == '+' || c == '-' || c == '/' || c == '*' || c == '=' || c == '<' || c == '>' || c == '&' ||
if (c == '+' || c == '-' || c == '/' || c == '*' || c == '=' || c == '<' || c == '>' || c == '&' || c == '|') c == '|')
mCurrentLexem.mType = LexemType::Oper; mCurrentLexem.mType = LexemType::Oper;
else else if (c == '"')
if (c == '"')
mCurrentLexem.mType = LexemType::Str; mCurrentLexem.mType = LexemType::Str;
else else
return Lexem(); return Lexem();
@@ -283,15 +316,9 @@ namespace Calc
if (mCurrentLexem.mType == LexemType::Oper) if (mCurrentLexem.mType == LexemType::Oper)
{ {
if (mCurrentLexem.mValue == "+" || if (mCurrentLexem.mValue == "+" || mCurrentLexem.mValue == "-" || mCurrentLexem.mValue == "*" ||
mCurrentLexem.mValue == "-" || mCurrentLexem.mValue == "/" || mCurrentLexem.mValue == ">=" || mCurrentLexem.mValue == "<=" ||
mCurrentLexem.mValue == "*" || mCurrentLexem.mValue == "==" || mCurrentLexem.mValue == "||" || mCurrentLexem.mValue == "&&")
mCurrentLexem.mValue == "/" ||
mCurrentLexem.mValue == ">=" ||
mCurrentLexem.mValue == "<=" ||
mCurrentLexem.mValue == "==" ||
mCurrentLexem.mValue == "||" ||
mCurrentLexem.mValue == "&&")
{ {
// Lexem finished // Lexem finished
result = mCurrentLexem; result = mCurrentLexem;
@@ -357,8 +384,7 @@ namespace Calc
case LexemType::Dec: case LexemType::Dec:
if (c == 'x' && mCurrentLexem.mValue == "0") if (c == 'x' && mCurrentLexem.mValue == "0")
mCurrentLexem.mType = LexemType::Hex; mCurrentLexem.mType = LexemType::Hex;
else else if (isdigit(c) || c == '.')
if (isdigit(c) || c == '.')
{ {
mCurrentLexem.mValue.push_back(c); mCurrentLexem.mValue.push_back(c);
} }
@@ -414,14 +440,14 @@ namespace Calc
if (putback) if (putback)
input.putback(c); input.putback(c);
else else if (!result)
if (!result)
c = input.get(); c = input.get();
} }
checkNumericLexem(); checkNumericLexem();
// Recover partially processed lexem - maybe we finish processing at all but there is dec / float / string / variable // Recover partially processed lexem - maybe we finish processing at all but there is dec / float / string /
// variable
if (mCurrentLexem.mType != LexemType::None && result.mType == LexemType::None) if (mCurrentLexem.mType != LexemType::None && result.mType == LexemType::None)
result = mCurrentLexem; result = mCurrentLexem;
@@ -441,38 +467,27 @@ namespace Calc
case LexemType::Oper: case LexemType::Oper:
if (l.mValue == "-") if (l.mValue == "-")
result->mType = Ast::Type::Sub; result->mType = Ast::Type::Sub;
else else if (l.mValue == "+")
if (l.mValue == "+")
result->mType = Ast::Type::Add; result->mType = Ast::Type::Add;
else else if (l.mValue == "*")
if (l.mValue == "*")
result->mType = Ast::Type::Mul; result->mType = Ast::Type::Mul;
else else if (l.mValue == "/")
if (l.mValue == "/")
result->mType = Ast::Type::Div; result->mType = Ast::Type::Div;
else else if (l.mValue == "<")
if (l.mValue == "<")
result->mType = Ast::Type::Less; result->mType = Ast::Type::Less;
else else if (l.mValue == "<=")
if (l.mValue == "<=")
result->mType = Ast::Type::LessOrEqual; result->mType = Ast::Type::LessOrEqual;
else else if (l.mValue == ">")
if (l.mValue == ">")
result->mType = Ast::Type::Greater; result->mType = Ast::Type::Greater;
else else if (l.mValue == ">=")
if (l.mValue == ">=")
result->mType = Ast::Type::GreatorOrEqual; result->mType = Ast::Type::GreatorOrEqual;
else else if (l.mValue == "==")
if (l.mValue == "==")
result->mType = Ast::Type::Equal; result->mType = Ast::Type::Equal;
else else if (l.mValue == "!=")
if (l.mValue == "!=")
result->mType = Ast::Type::NotEqual; result->mType = Ast::Type::NotEqual;
else else if (l.mValue == "&&")
if (l.mValue == "&&")
result->mType = Ast::Type::And; result->mType = Ast::Type::And;
else else if (l.mValue == "||")
if (l.mValue == "||")
result->mType = Ast::Type::Or; result->mType = Ast::Type::Or;
break; break;
@@ -509,11 +524,11 @@ namespace Calc
} }
Lexem mLexem; Lexem mLexem;
public:
public:
Ast::PItem parseExpression(std::istream& input) Ast::PItem parseExpression(std::istream& input)
{ {
Ast::PItem operationNode(nullptr), leftNode(nullptr), rightNode(nullptr), Ast::PItem operationNode(nullptr), leftNode(nullptr), rightNode(nullptr), currentOperation(nullptr);
currentOperation(nullptr);
// While we have lexem // While we have lexem
while (mLexem = getLexem(input)) while (mLexem = getLexem(input))
@@ -545,8 +560,7 @@ namespace Calc
throw std::runtime_error("Open bracket or constant / number / string / variable expected."); throw std::runtime_error("Open bracket or constant / number / string / variable expected.");
} }
} }
else else if (!operationNode)
if (!operationNode)
{ {
// Well, there is left node already // Well, there is left node already
// See operation here // See operation here
@@ -573,10 +587,12 @@ namespace Calc
// Parse rest of expression // Parse rest of expression
rightNode = parseExpression(input); rightNode = parseExpression(input);
// If right part of expression is operation - make left side child of right part - to allow calculation in right order // If right part of expression is operation - make left side child of right part - to allow calculation
// in right order
if (operationNode) if (operationNode)
{ {
if (rightNode->isOperation() && rightNode->getOperatorLevel() <= operationNode->getOperatorLevel() && !rightNode->hasBrackets()) if (rightNode->isOperation() &&
rightNode->getOperatorLevel() <= operationNode->getOperatorLevel() && !rightNode->hasBrackets())
{ {
// Get left child of right expression - make it our right child // Get left child of right expression - make it our right child
operationNode->children().push_back(leftNode); operationNode->children().push_back(leftNode);
@@ -598,11 +614,8 @@ namespace Calc
return currentOperation ? currentOperation : leftNode; return currentOperation ? currentOperation : leftNode;
} }
public: public:
Ast::PItem parse(std::istream& input) Ast::PItem parse(std::istream& input) { return nullptr; }
{
return nullptr;
}
void testLexemParser(const std::string& test) void testLexemParser(const std::string& test)
{ {
@@ -613,14 +626,14 @@ namespace Calc
std::cout << "Lexem type: " << (int)l.mType << ", value: " << l.mValue << std::endl; std::cout << "Lexem type: " << (int)l.mType << ", value: " << l.mValue << std::endl;
} }
} }
}; };
class Worker class Worker
{ {
public: public:
Variant eval(Ast::PItem ast); Variant eval(Ast::PItem ast);
}; };
} } // namespace Calc
#endif #endif
+23 -28
View File
@@ -17,19 +17,19 @@ BOOL WINAPI CrashReporter::Callback(LPVOID arg)
return TRUE; return TRUE;
} }
typedef int(__stdcall *CrInstallProc)(__in PCR_INSTALL_INFOW pInfo); typedef int(__stdcall* CrInstallProc)(__in PCR_INSTALL_INFOW pInfo);
static CrInstallProc CrInstall = nullptr; static CrInstallProc CrInstall = nullptr;
typedef int(__stdcall *CrUninstallProc)(); typedef int(__stdcall* CrUninstallProc)();
static CrUninstallProc CrUninstall = nullptr; static CrUninstallProc CrUninstall = nullptr;
typedef int(__stdcall *CrInstallIntoCurrentThreadProc)(DWORD dwFlags); typedef int(__stdcall* CrInstallIntoCurrentThreadProc)(DWORD dwFlags);
static CrInstallIntoCurrentThreadProc CrInstallIntoCurrentThread = nullptr; static CrInstallIntoCurrentThreadProc CrInstallIntoCurrentThread = nullptr;
typedef int(__stdcall *CrUninstallFromCurrentThreadProc)(); typedef int(__stdcall* CrUninstallFromCurrentThreadProc)();
static CrUninstallFromCurrentThreadProc CrUninstallFromCurrentThread = nullptr; static CrUninstallFromCurrentThreadProc CrUninstallFromCurrentThread = nullptr;
typedef int(__stdcall *CrGetLastErrorMsgProc)(LPWSTR buffer, UINT size); typedef int(__stdcall* CrGetLastErrorMsgProc)(LPWSTR buffer, UINT size);
static CrGetLastErrorMsgProc CrGetLastErrorMsg = nullptr; static CrGetLastErrorMsgProc CrGetLastErrorMsg = nullptr;
static HMODULE CrLibraryHandle = NULL; static HMODULE CrLibraryHandle = NULL;
@@ -49,8 +49,10 @@ void CrashReporter::init(const std::string& appname, const std::string& version,
CrInstall = (CrInstallProc)::GetProcAddress(CrLibraryHandle, "crInstallW"); CrInstall = (CrInstallProc)::GetProcAddress(CrLibraryHandle, "crInstallW");
CrUninstall = (CrUninstallProc)::GetProcAddress(CrLibraryHandle, "crUninstall"); CrUninstall = (CrUninstallProc)::GetProcAddress(CrLibraryHandle, "crUninstall");
CrInstallIntoCurrentThread = (CrInstallIntoCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crInstallToCurrentThread2"); CrInstallIntoCurrentThread =
CrUninstallFromCurrentThread = (CrUninstallFromCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crUninstallFromCurrentThread"); (CrInstallIntoCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crInstallToCurrentThread2");
CrUninstallFromCurrentThread =
(CrUninstallFromCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crUninstallFromCurrentThread");
CrGetLastErrorMsg = (CrGetLastErrorMsgProc)::GetProcAddress(CrLibraryHandle, "crGetLastErrorMsgW"); CrGetLastErrorMsg = (CrGetLastErrorMsgProc)::GetProcAddress(CrLibraryHandle, "crGetLastErrorMsgW");
#else #else
CrInstall = &crInstallW; CrInstall = &crInstallW;
@@ -70,8 +72,7 @@ void CrashReporter::init(const std::string& appname, const std::string& version,
{ {
std::wstring appname, version, url; std::wstring appname, version, url;
} unicode; } unicode;
unicode.appname = StringHelper::makeTstring(appname), unicode.appname = StringHelper::makeTstring(appname), unicode.version = StringHelper::makeTstring(version),
unicode.version = StringHelper::makeTstring(version),
unicode.url = StringHelper::makeTstring(url); unicode.url = StringHelper::makeTstring(url);
if (unicode.appname.empty()) if (unicode.appname.empty())
@@ -86,9 +87,9 @@ void CrashReporter::init(const std::string& appname, const std::string& version,
info.pszAppName = unicode.appname.c_str(); info.pszAppName = unicode.appname.c_str();
info.pszAppVersion = unicode.version.c_str(); info.pszAppVersion = unicode.version.c_str();
info.pszEmailSubject = TEXT("Crash report"); info.pszEmailSubject = TEXT("Crash report");
//info.pszEmailTo = L"amegyeri@minerva-soft.com"; // info.pszEmailTo = L"amegyeri@minerva-soft.com";
//info.pszUrl = L"http://ftp.minerva-soft.com/crashlog/crashrpt.php"; // info.pszUrl = L"http://ftp.minerva-soft.com/crashlog/crashrpt.php";
//info.pszUrl = L"http://sip.crypttalk.com/crashlog/crashrpt.php"; // info.pszUrl = L"http://sip.crypttalk.com/crashlog/crashrpt.php";
info.pszUrl = unicode.url.c_str(); info.pszUrl = unicode.url.c_str();
info.pfnCrashCallback = Callback; info.pfnCrashCallback = Callback;
info.uPriorities[CR_HTTP] = 1; info.uPriorities[CR_HTTP] = 1;
@@ -103,7 +104,8 @@ void CrashReporter::init(const std::string& appname, const std::string& version,
wchar_t errorMsg[512] = L""; wchar_t errorMsg[512] = L"";
CrGetLastErrorMsg(errorMsg, 512); CrGetLastErrorMsg(errorMsg, 512);
OutputDebugStringW(errorMsg); OutputDebugStringW(errorMsg);
//LogCritical("Core", << "Failed to install CrashReporter with code " << nResult << " and message " << errorMsg); // LogCritical("Core", << "Failed to install CrashReporter with code " << nResult << " and message " <<
// errorMsg);
} }
#endif #endif
} }
@@ -120,7 +122,8 @@ void CrashReporter::free()
CrUninstallFromCurrentThread = nullptr; CrUninstallFromCurrentThread = nullptr;
CrGetLastErrorMsg = nullptr; CrGetLastErrorMsg = nullptr;
#if defined(CRASHRPT_DYNAMIC) #if defined(CRASHRPT_DYNAMIC)
::FreeLibrary(CrLibraryHandle); CrLibraryHandle = NULL; ::FreeLibrary(CrLibraryHandle);
CrLibraryHandle = NULL;
#endif #endif
} }
#endif #endif
@@ -129,8 +132,8 @@ void CrashReporter::free()
bool CrashReporter::isLoaded() bool CrashReporter::isLoaded()
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
return !(!CrInstall || !CrUninstall || !CrGetLastErrorMsg || return !(!CrInstall || !CrUninstall || !CrGetLastErrorMsg || !CrInstallIntoCurrentThread ||
!CrInstallIntoCurrentThread || !CrUninstallFromCurrentThread); !CrUninstallFromCurrentThread);
#else #else
return false; return false;
#endif #endif
@@ -174,20 +177,12 @@ CrashReporterGuard::~CrashReporterGuard()
#else #else
CrashReporterThreadPoint::CrashReporterThreadPoint() CrashReporterThreadPoint::CrashReporterThreadPoint() {}
{
}
CrashReporterThreadPoint::~CrashReporterThreadPoint() CrashReporterThreadPoint::~CrashReporterThreadPoint() {}
{
}
CrashReporterGuard::CrashReporterGuard() CrashReporterGuard::CrashReporterGuard() {}
{
}
CrashReporterGuard::~CrashReporterGuard() CrashReporterGuard::~CrashReporterGuard() {}
{
}
#endif #endif
+5 -4
View File
@@ -4,8 +4,8 @@
#include <string> #include <string>
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
# include <WinSock2.h> #include <WinSock2.h>
# include <Windows.h> #include <Windows.h>
#endif #endif
// Helper class to translate SEH exceptions to C++ - sometimes it is needed // Helper class to translate SEH exceptions to C++ - sometimes it is needed
@@ -15,6 +15,7 @@ class SE_Exception
{ {
private: private:
unsigned int nSE; unsigned int nSE;
public: public:
SE_Exception() {} SE_Exception() {}
SE_Exception(unsigned int n) : nSE(n) {} SE_Exception(unsigned int n) : nSE(n) {}
@@ -26,10 +27,10 @@ extern void SEHToCpp(unsigned int, EXCEPTION_POINTERS*);
// Although better way is to have _set_se_translator set - in our case we do not call it. // Although better way is to have _set_se_translator set - in our case we do not call it.
// The cause is usage of CrashRpt libraries - it gives better control on exception reporting. // The cause is usage of CrashRpt libraries - it gives better control on exception reporting.
# define SET_SEH_TO_CPP #define SET_SEH_TO_CPP
//_set_se_translator(&SEHToCpp) //_set_se_translator(&SEHToCpp)
#else #else
# define SET_SEH_TO_CPP #define SET_SEH_TO_CPP
#endif #endif
class CrashReporter class CrashReporter
+2 -5
View File
@@ -2,12 +2,9 @@
#include "HL_String.h" #include "HL_String.h"
// --------- CsvFile ---------------- // --------- CsvFile ----------------
CsvReader::CsvReader(std::istream& stream) CsvReader::CsvReader(std::istream& stream) : mInputStream(stream) {}
:mInputStream(stream)
{}
CsvReader::~CsvReader() CsvReader::~CsvReader() {}
{}
std::istream& CsvReader::stream() const std::istream& CsvReader::stream() const
{ {
+8 -20
View File
@@ -38,11 +38,10 @@ enum
ERR_OPENSLES = 23 // OpenSL ES failed. Subcode has actual error code. ERR_OPENSLES = 23 // OpenSL ES failed. Subcode has actual error code.
}; };
class Exception: public std::exception class Exception : public std::exception
{ {
public: public:
Exception(int code, int subcode = 0) Exception(int code, int subcode = 0) : mCode(code), mSubcode(subcode)
:mCode(code), mSubcode(subcode)
{ {
std::snprintf(mMessage, sizeof(mMessage), "%d-%d", code, subcode); std::snprintf(mMessage, sizeof(mMessage), "%d-%d", code, subcode);
} }
@@ -50,32 +49,21 @@ public:
Exception(int code, const char* message) Exception(int code, const char* message)
{ {
if (message) if (message)
strncpy(mMessage, message, (sizeof mMessage) - 1 ); strncpy(mMessage, message, (sizeof mMessage) - 1);
} }
Exception(const Exception& src) Exception(const Exception& src) : mCode(src.mCode), mSubcode(src.mSubcode)
:mCode(src.mCode), mSubcode(src.mSubcode)
{ {
memcpy(mMessage, src.mMessage, sizeof mMessage); memcpy(mMessage, src.mMessage, sizeof mMessage);
} }
~Exception() ~Exception() {}
{ }
int code() const int code() const { return mCode; }
{
return mCode;
}
int subcode() const int subcode() const { return mSubcode; }
{
return mSubcode;
}
const char* what() const noexcept const char* what() const noexcept { return mMessage; }
{
return mMessage;
}
protected: protected:
int mCode = 0, mSubcode = 0; int mCode = 0, mSubcode = 0;
+8 -6
View File
@@ -2,9 +2,9 @@
#include <fstream> #include <fstream>
#if defined(TARGET_LINUX) || defined(TARGET_OSX) || defined(TARGET_ANDROID) #if defined(TARGET_LINUX) || defined(TARGET_OSX) || defined(TARGET_ANDROID)
# include <unistd.h> #include <unistd.h>
# include <sys/statvfs.h> #include <sys/statvfs.h>
# include <memory.h> #include <memory.h>
#endif #endif
bool FileHelper::exists(const std::string& s) bool FileHelper::exists(const std::string& s)
@@ -102,7 +102,8 @@ size_t FileHelper::getFreespace(const std::string& path)
size_t r = static_cast<size_t>(-1); size_t r = static_cast<size_t>(-1);
#if defined(TARGET_LINUX) #if defined(TARGET_LINUX)
struct statvfs stats; memset(&stats, 0, sizeof stats); struct statvfs stats;
memset(&stats, 0, sizeof stats);
int retcode = statvfs(path.c_str(), &stats); int retcode = statvfs(path.c_str(), &stats);
if (retcode == 0) if (retcode == 0)
@@ -111,7 +112,7 @@ size_t FileHelper::getFreespace(const std::string& path)
return r; return r;
} }
std::string FileHelper::expandUserHome(const std::string &path) std::string FileHelper::expandUserHome(const std::string& path)
{ {
if (path.empty() || path[0] != '~') if (path.empty() || path[0] != '~')
return path; // No expansion needed return path; // No expansion needed
@@ -124,7 +125,8 @@ std::string FileHelper::expandUserHome(const std::string &path)
{ {
home_dir = std::getenv("HOMEDRIVE"); home_dir = std::getenv("HOMEDRIVE");
const char* homepath = std::getenv("HOMEPATH"); const char* homepath = std::getenv("HOMEPATH");
if (home_dir && homepath) { if (home_dir && homepath)
{
std::string fullpath(home_dir); std::string fullpath(home_dir);
fullpath += homepath; fullpath += homepath;
return fullpath + path.substr(1); return fullpath + path.substr(1);
+52 -17
View File
@@ -102,8 +102,7 @@ bool Packet::parseV3(const ByteBuffer& packet)
if (!sourceAddr4.isEmpty()) if (!sourceAddr4.isEmpty())
mSourceAddress = sourceAddr4; mSourceAddress = sourceAddr4;
else else if (!sourceAddr6.isEmpty())
if (!sourceAddr6.isEmpty())
mSourceAddress = sourceAddr6; mSourceAddress = sourceAddr6;
if (!mSourceAddress.isEmpty()) if (!mSourceAddress.isEmpty())
@@ -111,8 +110,7 @@ bool Packet::parseV3(const ByteBuffer& packet)
if (!destAddr4.isEmpty()) if (!destAddr4.isEmpty())
mDestinationAddress = destAddr4; mDestinationAddress = destAddr4;
else else if (!destAddr6.isEmpty())
if (!destAddr6.isEmpty())
mDestinationAddress = destAddr6; mDestinationAddress = destAddr6;
if (!mDestinationAddress.isEmpty()) if (!mDestinationAddress.isEmpty())
@@ -121,7 +119,7 @@ bool Packet::parseV3(const ByteBuffer& packet)
return true; return true;
} }
bool Packet::parseV2(const ByteBuffer &packet) bool Packet::parseV2(const ByteBuffer& packet)
{ {
if (packet.size() < 31) if (packet.size() < 31)
return false; return false;
@@ -148,16 +146,53 @@ bool Packet::parseV2(const ByteBuffer &packet)
return true; return true;
} }
#define WRITE_CHUNK_UCHAR(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(1); w.writeUChar((uint8_t)V);} #define WRITE_CHUNK_UCHAR(T, V) \
#define WRITE_CHUNK_USHORT(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(2); w.writeUShort((uint16_t)V);} { \
#define WRITE_CHUNK_UINT(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(4); w.writeUInt((uint32_t)V);} w.writeUShort((uint16_t)mVendorId); \
#define WRITE_CHUNK_IP4(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(4); w.writeIp(V);} w.writeUShort((uint16_t)T); \
#define WRITE_CHUNK_IP6(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(8); w.writeIp(V);} w.writeUShort(1); \
#define WRITE_CHUNK_BUFFER(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(8); w.writeBuffer(V.data(), V.size());} w.writeUChar((uint8_t)V); \
}
#define WRITE_CHUNK_USHORT(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(2); \
w.writeUShort((uint16_t)V); \
}
#define WRITE_CHUNK_UINT(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(4); \
w.writeUInt((uint32_t)V); \
}
#define WRITE_CHUNK_IP4(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(4); \
w.writeIp(V); \
}
#define WRITE_CHUNK_IP6(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(8); \
w.writeIp(V); \
}
#define WRITE_CHUNK_BUFFER(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(8); \
w.writeBuffer(V.data(), V.size()); \
}
ByteBuffer Packet::buildV3() ByteBuffer Packet::buildV3()
{ {
ByteBuffer r; r.resize(mBody.size() + 512); ByteBuffer r;
r.resize(mBody.size() + 512);
BufferWriter w(r); BufferWriter w(r);
// Signature // Signature
@@ -174,8 +209,7 @@ ByteBuffer Packet::buildV3()
{ {
if (mSourceAddress.isV4()) if (mSourceAddress.isV4())
WRITE_CHUNK_IP4(ChunkType::IP4SourceAddress, mSourceAddress) WRITE_CHUNK_IP4(ChunkType::IP4SourceAddress, mSourceAddress)
else else if (mSourceAddress.isV6())
if (mSourceAddress.isV6())
WRITE_CHUNK_IP6(ChunkType::IP6SourceAddress, mSourceAddress); WRITE_CHUNK_IP6(ChunkType::IP6SourceAddress, mSourceAddress);
WRITE_CHUNK_USHORT(ChunkType::SourcePort, mSourceAddress.port()); WRITE_CHUNK_USHORT(ChunkType::SourcePort, mSourceAddress.port());
@@ -186,8 +220,7 @@ ByteBuffer Packet::buildV3()
{ {
if (mDestinationAddress.isV4()) if (mDestinationAddress.isV4())
WRITE_CHUNK_IP4(ChunkType::IP4DestinationAddress, mDestinationAddress) WRITE_CHUNK_IP4(ChunkType::IP4DestinationAddress, mDestinationAddress)
else else if (mDestinationAddress.isV6())
if (mDestinationAddress.isV6())
WRITE_CHUNK_IP6(ChunkType::IP6DestinationAddress, mDestinationAddress); WRITE_CHUNK_IP6(ChunkType::IP6DestinationAddress, mDestinationAddress);
WRITE_CHUNK_USHORT(ChunkType::DestinationPort, mDestinationAddress.port()); WRITE_CHUNK_USHORT(ChunkType::DestinationPort, mDestinationAddress.port());
@@ -216,7 +249,9 @@ ByteBuffer Packet::buildV3()
r.resize(w.offset()); r.resize(w.offset());
w.rewind(); w.skip(4); w.writeUShort((uint16_t)r.size()); w.rewind();
w.skip(4);
w.writeUShort((uint16_t)r.size());
return r; return r;
} }
+15 -19
View File
@@ -6,8 +6,8 @@
namespace HEP namespace HEP
{ {
enum class ChunkType enum class ChunkType
{ {
None = 0, None = 0,
IPProtocolFamily, IPProtocolFamily,
IPProtocolID, IPProtocolID,
@@ -26,10 +26,10 @@ namespace HEP
PacketPayload, PacketPayload,
CompressedPayload, CompressedPayload,
InternalC InternalC
}; };
enum class VendorId enum class VendorId
{ {
None, None,
FreeSwitch, FreeSwitch,
Kamailio, Kamailio,
@@ -37,10 +37,10 @@ namespace HEP
Asterisk, Asterisk,
Homer, Homer,
SipXecs SipXecs
}; };
enum class ProtocolId enum class ProtocolId
{ {
Reserved = 0, Reserved = 0,
SIP, SIP,
XMPP, XMPP,
@@ -54,21 +54,17 @@ namespace HEP
IAX, IAX,
H322, H322,
H321 H321
}; };
struct Packet struct Packet
{ {
bool parseV3(const ByteBuffer& packet); bool parseV3(const ByteBuffer& packet);
bool parseV2(const ByteBuffer& packet); bool parseV2(const ByteBuffer& packet);
ByteBuffer buildV3(); ByteBuffer buildV3();
uint8_t uint8_t mIpProtocolFamily, mIpProtocolId;
mIpProtocolFamily,
mIpProtocolId;
InternetAddress InternetAddress mSourceAddress, mDestinationAddress;
mSourceAddress,
mDestinationAddress;
timeval mTimestamp; timeval mTimestamp;
ProtocolId mProtocolType; ProtocolId mProtocolType;
@@ -78,8 +74,8 @@ namespace HEP
ByteBuffer mBody; ByteBuffer mBody;
VendorId mVendorId; VendorId mVendorId;
uint32_t mBodyOffset = 0; uint32_t mBodyOffset = 0;
}; };
} } // namespace HEP
#endif #endif
File diff suppressed because it is too large Load Diff
-1
View File
@@ -36,4 +36,3 @@ public:
#endif // HL_IUUP_H #endif // HL_IUUP_H
-1
View File
@@ -2,4 +2,3 @@
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+4 -4
View File
@@ -11,13 +11,13 @@
using ice::GLogger; using ice::GLogger;
using ice::LogLock; using ice::LogLock;
using ice::LL_MEDIA;
using ice::LL_DEBUG;
using ice::LL_INFO;
using ice::LL_CRITICAL; using ice::LL_CRITICAL;
using ice::LL_DEBUG;
using ice::LL_ERROR;
using ice::LL_INFO;
using ice::LL_MEDIA;
using ice::LL_NONE; using ice::LL_NONE;
using ice::LL_SPECIAL; using ice::LL_SPECIAL;
using ice::LL_ERROR;
using ice::LogLevelHelper; using ice::LogLevelHelper;
#endif #endif
+9 -14
View File
@@ -53,8 +53,7 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForEthernet(const Packet& data)
result.mData += sizeof(VlanHeader); result.mData += sizeof(VlanHeader);
result.mLength -= sizeof(VlanHeader); result.mLength -= sizeof(VlanHeader);
proto = ntohs(vlan->mData); proto = ntohs(vlan->mData);
} } while (proto == 0x8100);
while (proto == 0x8100);
} }
// Skip MPLS headers // Skip MPLS headers
@@ -63,11 +62,11 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForEthernet(const Packet& data)
case ETHERTYPE_MPLS_UC: case ETHERTYPE_MPLS_UC:
case ETHERTYPE_MPLS_MC: case ETHERTYPE_MPLS_MC:
// Parse MPLS here until marker "bottom of mpls stack" // Parse MPLS here until marker "bottom of mpls stack"
for(bool bottomOfStack = false; !bottomOfStack; for (bool bottomOfStack = false; !bottomOfStack;
bottomOfStack = ((ntohl(*(uint32_t*)(result.mData - 4)) & MPLS_STACK_MASK) >> MPLS_STACK_SHIFT) != 0) bottomOfStack = ((ntohl(*(uint32_t*)(result.mData - 4)) & MPLS_STACK_MASK) >> MPLS_STACK_SHIFT) != 0)
{ {
result.mData += 4; result.mData += 4;
result.mLength -=4; result.mLength -= 4;
} }
break; break;
@@ -187,20 +186,16 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForIp4(const Packet& data)
struct Ip6Header struct Ip6Header
{ {
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t traffic_class_hi:4, uint8_t traffic_class_hi : 4, version : 4;
version:4; uint8_t flow_label_hi : 4, traffic_class_lo : 4;
uint8_t flow_label_hi:4,
traffic_class_lo:4;
uint16_t flow_label_lo; uint16_t flow_label_lo;
#elif __BYTE_ORDER == __BIG_ENDIAN #elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t version:4, uint8_t version : 4, traffic_class_hi : 4;
traffic_class_hi:4; uint8_t traffic_class_lo : 4, flow_label_hi : 4;
uint8_t traffic_class_lo:4,
flow_label_hi:4;
uint16_t flow_label_lo; uint16_t flow_label_lo;
#else #else
# error "Please fix endianness defines" #error "Please fix endianness defines"
#endif #endif
uint16_t payload_len; uint16_t payload_len;
@@ -220,7 +215,7 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForIp6(const Packet& data)
*/ */
result.mData += sizeof(Ip6Header); result.mData += sizeof(Ip6Header);
result.mLength -= sizeof(Ip6Header); result.mLength -= sizeof(Ip6Header);
//std::cout << sizeof(Ip6Header) << std::endl; // std::cout << sizeof(Ip6Header) << std::endl;
const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData); const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData);
result.mData += sizeof(UdpHeader); result.mData += sizeof(UdpHeader);
+8 -23
View File
@@ -12,18 +12,11 @@ public:
const uint8_t* mData; const uint8_t* mData;
size_t mLength; size_t mLength;
Packet(const uint8_t* data, size_t length) Packet(const uint8_t* data, size_t length) : mData(data), mLength(length) {}
:mData(data), mLength(length)
{}
Packet() Packet() : mData(nullptr), mLength(0) {}
:mData(nullptr), mLength(0)
{}
bool is_empty() const bool is_empty() const { return mData == nullptr || mLength == 0; }
{
return mData == nullptr || mLength == 0;
}
}; };
struct Payload struct Payload
@@ -51,7 +44,7 @@ public:
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
struct /*__attribute__((packed))*/ LinuxSllHeader struct /*__attribute__((packed))*/ LinuxSllHeader
#else #else
struct __attribute__((packed)) LinuxSllHeader struct __attribute__((packed)) LinuxSllHeader
#endif #endif
{ {
@@ -82,18 +75,11 @@ public:
uint8_t mTtl; /* time to live */ uint8_t mTtl; /* time to live */
uint8_t mProtocol; /* protocol */ uint8_t mProtocol; /* protocol */
uint16_t mChecksum; /* checksum */ uint16_t mChecksum; /* checksum */
in_addr mSource, in_addr mSource, mDestination; /* source and dest address */
mDestination; /* source and dest address */
int headerLength() const int headerLength() const { return (mVhl & 0x0f) * 4; }
{
return (mVhl & 0x0f) * 4;
}
int version() const int version() const { return mVhl >> 4; }
{
return mVhl >> 4;
}
const in_addr& source4() const { return mSource; } const in_addr& source4() const { return mSource; }
const in_addr& dest4() const { return mDestination; } const in_addr& dest4() const { return mDestination; }
@@ -127,11 +113,10 @@ public:
#define TH_URG 0x20 #define TH_URG 0x20
#define TH_ECE 0x40 #define TH_ECE 0x40
#define TH_CWR 0x80 #define TH_CWR 0x80
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR) #define TH_FLAGS (TH_FIN | TH_SYN | TH_RST | TH_ACK | TH_URG | TH_ECE | TH_CWR)
uint16_t mWindow; /* window */ uint16_t mWindow; /* window */
uint16_t mChecksum; /* checksum */ uint16_t mChecksum; /* checksum */
uint16_t mUrgentPointer; /* urgent pointer */ uint16_t mUrgentPointer; /* urgent pointer */
}; };
}; };
#endif #endif
+23 -18
View File
@@ -4,7 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#if defined(TARGET_LINUX) || defined(TARGET_ANDROID) #if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
# include <asm/ioctls.h> #include <asm/ioctls.h>
#endif #endif
#include "../engine_config.h" #include "../engine_config.h"
@@ -12,19 +12,17 @@
#include "HL_Log.h" #include "HL_Log.h"
#if defined(TARGET_OSX) || defined(TARGET_LINUX) #if defined(TARGET_OSX) || defined(TARGET_LINUX)
# include <fcntl.h> #include <fcntl.h>
#endif #endif
#if !defined(TARGET_WIN) #if !defined(TARGET_WIN)
# include <unistd.h> #include <unistd.h>
#endif #endif
#include <assert.h> #include <assert.h>
#define LOG_SUBSYSTEM "network" #define LOG_SUBSYSTEM "network"
DatagramSocket::DatagramSocket() DatagramSocket::DatagramSocket() : mFamily(AF_INET), mHandle(INVALID_SOCKET), mLocalPort(0) {}
:mFamily(AF_INET), mHandle(INVALID_SOCKET), mLocalPort(0)
{}
DatagramSocket::~DatagramSocket() DatagramSocket::~DatagramSocket()
{ {
@@ -41,8 +39,10 @@ void DatagramSocket::open(int family)
mHandle = ::socket(mFamily, SOCK_DGRAM, IPPROTO_UDP); mHandle = ::socket(mFamily, SOCK_DGRAM, IPPROTO_UDP);
if (mHandle != INVALID_SOCKET) if (mHandle != INVALID_SOCKET)
{ {
sockaddr_in addr4; sockaddr_in6 addr6; sockaddr_in addr4;
memset(&addr4, 0, sizeof(addr4)); memset(&addr6, 0, sizeof(addr6)); sockaddr_in6 addr6;
memset(&addr4, 0, sizeof(addr4));
memset(&addr6, 0, sizeof(addr6));
socklen_t l = mFamily == AF_INET ? sizeof(addr4) : sizeof(addr6); socklen_t l = mFamily == AF_INET ? sizeof(addr4) : sizeof(addr6);
int retcode = getsockname(mHandle, (mFamily == AF_INET ? (sockaddr*)&addr4 : (sockaddr*)&addr6), &l); int retcode = getsockname(mHandle, (mFamily == AF_INET ? (sockaddr*)&addr4 : (sockaddr*)&addr6), &l);
if (!retcode) if (!retcode)
@@ -57,15 +57,16 @@ int DatagramSocket::localport()
return mLocalPort; return mLocalPort;
} }
void DatagramSocket::sendDatagram(InternetAddress &dest, const void *packetData, unsigned int packetSize) void DatagramSocket::sendDatagram(InternetAddress& dest, const void* packetData, unsigned int packetSize)
{ {
if (mHandle == INVALID_SOCKET) if (mHandle == INVALID_SOCKET)
return; return;
/*int sent = */::sendto(mHandle, (const char*)packetData, packetSize, 0, dest.genericsockaddr(), dest.sockaddrLen()); /*int sent = */ ::sendto(mHandle, (const char*)packetData, packetSize, 0, dest.genericsockaddr(),
dest.sockaddrLen());
} }
unsigned DatagramSocket::recvDatagram(InternetAddress &src, void *packetBuffer, unsigned packetCapacity) unsigned DatagramSocket::recvDatagram(InternetAddress& src, void* packetBuffer, unsigned packetCapacity)
{ {
if (mHandle == INVALID_SOCKET) if (mHandle == INVALID_SOCKET)
return 0; return 0;
@@ -76,8 +77,14 @@ unsigned DatagramSocket::recvDatagram(InternetAddress &src, void *packetBuffer,
sockaddr_in6 addr_6 = {AF_INET6, 0, 0, {0}, 0}; sockaddr_in6 addr_6 = {AF_INET6, 0, 0, {0}, 0};
switch (mFamily) switch (mFamily)
{ {
case AF_INET: addr = (sockaddr*)&addr_4; addrLen = sizeof(addr_4); break; case AF_INET:
case AF_INET6: addr = (sockaddr*)&addr_6; addrLen = sizeof(addr_6); break; addr = (sockaddr*)&addr_4;
addrLen = sizeof(addr_4);
break;
case AF_INET6:
addr = (sockaddr*)&addr_6;
addrLen = sizeof(addr_6);
break;
default: default:
assert(0); assert(0);
} }
@@ -131,7 +138,7 @@ bool DatagramSocket::setBlocking(bool blocking)
int flags = fcntl(mHandle, F_GETFL, 0); int flags = fcntl(mHandle, F_GETFL, 0);
if (flags < 0) if (flags < 0)
return false; return false;
flags = blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK); flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
return (fcntl(mHandle, F_SETFL, flags) == 0) ? true : false; return (fcntl(mHandle, F_SETFL, flags) == 0) ? true : false;
#endif #endif
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
@@ -152,9 +159,7 @@ DatagramAgreggator::DatagramAgreggator()
mMaxHandle = 0; mMaxHandle = 0;
} }
DatagramAgreggator::~DatagramAgreggator() DatagramAgreggator::~DatagramAgreggator() {}
{
}
void DatagramAgreggator::addSocket(PDatagramSocket socket) void DatagramAgreggator::addSocket(PDatagramSocket socket)
{ {
@@ -196,6 +201,6 @@ bool DatagramAgreggator::waitForData(std::chrono::milliseconds timeout)
tv.tv_sec = timeout.count() / 1000; tv.tv_sec = timeout.count() / 1000;
tv.tv_usec = (timeout.count() % 1000) * 1000; tv.tv_usec = (timeout.count() % 1000) * 1000;
int rescode = ::select(mMaxHandle+1, &mReadSet, nullptr, nullptr, &tv); int rescode = ::select(mMaxHandle + 1, &mReadSet, nullptr, nullptr, &tv);
return rescode > 0; return rescode > 0;
} }
+1 -1
View File
@@ -15,13 +15,13 @@ class NetworkSocket
{ {
public: public:
virtual int localport() = 0; virtual int localport() = 0;
}; };
class DatagramSocket class DatagramSocket
{ {
friend class SocketHeap; friend class SocketHeap;
friend class DatagramAgreggator; friend class DatagramAgreggator;
public: public:
DatagramSocket(); DatagramSocket();
virtual ~DatagramSocket(); virtual ~DatagramSocket();
File diff suppressed because it is too large Load Diff
+9 -9
View File
@@ -10,7 +10,7 @@
#include <windows.h> #include <windows.h>
#if defined(USE_MINIDUMP) #if defined(USE_MINIDUMP)
# include <DbgHelp.h> #include <DbgHelp.h>
#endif #endif
int winVersion() int winVersion()
@@ -39,17 +39,17 @@ int winVersion()
return Win_Seven; return Win_Seven;
else else
return Win_Vista; return Win_Vista;
} }
// ----------------- CrashMiniDump ----------------- // ----------------- CrashMiniDump -----------------
#if defined(USE_MINIDUMP) #if defined(USE_MINIDUMP)
static LONG WINAPI MyExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo) static LONG WINAPI MyExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo)
{ {
// Open the file // Open the file
HANDLE hFile = CreateFile( L"MiniDump.dmp", GENERIC_READ | GENERIC_WRITE, HANDLE hFile =
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); CreateFile(L"MiniDump.dmp", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) ) if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
{ {
// Create the minidump // Create the minidump
MINIDUMP_EXCEPTION_INFORMATION mdei; MINIDUMP_EXCEPTION_INFORMATION mdei;
@@ -60,11 +60,11 @@ static LONG WINAPI MyExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo)
MINIDUMP_TYPE mdt = MiniDumpWithFullMemory; MINIDUMP_TYPE mdt = MiniDumpWithFullMemory;
BOOL rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), BOOL rv = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt,
hFile, mdt, (ExceptionInfo != 0) ? &mdei : 0, 0, 0 ); (ExceptionInfo != 0) ? &mdei : 0, 0, 0);
// Close the file // Close the file
CloseHandle( hFile ); CloseHandle(hFile);
} }
else else
{ {
@@ -140,7 +140,7 @@ int _kbhit()
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf); fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF) if (ch != EOF)
{ {
ungetc(ch, stdin); ungetc(ch, stdin);
return 1; return 1;
+2 -2
View File
@@ -41,7 +41,7 @@ int iosVersion();
#include <sys/select.h> #include <sys/select.h>
#include <termios.h> #include <termios.h>
#if defined(TARGET_LINUX) #if defined(TARGET_LINUX)
//# include <stropts.h> // # include <stropts.h>
#endif #endif
extern int _kbhit(); extern int _kbhit();
@@ -49,6 +49,6 @@ extern int _kbhit();
#endif #endif
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
# include <conio.h> #include <conio.h>
#endif #endif
#endif #endif
+2 -4
View File
@@ -5,11 +5,9 @@
#include "HL_Pointer.h" #include "HL_Pointer.h"
UsageCounter::UsageCounter() UsageCounter::UsageCounter() {}
{}
UsageCounter::~UsageCounter() UsageCounter::~UsageCounter() {}
{}
int UsageCounter::obtain(int usageId) int UsageCounter::obtain(int usageId)
{ {
+97 -70
View File
@@ -19,36 +19,36 @@
#include <vector> #include <vector>
#ifndef HL_RTP_POOL #ifndef HL_RTP_POOL
# define HL_RTP_POOL 1 #define HL_RTP_POOL 1
#endif #endif
namespace hl namespace hl
{ {
#if HL_RTP_POOL #if HL_RTP_POOL
/// @class FixedBlockPool /// @class FixedBlockPool
/// A process-wide, fixed-block pool with a lock-free thread-local fast path: uniform 256-byte /// A process-wide, fixed-block pool with a lock-free thread-local fast path: uniform 256-byte
/// blocks carved from 64 KB chunks, an intrusive thread-local free list, and a per-block header /// blocks carved from 64 KB chunks, an intrusive thread-local free list, and a per-block header
/// tag so deallocate() is O(1) and lock-free for any block (and can tell pooled blocks from the /// tag so deallocate() is O(1) and lock-free for any block (and can tell pooled blocks from the
/// global-allocator fallback used for oversized requests) regardless of the freeing thread. /// global-allocator fallback used for oversized requests) regardless of the freeing thread.
/// ///
/// CROSS-THREAD RECLAIM (2026-07-26) — fixes an unbounded capacity ratchet. A uniform block size /// CROSS-THREAD RECLAIM (2026-07-26) — fixes an unbounded capacity ratchet. A uniform block size
/// makes a block allocated on one thread safe to free on another, but "safe" is not "reclaimed". /// makes a block allocated on one thread safe to free on another, but "safe" is not "reclaimed".
/// With a purely thread-local free list the block joins the FREEING thread's list, so a /// With a purely thread-local free list the block joins the FREEING thread's list, so a
/// producer/consumer split strands it: vq-core allocates RTPPacket / jitter-buffer nodes on the /// producer/consumer split strands it: vq-core allocates RTPPacket / jitter-buffer nodes on the
/// capture threads and frees them on the interval/reap side, so the capture threads' lists were /// capture threads and frees them on the interval/reap side, so the capture threads' lists were
/// perpetually empty and refill() carved a fresh chunk every BlocksPerChunk allocations, forever. /// perpetually empty and refill() carved a fresh chunk every BlocksPerChunk allocations, forever.
/// Observed on vqmonitor (the only box with >1 capture thread): capacityBlocks() 256 -> 11.8M /// Observed on vqmonitor (the only box with >1 capture thread): capacityBlocks() 256 -> 11.8M
/// = 2.7 GiB over 4 days at ~25 MiB/h, while activeBlocks() drained to 0 every idle interval. /// = 2.7 GiB over 4 days at ~25 MiB/h, while activeBlocks() drained to 0 every idle interval.
/// ///
/// The fix is a process-wide depot of free-block batches. A thread whose free list grows past /// The fix is a process-wide depot of free-block batches. A thread whose free list grows past
/// ThreadCacheHighWater hands a batch down to the depot; a thread that runs dry takes a batch /// ThreadCacheHighWater hands a batch down to the depot; a thread that runs dry takes a batch
/// from the depot before carving a new chunk; a thread flushes its remainder to the depot on /// from the depot before carving a new chunk; a thread flushes its remainder to the depot on
/// exit. The depot is touched once per BlocksPerChunk operations and the hysteresis between /// exit. The depot is touched once per BlocksPerChunk operations and the hysteresis between
/// "empty" and the high-water keeps it off the per-packet path, so the fast path stays lock-free. /// "empty" and the high-water keeps it off the per-packet path, so the fast path stays lock-free.
/// capacityBlocks() now plateaus at the true concurrent high-water instead of ratcheting. /// capacityBlocks() now plateaus at the true concurrent high-water instead of ratcheting.
class FixedBlockPool class FixedBlockPool
{ {
public: public:
/// Usable bytes handed back to the caller from a pooled block. Comfortably covers the /// Usable bytes handed back to the caller from a pooled block. Comfortably covers the
/// shared_ptr nodes we pool (control block + RTPPacket / RtpBuffer::Packet, ~90-130 bytes). /// shared_ptr nodes we pool (control block + RTPPacket / RtpBuffer::Packet, ~90-130 bytes).
static constexpr std::size_t PayloadSize = 240; static constexpr std::size_t PayloadSize = 240;
@@ -124,15 +124,9 @@ namespace hl
/// activeBlocks() is a genuine leak of referenced objects rather than a /// activeBlocks() is a genuine leak of referenced objects rather than a
/// traffic peak that merely carved extra chunks. Covers every pooled /// traffic peak that merely carved extra chunks. Covers every pooled
/// shared_ptr node (allocate_shared<RTPPacket> + jitter-buffer packets). /// shared_ptr node (allocate_shared<RTPPacket> + jitter-buffer packets).
static std::int64_t activeBlocks() noexcept static std::int64_t activeBlocks() noexcept { return s_activeBlocks.load(std::memory_order_relaxed); }
{
return s_activeBlocks.load(std::memory_order_relaxed);
}
/// Live oversized allocations that overflowed to ::operator new. /// Live oversized allocations that overflowed to ::operator new.
static std::int64_t activeGlobalAllocations() noexcept static std::int64_t activeGlobalAllocations() noexcept { return s_activeGlobal.load(std::memory_order_relaxed); }
{
return s_activeGlobal.load(std::memory_order_relaxed);
}
/// Total blocks ever carved (chunks x BlocksPerChunk) = high-water capacity. /// Total blocks ever carved (chunks x BlocksPerChunk) = high-water capacity.
/// With cross-thread reclaim this PLATEAUS once the pool has covered the /// With cross-thread reclaim this PLATEAUS once the pool has covered the
/// concurrent high-water; sustained linear growth means reclaim is not working. /// concurrent high-water; sustained linear growth means reclaim is not working.
@@ -143,18 +137,12 @@ namespace hl
/// Free blocks parked in the process-wide depot, i.e. reclaimed from a freeing /// Free blocks parked in the process-wide depot, i.e. reclaimed from a freeing
/// thread and available to any allocating thread. Healthy steady state is a /// thread and available to any allocating thread. Healthy steady state is a
/// non-zero, bounded value: it is the surplus that used to be stranded. /// non-zero, bounded value: it is the surplus that used to be stranded.
static std::int64_t depotBlocks() noexcept static std::int64_t depotBlocks() noexcept { return s_depotBlocks.load(std::memory_order_relaxed); }
{
return s_depotBlocks.load(std::memory_order_relaxed);
}
/// Approximate live pooled payload bytes (excludes per-block header). /// Approximate live pooled payload bytes (excludes per-block header).
static std::int64_t activeBytes() noexcept static std::int64_t activeBytes() noexcept { return activeBlocks() * static_cast<std::int64_t>(PayloadSize); }
{
return activeBlocks() * static_cast<std::int64_t>(PayloadSize);
}
/// @} /// @}
private: private:
inline static std::atomic<std::int64_t> s_activeBlocks{0}; inline static std::atomic<std::int64_t> s_activeBlocks{0};
inline static std::atomic<std::int64_t> s_activeGlobal{0}; inline static std::atomic<std::int64_t> s_activeGlobal{0};
inline static std::atomic<std::int64_t> s_chunks{0}; inline static std::atomic<std::int64_t> s_chunks{0};
@@ -174,10 +162,7 @@ namespace hl
static_assert(PayloadSize >= 2 * sizeof(void*) + sizeof(std::size_t), static_assert(PayloadSize >= 2 * sizeof(void*) + sizeof(std::size_t),
"pooled block payload must hold the free-list, depot and length slots"); "pooled block payload must hold the free-list, depot and length slots");
static uint64_t& tagOf(void* block) noexcept static uint64_t& tagOf(void* block) noexcept { return *reinterpret_cast<uint64_t*>(block); }
{
return *reinterpret_cast<uint64_t*>(block);
}
static void*& nextOf(void* block) noexcept static void*& nextOf(void* block) noexcept
{ {
@@ -317,36 +302,70 @@ namespace hl
static ChunkRegistry* instance = new ChunkRegistry(); static ChunkRegistry* instance = new ChunkRegistry();
return *instance; return *instance;
} }
}; };
#endif // HL_RTP_POOL #endif // HL_RTP_POOL
/// Mode-independent accessors for the pool diagnostics, so callers need not know /// Mode-independent accessors for the pool diagnostics, so callers need not know
/// whether HL_RTP_POOL is compiled in. When pooling is disabled they return -1 /// whether HL_RTP_POOL is compiled in. When pooling is disabled they return -1
/// ("not applicable") since allocate_shared then behaves like make_shared. /// ("not applicable") since allocate_shared then behaves like make_shared.
#if HL_RTP_POOL #if HL_RTP_POOL
inline std::int64_t poolActiveBlocks() noexcept { return FixedBlockPool::activeBlocks(); } inline std::int64_t poolActiveBlocks() noexcept
inline std::int64_t poolCapacityBlocks() noexcept { return FixedBlockPool::capacityBlocks(); } {
inline std::int64_t poolDepotBlocks() noexcept { return FixedBlockPool::depotBlocks(); } return FixedBlockPool::activeBlocks();
inline std::int64_t poolActiveGlobal() noexcept { return FixedBlockPool::activeGlobalAllocations(); } }
inline std::int64_t poolActiveBytes() noexcept { return FixedBlockPool::activeBytes(); } inline std::int64_t poolCapacityBlocks() noexcept
{
return FixedBlockPool::capacityBlocks();
}
inline std::int64_t poolDepotBlocks() noexcept
{
return FixedBlockPool::depotBlocks();
}
inline std::int64_t poolActiveGlobal() noexcept
{
return FixedBlockPool::activeGlobalAllocations();
}
inline std::int64_t poolActiveBytes() noexcept
{
return FixedBlockPool::activeBytes();
}
#else #else
inline std::int64_t poolActiveBlocks() noexcept { return -1; } inline std::int64_t poolActiveBlocks() noexcept
inline std::int64_t poolCapacityBlocks() noexcept { return -1; } {
inline std::int64_t poolDepotBlocks() noexcept { return -1; } return -1;
inline std::int64_t poolActiveGlobal() noexcept { return -1; } }
inline std::int64_t poolActiveBytes() noexcept { return -1; } inline std::int64_t poolCapacityBlocks() noexcept
{
return -1;
}
inline std::int64_t poolDepotBlocks() noexcept
{
return -1;
}
inline std::int64_t poolActiveGlobal() noexcept
{
return -1;
}
inline std::int64_t poolActiveBytes() noexcept
{
return -1;
}
#endif #endif
/// @class PoolAllocator /// @class PoolAllocator
/// A stateless, std-conforming Allocator suitable for std::allocate_shared. When HL_RTP_POOL is /// A stateless, std-conforming Allocator suitable for std::allocate_shared. When HL_RTP_POOL is
/// enabled it serves single-node allocations from FixedBlockPool; otherwise (and for any request /// enabled it serves single-node allocations from FixedBlockPool; otherwise (and for any request
/// that does not fit a pooled block) it delegates to the global allocator, matching make_shared. /// that does not fit a pooled block) it delegates to the global allocator, matching make_shared.
template <class T> struct PoolAllocator template<class T>
{ struct PoolAllocator
{
using value_type = T; using value_type = T;
PoolAllocator() noexcept = default; PoolAllocator() noexcept = default;
template <class U> PoolAllocator(const PoolAllocator<U>&) noexcept {} template<class U>
PoolAllocator(const PoolAllocator<U>&) noexcept
{
}
T* allocate(std::size_t n) T* allocate(std::size_t n)
{ {
@@ -366,7 +385,15 @@ namespace hl
#endif #endif
} }
template <class U> bool operator==(const PoolAllocator<U>&) const noexcept { return true; } template<class U>
template <class U> bool operator!=(const PoolAllocator<U>&) const noexcept { return false; } bool operator==(const PoolAllocator<U>&) const noexcept
}; {
return true;
}
template<class U>
bool operator!=(const PoolAllocator<U>&) const noexcept
{
return false;
}
};
} // namespace hl } // namespace hl
+59 -57
View File
@@ -4,8 +4,8 @@
#include <algorithm> #include <algorithm>
#ifdef TARGET_WIN #ifdef TARGET_WIN
# define popen _popen #define popen _popen
# define pclose _pclose #define pclose _pclose
#endif #endif
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
@@ -22,38 +22,38 @@ std::string OsProcess::execCommand(const std::string& cmd)
std::string output; std::string output;
HANDLE hPipeRead, hPipeWrite; HANDLE hPipeRead, hPipeWrite;
SECURITY_ATTRIBUTES saAttr = { sizeof(SECURITY_ATTRIBUTES) }; SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES)};
saAttr.bInheritHandle = TRUE; //Pipe handles are inherited by child process. saAttr.bInheritHandle = TRUE; // Pipe handles are inherited by child process.
saAttr.lpSecurityDescriptor = NULL; saAttr.lpSecurityDescriptor = NULL;
// Create a pipe to get results from child's stdout. // Create a pipe to get results from child's stdout.
if ( !CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0) ) if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
return output; return output;
STARTUPINFOA si = { sizeof(STARTUPINFOA) }; STARTUPINFOA si = {sizeof(STARTUPINFOA)};
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = hPipeWrite; si.hStdOutput = hPipeWrite;
si.hStdError = hPipeWrite; si.hStdError = hPipeWrite;
si.wShowWindow = SW_HIDE; // Prevents cmd window from flashing. Requires STARTF_USESHOWWINDOW in dwFlags. si.wShowWindow = SW_HIDE; // Prevents cmd window from flashing. Requires STARTF_USESHOWWINDOW in dwFlags.
PROCESS_INFORMATION pi = { 0 }; PROCESS_INFORMATION pi = {0};
char* cmdline = (char*)_alloca(cmd.size()+1); char* cmdline = (char*)_alloca(cmd.size() + 1);
strcpy(cmdline, strx::replace(cmd, "/", "\\").c_str()); strcpy(cmdline, strx::replace(cmd, "/", "\\").c_str());
BOOL fSuccess = CreateProcessA( nullptr, cmdline, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); BOOL fSuccess = CreateProcessA(nullptr, cmdline, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
if (! fSuccess) if (!fSuccess)
{ {
CloseHandle( hPipeWrite ); CloseHandle(hPipeWrite);
CloseHandle( hPipeRead ); CloseHandle(hPipeRead);
return output; return output;
} }
bool bProcessEnded = false; bool bProcessEnded = false;
for (; !bProcessEnded ;) for (; !bProcessEnded;)
{ {
// Give some timeslice (50ms), so we won't waste 100% cpu. // Give some timeslice (50ms), so we won't waste 100% cpu.
bProcessEnded = WaitForSingleObject( pi.hProcess, 50) == WAIT_OBJECT_0; bProcessEnded = WaitForSingleObject(pi.hProcess, 50) == WAIT_OBJECT_0;
// Even if process exited - we continue reading, if there is some data available over pipe. // Even if process exited - we continue reading, if there is some data available over pipe.
for (;;) for (;;)
@@ -75,35 +75,35 @@ std::string OsProcess::execCommand(const std::string& cmd)
buf[dwRead] = 0; buf[dwRead] = 0;
output += buf; output += buf;
} }
} //for } // for
CloseHandle( hPipeWrite ); CloseHandle(hPipeWrite);
CloseHandle( hPipeRead ); CloseHandle(hPipeRead);
CloseHandle( pi.hProcess ); CloseHandle(pi.hProcess);
CloseHandle( pi.hThread ); CloseHandle(pi.hThread);
return output; return output;
} }
std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdline, std::shared_ptr<std::thread>
std::function<void(const std::string& line)> callback, OsProcess::asyncExecCommand(const std::string& cmdline, std::function<void(const std::string& line)> callback,
std::function<void(const std::string& reason)> finished_callback, std::function<void(const std::string& reason)> finished_callback, bool& finish_flag)
bool& finish_flag)
{ {
// std::cout << cmdline << std::endl; // std::cout << cmdline << std::endl;
std::string output; std::string output;
HANDLE hPipeRead, hPipeWrite; HANDLE hPipeRead, hPipeWrite;
SECURITY_ATTRIBUTES saAttr = { sizeof(SECURITY_ATTRIBUTES), nullptr, FALSE }; SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES), nullptr, FALSE};
saAttr.bInheritHandle = TRUE; //Pipe handles are inherited by child process. saAttr.bInheritHandle = TRUE; // Pipe handles are inherited by child process.
saAttr.lpSecurityDescriptor = nullptr; saAttr.lpSecurityDescriptor = nullptr;
// Create a pipe to get results from child's stdout. // Create a pipe to get results from child's stdout.
if ( !CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0) ) if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
return std::shared_ptr<std::thread>(); return std::shared_ptr<std::thread>();
STARTUPINFOA si; memset(&si, 0, sizeof si); STARTUPINFOA si;
memset(&si, 0, sizeof si);
si.cb = sizeof(STARTUPINFOA); si.cb = sizeof(STARTUPINFOA);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = hPipeWrite; si.hStdOutput = hPipeWrite;
@@ -113,27 +113,28 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof pi); memset(&pi, 0, sizeof pi);
char* cmdbuffer = (char*)_alloca(cmdline.size()+1); char* cmdbuffer = (char*)_alloca(cmdline.size() + 1);
strcpy(cmdbuffer, strx::replace(cmdline, "/", "\\").c_str()); strcpy(cmdbuffer, strx::replace(cmdline, "/", "\\").c_str());
BOOL fSuccess = CreateProcessA( nullptr, cmdbuffer, nullptr, nullptr, TRUE, BOOL fSuccess =
CREATE_NEW_CONSOLE, nullptr, nullptr, &si, &pi); CreateProcessA(nullptr, cmdbuffer, nullptr, nullptr, TRUE, CREATE_NEW_CONSOLE, nullptr, nullptr, &si, &pi);
if (! fSuccess) if (!fSuccess)
{ {
CloseHandle( hPipeWrite ); CloseHandle(hPipeWrite);
CloseHandle( hPipeRead ); CloseHandle(hPipeRead);
return std::shared_ptr<std::thread>(); return std::shared_ptr<std::thread>();
} }
std::shared_ptr<std::thread> r = std::make_shared<std::thread>( std::shared_ptr<std::thread> r = std::make_shared<std::thread>(
[&finish_flag, pi, callback, finished_callback, hPipeRead, hPipeWrite]() [&finish_flag, pi, callback, finished_callback, hPipeRead, hPipeWrite]()
{ {
char buf[4096]; memset(buf, 0, sizeof buf); char buf[4096];
for (; !finish_flag ;) memset(buf, 0, sizeof buf);
for (; !finish_flag;)
{ {
// Give some timeslice (50ms), so we won't waste 100% cpu. // Give some timeslice (50ms), so we won't waste 100% cpu.
bool timeouted = WaitForSingleObject( pi.hProcess, 50) == WAIT_OBJECT_0; bool timeouted = WaitForSingleObject(pi.hProcess, 50) == WAIT_OBJECT_0;
// Even if process exited - we continue reading, if there is some data available over pipe. // Even if process exited - we continue reading, if there is some data available over pipe.
for (;;) for (;;)
@@ -148,7 +149,9 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
break; break;
int filled = strlen(buf); int filled = strlen(buf);
if (!::ReadFile(hPipeRead, buf + filled, std::min(sizeof(buf) - 1 - filled, (size_t)dwAvail), &dwRead, nullptr) || !dwRead) if (!::ReadFile(hPipeRead, buf + filled, std::min(sizeof(buf) - 1 - filled, (size_t)dwAvail),
&dwRead, nullptr) ||
!dwRead)
// error, the child process might ended // error, the child process might ended
break; break;
@@ -158,33 +161,33 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
const char* cr; const char* cr;
while ((cr = strchr(buf, '\n')) != nullptr) while ((cr = strchr(buf, '\n')) != nullptr)
{ {
std::string line(buf, cr - buf -1); std::string line(buf, cr - buf - 1);
if (callback) if (callback)
callback(strx::trim(line)); callback(strx::trim(line));
memmove(buf, cr + 1, strlen(cr+1) + 1); memmove(buf, cr + 1, strlen(cr + 1) + 1);
} }
} }
} //for } // for
if (buf[0]) if (buf[0])
callback(strx::trim(std::string(buf))); callback(strx::trim(std::string(buf)));
char ctrlc = 3; char ctrlc = 3;
//if (finish_flag) // if (finish_flag)
// ::WriteFile(hPipeWrite, &ctrlc, 1, nullptr, nullptr); // ::WriteFile(hPipeWrite, &ctrlc, 1, nullptr, nullptr);
// GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pi.dwProcessId); // GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pi.dwProcessId);
CloseHandle( hPipeWrite ); CloseHandle(hPipeWrite);
CloseHandle( hPipeRead ); CloseHandle(hPipeRead);
if (finish_flag) if (finish_flag)
{ {
//GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); // GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
// Close underlying process // Close underlying process
//TerminateProcess(pi.hProcess, 3); // TerminateProcess(pi.hProcess, 3);
} }
CloseHandle( pi.hProcess ); CloseHandle(pi.hProcess);
CloseHandle( pi.hThread ); CloseHandle(pi.hThread);
if (finished_callback) if (finished_callback)
finished_callback(std::string()); finished_callback(std::string());
}); });
@@ -229,12 +232,12 @@ int OsProcess::execSystem(const std::string& cmd)
#include "helper/HL_String.h" #include "helper/HL_String.h"
#include "helper/HL_Sync.h" #include "helper/HL_Sync.h"
std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdline, std::shared_ptr<std::thread>
std::function<void(const std::string& line)> line_callback, OsProcess::asyncExecCommand(const std::string& cmdline, std::function<void(const std::string& line)> line_callback,
std::function<void(const std::string& reason)> finished_callback, std::function<void(const std::string& reason)> finished_callback, bool& finish_flag)
bool& finish_flag)
{ {
std::shared_ptr<std::thread> t = std::make_shared<std::thread>([cmdline, line_callback, finished_callback, &finish_flag]() std::shared_ptr<std::thread> t = std::make_shared<std::thread>(
[cmdline, line_callback, finished_callback, &finish_flag]()
{ {
ThreadHelper::setName("OsProcess::asyncExecCommand"); ThreadHelper::setName("OsProcess::asyncExecCommand");
std::string cp = cmdline; std::string cp = cmdline;
@@ -257,7 +260,7 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
while (!feof(pipe) && !finish_flag) while (!feof(pipe) && !finish_flag)
{ {
// Wait for more data // Wait for more data
struct pollfd pfd{ .fd = fno, .events = POLLIN }; struct pollfd pfd{.fd = fno, .events = POLLIN};
while (poll(&pfd, 1, 0) == 0 && !finish_flag) while (poll(&pfd, 1, 0) == 0 && !finish_flag)
; ;
@@ -275,8 +278,7 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
buffer[r] = 0; buffer[r] = 0;
lines += std::string(buffer); lines += std::string(buffer);
} }
} } while (r == sizeof(buffer) - 1);
while (r == sizeof(buffer) - 1);
if (lines.find('\n') != std::string::npos && line_callback) if (lines.find('\n') != std::string::npos && line_callback)
{ {
@@ -287,7 +289,7 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
if (d != std::string::npos) if (d != std::string::npos)
{ {
if (line_callback) if (line_callback)
line_callback(strx::trim(lines.substr(p, d-p))); line_callback(strx::trim(lines.substr(p, d - p)));
p = d + 1; p = d + 1;
} }
} }
@@ -322,7 +324,7 @@ pid_t OsProcess::findPid(const std::string& cmdline)
std::string output = execCommand(oss.str()); std::string output = execCommand(oss.str());
return std::atoi(output.c_str()); return std::atoi(output.c_str());
} }
catch(...) catch (...)
{ {
return 0; return 0;
} }
+3 -5
View File
@@ -11,15 +11,13 @@ class OsProcess
public: public:
static std::string execCommand(const std::string& cmdline); static std::string execCommand(const std::string& cmdline);
static int execSystem(const std::string& cmdline); static int execSystem(const std::string& cmdline);
static std::shared_ptr<std::thread> asyncExecCommand(const std::string& cmdline, static std::shared_ptr<std::thread>
std::function<void(const std::string& line)> line_callback, asyncExecCommand(const std::string& cmdline, std::function<void(const std::string& line)> line_callback,
std::function<void(const std::string& reason)> finished_callback, std::function<void(const std::string& reason)> finished_callback, bool& finish_flag);
bool& finish_flag);
#if defined(TARGET_OSX) || defined(TARGET_LINUX) #if defined(TARGET_OSX) || defined(TARGET_LINUX)
static pid_t findPid(const std::string& cmdline); static pid_t findPid(const std::string& cmdline);
static void killByPid(pid_t pid); static void killByPid(pid_t pid);
#endif #endif
}; };
#endif #endif
+52 -41
View File
@@ -4,12 +4,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
# include <WinSock2.h> #include <WinSock2.h>
# include <Windows.h> #include <Windows.h>
#endif #endif
#if defined(TARGET_LINUX) || defined(TARGET_ANDROID) || defined(TARGET_OSX) #if defined(TARGET_LINUX) || defined(TARGET_ANDROID) || defined(TARGET_OSX)
# include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#include "HL_Rtp.h" #include "HL_Rtp.h"
@@ -33,12 +33,12 @@ static const char RTPDUMP_SHEBANG[] = "#!rtpplay1.0";
// RTP fixed header (little-endian bit-field layout) // RTP fixed header (little-endian bit-field layout)
struct RtpHeader struct RtpHeader
{ {
unsigned char cc:4; /* CSRC count */ unsigned char cc : 4; /* CSRC count */
unsigned char x:1; /* header extension flag */ unsigned char x : 1; /* header extension flag */
unsigned char p:1; /* padding flag */ unsigned char p : 1; /* padding flag */
unsigned char version:2; /* protocol version */ unsigned char version : 2; /* protocol version */
unsigned char pt:7; /* payload type */ unsigned char pt : 7; /* payload type */
unsigned char m:1; /* marker bit */ unsigned char m : 1; /* marker bit */
unsigned short seq; /* sequence number */ unsigned short seq; /* sequence number */
unsigned int ts; /* timestamp */ unsigned int ts; /* timestamp */
unsigned int ssrc; /* synchronization source */ unsigned int ssrc; /* synchronization source */
@@ -46,9 +46,9 @@ struct RtpHeader
struct RtcpHeader struct RtcpHeader
{ {
unsigned char rc:5; /* reception report count */ unsigned char rc : 5; /* reception report count */
unsigned char p:1; /* padding flag */ unsigned char p : 1; /* padding flag */
unsigned char version:2; /* protocol version */ unsigned char version : 2; /* protocol version */
unsigned char pt; /* payload type */ unsigned char pt; /* payload type */
uint16_t len; /* length */ uint16_t len; /* length */
uint32_t ssrc; /* synchronization source */ uint32_t ssrc; /* synchronization source */
@@ -59,10 +59,8 @@ struct RtcpHeader
static std::string ipToString(uint32_t ip) static std::string ipToString(uint32_t ip)
{ {
// ip in host byte order → dotted-decimal // ip in host byte order → dotted-decimal
return std::to_string((ip >> 24) & 0xFF) + "." + return std::to_string((ip >> 24) & 0xFF) + "." + std::to_string((ip >> 16) & 0xFF) + "." +
std::to_string((ip >> 16) & 0xFF) + "." + std::to_string((ip >> 8) & 0xFF) + "." + std::to_string(ip & 0xFF);
std::to_string((ip >> 8) & 0xFF) + "." +
std::to_string( ip & 0xFF);
} }
static uint32_t stringToIp(const std::string& s) static uint32_t stringToIp(const std::string& s)
@@ -130,7 +128,7 @@ int RtpHelper::findPtype(const void* buffer, size_t length)
return -1; return -1;
} }
int RtpHelper::findPacketNo(const void *buffer, size_t length) int RtpHelper::findPacketNo(const void* buffer, size_t length)
{ {
if (isRtp(buffer, length)) if (isRtp(buffer, length))
return ntohs(reinterpret_cast<const RtpHeader*>(buffer)->seq); return ntohs(reinterpret_cast<const RtpHeader*>(buffer)->seq);
@@ -152,7 +150,8 @@ int RtpHelper::findPayloadLength(const void* buffer, size_t length)
return -1; return -1;
// Header extension // Header extension
if (h->x) { if (h->x)
{
if (offset + 4 > length) if (offset + 4 > length)
return -1; return -1;
uint16_t extWords = (static_cast<uint16_t>(p[offset + 2]) << 8) | p[offset + 3]; uint16_t extWords = (static_cast<uint16_t>(p[offset + 2]) << 8) | p[offset + 3];
@@ -164,7 +163,8 @@ int RtpHelper::findPayloadLength(const void* buffer, size_t length)
size_t payloadLen = length - offset; size_t payloadLen = length - offset;
// Padding // Padding
if (h->p && payloadLen > 0) { if (h->p && payloadLen > 0)
{
uint8_t padBytes = p[length - 1]; uint8_t padBytes = p[length - 1];
if (padBytes > payloadLen) if (padBytes > payloadLen)
return -1; return -1;
@@ -186,7 +186,8 @@ std::shared_ptr<jrtplib::RTPPacket> RtpDump::parseRtpData(const uint8_t* data, s
if (!data || len < 12 || !RtpHelper::isRtp(data, len)) if (!data || len < 12 || !RtpHelper::isRtp(data, len))
return nullptr; return nullptr;
try { try
{
// Both are heap-allocated; RTPRawPacket takes ownership and deletes them // Both are heap-allocated; RTPRawPacket takes ownership and deletes them
jrtplib::RTPIPAddress senderAddress = {jrtplib::RTPIPv4Address(uint32_t(0), uint16_t(0))}; jrtplib::RTPIPAddress senderAddress = {jrtplib::RTPIPv4Address(uint32_t(0), uint16_t(0))};
uint8_t* dataCopy = new uint8_t[len]; uint8_t* dataCopy = new uint8_t[len];
@@ -199,16 +200,15 @@ std::shared_ptr<jrtplib::RTPPacket> RtpDump::parseRtpData(const uint8_t* data, s
return nullptr; return nullptr;
return packet; return packet;
} catch (const std::exception& e) { }
catch (const std::exception& e)
{
ICELogInfo(<< "Failed to parse RTP packet: " << e.what()); ICELogInfo(<< "Failed to parse RTP packet: " << e.what());
return nullptr; return nullptr;
} }
} }
RtpDump::RtpDump(const char* filename) RtpDump::RtpDump(const char* filename) : mFilename(filename ? filename : "") {}
: mFilename(filename ? filename : "")
{
}
RtpDump::~RtpDump() = default; RtpDump::~RtpDump() = default;
@@ -237,14 +237,19 @@ void RtpDump::load()
// Parse source address from the text line // Parse source address from the text line
size_t spacePos = textLine.find(' '); size_t spacePos = textLine.find(' ');
if (spacePos != std::string::npos) { if (spacePos != std::string::npos)
{
std::string addrPart = textLine.substr(spacePos + 1); std::string addrPart = textLine.substr(spacePos + 1);
size_t slashPos = addrPart.find('/'); size_t slashPos = addrPart.find('/');
if (slashPos != std::string::npos) { if (slashPos != std::string::npos)
{
mSourceIp = stringToIp(addrPart.substr(0, slashPos)); mSourceIp = stringToIp(addrPart.substr(0, slashPos));
try { try
{
mSourcePort = static_cast<uint16_t>(std::stoi(addrPart.substr(slashPos + 1))); mSourcePort = static_cast<uint16_t>(std::stoi(addrPart.substr(slashPos + 1)));
} catch (...) { }
catch (...)
{
mSourcePort = 0; mSourcePort = 0;
} }
} }
@@ -275,21 +280,25 @@ void RtpDump::load()
// --- 3. Packet records --- // --- 3. Packet records ---
size_t packetCount = 0; size_t packetCount = 0;
while (input.good() && input.peek() != EOF) { while (input.good() && input.peek() != EOF)
{
// Packet header: length(2) + plen(2) + offset(4) = 8 bytes // Packet header: length(2) + plen(2) + offset(4) = 8 bytes
uint16_t recLength, plen; uint16_t recLength, plen;
uint32_t offsetMs; uint32_t offsetMs;
input.read(reinterpret_cast<char*>(&recLength), 2); input.read(reinterpret_cast<char*>(&recLength), 2);
if (input.gcount() != 2) break; if (input.gcount() != 2)
break;
recLength = ntohs(recLength); recLength = ntohs(recLength);
input.read(reinterpret_cast<char*>(&plen), 2); input.read(reinterpret_cast<char*>(&plen), 2);
if (input.gcount() != 2) break; if (input.gcount() != 2)
break;
plen = ntohs(plen); plen = ntohs(plen);
input.read(reinterpret_cast<char*>(&offsetMs), 4); input.read(reinterpret_cast<char*>(&offsetMs), 4);
if (input.gcount() != 4) break; if (input.gcount() != 4)
break;
offsetMs = ntohl(offsetMs); offsetMs = ntohl(offsetMs);
// All-zeros record signals end of file in some implementations // All-zeros record signals end of file in some implementations
@@ -367,7 +376,8 @@ void RtpDump::add(const void* buffer, size_t len)
uint32_t offsetMs = 0; uint32_t offsetMs = 0;
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
if (!mRecording) { if (!mRecording)
{
mRecording = true; mRecording = true;
mRecordStart = now; mRecordStart = now;
@@ -378,7 +388,9 @@ void RtpDump::add(const void* buffer, size_t len)
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(epoch - sec); auto usec = std::chrono::duration_cast<std::chrono::microseconds>(epoch - sec);
mStartSec = static_cast<uint32_t>(sec.count()); mStartSec = static_cast<uint32_t>(sec.count());
mStartUsec = static_cast<uint32_t>(usec.count()); mStartUsec = static_cast<uint32_t>(usec.count());
} else { }
else
{
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - mRecordStart); auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - mRecordStart);
offsetMs = static_cast<uint32_t>(elapsed.count()); offsetMs = static_cast<uint32_t>(elapsed.count());
} }
@@ -396,8 +408,7 @@ void RtpDump::add(const void* buffer, size_t len, uint32_t offsetMs)
throw std::runtime_error("Packet too large: " + std::to_string(len)); throw std::runtime_error("Packet too large: " + std::to_string(len));
RtpData entry; RtpData entry;
entry.mRawData.assign(static_cast<const uint8_t*>(buffer), entry.mRawData.assign(static_cast<const uint8_t*>(buffer), static_cast<const uint8_t*>(buffer) + len);
static_cast<const uint8_t*>(buffer) + len);
entry.mOffsetMs = offsetMs; entry.mOffsetMs = offsetMs;
entry.mPacket = parseRtpData(entry.mRawData.data(), entry.mRawData.size()); entry.mPacket = parseRtpData(entry.mRawData.data(), entry.mRawData.size());
@@ -414,9 +425,8 @@ void RtpDump::flush()
throw std::runtime_error("Failed to open file for writing: " + mFilename); throw std::runtime_error("Failed to open file for writing: " + mFilename);
// --- 1. Text header --- // --- 1. Text header ---
std::string textLine = std::string(RTPDUMP_SHEBANG) + " " + std::string textLine =
ipToString(mSourceIp) + "/" + std::string(RTPDUMP_SHEBANG) + " " + ipToString(mSourceIp) + "/" + std::to_string(mSourcePort) + "\n";
std::to_string(mSourcePort) + "\n";
output.write(textLine.data(), static_cast<std::streamsize>(textLine.size())); output.write(textLine.data(), static_cast<std::streamsize>(textLine.size()));
// --- 2. Binary file header (16 bytes) --- // --- 2. Binary file header (16 bytes) ---
@@ -441,7 +451,8 @@ void RtpDump::flush()
// --- 3. Packet records --- // --- 3. Packet records ---
size_t written = 0; size_t written = 0;
for (const auto& pkt : mPacketList) { for (const auto& pkt : mPacketList)
{
if (pkt.mRawData.empty()) if (pkt.mRawData.empty())
continue; continue;
+3 -6
View File
@@ -22,12 +22,9 @@ struct RtpPair
T mRtp; T mRtp;
T mRtcp; T mRtcp;
RtpPair() RtpPair() {}
{}
RtpPair(const T& rtp, const T& rtcp) RtpPair(const T& rtp, const T& rtcp) : mRtp(rtp), mRtcp(rtcp) {}
:mRtp(rtp), mRtcp(rtcp)
{}
bool multiplexed() const { return mRtp == mRtcp; } bool multiplexed() const { return mRtp == mRtcp; }
}; };
@@ -37,7 +34,7 @@ class RtpHelper
public: public:
static bool isRtp(const void* buffer, size_t length); static bool isRtp(const void* buffer, size_t length);
static int findPtype(const void* buffer, size_t length); static int findPtype(const void* buffer, size_t length);
static int findPacketNo(const void *buffer, size_t length); static int findPacketNo(const void* buffer, size_t length);
static bool isRtpOrRtcp(const void* buffer, size_t length); static bool isRtpOrRtcp(const void* buffer, size_t length);
static bool isRtcp(const void* buffer, size_t length); static bool isRtcp(const void* buffer, size_t length);
static unsigned findSsrc(const void* buffer, size_t length); static unsigned findSsrc(const void* buffer, size_t length);
+4 -3
View File
@@ -4,12 +4,13 @@
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
template <class T> template<class T>
class SafeSingleton class SafeSingleton
{ {
protected: protected:
static std::atomic<T*> SharedInstance; static std::atomic<T*> SharedInstance;
static std::mutex mMutex; static std::mutex mMutex;
public: public:
static T& instance() static T& instance()
{ {
@@ -50,9 +51,9 @@ public:
} }
}; };
template <class T> template<class T>
std::atomic<T*> SafeSingleton<T>::SharedInstance; std::atomic<T*> SafeSingleton<T>::SharedInstance;
template <class T> template<class T>
std::mutex SafeSingleton<T>::mMutex; std::mutex SafeSingleton<T>::mMutex;
#endif #endif
+17 -18
View File
@@ -12,7 +12,7 @@
#include <assert.h> #include <assert.h>
#include <chrono> #include <chrono>
#if !defined(TARGET_WIN) #if !defined(TARGET_WIN)
# include <unistd.h> // Responsible for close() call on Linux #include <unistd.h> // Responsible for close() call on Linux
#endif #endif
#include "HL_SocketHeap.h" #include "HL_SocketHeap.h"
@@ -31,8 +31,7 @@
using namespace std::chrono_literals; using namespace std::chrono_literals;
// ----------------------------- SocketSink ------------------------- // ----------------------------- SocketSink -------------------------
SocketSink::~SocketSink() SocketSink::~SocketSink() {}
{}
// ----------------------------- SocketHeap ------------------------- // ----------------------------- SocketHeap -------------------------
@@ -74,7 +73,7 @@ void SocketHeap::setRange(unsigned short start, unsigned short finish)
mFinish = finish; mFinish = finish;
} }
void SocketHeap::range(unsigned short &start, unsigned short &finish) void SocketHeap::range(unsigned short& start, unsigned short& finish)
{ {
Lock l(mGuard); Lock l(mGuard);
@@ -82,10 +81,10 @@ void SocketHeap::range(unsigned short &start, unsigned short &finish)
finish = mFinish; finish = mFinish;
} }
RtpPair<PDatagramSocket> SocketHeap::allocSocketPair(int family, SocketSink *sink, Multiplex m) RtpPair<PDatagramSocket> SocketHeap::allocSocketPair(int family, SocketSink* sink, Multiplex m)
{ {
PDatagramSocket rtp, rtcp; PDatagramSocket rtp, rtcp;
for (int attempt=0; (!rtp || !rtcp) && attempt < (mFinish - mStart)/2; attempt++) for (int attempt = 0; (!rtp || !rtcp) && attempt < (mFinish - mStart) / 2; attempt++)
{ {
// Allocate RTP // Allocate RTP
try try
@@ -96,7 +95,7 @@ RtpPair<PDatagramSocket> SocketHeap::allocSocketPair(int family, SocketSink *sin
else else
rtcp = allocSocket(family, sink, rtp->localport() + 1); rtcp = allocSocket(family, sink, rtp->localport() + 1);
} }
catch(...) catch (...)
{ {
// Release a partially allocated pair before retrying - otherwise // Release a partially allocated pair before retrying - otherwise
// the RTP socket from this attempt leaks into the socket map. // the RTP socket from this attempt leaks into the socket map.
@@ -121,14 +120,13 @@ RtpPair<PDatagramSocket> SocketHeap::allocSocketPair(int family, SocketSink *sin
freeSocket(rtcp); freeSocket(rtcp);
throw Exception(ERR_NET_FAILED); throw Exception(ERR_NET_FAILED);
} }
ICELogInfo(<< "Allocated socket pair " << (family == AF_INET ? "AF_INET" : "AF_INET6") << " " ICELogInfo(<< "Allocated socket pair " << (family == AF_INET ? "AF_INET" : "AF_INET6") << " " << rtp->socket()
<< rtp->socket() << ":" << rtcp->socket() << ":" << rtcp->socket() << " at ports " << rtp->localport() << ":" << rtcp->localport());
<< " at ports " << rtp->localport() << ":"<< rtcp->localport());
return RtpPair<PDatagramSocket>(rtp, rtcp); return RtpPair<PDatagramSocket>(rtp, rtcp);
} }
void SocketHeap::freeSocketPair(const RtpPair<PDatagramSocket> &p) void SocketHeap::freeSocketPair(const RtpPair<PDatagramSocket>& p)
{ {
freeSocket(p.mRtp); freeSocket(p.mRtp);
freeSocket(p.mRtcp); freeSocket(p.mRtcp);
@@ -257,10 +255,11 @@ void SocketHeap::thread()
} }
// Update socket set // Update socket set
for (auto& socketIter: mSocketMap) for (auto& socketIter : mSocketMap)
agreggator.addSocket(socketIter.second.mSocket); agreggator.addSocket(socketIter.second.mSocket);
/* for (SocketMap::iterator socketIter = mSocketMap.begin(); socketIter != mSocketMap.end(); ++socketIter) /* for (SocketMap::iterator socketIter = mSocketMap.begin(); socketIter != mSocketMap.end();
++socketIter)
{ {
// Add handle to set // Add handle to set
agreggator.addSocket(socketIter->second.mSocket); agreggator.addSocket(socketIter->second.mSocket);
@@ -278,11 +277,11 @@ void SocketHeap::thread()
// Remove deleted sockets to avoid call non-existant sinks // Remove deleted sockets to avoid call non-existant sinks
processDeleted(); processDeleted();
for (unsigned i=0; i<agreggator.count(); i++) for (unsigned i = 0; i < agreggator.count(); i++)
{ {
if (agreggator.hasDataAtIndex(i)) if (agreggator.hasDataAtIndex(i))
{ {
//ICELogInfo(<<"Got incoming UDP packet at index " << (const int)i); // ICELogInfo(<<"Got incoming UDP packet at index " << (const int)i);
PDatagramSocket sock = agreggator.socketAt(i); PDatagramSocket sock = agreggator.socketAt(i);
// Find corresponding data sink // Find corresponding data sink
@@ -293,14 +292,14 @@ void SocketHeap::thread()
InternetAddress src; InternetAddress src;
unsigned received = sock->recvDatagram(src, mTempPacket, sizeof mTempPacket); unsigned received = sock->recvDatagram(src, mTempPacket, sizeof mTempPacket);
if ( received > 0 && received <= MAX_VALID_UDPPACKET_SIZE) if (received > 0 && received <= MAX_VALID_UDPPACKET_SIZE)
socketItemIter->second.mSink->onReceivedData(sock, src, mTempPacket, received); socketItemIter->second.mSink->onReceivedData(sock, src, mTempPacket, received);
} }
// There is a call to ProcessDeleted() as OnReceivedData() could delete sockets // There is a call to ProcessDeleted() as OnReceivedData() could delete sockets
processDeleted(); processDeleted();
} }
} //of for } // of for
} }
} }
else else
@@ -308,7 +307,7 @@ void SocketHeap::thread()
} }
mShutdown = false; mShutdown = false;
//#endif // #endif
} }
+8 -15
View File
@@ -22,7 +22,8 @@ class SocketSink
{ {
public: public:
virtual ~SocketSink(); virtual ~SocketSink();
virtual void onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr, unsigned receivedSize) = 0; virtual void onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr,
unsigned receivedSize) = 0;
}; };
// Class allocates new UDP sockets and tracks incoming packets on them. It runs in separate thread // Class allocates new UDP sockets and tracks incoming packets on them. It runs in separate thread
@@ -49,7 +50,8 @@ public:
// Returns used port number range // Returns used port number range
void range(unsigned short& start, unsigned short& finish); void range(unsigned short& start, unsigned short& finish);
// Attempts to allocate and return socket + allocated port number. REQUIRES pointer to data sink - it will be used to process incoming datagrams // Attempts to allocate and return socket + allocated port number. REQUIRES pointer to data sink - it will be used
// to process incoming datagrams
PDatagramSocket allocSocket(int family, SocketSink* sink, int port = 0); PDatagramSocket allocSocket(int family, SocketSink* sink, int port = 0);
RtpPair<PDatagramSocket> allocSocketPair(int family, SocketSink* sink, Multiplex m); RtpPair<PDatagramSocket> allocSocketPair(int family, SocketSink* sink, Multiplex m);
@@ -69,18 +71,11 @@ protected:
// Data sink pointer // Data sink pointer
SocketSink* mSink; SocketSink* mSink;
SocketItem() SocketItem() : mSink(nullptr) {}
:mSink(nullptr)
{ }
SocketItem(unsigned short portnumber, SocketSink* sink) SocketItem(unsigned short portnumber, SocketSink* sink) : mSink(sink) { mSocket->mLocalPort = portnumber; }
:mSink(sink)
{
mSocket->mLocalPort = portnumber;
}
~SocketItem() ~SocketItem() {}
{ }
}; };
typedef std::map<SOCKET, SocketItem> SocketMap; typedef std::map<SOCKET, SocketItem> SocketMap;
@@ -90,8 +85,7 @@ protected:
Mutex mGuard; Mutex mGuard;
SocketMap mSocketMap; SocketMap mSocketMap;
PortVector mPortVector; PortVector mPortVector;
unsigned short mStart, unsigned short mStart, mFinish;
mFinish;
SocketVector mDeleteVector; SocketVector mDeleteVector;
Mutex mDeleteGuard; Mutex mDeleteGuard;
@@ -107,7 +101,6 @@ protected:
// Processes mDeleteVector -> updates mSocketMap, removes socket items and closes sockets specified in mDeleteVector // Processes mDeleteVector -> updates mSocketMap, removes socket items and closes sockets specified in mDeleteVector
void processDeleted(); void processDeleted();
}; };
#endif #endif
+6 -21
View File
@@ -13,10 +13,7 @@ struct Average
return mSum / mCount; return mSum / mCount;
} }
T value() const T value() const { return average(); }
{
return average();
}
void process(T value) void process(T value)
{ {
@@ -43,10 +40,7 @@ struct TestResult
mAverage.process(value); mAverage.process(value);
} }
bool is_initialized() const bool is_initialized() const { return mAverage.mCount > 0; }
{
return mAverage.mCount > 0;
}
T current() const T current() const
{ {
@@ -56,26 +50,17 @@ struct TestResult
return 0; return 0;
} }
T value() const T value() const { return current(); }
{
return current();
}
T average() const T average() const { return mAverage.average(); }
{
return mAverage.average();
}
TestResult<T>& operator = (T value) TestResult<T>& operator=(T value)
{ {
process(value); process(value);
return *this; return *this;
} }
operator T() operator T() { return mCurrent; }
{
return mCurrent;
}
}; };
-1
View File
@@ -20,5 +20,4 @@ enum class StreamState
}; };
#endif #endif
+47 -47
View File
@@ -11,9 +11,9 @@
#include <inttypes.h> #include <inttypes.h>
#ifdef TARGET_WIN #ifdef TARGET_WIN
# include <WinSock2.h> #include <WinSock2.h>
# include <Windows.h> #include <Windows.h>
# include <cctype> #include <cctype>
#endif #endif
std::string strx::extractFilename(const std::string& path) std::string strx::extractFilename(const std::string& path)
@@ -45,7 +45,7 @@ std::string strx::appendPath(const std::string& s1, const std::string& s2)
return result + s2; return result + s2;
} }
std::string strx::makeUtf8(const std::tstring &arg) std::string strx::makeUtf8(const std::tstring& arg)
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
int required = WideCharToMultiByte(CP_UTF8, 0, arg.c_str(), -1, NULL, 0, NULL, NULL); int required = WideCharToMultiByte(CP_UTF8, 0, arg.c_str(), -1, NULL, 0, NULL, NULL);
@@ -60,7 +60,7 @@ std::string strx::makeUtf8(const std::tstring &arg)
#endif #endif
} }
std::string strx::toUtf8(const std::tstring &arg) std::string strx::toUtf8(const std::tstring& arg)
{ {
return makeUtf8(arg); return makeUtf8(arg);
} }
@@ -80,7 +80,7 @@ std::tstring strx::makeTstring(const std::string& arg)
#endif #endif
} }
int strx::toInt(const char *s, int defaultValue, bool* isOk) int strx::toInt(const char* s, int defaultValue, bool* isOk)
{ {
int result; int result;
if (sscanf(s, "%d", &result) != 1) if (sscanf(s, "%d", &result) != 1)
@@ -89,14 +89,13 @@ int strx::toInt(const char *s, int defaultValue, bool* isOk)
*isOk = false; *isOk = false;
result = defaultValue; result = defaultValue;
} }
else else if (isOk)
if (isOk)
*isOk = true; *isOk = true;
return result; return result;
} }
uint64_t strx::toUint64(const char* s, uint64_t def, bool *isOk) uint64_t strx::toUint64(const char* s, uint64_t def, bool* isOk)
{ {
uint64_t result = def; uint64_t result = def;
if (sscanf(s, "%" SCNu64, &result) != 1) if (sscanf(s, "%" SCNu64, &result) != 1)
@@ -105,8 +104,7 @@ uint64_t strx::toUint64(const char* s, uint64_t def, bool *isOk)
*isOk = false; *isOk = false;
result = def; result = def;
} }
else else if (isOk)
if (isOk)
*isOk = true; *isOk = true;
return result; return result;
@@ -119,27 +117,28 @@ std::string strx::toHex(unsigned int value)
return buffer; return buffer;
} }
std::string strx::toHex(const void *ptr) std::string strx::toHex(const void* ptr)
{ {
std::ostringstream oss; std::ostringstream oss;
oss << std::fixed << std::setw(8) << std::setfill('0') << std::hex << ptr; oss << std::fixed << std::setw(8) << std::setfill('0') << std::hex << ptr;
return oss.str(); return oss.str();
} }
//must be lowercase for MD5 // must be lowercase for MD5
static const char hexmap[] = "0123456789abcdef"; static const char hexmap[] = "0123456789abcdef";
std::string strx::toHex(const uint8_t* input, size_t inputLength) std::string strx::toHex(const uint8_t* input, size_t inputLength)
{ {
std::string result; result.resize(inputLength * 2); std::string result;
result.resize(inputLength * 2);
const char* p = (const char*)input; const char* p = (const char*)input;
char* r = &result[0]; char* r = &result[0];
for (size_t i=0; i < inputLength; ++i) for (size_t i = 0; i < inputLength; ++i)
{ {
unsigned char temp = *p++; unsigned char temp = *p++;
int hi = (temp & 0xf0)>>4; int hi = (temp & 0xf0) >> 4;
int low = (temp & 0xf); int low = (temp & 0xf);
*r++ = hexmap[hi]; *r++ = hexmap[hi];
@@ -149,13 +148,13 @@ std::string strx::toHex(const uint8_t* input, size_t inputLength)
return result; return result;
} }
std::string strx::prefixLines(const std::string &source, const std::string &prefix) std::string strx::prefixLines(const std::string& source, const std::string& prefix)
{ {
// Read source line by line // Read source line by line
std::istringstream iss(source); std::istringstream iss(source);
std::ostringstream oss; std::ostringstream oss;
std::string line; std::string line;
while (std::getline(iss,line)) while (std::getline(iss, line))
{ {
oss << prefix << line << std::endl; oss << prefix << line << std::endl;
} }
@@ -194,7 +193,7 @@ void strx::split(const std::string& src, std::vector<std::string>& dst, const st
} }
else else
{ {
std::string t = src.substr(p, f-p); std::string t = src.substr(p, f - p);
if (!t.empty()) if (!t.empty())
dst.push_back(t); dst.push_back(t);
p = f + 1; p = f + 1;
@@ -234,11 +233,11 @@ std::pair<std::string, std::string> strx::parseAssignment(const std::string& s,
if (p != std::string::npos) if (p != std::string::npos)
{ {
result.first = strx::trim(s.substr(0, p)); result.first = strx::trim(s.substr(0, p));
result.second = strx::trim(s.substr(p+1)); result.second = strx::trim(s.substr(p + 1));
if (trimQuotes && result.second.size() >= 2) if (trimQuotes && result.second.size() >= 2)
{ {
if ((result.second[0] == '"' && result.second[result.second.size()-1] == '"') || if ((result.second[0] == '"' && result.second[result.second.size() - 1] == '"') ||
(result.second[0] == '\'' && result.second[result.second.size()-1] == '\'')) (result.second[0] == '\'' && result.second[result.second.size() - 1] == '\''))
result.second = result.second.substr(1, result.second.size() - 2); result.second = result.second.substr(1, result.second.size() - 2);
} }
} }
@@ -255,7 +254,7 @@ std::string strx::intToString(int value)
return buffer; return buffer;
} }
float strx::toFloat(const std::string &s, float v, bool* isOk) float strx::toFloat(const std::string& s, float v, bool* isOk)
{ {
float result = 0.0; float result = 0.0;
int code = sscanf(s.c_str(), "%f", &result); int code = sscanf(s.c_str(), "%f", &result);
@@ -274,11 +273,13 @@ float strx::toFloat(const std::string &s, float v, bool* isOk)
return result; return result;
} }
std::string strx::trim(const std::string &s) std::string strx::trim(const std::string& s)
{ {
auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) { return std::isspace(c) || c == '\r' || c == '\n'; }); auto wsfront =
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) { return std::isspace(c) || c == '\r' || c == '\n'; }).base(); std::find_if_not(s.begin(), s.end(), [](int c) { return std::isspace(c) || c == '\r' || c == '\n'; });
return (wsback <= wsfront ? std::string() : std::string(wsfront,wsback)); auto wsback =
std::find_if_not(s.rbegin(), s.rend(), [](int c) { return std::isspace(c) || c == '\r' || c == '\n'; }).base();
return (wsback <= wsfront ? std::string() : std::string(wsfront, wsback));
} }
std::string strx::timeToString(time_t t) std::string strx::timeToString(time_t t)
@@ -291,16 +292,16 @@ std::string strx::timeToString(time_t t)
#else #else
lt = *localtime(&t); lt = *localtime(&t);
#endif #endif
strftime(buffer, sizeof(buffer)-1, "%Y-%m-%d %H:%M:%S", &lt); strftime(buffer, sizeof(buffer) - 1, "%Y-%m-%d %H:%M:%S", &lt);
return buffer; return buffer;
} }
std::string strx::millisecondsToString(uint64_t t) std::string strx::millisecondsToString(uint64_t t)
{ {
return timeToString(t/1000); return timeToString(t / 1000);
} }
int strx::fromHex2Int(const std::string &s) int strx::fromHex2Int(const std::string& s)
{ {
int result = 0; int result = 0;
int retcode = sscanf(s.c_str(), "%x", &result); int retcode = sscanf(s.c_str(), "%x", &result);
@@ -328,10 +329,11 @@ static int hex2code(char s)
std::string strx::fromHex2String(const std::string& s) std::string strx::fromHex2String(const std::string& s)
{ {
std::string result; result.resize(s.size() / 2); std::string result;
result.resize(s.size() / 2);
const char* t = s.c_str(); const char* t = s.c_str();
for (size_t i = 0; i < result.size(); i++) for (size_t i = 0; i < result.size(); i++)
result[i] = static_cast<char>((hex2code(t[i*2]) << 4) | hex2code(t[i*2+1])); result[i] = static_cast<char>((hex2code(t[i * 2]) << 4) | hex2code(t[i * 2 + 1]));
return result; return result;
} }
@@ -350,7 +352,7 @@ std::string strx::replace(const std::string& s, const std::string& tmpl, const s
{ {
std::string result(s); std::string result(s);
std::string::size_type p = 0; std::string::size_type p = 0;
while ( (p = result.find(tmpl, p)) != std::string::npos) while ((p = result.find(tmpl, p)) != std::string::npos)
{ {
result.replace(p, tmpl.size(), n); result.replace(p, tmpl.size(), n);
p += n.size(); p += n.size();
@@ -367,11 +369,11 @@ std::string strx::decodeUri(const std::string& s)
char ch; char ch;
int i, ii = 0; int i, ii = 0;
for (i=0; i<(int)s.length(); i++) for (i = 0; i < (int)s.length(); i++)
{ {
if (s[i] == '%' && i + 2 < (int)s.length()) if (s[i] == '%' && i + 2 < (int)s.length())
{ {
if (sscanf(s.substr(i+1,2).c_str(), "%x", &ii) == 1) if (sscanf(s.substr(i + 1, 2).c_str(), "%x", &ii) == 1)
{ {
ch = static_cast<char>(ii); ch = static_cast<char>(ii);
ret += ch; ret += ch;
@@ -403,13 +405,13 @@ bool strx::endsWith(const std::string& s, const std::string& suffix)
int strx::stringToDuration(const std::string& s) int strx::stringToDuration(const std::string& s)
{ {
if (endsWith(s, "ms")) if (endsWith(s, "ms"))
return std::stoi(s.substr(0, s.size()-2)); return std::stoi(s.substr(0, s.size() - 2));
if (endsWith(s, "s")) if (endsWith(s, "s"))
return std::stoi(s.substr(0, s.size()-1)) * 1000; return std::stoi(s.substr(0, s.size() - 1)) * 1000;
if (endsWith(s, "m")) if (endsWith(s, "m"))
return std::stoi(s.substr(0, s.size()-1)) * 60000; return std::stoi(s.substr(0, s.size() - 1)) * 60000;
if (endsWith(s, "h")) if (endsWith(s, "h"))
return std::stoi(s.substr(0, s.size()-1)) * 3600 * 1000; return std::stoi(s.substr(0, s.size() - 1)) * 3600 * 1000;
else else
return std::stoi(s) * 1000; return std::stoi(s) * 1000;
} }
@@ -438,7 +440,7 @@ std::string strx::removeQuotes(const std::string& s)
r = r.substr(1); r = r.substr(1);
if (r.back() == '"') if (r.back() == '"')
r = r.substr(0, r.size()-1); r = r.substr(0, r.size() - 1);
return r; return r;
} }
@@ -446,20 +448,18 @@ std::string strx::removeQuotes(const std::string& s)
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
// MSVC++ lacks memmem support // MSVC++ lacks memmem support
const void *memmem(const void *haystack, size_t haystack_len, const void* memmem(const void* haystack, size_t haystack_len, const void* const needle, const size_t needle_len)
const void * const needle, const size_t needle_len)
{ {
if (!haystack || !haystack_len || !needle || !needle_len) if (!haystack || !haystack_len || !needle || !needle_len)
return nullptr; return nullptr;
for (const char *h = (const char*)haystack; for (const char* h = (const char*)haystack; haystack_len >= needle_len; ++h, --haystack_len)
haystack_len >= needle_len; {
++h, --haystack_len) { if (!memcmp(h, needle, needle_len))
if (!memcmp(h, needle, needle_len)) { {
return h; return h;
} }
} }
return nullptr; return nullptr;
} }
#endif #endif
+3 -4
View File
@@ -28,7 +28,7 @@ public:
static std::tstring makeTstring(const std::string& arg); static std::tstring makeTstring(const std::string& arg);
static int toInt(const char* s, int defaultValue, bool* isOk = nullptr); static int toInt(const char* s, int defaultValue, bool* isOk = nullptr);
static uint64_t toUint64(const char* s, uint64_t def, bool *isOk = nullptr); static uint64_t toUint64(const char* s, uint64_t def, bool* isOk = nullptr);
static std::string toHex(unsigned int value); static std::string toHex(unsigned int value);
static std::string toHex(const void* ptr); static std::string toHex(const void* ptr);
static std::string toHex(const uint8_t* input, size_t inputLength); static std::string toHex(const uint8_t* input, size_t inputLength);
@@ -45,7 +45,7 @@ public:
static void split(const std::string& src, std::vector<std::string>& dst, const std::string& delims); static void split(const std::string& src, std::vector<std::string>& dst, const std::string& delims);
static std::vector<std::string> split(const std::string& src, const std::string& delims = "\n"); static std::vector<std::string> split(const std::string& src, const std::string& delims = "\n");
template <typename T> template<typename T>
static std::string join(const std::vector<T>& v, const std::string& delimiter) static std::string join(const std::vector<T>& v, const std::string& delimiter)
{ {
std::ostringstream s; std::ostringstream s;
@@ -88,8 +88,7 @@ public:
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
// MSVC++ lacks memmem support // MSVC++ lacks memmem support
extern const void *memmem(const void *haystack, size_t haystack_len, extern const void* memmem(const void* haystack, size_t haystack_len, const void* const needle, const size_t needle_len);
const void * const needle, const size_t needle_len);
#endif #endif
#endif #endif
+42 -34
View File
@@ -10,11 +10,11 @@
#include <iostream> #include <iostream>
#ifdef TARGET_OSX #ifdef TARGET_OSX
# include <libkern/OSAtomic.h> #include <libkern/OSAtomic.h>
#endif #endif
#ifdef TARGET_WIN #ifdef TARGET_WIN
# include <Windows.h> #include <Windows.h>
#endif #endif
void SyncHelper::delay(unsigned int microseconds) void SyncHelper::delay(unsigned int microseconds)
@@ -35,7 +35,7 @@ void SyncHelper::delay(unsigned int microseconds)
// ------------------- ThreadHelper ------------------- // ------------------- ThreadHelper -------------------
void ThreadHelper::setName(const std::string &name) void ThreadHelper::setName(const std::string& name)
{ {
#if defined(TARGET_LINUX) #if defined(TARGET_LINUX)
// The name will be truncated to 8 or 16 characters // The name will be truncated to 8 or 16 characters
@@ -53,7 +53,7 @@ uint64_t ThreadHelper::getCurrentId()
return static_cast<uint64_t>(GetCurrentThreadId()); return static_cast<uint64_t>(GetCurrentThreadId());
#endif #endif
#if defined(TARGET_LINUX)||defined(TARGET_OSX) #if defined(TARGET_LINUX) || defined(TARGET_OSX)
// RPi builds want this! // RPi builds want this!
return (uint64_t)(pthread_self()); return (uint64_t)(pthread_self());
#endif #endif
@@ -76,7 +76,7 @@ static time_t TimestampBase = time(nullptr);
std::chrono::milliseconds chronox::getTimestamp() std::chrono::milliseconds chronox::getTimestamp()
{ {
time_point<steady_clock> t = steady_clock::now(); time_point<steady_clock> t = steady_clock::now();
uint64_t ms = duration_cast< milliseconds >(t.time_since_epoch()).count(); uint64_t ms = duration_cast<milliseconds>(t.time_since_epoch()).count();
return std::chrono::milliseconds(ms - TimestampStartPoint + TimestampBase * 1000); return std::chrono::milliseconds(ms - TimestampStartPoint + TimestampBase * 1000);
} }
@@ -84,7 +84,7 @@ std::chrono::milliseconds chronox::getUptime()
{ {
time_point<steady_clock> t = steady_clock::now(); time_point<steady_clock> t = steady_clock::now();
uint64_t ms = duration_cast< milliseconds >(t.time_since_epoch()).count(); uint64_t ms = duration_cast<milliseconds>(t.time_since_epoch()).count();
return std::chrono::milliseconds(ms - TimestampStartPoint); return std::chrono::milliseconds(ms - TimestampStartPoint);
} }
@@ -140,15 +140,9 @@ std::chrono::milliseconds chronox::ExecutionTime::getSpentTime() const
} }
// --------------- BufferQueue ----------------- // --------------- BufferQueue -----------------
BufferQueue::BufferQueue() BufferQueue::BufferQueue() {}
{
} BufferQueue::~BufferQueue() {}
BufferQueue::~BufferQueue()
{
}
void BufferQueue::push(const void* data, int bytes) void BufferQueue::push(const void* data, int bytes)
{ {
@@ -164,8 +158,7 @@ void BufferQueue::push(const void* data, int bytes)
BufferQueue::PBlock BufferQueue::pull(int milliseconds) BufferQueue::PBlock BufferQueue::pull(int milliseconds)
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
mSignal.wait_for(l, std::chrono::milliseconds(milliseconds), mSignal.wait_for(l, std::chrono::milliseconds(milliseconds), [this]() { return !mBlockList.empty(); });
[this]() { return !mBlockList.empty(); });
PBlock r; PBlock r;
if (!mBlockList.empty()) if (!mBlockList.empty())
@@ -178,9 +171,7 @@ BufferQueue::PBlock BufferQueue::pull(int milliseconds)
} }
// ----------------- Semaphore --------------------- // ----------------- Semaphore ---------------------
Semaphore::Semaphore(unsigned int count) Semaphore::Semaphore(unsigned int count) : m_count(count) {}
: m_count(count)
{}
void Semaphore::notify() void Semaphore::notify()
{ {
@@ -196,7 +187,8 @@ void Semaphore::wait()
m_count--; m_count--;
} }
bool Semaphore::waitFor(std::chrono::milliseconds timeout) { bool Semaphore::waitFor(std::chrono::milliseconds timeout)
{
std::unique_lock<std::mutex> lock(m_mtx); std::unique_lock<std::mutex> lock(m_mtx);
if (!m_cv.wait_for(lock, timeout, [this]() { return m_count > 0; })) if (!m_cv.wait_for(lock, timeout, [this]() { return m_count > 0; }))
@@ -246,15 +238,18 @@ uint64_t TimerQueue::add(std::chrono::milliseconds milliseconds, std::function<v
// 1 if the timer was cancelled. // 1 if the timer was cancelled.
// 0 if you were too late to cancel (or the timer ID was never valid to // 0 if you were too late to cancel (or the timer ID was never valid to
// start with) // start with)
size_t TimerQueue::cancel(uint64_t id) { size_t TimerQueue::cancel(uint64_t id)
{
// Instead of removing the item from the container (thus breaking the // Instead of removing the item from the container (thus breaking the
// heap integrity), we set the item as having no handler, and put // heap integrity), we set the item as having no handler, and put
// that handler on a new item at the top for immediate execution // that handler on a new item at the top for immediate execution
// The timer thread will then ignore the original item, since it has no // The timer thread will then ignore the original item, since it has no
// handler. // handler.
std::unique_lock<std::mutex> lk(m_mtx); std::unique_lock<std::mutex> lk(m_mtx);
for (auto&& item : m_items.getContainer()) { for (auto&& item : m_items.getContainer())
if (item.id == id && item.handler) { {
if (item.id == id && item.handler)
{
WorkItem newItem; WorkItem newItem;
// Zero time, so it stays at the top for immediate execution // Zero time, so it stays at the top for immediate execution
newItem.end = Clock::time_point(); newItem.end = Clock::time_point();
@@ -285,8 +280,10 @@ size_t TimerQueue::cancelAll()
// Setting all "end" to 0 (for immediate execution) is ok, // Setting all "end" to 0 (for immediate execution) is ok,
// since it maintains the heap integrity // since it maintains the heap integrity
std::unique_lock<std::mutex> lk(m_mtx); std::unique_lock<std::mutex> lk(m_mtx);
for (auto&& item : m_items.getContainer()) { for (auto&& item : m_items.getContainer())
if (item.id) { {
if (item.id)
{
item.end = Clock::time_point(); item.end = Clock::time_point();
item.id = 0; item.id = 0;
} }
@@ -307,9 +304,12 @@ void TimerQueue::run()
if (end.first) if (end.first)
{ {
// Timers found, so wait until it expires (or something else changes) // Timers found, so wait until it expires (or something else changes)
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end.second - std::chrono::steady_clock::now()); auto milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(end.second - std::chrono::steady_clock::now());
m_checkWork.waitFor(milliseconds); m_checkWork.waitFor(milliseconds);
} else { }
else
{
// No timers exist, so wait forever until something changes // No timers exist, so wait forever until something changes
m_checkWork.wait(); m_checkWork.wait();
} }
@@ -327,11 +327,15 @@ void TimerQueue::run()
std::pair<bool, TimerQueue::Clock::time_point> TimerQueue::calcWaitTime() std::pair<bool, TimerQueue::Clock::time_point> TimerQueue::calcWaitTime()
{ {
std::lock_guard<std::mutex> lk(m_mtx); std::lock_guard<std::mutex> lk(m_mtx);
while (m_items.size()) { while (m_items.size())
if (m_items.top().handler) { {
if (m_items.top().handler)
{
// Item present, so return the new wait time // Item present, so return the new wait time
return std::make_pair(true, m_items.top().end); return std::make_pair(true, m_items.top().end);
} else { }
else
{
// Discard empty handlers (they were cancelled) // Discard empty handlers (they were cancelled)
m_items.pop(); m_items.pop();
} }
@@ -342,9 +346,11 @@ std::pair<bool, TimerQueue::Clock::time_point> TimerQueue::calcWaitTime()
return std::make_pair(false, Clock::time_point()); return std::make_pair(false, Clock::time_point());
} }
void TimerQueue::checkWork() { void TimerQueue::checkWork()
{
std::unique_lock<std::mutex> lk(m_mtx); std::unique_lock<std::mutex> lk(m_mtx);
while (m_items.size() && m_items.top().end <= Clock::now()) { while (m_items.size() && m_items.top().end <= Clock::now())
{
WorkItem item(std::move(m_items.top())); WorkItem item(std::move(m_items.top()));
m_items.pop(); m_items.pop();
@@ -355,10 +361,12 @@ void TimerQueue::checkWork() {
} }
} }
bool TimerQueue::WorkItem::operator > (const TimerQueue::WorkItem& other) const { bool TimerQueue::WorkItem::operator>(const TimerQueue::WorkItem& other) const
{
return end > other.end; return end > other.end;
} }
std::vector<TimerQueue::WorkItem>& TimerQueue::Queue::getContainer() { std::vector<TimerQueue::WorkItem>& TimerQueue::Queue::getContainer()
{
return this->c; return this->c;
} }
+6 -6
View File
@@ -15,8 +15,8 @@
#include <assert.h> #include <assert.h>
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
# include <WinSock2.h> #include <WinSock2.h>
# include <Windows.h> #include <Windows.h>
#endif #endif
typedef std::recursive_mutex Mutex; typedef std::recursive_mutex Mutex;
@@ -76,6 +76,7 @@ public:
public: public:
ExecutionTime(); ExecutionTime();
std::chrono::milliseconds getSpentTime() const; std::chrono::milliseconds getSpentTime() const;
protected: protected:
std::chrono::milliseconds mStart; std::chrono::milliseconds mStart;
}; };
@@ -106,7 +107,7 @@ protected:
// Allows execution of handlers at a specified time in the future // Allows execution of handlers at a specified time in the future
// Guarantees: // Guarantees:
// - All handlers are executed ONCE, even if canceled (aborted parameter will // - All handlers are executed ONCE, even if canceled (aborted parameter will
//be set to true) // be set to true)
// - If TimerQueue is destroyed, it will cancel all handlers. // - If TimerQueue is destroyed, it will cancel all handlers.
// - Handlers are ALWAYS executed in the Timer Queue worker thread. // - Handlers are ALWAYS executed in the Timer Queue worker thread.
// - Handlers execution order is NOT guaranteed // - Handlers execution order is NOT guaranteed
@@ -153,13 +154,12 @@ private:
Clock::time_point end; Clock::time_point end;
uint64_t id; // id==0 means it was cancelled uint64_t id; // id==0 means it was cancelled
std::function<void(bool)> handler; std::function<void(bool)> handler;
bool operator > (const WorkItem& other) const; bool operator>(const WorkItem& other) const;
}; };
std::mutex m_mtx; std::mutex m_mtx;
// Inheriting from priority_queue, so we can access the internal container // Inheriting from priority_queue, so we can access the internal container
class Queue : public std::priority_queue<WorkItem, std::vector<WorkItem>, class Queue : public std::priority_queue<WorkItem, std::vector<WorkItem>, std::greater<WorkItem>>
std::greater<WorkItem>>
{ {
public: public:
std::vector<WorkItem>& getContainer(); std::vector<WorkItem>& getContainer();
+3 -3
View File
@@ -6,7 +6,7 @@ thread_pool::thread_pool(size_t num_of_threads, const std::string& name)
if (!num_of_threads) if (!num_of_threads)
num_of_threads = std::thread::hardware_concurrency(); num_of_threads = std::thread::hardware_concurrency();
for(size_t idx = 0; idx < num_of_threads; idx++) for (size_t idx = 0; idx < num_of_threads; idx++)
this->workers.emplace_back(std::thread(&thread_pool::run_worker, this)); this->workers.emplace_back(std::thread(&thread_pool::run_worker, this));
} }
@@ -45,7 +45,7 @@ thread_pool::~thread_pool()
stop = true; stop = true;
} }
this->condition.notify_all(); this->condition.notify_all();
for(std::thread &worker: workers) for (std::thread& worker : workers)
worker.join(); worker.join();
} }
@@ -58,7 +58,7 @@ void thread_pool::run_worker()
{ {
std::unique_lock<std::mutex> lock(this->queue_mutex); std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock, [this]{return !this->tasks.empty() || this->stop;}); this->condition.wait(lock, [this] { return !this->tasks.empty() || this->stop; });
if (!tasks.empty()) if (!tasks.empty())
{ {
t = tasks.front(); t = tasks.front();
+2 -2
View File
@@ -28,10 +28,10 @@ public:
private: private:
// need to keep track of threads so we can join them // need to keep track of threads so we can join them
std::vector< std::thread > workers; std::vector<std::thread> workers;
// the task queue // the task queue
std::queue< task > tasks; std::queue<task> tasks;
// synchronization // synchronization
std::mutex queue_mutex; std::mutex queue_mutex;
+4 -5
View File
@@ -12,7 +12,7 @@ double now_ms(void)
#else #else
struct timespec res; struct timespec res;
clock_gettime(CLOCK_MONOTONIC, &res); clock_gettime(CLOCK_MONOTONIC, &res);
return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6; return 1000.0 * res.tv_sec + (double)res.tv_nsec / 1e6;
#endif #endif
} }
@@ -31,17 +31,17 @@ int compare_timespec(const timespec& lhs, const timespec& rhs)
return 0; return 0;
} }
bool operator < (const timespec& lhs, const timespec& rhs) bool operator<(const timespec& lhs, const timespec& rhs)
{ {
return compare_timespec(lhs, rhs) < 0; return compare_timespec(lhs, rhs) < 0;
} }
bool operator == (const timespec& lhs, const timespec& rhs) bool operator==(const timespec& lhs, const timespec& rhs)
{ {
return compare_timespec(lhs, rhs) == 0; return compare_timespec(lhs, rhs) == 0;
} }
bool operator > (const timespec& lhs, const timespec& rhs) bool operator>(const timespec& lhs, const timespec& rhs)
{ {
return compare_timespec(lhs, rhs) > 0; return compare_timespec(lhs, rhs) > 0;
} }
@@ -50,4 +50,3 @@ bool is_zero(const timespec& ts)
{ {
return !ts.tv_sec && !ts.tv_nsec; return !ts.tv_sec && !ts.tv_nsec;
} }
+3 -3
View File
@@ -9,9 +9,9 @@ extern double now_ms();
// Compare the timespec. // Compare the timespec.
// Returns -1 if lhs < rhs, 1 if lhs > rhs, 0 if equal // Returns -1 if lhs < rhs, 1 if lhs > rhs, 0 if equal
extern int compare_timespec(const timespec& lhs, const timespec& rhs); extern int compare_timespec(const timespec& lhs, const timespec& rhs);
extern bool operator < (const timespec& lhs, const timespec& rhs); extern bool operator<(const timespec& lhs, const timespec& rhs);
extern bool operator == (const timespec& lhs, const timespec& rhs); extern bool operator==(const timespec& lhs, const timespec& rhs);
extern bool operator > (const timespec& lhs, const timespec& rhs); extern bool operator>(const timespec& lhs, const timespec& rhs);
extern bool is_zero(const timespec& ts); extern bool is_zero(const timespec& ts);
#endif #endif
+62 -36
View File
@@ -7,11 +7,11 @@
#define __HL_TYPES_H #define __HL_TYPES_H
#ifdef WIN32 #ifdef WIN32
# define tstring wstring #define tstring wstring
# define to_tstring to_wstring #define to_tstring to_wstring
#else #else
# define tstring string #define tstring string
# define to_tstring to_string #define to_tstring to_string
#endif #endif
#ifdef WIN32 #ifdef WIN32
@@ -32,49 +32,61 @@ enum SdpDirection
#include <stdexcept> #include <stdexcept>
#include <map> #include <map>
template< template<class K, class V, class HashK = std::hash<K>, class EqK = std::equal_to<K>, class HashV = std::hash<V>,
class K, class V, class EqV = std::equal_to<V>>
class HashK = std::hash<K>, class EqK = std::equal_to<K>, class BiMap
class HashV = std::hash<V>, class EqV = std::equal_to<V> {
>
class BiMap {
public: public:
using key_type = K; using key_type = K;
using mapped_type = V; using mapped_type = V;
BiMap(const std::map<K,V>& initializers) { BiMap(const std::map<K, V>& initializers)
for (const auto& item: initializers) { {
for (const auto& item : initializers)
{
insert(item.first, item.second); insert(item.first, item.second);
} }
} }
// Insert a new (key, value) pair. Returns false if either key or value already exists. // Insert a new (key, value) pair. Returns false if either key or value already exists.
bool insert(const K& k, const V& v) { bool insert(const K& k, const V& v)
if (contains_key(k) || contains_value(v)) return false; {
if (contains_key(k) || contains_value(v))
return false;
auto ok = forward_.emplace(k, v); auto ok = forward_.emplace(k, v);
try { try
{
auto ov = reverse_.emplace(v, k); auto ov = reverse_.emplace(v, k);
if (!ov.second) { // shouldn't happen given the guard above if (!ov.second)
{ // shouldn't happen given the guard above
forward_.erase(k); forward_.erase(k);
return false; return false;
} }
} catch (...) { }
catch (...)
{
forward_.erase(k); forward_.erase(k);
throw; throw;
} }
return ok.second; return ok.second;
} }
bool insert(K&& k, V&& v) { bool insert(K&& k, V&& v)
if (contains_key(k) || contains_value(v)) return false; {
if (contains_key(k) || contains_value(v))
return false;
auto ok = forward_.emplace(std::move(k), std::move(v)); auto ok = forward_.emplace(std::move(k), std::move(v));
try { try
{
auto ov = reverse_.emplace(ok.first->second, ok.first->first); // use stored refs auto ov = reverse_.emplace(ok.first->second, ok.first->first); // use stored refs
if (!ov.second) { if (!ov.second)
{
forward_.erase(ok.first); forward_.erase(ok.first);
return false; return false;
} }
} catch (...) { }
catch (...)
{
forward_.erase(ok.first); forward_.erase(ok.first);
throw; throw;
} }
@@ -82,10 +94,13 @@ public:
} }
// Replace value for existing key (and update reverse map). Returns false if value is already bound elsewhere. // Replace value for existing key (and update reverse map). Returns false if value is already bound elsewhere.
bool replace_by_key(const K& k, const V& new_v) { bool replace_by_key(const K& k, const V& new_v)
{
auto it = forward_.find(k); auto it = forward_.find(k);
if (it == forward_.end()) return false; if (it == forward_.end())
if (contains_value(new_v)) return false; return false;
if (contains_value(new_v))
return false;
// remove old reverse, insert new reverse, then update forward // remove old reverse, insert new reverse, then update forward
reverse_.erase(it->second); reverse_.erase(it->second);
reverse_.emplace(new_v, k); reverse_.emplace(new_v, k);
@@ -94,10 +109,13 @@ public:
} }
// Replace key for existing value (and update forward map). Returns false if key is already bound elsewhere. // Replace key for existing value (and update forward map). Returns false if key is already bound elsewhere.
bool replace_by_value(const V& v, const K& new_k) { bool replace_by_value(const V& v, const K& new_k)
{
auto it = reverse_.find(v); auto it = reverse_.find(v);
if (it == reverse_.end()) return false; if (it == reverse_.end())
if (contains_key(new_k)) return false; return false;
if (contains_key(new_k))
return false;
forward_.erase(it->second); forward_.erase(it->second);
forward_.emplace(new_k, v); forward_.emplace(new_k, v);
it->second = new_k; it->second = new_k;
@@ -105,17 +123,21 @@ public:
} }
// Erase by key/value. Return number erased (0 or 1). // Erase by key/value. Return number erased (0 or 1).
size_t erase_key(const K& k) { size_t erase_key(const K& k)
{
auto it = forward_.find(k); auto it = forward_.find(k);
if (it == forward_.end()) return 0; if (it == forward_.end())
return 0;
reverse_.erase(it->second); reverse_.erase(it->second);
forward_.erase(it); forward_.erase(it);
return 1; return 1;
} }
size_t erase_value(const V& v) { size_t erase_value(const V& v)
{
auto it = reverse_.find(v); auto it = reverse_.find(v);
if (it == reverse_.end()) return 0; if (it == reverse_.end())
return 0;
forward_.erase(it->second); forward_.erase(it->second);
reverse_.erase(it); reverse_.erase(it);
return 1; return 1;
@@ -125,11 +147,13 @@ public:
bool contains_key(const K& k) const { return forward_.find(k) != forward_.end(); } bool contains_key(const K& k) const { return forward_.find(k) != forward_.end(); }
bool contains_value(const V& v) const { return reverse_.find(v) != reverse_.end(); } bool contains_value(const V& v) const { return reverse_.find(v) != reverse_.end(); }
const V* find_by_key(const K& k) const { const V* find_by_key(const K& k) const
{
auto it = forward_.find(k); auto it = forward_.find(k);
return (it == forward_.end()) ? nullptr : &it->second; return (it == forward_.end()) ? nullptr : &it->second;
} }
const K* find_by_value(const V& v) const { const K* find_by_value(const V& v) const
{
auto it = reverse_.find(v); auto it = reverse_.find(v);
return (it == reverse_.end()) ? nullptr : &it->second; return (it == reverse_.end()) ? nullptr : &it->second;
} }
@@ -138,7 +162,8 @@ public:
const V& at_key(const K& k) const { return forward_.at(k); } const V& at_key(const K& k) const { return forward_.at(k); }
const K& at_value(const V& v) const { return reverse_.at(v); } const K& at_value(const V& v) const { return reverse_.at(v); }
void clear() noexcept { void clear() noexcept
{
forward_.clear(); forward_.clear();
reverse_.clear(); reverse_.clear();
} }
@@ -147,7 +172,8 @@ public:
size_t size() const noexcept { return forward_.size(); } size_t size() const noexcept { return forward_.size(); }
// Reserve buckets for performance (optional) // Reserve buckets for performance (optional)
void reserve(size_t n) { void reserve(size_t n)
{
forward_.reserve(n); forward_.reserve(n);
reverse_.reserve(n); reverse_.reserve(n);
} }
+10 -13
View File
@@ -7,8 +7,7 @@
#define ADR_WINDOW_CLASS_NAME L"HIDDEN_USB_CHANGE_DELEGATE_WINDOWCLASS_%u" #define ADR_WINDOW_CLASS_NAME L"HIDDEN_USB_CHANGE_DELEGATE_WINDOWCLASS_%u"
#define ADR_WINDOW_NAME L"HIDDEN_USB_CHANGE_DELEGATE_WINDOW_%u" #define ADR_WINDOW_NAME L"HIDDEN_USB_CHANGE_DELEGATE_WINDOW_%u"
UsbChangeListener::UsbChangeListener() UsbChangeListener::UsbChangeListener() : mNotifyHandle(NULL), mHiddenWindow(NULL), mDelegate(NULL)
:mNotifyHandle(NULL), mHiddenWindow(NULL), mDelegate(NULL)
{ {
wsprintfW(mWindowClassName, ADR_WINDOW_CLASS_NAME, (unsigned int)rand()); wsprintfW(mWindowClassName, ADR_WINDOW_CLASS_NAME, (unsigned int)rand());
} }
@@ -32,7 +31,7 @@ void UsbChangeListener::start()
{ {
// Exposing Window to Mixer // Exposing Window to Mixer
WNDCLASSEXW wcx; WNDCLASSEXW wcx;
memset( &wcx, 0, sizeof(WNDCLASSEXW) ); memset(&wcx, 0, sizeof(WNDCLASSEXW));
wcx.cbSize = sizeof(WNDCLASSEXW); wcx.cbSize = sizeof(WNDCLASSEXW);
wcx.lpszClassName = mWindowClassName; wcx.lpszClassName = mWindowClassName;
wcx.lpfnWndProc = (WNDPROC)ADRWindowProc; wcx.lpfnWndProc = (WNDPROC)ADRWindowProc;
@@ -40,11 +39,8 @@ void UsbChangeListener::start()
wchar_t windowname[128]; wchar_t windowname[128];
wsprintfW(windowname, ADR_WINDOW_NAME, rand()); wsprintfW(windowname, ADR_WINDOW_NAME, rand());
mHiddenWindow = CreateWindowW( mWindowClassName, mHiddenWindow =
windowname, CreateWindowW(mWindowClassName, windowname, WS_POPUP | WS_DISABLED, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
WS_POPUP | WS_DISABLED,
0, 0, 0, 0,
NULL, NULL, NULL, NULL );
if (!mHiddenWindow) if (!mHiddenWindow)
throw Exception(ERR_CREATEWINDOW, GetLastError()); throw Exception(ERR_CREATEWINDOW, GetLastError());
if (!SetWindowLongPtr(mHiddenWindow, GWLP_USERDATA, (LONG_PTR)this)) if (!SetWindowLongPtr(mHiddenWindow, GWLP_USERDATA, (LONG_PTR)this))
@@ -57,13 +53,14 @@ void UsbChangeListener::start()
mNotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; mNotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
// Register notification // Register notification
if (!RegisterDeviceNotification(mHiddenWindow, &mNotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)) if (!RegisterDeviceNotification(mHiddenWindow, &mNotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES))
throw Exception(ERR_REGISTERNOTIFICATION, GetLastError()); throw Exception(ERR_REGISTERNOTIFICATION, GetLastError());
} }
LRESULT CALLBACK UsbChangeListener::ADRWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) LRESULT CALLBACK UsbChangeListener::ADRWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
if ( uMsg == WM_DEVICECHANGE ) if (uMsg == WM_DEVICECHANGE)
{ {
if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE) if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE)
{ {
@@ -84,7 +81,7 @@ LRESULT CALLBACK UsbChangeListener::ADRWindowProc( HWND hwnd, UINT uMsg, WPARAM
} }
} }
} }
return ::DefWindowProc( hwnd, uMsg, wParam, lParam); return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
} }
@@ -97,7 +94,7 @@ void UsbChangeListener::stop()
mNotifyHandle = NULL; mNotifyHandle = NULL;
} }
//Destroy the window // Destroy the window
if (mHiddenWindow != NULL) if (mHiddenWindow != NULL)
{ {
::DestroyWindow(mHiddenWindow); ::DestroyWindow(mHiddenWindow);
+6 -6
View File
@@ -7,9 +7,9 @@
#include <windows.h> #include <windows.h>
#include <Dbt.h> #include <Dbt.h>
class UsbChangeListener class UsbChangeListener
{ {
public: public:
class Delegate class Delegate
{ {
public: public:
@@ -26,7 +26,7 @@
void start(); void start();
void stop(); void stop();
protected: protected:
HDEVNOTIFY mNotifyHandle; /// Handle to track notifications about USB insert/removal. HDEVNOTIFY mNotifyHandle; /// Handle to track notifications about USB insert/removal.
HWND mHiddenWindow; /// Hidden window to receive notifications HWND mHiddenWindow; /// Hidden window to receive notifications
DEV_BROADCAST_DEVICEINTERFACE mNotificationFilter; /// Notifications filter DEV_BROADCAST_DEVICEINTERFACE mNotificationFilter; /// Notifications filter
@@ -38,8 +38,8 @@
/// @param uMsg Message ID /// @param uMsg Message ID
/// @param wParam First param /// @param wParam First param
/// @param lParam Second param /// @param lParam Second param
static LRESULT CALLBACK ADRWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ); static LRESULT CALLBACK ADRWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
}; };
#endif #endif
+3 -3
View File
@@ -28,7 +28,7 @@ Uuid Uuid::generateOne()
static thread_local std::mt19937 generator = [] static thread_local std::mt19937 generator = []
{ {
std::random_device rd; std::random_device rd;
auto seed_data = std::array<int, std::mt19937::state_size> {}; auto seed_data = std::array<int, std::mt19937::state_size>{};
std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd));
std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
return std::mt19937(seq); return std::mt19937(seq);
@@ -40,7 +40,7 @@ Uuid Uuid::generateOne()
return result; return result;
} }
Uuid Uuid::parse(const std::string &s) Uuid Uuid::parse(const std::string& s)
{ {
Uuid result; Uuid result;
auto id = uuids::uuid::from_string(s); auto id = uuids::uuid::from_string(s);
@@ -57,7 +57,7 @@ std::string Uuid::toString() const
return uuids::to_string(id); return uuids::to_string(id);
} }
bool Uuid::operator < (const Uuid& right) const bool Uuid::operator<(const Uuid& right) const
{ {
return memcmp(mUuid, right.mUuid, sizeof(mUuid)) < 0; return memcmp(mUuid, right.mUuid, sizeof(mUuid)) < 0;
return false; return false;
+1 -1
View File
@@ -11,7 +11,7 @@ public:
static Uuid generateOne(); static Uuid generateOne();
static Uuid parse(const std::string& s); static Uuid parse(const std::string& s);
std::string toString() const; std::string toString() const;
bool operator < (const Uuid& right) const; bool operator<(const Uuid& right) const;
protected: protected:
uint8_t mUuid[16]; uint8_t mUuid[16];
+78 -71
View File
@@ -8,7 +8,7 @@
#include <assert.h> #include <assert.h>
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
# define __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS
#endif #endif
#include <inttypes.h> #include <inttypes.h>
@@ -25,35 +25,21 @@ Variant::Variant()
mPointer = nullptr; mPointer = nullptr;
} }
Variant::~Variant() Variant::~Variant() {}
{
}
Variant::Variant(bool value) Variant::Variant(bool value) : mType(VTYPE_BOOL), mBool(value) {}
:mType(VTYPE_BOOL), mBool(value)
{}
Variant::Variant(int value) Variant::Variant(int value) : mType(VTYPE_INT), mInt(value) {}
:mType(VTYPE_INT), mInt(value)
{}
Variant::Variant(int64_t value) Variant::Variant(int64_t value) : mType(VTYPE_INT64), mInt64(value) {}
:mType(VTYPE_INT64), mInt64(value)
{}
Variant::Variant(float value) Variant::Variant(float value) : mType(VTYPE_FLOAT), mFloat(value) {}
:mType(VTYPE_FLOAT), mFloat(value)
{}
Variant::Variant(double value) Variant::Variant(double value) : mType(VTYPE_FLOAT), mFloat((float)value) {}
:mType(VTYPE_FLOAT), mFloat((float)value)
{}
Variant::Variant(const std::string& value) Variant::Variant(const std::string& value) : mType(VTYPE_STRING), mString(value) {}
:mType(VTYPE_STRING), mString(value)
{}
Variant& Variant::operator = (bool value) Variant& Variant::operator=(bool value)
{ {
mType = VTYPE_BOOL; mType = VTYPE_BOOL;
mBool = value; mBool = value;
@@ -61,7 +47,7 @@ Variant& Variant::operator = (bool value)
return *this; return *this;
} }
Variant& Variant::operator = (int value) Variant& Variant::operator=(int value)
{ {
mType = VTYPE_INT; mType = VTYPE_INT;
mInt = value; mInt = value;
@@ -69,7 +55,7 @@ Variant& Variant::operator = (int value)
return *this; return *this;
} }
Variant& Variant::operator = (int64_t value) Variant& Variant::operator=(int64_t value)
{ {
mType = VTYPE_INT64; mType = VTYPE_INT64;
mInt64 = value; mInt64 = value;
@@ -77,7 +63,7 @@ Variant& Variant::operator = (int64_t value)
return *this; return *this;
} }
Variant& Variant::operator = (float value) Variant& Variant::operator=(float value)
{ {
mType = VTYPE_FLOAT; mType = VTYPE_FLOAT;
mFloat = value; mFloat = value;
@@ -85,7 +71,7 @@ Variant& Variant::operator = (float value)
return *this; return *this;
} }
Variant& Variant::operator = (const std::string& value) Variant& Variant::operator=(const std::string& value)
{ {
mType = VTYPE_STRING; mType = VTYPE_STRING;
mString = value; mString = value;
@@ -93,7 +79,7 @@ Variant& Variant::operator = (const std::string& value)
return *this; return *this;
} }
Variant& Variant::operator = (const char* value) Variant& Variant::operator=(const char* value)
{ {
mType = VTYPE_STRING; mType = VTYPE_STRING;
mString = value; mString = value;
@@ -101,7 +87,7 @@ Variant& Variant::operator = (const char* value)
return *this; return *this;
} }
Variant& Variant::operator = (void* value) Variant& Variant::operator=(void* value)
{ {
mType = VTYPE_POINTER; mType = VTYPE_POINTER;
mPointer = value; mPointer = value;
@@ -109,7 +95,7 @@ Variant& Variant::operator = (void* value)
return *this; return *this;
} }
Variant& Variant::operator = (PVariantMap map) Variant& Variant::operator=(PVariantMap map)
{ {
mType = VTYPE_VMAP; mType = VTYPE_VMAP;
mVMap = map; mVMap = map;
@@ -117,94 +103,119 @@ Variant& Variant::operator = (PVariantMap map)
return *this; return *this;
} }
Variant Variant::operator + (const Variant& rhs) Variant Variant::operator+(const Variant& rhs)
{ {
switch (type()) switch (type())
{ {
case VTYPE_BOOL: case VTYPE_BOOL:
case VTYPE_INT: return asInt() + rhs.asInt(); case VTYPE_INT:
case VTYPE_INT64: return asInt64() + rhs.asInt64(); return asInt() + rhs.asInt();
case VTYPE_FLOAT: return asFloat() + rhs.asFloat(); case VTYPE_INT64:
case VTYPE_STRING: return asStdString() + rhs.asStdString(); return asInt64() + rhs.asInt64();
case VTYPE_FLOAT:
return asFloat() + rhs.asFloat();
case VTYPE_STRING:
return asStdString() + rhs.asStdString();
default: default:
return false; return false;
} }
} }
Variant Variant::operator - (const Variant& rhs) Variant Variant::operator-(const Variant& rhs)
{ {
switch (type()) switch (type())
{ {
case VTYPE_BOOL: case VTYPE_BOOL:
case VTYPE_STRING: case VTYPE_STRING:
case VTYPE_INT: return asInt() - rhs.asInt(); case VTYPE_INT:
case VTYPE_INT64: return asInt64() - rhs.asInt64(); return asInt() - rhs.asInt();
case VTYPE_FLOAT: return asFloat() - rhs.asFloat(); case VTYPE_INT64:
return asInt64() - rhs.asInt64();
case VTYPE_FLOAT:
return asFloat() - rhs.asFloat();
default: default:
return false; return false;
} }
} }
Variant Variant::operator * (const Variant& rhs) Variant Variant::operator*(const Variant& rhs)
{ {
switch (type()) switch (type())
{ {
case VTYPE_BOOL: case VTYPE_BOOL:
case VTYPE_STRING: case VTYPE_STRING:
case VTYPE_INT: return asInt() * rhs.asInt(); case VTYPE_INT:
case VTYPE_INT64: return asInt64() * rhs.asInt64(); return asInt() * rhs.asInt();
case VTYPE_FLOAT: return asFloat() * rhs.asFloat(); case VTYPE_INT64:
return asInt64() * rhs.asInt64();
case VTYPE_FLOAT:
return asFloat() * rhs.asFloat();
default: default:
return false; return false;
} }
} }
Variant Variant::operator / (const Variant& rhs) Variant Variant::operator/(const Variant& rhs)
{ {
switch (type()) switch (type())
{ {
case VTYPE_BOOL: case VTYPE_BOOL:
case VTYPE_STRING: case VTYPE_STRING:
case VTYPE_INT: return asInt() / rhs.asInt(); case VTYPE_INT:
case VTYPE_INT64: return asInt64() / rhs.asInt64(); return asInt() / rhs.asInt();
case VTYPE_FLOAT: return asFloat() / rhs.asFloat(); case VTYPE_INT64:
return asInt64() / rhs.asInt64();
case VTYPE_FLOAT:
return asFloat() / rhs.asFloat();
default: default:
return false; return false;
} }
} }
bool Variant::operator < (const Variant& rhs) const bool Variant::operator<(const Variant& rhs) const
{ {
switch (type()) switch (type())
{ {
case VTYPE_STRING: return asStdString() < rhs.asStdString(); case VTYPE_STRING:
return asStdString() < rhs.asStdString();
case VTYPE_BOOL: case VTYPE_BOOL:
case VTYPE_INT: return asInt() < rhs.asInt(); case VTYPE_INT:
case VTYPE_INT64: return asInt64() < rhs.asInt64(); return asInt() < rhs.asInt();
case VTYPE_FLOAT: return asFloat() < rhs.asFloat(); case VTYPE_INT64:
return asInt64() < rhs.asInt64();
case VTYPE_FLOAT:
return asFloat() < rhs.asFloat();
default: default:
return false; return false;
} }
} }
bool Variant::operator > (const Variant& rhs) const bool Variant::operator>(const Variant& rhs) const
{ {
return !(*this == rhs) && !(*this < rhs); return !(*this == rhs) && !(*this < rhs);
} }
bool Variant::operator == (const Variant& rhs) const bool Variant::operator==(const Variant& rhs) const
{ {
switch (type()) switch (type())
{ {
case VTYPE_STRING: return asStdString() == rhs.asStdString(); case VTYPE_STRING:
case VTYPE_BOOL: return asBool() == rhs.asBool(); return asStdString() == rhs.asStdString();
case VTYPE_INT: return asInt() == rhs.asInt(); case VTYPE_BOOL:
case VTYPE_INT64: return asInt64() == rhs.asInt64(); return asBool() == rhs.asBool();
case VTYPE_FLOAT: return asFloat() == rhs.asFloat(); case VTYPE_INT:
case VTYPE_POINTER: return asPointer() == rhs.asPointer(); return asInt() == rhs.asInt();
case VTYPE_VMAP: assert(0); break; case VTYPE_INT64:
return asInt64() == rhs.asInt64();
case VTYPE_FLOAT:
return asFloat() == rhs.asFloat();
case VTYPE_POINTER:
return asPointer() == rhs.asPointer();
case VTYPE_VMAP:
assert(0);
break;
default: default:
return false; return false;
} }
@@ -212,17 +223,17 @@ bool Variant::operator == (const Variant& rhs) const
return false; return false;
} }
bool Variant::operator != (const Variant& rhs) const bool Variant::operator!=(const Variant& rhs) const
{ {
return !(*this == rhs); return !(*this == rhs);
} }
bool Variant::operator <= (const Variant& rhs) const bool Variant::operator<=(const Variant& rhs) const
{ {
return (*this < rhs) || (*this == rhs); return (*this < rhs) || (*this == rhs);
} }
bool Variant::operator >= (const Variant& rhs) const bool Variant::operator>=(const Variant& rhs) const
{ {
return (*this > rhs) || (*this == rhs); return (*this > rhs) || (*this == rhs);
} }
@@ -345,13 +356,9 @@ VariantType Variant::type() const
return mType; return mType;
} }
VariantMap::VariantMap() VariantMap::VariantMap() {}
{
}
VariantMap::~VariantMap() VariantMap::~VariantMap() {}
{
}
bool VariantMap::empty() const bool VariantMap::empty() const
{ {
@@ -368,7 +375,7 @@ bool VariantMap::exists(int itemId) const
return mData.find(itemId) != mData.end(); return mData.find(itemId) != mData.end();
} }
Variant& VariantMap::operator [](int itemId) Variant& VariantMap::operator[](int itemId)
{ {
return mData[itemId]; return mData[itemId];
} }

Some files were not shown because too many files have changed in this diff Show More