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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
media / filters / source_buffer_state.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/filters/source_buffer_state.h"
#include <set>
#include <string_view>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/callback_helpers.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/types/cxx23_to_underlying.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "media/base/media_switches.h"
#include "media/base/media_track.h"
#include "media/base/media_tracks.h"
#include "media/base/mime_util.h"
#include "media/base/stream_parser.h"
#include "media/base/video_codec_string_parsers.h"
#include "media/filters/chunk_demuxer.h"
#include "media/filters/frame_processor.h"
#include "media/filters/source_buffer_stream.h"
#include "media/media_buildflags.h"
namespace media {
enum {
// Limits the number of MEDIA_LOG() calls warning the user that a muxed stream
// media segment is missing a block from at least one of the audio or video
// tracks.
kMaxMissingTrackInSegmentLogs = 10,
};
namespace {
base::TimeDelta EndTimestamp(const StreamParser::BufferQueue& queue) {
return queue.back()->timestamp() + queue.back()->duration();
}
// Check the input |bytestream_ids| and return false if
// duplicate track ids are detected.
bool CheckBytestreamTrackIds(const MediaTracks& tracks) {
std::set<StreamParser::TrackId> bytestream_ids;
for (const auto& track : tracks.tracks()) {
const StreamParser::TrackId& track_id = track->stream_id();
if (bytestream_ids.find(track_id) != bytestream_ids.end()) {
return false;
}
bytestream_ids.insert(track_id);
}
return true;
}
unsigned GetMSEBufferSizeLimitIfExists(std::string_view switch_string) {
auto* command_line = base::CommandLine::ForCurrentProcess();
unsigned memory_limit;
if (command_line->HasSwitch(switch_string) &&
base::StringToUint(command_line->GetSwitchValueASCII(switch_string),
&memory_limit)) {
return memory_limit * 1024 * 1024;
}
return 0;
}
} // namespace
// List of time ranges for each SourceBuffer.
// static
Ranges<base::TimeDelta> SourceBufferState::ComputeRangesIntersection(
const RangesList& active_ranges,
bool ended) {
// Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
// https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
// Step 1: If activeSourceBuffers.length equals 0 then return an empty
// TimeRanges object and abort these steps.
if (active_ranges.empty())
return Ranges<base::TimeDelta>();
// Step 2: Let active ranges be the ranges returned by buffered for each
// SourceBuffer object in activeSourceBuffers.
// Step 3: Let highest end time be the largest range end time in the active
// ranges.
base::TimeDelta highest_end_time;
for (const auto& range : active_ranges) {
if (!range.size())
continue;
highest_end_time = std::max(highest_end_time, range.end(range.size() - 1));
}
// Step 4: Let intersection ranges equal a TimeRange object containing a
// single range from 0 to highest end time.
Ranges<base::TimeDelta> intersection_ranges;
intersection_ranges.Add(base::TimeDelta(), highest_end_time);
// Step 5: For each SourceBuffer object in activeSourceBuffers run the
// following steps:
for (const auto& range : active_ranges) {
// Step 5.1: Let source ranges equal the ranges returned by the buffered
// attribute on the current SourceBuffer.
Ranges<base::TimeDelta> source_ranges = range;
// Step 5.2: If readyState is "ended", then set the end time on the last
// range in source ranges to highest end time.
if (ended && source_ranges.size()) {
source_ranges.Add(source_ranges.start(source_ranges.size() - 1),
highest_end_time);
}
// Step 5.3: Let new intersection ranges equal the intersection between
// the intersection ranges and the source ranges.
// Step 5.4: Replace the ranges in intersection ranges with the new
// intersection ranges.
intersection_ranges = intersection_ranges.IntersectionWith(source_ranges);
}
return intersection_ranges;
}
SourceBufferState::SourceBufferState(
std::unique_ptr<StreamParser> stream_parser,
std::unique_ptr<FrameProcessor> frame_processor,
CreateDemuxerStreamCB create_demuxer_stream_cb,
MediaLog* media_log)
: timestamp_offset_during_append_(nullptr),
parsing_media_segment_(false),
stream_parser_(stream_parser.release()),
frame_processor_(frame_processor.release()),
create_demuxer_stream_cb_(std::move(create_demuxer_stream_cb)),
media_log_(media_log),
state_(UNINITIALIZED) {
DCHECK(create_demuxer_stream_cb_);
DCHECK(frame_processor_);
}
SourceBufferState::~SourceBufferState() {
Shutdown();
}
void SourceBufferState::Init(StreamParser::InitCB init_cb,
std::optional<std::string_view> expected_codecs,
const StreamParser::EncryptedMediaInitDataCB&
encrypted_media_init_data_cb) {
DCHECK_EQ(state_, UNINITIALIZED);
init_cb_ = std::move(init_cb);
encrypted_media_init_data_cb_ = encrypted_media_init_data_cb;
state_ = PENDING_PARSER_CONFIG;
InitializeParser(expected_codecs);
}
void SourceBufferState::ChangeType(
std::unique_ptr<StreamParser> new_stream_parser,
const std::string& new_expected_codecs) {
DCHECK_GE(state_, PENDING_PARSER_CONFIG);
DCHECK_NE(state_, PENDING_PARSER_INIT);
DCHECK(!parsing_media_segment_);
// If this source buffer has already handled an initialization segment, avoid
// running |init_cb_| again later.
if (state_ == PARSER_INITIALIZED)
state_ = PENDING_PARSER_RECONFIG;
stream_parser_ = std::move(new_stream_parser);
InitializeParser(new_expected_codecs);
}
void SourceBufferState::SetSequenceMode(bool sequence_mode) {
DCHECK(!parsing_media_segment_);
frame_processor_->SetSequenceMode(sequence_mode);
}
void SourceBufferState::SetGroupStartTimestampIfInSequenceMode(
base::TimeDelta timestamp_offset) {
DCHECK(!parsing_media_segment_);
frame_processor_->SetGroupStartTimestampIfInSequenceMode(timestamp_offset);
}
void SourceBufferState::SetTracksWatcher(
Demuxer::MediaTracksUpdatedCB tracks_updated_cb) {
DCHECK(!init_segment_received_cb_);
DCHECK(tracks_updated_cb);
init_segment_received_cb_ = std::move(tracks_updated_cb);
}
void SourceBufferState::SetParseWarningCallback(
SourceBufferParseWarningCB parse_warning_cb) {
// Give the callback to |frame_processor_|; none of these warnings are
// currently emitted elsewhere.
frame_processor_->SetParseWarningCallback(std::move(parse_warning_cb));
}
bool SourceBufferState::AppendToParseBuffer(base::span<const uint8_t> data) {
return stream_parser_->AppendToParseBuffer(data);
}
StreamParser::ParseStatus SourceBufferState::RunSegmentParserLoop(
base::TimeDelta append_window_start,
base::TimeDelta append_window_end,
base::TimeDelta* timestamp_offset) {
DCHECK(!new_configs_possible_);
new_configs_possible_ = true;
DCHECK(timestamp_offset);
DCHECK(!timestamp_offset_during_append_);
append_window_start_during_append_ = append_window_start;
append_window_end_during_append_ = append_window_end;
timestamp_offset_during_append_ = timestamp_offset;
// TODO(wolenetz): Curry and pass a NewBuffersCB here bound with append window
// and timestamp offset pointer. See http://crbug.com/351454.
StreamParser::ParseStatus result =
stream_parser_->Parse(StreamParser::kMaxPendingBytesPerParse);
if (result == StreamParser::ParseStatus::kFailed) {
MEDIA_LOG(ERROR, media_log_)
<< __func__ << ": stream parsing failed. append_window_start="
<< append_window_start.InSecondsF()
<< " append_window_end=" << append_window_end.InSecondsF();
}
timestamp_offset_during_append_ = nullptr;
new_configs_possible_ = false;
return result;
}
bool SourceBufferState::AppendChunks(
std::unique_ptr<StreamParser::BufferQueue> buffer_queue,
base::TimeDelta append_window_start,
base::TimeDelta append_window_end,
base::TimeDelta* timestamp_offset) {
DCHECK(!new_configs_possible_);
new_configs_possible_ = true;
DCHECK(timestamp_offset);
DCHECK(!timestamp_offset_during_append_);
append_window_start_during_append_ = append_window_start;
append_window_end_during_append_ = append_window_end;
timestamp_offset_during_append_ = timestamp_offset;
// TODO(wolenetz): Curry and pass a NewBuffersCB here bound with append window
// and timestamp offset pointer. See http://crbug.com/351454.
bool result = stream_parser_->ProcessChunks(std::move(buffer_queue));
if (!result) {
MEDIA_LOG(ERROR, media_log_)
<< __func__ << ": Processing encoded chunks for buffering failed.";
}
timestamp_offset_during_append_ = nullptr;
new_configs_possible_ = false;
return result;
}
void SourceBufferState::ResetParserState(base::TimeDelta append_window_start,
base::TimeDelta append_window_end,
base::TimeDelta* timestamp_offset) {
DCHECK(timestamp_offset);
DCHECK(!timestamp_offset_during_append_);
timestamp_offset_during_append_ = timestamp_offset;
append_window_start_during_append_ = append_window_start;
append_window_end_during_append_ = append_window_end;
stream_parser_->Flush();
timestamp_offset_during_append_ = nullptr;
frame_processor_->Reset();
parsing_media_segment_ = false;
media_segment_has_data_for_track_.clear();
}
void SourceBufferState::Remove(base::TimeDelta start,
base::TimeDelta end,
base::TimeDelta duration) {
for (const auto& it : audio_streams_) {
it.second->Remove(start, end, duration);
}
for (const auto& it : video_streams_) {
it.second->Remove(start, end, duration);
}
}
bool SourceBufferState::EvictCodedFrames(base::TimeDelta media_time,
size_t newDataSize) {
size_t total_buffered_size = 0;
for (const auto& it : audio_streams_)
total_buffered_size += it.second->GetMemoryUsage();
for (const auto& it : video_streams_)
total_buffered_size += it.second->GetMemoryUsage();
DVLOG(3) << __func__ << " media_time=" << media_time.InSecondsF()
<< " newDataSize=" << newDataSize
<< " total_buffered_size=" << total_buffered_size;
if (total_buffered_size == 0)
return true;
bool success = true;
for (const auto& it : audio_streams_) {
uint64_t curr_size = it.second->GetMemoryUsage();
if (curr_size == 0)
continue;
uint64_t estimated_new_size = newDataSize * curr_size / total_buffered_size;
DCHECK_LE(estimated_new_size, SIZE_MAX);
success &= it.second->EvictCodedFrames(
media_time, static_cast<size_t>(estimated_new_size));
}
for (const auto& it : video_streams_) {
uint64_t curr_size = it.second->GetMemoryUsage();
if (curr_size == 0)
continue;
uint64_t estimated_new_size = newDataSize * curr_size / total_buffered_size;
DCHECK_LE(estimated_new_size, SIZE_MAX);
success &= it.second->EvictCodedFrames(
media_time, static_cast<size_t>(estimated_new_size));
}
DVLOG(3) << __func__ << " success=" << success;
return success;
}
void SourceBufferState::OnMemoryPressure(
base::TimeDelta media_time,
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level,
bool force_instant_gc) {
// TODO(sebmarchand): Check if MEMORY_PRESSURE_LEVEL_MODERATE should also be
// ignored.
if (memory_pressure_level ==
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) {
return;
}
// Notify video streams about memory pressure first, since video typically
// takes up the most memory and that's where we can expect most savings.
for (const auto& it : video_streams_) {
it.second->OnMemoryPressure(media_time, memory_pressure_level,
force_instant_gc);
}
for (const auto& it : audio_streams_) {
it.second->OnMemoryPressure(media_time, memory_pressure_level,
force_instant_gc);
}
}
Ranges<base::TimeDelta> SourceBufferState::GetBufferedRanges(
base::TimeDelta duration,
bool ended) const {
RangesList ranges_list;
for (const auto& it : audio_streams_)
ranges_list.push_back(it.second->GetBufferedRanges(duration));
for (const auto& it : video_streams_)
ranges_list.push_back(it.second->GetBufferedRanges(duration));
return ComputeRangesIntersection(ranges_list, ended);
}
base::TimeDelta SourceBufferState::GetLowestPresentationTimestamp() const {
base::TimeDelta min_pts = kInfiniteDuration;
for (const auto& it : audio_streams_) {
min_pts = std::min(min_pts, it.second->GetLowestPresentationTimestamp());
}
for (const auto& it : video_streams_) {
min_pts = std::min(min_pts, it.second->GetLowestPresentationTimestamp());
}
DCHECK_LE(base::TimeDelta(), min_pts);
if (min_pts == kInfiniteDuration) {
return base::TimeDelta();
}
return min_pts;
}
base::TimeDelta SourceBufferState::GetHighestPresentationTimestamp() const {
base::TimeDelta max_pts;
for (const auto& it : audio_streams_) {
max_pts = std::max(max_pts, it.second->GetHighestPresentationTimestamp());
}
for (const auto& it : video_streams_) {
max_pts = std::max(max_pts, it.second->GetHighestPresentationTimestamp());
}
return max_pts;
}
base::TimeDelta SourceBufferState::GetMaxBufferedDuration() const {
base::TimeDelta max_duration;
for (const auto& it : audio_streams_) {
max_duration = std::max(max_duration, it.second->GetBufferedDuration());
}
for (const auto& it : video_streams_) {
max_duration = std::max(max_duration, it.second->GetBufferedDuration());
}
return max_duration;
}
void SourceBufferState::StartReturningData() {
for (const auto& it : audio_streams_) {
it.second->StartReturningData();
}
for (const auto& it : video_streams_) {
it.second->StartReturningData();
}
}
void SourceBufferState::AbortReads() {
for (const auto& it : audio_streams_) {
it.second->AbortReads();
}
for (const auto& it : video_streams_) {
it.second->AbortReads();
}
}
void SourceBufferState::Seek(base::TimeDelta seek_time) {
for (const auto& it : audio_streams_) {
it.second->Seek(seek_time);
}
for (const auto& it : video_streams_) {
it.second->Seek(seek_time);
}
}
void SourceBufferState::CompletePendingReadIfPossible() {
for (const auto& it : audio_streams_) {
it.second->CompletePendingReadIfPossible();
}
for (const auto& it : video_streams_) {
it.second->CompletePendingReadIfPossible();
}
}
void SourceBufferState::OnSetDuration(base::TimeDelta duration) {
for (const auto& it : audio_streams_) {
it.second->OnSetDuration(duration);
}
for (const auto& it : video_streams_) {
it.second->OnSetDuration(duration);
}
}
void SourceBufferState::MarkEndOfStream() {
for (const auto& it : audio_streams_) {
it.second->MarkEndOfStream();
}
for (const auto& it : video_streams_) {
it.second->MarkEndOfStream();
}
}
void SourceBufferState::UnmarkEndOfStream() {
for (const auto& it : audio_streams_) {
it.second->UnmarkEndOfStream();
}
for (const auto& it : video_streams_) {
it.second->UnmarkEndOfStream();
}
}
void SourceBufferState::Shutdown() {
for (const auto& it : audio_streams_) {
it.second->Shutdown();
}
for (const auto& it : video_streams_) {
it.second->Shutdown();
}
}
void SourceBufferState::SetMemoryLimits(DemuxerStream::Type type,
size_t memory_limit) {
switch (type) {
case DemuxerStream::AUDIO:
for (const auto& it : audio_streams_) {
it.second->SetStreamMemoryLimit(memory_limit);
}
break;
case DemuxerStream::VIDEO:
for (const auto& it : video_streams_) {
it.second->SetStreamMemoryLimit(memory_limit);
}
break;
case DemuxerStream::UNKNOWN:
NOTREACHED();
}
}
bool SourceBufferState::IsSeekWaitingForData() const {
for (const auto& it : audio_streams_) {
if (it.second->IsSeekWaitingForData())
return true;
}
for (const auto& it : video_streams_) {
if (it.second->IsSeekWaitingForData())
return true;
}
return false;
}
void SourceBufferState::InitializeParser(
std::optional<std::string_view> expected_codecs) {
expected_audio_codecs_.clear();
expected_video_codecs_.clear();
std::vector<std::string> expected_codecs_parsed;
if ((strict_codec_expectations_ = expected_codecs.has_value())) {
DVLOG(1) << __func__ << " expected_codecs=" << expected_codecs.value();
SplitCodecs(expected_codecs.value(), &expected_codecs_parsed);
}
std::vector<AudioCodec> expected_acodecs;
std::vector<VideoCodec> expected_vcodecs;
for (const auto& codec_id : expected_codecs_parsed) {
AudioCodec acodec = StringToAudioCodec(codec_id);
if (acodec != AudioCodec::kUnknown) {
expected_audio_codecs_.push_back(acodec);
continue;
}
VideoCodec vcodec = StringToVideoCodec(codec_id);
if (vcodec != VideoCodec::kUnknown) {
expected_video_codecs_.push_back(vcodec);
continue;
}
MEDIA_LOG(INFO, media_log_) << "Unrecognized media codec: " << codec_id;
}
stream_parser_->Init(
base::BindOnce(&SourceBufferState::OnSourceInitDone,
base::Unretained(this)),
base::BindRepeating(&SourceBufferState::OnNewConfigs,
base::Unretained(this)),
base::BindRepeating(&SourceBufferState::OnNewBuffers,
base::Unretained(this)),
base::BindRepeating(&SourceBufferState::OnEncryptedMediaInitData,
base::Unretained(this)),
base::BindRepeating(&SourceBufferState::OnNewMediaSegment,
base::Unretained(this)),
base::BindRepeating(&SourceBufferState::OnEndOfMediaSegment,
base::Unretained(this)),
media_log_);
}
bool SourceBufferState::OnNewConfigs(std::unique_ptr<MediaTracks> tracks) {
DCHECK(tracks.get());
DCHECK_GE(state_, PENDING_PARSER_CONFIG);
// Check that there is no clashing bytestream track ids.
if (!CheckBytestreamTrackIds(*tracks)) {
MEDIA_LOG(ERROR, media_log_) << "Duplicate bytestream track ids detected";
for (const auto& track : tracks->tracks()) {
const StreamParser::TrackId& track_id = track->stream_id();
MEDIA_LOG(DEBUG, media_log_) << TrackTypeToStr(track->type()) << " track "
<< " bytestream track id=" << track_id;
}
return false;
}
// MSE spec allows new configs to be emitted only during
// RunSegmentParserLoop(), but not during Flush or parser reset operations.
CHECK(new_configs_possible_);
bool success = true;
// TODO(wolenetz): Update codec string strictness, if necessary, once spec
// issue https://github.com/w3c/media-source/issues/161 is resolved.
std::vector<AudioCodec> expected_acodecs = expected_audio_codecs_;
std::vector<VideoCodec> expected_vcodecs = expected_video_codecs_;
// TODO(wolenetz): Once codec strictness is relaxed, we can change
// |allow_codec_changes| to always be true. Until then, we only allow codec
// changes on explicit ChangeType().
const bool allow_codec_changes = state_ == PENDING_PARSER_RECONFIG;
FrameProcessor::TrackIdChanges track_id_changes;
for (const auto& track : tracks->tracks()) {
const auto& track_id = track->stream_id();
if (track->type() == MediaTrack::Type::kAudio) {
AudioDecoderConfig audio_config = tracks->getAudioConfig(track_id);
DVLOG(1) << "Audio track_id=" << track_id
<< " config: " << audio_config.AsHumanReadableString();
DCHECK(audio_config.IsValidConfig());
if (strict_codec_expectations_) {
const auto& it =
base::ranges::find(expected_acodecs, audio_config.codec());
if (it == expected_acodecs.end()) {
MEDIA_LOG(ERROR, media_log_)
<< "Audio stream codec " << GetCodecName(audio_config.codec())
<< " doesn't match SourceBuffer codecs.";
return false;
}
expected_acodecs.erase(it);
}
ChunkDemuxerStream* stream = nullptr;
if (!first_init_segment_received_) {
DCHECK(audio_streams_.find(track_id) == audio_streams_.end());
stream = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
if (!stream || !frame_processor_->AddTrack(track_id, stream)) {
MEDIA_LOG(ERROR, media_log_) << "Failed to create audio stream.";
return false;
}
audio_streams_[track_id] = stream;
media_log_->SetProperty<MediaLogProperty::kAudioTracks>(
std::vector<AudioDecoderConfig>{audio_config});
} else {
if (audio_streams_.size() > 1) {
auto stream_it = audio_streams_.find(track_id);
if (stream_it != audio_streams_.end())
stream = stream_it->second;
} else {
// If there is only one audio track then bytestream id might change in
// a new init segment. So update our state and notify frame processor.
const auto& stream_it = audio_streams_.begin();
if (stream_it != audio_streams_.end()) {
stream = stream_it->second;
if (stream_it->first != track_id) {
track_id_changes[stream_it->first] = track_id;
audio_streams_[track_id] = stream;
audio_streams_.erase(stream_it->first);
}
}
}
if (!stream) {
MEDIA_LOG(ERROR, media_log_) << "Got unexpected audio track"
<< " track_id=" << track_id;
return false;
}
}
track->set_id(stream->media_track_id());
frame_processor_->OnPossibleAudioConfigUpdate(audio_config);
success &= stream->UpdateAudioConfig(audio_config, allow_codec_changes,
media_log_);
} else if (track->type() == MediaTrack::Type::kVideo) {
VideoDecoderConfig video_config = tracks->getVideoConfig(track_id);
DVLOG(1) << "Video track_id=" << track_id
<< " config: " << video_config.AsHumanReadableString();
DCHECK(video_config.IsValidConfig());
#if BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)
// When ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION is true, in general
// encrypted Dolby Vision is allowed while clear Dolby Vision is not.
if (video_config.codec() == VideoCodec::kDolbyVision) {
// If `kPlatformEncryptedDolbyVision` is disabled, encrypted Dolby
// Vision is also not allowed, so just return false.
if (!base::FeatureList::IsEnabled(kPlatformEncryptedDolbyVision)) {
MEDIA_LOG(ERROR, media_log_)
<< "MSE playback of DolbyVision is not supported because "
"kPlatformEncryptedDolbyVision feature is disabled.";
return false;
}
// If `kAllowClearDolbyVisionInMseWhenPlatformEncryptedDvEnabled` is
// specified which force allow Dolby Vision in Media Source.
if (!base::FeatureList::IsEnabled(
kAllowClearDolbyVisionInMseWhenPlatformEncryptedDvEnabled) &&
!video_config.is_encrypted()) {
MEDIA_LOG(ERROR, media_log_)
<< "MSE playback of DolbyVision is only supported via platform "
"decryptor, but the provided DV track is not encrypted.";
return false;
}
}
#endif // BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)
if (strict_codec_expectations_) {
const auto& it =
base::ranges::find(expected_vcodecs, video_config.codec());
if (it == expected_vcodecs.end()) {
MEDIA_LOG(ERROR, media_log_)
<< "Video stream codec " << GetCodecName(video_config.codec())
<< " doesn't match SourceBuffer codecs.";
return false;
}
expected_vcodecs.erase(it);
}
ChunkDemuxerStream* stream = nullptr;
if (!first_init_segment_received_) {
DCHECK(video_streams_.find(track_id) == video_streams_.end());
stream = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
if (!stream || !frame_processor_->AddTrack(track_id, stream)) {
MEDIA_LOG(ERROR, media_log_) << "Failed to create video stream.";
return false;
}
video_streams_[track_id] = stream;
media_log_->SetProperty<MediaLogProperty::kVideoTracks>(
std::vector<VideoDecoderConfig>{video_config});
} else {
if (video_streams_.size() > 1) {
auto stream_it = video_streams_.find(track_id);
if (stream_it != video_streams_.end())
stream = stream_it->second;
} else {
// If there is only one video track then bytestream id might change in
// a new init segment. So update our state and notify frame processor.
const auto& stream_it = video_streams_.begin();
if (stream_it != video_streams_.end()) {
stream = stream_it->second;
if (stream_it->first != track_id) {
track_id_changes[stream_it->first] = track_id;
video_streams_[track_id] = stream;
video_streams_.erase(stream_it->first);
}
}
}
if (!stream) {
MEDIA_LOG(ERROR, media_log_) << "Got unexpected video track"
<< " track_id=" << track_id;
return false;
}
}
track->set_id(stream->media_track_id());
success &= stream->UpdateVideoConfig(video_config, allow_codec_changes,
media_log_);
} else {
MEDIA_LOG(ERROR, media_log_) << "Error: unsupported media track type "
<< base::to_underlying(track->type());
return false;
}
}
if (!expected_acodecs.empty() || !expected_vcodecs.empty()) {
for (const auto& acodec : expected_acodecs) {
MEDIA_LOG(ERROR, media_log_) << "Initialization segment misses expected "
<< GetCodecName(acodec) << " track.";
}
for (const auto& vcodec : expected_vcodecs) {
MEDIA_LOG(ERROR, media_log_) << "Initialization segment misses expected "
<< GetCodecName(vcodec) << " track.";
}
return false;
}
if (audio_streams_.empty() && video_streams_.empty()) {
DVLOG(1) << __func__ << ": couldn't find a valid audio or video stream";
return false;
}
if (!frame_processor_->UpdateTrackIds(track_id_changes)) {
DVLOG(1) << __func__ << ": failed to remap track ids in frame processor";
return false;
}
frame_processor_->SetAllTrackBuffersNeedRandomAccessPoint();
if (!first_init_segment_received_) {
first_init_segment_received_ = true;
SetStreamMemoryLimits();
}
DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
if (success) {
if (state_ == PENDING_PARSER_CONFIG)
state_ = PENDING_PARSER_INIT;
if (state_ == PENDING_PARSER_RECONFIG)
state_ = PENDING_PARSER_REINIT;
DCHECK(init_segment_received_cb_);
init_segment_received_cb_.Run(std::move(tracks));
}
return success;
}
void SourceBufferState::SetStreamMemoryLimits() {
size_t audio_buf_size_limit =
GetMSEBufferSizeLimitIfExists(switches::kMSEAudioBufferSizeLimitMb);
if (audio_buf_size_limit) {
MEDIA_LOG(INFO, media_log_)
<< "Custom audio per-track SourceBuffer size limit="
<< audio_buf_size_limit;
for (const auto& it : audio_streams_)
it.second->SetStreamMemoryLimit(audio_buf_size_limit);
}
size_t video_buf_size_limit =
GetMSEBufferSizeLimitIfExists(switches::kMSEVideoBufferSizeLimitMb);
if (video_buf_size_limit) {
MEDIA_LOG(INFO, media_log_)
<< "Custom video per-track SourceBuffer size limit="
<< video_buf_size_limit;
for (const auto& it : video_streams_)
it.second->SetStreamMemoryLimit(video_buf_size_limit);
}
}
void SourceBufferState::OnNewMediaSegment() {
DVLOG(2) << "OnNewMediaSegment()";
DCHECK_EQ(state_, PARSER_INITIALIZED);
parsing_media_segment_ = true;
media_segment_has_data_for_track_.clear();
}
void SourceBufferState::OnEndOfMediaSegment() {
DVLOG(2) << "OnEndOfMediaSegment()";
DCHECK_EQ(state_, PARSER_INITIALIZED);
parsing_media_segment_ = false;
for (const auto& it : audio_streams_) {
if (!media_segment_has_data_for_track_[it.first]) {
LIMITED_MEDIA_LOG(DEBUG, media_log_, num_missing_track_logs_,
kMaxMissingTrackInSegmentLogs)
<< "Media segment did not contain any coded frames for track "
<< it.first << ", mismatching initialization segment. Therefore, MSE"
" coded frame processing may not interoperably detect"
" discontinuities in appended media.";
}
}
for (const auto& it : video_streams_) {
if (!media_segment_has_data_for_track_[it.first]) {
LIMITED_MEDIA_LOG(DEBUG, media_log_, num_missing_track_logs_,
kMaxMissingTrackInSegmentLogs)
<< "Media segment did not contain any coded frames for track "
<< it.first << ", mismatching initialization segment. Therefore, MSE"
" coded frame processing may not interoperably detect"
" discontinuities in appended media.";
}
}
}
bool SourceBufferState::OnNewBuffers(
const StreamParser::BufferQueueMap& buffer_queue_map) {
DVLOG(2) << __func__ << " buffer_queues=" << buffer_queue_map.size();
DCHECK_EQ(state_, PARSER_INITIALIZED);
DCHECK(timestamp_offset_during_append_);
DCHECK(parsing_media_segment_);
for (const auto& [track_id, buffer_queue] : buffer_queue_map) {
DCHECK(!buffer_queue.empty());
media_segment_has_data_for_track_[track_id] = true;
}
const base::TimeDelta timestamp_offset_before_processing =
*timestamp_offset_during_append_;
// Calculate the new timestamp offset for audio/video tracks if the stream
// parser corresponds to MSE MIME type with 'Generate Timestamps Flag' set
// true.
base::TimeDelta predicted_timestamp_offset =
timestamp_offset_before_processing;
if (generate_timestamps_flag()) {
base::TimeDelta min_end_timestamp = kNoTimestamp;
for (const auto& [track_id, buffer_queue] : buffer_queue_map) {
DCHECK(!buffer_queue.empty());
if (min_end_timestamp == kNoTimestamp ||
EndTimestamp(buffer_queue) < min_end_timestamp) {
min_end_timestamp = EndTimestamp(buffer_queue);
DCHECK_NE(kNoTimestamp, min_end_timestamp);
}
}
if (min_end_timestamp != kNoTimestamp)
predicted_timestamp_offset += min_end_timestamp;
}
if (!frame_processor_->ProcessFrames(
buffer_queue_map, append_window_start_during_append_,
append_window_end_during_append_, timestamp_offset_during_append_)) {
return false;
}
// Only update the timestamp offset if the frame processor hasn't already.
if (generate_timestamps_flag() &&
timestamp_offset_before_processing == *timestamp_offset_during_append_) {
// TODO(wolenetz): This prediction assumes the last frame in each track
// isn't dropped by append window trimming. See https://crbug.com/850316.
*timestamp_offset_during_append_ = predicted_timestamp_offset;
}
return true;
}
void SourceBufferState::OnEncryptedMediaInitData(
EmeInitDataType type,
const std::vector<uint8_t>& init_data) {
encrypted_media_init_data_reported_ = true;
encrypted_media_init_data_cb_.Run(type, init_data);
}
void SourceBufferState::OnSourceInitDone(
const StreamParser::InitParameters& params) {
// We've either yet-to-run |init_cb_| if pending init, or we've previously
// run it if pending reinit.
DCHECK((init_cb_ && state_ == PENDING_PARSER_INIT) ||
(!init_cb_ && state_ == PENDING_PARSER_REINIT));
State old_state = state_;
state_ = PARSER_INITIALIZED;
if (old_state == PENDING_PARSER_INIT)
std::move(init_cb_).Run(params);
}
} // namespace media