forked from grishka/libtgvoip
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added tests (using xcode) - Added a define to enable callback-based audio I/O instead of using actual audio devices - Fixed a crash on Windows when there's no access to the microphone - Misc fixes
- Loading branch information
Showing
18 changed files
with
1,394 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// libtgvoip is free and unencumbered public domain software. | ||
// For more information, see http://unlicense.org or the UNLICENSE file | ||
// you should have received with this source code distribution. | ||
// | ||
|
||
#include "AudioIOCallback.h" | ||
#include "../VoIPController.h" | ||
#include "../logging.h" | ||
|
||
using namespace tgvoip; | ||
using namespace tgvoip::audio; | ||
|
||
#pragma mark - IO | ||
|
||
AudioIOCallback::AudioIOCallback(){ | ||
input=new AudioInputCallback(); | ||
output=new AudioOutputCallback(); | ||
} | ||
|
||
AudioIOCallback::~AudioIOCallback(){ | ||
delete input; | ||
delete output; | ||
} | ||
|
||
AudioInput* AudioIOCallback::GetInput(){ | ||
return input; | ||
} | ||
|
||
AudioOutput* AudioIOCallback::GetOutput(){ | ||
return output; | ||
} | ||
|
||
#pragma mark - Input | ||
|
||
AudioInputCallback::AudioInputCallback(){ | ||
thread=new Thread(new MethodPointer<AudioInputCallback>(&AudioInputCallback::RunThread, this), NULL); | ||
thread->SetName("AudioInputCallback"); | ||
} | ||
|
||
AudioInputCallback::~AudioInputCallback(){ | ||
running=false; | ||
thread->Join(); | ||
delete thread; | ||
} | ||
|
||
void AudioInputCallback::Start(){ | ||
if(!running){ | ||
running=true; | ||
thread->Start(); | ||
} | ||
recording=true; | ||
} | ||
|
||
void AudioInputCallback::Stop(){ | ||
recording=false; | ||
} | ||
|
||
void AudioInputCallback::SetDataCallback(std::function<void(int16_t*, size_t)> c){ | ||
dataCallback=c; | ||
} | ||
|
||
void AudioInputCallback::RunThread(void*){ | ||
int16_t buf[960]; | ||
while(running){ | ||
double t=VoIPController::GetCurrentTime(); | ||
memset(buf, 0, sizeof(buf)); | ||
dataCallback(buf, 960); | ||
InvokeCallback(reinterpret_cast<unsigned char*>(buf), 960*2); | ||
double sl=0.02-(VoIPController::GetCurrentTime()-t); | ||
if(sl>0) | ||
Thread::Sleep(sl); | ||
} | ||
} | ||
|
||
#pragma mark - Output | ||
|
||
AudioOutputCallback::AudioOutputCallback(){ | ||
thread=new Thread(new MethodPointer<AudioOutputCallback>(&AudioOutputCallback::RunThread, this), NULL); | ||
thread->SetName("AudioOutputCallback"); | ||
} | ||
|
||
AudioOutputCallback::~AudioOutputCallback(){ | ||
running=false; | ||
thread->Join(); | ||
delete thread; | ||
} | ||
|
||
void AudioOutputCallback::Start(){ | ||
if(!running){ | ||
running=true; | ||
thread->Start(); | ||
} | ||
playing=true; | ||
} | ||
|
||
void AudioOutputCallback::Stop(){ | ||
playing=false; | ||
} | ||
|
||
bool AudioOutputCallback::IsPlaying(){ | ||
return playing; | ||
} | ||
|
||
void AudioOutputCallback::SetDataCallback(std::function<void(int16_t*, size_t)> c){ | ||
dataCallback=c; | ||
} | ||
|
||
void AudioOutputCallback::RunThread(void*){ | ||
int16_t buf[960]; | ||
while(running){ | ||
double t=VoIPController::GetCurrentTime(); | ||
InvokeCallback(reinterpret_cast<unsigned char*>(buf), 960*2); | ||
dataCallback(buf, 960); | ||
double sl=0.02-(VoIPController::GetCurrentTime()-t); | ||
if(sl>0) | ||
Thread::Sleep(sl); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// libtgvoip is free and unencumbered public domain software. | ||
// For more information, see http://unlicense.org or the UNLICENSE file | ||
// you should have received with this source code distribution. | ||
// | ||
|
||
#ifndef LIBTGVOIP_AUDIO_IO_CALLBACK | ||
#define LIBTGVOIP_AUDIO_IO_CALLBACK | ||
|
||
#include <AudioIO.h> | ||
#include <functional> | ||
|
||
#include "../threading.h" | ||
|
||
namespace tgvoip{ | ||
namespace audio{ | ||
class AudioInputCallback : public AudioInput{ | ||
public: | ||
AudioInputCallback(); | ||
virtual ~AudioInputCallback(); | ||
virtual void Start() override; | ||
virtual void Stop() override; | ||
void SetDataCallback(std::function<void(int16_t*, size_t)> c); | ||
private: | ||
void RunThread(void*); | ||
bool running=false; | ||
bool recording=false; | ||
Thread* thread; | ||
std::function<void(int16_t*, size_t)> dataCallback; | ||
}; | ||
|
||
class AudioOutputCallback : public AudioOutput{ | ||
public: | ||
AudioOutputCallback(); | ||
virtual ~AudioOutputCallback(); | ||
virtual void Start() override; | ||
virtual void Stop() override; | ||
virtual bool IsPlaying() override; | ||
void SetDataCallback(std::function<void(int16_t*, size_t)> c); | ||
private: | ||
void RunThread(void*); | ||
bool running=false; | ||
bool playing=false; | ||
Thread* thread; | ||
std::function<void(int16_t*, size_t)> dataCallback; | ||
}; | ||
|
||
class AudioIOCallback : public AudioIO{ | ||
public: | ||
AudioIOCallback(); | ||
virtual ~AudioIOCallback(); | ||
virtual AudioInput* GetInput() override; | ||
virtual AudioOutput* GetOutput() override; | ||
private: | ||
AudioInputCallback* input; | ||
AudioOutputCallback* output; | ||
}; | ||
} | ||
} | ||
|
||
|
||
#endif /* LIBTGVOIP_AUDIO_IO_CALLBACK */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.