1 /* 2 * libjingle 3 * Copyright 2005--2007, 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/phone/mediamonitor.h" 30 #include "talk/session/phone/channelmanager.h" 31 #include "talk/session/phone/mediaengine.h" 32 33 namespace cricket { 34 35 enum { 36 MSG_MONITOR_POLL = 1, 37 MSG_MONITOR_START = 2, 38 MSG_MONITOR_STOP = 3, 39 MSG_MONITOR_SIGNAL = 4 40 }; 41 42 MediaMonitor::MediaMonitor(talk_base::Thread* worker_thread, 43 talk_base::Thread* monitor_thread) 44 : worker_thread_(worker_thread), 45 monitor_thread_(monitor_thread), monitoring_(false), rate_(0) { 46 } 47 48 MediaMonitor::~MediaMonitor() { 49 monitoring_ = false; 50 monitor_thread_->Clear(this); 51 worker_thread_->Clear(this); 52 } 53 54 void MediaMonitor::Start(uint32 milliseconds) { 55 rate_ = milliseconds; 56 if (rate_ < 100) 57 rate_ = 100; 58 worker_thread_->Post(this, MSG_MONITOR_START); 59 } 60 61 void MediaMonitor::Stop() { 62 worker_thread_->Post(this, MSG_MONITOR_STOP); 63 rate_ = 0; 64 } 65 66 void MediaMonitor::OnMessage(talk_base::Message* message) { 67 talk_base::CritScope cs(&crit_); 68 69 switch (message->message_id) { 70 case MSG_MONITOR_START: 71 ASSERT(talk_base::Thread::Current() == worker_thread_); 72 if (!monitoring_) { 73 monitoring_ = true; 74 PollMediaChannel(); 75 } 76 break; 77 78 case MSG_MONITOR_STOP: 79 ASSERT(talk_base::Thread::Current() == worker_thread_); 80 if (monitoring_) { 81 monitoring_ = false; 82 worker_thread_->Clear(this); 83 } 84 break; 85 86 case MSG_MONITOR_POLL: 87 ASSERT(talk_base::Thread::Current() == worker_thread_); 88 PollMediaChannel(); 89 break; 90 91 case MSG_MONITOR_SIGNAL: 92 ASSERT(talk_base::Thread::Current() == monitor_thread_); 93 Update(); 94 break; 95 } 96 } 97 98 void MediaMonitor::PollMediaChannel() { 99 talk_base::CritScope cs(&crit_); 100 ASSERT(talk_base::Thread::Current() == worker_thread_); 101 102 GetStats(); 103 104 // Signal the monitoring thread, start another poll timer 105 monitor_thread_->Post(this, MSG_MONITOR_SIGNAL); 106 worker_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL); 107 } 108 109 } 110