Bug 1772053 - Enable dynamic code disable mitigations only on Windows 10 1703+ r...
[gecko.git] / dom / media / BufferMediaResource.h
blob693704a2ae04fc6038b77f17ecda108ac28b4894
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"
11 # include <algorithm>
13 namespace mozilla {
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> {
24 public:
25 BufferMediaResource(const uint8_t* aBuffer, uint32_t aLength)
26 : mBuffer(aBuffer), mLength(aLength) {}
28 protected:
29 virtual ~BufferMediaResource() = default;
31 private:
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);
40 return NS_OK;
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 {
55 if (aOffset < 0) {
56 return NS_ERROR_FAILURE;
59 uint32_t bytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
60 memcpy(aBuffer, mBuffer + aOffset, bytes);
61 return NS_OK;
64 nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override {
65 aRanges += MediaByteRange(0, int64_t(mLength));
66 return NS_OK;
69 private:
70 const uint8_t* mBuffer;
71 uint32_t mLength;
74 } // namespace mozilla
76 #endif