Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/MediaBlockCacheBase.h
Line
Count
Source (jump to first uncovered line)
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
class MediaBlockCacheBase
37
{
38
public:
39
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaBlockCacheBase)
40
41
  static_assert(
42
    MediaCacheStream::BLOCK_SIZE <
43
      static_cast<std::remove_const<decltype(MediaCacheStream::BLOCK_SIZE)>::type>(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
  virtual nsresult Init() = 0;
53
54
  // Erase data and discard pending changes to reset the cache to its pristine
55
  // state as it was after Init().
56
  virtual void Flush() = 0;
57
58
  // Maximum number of blocks expected in this block cache. (But allow overflow
59
  // to accomodate incoming traffic before MediaCache can handle it.)
60
  virtual int32_t GetMaxBlocks() const = 0;
61
62
  // Can be called on any thread. This defers to a non-main thread.
63
  virtual nsresult WriteBlock(uint32_t aBlockIndex,
64
                              Span<const uint8_t> aData1,
65
                              Span<const uint8_t> aData2) = 0;
66
67
  // Synchronously reads data from file. May read from file or memory
68
  // depending on whether written blocks have been flushed to file yet.
69
  // Not recommended to be called from the main thread, as can cause jank.
70
  virtual nsresult Read(int64_t aOffset,
71
                        uint8_t* aData,
72
                        int32_t aLength,
73
                        int32_t* aBytes) = 0;
74
75
  // Moves a block asynchronously. Can be called on any thread.
76
  // This defers file I/O to a non-main thread.
77
  virtual nsresult MoveBlock(int32_t aSourceBlockIndex,
78
                             int32_t aDestBlockIndex) = 0;
79
};
80
81
} // End namespace mozilla.
82
83
#endif /* MEDIA_BLOCK_CACHE_BASE_H_ */