Kaido Kert | 08336fa | 2022-02-08 14:10:50 -0800 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "media/audio/audio_thread_impl.h" |
| 6 | |
| 7 | #include "base/message_loop/message_pump_type.h" |
| 8 | #include "base/threading/thread_task_runner_handle.h" |
| 9 | #include "base/time/default_tick_clock.h" |
| 10 | #include "build/build_config.h" |
| 11 | #include "media/audio/audio_thread_hang_monitor.h" |
| 12 | #include "third_party/abseil-cpp/absl/types/optional.h" |
| 13 | |
| 14 | namespace media { |
| 15 | |
| 16 | AudioThreadImpl::AudioThreadImpl() |
| 17 | : thread_("AudioThread"), |
| 18 | hang_monitor_(nullptr, base::OnTaskRunnerDeleter(nullptr)) { |
| 19 | base::Thread::Options thread_options; |
| 20 | #if defined(OS_WIN) |
| 21 | thread_.init_com_with_mta(true); |
| 22 | #elif defined(OS_FUCHSIA) |
| 23 | // FIDL-based APIs require async_t, which is initialized on IO thread. |
| 24 | thread_options.message_pump_type = base::MessagePumpType::IO; |
| 25 | thread_options.priority = base::ThreadPriority::REALTIME_AUDIO; |
| 26 | #endif |
| 27 | CHECK(thread_.StartWithOptions(std::move(thread_options))); |
| 28 | |
| 29 | #if defined(OS_MAC) |
| 30 | // On Mac, the audio task runner must belong to the main thread. |
| 31 | // See http://crbug.com/158170. |
| 32 | task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 33 | #else |
| 34 | task_runner_ = thread_.task_runner(); |
| 35 | #endif |
| 36 | worker_task_runner_ = thread_.task_runner(); |
| 37 | |
| 38 | #if !defined(OS_MAC) && !defined(OS_ANDROID) |
| 39 | // Since we run on the main thread on Mac, we don't need a hang monitor. |
| 40 | // https://crbug.com/946968: The hang monitor possibly causes crashes on |
| 41 | // Android |
| 42 | hang_monitor_ = AudioThreadHangMonitor::Create( |
| 43 | AudioThreadHangMonitor::HangAction::kDoNothing, absl::nullopt, |
| 44 | base::DefaultTickClock::GetInstance(), task_runner_); |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | AudioThreadImpl::~AudioThreadImpl() { |
| 49 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 50 | } |
| 51 | |
| 52 | void AudioThreadImpl::Stop() { |
| 53 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 54 | |
| 55 | hang_monitor_.reset(); |
| 56 | |
| 57 | // Note that on MACOSX, we can still have tasks posted on the |task_runner_|, |
| 58 | // since it is the main thread task runner and we do not stop the main thread. |
| 59 | // But this is fine because none of those tasks will actually run. |
| 60 | thread_.Stop(); |
| 61 | } |
| 62 | |
| 63 | bool AudioThreadImpl::IsHung() const { |
| 64 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 65 | return hang_monitor_ ? hang_monitor_->IsAudioThreadHung() : false; |
| 66 | } |
| 67 | |
| 68 | base::SingleThreadTaskRunner* AudioThreadImpl::GetTaskRunner() { |
| 69 | return task_runner_.get(); |
| 70 | } |
| 71 | |
| 72 | base::SingleThreadTaskRunner* AudioThreadImpl::GetWorkerTaskRunner() { |
| 73 | return worker_task_runner_.get(); |
| 74 | } |
| 75 | |
| 76 | } // namespace media |