10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2005 Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/session/media/channelmanager.h"
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/session/media/mediamonitor.h"
30cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/common.h"
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum {
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  MSG_MONITOR_POLL = 1,
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  MSG_MONITOR_START = 2,
370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  MSG_MONITOR_STOP = 3,
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  MSG_MONITOR_SIGNAL = 4
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
412a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgMediaMonitor::MediaMonitor(rtc::Thread* worker_thread,
422a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org                           rtc::Thread* monitor_thread)
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    : worker_thread_(worker_thread),
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      monitor_thread_(monitor_thread), monitoring_(false), rate_(0) {
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgMediaMonitor::~MediaMonitor() {
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  monitoring_ = false;
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  monitor_thread_->Clear(this);
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  worker_thread_->Clear(this);
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MediaMonitor::Start(uint32 milliseconds) {
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  rate_ = milliseconds;
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (rate_ < 100)
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    rate_ = 100;
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  worker_thread_->Post(this, MSG_MONITOR_START);
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MediaMonitor::Stop() {
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  worker_thread_->Post(this, MSG_MONITOR_STOP);
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  rate_ = 0;
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
652a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgvoid MediaMonitor::OnMessage(rtc::Message* message) {
662a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::CritScope cs(&crit_);
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  switch (message->message_id) {
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  case MSG_MONITOR_START:
702a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    ASSERT(rtc::Thread::Current() == worker_thread_);
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (!monitoring_) {
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      monitoring_ = true;
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      PollMediaChannel();
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    break;
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  case MSG_MONITOR_STOP:
782a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    ASSERT(rtc::Thread::Current() == worker_thread_);
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (monitoring_) {
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      monitoring_ = false;
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      worker_thread_->Clear(this);
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    break;
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  case MSG_MONITOR_POLL:
862a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    ASSERT(rtc::Thread::Current() == worker_thread_);
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    PollMediaChannel();
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    break;
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  case MSG_MONITOR_SIGNAL:
912a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    ASSERT(rtc::Thread::Current() == monitor_thread_);
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    Update();
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    break;
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MediaMonitor::PollMediaChannel() {
982a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::CritScope cs(&crit_);
992a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  ASSERT(rtc::Thread::Current() == worker_thread_);
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  GetStats();
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Signal the monitoring thread, start another poll timer
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  monitor_thread_->Post(this, MSG_MONITOR_SIGNAL);
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  worker_thread_->PostDelayed(rate_, this, MSG_MONITOR_POLL);
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
109