forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsourcebuffer_impl.cc
263 lines (215 loc) · 8.97 KB
/
websourcebuffer_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/blink/websourcebuffer_impl.h"
#include <stdint.h>
#include <cmath>
#include <limits>
#include "base/bind.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/strings/string_number_conversions.h"
#include "media/base/media_tracks.h"
#include "media/base/timestamp_constants.h"
#include "media/filters/chunk_demuxer.h"
#include "third_party/blink/public/platform/web_media_player.h"
#include "third_party/blink/public/platform/web_source_buffer_client.h"
namespace media {
static blink::WebSourceBufferClient::ParseWarning ParseWarningToBlink(
const SourceBufferParseWarning warning) {
#define CHROMIUM_PARSE_WARNING_TO_BLINK_ENUM_CASE(name) \
case SourceBufferParseWarning::name: \
return blink::WebSourceBufferClient::ParseWarning::name
switch (warning) {
CHROMIUM_PARSE_WARNING_TO_BLINK_ENUM_CASE(
kKeyframeTimeGreaterThanDependant);
CHROMIUM_PARSE_WARNING_TO_BLINK_ENUM_CASE(kMuxedSequenceMode);
CHROMIUM_PARSE_WARNING_TO_BLINK_ENUM_CASE(
kGroupEndTimestampDecreaseWithinMediaSegment);
}
NOTREACHED();
return blink::WebSourceBufferClient::ParseWarning::
kKeyframeTimeGreaterThanDependant;
#undef CHROMIUM_PARSE_WARNING_TO_BLINK_ENUM_CASE
}
static base::TimeDelta DoubleToTimeDelta(double time) {
DCHECK(!std::isnan(time));
DCHECK_NE(time, -std::numeric_limits<double>::infinity());
if (time == std::numeric_limits<double>::infinity())
return kInfiniteDuration;
// Don't use base::TimeDelta::Max() here, as we want the largest finite time
// delta.
base::TimeDelta max_time = base::TimeDelta::FromInternalValue(
std::numeric_limits<int64_t>::max() - 1);
double max_time_in_seconds = max_time.InSecondsF();
if (time >= max_time_in_seconds)
return max_time;
return base::TimeDelta::FromMicroseconds(
time * base::Time::kMicrosecondsPerSecond);
}
WebSourceBufferImpl::WebSourceBufferImpl(const std::string& id,
ChunkDemuxer* demuxer)
: id_(id),
demuxer_(demuxer),
client_(nullptr),
append_window_end_(kInfiniteDuration) {
DCHECK(demuxer_);
demuxer_->SetTracksWatcher(
id, base::BindRepeating(&WebSourceBufferImpl::InitSegmentReceived,
base::Unretained(this)));
demuxer_->SetParseWarningCallback(
id, base::BindRepeating(&WebSourceBufferImpl::NotifyParseWarning,
base::Unretained(this)));
}
WebSourceBufferImpl::~WebSourceBufferImpl() = default;
void WebSourceBufferImpl::SetClient(blink::WebSourceBufferClient* client) {
DCHECK(client);
DCHECK(!client_);
client_ = client;
}
bool WebSourceBufferImpl::GetGenerateTimestampsFlag() {
return demuxer_->GetGenerateTimestampsFlag(id_);
}
bool WebSourceBufferImpl::SetMode(WebSourceBuffer::AppendMode mode) {
if (demuxer_->IsParsingMediaSegment(id_))
return false;
switch (mode) {
case WebSourceBuffer::kAppendModeSegments:
demuxer_->SetSequenceMode(id_, false);
return true;
case WebSourceBuffer::kAppendModeSequence:
demuxer_->SetSequenceMode(id_, true);
return true;
}
NOTREACHED();
return false;
}
blink::WebTimeRanges WebSourceBufferImpl::Buffered() {
Ranges<base::TimeDelta> ranges = demuxer_->GetBufferedRanges(id_);
blink::WebTimeRanges result(ranges.size());
for (size_t i = 0; i < ranges.size(); i++) {
result[i].start = ranges.start(i).InSecondsF();
result[i].end = ranges.end(i).InSecondsF();
}
return result;
}
double WebSourceBufferImpl::HighestPresentationTimestamp() {
return demuxer_->GetHighestPresentationTimestamp(id_).InSecondsF();
}
bool WebSourceBufferImpl::EvictCodedFrames(double currentPlaybackTime,
size_t newDataSize) {
return demuxer_->EvictCodedFrames(
id_,
base::TimeDelta::FromSecondsD(currentPlaybackTime),
newDataSize);
}
bool WebSourceBufferImpl::Append(const unsigned char* data,
unsigned length,
double* timestamp_offset) {
base::TimeDelta old_offset = timestamp_offset_;
bool success = demuxer_->AppendData(id_, data, length, append_window_start_,
append_window_end_, ×tamp_offset_);
// Coded frame processing may update the timestamp offset. If the caller
// provides a non-nullptr |timestamp_offset| and frame processing changes the
// timestamp offset, report the new offset to the caller. Do not update the
// caller's offset otherwise, to preserve any pre-existing value that may have
// more than microsecond precision.
if (timestamp_offset && old_offset != timestamp_offset_)
*timestamp_offset = timestamp_offset_.InSecondsF();
return success;
}
bool WebSourceBufferImpl::AppendChunks(
std::unique_ptr<media::StreamParser::BufferQueue> buffer_queue,
double* timestamp_offset) {
base::TimeDelta old_offset = timestamp_offset_;
bool success =
demuxer_->AppendChunks(id_, std::move(buffer_queue), append_window_start_,
append_window_end_, ×tamp_offset_);
// Like in ::Append, timestamp_offset may be updated by coded frame
// processing.
// TODO(crbug.com/1144908): Consider refactoring this common bit into helper.
if (timestamp_offset && old_offset != timestamp_offset_)
*timestamp_offset = timestamp_offset_.InSecondsF();
return success;
}
void WebSourceBufferImpl::ResetParserState() {
demuxer_->ResetParserState(id_,
append_window_start_, append_window_end_,
×tamp_offset_);
// TODO(wolenetz): resetParserState should be able to modify the caller
// timestamp offset (just like WebSourceBufferImpl::append).
// See http://crbug.com/370229 for further details.
}
void WebSourceBufferImpl::Remove(double start, double end) {
DCHECK_GE(start, 0);
DCHECK_GE(end, 0);
demuxer_->Remove(id_, DoubleToTimeDelta(start), DoubleToTimeDelta(end));
}
bool WebSourceBufferImpl::CanChangeType(const blink::WebString& content_type,
const blink::WebString& codecs) {
return demuxer_->CanChangeType(id_, content_type.Utf8(), codecs.Utf8());
}
void WebSourceBufferImpl::ChangeType(const blink::WebString& content_type,
const blink::WebString& codecs) {
// Caller must first call ResetParserState() to flush any pending frames.
DCHECK(!demuxer_->IsParsingMediaSegment(id_));
demuxer_->ChangeType(id_, content_type.Utf8(), codecs.Utf8());
}
bool WebSourceBufferImpl::SetTimestampOffset(double offset) {
if (demuxer_->IsParsingMediaSegment(id_))
return false;
timestamp_offset_ = DoubleToTimeDelta(offset);
// http://www.w3.org/TR/media-source/#widl-SourceBuffer-timestampOffset
// Step 6: If the mode attribute equals "sequence", then set the group start
// timestamp to new timestamp offset.
demuxer_->SetGroupStartTimestampIfInSequenceMode(id_, timestamp_offset_);
return true;
}
void WebSourceBufferImpl::SetAppendWindowStart(double start) {
DCHECK_GE(start, 0);
append_window_start_ = DoubleToTimeDelta(start);
}
void WebSourceBufferImpl::SetAppendWindowEnd(double end) {
DCHECK_GE(end, 0);
append_window_end_ = DoubleToTimeDelta(end);
}
void WebSourceBufferImpl::RemovedFromMediaSource() {
demuxer_->RemoveId(id_);
demuxer_ = nullptr;
client_ = nullptr;
}
blink::WebMediaPlayer::TrackType mediaTrackTypeToBlink(MediaTrack::Type type) {
switch (type) {
case MediaTrack::Audio:
return blink::WebMediaPlayer::kAudioTrack;
case MediaTrack::Text:
return blink::WebMediaPlayer::kTextTrack;
case MediaTrack::Video:
return blink::WebMediaPlayer::kVideoTrack;
}
NOTREACHED();
return blink::WebMediaPlayer::kAudioTrack;
}
void WebSourceBufferImpl::InitSegmentReceived(
std::unique_ptr<MediaTracks> tracks) {
DCHECK(tracks.get());
DVLOG(1) << __func__ << " tracks=" << tracks->tracks().size();
std::vector<blink::WebSourceBufferClient::MediaTrackInfo> trackInfoVector;
for (const auto& track : tracks->tracks()) {
blink::WebSourceBufferClient::MediaTrackInfo trackInfo;
trackInfo.track_type = mediaTrackTypeToBlink(track->type());
trackInfo.id = blink::WebString::FromUTF8(track->id().value());
trackInfo.byte_stream_track_id = blink::WebString::FromUTF8(
base::NumberToString(track->bytestream_track_id()));
trackInfo.kind = blink::WebString::FromUTF8(track->kind().value());
trackInfo.label = blink::WebString::FromUTF8(track->label().value());
trackInfo.language = blink::WebString::FromUTF8(track->language().value());
trackInfoVector.push_back(trackInfo);
}
client_->InitializationSegmentReceived(trackInfoVector);
}
void WebSourceBufferImpl::NotifyParseWarning(
const SourceBufferParseWarning warning) {
client_->NotifyParseWarning(ParseWarningToBlink(warning));
}
} // namespace media