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/. */
6 #if !defined(BufferMediaResource_h_)
7 # define BufferMediaResource_h_
9 # include "MediaResource.h"
10 # include "nsISeekableStream.h"
15 DDLoggedTypeDeclNameAndBase(BufferMediaResource
, MediaResource
);
17 // A simple MediaResource based on an in memory buffer. This class accepts
18 // the address and the length of the buffer, and simulates a read/seek API
19 // on top of it. The Read implementation involves copying memory, which is
20 // unfortunate, but the MediaResource interface mandates that.
21 class BufferMediaResource
22 : public MediaResource
,
23 public DecoderDoctorLifeLogger
<BufferMediaResource
> {
25 BufferMediaResource(const uint8_t* aBuffer
, uint32_t aLength
)
26 : mBuffer(aBuffer
), mLength(aLength
) {}
29 virtual ~BufferMediaResource() = default;
32 // These methods are called off the main thread.
33 nsresult
ReadAt(int64_t aOffset
, char* aBuffer
, uint32_t aCount
,
34 uint32_t* aBytes
) override
{
35 if (aOffset
< 0 || aOffset
> mLength
) {
36 return NS_ERROR_FAILURE
;
38 *aBytes
= std::min(mLength
- static_cast<uint32_t>(aOffset
), aCount
);
39 memcpy(aBuffer
, mBuffer
+ aOffset
, *aBytes
);
42 // Memory-based and no locks, caching discouraged.
43 bool ShouldCacheReads() override
{ return false; }
45 void Pin() override
{}
46 void Unpin() override
{}
47 int64_t GetLength() override
{ return mLength
; }
48 int64_t GetNextCachedData(int64_t aOffset
) override
{ return aOffset
; }
49 int64_t GetCachedDataEnd(int64_t aOffset
) override
{
50 return std::max(aOffset
, int64_t(mLength
));
52 bool IsDataCachedToEndOfResource(int64_t aOffset
) override
{ return true; }
53 nsresult
ReadFromCache(char* aBuffer
, int64_t aOffset
,
54 uint32_t aCount
) override
{
56 return NS_ERROR_FAILURE
;
59 uint32_t bytes
= std::min(mLength
- static_cast<uint32_t>(aOffset
), aCount
);
60 memcpy(aBuffer
, mBuffer
+ aOffset
, bytes
);
64 nsresult
GetCachedRanges(MediaByteRangeSet
& aRanges
) override
{
65 aRanges
+= MediaByteRange(0, int64_t(mLength
));
70 const uint8_t* mBuffer
;
74 } // namespace mozilla