| 6 |
| 7 | #include "../logging.h" |
| 8 | #include "../VoIPController.h" |
| 9 | #include "AudioIOCallback.h" |
| 10 |
| 11 | #include <cstring> |
| 12 |
| 13 | using namespace tgvoip; |
| 14 | using namespace tgvoip::audio; |
| 15 |
| 16 | #pragma mark - IO |
| 17 |
| 18 | AudioIOCallback::AudioIOCallback() |
| 19 | { |
| 20 | m_input = new AudioInputCallback(); |
| 21 | m_output = new AudioOutputCallback(); |
| 22 | } |
| 23 |
| 24 | AudioIOCallback::~AudioIOCallback() |
| 25 | { |
| 26 | delete m_input; |
| 27 | delete m_output; |
| 28 | } |
| 29 |
| 30 | AudioInput* AudioIOCallback::GetInput() |
| 31 | { |
| 32 | return m_input; |
| 33 | } |
| 34 |
| 35 | AudioOutput* AudioIOCallback::GetOutput() |
| 36 | { |
| 37 | return m_output; |
| 38 | } |
| 39 |
| 40 | #pragma mark - Input |
| 41 |
| 42 | AudioInputCallback::AudioInputCallback() |
| 43 | { |
| 44 | m_thread = new Thread(std::bind(&AudioInputCallback::RunThread, this)); |
| 45 | m_thread->SetName("AudioInputCallback"); |
| 46 | } |
| 47 |
| 48 | AudioInputCallback::~AudioInputCallback() |
| 49 | { |
| 50 | m_running = false; |
| 51 | m_thread->Join(); |
| 52 | delete m_thread; |
| 53 | } |
| 54 |
| 55 | void AudioInputCallback::Start() |
| 56 | { |
| 57 | if (!m_running) |
| 58 | { |
| 59 | m_running = true; |
| 60 | m_thread->Start(); |
| 61 | } |
| 62 | m_recording = true; |
| 63 | } |
| 64 |
| 65 | void AudioInputCallback::Stop() |
| 66 | { |
| 67 | m_recording = false; |
| 68 | } |
| 69 |
| 70 | void AudioInputCallback::SetDataCallback(std::function<void(std::int16_t*, std::size_t)> dataCallback) |
| 71 | { |
| 72 | m_dataCallback = std::move(dataCallback); |
| 73 | } |
| 74 |
| 75 | void AudioInputCallback::RunThread() |
| 76 | { |
| 77 | std::int16_t buf[960]; |
| 78 | while (m_running) |
| 79 | { |
| 80 | double t = VoIPController::GetCurrentTime(); |
| 81 | std::memset(buf, 0, sizeof(buf)); |
| 82 | m_dataCallback(buf, 960); |
| 83 | InvokeCallback(reinterpret_cast<std::uint8_t*>(buf), 960 * 2); |
| 84 | double sl = 0.02 - (VoIPController::GetCurrentTime() - t); |
| 85 | if (sl > 0) |
| 86 | Thread::Sleep(sl); |
| 87 | } |
| 88 | } |
| 89 |
| 90 | #pragma mark - Output |
| 91 |
| 92 | AudioOutputCallback::AudioOutputCallback() |
| 93 | { |
| 94 | m_thread = new Thread(std::bind(&AudioOutputCallback::RunThread, this)); |
| 95 | m_thread->SetName("AudioOutputCallback"); |
| 96 | } |
| 97 |
| 98 | AudioOutputCallback::~AudioOutputCallback() |
| 99 | { |
| 100 | m_running = false; |
| 101 | m_thread->Join(); |
| 102 | delete m_thread; |
| 103 | } |
| 104 |
| 105 | void AudioOutputCallback::Start() |
| 106 | { |
| 107 | if (!m_running) |
| 108 | { |
| 109 | m_running = true; |
| 110 | m_thread->Start(); |
| 111 | } |
| 112 | m_playing = true; |
| 113 | } |
| 114 |
| 115 | void AudioOutputCallback::Stop() |
| 116 | { |
| 117 | m_playing = false; |
| 118 | } |
| 119 |
| 120 | bool AudioOutputCallback::IsPlaying() |
| 121 | { |
| 122 | return m_playing; |
| 123 | } |
| 124 |
| 125 | void AudioOutputCallback::SetDataCallback(std::function<void(std::int16_t*, std::size_t)> c) |
| 126 | { |
| 127 | m_dataCallback = c; |
| 128 | } |
| 129 |
| 130 | void AudioOutputCallback::RunThread() |
| 131 | { |
| 132 | std::int16_t buf[960]; |
| 133 | while (m_running) |
| 134 | { |
| 135 | double t = VoIPController::GetCurrentTime(); |
| 136 | InvokeCallback(reinterpret_cast<std::uint8_t*>(buf), 960 * 2); |
| 137 | m_dataCallback(buf, 960); |
| 138 | double sl = 0.02 - (VoIPController::GetCurrentTime() - t); |
| 139 | if (sl > 0) |
| 140 | Thread::Sleep(sl); |
| 141 | } |
| 142 | } |
| 143 |