blob: 9e302c4007ff258ebda9c8974b10823d753df50a [file] [log] [blame]
solenberg566ef242015-11-06 15:34:49 -08001/*
2 * Copyright (c) 2015 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef AUDIO_AUDIO_STATE_H_
12#define AUDIO_AUDIO_STATE_H_
solenberg566ef242015-11-06 15:34:49 -080013
Fredrik Solenberg2a877972017-12-15 16:42:15 +010014#include <map>
henrika5f6bf242017-11-01 11:06:56 +010015#include <memory>
Fredrik Solenbergd5247512017-12-18 22:41:03 +010016#include <unordered_set>
henrika5f6bf242017-11-01 11:06:56 +010017
Fredrik Solenberg2a877972017-12-15 16:42:15 +010018#include "audio/audio_transport_impl.h"
henrika5f6bf242017-11-01 11:06:56 +010019#include "audio/null_audio_poller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/audio_state.h"
21#include "rtc_base/constructormagic.h"
22#include "rtc_base/criticalsection.h"
Niels Möller6f72f562017-10-19 13:15:17 +020023#include "rtc_base/refcount.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread_checker.h"
solenberg566ef242015-11-06 15:34:49 -080025
26namespace webrtc {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010027
28class AudioSendStream;
Fredrik Solenbergd5247512017-12-18 22:41:03 +010029class AudioReceiveStream;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010030
solenberg566ef242015-11-06 15:34:49 -080031namespace internal {
32
solenbergfc3a2e32017-09-26 09:35:01 -070033class AudioState final : public webrtc::AudioState {
solenberg566ef242015-11-06 15:34:49 -080034 public:
35 explicit AudioState(const AudioState::Config& config);
36 ~AudioState() override;
37
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020038 AudioProcessing* audio_processing() override;
39 AudioTransport* audio_transport() override;
aleloidd310712016-11-17 06:28:59 -080040
henrika5f6bf242017-11-01 11:06:56 +010041 void SetPlayout(bool enabled) override;
42 void SetRecording(bool enabled) override;
43
Fredrik Solenberg2a877972017-12-15 16:42:15 +010044 Stats GetAudioInputStats() const override;
45 void SetStereoChannelSwapping(bool enable) override;
46
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010047 AudioDeviceModule* audio_device_module() {
48 RTC_DCHECK(config_.audio_device_module);
49 return config_.audio_device_module.get();
50 }
51
solenberg566ef242015-11-06 15:34:49 -080052 bool typing_noise_detected() const;
53
Fredrik Solenbergd5247512017-12-18 22:41:03 +010054 void AddReceivingStream(webrtc::AudioReceiveStream* stream);
55 void RemoveReceivingStream(webrtc::AudioReceiveStream* stream);
56
Fredrik Solenberg2a877972017-12-15 16:42:15 +010057 void AddSendingStream(webrtc::AudioSendStream* stream,
Yves Gerey665174f2018-06-19 15:03:05 +020058 int sample_rate_hz,
59 size_t num_channels);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010060 void RemoveSendingStream(webrtc::AudioSendStream* stream);
61
solenberg566ef242015-11-06 15:34:49 -080062 private:
63 // rtc::RefCountInterface implementation.
Niels Möller6f72f562017-10-19 13:15:17 +020064 void AddRef() const override;
65 rtc::RefCountReleaseStatus Release() const override;
solenberg566ef242015-11-06 15:34:49 -080066
Fredrik Solenberg2a877972017-12-15 16:42:15 +010067 void UpdateAudioTransportWithSendingStreams();
68
solenberg566ef242015-11-06 15:34:49 -080069 rtc::ThreadChecker thread_checker_;
70 rtc::ThreadChecker process_thread_checker_;
71 const webrtc::AudioState::Config config_;
Fredrik Solenbergaaedf752017-12-18 13:09:12 +010072 bool recording_enabled_ = true;
Fredrik Solenbergd5247512017-12-18 22:41:03 +010073 bool playout_enabled_ = true;
solenberg566ef242015-11-06 15:34:49 -080074
solenberg566ef242015-11-06 15:34:49 -080075 // Reference count; implementation copied from rtc::RefCountedObject.
Niels Möller9155e492017-10-23 11:22:30 +020076 // TODO(nisse): Use RefCountedObject or RefCountedBase instead.
solenberg566ef242015-11-06 15:34:49 -080077 mutable volatile int ref_count_ = 0;
78
aleloidd310712016-11-17 06:28:59 -080079 // Transports mixed audio from the mixer to the audio device and
Fredrik Solenberg2a877972017-12-15 16:42:15 +010080 // recorded audio to the sending streams.
81 AudioTransportImpl audio_transport_;
aleloidd310712016-11-17 06:28:59 -080082
henrika5f6bf242017-11-01 11:06:56 +010083 // Null audio poller is used to continue polling the audio streams if audio
84 // playout is disabled so that audio processing still happens and the audio
85 // stats are still updated.
86 std::unique_ptr<NullAudioPoller> null_audio_poller_;
87
Fredrik Solenbergd5247512017-12-18 22:41:03 +010088 std::unordered_set<webrtc::AudioReceiveStream*> receiving_streams_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010089 struct StreamProperties {
90 int sample_rate_hz = 0;
91 size_t num_channels = 0;
92 };
93 std::map<webrtc::AudioSendStream*, StreamProperties> sending_streams_;
94
solenberg566ef242015-11-06 15:34:49 -080095 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioState);
96};
97} // namespace internal
98} // namespace webrtc
99
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200100#endif // AUDIO_AUDIO_STATE_H_