blob: 0b1406f680f00d59e6d853425e36c2043109fb50 [file] [log] [blame]
Fredrik Solenberg2a877972017-12-15 16:42:15 +01001/*
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 Sharonova09ceed22020-09-30 18:27:39 +020014#include <memory>
Fredrik Solenberg2a877972017-12-15 16:42:15 +010015#include <vector>
16
17#include "api/audio/audio_mixer.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010019#include "common_audio/resampler/include/push_resampler.h"
Olga Sharonova09ceed22020-09-30 18:27:39 +020020#include "modules/async_audio_processing/async_audio_processing.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010021#include "modules/audio_device/include/audio_device.h"
22#include "modules/audio_processing/include/audio_processing.h"
23#include "modules/audio_processing/typing_detection.h"
Markus Handell62872802020-07-06 15:15:07 +020024#include "rtc_base/synchronization/mutex.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010025#include "rtc_base/thread_annotations.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010026
27namespace webrtc {
28
Tim Nab8c775a2020-01-10 10:33:05 -080029class AudioSender;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010030
31class AudioTransportImpl : public AudioTransport {
32 public:
Olga Sharonova09ceed22020-09-30 18:27:39 +020033 AudioTransportImpl(
34 AudioMixer* mixer,
35 AudioProcessing* audio_processing,
36 AsyncAudioProcessing::Factory* async_audio_processing_factory);
Niels Möllerde953292020-09-29 09:46:21 +020037
38 AudioTransportImpl() = delete;
39 AudioTransportImpl(const AudioTransportImpl&) = delete;
40 AudioTransportImpl& operator=(const AudioTransportImpl&) = delete;
41
Fredrik Solenberg2a877972017-12-15 16:42:15 +010042 ~AudioTransportImpl() override;
43
Olov Brändströmb732bd52022-01-28 15:07:39 +010044 // TODO(bugs.webrtc.org/13620) Deprecate this function
Fredrik Solenberg2a877972017-12-15 16:42:15 +010045 int32_t RecordedDataIsAvailable(const void* audioSamples,
Ali Tofigh62238092022-01-25 13:27:19 +010046 size_t nSamples,
47 size_t nBytesPerSample,
48 size_t nChannels,
49 uint32_t samplesPerSec,
50 uint32_t totalDelayMS,
51 int32_t clockDrift,
52 uint32_t currentMicLevel,
53 bool keyPressed,
Fredrik Solenberg2a877972017-12-15 16:42:15 +010054 uint32_t& newMicLevel) override;
55
Olov Brändströmb732bd52022-01-28 15:07:39 +010056 int32_t RecordedDataIsAvailable(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 int64_t estimated_capture_time_ns) override;
67
Ali Tofigh62238092022-01-25 13:27:19 +010068 int32_t NeedMorePlayData(size_t nSamples,
69 size_t nBytesPerSample,
70 size_t nChannels,
71 uint32_t samplesPerSec,
Fredrik Solenberg2a877972017-12-15 16:42:15 +010072 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 Nab8c775a2020-01-10 10:33:05 -080085 void UpdateAudioSenders(std::vector<AudioSender*> senders,
86 int send_sample_rate_hz,
87 size_t send_num_channels);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010088 void SetStereoChannelSwapping(bool enable);
89 bool typing_noise_detected() const;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010090
91 private:
Olga Sharonova09ceed22020-09-30 18:27:39 +020092 void SendProcessedData(std::unique_ptr<AudioFrame> audio_frame);
93
Fredrik Solenberg2a877972017-12-15 16:42:15 +010094 // Shared.
95 AudioProcessing* audio_processing_ = nullptr;
96
97 // Capture side.
Olga Sharonova09ceed22020-09-30 18:27:39 +020098
99 // Thread-safe.
100 const std::unique_ptr<AsyncAudioProcessing> async_audio_processing_;
101
Markus Handell62872802020-07-06 15:15:07 +0200102 mutable Mutex capture_lock_;
Tim Nab8c775a2020-01-10 10:33:05 -0800103 std::vector<AudioSender*> audio_senders_ RTC_GUARDED_BY(capture_lock_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100104 int send_sample_rate_hz_ RTC_GUARDED_BY(capture_lock_) = 8000;
105 size_t send_num_channels_ RTC_GUARDED_BY(capture_lock_) = 1;
106 bool typing_noise_detected_ RTC_GUARDED_BY(capture_lock_) = false;
107 bool swap_stereo_channels_ RTC_GUARDED_BY(capture_lock_) = false;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100108 PushResampler<int16_t> capture_resampler_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100109 TypingDetection typing_detection_;
110
111 // Render side.
Olga Sharonova09ceed22020-09-30 18:27:39 +0200112
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100113 rtc::scoped_refptr<AudioMixer> mixer_;
114 AudioFrame mixed_frame_;
115 // Converts mixed audio to the audio device output rate.
116 PushResampler<int16_t> render_resampler_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100117};
118} // namespace webrtc
119
120#endif // AUDIO_AUDIO_TRANSPORT_IMPL_H_