blob: 952ee9fbda8fa836e00b351821427ae493700104 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellander65c7f672016-02-12 08:05:012 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellander65c7f672016-02-12 08:05:014 * 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.org28e20752013-07-10 00:45:369 */
10
buildbot@webrtc.orga09a9992014-08-13 17:26:0811#include <assert.h>
kjellander@webrtc.org9b8df252016-02-12 05:47:5912#include "webrtc/pc/audiomonitor.h"
13#include "webrtc/pc/voicechannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:3614
15namespace cricket {
16
Peter Boström0c4e06b2015-10-07 10:23:2117const uint32_t MSG_MONITOR_POLL = 1;
18const uint32_t MSG_MONITOR_START = 2;
19const uint32_t MSG_MONITOR_STOP = 3;
20const uint32_t MSG_MONITOR_SIGNAL = 4;
henrike@webrtc.org28e20752013-07-10 00:45:3621
22AudioMonitor::AudioMonitor(VoiceChannel *voice_channel,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5223 rtc::Thread *monitor_thread) {
henrike@webrtc.org28e20752013-07-10 00:45:3624 voice_channel_ = voice_channel;
25 monitoring_thread_ = monitor_thread;
26 monitoring_ = false;
27}
28
29AudioMonitor::~AudioMonitor() {
30 voice_channel_->worker_thread()->Clear(this);
31 monitoring_thread_->Clear(this);
32}
33
34void 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
41void AudioMonitor::Stop() {
42 voice_channel_->worker_thread()->Post(this, MSG_MONITOR_STOP);
43}
44
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5245void AudioMonitor::OnMessage(rtc::Message *message) {
46 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:3647
48 switch (message->message_id) {
49 case MSG_MONITOR_START:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5250 assert(rtc::Thread::Current() == voice_channel_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:3651 if (!monitoring_) {
52 monitoring_ = true;
53 PollVoiceChannel();
54 }
55 break;
56
57 case MSG_MONITOR_STOP:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5258 assert(rtc::Thread::Current() == voice_channel_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:3659 if (monitoring_) {
60 monitoring_ = false;
61 voice_channel_->worker_thread()->Clear(this);
62 }
63 break;
64
65 case MSG_MONITOR_POLL:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5266 assert(rtc::Thread::Current() == voice_channel_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:3667 PollVoiceChannel();
68 break;
69
70 case MSG_MONITOR_SIGNAL:
71 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5272 assert(rtc::Thread::Current() == monitoring_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:3673 AudioInfo info = audio_info_;
74 crit_.Leave();
75 SignalUpdate(this, info);
76 crit_.Enter();
77 }
78 break;
79 }
80}
81
82void AudioMonitor::PollVoiceChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5283 rtc::CritScope cs(&crit_);
84 assert(rtc::Thread::Current() == voice_channel_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:3685
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
96VoiceChannel *AudioMonitor::voice_channel() {
97 return voice_channel_;
98}
99
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52100rtc::Thread *AudioMonitor::monitor_thread() {
henrike@webrtc.org28e20752013-07-10 00:45:36101 return monitoring_thread_;
102}
103
104}