Line data Source code
1 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #if !defined(BufferMediaResource_h_)
7 : #define BufferMediaResource_h_
8 :
9 : #include "MediaResource.h"
10 : #include "nsISeekableStream.h"
11 : #include "nsIPrincipal.h"
12 : #include <algorithm>
13 :
14 : namespace mozilla {
15 :
16 : // A simple MediaResource based on an in memory buffer. This class accepts
17 : // the address and the length of the buffer, and simulates a read/seek API
18 : // on top of it. The Read implementation involves copying memory, which is
19 : // unfortunate, but the MediaResource interface mandates that.
20 : class BufferMediaResource : public MediaResource
21 : {
22 : public:
23 0 : BufferMediaResource(const uint8_t* aBuffer,
24 : uint32_t aLength,
25 : nsIPrincipal* aPrincipal)
26 0 : : mBuffer(aBuffer)
27 : , mLength(aLength)
28 : , mOffset(0)
29 0 : , mPrincipal(aPrincipal)
30 : {
31 0 : }
32 :
33 : protected:
34 0 : virtual ~BufferMediaResource()
35 0 : {
36 0 : }
37 :
38 : private:
39 0 : nsresult Close() override { return NS_OK; }
40 0 : void Suspend(bool aCloseImmediately) override {}
41 0 : void Resume() override {}
42 : // Get the current principal for the channel
43 0 : already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
44 : {
45 0 : nsCOMPtr<nsIPrincipal> principal = mPrincipal;
46 0 : return principal.forget();
47 : }
48 : // These methods are called off the main thread.
49 : // The mode is initially MODE_PLAYBACK.
50 0 : void SetReadMode(MediaCacheStream::ReadMode aMode) override {}
51 0 : void SetPlaybackRate(uint32_t aBytesPerSecond) override {}
52 0 : nsresult ReadAt(int64_t aOffset, char* aBuffer,
53 : uint32_t aCount, uint32_t* aBytes) override
54 : {
55 0 : if (aOffset < 0 || aOffset > mLength) {
56 0 : return NS_ERROR_FAILURE;
57 : }
58 0 : *aBytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
59 0 : memcpy(aBuffer, mBuffer + aOffset, *aBytes);
60 0 : mOffset = aOffset + *aBytes;
61 0 : return NS_OK;
62 : }
63 : // Memory-based and no locks, caching discouraged.
64 0 : bool ShouldCacheReads() override { return false; }
65 :
66 0 : int64_t Tell() override { return mOffset; }
67 :
68 0 : void Pin() override {}
69 0 : void Unpin() override {}
70 0 : double GetDownloadRate(bool* aIsReliable) override { *aIsReliable = false; return 0.; }
71 0 : int64_t GetLength() override { return mLength; }
72 0 : int64_t GetNextCachedData(int64_t aOffset) override { return aOffset; }
73 0 : int64_t GetCachedDataEnd(int64_t aOffset) override
74 : {
75 0 : return std::max(aOffset, int64_t(mLength));
76 : }
77 0 : bool IsDataCachedToEndOfResource(int64_t aOffset) override { return true; }
78 0 : bool IsSuspendedByCache() override { return false; }
79 0 : bool IsSuspended() override { return false; }
80 0 : nsresult ReadFromCache(char* aBuffer,
81 : int64_t aOffset,
82 : uint32_t aCount) override
83 : {
84 0 : if (aOffset < 0) {
85 0 : return NS_ERROR_FAILURE;
86 : }
87 :
88 0 : uint32_t bytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
89 0 : memcpy(aBuffer, mBuffer + aOffset, bytes);
90 0 : return NS_OK;
91 : }
92 :
93 0 : nsresult Open(nsIStreamListener** aStreamListener) override
94 : {
95 0 : return NS_ERROR_FAILURE;
96 : }
97 :
98 0 : nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override
99 : {
100 0 : aRanges += MediaByteRange(0, int64_t(mLength));
101 0 : return NS_OK;
102 : }
103 :
104 0 : bool IsTransportSeekable() override { return true; }
105 :
106 0 : size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
107 : {
108 : // Not owned:
109 : // - mBuffer
110 : // - mPrincipal
111 0 : size_t size = MediaResource::SizeOfExcludingThis(aMallocSizeOf);
112 0 : return size;
113 : }
114 :
115 0 : size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
116 : {
117 0 : return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
118 : }
119 :
120 : private:
121 : const uint8_t * mBuffer;
122 : uint32_t mLength;
123 : uint32_t mOffset;
124 : nsCOMPtr<nsIPrincipal> mPrincipal;
125 : };
126 :
127 : } // namespace mozilla
128 :
129 : #endif
|