| 1 | // |
| 2 | // libtgvoip is free and unencumbered public domain software. |
| 3 | // For more information, see http://unlicense.org or the UNLICENSE file |
| 4 | // you should have received with this source code distribution. |
| 5 | // |
| 6 | |
| 7 | #include "AudioIOCallback.h" |
| 8 | #include "../VoIPController.h" |
| 9 | #include "../logging.h" |
| 10 | |
| 11 | using namespace tgvoip; |
| 12 | using namespace tgvoip::audio; |
| 13 | |
| 14 | #pragma mark - IO |
| 15 | |
| 16 | AudioIOCallback::AudioIOCallback(){ |
| 17 | input=new AudioInputCallback(); |
| 18 | output=new AudioOutputCallback(); |
| 19 | } |
| 20 | |
| 21 | AudioIOCallback::~AudioIOCallback(){ |
| 22 | delete input; |
| 23 | delete output; |
| 24 | } |
| 25 | |
| 26 | AudioInput* AudioIOCallback::GetInput(){ |
| 27 | return input; |
| 28 | } |
| 29 | |
| 30 | AudioOutput* AudioIOCallback::GetOutput(){ |
| 31 | return output; |
| 32 | } |
| 33 | |
| 34 | #pragma mark - Input |
| 35 | |
| 36 | AudioInputCallback::AudioInputCallback(){ |
| 37 | thread=new Thread(std::bind(&AudioInputCallback::RunThread, this)); |
| 38 | thread->SetName("AudioInputCallback"); |
| 39 | } |
| 40 | |
| 41 | AudioInputCallback::~AudioInputCallback(){ |
| 42 | running=false; |
| 43 | thread->Join(); |
| 44 | delete thread; |
| 45 | } |
| 46 | |
| 47 | void AudioInputCallback::Start(){ |
| 48 | if(!running){ |
| 49 | running=true; |
| 50 | thread->Start(); |
| 51 | } |
| 52 | recording=true; |
| 53 | } |
| 54 | |
| 55 | void AudioInputCallback::Stop(){ |
| 56 | recording=false; |
| 57 | } |
| 58 | |
| 59 | void AudioInputCallback::SetDataCallback(std::function<void(int16_t*, size_t)> c){ |
| 60 | dataCallback=c; |
| 61 | } |
| 62 | |
| 63 | void AudioInputCallback::RunThread(){ |
| 64 | int16_t buf[960]; |
| 65 | while(running){ |
| 66 | double t=VoIPController::GetCurrentTime(); |
| 67 | memset(buf, 0, sizeof(buf)); |
| 68 | dataCallback(buf, 960); |
| 69 | InvokeCallback(reinterpret_cast<unsigned char*>(buf), 960*2); |
| 70 | double sl=0.02-(VoIPController::GetCurrentTime()-t); |
| 71 | if(sl>0) |
| 72 | Thread::Sleep(sl); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #pragma mark - Output |
| 77 | |
| 78 | AudioOutputCallback::AudioOutputCallback(){ |
| 79 | thread=new Thread(std::bind(&AudioOutputCallback::RunThread, this)); |
| 80 | thread->SetName("AudioOutputCallback"); |
| 81 | } |
| 82 | |
| 83 | AudioOutputCallback::~AudioOutputCallback(){ |
| 84 | running=false; |
| 85 | thread->Join(); |
| 86 | delete thread; |
| 87 | } |
| 88 | |
| 89 | void AudioOutputCallback::Start(){ |
| 90 | if(!running){ |
| 91 | running=true; |
| 92 | thread->Start(); |
| 93 | } |
| 94 | playing=true; |
| 95 | } |
| 96 | |
| 97 | void AudioOutputCallback::Stop(){ |
| 98 | playing=false; |
| 99 | } |
| 100 | |
| 101 | bool AudioOutputCallback::IsPlaying(){ |
| 102 | return playing; |
| 103 | } |
| 104 | |
| 105 | void AudioOutputCallback::SetDataCallback(std::function<void(int16_t*, size_t)> c){ |
| 106 | dataCallback=c; |
| 107 | } |
| 108 | |
| 109 | void AudioOutputCallback::RunThread(){ |
| 110 | int16_t buf[960]; |
| 111 | while(running){ |
| 112 | double t=VoIPController::GetCurrentTime(); |
| 113 | InvokeCallback(reinterpret_cast<unsigned char*>(buf), 960*2); |
| 114 | dataCallback(buf, 960); |
| 115 | double sl=0.02-(VoIPController::GetCurrentTime()-t); |
| 116 | if(sl>0) |
| 117 | Thread::Sleep(sl); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |