henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2005 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [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 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/mediamonitor.h" |
| 12 | #include "pc/channelmanager.h" |
| 13 | #include "rtc_base/checks.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | |
| 15 | namespace cricket { |
| 16 | |
| 17 | enum { |
| 18 | MSG_MONITOR_POLL = 1, |
| 19 | MSG_MONITOR_START = 2, |
| 20 | MSG_MONITOR_STOP = 3, |
| 21 | MSG_MONITOR_SIGNAL = 4 |
| 22 | }; |
| 23 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 24 | MediaMonitor::MediaMonitor(rtc::Thread* worker_thread, |
| 25 | rtc::Thread* monitor_thread) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | : worker_thread_(worker_thread), |
| 27 | monitor_thread_(monitor_thread), monitoring_(false), rate_(0) { |
| 28 | } |
| 29 | |
| 30 | MediaMonitor::~MediaMonitor() { |
| 31 | monitoring_ = false; |
| 32 | monitor_thread_->Clear(this); |
| 33 | worker_thread_->Clear(this); |
| 34 | } |
| 35 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 36 | void MediaMonitor::Start(uint32_t milliseconds) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | rate_ = milliseconds; |
| 38 | if (rate_ < 100) |
| 39 | rate_ = 100; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 40 | worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void MediaMonitor::Stop() { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 44 | worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | rate_ = 0; |
| 46 | } |
| 47 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 48 | void MediaMonitor::OnMessage(rtc::Message* message) { |
| 49 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | |
| 51 | switch (message->message_id) { |
| 52 | case MSG_MONITOR_START: |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 53 | RTC_DCHECK(rtc::Thread::Current() == worker_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | if (!monitoring_) { |
| 55 | monitoring_ = true; |
| 56 | PollMediaChannel(); |
| 57 | } |
| 58 | break; |
| 59 | |
| 60 | case MSG_MONITOR_STOP: |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 61 | RTC_DCHECK(rtc::Thread::Current() == worker_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | if (monitoring_) { |
| 63 | monitoring_ = false; |
| 64 | worker_thread_->Clear(this); |
| 65 | } |
| 66 | break; |
| 67 | |
| 68 | case MSG_MONITOR_POLL: |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 69 | RTC_DCHECK(rtc::Thread::Current() == worker_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | PollMediaChannel(); |
| 71 | break; |
| 72 | |
| 73 | case MSG_MONITOR_SIGNAL: |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 74 | RTC_DCHECK(rtc::Thread::Current() == monitor_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | Update(); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void MediaMonitor::PollMediaChannel() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 81 | rtc::CritScope cs(&crit_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 82 | RTC_DCHECK(rtc::Thread::Current() == worker_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | |
| 84 | GetStats(); |
| 85 | |
| 86 | // Signal the monitoring thread, start another poll timer |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 87 | monitor_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL); |
| 88 | worker_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
terelius | 8c011e5 | 2016-04-26 05:28:11 -0700 | [diff] [blame] | 91 | } // namespace cricket |