1 /*
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/session/media/channelmanager.h"
29 #include "talk/session/media/mediamonitor.h"
30 #include "webrtc/base/common.h"
31 
32 namespace cricket {
33 
34 enum {
35   MSG_MONITOR_POLL = 1,
36   MSG_MONITOR_START = 2,
37   MSG_MONITOR_STOP = 3,
38   MSG_MONITOR_SIGNAL = 4
39 };
40 
MediaMonitor(rtc::Thread * worker_thread,rtc::Thread * monitor_thread)41 MediaMonitor::MediaMonitor(rtc::Thread* worker_thread,
42                            rtc::Thread* monitor_thread)
43     : worker_thread_(worker_thread),
44       monitor_thread_(monitor_thread), monitoring_(false), rate_(0) {
45 }
46 
~MediaMonitor()47 MediaMonitor::~MediaMonitor() {
48   monitoring_ = false;
49   monitor_thread_->Clear(this);
50   worker_thread_->Clear(this);
51 }
52 
Start(uint32_t milliseconds)53 void MediaMonitor::Start(uint32_t milliseconds) {
54   rate_ = milliseconds;
55   if (rate_ < 100)
56     rate_ = 100;
57   worker_thread_->Post(this, MSG_MONITOR_START);
58 }
59 
Stop()60 void MediaMonitor::Stop() {
61   worker_thread_->Post(this, MSG_MONITOR_STOP);
62   rate_ = 0;
63 }
64 
OnMessage(rtc::Message * message)65 void MediaMonitor::OnMessage(rtc::Message* message) {
66   rtc::CritScope cs(&crit_);
67 
68   switch (message->message_id) {
69   case MSG_MONITOR_START:
70     ASSERT(rtc::Thread::Current() == worker_thread_);
71     if (!monitoring_) {
72       monitoring_ = true;
73       PollMediaChannel();
74     }
75     break;
76 
77   case MSG_MONITOR_STOP:
78     ASSERT(rtc::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(rtc::Thread::Current() == worker_thread_);
87     PollMediaChannel();
88     break;
89 
90   case MSG_MONITOR_SIGNAL:
91     ASSERT(rtc::Thread::Current() == monitor_thread_);
92     Update();
93     break;
94   }
95 }
96 
PollMediaChannel()97 void MediaMonitor::PollMediaChannel() {
98   rtc::CritScope cs(&crit_);
99   ASSERT(rtc::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 }
109