1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef BOX_H_
8 #define BOX_H_
9 
10 #include <stdint.h>
11 #include "nsTArray.h"
12 #include "MediaResource.h"
13 #include "mozilla/EndianUtils.h"
14 #include "AtomType.h"
15 #include "BufferReader.h"
16 
17 namespace mozilla {
18 class ByteStream;
19 
20 class BumpAllocator {
21  public:
22   uint8_t* Allocate(size_t aNumBytes);
23 
24  private:
25   nsTArray<nsTArray<uint8_t>> mBuffers;
26 };
27 
28 class BoxContext {
29  public:
BoxContext(ByteStream * aSource,const MediaByteRangeSet & aByteRanges)30   BoxContext(ByteStream* aSource, const MediaByteRangeSet& aByteRanges)
31       : mSource(aSource), mByteRanges(aByteRanges) {}
32 
33   RefPtr<ByteStream> mSource;
34   const MediaByteRangeSet& mByteRanges;
35   BumpAllocator mAllocator;
36 };
37 
38 struct ByteSlice {
39   const uint8_t* mBytes;
40   size_t mSize;
41 };
42 
43 class Box {
44  public:
45   Box(BoxContext* aContext, uint64_t aOffset, const Box* aParent = nullptr);
46   Box();
47 
IsAvailable()48   bool IsAvailable() const { return !mRange.IsEmpty(); }
Offset()49   uint64_t Offset() const { return mRange.mStart; }
Length()50   uint64_t Length() const { return mRange.mEnd - mRange.mStart; }
NextOffset()51   uint64_t NextOffset() const { return mRange.mEnd; }
Range()52   const MediaByteRange& Range() const { return mRange; }
Parent()53   const Box* Parent() const { return mParent; }
IsType(const char * aType)54   bool IsType(const char* aType) const { return mType == AtomType(aType); }
55 
56   Box Next() const;
57   Box FirstChild() const;
58   // Reads the box contents, excluding the header.
59   nsTArray<uint8_t> Read() const;
60 
61   // Reads the complete box; its header and body.
62   nsTArray<uint8_t> ReadCompleteBox() const;
63 
64   // Reads from the content of the box, excluding header.
65   bool Read(nsTArray<uint8_t>* aDest, const MediaByteRange& aRange) const;
66 
67   static const uint64_t kMAX_BOX_READ;
68 
69   // Returns a slice, pointing to the data of this box. The lifetime of
70   // the memory this slice points to matches the box's context's lifetime.
71   ByteSlice ReadAsSlice();
72 
73  private:
74   bool Contains(MediaByteRange aRange) const;
75   BoxContext* mContext;
76   mozilla::MediaByteRange mRange;
77   uint64_t mBodyOffset;
78   uint64_t mChildOffset;
79   AtomType mType;
80   const Box* mParent;
81 };
82 
83 // BoxReader serves box data through an AutoByteReader. The box data is
84 // stored either in the box's context's bump allocator, or in the ByteStream
85 // itself if the ByteStream implements the Access() method.
86 // NOTE: The data the BoxReader reads may be stored in the Box's BoxContext.
87 // Ensure that the BoxReader doesn't outlive the BoxContext!
88 class MOZ_RAII BoxReader {
89  public:
BoxReader(Box & aBox)90   explicit BoxReader(Box& aBox)
91       : mData(aBox.ReadAsSlice()), mReader(mData.mBytes, mData.mSize) {}
92   BufferReader* operator->() { return &mReader; }
93 
94  private:
95   ByteSlice mData;
96   BufferReader mReader;
97 };
98 }  // namespace mozilla
99 
100 #endif
101