henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 08:05:01 | [diff] [blame] | 2 | * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 08:05:01 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 | [diff] [blame] | 11 | #include <assert.h> |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 05:47:59 | [diff] [blame] | 12 | #include "webrtc/pc/audiomonitor.h" |
| 13 | #include "webrtc/pc/voicechannel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 14 | |
| 15 | namespace cricket { |
| 16 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 17 | const uint32_t MSG_MONITOR_POLL = 1; |
| 18 | const uint32_t MSG_MONITOR_START = 2; |
| 19 | const uint32_t MSG_MONITOR_STOP = 3; |
| 20 | const uint32_t MSG_MONITOR_SIGNAL = 4; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 21 | |
| 22 | AudioMonitor::AudioMonitor(VoiceChannel *voice_channel, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 23 | rtc::Thread *monitor_thread) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 24 | voice_channel_ = voice_channel; |
| 25 | monitoring_thread_ = monitor_thread; |
| 26 | monitoring_ = false; |
| 27 | } |
| 28 | |
| 29 | AudioMonitor::~AudioMonitor() { |
| 30 | voice_channel_->worker_thread()->Clear(this); |
| 31 | monitoring_thread_->Clear(this); |
| 32 | } |
| 33 | |
| 34 | void AudioMonitor::Start(int milliseconds) { |
| 35 | rate_ = milliseconds; |
| 36 | if (rate_ < 100) |
| 37 | rate_ = 100; |
| 38 | voice_channel_->worker_thread()->Post(this, MSG_MONITOR_START); |
| 39 | } |
| 40 | |
| 41 | void AudioMonitor::Stop() { |
| 42 | voice_channel_->worker_thread()->Post(this, MSG_MONITOR_STOP); |
| 43 | } |
| 44 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 45 | void AudioMonitor::OnMessage(rtc::Message *message) { |
| 46 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 47 | |
| 48 | switch (message->message_id) { |
| 49 | case MSG_MONITOR_START: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 50 | assert(rtc::Thread::Current() == voice_channel_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 51 | if (!monitoring_) { |
| 52 | monitoring_ = true; |
| 53 | PollVoiceChannel(); |
| 54 | } |
| 55 | break; |
| 56 | |
| 57 | case MSG_MONITOR_STOP: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 58 | assert(rtc::Thread::Current() == voice_channel_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 59 | if (monitoring_) { |
| 60 | monitoring_ = false; |
| 61 | voice_channel_->worker_thread()->Clear(this); |
| 62 | } |
| 63 | break; |
| 64 | |
| 65 | case MSG_MONITOR_POLL: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 66 | assert(rtc::Thread::Current() == voice_channel_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 67 | PollVoiceChannel(); |
| 68 | break; |
| 69 | |
| 70 | case MSG_MONITOR_SIGNAL: |
| 71 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 72 | assert(rtc::Thread::Current() == monitoring_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 73 | AudioInfo info = audio_info_; |
| 74 | crit_.Leave(); |
| 75 | SignalUpdate(this, info); |
| 76 | crit_.Enter(); |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void AudioMonitor::PollVoiceChannel() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 83 | rtc::CritScope cs(&crit_); |
| 84 | assert(rtc::Thread::Current() == voice_channel_->worker_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 85 | |
| 86 | // Gather connection infos |
| 87 | audio_info_.input_level = voice_channel_->GetInputLevel_w(); |
| 88 | audio_info_.output_level = voice_channel_->GetOutputLevel_w(); |
| 89 | voice_channel_->GetActiveStreams_w(&audio_info_.active_streams); |
| 90 | |
| 91 | // Signal the monitoring thread, start another poll timer |
| 92 | monitoring_thread_->Post(this, MSG_MONITOR_SIGNAL); |
| 93 | voice_channel_->worker_thread()->PostDelayed(rate_, this, MSG_MONITOR_POLL); |
| 94 | } |
| 95 | |
| 96 | VoiceChannel *AudioMonitor::voice_channel() { |
| 97 | return voice_channel_; |
| 98 | } |
| 99 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 100 | rtc::Thread *AudioMonitor::monitor_thread() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 101 | return monitoring_thread_; |
| 102 | } |
| 103 | |
| 104 | } |