diff --git a/core.cpp b/core.cpp index dc64dd7e9..4709a3ee8 100644 --- a/core.cpp +++ b/core.cpp @@ -63,6 +63,26 @@ Core::Core(Camera* cam, QThread *coreThread) : calls[i].sendVideoTimer->moveToThread(coreThread); connect(calls[i].sendVideoTimer, &QTimer::timeout, [this,i](){sendCallVideo(i);}); } + + // OpenAL init + alOutDev = alcOpenDevice(nullptr); + if (!alOutDev) + { + qWarning() << "Core: Cannot open output audio device"; + } + else + { + alContext=alcCreateContext(alOutDev,nullptr); + if (!alcMakeContextCurrent(alContext)) + { + qWarning() << "Core: Cannot create output audio context"; + alcCloseDevice(alOutDev); + } + } + alInDev = alcCaptureOpenDevice(NULL,av_DefaultSettings.audio_sample_rate, AL_FORMAT_MONO16, + (av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000); + if (!alInDev) + qWarning() << "Core: Cannot open input audio device"; } Core::~Core() @@ -78,6 +98,16 @@ Core::~Core() delete[] videobuf; videobuf=nullptr; } + + if (alContext) + { + alcMakeContextCurrent(nullptr); + alcDestroyContext(alContext); + } + if (alOutDev) + alcCloseDevice(alOutDev); + if (alInDev) + alcCaptureCloseDevice(alInDev); } void Core::start() diff --git a/core.h b/core.h index 40fe53f0c..32c34c39d 100644 --- a/core.h +++ b/core.h @@ -104,8 +104,6 @@ public: bool videoEnabled; bool active; bool muteMic; - ALCdevice* alOutDev, *alInDev; - ALCcontext* alContext; ALuint alSource; }; @@ -303,6 +301,9 @@ private: static const int videobufsize; static uint8_t* videobuf; static int videoBusyness; // Used to know when to drop frames + + static ALCdevice* alOutDev, *alInDev; + static ALCcontext* alContext; }; #endif // CORE_HPP diff --git a/coreav.cpp b/coreav.cpp index e8fc38514..cb4a518be 100644 --- a/coreav.cpp +++ b/coreav.cpp @@ -6,6 +6,9 @@ const int Core::videobufsize{TOXAV_MAX_VIDEO_WIDTH * TOXAV_MAX_VIDEO_HEIGHT * 4} uint8_t* Core::videobuf; int Core::videoBusyness; +ALCdevice* Core::alOutDev, *Core::alInDev; +ALCcontext* Core::alContext; + void Core::prepareCall(int friendId, int callId, ToxAv* toxav, bool videoEnabled) { qDebug() << QString("Core: preparing call %1").arg(callId); @@ -20,33 +23,9 @@ void Core::prepareCall(int friendId, int callId, ToxAv* toxav, bool videoEnabled calls[callId].videoEnabled = videoEnabled; toxav_prepare_transmission(toxav, callId, av_jbufdc, av_VADd, videoEnabled); - // Audio output - calls[callId].alOutDev = alcOpenDevice(nullptr); - if (!calls[callId].alOutDev) - { - qWarning() << "Coreav: Cannot open output audio device, hanging up call"; - toxav_hangup(toxav, callId); - return; - } - calls[callId].alContext=alcCreateContext(calls[callId].alOutDev,nullptr); - if (!alcMakeContextCurrent(calls[callId].alContext)) - { - qWarning() << "Coreav: Cannot create output audio context, hanging up call"; - alcCloseDevice(calls[callId].alOutDev); - toxav_hangup(toxav, callId); - return; - } + // Audio alGenSources(1, &calls[callId].alSource); - - // Audio Input - calls[callId].alInDev = alcCaptureOpenDevice(NULL,av_DefaultSettings.audio_sample_rate, AL_FORMAT_MONO16, (av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000); - if (!calls[callId].alInDev) - { - qWarning() << "Coreav: Cannot open input audio device, hanging up call"; - toxav_hangup(toxav, callId); - return; - } - alcCaptureStart(calls[callId].alInDev); + alcCaptureStart(alInDev); // Go calls[callId].active = true; @@ -164,11 +143,7 @@ void Core::cleanupCall(int callId) calls[callId].sendVideoTimer->stop(); if (calls[callId].videoEnabled) Widget::getInstance()->getCamera()->unsuscribe(); - alcMakeContextCurrent(nullptr); - alcDestroyContext(calls[callId].alContext); - alcCloseDevice(calls[callId].alOutDev); - alcCaptureStop(calls[callId].alInDev); - alcCaptureCloseDevice(calls[callId].alInDev); + alcCaptureStop(alInDev); } void Core::playCallAudio(ToxAv* toxav, int32_t callId, int16_t *data, int samples, void *user_data) @@ -199,10 +174,10 @@ void Core::sendCallAudio(int callId, ToxAv* toxav) bool frame = false; ALint samples; - alcGetIntegerv(calls[callId].alInDev, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples); + alcGetIntegerv(alInDev, ALC_CAPTURE_SAMPLES, sizeof(samples), &samples); if(samples >= framesize) { - alcCaptureSamples(calls[callId].alInDev, buf, framesize); + alcCaptureSamples(alInDev, buf, framesize); frame = 1; }