|
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 <string.h> |
|
10 #include <algorithm> |
|
11 |
|
12 #include "nsISeekableStream.h" |
|
13 #include "nsISupportsImpl.h" |
|
14 #include "prenv.h" |
|
15 #include "prlog.h" |
|
16 |
|
17 #ifdef PR_LOGGING |
|
18 extern PRLogModuleInfo* gMediaSourceLog; |
|
19 #define MSE_DEBUG(...) PR_LOG(gMediaSourceLog, PR_LOG_DEBUG, (__VA_ARGS__)) |
|
20 #else |
|
21 #define MSE_DEBUG(...) |
|
22 #endif |
|
23 |
|
24 namespace mozilla { |
|
25 |
|
26 namespace dom { |
|
27 |
|
28 class SourceBuffer; |
|
29 |
|
30 } // namespace dom |
|
31 |
|
32 nsresult |
|
33 SourceBufferResource::Close() |
|
34 { |
|
35 ReentrantMonitorAutoEnter mon(mMonitor); |
|
36 MSE_DEBUG("%p SBR::Close", this); |
|
37 //MOZ_ASSERT(!mClosed); |
|
38 mClosed = true; |
|
39 mon.NotifyAll(); |
|
40 return NS_OK; |
|
41 } |
|
42 |
|
43 nsresult |
|
44 SourceBufferResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes) |
|
45 { |
|
46 ReentrantMonitorAutoEnter mon(mMonitor); |
|
47 bool blockingRead = !!aBytes; |
|
48 |
|
49 while (blockingRead && |
|
50 !mEnded && |
|
51 mOffset + aCount > static_cast<uint64_t>(GetLength())) { |
|
52 MSE_DEBUG("%p SBR::Read waiting for data", this); |
|
53 mon.Wait(); |
|
54 } |
|
55 |
|
56 uint32_t available = GetLength() - mOffset; |
|
57 uint32_t count = std::min(aCount, available); |
|
58 if (!PR_GetEnv("MOZ_QUIET")) { |
|
59 MSE_DEBUG("%p SBR::Read aCount=%u length=%u offset=%u " |
|
60 "available=%u count=%u, blocking=%d bufComplete=%d", |
|
61 this, aCount, GetLength(), mOffset, available, count, |
|
62 blockingRead, mEnded); |
|
63 } |
|
64 if (available == 0) { |
|
65 MSE_DEBUG("%p SBR::Read EOF", this); |
|
66 *aBytes = 0; |
|
67 return NS_OK; |
|
68 } |
|
69 |
|
70 mInputBuffer.CopyData(mOffset, count, aBuffer); |
|
71 *aBytes = count; |
|
72 mOffset += count; |
|
73 return NS_OK; |
|
74 } |
|
75 |
|
76 nsresult |
|
77 SourceBufferResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) |
|
78 { |
|
79 ReentrantMonitorAutoEnter mon(mMonitor); |
|
80 nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset); |
|
81 if (NS_FAILED(rv)) { |
|
82 return rv; |
|
83 } |
|
84 return Read(aBuffer, aCount, aBytes); |
|
85 } |
|
86 |
|
87 nsresult |
|
88 SourceBufferResource::Seek(int32_t aWhence, int64_t aOffset) |
|
89 { |
|
90 ReentrantMonitorAutoEnter mon(mMonitor); |
|
91 if (mClosed) { |
|
92 return NS_ERROR_FAILURE; |
|
93 } |
|
94 |
|
95 int64_t newOffset = mOffset; |
|
96 switch (aWhence) { |
|
97 case nsISeekableStream::NS_SEEK_END: |
|
98 newOffset = GetLength() - aOffset; |
|
99 break; |
|
100 case nsISeekableStream::NS_SEEK_CUR: |
|
101 newOffset += aOffset; |
|
102 break; |
|
103 case nsISeekableStream::NS_SEEK_SET: |
|
104 newOffset = aOffset; |
|
105 break; |
|
106 } |
|
107 |
|
108 if (newOffset < 0 || uint64_t(newOffset) < mInputBuffer.GetOffset() || newOffset > GetLength()) { |
|
109 return NS_ERROR_FAILURE; |
|
110 } |
|
111 |
|
112 mOffset = newOffset; |
|
113 mon.NotifyAll(); |
|
114 |
|
115 return NS_OK; |
|
116 } |
|
117 |
|
118 nsresult |
|
119 SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) |
|
120 { |
|
121 ReentrantMonitorAutoEnter mon(mMonitor); |
|
122 nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset); |
|
123 if (NS_FAILED(rv)) { |
|
124 return rv; |
|
125 } |
|
126 return Read(aBuffer, aCount, nullptr); |
|
127 } |
|
128 |
|
129 bool |
|
130 SourceBufferResource::EvictData(uint32_t aThreshold) |
|
131 { |
|
132 return mInputBuffer.Evict(mOffset, aThreshold); |
|
133 } |
|
134 |
|
135 void |
|
136 SourceBufferResource::EvictBefore(uint64_t aOffset) |
|
137 { |
|
138 // If aOffset is past the current playback offset we don't evict. |
|
139 if (aOffset < mOffset) { |
|
140 mInputBuffer.Evict(aOffset, 0); |
|
141 } |
|
142 } |
|
143 |
|
144 void |
|
145 SourceBufferResource::AppendData(const uint8_t* aData, uint32_t aLength) |
|
146 { |
|
147 ReentrantMonitorAutoEnter mon(mMonitor); |
|
148 mInputBuffer.PushBack(new ResourceItem(aData, aLength)); |
|
149 mon.NotifyAll(); |
|
150 } |
|
151 |
|
152 void |
|
153 SourceBufferResource::Ended() |
|
154 { |
|
155 ReentrantMonitorAutoEnter mon(mMonitor); |
|
156 mEnded = true; |
|
157 mon.NotifyAll(); |
|
158 } |
|
159 |
|
160 SourceBufferResource::~SourceBufferResource() |
|
161 { |
|
162 MOZ_COUNT_DTOR(SourceBufferResource); |
|
163 MSE_DEBUG("%p SBR::~SBR", this); |
|
164 } |
|
165 |
|
166 SourceBufferResource::SourceBufferResource(nsIPrincipal* aPrincipal, |
|
167 const nsACString& aType) |
|
168 : mPrincipal(aPrincipal) |
|
169 , mType(aType) |
|
170 , mMonitor("mozilla::SourceBufferResource::mMonitor") |
|
171 , mOffset(0) |
|
172 , mClosed(false) |
|
173 , mEnded(false) |
|
174 { |
|
175 MOZ_COUNT_CTOR(SourceBufferResource); |
|
176 MSE_DEBUG("%p SBR::SBR()", this); |
|
177 } |
|
178 |
|
179 } // namespace mozilla |