Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/mediasource/SourceBufferResource.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 MOZILLA_SOURCEBUFFERRESOURCE_H_
8
#define MOZILLA_SOURCEBUFFERRESOURCE_H_
9
10
#include "mozilla/AbstractThread.h"
11
#include "mozilla/Logging.h"
12
#include "MediaResource.h"
13
#include "nsIPrincipal.h"
14
#include "ResourceQueue.h"
15
16
0
#define UNIMPLEMENTED() { /* Logging this is too spammy to do by default */ }
17
18
namespace mozilla {
19
20
class MediaByteBuffer;
21
class TaskQueue;
22
23
namespace dom {
24
25
class SourceBuffer;
26
27
} // namespace dom
28
29
DDLoggedTypeDeclNameAndBase(SourceBufferResource, MediaResource);
30
31
// SourceBufferResource is not thread safe.
32
class SourceBufferResource final
33
  : public MediaResource
34
  , public DecoderDoctorLifeLogger<SourceBufferResource>
35
{
36
public:
37
  SourceBufferResource();
38
  nsresult Close() override;
39
  nsresult ReadAt(int64_t aOffset,
40
                  char* aBuffer,
41
                  uint32_t aCount,
42
                  uint32_t* aBytes) override;
43
  // Memory-based and no locks, caching discouraged.
44
0
  bool ShouldCacheReads() override { return false; }
45
0
  void Pin() override { UNIMPLEMENTED(); }
46
0
  void Unpin() override { UNIMPLEMENTED(); }
47
0
  int64_t GetLength() override { return mInputBuffer.GetLength(); }
48
  int64_t GetNextCachedData(int64_t aOffset) override
49
0
  {
50
0
    MOZ_ASSERT(OnTaskQueue());
51
0
    MOZ_ASSERT(aOffset >= 0);
52
0
    if (uint64_t(aOffset) < mInputBuffer.GetOffset()) {
53
0
      return mInputBuffer.GetOffset();
54
0
    } else if (aOffset == GetLength()) {
55
0
      return -1;
56
0
    }
57
0
    return aOffset;
58
0
  }
59
  int64_t GetCachedDataEnd(int64_t aOffset) override
60
0
  {
61
0
    MOZ_ASSERT(OnTaskQueue());
62
0
    MOZ_ASSERT(aOffset >= 0);
63
0
    if (uint64_t(aOffset) < mInputBuffer.GetOffset() ||
64
0
        aOffset >= GetLength()) {
65
0
      // aOffset is outside of the buffered range.
66
0
      return aOffset;
67
0
    }
68
0
    return GetLength();
69
0
  }
70
0
  bool IsDataCachedToEndOfResource(int64_t aOffset) override { return false; }
71
  nsresult ReadFromCache(char* aBuffer,
72
                         int64_t aOffset,
73
                         uint32_t aCount) override;
74
75
  nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override
76
0
  {
77
0
    MOZ_ASSERT(OnTaskQueue());
78
0
    if (mInputBuffer.GetLength()) {
79
0
      aRanges += MediaByteRange(mInputBuffer.GetOffset(),
80
0
                                mInputBuffer.GetLength());
81
0
    }
82
0
    return NS_OK;
83
0
  }
84
85
  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
86
0
  {
87
0
    MOZ_ASSERT(OnTaskQueue());
88
0
    return mInputBuffer.SizeOfExcludingThis(aMallocSizeOf);
89
0
  }
90
91
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
92
0
  {
93
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
94
0
  }
95
96
  // Used by SourceBuffer.
97
  void AppendData(MediaByteBuffer* aData);
98
  void Ended();
99
  bool IsEnded()
100
0
  {
101
0
    MOZ_ASSERT(OnTaskQueue());
102
0
    return mEnded;
103
0
  }
104
  // Remove data from resource if it holds more than the threshold reduced by
105
  // the given number of bytes. Returns amount evicted.
106
  uint32_t EvictData(uint64_t aPlaybackOffset, int64_t aThresholdReduct,
107
                     ErrorResult& aRv);
108
109
  // Remove data from resource before the given offset.
110
  void EvictBefore(uint64_t aOffset, ErrorResult& aRv);
111
112
  // Remove all data from the resource
113
  uint32_t EvictAll();
114
115
  // Returns the amount of data currently retained by this resource.
116
  int64_t GetSize()
117
0
  {
118
0
    MOZ_ASSERT(OnTaskQueue());
119
0
    return mInputBuffer.GetLength() - mInputBuffer.GetOffset();
120
0
  }
121
122
#if defined(DEBUG)
123
  void Dump(const char* aPath)
124
  {
125
    mInputBuffer.Dump(aPath);
126
  }
127
#endif
128
129
private:
130
  virtual ~SourceBufferResource();
131
  nsresult ReadAtInternal(int64_t aOffset,
132
                          char* aBuffer,
133
                          uint32_t aCount,
134
                          uint32_t* aBytes);
135
136
#if defined(DEBUG)
137
  const RefPtr<TaskQueue> mTaskQueue;
138
  // TaskQueue methods and objects.
139
  AbstractThread* GetTaskQueue() const;
140
  bool OnTaskQueue() const;
141
#endif
142
143
  // The buffer holding resource data.
144
  ResourceQueue mInputBuffer;
145
146
  bool mClosed = false;
147
  bool mEnded = false;
148
};
149
150
} // namespace mozilla
151
152
#undef UNIMPLEMENTED
153
154
#endif /* MOZILLA_SOURCEBUFFERRESOURCE_H_ */