blob: f3ca2fa848972955243af4f9d7eab9828d062055 [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
44 int32_t RecordedDataIsAvailable(const void* audioSamples,
45 const size_t nSamples,
46 const size_t nBytesPerSample,
47 const size_t nChannels,
48 const uint32_t samplesPerSec,
49 const uint32_t totalDelayMS,
50 const int32_t clockDrift,
51 const uint32_t currentMicLevel,
52 const bool keyPressed,
53 uint32_t& newMicLevel) override;
54
55 int32_t NeedMorePlayData(const size_t nSamples,
56 const size_t nBytesPerSample,
57 const size_t nChannels,
58 const uint32_t samplesPerSec,
59 void* audioSamples,
60 size_t& nSamplesOut,
61 int64_t* elapsed_time_ms,
62 int64_t* ntp_time_ms) override;
63
64 void PullRenderData(int bits_per_sample,
65 int sample_rate,
66 size_t number_of_channels,
67 size_t number_of_frames,
68 void* audio_data,
69 int64_t* elapsed_time_ms,
70 int64_t* ntp_time_ms) override;
71
Tim Nab8c775a2020-01-10 10:33:05 -080072 void UpdateAudioSenders(std::vector<AudioSender*> senders,
73 int send_sample_rate_hz,
74 size_t send_num_channels);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010075 void SetStereoChannelSwapping(bool enable);
76 bool typing_noise_detected() const;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010077
78 private:
Olga Sharonova09ceed22020-09-30 18:27:39 +020079 void SendProcessedData(std::unique_ptr<AudioFrame> audio_frame);
80
Fredrik Solenberg2a877972017-12-15 16:42:15 +010081 // Shared.
82 AudioProcessing* audio_processing_ = nullptr;
83
84 // Capture side.
Olga Sharonova09ceed22020-09-30 18:27:39 +020085
86 // Thread-safe.
87 const std::unique_ptr<AsyncAudioProcessing> async_audio_processing_;
88
Markus Handell62872802020-07-06 15:15:07 +020089 mutable Mutex capture_lock_;
Tim Nab8c775a2020-01-10 10:33:05 -080090 std::vector<AudioSender*> audio_senders_ RTC_GUARDED_BY(capture_lock_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010091 int send_sample_rate_hz_ RTC_GUARDED_BY(capture_lock_) = 8000;
92 size_t send_num_channels_ RTC_GUARDED_BY(capture_lock_) = 1;
93 bool typing_noise_detected_ RTC_GUARDED_BY(capture_lock_) = false;
94 bool swap_stereo_channels_ RTC_GUARDED_BY(capture_lock_) = false;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010095 PushResampler<int16_t> capture_resampler_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010096 TypingDetection typing_detection_;
97
98 // Render side.
Olga Sharonova09ceed22020-09-30 18:27:39 +020099
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100100 rtc::scoped_refptr<AudioMixer> mixer_;
101 AudioFrame mixed_frame_;
102 // Converts mixed audio to the audio device output rate.
103 PushResampler<int16_t> render_resampler_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100104};
105} // namespace webrtc
106
107#endif // AUDIO_AUDIO_TRANSPORT_IMPL_H_