blob: e2e3da8b20932989770e27aaff07cd7aacb52781 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2005 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/mediamonitor.h"
12#include "pc/channelmanager.h"
13#include "rtc_base/checks.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15namespace cricket {
16
17enum {
18 MSG_MONITOR_POLL = 1,
19 MSG_MONITOR_START = 2,
20 MSG_MONITOR_STOP = 3,
21 MSG_MONITOR_SIGNAL = 4
22};
23
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024MediaMonitor::MediaMonitor(rtc::Thread* worker_thread,
25 rtc::Thread* monitor_thread)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026 : worker_thread_(worker_thread),
27 monitor_thread_(monitor_thread), monitoring_(false), rate_(0) {
28}
29
30MediaMonitor::~MediaMonitor() {
31 monitoring_ = false;
32 monitor_thread_->Clear(this);
33 worker_thread_->Clear(this);
34}
35
Peter Boström0c4e06b2015-10-07 12:23:21 +020036void MediaMonitor::Start(uint32_t milliseconds) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037 rate_ = milliseconds;
38 if (rate_ < 100)
39 rate_ = 100;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070040 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041}
42
43void MediaMonitor::Stop() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070044 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 rate_ = 0;
46}
47
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000048void MediaMonitor::OnMessage(rtc::Message* message) {
49 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51 switch (message->message_id) {
52 case MSG_MONITOR_START:
nisseede5da42017-01-12 05:15:36 -080053 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 if (!monitoring_) {
55 monitoring_ = true;
56 PollMediaChannel();
57 }
58 break;
59
60 case MSG_MONITOR_STOP:
nisseede5da42017-01-12 05:15:36 -080061 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 if (monitoring_) {
63 monitoring_ = false;
64 worker_thread_->Clear(this);
65 }
66 break;
67
68 case MSG_MONITOR_POLL:
nisseede5da42017-01-12 05:15:36 -080069 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 PollMediaChannel();
71 break;
72
73 case MSG_MONITOR_SIGNAL:
nisseede5da42017-01-12 05:15:36 -080074 RTC_DCHECK(rtc::Thread::Current() == monitor_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 Update();
76 break;
77 }
78}
79
80void MediaMonitor::PollMediaChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081 rtc::CritScope cs(&crit_);
nisseede5da42017-01-12 05:15:36 -080082 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
84 GetStats();
85
86 // Signal the monitoring thread, start another poll timer
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070087 monitor_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL);
88 worker_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089}
90
terelius8c011e52016-04-26 05:28:11 -070091} // namespace cricket