Line data Source code
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/. */
6 :
7 : #ifndef MEDIA_BLOCK_CACHE_BASE_H_
8 : #define MEDIA_BLOCK_CACHE_BASE_H_
9 :
10 : #include "MediaCache.h"
11 : #include "mozilla/Span.h"
12 :
13 : namespace mozilla {
14 :
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.
19 : //
20 : // So MediaBlockCacheBase provides an abstraction for a temporary memory buffer or file accessible
21 : // as an array of blocks, which supports a block move operation, and
22 : // allows synchronous reading and writing from any thread, with writes being
23 : // buffered as needed so as not to block.
24 : //
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.
30 : //
31 : // To improve efficiency, writes can only be done at block granularity,
32 : // whereas reads can be done with byte granularity.
33 : //
34 : // Note it's also recommended not to read from the media cache from the main
35 : // thread to prevent jank.
36 0 : class MediaBlockCacheBase
37 : {
38 : public:
39 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaBlockCacheBase)
40 :
41 : static_assert(
42 : MediaCacheStream::BLOCK_SIZE <
43 : static_cast<decltype(MediaCacheStream::BLOCK_SIZE)>(INT32_MAX),
44 : "MediaCacheStream::BLOCK_SIZE should fit in 31 bits");
45 : static const int32_t BLOCK_SIZE = MediaCacheStream::BLOCK_SIZE;
46 :
47 : protected:
48 0 : virtual ~MediaBlockCacheBase() {}
49 :
50 : public:
51 : // Initialize this cache.
52 : // If called again, re-initialize cache with minimal chance of failure.
53 : virtual nsresult Init() = 0;
54 :
55 : // Maximum number of blocks expected in this block cache. (But allow overflow
56 : // to accomodate incoming traffic before MediaCache can handle it.)
57 : virtual int32_t GetMaxBlocks() const = 0;
58 :
59 : // Can be called on any thread. This defers to a non-main thread.
60 : virtual nsresult WriteBlock(uint32_t aBlockIndex,
61 : Span<const uint8_t> aData1,
62 : Span<const uint8_t> aData2) = 0;
63 :
64 : // Synchronously reads data from file. May read from file or memory
65 : // depending on whether written blocks have been flushed to file yet.
66 : // Not recommended to be called from the main thread, as can cause jank.
67 : virtual nsresult Read(int64_t aOffset,
68 : uint8_t* aData,
69 : int32_t aLength,
70 : int32_t* aBytes) = 0;
71 :
72 : // Moves a block asynchronously. Can be called on any thread.
73 : // This defers file I/O to a non-main thread.
74 : virtual nsresult MoveBlock(int32_t aSourceBlockIndex,
75 : int32_t aDestBlockIndex) = 0;
76 : };
77 :
78 : } // End namespace mozilla.
79 :
80 : #endif /* MEDIA_BLOCK_CACHE_BASE_H_ */
|