1 /*
2 * Copyright 2005 The WebRTC project authors. All Rights Reserved.
3 *
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.
9 */
10
11 #include "webrtc/base/checks.h"
12 #include "webrtc/base/common.h"
13 #include "webrtc/pc/channelmanager.h"
14 #include "webrtc/pc/mediamonitor.h"
15
16 namespace cricket {
17
18 enum {
19 MSG_MONITOR_POLL = 1,
20 MSG_MONITOR_START = 2,
21 MSG_MONITOR_STOP = 3,
22 MSG_MONITOR_SIGNAL = 4
23 };
24
MediaMonitor(rtc::Thread * worker_thread,rtc::Thread * monitor_thread)25 MediaMonitor::MediaMonitor(rtc::Thread* worker_thread,
26 rtc::Thread* monitor_thread)
27 : worker_thread_(worker_thread),
28 monitor_thread_(monitor_thread), monitoring_(false), rate_(0) {
29 }
30
~MediaMonitor()31 MediaMonitor::~MediaMonitor() {
32 monitoring_ = false;
33 monitor_thread_->Clear(this);
34 worker_thread_->Clear(this);
35 }
36
Start(uint32_t milliseconds)37 void MediaMonitor::Start(uint32_t milliseconds) {
38 rate_ = milliseconds;
39 if (rate_ < 100)
40 rate_ = 100;
41 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_START);
42 }
43
Stop()44 void MediaMonitor::Stop() {
45 worker_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_STOP);
46 rate_ = 0;
47 }
48
OnMessage(rtc::Message * message)49 void MediaMonitor::OnMessage(rtc::Message* message) {
50 rtc::CritScope cs(&crit_);
51
52 switch (message->message_id) {
53 case MSG_MONITOR_START:
54 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
55 if (!monitoring_) {
56 monitoring_ = true;
57 PollMediaChannel();
58 }
59 break;
60
61 case MSG_MONITOR_STOP:
62 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
63 if (monitoring_) {
64 monitoring_ = false;
65 worker_thread_->Clear(this);
66 }
67 break;
68
69 case MSG_MONITOR_POLL:
70 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
71 PollMediaChannel();
72 break;
73
74 case MSG_MONITOR_SIGNAL:
75 RTC_DCHECK(rtc::Thread::Current() == monitor_thread_);
76 Update();
77 break;
78 }
79 }
80
PollMediaChannel()81 void MediaMonitor::PollMediaChannel() {
82 rtc::CritScope cs(&crit_);
83 RTC_DCHECK(rtc::Thread::Current() == worker_thread_);
84
85 GetStats();
86
87 // Signal the monitoring thread, start another poll timer
88 monitor_thread_->Post(RTC_FROM_HERE, this, MSG_MONITOR_SIGNAL);
89 worker_thread_->PostDelayed(RTC_FROM_HERE, rate_, this, MSG_MONITOR_POLL);
90 }
91
92 } // namespace cricket
93