Bug 1572460 - Refactor `selection` out of the `InspectorFront`. r=yulia
[gecko.git] / dom / media / MediaBlockCacheBase.h
blob97c3a6c1b0fc5013d8a42f7ca792e4d44c67e696
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 MEDIA_BLOCK_CACHE_BASE_H_
8 #define MEDIA_BLOCK_CACHE_BASE_H_
10 #include "MediaCache.h"
11 #include "mozilla/Span.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 // or file accessible as an array of blocks, which supports a block move
22 // operation, and allows synchronous reading and writing from any thread, with
23 // writes being buffered as needed so as not to block.
25 // Writes and cache block moves (which require reading) may be deferred to
26 // their own non-main thread. This object also ensures that data which has
27 // been scheduled to be written, but hasn't actually *been* written, is read
28 // as if it had, i.e. pending writes are cached in readable memory until
29 // they're flushed to file.
31 // To improve efficiency, writes can only be done at block granularity,
32 // whereas reads can be done with byte granularity.
34 // Note it's also recommended not to read from the media cache from the main
35 // thread to prevent jank.
36 class MediaBlockCacheBase {
37 public:
38 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaBlockCacheBase)
40 static_assert(MediaCacheStream::BLOCK_SIZE <
41 static_cast<std::remove_const<decltype(
42 MediaCacheStream::BLOCK_SIZE)>::type>(INT32_MAX),
43 "MediaCacheStream::BLOCK_SIZE should fit in 31 bits");
44 static const int32_t BLOCK_SIZE = MediaCacheStream::BLOCK_SIZE;
46 protected:
47 virtual ~MediaBlockCacheBase() {}
49 public:
50 // Initialize this cache.
51 virtual nsresult Init() = 0;
53 // Erase data and discard pending changes to reset the cache to its pristine
54 // state as it was after Init().
55 virtual void Flush() = 0;
57 // Maximum number of blocks expected in this block cache. (But allow overflow
58 // to accomodate incoming traffic before MediaCache can handle it.)
59 virtual size_t GetMaxBlocks(size_t aCacheSizeInKiB) const = 0;
61 // Can be called on any thread. This defers to a non-main thread.
62 virtual nsresult WriteBlock(uint32_t aBlockIndex, Span<const uint8_t> aData1,
63 Span<const uint8_t> aData2) = 0;
65 // Synchronously reads data from file. May read from file or memory
66 // depending on whether written blocks have been flushed to file yet.
67 // Not recommended to be called from the main thread, as can cause jank.
68 virtual nsresult Read(int64_t aOffset, uint8_t* aData, int32_t aLength,
69 int32_t* aBytes) = 0;
71 // Moves a block asynchronously. Can be called on any thread.
72 // This defers file I/O to a non-main thread.
73 virtual nsresult MoveBlock(int32_t aSourceBlockIndex,
74 int32_t aDestBlockIndex) = 0;
77 } // End namespace mozilla.
79 #endif /* MEDIA_BLOCK_CACHE_BASE_H_ */