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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
|
// Copyright (c) 2012 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/filters/chunk_demuxer.h"
#include <algorithm>
#include <limits>
#include <list>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/stl_util.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/stream_parser_buffer.h"
#include "media/base/timestamp_constants.h"
#include "media/base/video_decoder_config.h"
#include "media/filters/frame_processor.h"
#include "media/filters/stream_parser_factory.h"
using base::TimeDelta;
namespace media {
static TimeDelta EndTimestamp(const StreamParser::BufferQueue& queue) {
return queue.back()->timestamp() + queue.back()->duration();
}
// List of time ranges for each SourceBuffer.
typedef std::list<Ranges<TimeDelta> > RangesList;
static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
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 (activeRanges.empty())
return Ranges<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.
TimeDelta highest_end_time;
for (RangesList::const_iterator itr = activeRanges.begin();
itr != activeRanges.end(); ++itr) {
if (!itr->size())
continue;
highest_end_time = std::max(highest_end_time, itr->end(itr->size() - 1));
}
// Step 4: Let intersection ranges equal a TimeRange object containing a
// single range from 0 to highest end time.
Ranges<TimeDelta> intersection_ranges;
intersection_ranges.Add(TimeDelta(), highest_end_time);
// Step 5: For each SourceBuffer object in activeSourceBuffers run the
// following steps:
for (RangesList::const_iterator itr = activeRanges.begin();
itr != activeRanges.end(); ++itr) {
// Step 5.1: Let source ranges equal the ranges returned by the buffered
// attribute on the current SourceBuffer.
Ranges<TimeDelta> source_ranges = *itr;
// 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() > 0u) {
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;
}
// Contains state belonging to a source id.
// TODO: SourceState needs to be moved to a separate file and covered with unit
// tests (see crbug.com/525836)
class SourceState {
public:
// Callback signature used to create ChunkDemuxerStreams.
typedef base::Callback<ChunkDemuxerStream*(
DemuxerStream::Type)> CreateDemuxerStreamCB;
typedef ChunkDemuxer::InitSegmentReceivedCB InitSegmentReceivedCB;
typedef base::Callback<void(
ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
SourceState(scoped_ptr<StreamParser> stream_parser,
scoped_ptr<FrameProcessor> frame_processor,
const CreateDemuxerStreamCB& create_demuxer_stream_cb,
const scoped_refptr<MediaLog>& media_log);
~SourceState();
void Init(const StreamParser::InitCB& init_cb,
bool allow_audio,
bool allow_video,
const StreamParser::EncryptedMediaInitDataCB&
encrypted_media_init_data_cb,
const NewTextTrackCB& new_text_track_cb);
// Appends new data to the StreamParser.
// Returns true if the data was successfully appended. Returns false if an
// error occurred. |*timestamp_offset| is used and possibly updated by the
// append. |append_window_start| and |append_window_end| correspond to the MSE
// spec's similarly named source buffer attributes that are used in coded
// frame processing. |init_segment_received_cb| is run for each new fully
// parsed initialization segment.
bool Append(const uint8* data,
size_t length,
TimeDelta append_window_start,
TimeDelta append_window_end,
TimeDelta* timestamp_offset,
const InitSegmentReceivedCB& init_segment_received_cb);
// Aborts the current append sequence and resets the parser.
void Abort(TimeDelta append_window_start,
TimeDelta append_window_end,
TimeDelta* timestamp_offset);
// Calls Remove(|start|, |end|, |duration|) on all
// ChunkDemuxerStreams managed by this object.
void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
// If the buffer is full, attempts to try to free up space, as specified in
// the "Coded Frame Eviction Algorithm" in the Media Source Extensions Spec.
// Returns false iff buffer is still full after running eviction.
// https://w3c.github.io/media-source/#sourcebuffer-coded-frame-eviction
bool EvictCodedFrames(DecodeTimestamp media_time, size_t newDataSize);
// Returns true if currently parsing a media segment, or false otherwise.
bool parsing_media_segment() const { return parsing_media_segment_; }
// Sets |frame_processor_|'s sequence mode to |sequence_mode|.
void SetSequenceMode(bool sequence_mode);
// Signals the coded frame processor to update its group start timestamp to be
// |timestamp_offset| if it is in sequence append mode.
void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset);
// Returns the range of buffered data in this source, capped at |duration|.
// |ended| - Set to true if end of stream has been signaled and the special
// end of stream range logic needs to be executed.
Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
// Returns the highest buffered duration across all streams managed
// by this object.
// Returns TimeDelta() if none of the streams contain buffered data.
TimeDelta GetMaxBufferedDuration() const;
// Helper methods that call methods with similar names on all the
// ChunkDemuxerStreams managed by this object.
void StartReturningData();
void AbortReads();
void Seek(TimeDelta seek_time);
void CompletePendingReadIfPossible();
void OnSetDuration(TimeDelta duration);
void MarkEndOfStream();
void UnmarkEndOfStream();
void Shutdown();
// Sets the memory limit on each stream of a specific type.
// |memory_limit| is the maximum number of bytes each stream of type |type|
// is allowed to hold in its buffer.
void SetMemoryLimits(DemuxerStream::Type type, size_t memory_limit);
bool IsSeekWaitingForData() const;
private:
// Called by the |stream_parser_| when a new initialization segment is
// encountered.
// Returns true on a successful call. Returns false if an error occurred while
// processing decoder configurations.
bool OnNewConfigs(bool allow_audio, bool allow_video,
const AudioDecoderConfig& audio_config,
const VideoDecoderConfig& video_config,
const StreamParser::TextTrackConfigMap& text_configs);
// Called by the |stream_parser_| at the beginning of a new media segment.
void OnNewMediaSegment();
// Called by the |stream_parser_| at the end of a media segment.
void OnEndOfMediaSegment();
// Called by the |stream_parser_| when new buffers have been parsed.
// It processes the new buffers using |frame_processor_|, which includes
// appending the processed frames to associated demuxer streams for each
// frame's track.
// Returns true on a successful call. Returns false if an error occurred while
// processing the buffers.
bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
const StreamParser::BufferQueue& video_buffers,
const StreamParser::TextBufferQueueMap& text_map);
void OnSourceInitDone(const StreamParser::InitParameters& params);
// EstimateVideoDataSize uses some heuristics to estimate the size of the
// video size in the chunk of muxed audio/video data without parsing it.
// This is used by EvictCodedFrames algorithm, which happens before Append
// (and therefore before parsing is performed) to prepare space for new data.
size_t EstimateVideoDataSize(size_t muxed_data_chunk_size) const;
CreateDemuxerStreamCB create_demuxer_stream_cb_;
NewTextTrackCB new_text_track_cb_;
// During Append(), if OnNewBuffers() coded frame processing updates the
// timestamp offset then |*timestamp_offset_during_append_| is also updated
// so Append()'s caller can know the new offset. This pointer is only non-NULL
// during the lifetime of an Append() call.
TimeDelta* timestamp_offset_during_append_;
// During Append(), coded frame processing triggered by OnNewBuffers()
// requires these two attributes. These are only valid during the lifetime of
// an Append() call.
TimeDelta append_window_start_during_append_;
TimeDelta append_window_end_during_append_;
// Set to true if the next buffers appended within the append window
// represent the start of a new media segment. This flag being set
// triggers a call to |new_segment_cb_| when the new buffers are
// appended. The flag is set on actual media segment boundaries and
// when the "append window" filtering causes discontinuities in the
// appended data.
// TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
// processing's discontinuity logic is enough. See http://crbug.com/351489.
bool new_media_segment_;
// Keeps track of whether a media segment is being parsed.
bool parsing_media_segment_;
// The object used to parse appended data.
scoped_ptr<StreamParser> stream_parser_;
ChunkDemuxerStream* audio_; // Not owned by |this|.
ChunkDemuxerStream* video_; // Not owned by |this|.
typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
TextStreamMap text_stream_map_; // |this| owns the map's stream pointers.
scoped_ptr<FrameProcessor> frame_processor_;
scoped_refptr<MediaLog> media_log_;
StreamParser::InitCB init_cb_;
// During Append(), OnNewConfigs() will trigger the initialization segment
// received algorithm. This callback is only non-NULL during the lifetime of
// an Append() call. Note, the MSE spec explicitly disallows this algorithm
// during an Abort(), since Abort() is allowed only to emit coded frames, and
// only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment).
InitSegmentReceivedCB init_segment_received_cb_;
// Indicates that timestampOffset should be updated automatically during
// OnNewBuffers() based on the earliest end timestamp of the buffers provided.
// TODO(wolenetz): Refactor this function while integrating April 29, 2014
// changes to MSE spec. See http://crbug.com/371499.
bool auto_update_timestamp_offset_;
DISALLOW_COPY_AND_ASSIGN(SourceState);
};
SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
scoped_ptr<FrameProcessor> frame_processor,
const CreateDemuxerStreamCB& create_demuxer_stream_cb,
const scoped_refptr<MediaLog>& media_log)
: create_demuxer_stream_cb_(create_demuxer_stream_cb),
timestamp_offset_during_append_(NULL),
new_media_segment_(false),
parsing_media_segment_(false),
stream_parser_(stream_parser.release()),
audio_(NULL),
video_(NULL),
frame_processor_(frame_processor.release()),
media_log_(media_log),
auto_update_timestamp_offset_(false) {
DCHECK(!create_demuxer_stream_cb_.is_null());
DCHECK(frame_processor_);
}
SourceState::~SourceState() {
Shutdown();
STLDeleteValues(&text_stream_map_);
}
void SourceState::Init(
const StreamParser::InitCB& init_cb,
bool allow_audio,
bool allow_video,
const StreamParser::EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
const NewTextTrackCB& new_text_track_cb) {
new_text_track_cb_ = new_text_track_cb;
init_cb_ = init_cb;
stream_parser_->Init(
base::Bind(&SourceState::OnSourceInitDone, base::Unretained(this)),
base::Bind(&SourceState::OnNewConfigs, base::Unretained(this),
allow_audio, allow_video),
base::Bind(&SourceState::OnNewBuffers, base::Unretained(this)),
new_text_track_cb_.is_null(), encrypted_media_init_data_cb,
base::Bind(&SourceState::OnNewMediaSegment, base::Unretained(this)),
base::Bind(&SourceState::OnEndOfMediaSegment, base::Unretained(this)),
media_log_);
}
void SourceState::SetSequenceMode(bool sequence_mode) {
DCHECK(!parsing_media_segment_);
frame_processor_->SetSequenceMode(sequence_mode);
}
void SourceState::SetGroupStartTimestampIfInSequenceMode(
base::TimeDelta timestamp_offset) {
DCHECK(!parsing_media_segment_);
frame_processor_->SetGroupStartTimestampIfInSequenceMode(timestamp_offset);
}
bool SourceState::Append(
const uint8* data,
size_t length,
TimeDelta append_window_start,
TimeDelta append_window_end,
TimeDelta* timestamp_offset,
const InitSegmentReceivedCB& init_segment_received_cb) {
DCHECK(timestamp_offset);
DCHECK(!timestamp_offset_during_append_);
DCHECK(!init_segment_received_cb.is_null());
DCHECK(init_segment_received_cb_.is_null());
append_window_start_during_append_ = append_window_start;
append_window_end_during_append_ = append_window_end;
timestamp_offset_during_append_ = timestamp_offset;
init_segment_received_cb_= init_segment_received_cb;
// TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
// append window and timestamp offset pointer. See http://crbug.com/351454.
bool result = stream_parser_->Parse(data, length);
if (!result) {
MEDIA_LOG(ERROR, media_log_)
<< __FUNCTION__ << ": stream parsing failed."
<< " Data size=" << length
<< " append_window_start=" << append_window_start.InSecondsF()
<< " append_window_end=" << append_window_end.InSecondsF();
}
timestamp_offset_during_append_ = NULL;
init_segment_received_cb_.Reset();
return result;
}
void SourceState::Abort(TimeDelta append_window_start,
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_ = NULL;
frame_processor_->Reset();
parsing_media_segment_ = false;
}
void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
if (audio_)
audio_->Remove(start, end, duration);
if (video_)
video_->Remove(start, end, duration);
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->Remove(start, end, duration);
}
}
size_t SourceState::EstimateVideoDataSize(size_t muxed_data_chunk_size) const {
DCHECK(audio_);
DCHECK(video_);
size_t videoBufferedSize = video_->GetBufferedSize();
size_t audioBufferedSize = audio_->GetBufferedSize();
if (videoBufferedSize == 0 || audioBufferedSize == 0) {
// At this point either audio or video buffer is empty, which means buffer
// levels are probably low anyway and we should have enough space in the
// buffers for appending new data, so just take a very rough guess.
return muxed_data_chunk_size / 2;
}
// We need to estimate how much audio and video data is going to be in the
// newly appended data chunk to make space for the new data. And we need to do
// that without parsing the data (which will happen later, in the Append
// phase). So for now we can only rely on some heuristic here. Let's assume
// that the proportion of the audio/video in the new data chunk is the same as
// the current ratio of buffered audio/video.
// Longer term this should go away once we further change the MSE GC algorithm
// to work across all streams of a SourceBuffer (see crbug.com/520704).
double videoBufferedSizeF = static_cast<double>(videoBufferedSize);
double audioBufferedSizeF = static_cast<double>(audioBufferedSize);
double totalBufferedSizeF = videoBufferedSizeF + audioBufferedSizeF;
CHECK_GT(totalBufferedSizeF, 0.0);
double videoRatio = videoBufferedSizeF / totalBufferedSizeF;
CHECK_GE(videoRatio, 0.0);
CHECK_LE(videoRatio, 1.0);
double estimatedVideoSize = muxed_data_chunk_size * videoRatio;
return static_cast<size_t>(estimatedVideoSize);
}
bool SourceState::EvictCodedFrames(DecodeTimestamp media_time,
size_t newDataSize) {
bool success = true;
DVLOG(3) << __FUNCTION__ << " media_time=" << media_time.InSecondsF()
<< " newDataSize=" << newDataSize
<< " videoBufferedSize=" << (video_ ? video_->GetBufferedSize() : 0)
<< " audioBufferedSize=" << (audio_ ? audio_->GetBufferedSize() : 0);
size_t newAudioSize = 0;
size_t newVideoSize = 0;
if (audio_ && video_) {
newVideoSize = EstimateVideoDataSize(newDataSize);
newAudioSize = newDataSize - newVideoSize;
} else if (video_) {
newVideoSize = newDataSize;
} else if (audio_) {
newAudioSize = newDataSize;
}
DVLOG(3) << __FUNCTION__ << " estimated audio/video sizes: "
<< " newVideoSize=" << newVideoSize
<< " newAudioSize=" << newAudioSize;
if (audio_)
success = audio_->EvictCodedFrames(media_time, newAudioSize) && success;
if (video_)
success = video_->EvictCodedFrames(media_time, newVideoSize) && success;
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
success = itr->second->EvictCodedFrames(media_time, 0) && success;
}
DVLOG(3) << __FUNCTION__ << " result=" << success
<< " videoBufferedSize=" << (video_ ? video_->GetBufferedSize() : 0)
<< " audioBufferedSize=" << (audio_ ? audio_->GetBufferedSize() : 0);
return success;
}
Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
bool ended) const {
// TODO(acolwell): When we start allowing disabled tracks we'll need to update
// this code to only add ranges from active tracks.
RangesList ranges_list;
if (audio_)
ranges_list.push_back(audio_->GetBufferedRanges(duration));
if (video_)
ranges_list.push_back(video_->GetBufferedRanges(duration));
for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
ranges_list.push_back(itr->second->GetBufferedRanges(duration));
}
return ComputeIntersection(ranges_list, ended);
}
TimeDelta SourceState::GetMaxBufferedDuration() const {
TimeDelta max_duration;
if (audio_)
max_duration = std::max(max_duration, audio_->GetBufferedDuration());
if (video_)
max_duration = std::max(max_duration, video_->GetBufferedDuration());
for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
}
return max_duration;
}
void SourceState::StartReturningData() {
if (audio_)
audio_->StartReturningData();
if (video_)
video_->StartReturningData();
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->StartReturningData();
}
}
void SourceState::AbortReads() {
if (audio_)
audio_->AbortReads();
if (video_)
video_->AbortReads();
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->AbortReads();
}
}
void SourceState::Seek(TimeDelta seek_time) {
if (audio_)
audio_->Seek(seek_time);
if (video_)
video_->Seek(seek_time);
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->Seek(seek_time);
}
}
void SourceState::CompletePendingReadIfPossible() {
if (audio_)
audio_->CompletePendingReadIfPossible();
if (video_)
video_->CompletePendingReadIfPossible();
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->CompletePendingReadIfPossible();
}
}
void SourceState::OnSetDuration(TimeDelta duration) {
if (audio_)
audio_->OnSetDuration(duration);
if (video_)
video_->OnSetDuration(duration);
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->OnSetDuration(duration);
}
}
void SourceState::MarkEndOfStream() {
if (audio_)
audio_->MarkEndOfStream();
if (video_)
video_->MarkEndOfStream();
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->MarkEndOfStream();
}
}
void SourceState::UnmarkEndOfStream() {
if (audio_)
audio_->UnmarkEndOfStream();
if (video_)
video_->UnmarkEndOfStream();
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->UnmarkEndOfStream();
}
}
void SourceState::Shutdown() {
if (audio_)
audio_->Shutdown();
if (video_)
video_->Shutdown();
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->Shutdown();
}
}
void SourceState::SetMemoryLimits(DemuxerStream::Type type,
size_t memory_limit) {
switch (type) {
case DemuxerStream::AUDIO:
if (audio_)
audio_->SetStreamMemoryLimit(memory_limit);
break;
case DemuxerStream::VIDEO:
if (video_)
video_->SetStreamMemoryLimit(memory_limit);
break;
case DemuxerStream::TEXT:
for (TextStreamMap::iterator itr = text_stream_map_.begin();
itr != text_stream_map_.end(); ++itr) {
itr->second->SetStreamMemoryLimit(memory_limit);
}
break;
case DemuxerStream::UNKNOWN:
case DemuxerStream::NUM_TYPES:
NOTREACHED();
break;
}
}
bool SourceState::IsSeekWaitingForData() const {
if (audio_ && audio_->IsSeekWaitingForData())
return true;
if (video_ && video_->IsSeekWaitingForData())
return true;
// NOTE: We are intentionally not checking the text tracks
// because text tracks are discontinuous and may not have data
// for the seek position. This is ok and playback should not be
// stalled because we don't have cues. If cues, with timestamps after
// the seek time, eventually arrive they will be delivered properly
// in response to ChunkDemuxerStream::Read() calls.
return false;
}
bool SourceState::OnNewConfigs(
bool allow_audio, bool allow_video,
const AudioDecoderConfig& audio_config,
const VideoDecoderConfig& video_config,
const StreamParser::TextTrackConfigMap& text_configs) {
DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
<< ", " << audio_config.IsValidConfig()
<< ", " << video_config.IsValidConfig() << ")";
DCHECK(!init_segment_received_cb_.is_null());
if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
return false;
}
// Signal an error if we get configuration info for stream types that weren't
// specified in AddId() or more configs after a stream is initialized.
if (allow_audio != audio_config.IsValidConfig()) {
MEDIA_LOG(ERROR, media_log_)
<< "Initialization segment"
<< (audio_config.IsValidConfig() ? " has" : " does not have")
<< " an audio track, but the mimetype"
<< (allow_audio ? " specifies" : " does not specify")
<< " an audio codec.";
return false;
}
if (allow_video != video_config.IsValidConfig()) {
MEDIA_LOG(ERROR, media_log_)
<< "Initialization segment"
<< (video_config.IsValidConfig() ? " has" : " does not have")
<< " a video track, but the mimetype"
<< (allow_video ? " specifies" : " does not specify")
<< " a video codec.";
return false;
}
bool success = true;
if (audio_config.IsValidConfig()) {
if (!audio_) {
media_log_->SetBooleanProperty("found_audio_stream", true);
}
if (!audio_ ||
audio_->audio_decoder_config().codec() != audio_config.codec()) {
media_log_->SetStringProperty("audio_codec_name",
audio_config.GetHumanReadableCodecName());
}
if (!audio_) {
audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
if (!audio_) {
DVLOG(1) << "Failed to create an audio stream.";
return false;
}
if (!frame_processor_->AddTrack(FrameProcessor::kAudioTrackId, audio_)) {
DVLOG(1) << "Failed to add audio track to frame processor.";
return false;
}
}
frame_processor_->OnPossibleAudioConfigUpdate(audio_config);
success &= audio_->UpdateAudioConfig(audio_config, media_log_);
}
if (video_config.IsValidConfig()) {
if (!video_) {
media_log_->SetBooleanProperty("found_video_stream", true);
}
if (!video_ ||
video_->video_decoder_config().codec() != video_config.codec()) {
media_log_->SetStringProperty("video_codec_name",
video_config.GetHumanReadableCodecName());
}
if (!video_) {
video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
if (!video_) {
DVLOG(1) << "Failed to create a video stream.";
return false;
}
if (!frame_processor_->AddTrack(FrameProcessor::kVideoTrackId, video_)) {
DVLOG(1) << "Failed to add video track to frame processor.";
return false;
}
}
success &= video_->UpdateVideoConfig(video_config, media_log_);
}
typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
if (text_stream_map_.empty()) {
for (TextConfigItr itr = text_configs.begin();
itr != text_configs.end(); ++itr) {
ChunkDemuxerStream* const text_stream =
create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
if (!frame_processor_->AddTrack(itr->first, text_stream)) {
success &= false;
MEDIA_LOG(ERROR, media_log_) << "Failed to add text track ID "
<< itr->first << " to frame processor.";
break;
}
text_stream->UpdateTextConfig(itr->second, media_log_);
text_stream_map_[itr->first] = text_stream;
new_text_track_cb_.Run(text_stream, itr->second);
}
} else {
const size_t text_count = text_stream_map_.size();
if (text_configs.size() != text_count) {
success &= false;
MEDIA_LOG(ERROR, media_log_)
<< "The number of text track configs changed.";
} else if (text_count == 1) {
TextConfigItr config_itr = text_configs.begin();
TextStreamMap::iterator stream_itr = text_stream_map_.begin();
ChunkDemuxerStream* text_stream = stream_itr->second;
TextTrackConfig old_config = text_stream->text_track_config();
TextTrackConfig new_config(config_itr->second.kind(),
config_itr->second.label(),
config_itr->second.language(),
old_config.id());
if (!new_config.Matches(old_config)) {
success &= false;
MEDIA_LOG(ERROR, media_log_)
<< "New text track config does not match old one.";
} else {
StreamParser::TrackId old_id = stream_itr->first;
StreamParser::TrackId new_id = config_itr->first;
if (new_id != old_id) {
if (frame_processor_->UpdateTrack(old_id, new_id)) {
text_stream_map_.clear();
text_stream_map_[config_itr->first] = text_stream;
} else {
success &= false;
MEDIA_LOG(ERROR, media_log_)
<< "Error remapping single text track number";
}
}
}
} else {
for (TextConfigItr config_itr = text_configs.begin();
config_itr != text_configs.end(); ++config_itr) {
TextStreamMap::iterator stream_itr =
text_stream_map_.find(config_itr->first);
if (stream_itr == text_stream_map_.end()) {
success &= false;
MEDIA_LOG(ERROR, media_log_)
<< "Unexpected text track configuration for track ID "
<< config_itr->first;
break;
}
const TextTrackConfig& new_config = config_itr->second;
ChunkDemuxerStream* stream = stream_itr->second;
TextTrackConfig old_config = stream->text_track_config();
if (!new_config.Matches(old_config)) {
success &= false;
MEDIA_LOG(ERROR, media_log_) << "New text track config for track ID "
<< config_itr->first
<< " does not match old one.";
break;
}
}
}
}
frame_processor_->SetAllTrackBuffersNeedRandomAccessPoint();
DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
if (success)
init_segment_received_cb_.Run();
return success;
}
void SourceState::OnNewMediaSegment() {
DVLOG(2) << "OnNewMediaSegment()";
parsing_media_segment_ = true;
new_media_segment_ = true;
}
void SourceState::OnEndOfMediaSegment() {
DVLOG(2) << "OnEndOfMediaSegment()";
parsing_media_segment_ = false;
new_media_segment_ = false;
}
bool SourceState::OnNewBuffers(
const StreamParser::BufferQueue& audio_buffers,
const StreamParser::BufferQueue& video_buffers,
const StreamParser::TextBufferQueueMap& text_map) {
DVLOG(2) << "OnNewBuffers()";
DCHECK(timestamp_offset_during_append_);
DCHECK(parsing_media_segment_);
const TimeDelta timestamp_offset_before_processing =
*timestamp_offset_during_append_;
// Calculate the new timestamp offset for audio/video tracks if the stream
// parser has requested automatic updates.
TimeDelta new_timestamp_offset = timestamp_offset_before_processing;
if (auto_update_timestamp_offset_) {
const bool have_audio_buffers = !audio_buffers.empty();
const bool have_video_buffers = !video_buffers.empty();
if (have_audio_buffers && have_video_buffers) {
new_timestamp_offset +=
std::min(EndTimestamp(audio_buffers), EndTimestamp(video_buffers));
} else if (have_audio_buffers) {
new_timestamp_offset += EndTimestamp(audio_buffers);
} else if (have_video_buffers) {
new_timestamp_offset += EndTimestamp(video_buffers);
}
}
if (!frame_processor_->ProcessFrames(audio_buffers,
video_buffers,
text_map,
append_window_start_during_append_,
append_window_end_during_append_,
&new_media_segment_,
timestamp_offset_during_append_)) {
return false;
}
// Only update the timestamp offset if the frame processor hasn't already.
if (auto_update_timestamp_offset_ &&
timestamp_offset_before_processing == *timestamp_offset_during_append_) {
*timestamp_offset_during_append_ = new_timestamp_offset;
}
return true;
}
void SourceState::OnSourceInitDone(const StreamParser::InitParameters& params) {
auto_update_timestamp_offset_ = params.auto_update_timestamp_offset;
base::ResetAndReturn(&init_cb_).Run(params);
}
ChunkDemuxerStream::ChunkDemuxerStream(Type type,
bool splice_frames_enabled)
: type_(type),
liveness_(DemuxerStream::LIVENESS_UNKNOWN),
state_(UNINITIALIZED),
splice_frames_enabled_(splice_frames_enabled),
partial_append_window_trimming_enabled_(false) {
}
void ChunkDemuxerStream::StartReturningData() {
DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
base::AutoLock auto_lock(lock_);
DCHECK(read_cb_.is_null());
ChangeState_Locked(RETURNING_DATA_FOR_READS);
}
void ChunkDemuxerStream::AbortReads() {
DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
base::AutoLock auto_lock(lock_);
ChangeState_Locked(RETURNING_ABORT_FOR_READS);
if (!read_cb_.is_null())
base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
}
void ChunkDemuxerStream::CompletePendingReadIfPossible() {
base::AutoLock auto_lock(lock_);
if (read_cb_.is_null())
return;
CompletePendingReadIfPossible_Locked();
}
void ChunkDemuxerStream::Shutdown() {
DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
base::AutoLock auto_lock(lock_);
ChangeState_Locked(SHUTDOWN);
// Pass an end of stream buffer to the pending callback to signal that no more
// data will be sent.
if (!read_cb_.is_null()) {
base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
StreamParserBuffer::CreateEOSBuffer());
}
}
bool ChunkDemuxerStream::IsSeekWaitingForData() const {
base::AutoLock auto_lock(lock_);
// This method should not be called for text tracks. See the note in
// SourceState::IsSeekWaitingForData().
DCHECK_NE(type_, DemuxerStream::TEXT);
return stream_->IsSeekPending();
}
void ChunkDemuxerStream::Seek(TimeDelta time) {
DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
base::AutoLock auto_lock(lock_);
DCHECK(read_cb_.is_null());
DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
<< state_;
stream_->Seek(time);
}
bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
if (buffers.empty())
return false;
base::AutoLock auto_lock(lock_);
DCHECK_NE(state_, SHUTDOWN);
if (!stream_->Append(buffers)) {
DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
return false;
}
if (!read_cb_.is_null())
CompletePendingReadIfPossible_Locked();
return true;
}
void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
TimeDelta duration) {
base::AutoLock auto_lock(lock_);
stream_->Remove(start, end, duration);
}
bool ChunkDemuxerStream::EvictCodedFrames(DecodeTimestamp media_time,
size_t newDataSize) {
base::AutoLock auto_lock(lock_);
return stream_->GarbageCollectIfNeeded(media_time, newDataSize);
}
void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
base::AutoLock auto_lock(lock_);
stream_->OnSetDuration(duration);
}
Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
TimeDelta duration) const {
base::AutoLock auto_lock(lock_);
if (type_ == TEXT) {
// Since text tracks are discontinuous and the lack of cues should not block
// playback, report the buffered range for text tracks as [0, |duration|) so
// that intesections with audio & video tracks are computed correctly when
// no cues are present.
Ranges<TimeDelta> text_range;
text_range.Add(TimeDelta(), duration);
return text_range;
}
Ranges<TimeDelta> range = stream_->GetBufferedTime();
if (range.size() == 0u)
return range;
// Clamp the end of the stream's buffered ranges to fit within the duration.
// This can be done by intersecting the stream's range with the valid time
// range.
Ranges<TimeDelta> valid_time_range;
valid_time_range.Add(range.start(0), duration);
return range.IntersectionWith(valid_time_range);
}
TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
return stream_->GetBufferedDuration();
}
size_t ChunkDemuxerStream::GetBufferedSize() const {
return stream_->GetBufferedSize();
}
void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp) {
DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
<< start_timestamp.InSecondsF() << ")";
base::AutoLock auto_lock(lock_);
stream_->OnNewMediaSegment(start_timestamp);
}
bool ChunkDemuxerStream::UpdateAudioConfig(
const AudioDecoderConfig& config,
const scoped_refptr<MediaLog>& media_log) {
DCHECK(config.IsValidConfig());
DCHECK_EQ(type_, AUDIO);
base::AutoLock auto_lock(lock_);
if (!stream_) {
DCHECK_EQ(state_, UNINITIALIZED);
// On platforms which support splice frames, enable splice frames and
// partial append window support for most codecs (notably: not opus).
const bool codec_supported = config.codec() == kCodecMP3 ||
config.codec() == kCodecAAC ||
config.codec() == kCodecVorbis;
splice_frames_enabled_ = splice_frames_enabled_ && codec_supported;
partial_append_window_trimming_enabled_ =
splice_frames_enabled_ && codec_supported;
stream_.reset(
new SourceBufferStream(config, media_log, splice_frames_enabled_));
return true;
}
return stream_->UpdateAudioConfig(config);
}
bool ChunkDemuxerStream::UpdateVideoConfig(
const VideoDecoderConfig& config,
const scoped_refptr<MediaLog>& media_log) {
DCHECK(config.IsValidConfig());
DCHECK_EQ(type_, VIDEO);
base::AutoLock auto_lock(lock_);
if (!stream_) {
DCHECK_EQ(state_, UNINITIALIZED);
stream_.reset(
new SourceBufferStream(config, media_log, splice_frames_enabled_));
return true;
}
return stream_->UpdateVideoConfig(config);
}
void ChunkDemuxerStream::UpdateTextConfig(
const TextTrackConfig& config,
const scoped_refptr<MediaLog>& media_log) {
DCHECK_EQ(type_, TEXT);
base::AutoLock auto_lock(lock_);
DCHECK(!stream_);
DCHECK_EQ(state_, UNINITIALIZED);
stream_.reset(
new SourceBufferStream(config, media_log, splice_frames_enabled_));
}
void ChunkDemuxerStream::MarkEndOfStream() {
base::AutoLock auto_lock(lock_);
stream_->MarkEndOfStream();
}
void ChunkDemuxerStream::UnmarkEndOfStream() {
base::AutoLock auto_lock(lock_);
stream_->UnmarkEndOfStream();
}
// DemuxerStream methods.
void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
base::AutoLock auto_lock(lock_);
DCHECK_NE(state_, UNINITIALIZED);
DCHECK(read_cb_.is_null());
read_cb_ = BindToCurrentLoop(read_cb);
CompletePendingReadIfPossible_Locked();
}
DemuxerStream::Type ChunkDemuxerStream::type() const { return type_; }
DemuxerStream::Liveness ChunkDemuxerStream::liveness() const {
base::AutoLock auto_lock(lock_);
return liveness_;
}
AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
CHECK_EQ(type_, AUDIO);
base::AutoLock auto_lock(lock_);
return stream_->GetCurrentAudioDecoderConfig();
}
VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
CHECK_EQ(type_, VIDEO);
base::AutoLock auto_lock(lock_);
return stream_->GetCurrentVideoDecoderConfig();
}
bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
VideoRotation ChunkDemuxerStream::video_rotation() {
return VIDEO_ROTATION_0;
}
TextTrackConfig ChunkDemuxerStream::text_track_config() {
CHECK_EQ(type_, TEXT);
base::AutoLock auto_lock(lock_);
return stream_->GetCurrentTextTrackConfig();
}
void ChunkDemuxerStream::SetStreamMemoryLimit(size_t memory_limit) {
stream_->set_memory_limit(memory_limit);
}
void ChunkDemuxerStream::SetLiveness(Liveness liveness) {
base::AutoLock auto_lock(lock_);
liveness_ = liveness;
}
void ChunkDemuxerStream::ChangeState_Locked(State state) {
lock_.AssertAcquired();
DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
<< "type " << type_
<< " - " << state_ << " -> " << state;
state_ = state;
}
ChunkDemuxerStream::~ChunkDemuxerStream() {}
void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
lock_.AssertAcquired();
DCHECK(!read_cb_.is_null());
DemuxerStream::Status status;
scoped_refptr<StreamParserBuffer> buffer;
switch (state_) {
case UNINITIALIZED:
NOTREACHED();
return;
case RETURNING_DATA_FOR_READS:
switch (stream_->GetNextBuffer(&buffer)) {
case SourceBufferStream::kSuccess:
status = DemuxerStream::kOk;
DVLOG(2) << __FUNCTION__ << ": returning kOk, type " << type_
<< ", dts " << buffer->GetDecodeTimestamp().InSecondsF()
<< ", pts " << buffer->timestamp().InSecondsF()
<< ", dur " << buffer->duration().InSecondsF()
<< ", key " << buffer->is_key_frame();
break;
case SourceBufferStream::kNeedBuffer:
// Return early without calling |read_cb_| since we don't have
// any data to return yet.
DVLOG(2) << __FUNCTION__ << ": returning kNeedBuffer, type "
<< type_;
return;
case SourceBufferStream::kEndOfStream:
status = DemuxerStream::kOk;
buffer = StreamParserBuffer::CreateEOSBuffer();
DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
<< type_;
break;
case SourceBufferStream::kConfigChange:
status = kConfigChanged;
buffer = NULL;
DVLOG(2) << __FUNCTION__ << ": returning kConfigChange, type "
<< type_;
break;
}
break;
case RETURNING_ABORT_FOR_READS:
// Null buffers should be returned in this state since we are waiting
// for a seek. Any buffers in the SourceBuffer should NOT be returned
// because they are associated with the seek.
status = DemuxerStream::kAborted;
buffer = NULL;
DVLOG(2) << __FUNCTION__ << ": returning kAborted, type " << type_;
break;
case SHUTDOWN:
status = DemuxerStream::kOk;
buffer = StreamParserBuffer::CreateEOSBuffer();
DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
<< type_;
break;
}
base::ResetAndReturn(&read_cb_).Run(status, buffer);
}
ChunkDemuxer::ChunkDemuxer(
const base::Closure& open_cb,
const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
const scoped_refptr<MediaLog>& media_log,
bool splice_frames_enabled)
: state_(WAITING_FOR_INIT),
cancel_next_seek_(false),
host_(NULL),
open_cb_(open_cb),
encrypted_media_init_data_cb_(encrypted_media_init_data_cb),
enable_text_(false),
media_log_(media_log),
duration_(kNoTimestamp()),
user_specified_duration_(-1),
liveness_(DemuxerStream::LIVENESS_UNKNOWN),
splice_frames_enabled_(splice_frames_enabled) {
DCHECK(!open_cb_.is_null());
DCHECK(!encrypted_media_init_data_cb_.is_null());
}
std::string ChunkDemuxer::GetDisplayName() const {
return "ChunkDemuxer";
}
void ChunkDemuxer::Initialize(
DemuxerHost* host,
const PipelineStatusCB& cb,
bool enable_text_tracks) {
DVLOG(1) << "Init()";
base::AutoLock auto_lock(lock_);
// The |init_cb_| must only be run after this method returns, so always post.
init_cb_ = BindToCurrentLoop(cb);
if (state_ == SHUTDOWN) {
base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
return;
}
DCHECK_EQ(state_, WAITING_FOR_INIT);
host_ = host;
enable_text_ = enable_text_tracks;
ChangeState_Locked(INITIALIZING);
base::ResetAndReturn(&open_cb_).Run();
}
void ChunkDemuxer::Stop() {
DVLOG(1) << "Stop()";
Shutdown();
}
void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
DCHECK(time >= TimeDelta());
base::AutoLock auto_lock(lock_);
DCHECK(seek_cb_.is_null());
seek_cb_ = BindToCurrentLoop(cb);
if (state_ != INITIALIZED && state_ != ENDED) {
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
return;
}
if (cancel_next_seek_) {
cancel_next_seek_ = false;
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
return;
}
SeekAllSources(time);
StartReturningData();
if (IsSeekWaitingForData_Locked()) {
DVLOG(1) << "Seek() : waiting for more data to arrive.";
return;
}
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
}
// Demuxer implementation.
base::Time ChunkDemuxer::GetTimelineOffset() const {
return timeline_offset_;
}
DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
DCHECK_NE(type, DemuxerStream::TEXT);
base::AutoLock auto_lock(lock_);
if (type == DemuxerStream::VIDEO)
return video_.get();
if (type == DemuxerStream::AUDIO)
return audio_.get();
return NULL;
}
TimeDelta ChunkDemuxer::GetStartTime() const {
return TimeDelta();
}
void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
DVLOG(1) << "StartWaitingForSeek()";
base::AutoLock auto_lock(lock_);
DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
state_ == PARSE_ERROR) << state_;
DCHECK(seek_cb_.is_null());
if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
return;
AbortPendingReads();
SeekAllSources(seek_time);
// Cancel state set in CancelPendingSeek() since we want to
// accept the next Seek().
cancel_next_seek_ = false;
}
void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
base::AutoLock auto_lock(lock_);
DCHECK_NE(state_, INITIALIZING);
DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
if (cancel_next_seek_)
return;
AbortPendingReads();
SeekAllSources(seek_time);
if (seek_cb_.is_null()) {
cancel_next_seek_ = true;
return;
}
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
}
ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
const std::string& type,
std::vector<std::string>& codecs) {
base::AutoLock auto_lock(lock_);
if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
return kReachedIdLimit;
bool has_audio = false;
bool has_video = false;
scoped_ptr<media::StreamParser> stream_parser(StreamParserFactory::Create(
type, codecs, media_log_, &has_audio, &has_video));
if (!stream_parser)
return ChunkDemuxer::kNotSupported;
if ((has_audio && !source_id_audio_.empty()) ||
(has_video && !source_id_video_.empty()))
return kReachedIdLimit;
if (has_audio)
source_id_audio_ = id;
if (has_video)
source_id_video_ = id;
scoped_ptr<FrameProcessor> frame_processor(
new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
base::Unretained(this)),
media_log_));
scoped_ptr<SourceState> source_state(new SourceState(
stream_parser.Pass(), frame_processor.Pass(),
base::Bind(&ChunkDemuxer::CreateDemuxerStream, base::Unretained(this)),
media_log_));
SourceState::NewTextTrackCB new_text_track_cb;
if (enable_text_) {
new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
base::Unretained(this));
}
source_state->Init(
base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
has_audio, has_video, encrypted_media_init_data_cb_, new_text_track_cb);
source_state_map_[id] = source_state.release();
return kOk;
}
void ChunkDemuxer::RemoveId(const std::string& id) {
base::AutoLock auto_lock(lock_);
CHECK(IsValidId(id));
delete source_state_map_[id];
source_state_map_.erase(id);
if (source_id_audio_ == id)
source_id_audio_.clear();
if (source_id_video_ == id)
source_id_video_.clear();
}
Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
base::AutoLock auto_lock(lock_);
DCHECK(!id.empty());
SourceStateMap::const_iterator itr = source_state_map_.find(id);
DCHECK(itr != source_state_map_.end());
return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
}
bool ChunkDemuxer::EvictCodedFrames(const std::string& id,
base::TimeDelta currentMediaTime,
size_t newDataSize) {
DVLOG(1) << __FUNCTION__ << "(" << id << ")"
<< " media_time=" << currentMediaTime.InSecondsF()
<< " newDataSize=" << newDataSize;
base::AutoLock auto_lock(lock_);
// Note: The direct conversion from PTS to DTS is safe here, since we don't
// need to know currentTime precisely for GC. GC only needs to know which GOP
// currentTime points to.
DecodeTimestamp media_time_dts =
DecodeTimestamp::FromPresentationTime(currentMediaTime);
DCHECK(!id.empty());
SourceStateMap::const_iterator itr = source_state_map_.find(id);
if (itr == source_state_map_.end()) {
LOG(WARNING) << __FUNCTION__ << " stream " << id << " not found";
return false;
}
return itr->second->EvictCodedFrames(media_time_dts, newDataSize);
}
void ChunkDemuxer::AppendData(
const std::string& id,
const uint8* data,
size_t length,
TimeDelta append_window_start,
TimeDelta append_window_end,
TimeDelta* timestamp_offset,
const InitSegmentReceivedCB& init_segment_received_cb) {
DVLOG(1) << "AppendData(" << id << ", " << length << ")";
DCHECK(!id.empty());
DCHECK(timestamp_offset);
DCHECK(!init_segment_received_cb.is_null());
Ranges<TimeDelta> ranges;
{
base::AutoLock auto_lock(lock_);
DCHECK_NE(state_, ENDED);
// Capture if any of the SourceBuffers are waiting for data before we start
// parsing.
bool old_waiting_for_data = IsSeekWaitingForData_Locked();
if (length == 0u)
return;
DCHECK(data);
switch (state_) {
case INITIALIZING:
case INITIALIZED:
DCHECK(IsValidId(id));
if (!source_state_map_[id]->Append(data, length,
append_window_start,
append_window_end,
timestamp_offset,
init_segment_received_cb)) {
ReportError_Locked(PIPELINE_ERROR_DECODE);
return;
}
break;
case PARSE_ERROR:
DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
return;
case WAITING_FOR_INIT:
case ENDED:
case SHUTDOWN:
DVLOG(1) << "AppendData(): called in unexpected state " << state_;
return;
}
// Check to see if data was appended at the pending seek point. This
// indicates we have parsed enough data to complete the seek.
if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
!seek_cb_.is_null()) {
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
}
ranges = GetBufferedRanges_Locked();
}
for (size_t i = 0; i < ranges.size(); ++i)
host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
}
void ChunkDemuxer::Abort(const std::string& id,
TimeDelta append_window_start,
TimeDelta append_window_end,
TimeDelta* timestamp_offset) {
DVLOG(1) << "Abort(" << id << ")";
base::AutoLock auto_lock(lock_);
DCHECK(!id.empty());
CHECK(IsValidId(id));
bool old_waiting_for_data = IsSeekWaitingForData_Locked();
source_state_map_[id]->Abort(append_window_start,
append_window_end,
timestamp_offset);
// Abort can possibly emit some buffers.
// Need to check whether seeking can be completed.
if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
!seek_cb_.is_null()) {
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
}
}
void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
TimeDelta end) {
DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
<< ", " << end.InSecondsF() << ")";
base::AutoLock auto_lock(lock_);
DCHECK(!id.empty());
CHECK(IsValidId(id));
DCHECK(start >= base::TimeDelta()) << start.InSecondsF();
DCHECK(start < end) << "start " << start.InSecondsF()
<< " end " << end.InSecondsF();
DCHECK(duration_ != kNoTimestamp());
DCHECK(start <= duration_) << "start " << start.InSecondsF()
<< " duration " << duration_.InSecondsF();
if (start == duration_)
return;
source_state_map_[id]->Remove(start, end, duration_);
}
double ChunkDemuxer::GetDuration() {
base::AutoLock auto_lock(lock_);
return GetDuration_Locked();
}
double ChunkDemuxer::GetDuration_Locked() {
lock_.AssertAcquired();
if (duration_ == kNoTimestamp())
return std::numeric_limits<double>::quiet_NaN();
// Return positive infinity if the resource is unbounded.
// http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
if (duration_ == kInfiniteDuration())
return std::numeric_limits<double>::infinity();
if (user_specified_duration_ >= 0)
return user_specified_duration_;
return duration_.InSecondsF();
}
void ChunkDemuxer::SetDuration(double duration) {
base::AutoLock auto_lock(lock_);
DVLOG(1) << "SetDuration(" << duration << ")";
DCHECK_GE(duration, 0);
if (duration == GetDuration_Locked())
return;
// Compute & bounds check the TimeDelta representation of duration.
// This can be different if the value of |duration| doesn't fit the range or
// precision of TimeDelta.
TimeDelta min_duration = TimeDelta::FromInternalValue(1);
// Don't use TimeDelta::Max() here, as we want the largest finite time delta.
TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
double min_duration_in_seconds = min_duration.InSecondsF();
double max_duration_in_seconds = max_duration.InSecondsF();
TimeDelta duration_td;
if (duration == std::numeric_limits<double>::infinity()) {
duration_td = media::kInfiniteDuration();
} else if (duration < min_duration_in_seconds) {
duration_td = min_duration;
} else if (duration > max_duration_in_seconds) {
duration_td = max_duration;
} else {
duration_td = TimeDelta::FromMicroseconds(
duration * base::Time::kMicrosecondsPerSecond);
}
DCHECK(duration_td > TimeDelta());
user_specified_duration_ = duration;
duration_ = duration_td;
host_->SetDuration(duration_);
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->OnSetDuration(duration_);
}
}
bool ChunkDemuxer::IsParsingMediaSegment(const std::string& id) {
base::AutoLock auto_lock(lock_);
DVLOG(1) << "IsParsingMediaSegment(" << id << ")";
CHECK(IsValidId(id));
return source_state_map_[id]->parsing_media_segment();
}
void ChunkDemuxer::SetSequenceMode(const std::string& id,
bool sequence_mode) {
base::AutoLock auto_lock(lock_);
DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
CHECK(IsValidId(id));
DCHECK_NE(state_, ENDED);
source_state_map_[id]->SetSequenceMode(sequence_mode);
}
void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
const std::string& id,
base::TimeDelta timestamp_offset) {
base::AutoLock auto_lock(lock_);
DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id << ", "
<< timestamp_offset.InSecondsF() << ")";
CHECK(IsValidId(id));
DCHECK_NE(state_, ENDED);
source_state_map_[id]->SetGroupStartTimestampIfInSequenceMode(
timestamp_offset);
}
void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
DVLOG(1) << "MarkEndOfStream(" << status << ")";
base::AutoLock auto_lock(lock_);
DCHECK_NE(state_, WAITING_FOR_INIT);
DCHECK_NE(state_, ENDED);
if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
return;
if (state_ == INITIALIZING) {
ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
return;
}
bool old_waiting_for_data = IsSeekWaitingForData_Locked();
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->MarkEndOfStream();
}
CompletePendingReadsIfPossible();
// Give a chance to resume the pending seek process.
if (status != PIPELINE_OK) {
ReportError_Locked(status);
return;
}
ChangeState_Locked(ENDED);
DecreaseDurationIfNecessary();
if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
!seek_cb_.is_null()) {
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
}
}
void ChunkDemuxer::UnmarkEndOfStream() {
DVLOG(1) << "UnmarkEndOfStream()";
base::AutoLock auto_lock(lock_);
DCHECK_EQ(state_, ENDED);
ChangeState_Locked(INITIALIZED);
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->UnmarkEndOfStream();
}
}
void ChunkDemuxer::Shutdown() {
DVLOG(1) << "Shutdown()";
base::AutoLock auto_lock(lock_);
if (state_ == SHUTDOWN)
return;
ShutdownAllStreams();
ChangeState_Locked(SHUTDOWN);
if(!seek_cb_.is_null())
base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
}
void ChunkDemuxer::SetMemoryLimits(DemuxerStream::Type type,
size_t memory_limit) {
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->SetMemoryLimits(type, memory_limit);
}
}
void ChunkDemuxer::ChangeState_Locked(State new_state) {
lock_.AssertAcquired();
DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
<< state_ << " -> " << new_state;
state_ = new_state;
}
ChunkDemuxer::~ChunkDemuxer() {
DCHECK_NE(state_, INITIALIZED);
STLDeleteValues(&source_state_map_);
}
void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
DVLOG(1) << "ReportError_Locked(" << error << ")";
lock_.AssertAcquired();
DCHECK_NE(error, PIPELINE_OK);
ChangeState_Locked(PARSE_ERROR);
PipelineStatusCB cb;
if (!init_cb_.is_null()) {
std::swap(cb, init_cb_);
} else {
if (!seek_cb_.is_null())
std::swap(cb, seek_cb_);
ShutdownAllStreams();
}
if (!cb.is_null()) {
cb.Run(error);
return;
}
base::AutoUnlock auto_unlock(lock_);
host_->OnDemuxerError(error);
}
bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
lock_.AssertAcquired();
for (SourceStateMap::const_iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
if (itr->second->IsSeekWaitingForData())
return true;
}
return false;
}
void ChunkDemuxer::OnSourceInitDone(
const StreamParser::InitParameters& params) {
DVLOG(1) << "OnSourceInitDone(" << params.duration.InSecondsF() << ")";
lock_.AssertAcquired();
DCHECK_EQ(state_, INITIALIZING);
if (!audio_ && !video_) {
ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
return;
}
if (params.duration != TimeDelta() && duration_ == kNoTimestamp())
UpdateDuration(params.duration);
if (!params.timeline_offset.is_null()) {
if (!timeline_offset_.is_null() &&
params.timeline_offset != timeline_offset_) {
MEDIA_LOG(ERROR, media_log_)
<< "Timeline offset is not the same across all SourceBuffers.";
ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
return;
}
timeline_offset_ = params.timeline_offset;
}
if (params.liveness != DemuxerStream::LIVENESS_UNKNOWN) {
if (audio_)
audio_->SetLiveness(params.liveness);
if (video_)
video_->SetLiveness(params.liveness);
}
// Wait until all streams have initialized.
if ((!source_id_audio_.empty() && !audio_) ||
(!source_id_video_.empty() && !video_)) {
return;
}
SeekAllSources(GetStartTime());
StartReturningData();
if (duration_ == kNoTimestamp())
duration_ = kInfiniteDuration();
// The demuxer is now initialized after the |start_timestamp_| was set.
ChangeState_Locked(INITIALIZED);
base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
}
ChunkDemuxerStream*
ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
switch (type) {
case DemuxerStream::AUDIO:
if (audio_)
return NULL;
audio_.reset(
new ChunkDemuxerStream(DemuxerStream::AUDIO, splice_frames_enabled_));
return audio_.get();
break;
case DemuxerStream::VIDEO:
if (video_)
return NULL;
video_.reset(
new ChunkDemuxerStream(DemuxerStream::VIDEO, splice_frames_enabled_));
return video_.get();
break;
case DemuxerStream::TEXT: {
return new ChunkDemuxerStream(DemuxerStream::TEXT,
splice_frames_enabled_);
break;
}
case DemuxerStream::UNKNOWN:
case DemuxerStream::NUM_TYPES:
NOTREACHED();
return NULL;
}
NOTREACHED();
return NULL;
}
void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
const TextTrackConfig& config) {
lock_.AssertAcquired();
DCHECK_NE(state_, SHUTDOWN);
host_->AddTextStream(text_stream, config);
}
bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
lock_.AssertAcquired();
return source_state_map_.count(source_id) > 0u;
}
void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
DCHECK(duration_ != new_duration);
user_specified_duration_ = -1;
duration_ = new_duration;
host_->SetDuration(new_duration);
}
void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration) {
DCHECK(new_duration != kNoTimestamp());
DCHECK(new_duration != kInfiniteDuration());
// Per April 1, 2014 MSE spec editor's draft:
// https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
// media-source.html#sourcebuffer-coded-frame-processing
// 5. If the media segment contains data beyond the current duration, then run
// the duration change algorithm with new duration set to the maximum of
// the current duration and the group end timestamp.
if (new_duration <= duration_)
return;
DVLOG(2) << __FUNCTION__ << ": Increasing duration: "
<< duration_.InSecondsF() << " -> " << new_duration.InSecondsF();
UpdateDuration(new_duration);
}
void ChunkDemuxer::DecreaseDurationIfNecessary() {
lock_.AssertAcquired();
TimeDelta max_duration;
for (SourceStateMap::const_iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
max_duration = std::max(max_duration,
itr->second->GetMaxBufferedDuration());
}
if (max_duration == TimeDelta())
return;
if (max_duration < duration_)
UpdateDuration(max_duration);
}
Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
base::AutoLock auto_lock(lock_);
return GetBufferedRanges_Locked();
}
Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
lock_.AssertAcquired();
bool ended = state_ == ENDED;
// TODO(acolwell): When we start allowing SourceBuffers that are not active,
// we'll need to update this loop to only add ranges from active sources.
RangesList ranges_list;
for (SourceStateMap::const_iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
}
return ComputeIntersection(ranges_list, ended);
}
void ChunkDemuxer::StartReturningData() {
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->StartReturningData();
}
}
void ChunkDemuxer::AbortPendingReads() {
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->AbortReads();
}
}
void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->Seek(seek_time);
}
}
void ChunkDemuxer::CompletePendingReadsIfPossible() {
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->CompletePendingReadIfPossible();
}
}
void ChunkDemuxer::ShutdownAllStreams() {
for (SourceStateMap::iterator itr = source_state_map_.begin();
itr != source_state_map_.end(); ++itr) {
itr->second->Shutdown();
}
}
} // namespace media
|