ResourceQueue.h (2796B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MOZILLA_RESOURCEQUEUE_H_ 8 #define MOZILLA_RESOURCEQUEUE_H_ 9 10 #include "MediaSpan.h" 11 #include "nsDeque.h" 12 13 namespace mozilla { 14 15 class ErrorResult; 16 17 // A SourceBufferResource has a queue containing the data that is appended 18 // to it. The queue holds instances of ResourceItem which is an array of the 19 // bytes. Appending data to the SourceBufferResource pushes this onto the 20 // queue. 21 22 // Data is evicted once it reaches a size threshold. This pops the items off 23 // the front of the queue and deletes it. If an eviction happens then the 24 // MediaSource is notified (done in SourceBuffer::AppendData) which then 25 // requests all SourceBuffers to evict data up to approximately the same 26 // timepoint. 27 28 struct ResourceItem { 29 ResourceItem(const MediaSpan& aData, uint64_t aOffset); 30 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const; 31 MediaSpan mData; 32 uint64_t mOffset; 33 }; 34 35 class ResourceQueue : private nsDeque<ResourceItem> { 36 public: 37 ResourceQueue(); 38 39 // Returns the logical byte offset of the start of the data. 40 uint64_t GetOffset(); 41 42 // Returns the length of all items in the queue plus the offset. 43 // This is the logical length of the resource. 44 uint64_t GetLength(); 45 46 // Copies aCount bytes from aOffset in the queue into aDest. 47 void CopyData(uint64_t aOffset, uint32_t aCount, char* aDest); 48 49 void AppendItem(const MediaSpan& aData); 50 51 // Tries to evict at least aSizeToEvict from the queue up until 52 // aOffset. Returns amount evicted. 53 uint32_t Evict(uint64_t aOffset, uint32_t aSizeToEvict); 54 55 uint32_t EvictBefore(uint64_t aOffset); 56 57 uint32_t EvictAll(); 58 59 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const; 60 61 #if defined(DEBUG) 62 void Dump(const char* aPath); 63 #endif 64 65 const uint8_t* GetContiguousAccess(int64_t aOffset, size_t aSize); 66 67 private: 68 ResourceItem* ResourceAt(uint32_t aIndex) const; 69 70 // Returns the index of the resource that contains the given 71 // logical offset. aResourceOffset will contain the offset into 72 // the resource at the given index returned if it is not null. If 73 // no such resource exists, returns GetSize() and aOffset is 74 // untouched. 75 uint32_t GetAtOffset(uint64_t aOffset, uint32_t* aResourceOffset) const; 76 77 ResourceItem* PopFront(); 78 79 // Logical length of the resource. 80 uint64_t mLogicalLength; 81 82 // Logical offset into the resource of the first element in the queue. 83 uint64_t mOffset; 84 }; 85 86 } // namespace mozilla 87 88 #endif /* MOZILLA_RESOURCEQUEUE_H_ */