1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/mediasource/SourceBufferResource.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 "SourceBufferResource.h" 1.11 + 1.12 +#include <string.h> 1.13 +#include <algorithm> 1.14 + 1.15 +#include "nsISeekableStream.h" 1.16 +#include "nsISupportsImpl.h" 1.17 +#include "prenv.h" 1.18 +#include "prlog.h" 1.19 + 1.20 +#ifdef PR_LOGGING 1.21 +extern PRLogModuleInfo* gMediaSourceLog; 1.22 +#define MSE_DEBUG(...) PR_LOG(gMediaSourceLog, PR_LOG_DEBUG, (__VA_ARGS__)) 1.23 +#else 1.24 +#define MSE_DEBUG(...) 1.25 +#endif 1.26 + 1.27 +namespace mozilla { 1.28 + 1.29 +namespace dom { 1.30 + 1.31 +class SourceBuffer; 1.32 + 1.33 +} // namespace dom 1.34 + 1.35 +nsresult 1.36 +SourceBufferResource::Close() 1.37 +{ 1.38 + ReentrantMonitorAutoEnter mon(mMonitor); 1.39 + MSE_DEBUG("%p SBR::Close", this); 1.40 + //MOZ_ASSERT(!mClosed); 1.41 + mClosed = true; 1.42 + mon.NotifyAll(); 1.43 + return NS_OK; 1.44 +} 1.45 + 1.46 +nsresult 1.47 +SourceBufferResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes) 1.48 +{ 1.49 + ReentrantMonitorAutoEnter mon(mMonitor); 1.50 + bool blockingRead = !!aBytes; 1.51 + 1.52 + while (blockingRead && 1.53 + !mEnded && 1.54 + mOffset + aCount > static_cast<uint64_t>(GetLength())) { 1.55 + MSE_DEBUG("%p SBR::Read waiting for data", this); 1.56 + mon.Wait(); 1.57 + } 1.58 + 1.59 + uint32_t available = GetLength() - mOffset; 1.60 + uint32_t count = std::min(aCount, available); 1.61 + if (!PR_GetEnv("MOZ_QUIET")) { 1.62 + MSE_DEBUG("%p SBR::Read aCount=%u length=%u offset=%u " 1.63 + "available=%u count=%u, blocking=%d bufComplete=%d", 1.64 + this, aCount, GetLength(), mOffset, available, count, 1.65 + blockingRead, mEnded); 1.66 + } 1.67 + if (available == 0) { 1.68 + MSE_DEBUG("%p SBR::Read EOF", this); 1.69 + *aBytes = 0; 1.70 + return NS_OK; 1.71 + } 1.72 + 1.73 + mInputBuffer.CopyData(mOffset, count, aBuffer); 1.74 + *aBytes = count; 1.75 + mOffset += count; 1.76 + return NS_OK; 1.77 +} 1.78 + 1.79 +nsresult 1.80 +SourceBufferResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) 1.81 +{ 1.82 + ReentrantMonitorAutoEnter mon(mMonitor); 1.83 + nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset); 1.84 + if (NS_FAILED(rv)) { 1.85 + return rv; 1.86 + } 1.87 + return Read(aBuffer, aCount, aBytes); 1.88 +} 1.89 + 1.90 +nsresult 1.91 +SourceBufferResource::Seek(int32_t aWhence, int64_t aOffset) 1.92 +{ 1.93 + ReentrantMonitorAutoEnter mon(mMonitor); 1.94 + if (mClosed) { 1.95 + return NS_ERROR_FAILURE; 1.96 + } 1.97 + 1.98 + int64_t newOffset = mOffset; 1.99 + switch (aWhence) { 1.100 + case nsISeekableStream::NS_SEEK_END: 1.101 + newOffset = GetLength() - aOffset; 1.102 + break; 1.103 + case nsISeekableStream::NS_SEEK_CUR: 1.104 + newOffset += aOffset; 1.105 + break; 1.106 + case nsISeekableStream::NS_SEEK_SET: 1.107 + newOffset = aOffset; 1.108 + break; 1.109 + } 1.110 + 1.111 + if (newOffset < 0 || uint64_t(newOffset) < mInputBuffer.GetOffset() || newOffset > GetLength()) { 1.112 + return NS_ERROR_FAILURE; 1.113 + } 1.114 + 1.115 + mOffset = newOffset; 1.116 + mon.NotifyAll(); 1.117 + 1.118 + return NS_OK; 1.119 +} 1.120 + 1.121 +nsresult 1.122 +SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) 1.123 +{ 1.124 + ReentrantMonitorAutoEnter mon(mMonitor); 1.125 + nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset); 1.126 + if (NS_FAILED(rv)) { 1.127 + return rv; 1.128 + } 1.129 + return Read(aBuffer, aCount, nullptr); 1.130 +} 1.131 + 1.132 +bool 1.133 +SourceBufferResource::EvictData(uint32_t aThreshold) 1.134 +{ 1.135 + return mInputBuffer.Evict(mOffset, aThreshold); 1.136 +} 1.137 + 1.138 +void 1.139 +SourceBufferResource::EvictBefore(uint64_t aOffset) 1.140 +{ 1.141 + // If aOffset is past the current playback offset we don't evict. 1.142 + if (aOffset < mOffset) { 1.143 + mInputBuffer.Evict(aOffset, 0); 1.144 + } 1.145 +} 1.146 + 1.147 +void 1.148 +SourceBufferResource::AppendData(const uint8_t* aData, uint32_t aLength) 1.149 +{ 1.150 + ReentrantMonitorAutoEnter mon(mMonitor); 1.151 + mInputBuffer.PushBack(new ResourceItem(aData, aLength)); 1.152 + mon.NotifyAll(); 1.153 +} 1.154 + 1.155 +void 1.156 +SourceBufferResource::Ended() 1.157 +{ 1.158 + ReentrantMonitorAutoEnter mon(mMonitor); 1.159 + mEnded = true; 1.160 + mon.NotifyAll(); 1.161 +} 1.162 + 1.163 +SourceBufferResource::~SourceBufferResource() 1.164 +{ 1.165 + MOZ_COUNT_DTOR(SourceBufferResource); 1.166 + MSE_DEBUG("%p SBR::~SBR", this); 1.167 +} 1.168 + 1.169 +SourceBufferResource::SourceBufferResource(nsIPrincipal* aPrincipal, 1.170 + const nsACString& aType) 1.171 + : mPrincipal(aPrincipal) 1.172 + , mType(aType) 1.173 + , mMonitor("mozilla::SourceBufferResource::mMonitor") 1.174 + , mOffset(0) 1.175 + , mClosed(false) 1.176 + , mEnded(false) 1.177 +{ 1.178 + MOZ_COUNT_CTOR(SourceBufferResource); 1.179 + MSE_DEBUG("%p SBR::SBR()", this); 1.180 +} 1.181 + 1.182 +} // namespace mozilla