1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/directshow/SampleSink.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,163 @@ 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 "SampleSink.h" 1.11 +#include "AudioSinkFilter.h" 1.12 +#include "AudioSinkInputPin.h" 1.13 +#include "VideoUtils.h" 1.14 +#include "prlog.h" 1.15 + 1.16 +using namespace mozilla::media; 1.17 + 1.18 +namespace mozilla { 1.19 + 1.20 +#ifdef PR_LOGGING 1.21 +PRLogModuleInfo* GetDirectShowLog(); 1.22 +#define LOG(...) PR_LOG(GetDirectShowLog(), PR_LOG_DEBUG, (__VA_ARGS__)) 1.23 +#else 1.24 +#define LOG(...) 1.25 +#endif 1.26 + 1.27 +SampleSink::SampleSink() 1.28 + : mMonitor("SampleSink"), 1.29 + mIsFlushing(false), 1.30 + mAtEOS(false) 1.31 +{ 1.32 + MOZ_COUNT_CTOR(SampleSink); 1.33 +} 1.34 + 1.35 +SampleSink::~SampleSink() 1.36 +{ 1.37 + MOZ_COUNT_DTOR(SampleSink); 1.38 +} 1.39 + 1.40 +void 1.41 +SampleSink::SetAudioFormat(const WAVEFORMATEX* aInFormat) 1.42 +{ 1.43 + NS_ENSURE_TRUE(aInFormat, ); 1.44 + ReentrantMonitorAutoEnter mon(mMonitor); 1.45 + memcpy(&mAudioFormat, aInFormat, sizeof(WAVEFORMATEX)); 1.46 +} 1.47 + 1.48 +void 1.49 +SampleSink::GetAudioFormat(WAVEFORMATEX* aOutFormat) 1.50 +{ 1.51 + MOZ_ASSERT(aOutFormat); 1.52 + ReentrantMonitorAutoEnter mon(mMonitor); 1.53 + memcpy(aOutFormat, &mAudioFormat, sizeof(WAVEFORMATEX)); 1.54 +} 1.55 + 1.56 +HRESULT 1.57 +SampleSink::Receive(IMediaSample* aSample) 1.58 +{ 1.59 + ReentrantMonitorAutoEnter mon(mMonitor); 1.60 + 1.61 + while (true) { 1.62 + if (mIsFlushing) { 1.63 + return S_FALSE; 1.64 + } 1.65 + if (!mSample) { 1.66 + break; 1.67 + } 1.68 + if (mAtEOS) { 1.69 + return E_UNEXPECTED; 1.70 + } 1.71 + // Wait until the consumer thread consumes the sample. 1.72 + mon.Wait(); 1.73 + } 1.74 + 1.75 +#ifdef PR_LOGGING 1.76 + REFERENCE_TIME start = 0, end = 0; 1.77 + HRESULT hr = aSample->GetMediaTime(&start, &end); 1.78 + LOG("SampleSink::Receive() [%4.2lf-%4.2lf]", 1.79 + (double)RefTimeToUsecs(start) / USECS_PER_S, 1.80 + (double)RefTimeToUsecs(end) / USECS_PER_S); 1.81 +#endif 1.82 + 1.83 + mSample = aSample; 1.84 + // Notify the signal, to awaken the consumer thread in WaitForSample() 1.85 + // if necessary. 1.86 + mon.NotifyAll(); 1.87 + return S_OK; 1.88 +} 1.89 + 1.90 +HRESULT 1.91 +SampleSink::Extract(RefPtr<IMediaSample>& aOutSample) 1.92 +{ 1.93 + ReentrantMonitorAutoEnter mon(mMonitor); 1.94 + // Loop until we have a sample, or we should abort. 1.95 + while (true) { 1.96 + if (mIsFlushing) { 1.97 + return S_FALSE; 1.98 + } 1.99 + if (mSample) { 1.100 + break; 1.101 + } 1.102 + if (mAtEOS) { 1.103 + // Order is important here, if we have a sample, we should return it 1.104 + // before reporting EOS. 1.105 + return E_UNEXPECTED; 1.106 + } 1.107 + // Wait until the producer thread gives us a sample. 1.108 + mon.Wait(); 1.109 + } 1.110 + aOutSample = mSample; 1.111 + 1.112 +#ifdef PR_LOGGING 1.113 + int64_t start = 0, end = 0; 1.114 + mSample->GetMediaTime(&start, &end); 1.115 + LOG("SampleSink::Extract() [%4.2lf-%4.2lf]", 1.116 + (double)RefTimeToUsecs(start) / USECS_PER_S, 1.117 + (double)RefTimeToUsecs(end) / USECS_PER_S); 1.118 +#endif 1.119 + 1.120 + mSample = nullptr; 1.121 + // Notify the signal, to awaken the producer thread in Receive() 1.122 + // if necessary. 1.123 + mon.NotifyAll(); 1.124 + return S_OK; 1.125 +} 1.126 + 1.127 +void 1.128 +SampleSink::Flush() 1.129 +{ 1.130 + LOG("SampleSink::Flush()"); 1.131 + ReentrantMonitorAutoEnter mon(mMonitor); 1.132 + mIsFlushing = true; 1.133 + mSample = nullptr; 1.134 + mon.NotifyAll(); 1.135 +} 1.136 + 1.137 +void 1.138 +SampleSink::Reset() 1.139 +{ 1.140 + LOG("SampleSink::Reset()"); 1.141 + ReentrantMonitorAutoEnter mon(mMonitor); 1.142 + mIsFlushing = false; 1.143 + mAtEOS = false; 1.144 +} 1.145 + 1.146 +void 1.147 +SampleSink::SetEOS() 1.148 +{ 1.149 + LOG("SampleSink::SetEOS()"); 1.150 + ReentrantMonitorAutoEnter mon(mMonitor); 1.151 + mAtEOS = true; 1.152 + // Notify to unblock any threads waiting for samples in 1.153 + // Extract() or Receive(). Now that we're at EOS, no more samples 1.154 + // will come! 1.155 + mon.NotifyAll(); 1.156 +} 1.157 + 1.158 +bool 1.159 +SampleSink::AtEOS() 1.160 +{ 1.161 + ReentrantMonitorAutoEnter mon(mMonitor); 1.162 + return mAtEOS && !mSample; 1.163 +} 1.164 + 1.165 +} // namespace mozilla 1.166 +