blob: 844180eb87a9bae6bbd20882921826503bbdbd43 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2005 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/common.h"
29#include "talk/session/media/channelmanager.h"
30#include "talk/session/media/mediamonitor.h"
31
32namespace cricket {
33
34enum {
35 MSG_MONITOR_POLL = 1,
36 MSG_MONITOR_START = 2,
37 MSG_MONITOR_STOP = 3,
38 MSG_MONITOR_SIGNAL = 4
39};
40
41MediaMonitor::MediaMonitor(talk_base::Thread* worker_thread,
42 talk_base::Thread* monitor_thread)
43 : worker_thread_(worker_thread),
44 monitor_thread_(monitor_thread), monitoring_(false), rate_(0) {
45}
46
47MediaMonitor::~MediaMonitor() {
48 monitoring_ = false;
49 monitor_thread_->Clear(this);
50 worker_thread_->Clear(this);
51}
52
53void MediaMonitor::Start(uint32 milliseconds) {
54 rate_ = milliseconds;
55 if (rate_ < 100)
56 rate_ = 100;
57 worker_thread_->Post(this, MSG_MONITOR_START);
58}
59
60void MediaMonitor::Stop() {
61 worker_thread_->Post(this, MSG_MONITOR_STOP);
62 rate_ = 0;
63}
64
65void MediaMonitor::OnMessage(talk_base::Message* message) {
66 talk_base::CritScope cs(&crit_);
67
68 switch (message->message_id) {
69 case MSG_MONITOR_START:
70 ASSERT(talk_base::Thread::Current() == worker_thread_);
71 if (!monitoring_) {
72 monitoring_ = true;
73 PollMediaChannel();
74 }
75 break;
76
77 case MSG_MONITOR_STOP:
78 ASSERT(talk_base::Thread::Current() == worker_thread_);
79 if (monitoring_) {
80 monitoring_ = false;
81 worker_thread_->Clear(this);
82 }
83 break;
84
85 case MSG_MONITOR_POLL:
86 ASSERT(talk_base::Thread::Current() == worker_thread_);
87 PollMediaChannel();
88 break;
89
90 case MSG_MONITOR_SIGNAL:
91 ASSERT(talk_base::Thread::Current() == monitor_thread_);
92 Update();
93 break;
94 }
95}
96
97void MediaMonitor::PollMediaChannel() {
98 talk_base::CritScope cs(&crit_);
99 ASSERT(talk_base::Thread::Current() == worker_thread_);
100
101 GetStats();
102
103 // Signal the monitoring thread, start another poll timer
104 monitor_thread_->Post(this, MSG_MONITOR_SIGNAL);
105 worker_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL);
106}
107
108}