Split EventWrapper in twain.

I'm splitting the timer functions in EventWrapper into a separate interface.
- Users of the timer functions have different needs than users of a generic event
- Providing a default implementation for EventWrapper that simply uses rtc::Event.

This means that clients of WebRTC that don't use the relatively few classes, typically rendering classes, that depend on the event timer functionality, also don't pull in dependencies on multimedia timers.

R=mflodman@webrtc.org, mflodman
BUG=

Review URL: https://webrtc-codereview.appspot.com/48599004

Cr-Commit-Position: refs/heads/master@{#8833}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8833 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
tommi@webrtc.org 2015-03-23 16:25:09 +00:00
parent 82e8ae4ee8
commit 9509fbfc30
25 changed files with 119 additions and 94 deletions

View File

@ -247,14 +247,14 @@ int16_t APITest::SetUp() {
//--- EVENT TIMERS
// A
_pullEventA = EventWrapper::Create();
_pushEventA = EventWrapper::Create();
_processEventA = EventWrapper::Create();
_pullEventA = EventTimerWrapper::Create();
_pushEventA = EventTimerWrapper::Create();
_processEventA = EventTimerWrapper::Create();
_apiEventA = EventWrapper::Create();
// B
_pullEventB = EventWrapper::Create();
_pushEventB = EventWrapper::Create();
_processEventB = EventWrapper::Create();
_pullEventB = EventTimerWrapper::Create();
_pushEventB = EventTimerWrapper::Create();
_processEventB = EventTimerWrapper::Create();
_apiEventB = EventWrapper::Create();
//--- I/O params
@ -682,7 +682,7 @@ void APITest::TestDelay(char side) {
AudioCodingModule* myACM;
Channel* myChannel;
int32_t* myMinDelay;
EventWrapper* myEvent = EventWrapper::Create();
EventTimerWrapper* myEvent = EventTimerWrapper::Create();
uint32_t inTimestamp = 0;
uint32_t outTimestamp = 0;

View File

@ -109,14 +109,14 @@ class APITest : public ACMTest {
bool _writeToFile;
//--- Events
// A
EventWrapper* _pullEventA; // pulling data from ACM
EventWrapper* _pushEventA; // pushing data to ACM
EventWrapper* _processEventA; // process
EventTimerWrapper* _pullEventA; // pulling data from ACM
EventTimerWrapper* _pushEventA; // pushing data to ACM
EventTimerWrapper* _processEventA; // process
EventWrapper* _apiEventA; // API calls
// B
EventWrapper* _pullEventB; // pulling data from ACM
EventWrapper* _pushEventB; // pushing data to ACM
EventWrapper* _processEventB; // process
EventTimerWrapper* _pullEventB; // pulling data from ACM
EventTimerWrapper* _pushEventB; // pushing data to ACM
EventTimerWrapper* _processEventB; // process
EventWrapper* _apiEventB; // API calls
// keep track of the codec in either side.

View File

@ -292,7 +292,7 @@ void ISACTest::EncodeDecode(int testNr, ACMTestISACConfig& wbISACConfig,
char currentTime[500];
CodecInst sendCodec;
EventWrapper* myEvent = EventWrapper::Create();
EventTimerWrapper* myEvent = EventTimerWrapper::Create();
EXPECT_TRUE(myEvent->StartTimer(true, 10));
while (!(_inFileA.EndOfFile() || _inFileA.Rewinded())) {
Run10ms();

View File

@ -47,7 +47,7 @@ namespace webrtc {
AudioDeviceWindowsWave::AudioDeviceWindowsWave(const int32_t id) :
_ptrAudioBuffer(NULL),
_critSect(*CriticalSectionWrapper::CreateCriticalSection()),
_timeEvent(*EventWrapper::Create()),
_timeEvent(*EventTimerWrapper::Create()),
_recStartEvent(*EventWrapper::Create()),
_playStartEvent(*EventWrapper::Create()),
_hGetCaptureVolumeThread(NULL),

View File

@ -18,6 +18,7 @@
#pragma comment( lib, "winmm.lib" )
namespace webrtc {
class EventTimerWrapper;
class EventWrapper;
const uint32_t TIMER_PERIOD_MS = 2;
@ -211,7 +212,7 @@ private:
AudioDeviceBuffer* _ptrAudioBuffer;
CriticalSectionWrapper& _critSect;
EventWrapper& _timeEvent;
EventTimerWrapper& _timeEvent;
EventWrapper& _recStartEvent;
EventWrapper& _playStartEvent;

View File

@ -39,7 +39,7 @@ IncomingVideoStream::IncomingVideoStream(const int32_t module_id,
thread_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
buffer_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
incoming_render_thread_(),
deliver_buffer_event_(*EventWrapper::Create()),
deliver_buffer_event_(*EventTimerWrapper::Create()),
running_(false),
external_callback_(NULL),
render_callback_(NULL),

View File

@ -15,7 +15,7 @@
namespace webrtc {
class CriticalSectionWrapper;
class EventWrapper;
class EventTimerWrapper;
class ThreadWrapper;
class VideoRenderCallback;
class VideoRenderFrames;
@ -73,7 +73,7 @@ class IncomingVideoStream : public VideoRenderCallback {
CriticalSectionWrapper& thread_critsect_;
CriticalSectionWrapper& buffer_critsect_;
rtc::scoped_ptr<ThreadWrapper> incoming_render_thread_;
EventWrapper& deliver_buffer_event_;
EventTimerWrapper& deliver_buffer_event_;
bool running_;
VideoRenderCallback* external_callback_;

View File

@ -22,7 +22,7 @@
namespace webrtc {
class CriticalSectionWrapper;
class EventWrapper;
class EventTimerWrapper;
class VideoRenderIosGles20 {
public:
@ -63,7 +63,7 @@ class VideoRenderIosGles20 {
private:
rtc::scoped_ptr<CriticalSectionWrapper> gles_crit_sec_;
EventWrapper* screen_update_event_;
EventTimerWrapper* screen_update_event_;
rtc::scoped_ptr<ThreadWrapper> screen_update_thread_;
VideoRenderIosView* view_;

View File

@ -34,7 +34,7 @@ VideoRenderIosGles20::VideoRenderIosGles20(VideoRenderIosView* view,
is_rendering_(true) {
screen_update_thread_ = ThreadWrapper::CreateThread(
ScreenUpdateThreadProc, this, "ScreenUpdateGles20");
screen_update_event_ = EventWrapper::Create();
screen_update_event_ = EventTimerWrapper::Create();
GetWindowRect(window_rect_);
}

View File

@ -31,16 +31,13 @@
class Trace;
namespace webrtc {
class EventWrapper;
class EventTimerWrapper;
class ThreadWrapper;
class VideoRenderNSOpenGL;
class CriticalSectionWrapper;
class VideoChannelNSOpenGL : public VideoRenderCallback
{
class VideoChannelNSOpenGL : public VideoRenderCallback {
public:
VideoChannelNSOpenGL(NSOpenGLContext *nsglContext, int iId, VideoRenderNSOpenGL* owner);
virtual ~VideoChannelNSOpenGL();
@ -170,7 +167,7 @@ private: // variables
int _id;
CriticalSectionWrapper& _nsglContextCritSec;
rtc::scoped_ptr<ThreadWrapper> _screenUpdateThread;
EventWrapper* _screenUpdateEvent;
EventTimerWrapper* _screenUpdateEvent;
NSOpenGLContext* _nsglContext;
NSOpenGLContext* _nsglFullScreenContext;
CocoaFullScreenWindow* _fullScreenWindow;

View File

@ -367,7 +367,7 @@ _windowRef( (CocoaRenderView*)windowRef),
_fullScreen( fullScreen),
_id( iId),
_nsglContextCritSec( *CriticalSectionWrapper::CreateCriticalSection()),
_screenUpdateEvent( 0),
_screenUpdateEvent(EventTimerWrapper::Create()),
_nsglContext( 0),
_nsglFullScreenContext( 0),
_fullScreenWindow( nil),
@ -382,7 +382,6 @@ _windowRefSuperViewFrame(NSMakeRect(0,0,0,0))
{
_screenUpdateThread = ThreadWrapper::CreateThread(ScreenUpdateThreadProc,
this, "ScreenUpdateNSOpenGL");
_screenUpdateEvent = EventWrapper::Create();
}
int VideoRenderNSOpenGL::ChangeWindow(CocoaRenderView* newWindowRef)

View File

@ -297,7 +297,7 @@ VideoRenderDirect3D9::VideoRenderDirect3D9(Trace* trace,
{
_screenUpdateThread = ThreadWrapper::CreateThread(
ScreenUpdateThreadProc, this, "ScreenUpdateThread");
_screenUpdateEvent = EventWrapper::Create();
_screenUpdateEvent = EventTimerWrapper::Create();
SetRect(&_originalHwndRect, 0, 0, 0, 0);
}

View File

@ -27,7 +27,7 @@
namespace webrtc {
class CriticalSectionWrapper;
class EventWrapper;
class EventTimerWrapper;
class Trace;
class D3D9Channel: public VideoRenderCallback
@ -205,7 +205,7 @@ private:
CriticalSectionWrapper& _refD3DCritsect;
Trace* _trace;
rtc::scoped_ptr<ThreadWrapper> _screenUpdateThread;
EventWrapper* _screenUpdateEvent;
EventTimerWrapper* _screenUpdateEvent;
HWND _hWnd;
bool _fullScreen;

View File

@ -65,11 +65,11 @@ static_library("system_wrappers") {
"source/critical_section_win.h",
"source/data_log_c.cc",
"source/event.cc",
"source/event_posix.cc",
"source/event_posix.h",
"source/event_timer_posix.cc",
"source/event_timer_posix.h",
"source/event_timer_win.cc",
"source/event_timer_win.h",
"source/event_tracer.cc",
"source/event_win.cc",
"source/event_win.h",
"source/file_impl.cc",
"source/file_impl.h",
"source/logging.cc",

View File

@ -18,13 +18,15 @@ enum EventTypeWrapper {
kEventTimeout = 3
};
#define WEBRTC_EVENT_10_SEC 10000
#define WEBRTC_EVENT_INFINITE 0xffffffff
class EventTimerWrapper;
class EventWrapper {
public:
// Factory method. Constructor disabled.
static EventWrapper* Create();
virtual ~EventWrapper() {}
// Releases threads who are calling Wait() and has started waiting. Please
@ -43,6 +45,11 @@ class EventWrapper {
// be released. It is possible that multiple (random) threads are released
// Depending on timing.
virtual EventTypeWrapper Wait(unsigned long max_time) = 0;
};
class EventTimerWrapper : public EventWrapper {
public:
static EventTimerWrapper* Create();
// Starts a timer that will call a non-sticky version of Set() either once
// or periodically. If the timer is periodic it ensures that there is no
@ -52,6 +59,7 @@ class EventWrapper {
virtual bool StopTimer() = 0;
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_

View File

@ -12,22 +12,43 @@
#if defined(_WIN32)
#include <windows.h>
#include "webrtc/system_wrappers/source/event_win.h"
#include "webrtc/system_wrappers/source/event_timer_win.h"
#elif defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
#include <ApplicationServices/ApplicationServices.h>
#include <pthread.h>
#include "webrtc/system_wrappers/source/event_posix.h"
#include "webrtc/system_wrappers/source/event_timer_posix.h"
#else
#include <pthread.h>
#include "webrtc/system_wrappers/source/event_posix.h"
#include "webrtc/system_wrappers/source/event_timer_posix.h"
#endif
#include "webrtc/base/event.h"
namespace webrtc {
class EventWrapperImpl : public EventWrapper {
public:
EventWrapperImpl() : event_(false, false) {}
~EventWrapperImpl() override {}
bool Set() override {
event_.Set();
return true;
}
EventTypeWrapper Wait(unsigned long max_time) override {
int to_wait = max_time == WEBRTC_EVENT_INFINITE ?
rtc::Event::kForever : static_cast<int>(max_time);
return event_.Wait(to_wait) ? kEventSignaled : kEventTimeout;
}
private:
rtc::Event event_;
};
// static
EventWrapper* EventWrapper::Create() {
#if defined(_WIN32)
return new EventWindows();
#else
return EventPosix::Create();
#endif
return new EventWrapperImpl();
}
} // namespace webrtc

View File

@ -8,7 +8,7 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/system_wrappers/source/event_posix.h"
#include "webrtc/system_wrappers/source/event_timer_posix.h"
#include <errno.h>
#include <pthread.h>
@ -22,17 +22,17 @@
namespace webrtc {
// static
EventTimerWrapper* EventTimerWrapper::Create() {
return new EventTimerPosix();
}
const long int E6 = 1000000;
const long int E9 = 1000 * E6;
EventWrapper* EventPosix::Create() {
return new EventPosix();
}
EventPosix::EventPosix()
EventTimerPosix::EventTimerPosix()
: event_set_(false),
timer_thread_(nullptr),
timer_event_(0),
created_at_(),
periodic_(false),
time_(0),
@ -52,14 +52,14 @@ EventPosix::EventPosix()
#endif
}
EventPosix::~EventPosix() {
EventTimerPosix::~EventTimerPosix() {
StopTimer();
pthread_cond_destroy(&cond_);
pthread_mutex_destroy(&mutex_);
}
// TODO(pbos): Make this void.
bool EventPosix::Set() {
bool EventTimerPosix::Set() {
CHECK_EQ(0, pthread_mutex_lock(&mutex_));
event_set_ = true;
pthread_cond_signal(&cond_);
@ -67,7 +67,7 @@ bool EventPosix::Set() {
return true;
}
EventTypeWrapper EventPosix::Wait(unsigned long timeout) {
EventTypeWrapper EventTimerPosix::Wait(unsigned long timeout) {
int ret_val = 0;
CHECK_EQ(0, pthread_mutex_lock(&mutex_));
@ -115,7 +115,7 @@ EventTypeWrapper EventPosix::Wait(unsigned long timeout) {
return ret_val == 0 ? kEventSignaled : kEventTimeout;
}
EventTypeWrapper EventPosix::Wait(timespec* end_at) {
EventTypeWrapper EventTimerPosix::Wait(timespec* end_at) {
int ret_val = 0;
CHECK_EQ(0, pthread_mutex_lock(&mutex_));
@ -134,7 +134,7 @@ EventTypeWrapper EventPosix::Wait(timespec* end_at) {
return ret_val == 0 ? kEventSignaled : kEventTimeout;
}
bool EventPosix::StartTimer(bool periodic, unsigned long time) {
bool EventTimerPosix::StartTimer(bool periodic, unsigned long time) {
pthread_mutex_lock(&mutex_);
if (timer_thread_) {
if (periodic_) {
@ -152,7 +152,7 @@ bool EventPosix::StartTimer(bool periodic, unsigned long time) {
}
// Start the timer thread
timer_event_ = static_cast<EventPosix*>(EventWrapper::Create());
timer_event_.reset(new EventTimerPosix());
const char* thread_name = "WebRtc_event_timer_thread";
timer_thread_ = ThreadWrapper::CreateThread(Run, this, thread_name);
periodic_ = periodic;
@ -164,11 +164,11 @@ bool EventPosix::StartTimer(bool periodic, unsigned long time) {
return started;
}
bool EventPosix::Run(void* obj) {
return static_cast<EventPosix*>(obj)->Process();
bool EventTimerPosix::Run(void* obj) {
return static_cast<EventTimerPosix*>(obj)->Process();
}
bool EventPosix::Process() {
bool EventTimerPosix::Process() {
pthread_mutex_lock(&mutex_);
if (created_at_.tv_sec == 0) {
#ifndef WEBRTC_MAC
@ -210,7 +210,7 @@ bool EventPosix::Process() {
return true;
}
bool EventPosix::StopTimer() {
bool EventTimerPosix::StopTimer() {
if (timer_event_) {
timer_event_->Set();
}
@ -220,10 +220,7 @@ bool EventPosix::StopTimer() {
}
timer_thread_.reset();
}
if (timer_event_) {
delete timer_event_;
timer_event_ = 0;
}
timer_event_.reset();
// Set time to zero to force new reference time for the timer.
memset(&created_at_, 0, sizeof(created_at_));

View File

@ -25,11 +25,10 @@ enum State {
kDown = 2
};
class EventPosix : public EventWrapper {
class EventTimerPosix : public EventTimerWrapper {
public:
static EventWrapper* Create();
~EventPosix() override;
EventTimerPosix();
~EventTimerPosix() override;
EventTypeWrapper Wait(unsigned long max_time) override;
bool Set() override;
@ -38,8 +37,6 @@ class EventPosix : public EventWrapper {
bool StopTimer() override;
private:
EventPosix();
static bool Run(void* obj);
bool Process();
EventTypeWrapper Wait(timespec* end_at);
@ -50,7 +47,7 @@ class EventPosix : public EventWrapper {
bool event_set_;
rtc::scoped_ptr<ThreadWrapper> timer_thread_;
EventPosix* timer_event_;
rtc::scoped_ptr<EventTimerPosix> timer_event_;
timespec created_at_;
bool periodic_;

View File

@ -8,13 +8,18 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/system_wrappers/source/event_win.h"
#include "webrtc/system_wrappers/source/event_timer_win.h"
#include "Mmsystem.h"
namespace webrtc {
EventWindows::EventWindows()
// static
EventTimerWrapper* EventTimerWrapper::Create() {
return new EventTimerWin();
}
EventTimerWin::EventTimerWin()
: event_(::CreateEvent(NULL, // security attributes
FALSE, // manual reset
FALSE, // initial state
@ -22,17 +27,17 @@ EventWindows::EventWindows()
timerID_(NULL) {
}
EventWindows::~EventWindows() {
EventTimerWin::~EventTimerWin() {
StopTimer();
CloseHandle(event_);
}
bool EventWindows::Set() {
bool EventTimerWin::Set() {
// Note: setting an event that is already set has no effect.
return SetEvent(event_) == 1;
}
EventTypeWrapper EventWindows::Wait(unsigned long max_time) {
EventTypeWrapper EventTimerWin::Wait(unsigned long max_time) {
unsigned long res = WaitForSingleObject(event_, max_time);
switch (res) {
case WAIT_OBJECT_0:
@ -44,7 +49,7 @@ EventTypeWrapper EventWindows::Wait(unsigned long max_time) {
}
}
bool EventWindows::StartTimer(bool periodic, unsigned long time) {
bool EventTimerWin::StartTimer(bool periodic, unsigned long time) {
if (timerID_ != NULL) {
timeKillEvent(timerID_);
timerID_ = NULL;
@ -61,7 +66,7 @@ bool EventWindows::StartTimer(bool periodic, unsigned long time) {
return timerID_ != NULL;
}
bool EventWindows::StopTimer() {
bool EventTimerWin::StopTimer() {
if (timerID_ != NULL) {
timeKillEvent(timerID_);
timerID_ = NULL;

View File

@ -19,10 +19,10 @@
namespace webrtc {
class EventWindows : public EventWrapper {
class EventTimerWin : public EventTimerWrapper {
public:
EventWindows();
virtual ~EventWindows();
EventTimerWin();
virtual ~EventTimerWin();
virtual EventTypeWrapper Wait(unsigned long max_time);
virtual bool Set();

View File

@ -75,11 +75,11 @@
'source/data_log_c.cc',
'source/data_log_no_op.cc',
'source/event.cc',
'source/event_posix.cc',
'source/event_posix.h',
'source/event_timer_posix.cc',
'source/event_timer_posix.h',
'source/event_timer_win.cc',
'source/event_timer_win.h',
'source/event_tracer.cc',
'source/event_win.cc',
'source/event_win.h',
'source/file_impl.cc',
'source/file_impl.h',
'source/logcat_trace_context.cc',

View File

@ -30,7 +30,7 @@ FakeAudioDevice::FakeAudioDevice(Clock* clock, const std::string& filename)
playout_buffer_(),
last_playout_ms_(-1),
clock_(clock),
tick_(EventWrapper::Create()),
tick_(EventTimerWrapper::Create()),
lock_(CriticalSectionWrapper::CreateCriticalSection()),
file_utility_(new ModuleFileUtility(0)),
input_stream_(FileWrapper::Create()) {

View File

@ -20,7 +20,7 @@ namespace webrtc {
class Clock;
class CriticalSectionWrapper;
class EventWrapper;
class EventTimerWrapper;
class FileWrapper;
class ModuleFileUtility;
class ThreadWrapper;
@ -57,7 +57,7 @@ class FakeAudioDevice : public FakeAudioDeviceModule {
int64_t last_playout_ms_;
Clock* clock_;
rtc::scoped_ptr<EventWrapper> tick_;
rtc::scoped_ptr<EventTimerWrapper> tick_;
rtc::scoped_ptr<CriticalSectionWrapper> lock_;
rtc::scoped_ptr<ThreadWrapper> thread_;
rtc::scoped_ptr<ModuleFileUtility> file_utility_;

View File

@ -65,7 +65,7 @@ FrameGeneratorCapturer::FrameGeneratorCapturer(Clock* clock,
: VideoCapturer(input),
clock_(clock),
sending_(false),
tick_(EventWrapper::Create()),
tick_(EventTimerWrapper::Create()),
lock_(CriticalSectionWrapper::CreateCriticalSection()),
frame_generator_(frame_generator),
target_fps_(target_fps),

View File

@ -19,7 +19,7 @@
namespace webrtc {
class CriticalSectionWrapper;
class EventWrapper;
class EventTimerWrapper;
class ThreadWrapper;
namespace test {
@ -60,7 +60,7 @@ class FrameGeneratorCapturer : public VideoCapturer {
Clock* const clock_;
bool sending_;
rtc::scoped_ptr<EventWrapper> tick_;
rtc::scoped_ptr<EventTimerWrapper> tick_;
rtc::scoped_ptr<CriticalSectionWrapper> lock_;
rtc::scoped_ptr<ThreadWrapper> thread_;
rtc::scoped_ptr<FrameGenerator> frame_generator_;