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  public:
BufferMediaResource(const uint8_t * aBuffer,uint32_t aLength)25   BufferMediaResource(const uint8_t* aBuffer, uint32_t aLength)
26       : mBuffer(aBuffer), mLength(aLength) {}
27 
28  protected:
29   virtual ~BufferMediaResource() = default;
30 
31  private:
32   // These methods are called off the main thread.
ReadAt(int64_t aOffset,char * aBuffer,uint32_t aCount,uint32_t * aBytes)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;
37     }
38     *aBytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
39     memcpy(aBuffer, mBuffer + aOffset, *aBytes);
40     return NS_OK;
41   }
42   // Memory-based and no locks, caching discouraged.
ShouldCacheReads()43   bool ShouldCacheReads() override { return false; }
44 
Pin()45   void Pin() override {}
Unpin()46   void Unpin() override {}
GetLength()47   int64_t GetLength() override { return mLength; }
GetNextCachedData(int64_t aOffset)48   int64_t GetNextCachedData(int64_t aOffset) override { return aOffset; }
GetCachedDataEnd(int64_t aOffset)49   int64_t GetCachedDataEnd(int64_t aOffset) override {
50     return std::max(aOffset, int64_t(mLength));
51   }
IsDataCachedToEndOfResource(int64_t aOffset)52   bool IsDataCachedToEndOfResource(int64_t aOffset) override { return true; }
ReadFromCache(char * aBuffer,int64_t aOffset,uint32_t aCount)53   nsresult ReadFromCache(char* aBuffer, int64_t aOffset,
54                          uint32_t aCount) override {
55     if (aOffset < 0) {
56       return NS_ERROR_FAILURE;
57     }
58 
59     uint32_t bytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
60     memcpy(aBuffer, mBuffer + aOffset, bytes);
61     return NS_OK;
62   }
63 
GetCachedRanges(MediaByteRangeSet & aRanges)64   nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override {
65     aRanges += MediaByteRange(0, int64_t(mLength));
66     return NS_OK;
67   }
68 
69  private:
70   const uint8_t* mBuffer;
71   uint32_t mLength;
72 };
73 
74 }  // namespace mozilla
75 
76 #endif
77