/src/mozilla-central/dom/media/BufferMediaResource.h
Line | Count | Source (jump to first uncovered line) |
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 <algorithm> |
12 | | |
13 | | namespace mozilla { |
14 | | |
15 | | DDLoggedTypeDeclNameAndBase(BufferMediaResource, MediaResource); |
16 | | |
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 | | { |
25 | | public: |
26 | | BufferMediaResource(const uint8_t* aBuffer, uint32_t aLength) |
27 | | : mBuffer(aBuffer) |
28 | | , mLength(aLength) |
29 | 0 | { |
30 | 0 | } |
31 | | |
32 | | protected: |
33 | | virtual ~BufferMediaResource() |
34 | 0 | { |
35 | 0 | } |
36 | | |
37 | | private: |
38 | | // These methods are called off the main thread. |
39 | | nsresult ReadAt(int64_t aOffset, char* aBuffer, |
40 | | uint32_t aCount, uint32_t* aBytes) override |
41 | 0 | { |
42 | 0 | if (aOffset < 0 || aOffset > mLength) { |
43 | 0 | return NS_ERROR_FAILURE; |
44 | 0 | } |
45 | 0 | *aBytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount); |
46 | 0 | memcpy(aBuffer, mBuffer + aOffset, *aBytes); |
47 | 0 | return NS_OK; |
48 | 0 | } |
49 | | // Memory-based and no locks, caching discouraged. |
50 | 0 | bool ShouldCacheReads() override { return false; } |
51 | | |
52 | 0 | void Pin() override {} |
53 | 0 | void Unpin() override {} |
54 | 0 | int64_t GetLength() override { return mLength; } |
55 | 0 | int64_t GetNextCachedData(int64_t aOffset) override { return aOffset; } |
56 | | int64_t GetCachedDataEnd(int64_t aOffset) override |
57 | 0 | { |
58 | 0 | return std::max(aOffset, int64_t(mLength)); |
59 | 0 | } |
60 | 0 | bool IsDataCachedToEndOfResource(int64_t aOffset) override { return true; } |
61 | | nsresult ReadFromCache(char* aBuffer, |
62 | | int64_t aOffset, |
63 | | uint32_t aCount) override |
64 | 0 | { |
65 | 0 | if (aOffset < 0) { |
66 | 0 | return NS_ERROR_FAILURE; |
67 | 0 | } |
68 | 0 | |
69 | 0 | uint32_t bytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount); |
70 | 0 | memcpy(aBuffer, mBuffer + aOffset, bytes); |
71 | 0 | return NS_OK; |
72 | 0 | } |
73 | | |
74 | | nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override |
75 | 0 | { |
76 | 0 | aRanges += MediaByteRange(0, int64_t(mLength)); |
77 | 0 | return NS_OK; |
78 | 0 | } |
79 | | |
80 | | private: |
81 | | const uint8_t * mBuffer; |
82 | | uint32_t mLength; |
83 | | }; |
84 | | |
85 | | } // namespace mozilla |
86 | | |
87 | | #endif |