michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_SOURCEBUFFERRESOURCE_H_ michael@0: #define MOZILLA_SOURCEBUFFERRESOURCE_H_ michael@0: michael@0: #include michael@0: #include "MediaCache.h" michael@0: #include "MediaResource.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/ReentrantMonitor.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsError.h" michael@0: #include "nsIPrincipal.h" michael@0: #include "nsStringGlue.h" michael@0: #include "nsTArray.h" michael@0: #include "nsDeque.h" michael@0: #include "nscore.h" michael@0: michael@0: class nsIStreamListener; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class MediaDecoder; michael@0: michael@0: namespace dom { michael@0: michael@0: class SourceBuffer; michael@0: michael@0: } // namespace dom michael@0: michael@0: class SourceBufferResource MOZ_FINAL : public MediaResource michael@0: { michael@0: private: michael@0: // A SourceBufferResource has a queue containing the data michael@0: // that is appended to it. The queue holds instances of michael@0: // ResourceItem which is an array of the bytes. Appending michael@0: // data to the SourceBufferResource pushes this onto the michael@0: // queue. As items are played they are taken off the front michael@0: // of the queue. michael@0: // Data is evicted once it reaches a size threshold. This michael@0: // pops the items off the front of the queue and deletes it. michael@0: // If an eviction happens then the MediaSource is notified michael@0: // (done in SourceBuffer::AppendData) which then requests michael@0: // all SourceBuffers to evict data up to approximately michael@0: // the same timepoint. michael@0: struct ResourceItem { michael@0: ResourceItem(uint8_t const* aData, uint32_t aSize) { michael@0: mData.AppendElements(aData, aSize); michael@0: } michael@0: nsTArray mData; michael@0: michael@0: size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { michael@0: // size including this michael@0: size_t size = aMallocSizeOf(this); michael@0: michael@0: // size excluding this michael@0: size += mData.SizeOfExcludingThis(aMallocSizeOf); michael@0: michael@0: return size; michael@0: } michael@0: }; michael@0: michael@0: class ResourceQueueDeallocator : public nsDequeFunctor { michael@0: virtual void* operator() (void* anObject) { michael@0: delete static_cast(anObject); michael@0: return nullptr; michael@0: } michael@0: }; michael@0: michael@0: class ResourceQueue : private nsDeque { michael@0: private: michael@0: // Logical offset into the resource of the first element michael@0: // in the queue. michael@0: uint64_t mOffset; michael@0: michael@0: public: michael@0: ResourceQueue() : michael@0: nsDeque(new ResourceQueueDeallocator()), michael@0: mOffset(0) michael@0: { michael@0: } michael@0: michael@0: // Clears all items from the queue michael@0: inline void Clear() { michael@0: return nsDeque::Erase(); michael@0: } michael@0: michael@0: // Returns the number of items in the queue michael@0: inline uint32_t GetSize() { michael@0: return nsDeque::GetSize(); michael@0: } michael@0: michael@0: // Returns the logical byte offset of the start of the data. michael@0: inline uint64_t GetOffset() { michael@0: return mOffset; michael@0: } michael@0: michael@0: inline ResourceItem* ResourceAt(uint32_t aIndex) { michael@0: return static_cast(nsDeque::ObjectAt(aIndex)); michael@0: } michael@0: michael@0: // Returns the length of all items in the queue plus the offset. michael@0: // This is the logical length of the resource. michael@0: inline uint64_t GetLength() { michael@0: uint64_t s = mOffset; michael@0: for (uint32_t i = 0; i < GetSize(); ++i) { michael@0: ResourceItem* item = ResourceAt(i); michael@0: s += item->mData.Length(); michael@0: } michael@0: return s; michael@0: } michael@0: michael@0: // Returns the index of the resource that contains the given michael@0: // logical offset. aResourceOffset will contain the offset into michael@0: // the resource at the given index returned if it is not null. If michael@0: // no such resource exists, returns GetSize() and aOffset is michael@0: // untouched. michael@0: inline uint32_t GetAtOffset(uint64_t aOffset, uint32_t *aResourceOffset) { michael@0: MOZ_ASSERT(aOffset >= mOffset); michael@0: uint64_t offset = mOffset; michael@0: for (uint32_t i = 0; i < GetSize(); ++i) { michael@0: ResourceItem* item = ResourceAt(i); michael@0: // If the item contains the start of the offset we want to michael@0: // break out of the loop. michael@0: if (item->mData.Length() + offset > aOffset) { michael@0: if (aResourceOffset) { michael@0: *aResourceOffset = aOffset - offset; michael@0: } michael@0: return i; michael@0: } michael@0: offset += item->mData.Length(); michael@0: } michael@0: return GetSize(); michael@0: } michael@0: michael@0: // Copies aCount bytes from aOffset in the queue into aDest. michael@0: inline void CopyData(uint64_t aOffset, uint32_t aCount, char* aDest) { michael@0: uint32_t offset = 0; michael@0: uint32_t start = GetAtOffset(aOffset, &offset); michael@0: uint32_t end = std::min(GetAtOffset(aOffset + aCount, nullptr) + 1, GetSize()); michael@0: for (uint32_t i = start; i < end; ++i) { michael@0: ResourceItem* item = ResourceAt(i); michael@0: uint32_t bytes = std::min(aCount, item->mData.Length() - offset); michael@0: if (bytes != 0) { michael@0: memcpy(aDest, &item->mData[offset], bytes); michael@0: offset = 0; michael@0: aCount -= bytes; michael@0: aDest += bytes; michael@0: } michael@0: } michael@0: } michael@0: michael@0: inline void PushBack(ResourceItem* aItem) { michael@0: nsDeque::Push(aItem); michael@0: } michael@0: michael@0: inline void PushFront(ResourceItem* aItem) { michael@0: nsDeque::PushFront(aItem); michael@0: } michael@0: michael@0: inline ResourceItem* PopBack() { michael@0: return static_cast(nsDeque::Pop()); michael@0: } michael@0: michael@0: inline ResourceItem* PopFront() { michael@0: return static_cast(nsDeque::PopFront()); michael@0: } michael@0: michael@0: // Evict data in queue if the total queue size is greater than michael@0: // aThreshold past the offset. Returns true if some data was michael@0: // actually evicted. michael@0: inline bool Evict(uint64_t aOffset, uint32_t aThreshold) { michael@0: bool evicted = false; michael@0: while (GetLength() - mOffset > aThreshold) { michael@0: ResourceItem* item = ResourceAt(0); michael@0: if (item->mData.Length() + mOffset > aOffset) { michael@0: break; michael@0: } michael@0: mOffset += item->mData.Length(); michael@0: delete PopFront(); michael@0: evicted = true; michael@0: } michael@0: return evicted; michael@0: } michael@0: michael@0: size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { michael@0: // Calculate the size of the internal deque. michael@0: size_t size = nsDeque::SizeOfExcludingThis(aMallocSizeOf); michael@0: michael@0: // Sum the ResourceItems. michael@0: for (int32_t i = 0; i < nsDeque::GetSize(); ++i) { michael@0: const ResourceItem* item = michael@0: static_cast(nsDeque::ObjectAt(i)); michael@0: size += item->SizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: return size; michael@0: } michael@0: }; michael@0: michael@0: public: michael@0: SourceBufferResource(nsIPrincipal* aPrincipal, michael@0: const nsACString& aType); michael@0: ~SourceBufferResource(); michael@0: michael@0: virtual nsresult Close() MOZ_OVERRIDE; michael@0: virtual void Suspend(bool aCloseImmediately) MOZ_OVERRIDE {} michael@0: virtual void Resume() MOZ_OVERRIDE {} michael@0: michael@0: virtual already_AddRefed GetCurrentPrincipal() MOZ_OVERRIDE michael@0: { michael@0: return nsCOMPtr(mPrincipal).forget(); michael@0: } michael@0: michael@0: virtual already_AddRefed CloneData(MediaDecoder* aDecoder) MOZ_OVERRIDE michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: virtual void SetReadMode(MediaCacheStream::ReadMode aMode) MOZ_OVERRIDE {} michael@0: virtual void SetPlaybackRate(uint32_t aBytesPerSecond) MOZ_OVERRIDE {} michael@0: virtual nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes) MOZ_OVERRIDE; michael@0: virtual nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) MOZ_OVERRIDE; michael@0: virtual nsresult Seek(int32_t aWhence, int64_t aOffset) MOZ_OVERRIDE; michael@0: virtual void StartSeekingForMetadata() MOZ_OVERRIDE { } michael@0: virtual void EndSeekingForMetadata() MOZ_OVERRIDE {} michael@0: virtual int64_t Tell() MOZ_OVERRIDE { return mOffset; } michael@0: virtual void Pin() MOZ_OVERRIDE {} michael@0: virtual void Unpin() MOZ_OVERRIDE {} michael@0: virtual double GetDownloadRate(bool* aIsReliable) MOZ_OVERRIDE { return 0; } michael@0: virtual int64_t GetLength() MOZ_OVERRIDE { return mInputBuffer.GetLength(); } michael@0: virtual int64_t GetNextCachedData(int64_t aOffset) MOZ_OVERRIDE { return aOffset; } michael@0: virtual int64_t GetCachedDataEnd(int64_t aOffset) MOZ_OVERRIDE { return GetLength(); } michael@0: virtual bool IsDataCachedToEndOfResource(int64_t aOffset) MOZ_OVERRIDE { return false; } michael@0: virtual bool IsSuspendedByCache() MOZ_OVERRIDE { return false; } michael@0: virtual bool IsSuspended() MOZ_OVERRIDE { return false; } michael@0: virtual nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) MOZ_OVERRIDE; michael@0: virtual bool IsTransportSeekable() MOZ_OVERRIDE { return true; } michael@0: virtual nsresult Open(nsIStreamListener** aStreamListener) MOZ_OVERRIDE { return NS_ERROR_FAILURE; } michael@0: michael@0: virtual nsresult GetCachedRanges(nsTArray& aRanges) MOZ_OVERRIDE michael@0: { michael@0: aRanges.AppendElement(MediaByteRange(mInputBuffer.GetOffset(), michael@0: mInputBuffer.GetLength())); michael@0: return NS_OK; michael@0: } michael@0: michael@0: virtual const nsCString& GetContentType() const MOZ_OVERRIDE { return mType; } michael@0: michael@0: virtual size_t SizeOfExcludingThis( michael@0: MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE michael@0: { michael@0: ReentrantMonitorAutoEnter mon(mMonitor); michael@0: michael@0: // Not owned: michael@0: // - mPrincipal michael@0: size_t size = MediaResource::SizeOfExcludingThis(aMallocSizeOf); michael@0: size += mType.SizeOfExcludingThisIfUnshared(aMallocSizeOf); michael@0: size += mInputBuffer.SizeOfExcludingThis(aMallocSizeOf); michael@0: michael@0: return size; michael@0: } michael@0: michael@0: virtual size_t SizeOfIncludingThis( michael@0: MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE michael@0: { michael@0: return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: // Used by SourceBuffer. michael@0: void AppendData(const uint8_t* aData, uint32_t aLength); michael@0: void Ended(); michael@0: // Remove data from resource if it holds more than the threshold michael@0: // number of bytes. Returns true if some data was evicted. michael@0: bool EvictData(uint32_t aThreshold); michael@0: michael@0: // Remove data from resource before the given offset. michael@0: void EvictBefore(uint64_t aOffset); michael@0: michael@0: private: michael@0: nsCOMPtr mPrincipal; michael@0: const nsAutoCString mType; michael@0: michael@0: // Provides synchronization between SourceBuffers and InputAdapters. michael@0: // Protects all of the member variables below. Read() will await a michael@0: // Notify() (from Seek, AppendData, Ended, or Close) when insufficient michael@0: // data is available in mData. michael@0: mutable ReentrantMonitor mMonitor; michael@0: michael@0: // The buffer holding resource data is a queue of ResourceItem's. michael@0: ResourceQueue mInputBuffer; michael@0: michael@0: uint64_t mOffset; michael@0: bool mClosed; michael@0: bool mEnded; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: #endif /* MOZILLA_SOURCEBUFFERRESOURCE_H_ */