From 05c1a838c881ea502888659848d8792843b28718 Mon Sep 17 00:00:00 2001 From: Luca Muscariello Date: Sat, 25 Feb 2017 23:42:31 +0100 Subject: Initial commit: video player - viper Change-Id: Id5aa33598ce34659bad4a7a9ae5006bfb84f9bd1 Signed-off-by: Luca Muscariello --- libdash/source/network/DownloadStateManager.cpp | 100 ++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 libdash/source/network/DownloadStateManager.cpp (limited to 'libdash/source/network/DownloadStateManager.cpp') diff --git a/libdash/source/network/DownloadStateManager.cpp b/libdash/source/network/DownloadStateManager.cpp new file mode 100644 index 00000000..5117c099 --- /dev/null +++ b/libdash/source/network/DownloadStateManager.cpp @@ -0,0 +1,100 @@ +/* + * DownloadStateManager.cpp + ***************************************************************************** + * Copyright (C) 2012, bitmovin Softwareentwicklung OG, All Rights Reserved + * + * Email: libdash-dev@vicky.bitmovin.net + * + * This source code and its use and distribution, is subject to the terms + * and conditions of the applicable license agreement. + *****************************************************************************/ + +#include "DownloadStateManager.h" + +using namespace dash::network; + +DownloadStateManager::DownloadStateManager () : + state (NOT_STARTED) +{ + InitializeConditionVariable (&this->stateChanged); + InitializeCriticalSection (&this->stateLock); +} +DownloadStateManager::~DownloadStateManager () +{ + DeleteConditionVariable (&this->stateChanged); + DeleteCriticalSection (&this->stateLock); +} + +DownloadState DownloadStateManager::State () const +{ + EnterCriticalSection(&this->stateLock); + + DownloadState ret = this->state; + + LeaveCriticalSection(&this->stateLock); + + return ret; +} +void DownloadStateManager::State (DownloadState state) +{ + EnterCriticalSection(&this->stateLock); + + this->state = state; + + this->Notify(); + WakeAllConditionVariable(&this->stateChanged); + LeaveCriticalSection(&this->stateLock); +} +void DownloadStateManager::WaitState (DownloadState state) const +{ + EnterCriticalSection(&this->stateLock); + + while(this->state != state) + SleepConditionVariableCS(&this->stateChanged, &this->stateLock, INFINITE); + + LeaveCriticalSection(&this->stateLock); +} +void DownloadStateManager::CheckAndWait (DownloadState check, DownloadState wait) const +{ + EnterCriticalSection(&this->stateLock); + + if(this->state == check) + while(this->state != wait) + SleepConditionVariableCS(&this->stateChanged, &this->stateLock, INFINITE); + + LeaveCriticalSection(&this->stateLock); +} +void DownloadStateManager::Attach (IDownloadObserver *observer) +{ + EnterCriticalSection(&this->stateLock); + this->observers.push_back(observer); + LeaveCriticalSection(&this->stateLock); +} +void DownloadStateManager::Detach (IDownloadObserver *observer) +{ + EnterCriticalSection(&this->stateLock); + + uint32_t pos = -1; + + for(size_t i = 0; i < this->observers.size(); i++) + if(this->observers.at(i) == observer) + pos = i; + + if(pos != -1) + this->observers.erase(this->observers.begin() + pos); + + LeaveCriticalSection(&this->stateLock); +} +void DownloadStateManager::Notify () +{ + for(size_t i = 0; i < this->observers.size(); i++) + this->observers.at(i)->OnDownloadStateChanged(this->state); +} +void DownloadStateManager::CheckAndSet (DownloadState check, DownloadState set) +{ + EnterCriticalSection(&this->stateLock); + + if(this->state == check) + this->state = set; + LeaveCriticalSection(&this->stateLock); +} -- cgit 1.2.3-korg