1 /*
2  *  Copyright (c) 2012 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/system_wrappers/include/critical_section_wrapper.h"
12 #include "webrtc/system_wrappers/include/tick_util.h"
13 #include "webrtc/voice_engine/monitor_module.h"
14 
15 namespace webrtc  {
16 
17 namespace voe  {
18 
MonitorModule()19 MonitorModule::MonitorModule() :
20     _observerPtr(NULL),
21     _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
22     _lastProcessTime(TickTime::MillisecondTimestamp())
23 {
24 }
25 
~MonitorModule()26 MonitorModule::~MonitorModule()
27 {
28     delete &_callbackCritSect;
29 }
30 
31 int32_t
RegisterObserver(MonitorObserver & observer)32 MonitorModule::RegisterObserver(MonitorObserver& observer)
33 {
34     CriticalSectionScoped lock(&_callbackCritSect);
35     if (_observerPtr)
36     {
37         return -1;
38     }
39     _observerPtr = &observer;
40     return 0;
41 }
42 
43 int32_t
DeRegisterObserver()44 MonitorModule::DeRegisterObserver()
45 {
46     CriticalSectionScoped lock(&_callbackCritSect);
47     if (!_observerPtr)
48     {
49         return 0;
50     }
51     _observerPtr = NULL;
52     return 0;
53 }
54 
55 int64_t
TimeUntilNextProcess()56 MonitorModule::TimeUntilNextProcess()
57 {
58     int64_t now = TickTime::MillisecondTimestamp();
59     const int64_t kAverageProcessUpdateTimeMs = 1000;
60     return kAverageProcessUpdateTimeMs - (now - _lastProcessTime);
61 }
62 
63 int32_t
Process()64 MonitorModule::Process()
65 {
66     _lastProcessTime = TickTime::MillisecondTimestamp();
67     if (_observerPtr)
68     {
69         CriticalSectionScoped lock(&_callbackCritSect);
70         _observerPtr->OnPeriodicProcess();
71     }
72     return 0;
73 }
74 
75 }  // namespace voe
76 
77 }  // namespace webrtc
78