blob: 066094d159f0867fab680587cbe1531c27171e72 [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
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000011#include "webrtc/base/common.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010012#include "webrtc/pc/channelmanager.h"
13#include "webrtc/pc/mediamonitor.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;
40 worker_thread_->Post(this, MSG_MONITOR_START);
41}
42
43void MediaMonitor::Stop() {
44 worker_thread_->Post(this, MSG_MONITOR_STOP);
45 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:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000053 ASSERT(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:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000061 ASSERT(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:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069 ASSERT(rtc::Thread::Current() == worker_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 PollMediaChannel();
71 break;
72
73 case MSG_MONITOR_SIGNAL:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 ASSERT(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_);
82 ASSERT(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
87 monitor_thread_->Post(this, MSG_MONITOR_SIGNAL);
88 worker_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL);
89}
90
terelius8c011e52016-04-26 05:28:11 -070091} // namespace cricket