content/media/mediasource/SourceBufferResource.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:27d0468720db
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_SOURCEBUFFERRESOURCE_H_
8 #define MOZILLA_SOURCEBUFFERRESOURCE_H_
9
10 #include <algorithm>
11 #include "MediaCache.h"
12 #include "MediaResource.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/ReentrantMonitor.h"
15 #include "nsCOMPtr.h"
16 #include "nsError.h"
17 #include "nsIPrincipal.h"
18 #include "nsStringGlue.h"
19 #include "nsTArray.h"
20 #include "nsDeque.h"
21 #include "nscore.h"
22
23 class nsIStreamListener;
24
25 namespace mozilla {
26
27 class MediaDecoder;
28
29 namespace dom {
30
31 class SourceBuffer;
32
33 } // namespace dom
34
35 class SourceBufferResource MOZ_FINAL : public MediaResource
36 {
37 private:
38 // A SourceBufferResource has a queue containing the data
39 // that is appended to it. The queue holds instances of
40 // ResourceItem which is an array of the bytes. Appending
41 // data to the SourceBufferResource pushes this onto the
42 // queue. As items are played they are taken off the front
43 // of the queue.
44 // Data is evicted once it reaches a size threshold. This
45 // pops the items off the front of the queue and deletes it.
46 // If an eviction happens then the MediaSource is notified
47 // (done in SourceBuffer::AppendData) which then requests
48 // all SourceBuffers to evict data up to approximately
49 // the same timepoint.
50 struct ResourceItem {
51 ResourceItem(uint8_t const* aData, uint32_t aSize) {
52 mData.AppendElements(aData, aSize);
53 }
54 nsTArray<uint8_t> mData;
55
56 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
57 // size including this
58 size_t size = aMallocSizeOf(this);
59
60 // size excluding this
61 size += mData.SizeOfExcludingThis(aMallocSizeOf);
62
63 return size;
64 }
65 };
66
67 class ResourceQueueDeallocator : public nsDequeFunctor {
68 virtual void* operator() (void* anObject) {
69 delete static_cast<ResourceItem*>(anObject);
70 return nullptr;
71 }
72 };
73
74 class ResourceQueue : private nsDeque {
75 private:
76 // Logical offset into the resource of the first element
77 // in the queue.
78 uint64_t mOffset;
79
80 public:
81 ResourceQueue() :
82 nsDeque(new ResourceQueueDeallocator()),
83 mOffset(0)
84 {
85 }
86
87 // Clears all items from the queue
88 inline void Clear() {
89 return nsDeque::Erase();
90 }
91
92 // Returns the number of items in the queue
93 inline uint32_t GetSize() {
94 return nsDeque::GetSize();
95 }
96
97 // Returns the logical byte offset of the start of the data.
98 inline uint64_t GetOffset() {
99 return mOffset;
100 }
101
102 inline ResourceItem* ResourceAt(uint32_t aIndex) {
103 return static_cast<ResourceItem*>(nsDeque::ObjectAt(aIndex));
104 }
105
106 // Returns the length of all items in the queue plus the offset.
107 // This is the logical length of the resource.
108 inline uint64_t GetLength() {
109 uint64_t s = mOffset;
110 for (uint32_t i = 0; i < GetSize(); ++i) {
111 ResourceItem* item = ResourceAt(i);
112 s += item->mData.Length();
113 }
114 return s;
115 }
116
117 // Returns the index of the resource that contains the given
118 // logical offset. aResourceOffset will contain the offset into
119 // the resource at the given index returned if it is not null. If
120 // no such resource exists, returns GetSize() and aOffset is
121 // untouched.
122 inline uint32_t GetAtOffset(uint64_t aOffset, uint32_t *aResourceOffset) {
123 MOZ_ASSERT(aOffset >= mOffset);
124 uint64_t offset = mOffset;
125 for (uint32_t i = 0; i < GetSize(); ++i) {
126 ResourceItem* item = ResourceAt(i);
127 // If the item contains the start of the offset we want to
128 // break out of the loop.
129 if (item->mData.Length() + offset > aOffset) {
130 if (aResourceOffset) {
131 *aResourceOffset = aOffset - offset;
132 }
133 return i;
134 }
135 offset += item->mData.Length();
136 }
137 return GetSize();
138 }
139
140 // Copies aCount bytes from aOffset in the queue into aDest.
141 inline void CopyData(uint64_t aOffset, uint32_t aCount, char* aDest) {
142 uint32_t offset = 0;
143 uint32_t start = GetAtOffset(aOffset, &offset);
144 uint32_t end = std::min(GetAtOffset(aOffset + aCount, nullptr) + 1, GetSize());
145 for (uint32_t i = start; i < end; ++i) {
146 ResourceItem* item = ResourceAt(i);
147 uint32_t bytes = std::min(aCount, item->mData.Length() - offset);
148 if (bytes != 0) {
149 memcpy(aDest, &item->mData[offset], bytes);
150 offset = 0;
151 aCount -= bytes;
152 aDest += bytes;
153 }
154 }
155 }
156
157 inline void PushBack(ResourceItem* aItem) {
158 nsDeque::Push(aItem);
159 }
160
161 inline void PushFront(ResourceItem* aItem) {
162 nsDeque::PushFront(aItem);
163 }
164
165 inline ResourceItem* PopBack() {
166 return static_cast<ResourceItem*>(nsDeque::Pop());
167 }
168
169 inline ResourceItem* PopFront() {
170 return static_cast<ResourceItem*>(nsDeque::PopFront());
171 }
172
173 // Evict data in queue if the total queue size is greater than
174 // aThreshold past the offset. Returns true if some data was
175 // actually evicted.
176 inline bool Evict(uint64_t aOffset, uint32_t aThreshold) {
177 bool evicted = false;
178 while (GetLength() - mOffset > aThreshold) {
179 ResourceItem* item = ResourceAt(0);
180 if (item->mData.Length() + mOffset > aOffset) {
181 break;
182 }
183 mOffset += item->mData.Length();
184 delete PopFront();
185 evicted = true;
186 }
187 return evicted;
188 }
189
190 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
191 // Calculate the size of the internal deque.
192 size_t size = nsDeque::SizeOfExcludingThis(aMallocSizeOf);
193
194 // Sum the ResourceItems.
195 for (int32_t i = 0; i < nsDeque::GetSize(); ++i) {
196 const ResourceItem* item =
197 static_cast<const ResourceItem*>(nsDeque::ObjectAt(i));
198 size += item->SizeOfIncludingThis(aMallocSizeOf);
199 }
200
201 return size;
202 }
203 };
204
205 public:
206 SourceBufferResource(nsIPrincipal* aPrincipal,
207 const nsACString& aType);
208 ~SourceBufferResource();
209
210 virtual nsresult Close() MOZ_OVERRIDE;
211 virtual void Suspend(bool aCloseImmediately) MOZ_OVERRIDE {}
212 virtual void Resume() MOZ_OVERRIDE {}
213
214 virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal() MOZ_OVERRIDE
215 {
216 return nsCOMPtr<nsIPrincipal>(mPrincipal).forget();
217 }
218
219 virtual already_AddRefed<MediaResource> CloneData(MediaDecoder* aDecoder) MOZ_OVERRIDE
220 {
221 return nullptr;
222 }
223
224 virtual void SetReadMode(MediaCacheStream::ReadMode aMode) MOZ_OVERRIDE {}
225 virtual void SetPlaybackRate(uint32_t aBytesPerSecond) MOZ_OVERRIDE {}
226 virtual nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes) MOZ_OVERRIDE;
227 virtual nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) MOZ_OVERRIDE;
228 virtual nsresult Seek(int32_t aWhence, int64_t aOffset) MOZ_OVERRIDE;
229 virtual void StartSeekingForMetadata() MOZ_OVERRIDE { }
230 virtual void EndSeekingForMetadata() MOZ_OVERRIDE {}
231 virtual int64_t Tell() MOZ_OVERRIDE { return mOffset; }
232 virtual void Pin() MOZ_OVERRIDE {}
233 virtual void Unpin() MOZ_OVERRIDE {}
234 virtual double GetDownloadRate(bool* aIsReliable) MOZ_OVERRIDE { return 0; }
235 virtual int64_t GetLength() MOZ_OVERRIDE { return mInputBuffer.GetLength(); }
236 virtual int64_t GetNextCachedData(int64_t aOffset) MOZ_OVERRIDE { return aOffset; }
237 virtual int64_t GetCachedDataEnd(int64_t aOffset) MOZ_OVERRIDE { return GetLength(); }
238 virtual bool IsDataCachedToEndOfResource(int64_t aOffset) MOZ_OVERRIDE { return false; }
239 virtual bool IsSuspendedByCache() MOZ_OVERRIDE { return false; }
240 virtual bool IsSuspended() MOZ_OVERRIDE { return false; }
241 virtual nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) MOZ_OVERRIDE;
242 virtual bool IsTransportSeekable() MOZ_OVERRIDE { return true; }
243 virtual nsresult Open(nsIStreamListener** aStreamListener) MOZ_OVERRIDE { return NS_ERROR_FAILURE; }
244
245 virtual nsresult GetCachedRanges(nsTArray<MediaByteRange>& aRanges) MOZ_OVERRIDE
246 {
247 aRanges.AppendElement(MediaByteRange(mInputBuffer.GetOffset(),
248 mInputBuffer.GetLength()));
249 return NS_OK;
250 }
251
252 virtual const nsCString& GetContentType() const MOZ_OVERRIDE { return mType; }
253
254 virtual size_t SizeOfExcludingThis(
255 MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
256 {
257 ReentrantMonitorAutoEnter mon(mMonitor);
258
259 // Not owned:
260 // - mPrincipal
261 size_t size = MediaResource::SizeOfExcludingThis(aMallocSizeOf);
262 size += mType.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
263 size += mInputBuffer.SizeOfExcludingThis(aMallocSizeOf);
264
265 return size;
266 }
267
268 virtual size_t SizeOfIncludingThis(
269 MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
270 {
271 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
272 }
273
274 // Used by SourceBuffer.
275 void AppendData(const uint8_t* aData, uint32_t aLength);
276 void Ended();
277 // Remove data from resource if it holds more than the threshold
278 // number of bytes. Returns true if some data was evicted.
279 bool EvictData(uint32_t aThreshold);
280
281 // Remove data from resource before the given offset.
282 void EvictBefore(uint64_t aOffset);
283
284 private:
285 nsCOMPtr<nsIPrincipal> mPrincipal;
286 const nsAutoCString mType;
287
288 // Provides synchronization between SourceBuffers and InputAdapters.
289 // Protects all of the member variables below. Read() will await a
290 // Notify() (from Seek, AppendData, Ended, or Close) when insufficient
291 // data is available in mData.
292 mutable ReentrantMonitor mMonitor;
293
294 // The buffer holding resource data is a queue of ResourceItem's.
295 ResourceQueue mInputBuffer;
296
297 uint64_t mOffset;
298 bool mClosed;
299 bool mEnded;
300 };
301
302 } // namespace mozilla
303 #endif /* MOZILLA_SOURCEBUFFERRESOURCE_H_ */

mercurial