Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef AUDIO_AUDIO_TRANSPORT_IMPL_H_ |
| 12 | #define AUDIO_AUDIO_TRANSPORT_IMPL_H_ |
| 13 | |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 14 | #include <memory> |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include "api/audio/audio_mixer.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 19 | #include "common_audio/resampler/include/push_resampler.h" |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 20 | #include "modules/async_audio_processing/async_audio_processing.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 21 | #include "modules/audio_device/include/audio_device.h" |
| 22 | #include "modules/audio_processing/include/audio_processing.h" |
Markus Handell | 6287280 | 2020-07-06 13:15:07 | [diff] [blame] | 23 | #include "rtc_base/synchronization/mutex.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 24 | #include "rtc_base/thread_annotations.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
Tim Na | b8c775a | 2020-01-10 18:33:05 | [diff] [blame] | 28 | class AudioSender; |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 29 | |
| 30 | class AudioTransportImpl : public AudioTransport { |
| 31 | public: |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 32 | AudioTransportImpl( |
| 33 | AudioMixer* mixer, |
| 34 | AudioProcessing* audio_processing, |
| 35 | AsyncAudioProcessing::Factory* async_audio_processing_factory); |
Niels Möller | de95329 | 2020-09-29 07:46:21 | [diff] [blame] | 36 | |
| 37 | AudioTransportImpl() = delete; |
| 38 | AudioTransportImpl(const AudioTransportImpl&) = delete; |
| 39 | AudioTransportImpl& operator=(const AudioTransportImpl&) = delete; |
| 40 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 41 | ~AudioTransportImpl() override; |
| 42 | |
Olov Brändström | b732bd5 | 2022-01-28 14:07:39 | [diff] [blame] | 43 | // TODO(bugs.webrtc.org/13620) Deprecate this function |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 44 | int32_t RecordedDataIsAvailable(const void* audioSamples, |
Ali Tofigh | 6223809 | 2022-01-25 12:27:19 | [diff] [blame] | 45 | size_t nSamples, |
| 46 | size_t nBytesPerSample, |
| 47 | size_t nChannels, |
| 48 | uint32_t samplesPerSec, |
| 49 | uint32_t totalDelayMS, |
| 50 | int32_t clockDrift, |
| 51 | uint32_t currentMicLevel, |
| 52 | bool keyPressed, |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 53 | uint32_t& newMicLevel) override; |
| 54 | |
Jakob Ivarsson | 22821de | 2023-01-20 21:09:29 | [diff] [blame] | 55 | int32_t RecordedDataIsAvailable( |
| 56 | const void* audioSamples, |
| 57 | size_t nSamples, |
| 58 | size_t nBytesPerSample, |
| 59 | size_t nChannels, |
| 60 | uint32_t samplesPerSec, |
| 61 | uint32_t totalDelayMS, |
| 62 | int32_t clockDrift, |
| 63 | uint32_t currentMicLevel, |
| 64 | bool keyPressed, |
| 65 | uint32_t& newMicLevel, |
| 66 | absl::optional<int64_t> estimated_capture_time_ns) override; |
Olov Brändström | b732bd5 | 2022-01-28 14:07:39 | [diff] [blame] | 67 | |
Ali Tofigh | 6223809 | 2022-01-25 12:27:19 | [diff] [blame] | 68 | int32_t NeedMorePlayData(size_t nSamples, |
| 69 | size_t nBytesPerSample, |
| 70 | size_t nChannels, |
| 71 | uint32_t samplesPerSec, |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 72 | void* audioSamples, |
| 73 | size_t& nSamplesOut, |
| 74 | int64_t* elapsed_time_ms, |
| 75 | int64_t* ntp_time_ms) override; |
| 76 | |
| 77 | void PullRenderData(int bits_per_sample, |
| 78 | int sample_rate, |
| 79 | size_t number_of_channels, |
| 80 | size_t number_of_frames, |
| 81 | void* audio_data, |
| 82 | int64_t* elapsed_time_ms, |
| 83 | int64_t* ntp_time_ms) override; |
| 84 | |
Tim Na | b8c775a | 2020-01-10 18:33:05 | [diff] [blame] | 85 | void UpdateAudioSenders(std::vector<AudioSender*> senders, |
| 86 | int send_sample_rate_hz, |
| 87 | size_t send_num_channels); |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 88 | void SetStereoChannelSwapping(bool enable); |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 89 | |
| 90 | private: |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 91 | void SendProcessedData(std::unique_ptr<AudioFrame> audio_frame); |
| 92 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 93 | // Shared. |
| 94 | AudioProcessing* audio_processing_ = nullptr; |
| 95 | |
| 96 | // Capture side. |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 97 | |
| 98 | // Thread-safe. |
| 99 | const std::unique_ptr<AsyncAudioProcessing> async_audio_processing_; |
| 100 | |
Markus Handell | 6287280 | 2020-07-06 13:15:07 | [diff] [blame] | 101 | mutable Mutex capture_lock_; |
Tim Na | b8c775a | 2020-01-10 18:33:05 | [diff] [blame] | 102 | std::vector<AudioSender*> audio_senders_ RTC_GUARDED_BY(capture_lock_); |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 103 | int send_sample_rate_hz_ RTC_GUARDED_BY(capture_lock_) = 8000; |
| 104 | size_t send_num_channels_ RTC_GUARDED_BY(capture_lock_) = 1; |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 105 | bool swap_stereo_channels_ RTC_GUARDED_BY(capture_lock_) = false; |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 106 | PushResampler<int16_t> capture_resampler_; |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 107 | |
| 108 | // Render side. |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 109 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 110 | rtc::scoped_refptr<AudioMixer> mixer_; |
| 111 | AudioFrame mixed_frame_; |
| 112 | // Converts mixed audio to the audio device output rate. |
| 113 | PushResampler<int16_t> render_resampler_; |
Fredrik Solenberg | 2a87797 | 2017-12-15 15:42:15 | [diff] [blame] | 114 | }; |
| 115 | } // namespace webrtc |
| 116 | |
| 117 | #endif // AUDIO_AUDIO_TRANSPORT_IMPL_H_ |