1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/wmf/WMFSourceReaderCallback.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,157 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "WMFSourceReaderCallback.h" 1.11 +#include "WMFUtils.h" 1.12 + 1.13 +namespace mozilla { 1.14 + 1.15 +#ifdef PR_LOGGING 1.16 +static PRLogModuleInfo* gWMFSourceReaderCallbackLog = nullptr; 1.17 +#define WMF_CB_LOG(...) PR_LOG(gWMFSourceReaderCallbackLog, PR_LOG_DEBUG, (__VA_ARGS__)) 1.18 +#else 1.19 +#define WMF_CB_LOG(...) 1.20 +#endif 1.21 + 1.22 +// IUnknown Methods 1.23 +STDMETHODIMP 1.24 +WMFSourceReaderCallback::QueryInterface(REFIID aIId, void **aInterface) 1.25 +{ 1.26 + WMF_CB_LOG("WMFSourceReaderCallback::QueryInterface %s", GetGUIDName(aIId).get()); 1.27 + 1.28 + if (aIId == IID_IMFSourceReaderCallback) { 1.29 + return DoGetInterface(static_cast<WMFSourceReaderCallback*>(this), aInterface); 1.30 + } 1.31 + if (aIId == IID_IUnknown) { 1.32 + return DoGetInterface(static_cast<WMFSourceReaderCallback*>(this), aInterface); 1.33 + } 1.34 + 1.35 + *aInterface = nullptr; 1.36 + return E_NOINTERFACE; 1.37 +} 1.38 + 1.39 +NS_IMPL_ADDREF(WMFSourceReaderCallback) 1.40 +NS_IMPL_RELEASE(WMFSourceReaderCallback) 1.41 + 1.42 +WMFSourceReaderCallback::WMFSourceReaderCallback() 1.43 + : mMonitor("WMFSourceReaderCallback") 1.44 + , mResultStatus(S_OK) 1.45 + , mStreamFlags(0) 1.46 + , mTimestamp(0) 1.47 + , mSample(nullptr) 1.48 + , mReadFinished(false) 1.49 +{ 1.50 +#ifdef PR_LOGGING 1.51 + if (!gWMFSourceReaderCallbackLog) { 1.52 + gWMFSourceReaderCallbackLog = PR_NewLogModule("WMFSourceReaderCallback"); 1.53 + } 1.54 +#endif 1.55 +} 1.56 + 1.57 +HRESULT 1.58 +WMFSourceReaderCallback::NotifyReadComplete(HRESULT aReadStatus, 1.59 + DWORD aStreamIndex, 1.60 + DWORD aStreamFlags, 1.61 + LONGLONG aTimestamp, 1.62 + IMFSample *aSample) 1.63 +{ 1.64 + // Note: aSample can be nullptr on success if more data is required! 1.65 + ReentrantMonitorAutoEnter mon(mMonitor); 1.66 + 1.67 + if (mSample) { 1.68 + // The WMFReader should have called Wait() to retrieve the last 1.69 + // sample returned by the last ReadSample() call, but if we're 1.70 + // aborting the read before Wait() is called the sample ref 1.71 + // can be non-null. 1.72 + mSample->Release(); 1.73 + mSample = nullptr; 1.74 + } 1.75 + 1.76 + if (SUCCEEDED(aReadStatus)) { 1.77 + if (aSample) { 1.78 + mTimestamp = aTimestamp; 1.79 + mSample = aSample; 1.80 + mSample->AddRef(); 1.81 + } 1.82 + } 1.83 + 1.84 + mResultStatus = aReadStatus; 1.85 + mStreamFlags = aStreamFlags; 1.86 + 1.87 + // Set the sentinal value and notify the monitor, so that threads waiting 1.88 + // in Wait() are awoken. 1.89 + mReadFinished = true; 1.90 + mon.NotifyAll(); 1.91 + 1.92 + return S_OK; 1.93 +} 1.94 + 1.95 +STDMETHODIMP 1.96 +WMFSourceReaderCallback::OnReadSample(HRESULT aReadStatus, 1.97 + DWORD aStreamIndex, 1.98 + DWORD aStreamFlags, 1.99 + LONGLONG aTimestamp, 1.100 + IMFSample *aSample) 1.101 +{ 1.102 + WMF_CB_LOG("WMFSourceReaderCallback::OnReadSample() hr=0x%x flags=0x%x time=%lld sample=%p", 1.103 + aReadStatus, aStreamFlags, aTimestamp, aSample); 1.104 + return NotifyReadComplete(aReadStatus, 1.105 + aStreamIndex, 1.106 + aStreamFlags, 1.107 + aTimestamp, 1.108 + aSample); 1.109 +} 1.110 + 1.111 +HRESULT 1.112 +WMFSourceReaderCallback::Cancel() 1.113 +{ 1.114 + WMF_CB_LOG("WMFSourceReaderCallback::Cancel()"); 1.115 + return NotifyReadComplete(E_ABORT, 1.116 + 0, 1.117 + 0, 1.118 + 0, 1.119 + nullptr); 1.120 +} 1.121 + 1.122 +STDMETHODIMP 1.123 +WMFSourceReaderCallback::OnEvent(DWORD, IMFMediaEvent *) 1.124 +{ 1.125 + return S_OK; 1.126 +} 1.127 + 1.128 +STDMETHODIMP 1.129 +WMFSourceReaderCallback::OnFlush(DWORD) 1.130 +{ 1.131 + return S_OK; 1.132 +} 1.133 + 1.134 +HRESULT 1.135 +WMFSourceReaderCallback::Wait(DWORD* aStreamFlags, 1.136 + LONGLONG* aTimeStamp, 1.137 + IMFSample** aSample) 1.138 +{ 1.139 + ReentrantMonitorAutoEnter mon(mMonitor); 1.140 + WMF_CB_LOG("WMFSourceReaderCallback::Wait() starting wait"); 1.141 + while (!mReadFinished) { 1.142 + mon.Wait(); 1.143 + } 1.144 + mReadFinished = false; 1.145 + WMF_CB_LOG("WMFSourceReaderCallback::Wait() done waiting"); 1.146 + 1.147 + *aStreamFlags = mStreamFlags; 1.148 + *aTimeStamp = mTimestamp; 1.149 + *aSample = mSample; 1.150 + HRESULT hr = mResultStatus; 1.151 + 1.152 + mSample = nullptr; 1.153 + mTimestamp = 0; 1.154 + mStreamFlags = 0; 1.155 + mResultStatus = S_OK; 1.156 + 1.157 + return hr; 1.158 +} 1.159 + 1.160 +} // namespace mozilla