Backed out changeset ddccd40117a0 (bug 1853271) for causing bug 1854769. CLOSED TREE
[gecko.git] / dom / media / MemoryBlockCache.h
blob32bf9615c6c846ee8ff2fa7964158a9bfee178db
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MEMORY_BLOCK_CACHE_H_
8 #define MEMORY_BLOCK_CACHE_H_
10 #include "MediaBlockCacheBase.h"
11 #include "mozilla/Mutex.h"
13 namespace mozilla {
15 // Manages block management for the media cache. Data comes in over the network
16 // via callbacks on the main thread, however we don't want to write the
17 // incoming data to the media cache on the main thread, as this could block
18 // causing UI jank.
20 // So MediaBlockCacheBase provides an abstraction for a temporary memory buffer
21 // as an array of blocks, which supports a block move operation, and
22 // allows synchronous reading and writing from any thread.
24 // Writes and cache block moves (which require reading) may be deferred to
25 // their own non-main thread. This object also ensures that data which has
26 // been scheduled to be written, but hasn't actually *been* written, is read
27 // as if it had, i.e. pending writes are cached in readable memory until
28 // they're flushed to file.
30 // To improve efficiency, writes can only be done at block granularity,
31 // whereas reads can be done with byte granularity.
32 class MemoryBlockCache : public MediaBlockCacheBase {
33 public:
34 explicit MemoryBlockCache(int64_t aContentLength);
36 protected:
37 virtual ~MemoryBlockCache();
39 public:
40 // Allocate initial buffer.
41 // If re-initializing, clear buffer.
42 virtual nsresult Init() override;
44 void Flush() override;
46 // Maximum number of blocks allowed in this block cache.
47 // Based on initial content length, and minimum usable block cache.
48 size_t GetMaxBlocks(size_t) const override { return mMaxBlocks; }
50 // Can be called on any thread.
51 virtual nsresult WriteBlock(uint32_t aBlockIndex, Span<const uint8_t> aData1,
52 Span<const uint8_t> aData2) override;
54 // Synchronously reads data from buffer.
55 virtual nsresult Read(int64_t aOffset, uint8_t* aData, int32_t aLength,
56 int32_t* aBytes) override;
58 // Moves a block. Can be called on any thread.
59 virtual nsresult MoveBlock(int32_t aSourceBlockIndex,
60 int32_t aDestBlockIndex) override;
62 private:
63 static size_t BlockIndexToOffset(uint32_t aBlockIndex) {
64 return static_cast<size_t>(aBlockIndex) * BLOCK_SIZE;
67 // Ensure the buffer has at least a multiple of BLOCK_SIZE that can contain
68 // aContentLength bytes. Buffer size can only grow.
69 // Returns false if allocation failed.
70 bool EnsureBufferCanContain(size_t aContentLength);
72 // Initial content length.
73 const size_t mInitialContentLength;
75 // Maximum number of blocks that this MemoryBlockCache expects.
76 const size_t mMaxBlocks;
78 // Mutex which controls access to all members below.
79 Mutex mMutex MOZ_UNANNOTATED;
81 nsTArray<uint8_t> mBuffer;
82 bool mHasGrown = false;
85 } // End namespace mozilla.
87 #endif /* MEMORY_BLOCK_CACHE_H_ */