Line data Source code
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 : #include "SourceBufferResource.h"
8 :
9 : #include <algorithm>
10 :
11 : #include "nsISeekableStream.h"
12 : #include "nsISupports.h"
13 : #include "mozilla/Logging.h"
14 : #include "mozilla/SizePrintfMacros.h"
15 : #include "mozilla/TaskQueue.h"
16 : #include "MediaData.h"
17 :
18 0 : mozilla::LogModule* GetSourceBufferResourceLog()
19 : {
20 : static mozilla::LazyLogModule sLogModule("SourceBufferResource");
21 0 : return sLogModule;
22 : }
23 :
24 : #define SBR_DEBUG(arg, ...) \
25 : MOZ_LOG( \
26 : GetSourceBufferResourceLog(), \
27 : mozilla::LogLevel::Debug, \
28 : ("SourceBufferResource(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
29 : #define SBR_DEBUGV(arg, ...) \
30 : MOZ_LOG( \
31 : GetSourceBufferResourceLog(), \
32 : mozilla::LogLevel::Verbose, \
33 : ("SourceBufferResource(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
34 :
35 : namespace mozilla {
36 :
37 : nsresult
38 0 : SourceBufferResource::Close()
39 : {
40 0 : MOZ_ASSERT(OnTaskQueue());
41 0 : SBR_DEBUG("Close");
42 0 : mClosed = true;
43 0 : return NS_OK;
44 : }
45 :
46 : nsresult
47 0 : SourceBufferResource::ReadAt(int64_t aOffset,
48 : char* aBuffer,
49 : uint32_t aCount,
50 : uint32_t* aBytes)
51 : {
52 0 : SBR_DEBUG("ReadAt(aOffset=%" PRId64 ", aBuffer=%p, aCount=%u, aBytes=%p)",
53 : aOffset, aBytes, aCount, aBytes);
54 0 : return ReadAtInternal(aOffset, aBuffer, aCount, aBytes);
55 : }
56 :
57 : nsresult
58 0 : SourceBufferResource::ReadAtInternal(int64_t aOffset,
59 : char* aBuffer,
60 : uint32_t aCount,
61 : uint32_t* aBytes)
62 : {
63 0 : MOZ_ASSERT(OnTaskQueue());
64 :
65 0 : if (mClosed ||
66 0 : aOffset < 0 ||
67 0 : uint64_t(aOffset) < mInputBuffer.GetOffset() ||
68 0 : aOffset > GetLength()) {
69 0 : return NS_ERROR_FAILURE;
70 : }
71 :
72 0 : uint32_t available = GetLength() - aOffset;
73 0 : uint32_t count = std::min(aCount, available);
74 :
75 : // Keep the position of the last read to have Tell() approximately give us
76 : // the position we're up to in the stream.
77 0 : mOffset = aOffset + count;
78 :
79 0 : SBR_DEBUGV("offset=%" PRId64 " GetLength()=%" PRId64
80 : " available=%u count=%u mEnded=%d",
81 : aOffset,
82 : GetLength(),
83 : available,
84 : count,
85 : mEnded);
86 0 : if (available == 0) {
87 0 : SBR_DEBUGV("reached EOF");
88 0 : *aBytes = 0;
89 0 : return NS_OK;
90 : }
91 :
92 0 : mInputBuffer.CopyData(aOffset, count, aBuffer);
93 0 : *aBytes = count;
94 :
95 0 : return NS_OK;
96 : }
97 :
98 : nsresult
99 0 : SourceBufferResource::ReadFromCache(char* aBuffer,
100 : int64_t aOffset,
101 : uint32_t aCount)
102 : {
103 0 : SBR_DEBUG("ReadFromCache(aBuffer=%p, aOffset=%" PRId64 ", aCount=%u)",
104 : aBuffer, aOffset, aCount);
105 : uint32_t bytesRead;
106 0 : nsresult rv = ReadAtInternal(aOffset, aBuffer, aCount, &bytesRead);
107 0 : NS_ENSURE_SUCCESS(rv, rv);
108 :
109 : // ReadFromCache return failure if not all the data is cached.
110 0 : return bytesRead == aCount ? NS_OK : NS_ERROR_FAILURE;
111 : }
112 :
113 : uint32_t
114 0 : SourceBufferResource::EvictData(uint64_t aPlaybackOffset,
115 : int64_t aThreshold,
116 : ErrorResult& aRv)
117 : {
118 0 : MOZ_ASSERT(OnTaskQueue());
119 0 : SBR_DEBUG("EvictData(aPlaybackOffset=%" PRIu64 ","
120 : "aThreshold=%" PRId64 ")", aPlaybackOffset, aThreshold);
121 0 : uint32_t result = mInputBuffer.Evict(aPlaybackOffset, aThreshold, aRv);
122 0 : return result;
123 : }
124 :
125 : void
126 0 : SourceBufferResource::EvictBefore(uint64_t aOffset, ErrorResult& aRv)
127 : {
128 0 : MOZ_ASSERT(OnTaskQueue());
129 0 : SBR_DEBUG("EvictBefore(aOffset=%" PRIu64 ")", aOffset);
130 :
131 0 : mInputBuffer.EvictBefore(aOffset, aRv);
132 0 : }
133 :
134 : uint32_t
135 0 : SourceBufferResource::EvictAll()
136 : {
137 0 : MOZ_ASSERT(OnTaskQueue());
138 0 : SBR_DEBUG("EvictAll()");
139 0 : return mInputBuffer.EvictAll();
140 : }
141 :
142 : void
143 0 : SourceBufferResource::AppendData(MediaByteBuffer* aData)
144 : {
145 0 : MOZ_ASSERT(OnTaskQueue());
146 0 : SBR_DEBUG("AppendData(aData=%p, aLength=%" PRIuSIZE ")",
147 : aData->Elements(), aData->Length());
148 0 : mInputBuffer.AppendItem(aData);
149 0 : mEnded = false;
150 0 : }
151 :
152 : void
153 0 : SourceBufferResource::Ended()
154 : {
155 0 : MOZ_ASSERT(OnTaskQueue());
156 0 : SBR_DEBUG("");
157 0 : mEnded = true;
158 0 : }
159 :
160 0 : SourceBufferResource::~SourceBufferResource()
161 : {
162 0 : SBR_DEBUG("");
163 0 : }
164 :
165 0 : SourceBufferResource::SourceBufferResource()
166 : #if defined(DEBUG)
167 0 : : mTaskQueue(AbstractThread::GetCurrent()->AsTaskQueue())
168 : , mOffset(0)
169 : #else
170 : : mOffset(0)
171 : #endif
172 : , mClosed(false)
173 0 : , mEnded(false)
174 : {
175 0 : SBR_DEBUG("");
176 0 : }
177 :
178 : #if defined(DEBUG)
179 : AbstractThread*
180 0 : SourceBufferResource::GetTaskQueue() const
181 : {
182 0 : return mTaskQueue;
183 : }
184 : bool
185 0 : SourceBufferResource::OnTaskQueue() const
186 : {
187 0 : return !GetTaskQueue() || GetTaskQueue()->IsCurrentThreadIn();
188 : }
189 : #endif
190 :
191 : #undef SBR_DEBUG
192 : #undef SBR_DEBUGV
193 : } // namespace mozilla
|