summaryrefslogtreecommitdiff
path: root/content/media/webm/WebMReader.cpp
blob: 079d9616f66722a7cb2134cc679567115dbcb646 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsError.h"
#include "MediaDecoderStateMachine.h"
#include "AbstractMediaDecoder.h"
#include "MediaResource.h"
#include "WebMReader.h"
#include "WebMBufferedParser.h"
#include "VideoUtils.h"
#include "mozilla/dom/TimeRanges.h"
#include "VorbisUtils.h"

#define VPX_DONT_DEFINE_STDINT_TYPES
#include "vpx/vp8dx.h"
#include "vpx/vpx_decoder.h"

#include "OggReader.h"

using mozilla::NesteggPacketHolder;

template <>
class nsAutoRefTraits<NesteggPacketHolder> : public nsPointerRefTraits<NesteggPacketHolder>
{
public:
  static void Release(NesteggPacketHolder* aHolder) { delete aHolder; }
};

namespace mozilla {

using namespace layers;

// Un-comment to enable logging of seek bisections.
//#define SEEK_LOGGING

#ifdef PR_LOGGING
extern PRLogModuleInfo* gMediaDecoderLog;
PRLogModuleInfo* gNesteggLog;
#define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
#ifdef SEEK_LOGGING
#define SEEK_LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
#else
#define SEEK_LOG(type, msg)
#endif
#else
#define LOG(type, msg)
#define SEEK_LOG(type, msg)
#endif

static const unsigned NS_PER_USEC = 1000;
static const double NS_PER_S = 1e9;
static const double USEC_PER_S = 1e6;

// If a seek request is within SEEK_DECODE_MARGIN microseconds of the
// current time, decode ahead from the current frame rather than performing
// a full seek.
static const int SEEK_DECODE_MARGIN = 250000;

// Functions for reading and seeking using MediaResource required for
// nestegg_io. The 'user data' passed to these functions is the
// decoder from which the media resource is obtained.
static int webm_read(void *aBuffer, size_t aLength, void *aUserData)
{
  NS_ASSERTION(aUserData, "aUserData must point to a valid AbstractMediaDecoder");
  AbstractMediaDecoder* decoder = reinterpret_cast<AbstractMediaDecoder*>(aUserData);
  MediaResource* resource = decoder->GetResource();
  NS_ASSERTION(resource, "Decoder has no media resource");

  nsresult rv = NS_OK;
  bool eof = false;

  char *p = static_cast<char *>(aBuffer);
  while (NS_SUCCEEDED(rv) && aLength > 0) {
    uint32_t bytes = 0;
    rv = resource->Read(p, aLength, &bytes);
    if (bytes == 0) {
      eof = true;
      break;
    }
    decoder->NotifyBytesConsumed(bytes);
    aLength -= bytes;
    p += bytes;
  }

  return NS_FAILED(rv) ? -1 : eof ? 0 : 1;
}

static int webm_seek(int64_t aOffset, int aWhence, void *aUserData)
{
  NS_ASSERTION(aUserData, "aUserData must point to a valid AbstractMediaDecoder");
  AbstractMediaDecoder* decoder = reinterpret_cast<AbstractMediaDecoder*>(aUserData);
  MediaResource* resource = decoder->GetResource();
  NS_ASSERTION(resource, "Decoder has no media resource");
  nsresult rv = resource->Seek(aWhence, aOffset);
  return NS_SUCCEEDED(rv) ? 0 : -1;
}

static int64_t webm_tell(void *aUserData)
{
  NS_ASSERTION(aUserData, "aUserData must point to a valid AbstractMediaDecoder");
  AbstractMediaDecoder* decoder = reinterpret_cast<AbstractMediaDecoder*>(aUserData);
  MediaResource* resource = decoder->GetResource();
  NS_ASSERTION(resource, "Decoder has no media resource");
  return resource->Tell();
}

static void webm_log(nestegg * context,
                     unsigned int severity,
                     char const * format, ...)
{
#ifdef PR_LOGGING
  va_list args;
  char msg[256];
  const char * sevStr;

  switch(severity) {
    case NESTEGG_LOG_DEBUG:
      sevStr = "DBG";
      break;
    case NESTEGG_LOG_INFO:
      sevStr = "INF";
      break;
    case NESTEGG_LOG_WARNING:
      sevStr = "WRN";
      break;
    case NESTEGG_LOG_ERROR:
      sevStr = "ERR";
      break;
    case NESTEGG_LOG_CRITICAL:
      sevStr = "CRT";
      break;
    default:
      sevStr = "UNK";
      break;
  }

  va_start(args, format);

  PR_snprintf(msg, sizeof(msg), "%p [Nestegg-%s] ", context, sevStr);
  PR_vsnprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), format, args);
  PR_LOG(gNesteggLog, PR_LOG_DEBUG, (msg));

  va_end(args);
#endif
}

WebMReader::WebMReader(AbstractMediaDecoder* aDecoder)
#ifdef MOZ_DASH
  : DASHRepReader(aDecoder),
#else
  : MediaDecoderReader(aDecoder),
#endif
  mContext(nullptr),
  mPacketCount(0),
  mChannels(0),
#ifdef MOZ_OPUS
  mOpusParser(nullptr),
  mOpusDecoder(nullptr),
  mSkip(0),
#endif
  mVideoTrack(0),
  mAudioTrack(0),
  mAudioStartUsec(-1),
  mAudioFrames(0),
  mHasVideo(false),
  mHasAudio(false)
#ifdef MOZ_DASH
  , mMainReader(nullptr),
  mSwitchingCluster(-1),
  mNextReader(nullptr),
  mSeekToCluster(-1),
  mCurrentOffset(-1),
  mNextCluster(1),
  mPushVideoPacketToNextReader(false),
  mReachedSwitchAccessPoint(false)
#endif
{
  MOZ_COUNT_CTOR(WebMReader);
#ifdef PR_LOGGING
  if (!gNesteggLog) {
    gNesteggLog = PR_NewLogModule("Nestegg");
  }
#endif
  // Zero these member vars to avoid crashes in VP8 destroy and Vorbis clear
  // functions when destructor is called before |Init|.
  memset(&mVPX, 0, sizeof(vpx_codec_ctx_t));
  memset(&mVorbisBlock, 0, sizeof(vorbis_block));
  memset(&mVorbisDsp, 0, sizeof(vorbis_dsp_state));
  memset(&mVorbisInfo, 0, sizeof(vorbis_info));
  memset(&mVorbisComment, 0, sizeof(vorbis_comment));
}

WebMReader::~WebMReader()
{
  Cleanup();

  mVideoPackets.Reset();
  mAudioPackets.Reset();

  vpx_codec_destroy(&mVPX);

  vorbis_block_clear(&mVorbisBlock);
  vorbis_dsp_clear(&mVorbisDsp);
  vorbis_info_clear(&mVorbisInfo);
  vorbis_comment_clear(&mVorbisComment);

  if (mOpusDecoder) {
    opus_multistream_decoder_destroy(mOpusDecoder);
    mOpusDecoder = nullptr;
  }

  MOZ_COUNT_DTOR(WebMReader);
}

nsresult WebMReader::Init(MediaDecoderReader* aCloneDonor)
{
  vorbis_info_init(&mVorbisInfo);
  vorbis_comment_init(&mVorbisComment);
  memset(&mVorbisDsp, 0, sizeof(vorbis_dsp_state));
  memset(&mVorbisBlock, 0, sizeof(vorbis_block));

  if (aCloneDonor) {
    mBufferedState = static_cast<WebMReader*>(aCloneDonor)->mBufferedState;
  } else {
    mBufferedState = new WebMBufferedState;
  }

  return NS_OK;
}

nsresult WebMReader::ResetDecode()
{
  mAudioFrames = 0;
  mAudioStartUsec = -1;
  nsresult res = NS_OK;
  if (NS_FAILED(MediaDecoderReader::ResetDecode())) {
    res = NS_ERROR_FAILURE;
  }

  // Ignore failed results from vorbis_synthesis_restart. They
  // aren't fatal and it fails when ResetDecode is called at a
  // time when no vorbis data has been read.
  vorbis_synthesis_restart(&mVorbisDsp);

  mVideoPackets.Reset();
  mAudioPackets.Reset();

#ifdef MOZ_DASH
  LOG(PR_LOG_DEBUG, ("Resetting DASH seek vars"));
  mSwitchingCluster = -1;
  mNextReader = nullptr;
  mSeekToCluster = -1;
  mCurrentOffset = -1;
  mPushVideoPacketToNextReader = false;
  mReachedSwitchAccessPoint = false;
#endif

  return res;
}

void WebMReader::Cleanup()
{
  if (mContext) {
    nestegg_destroy(mContext);
    mContext = nullptr;
  }
}

nsresult WebMReader::ReadMetadata(VideoInfo* aInfo,
                                    MetadataTags** aTags)
{
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");

#ifdef MOZ_DASH
  LOG(PR_LOG_DEBUG, ("Reader [%p] for Decoder [%p]: Reading WebM Metadata: "
                     "init bytes [%d - %d] cues bytes [%d - %d]",
                     this, mDecoder,
                     mInitByteRange.mStart, mInitByteRange.mEnd,
                     mCuesByteRange.mStart, mCuesByteRange.mEnd));
#endif
  nestegg_io io;
  io.read = webm_read;
  io.seek = webm_seek;
  io.tell = webm_tell;
  io.userdata = mDecoder;
#ifdef MOZ_DASH
  int64_t maxOffset = mInitByteRange.IsNull() ? -1 : mInitByteRange.mEnd;
#else
  int64_t maxOffset = -1;
#endif
  int r = nestegg_init(&mContext, io, &webm_log, maxOffset);
  if (r == -1) {
    return NS_ERROR_FAILURE;
  }

  uint64_t duration = 0;
  r = nestegg_duration(mContext, &duration);
  if (r == 0) {
    ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
    mDecoder->SetMediaDuration(duration / NS_PER_USEC);
  }

  unsigned int ntracks = 0;
  r = nestegg_track_count(mContext, &ntracks);
  if (r == -1) {
    Cleanup();
    return NS_ERROR_FAILURE;
  }

  mInfo.mHasAudio = false;
  mInfo.mHasVideo = false;
  for (uint32_t track = 0; track < ntracks; ++track) {
    int id = nestegg_track_codec_id(mContext, track);
    if (id == -1) {
      Cleanup();
      return NS_ERROR_FAILURE;
    }
    int type = nestegg_track_type(mContext, track);
    if (!mHasVideo && type == NESTEGG_TRACK_VIDEO) {
      nestegg_video_params params;
      r = nestegg_track_video_params(mContext, track, &params);
      if (r == -1) {
        Cleanup();
        return NS_ERROR_FAILURE;
      }

      vpx_codec_iface_t* dx = nullptr;
      mVideoCodec = nestegg_track_codec_id(mContext, track);
      if (mVideoCodec == NESTEGG_CODEC_VP8) {
        dx = vpx_codec_vp8_dx();
      } else if (mVideoCodec == NESTEGG_CODEC_VP9) {
        dx = vpx_codec_vp9_dx();
      }
      if (!dx || vpx_codec_dec_init(&mVPX, dx, nullptr, 0)) {
        Cleanup();
        return NS_ERROR_FAILURE;
      }

      // Picture region, taking into account cropping, before scaling
      // to the display size.
      nsIntRect pictureRect(params.crop_left,
                            params.crop_top,
                            params.width - (params.crop_right + params.crop_left),
                            params.height - (params.crop_bottom + params.crop_top));

      // If the cropping data appears invalid then use the frame data
      if (pictureRect.width <= 0 ||
          pictureRect.height <= 0 ||
          pictureRect.x < 0 ||
          pictureRect.y < 0)
      {
        pictureRect.x = 0;
        pictureRect.y = 0;
        pictureRect.width = params.width;
        pictureRect.height = params.height;
      }

      // Validate the container-reported frame and pictureRect sizes. This ensures
      // that our video frame creation code doesn't overflow.
      nsIntSize displaySize(params.display_width, params.display_height);
      nsIntSize frameSize(params.width, params.height);
      if (!VideoInfo::ValidateVideoRegion(frameSize, pictureRect, displaySize)) {
        // Video track's frame sizes will overflow. Ignore the video track.
        continue;
      }

      mVideoTrack = track;
      mHasVideo = true;
      mInfo.mHasVideo = true;

      mInfo.mDisplay = displaySize;
      mPicture = pictureRect;
      mInitialFrame = frameSize;

      switch (params.stereo_mode) {
      case NESTEGG_VIDEO_MONO:
        mInfo.mStereoMode = STEREO_MODE_MONO;
        break;
      case NESTEGG_VIDEO_STEREO_LEFT_RIGHT:
        mInfo.mStereoMode = STEREO_MODE_LEFT_RIGHT;
        break;
      case NESTEGG_VIDEO_STEREO_BOTTOM_TOP:
        mInfo.mStereoMode = STEREO_MODE_BOTTOM_TOP;
        break;
      case NESTEGG_VIDEO_STEREO_TOP_BOTTOM:
        mInfo.mStereoMode = STEREO_MODE_TOP_BOTTOM;
        break;
      case NESTEGG_VIDEO_STEREO_RIGHT_LEFT:
        mInfo.mStereoMode = STEREO_MODE_RIGHT_LEFT;
        break;
      }
    }
    else if (!mHasAudio && type == NESTEGG_TRACK_AUDIO) {
      nestegg_audio_params params;
      r = nestegg_track_audio_params(mContext, track, &params);
      if (r == -1) {
        Cleanup();
        return NS_ERROR_FAILURE;
      }

      mAudioTrack = track;
      mHasAudio = true;
      mInfo.mHasAudio = true;
      mAudioCodec = nestegg_track_codec_id(mContext, track);
      mCodecDelay = params.codec_delay;

      if (mAudioCodec == NESTEGG_CODEC_VORBIS) {
        // Get the Vorbis header data
        unsigned int nheaders = 0;
        r = nestegg_track_codec_data_count(mContext, track, &nheaders);
        if (r == -1 || nheaders != 3) {
          Cleanup();
          return NS_ERROR_FAILURE;
        }

        for (uint32_t header = 0; header < nheaders; ++header) {
          unsigned char* data = 0;
          size_t length = 0;

          r = nestegg_track_codec_data(mContext, track, header, &data, &length);
          if (r == -1) {
            Cleanup();
            return NS_ERROR_FAILURE;
          }
          ogg_packet opacket = InitOggPacket(data, length, header == 0, false, 0);

          r = vorbis_synthesis_headerin(&mVorbisInfo,
                                        &mVorbisComment,
                                        &opacket);
          if (r != 0) {
            Cleanup();
            return NS_ERROR_FAILURE;
          }
        }

        r = vorbis_synthesis_init(&mVorbisDsp, &mVorbisInfo);
        if (r != 0) {
          Cleanup();
          return NS_ERROR_FAILURE;
        }

        r = vorbis_block_init(&mVorbisDsp, &mVorbisBlock);
        if (r != 0) {
          Cleanup();
          return NS_ERROR_FAILURE;
        }

        mInfo.mAudioRate = mVorbisDsp.vi->rate;
        mInfo.mAudioChannels = mVorbisDsp.vi->channels;
        mChannels = mInfo.mAudioChannels;
#ifdef MOZ_OPUS
      } else if (mAudioCodec == NESTEGG_CODEC_OPUS) {
        unsigned char* data = 0;
        size_t length = 0;
        r = nestegg_track_codec_data(mContext, track, 0, &data, &length);
        if (r == -1) {
          Cleanup();
          return NS_ERROR_FAILURE;
        }

        mOpusParser = new OpusParser;
        if (!mOpusParser->DecodeHeader(data, length)) {
          Cleanup();
          return NS_ERROR_FAILURE;
        }

        if (!InitOpusDecoder()) {
          Cleanup();
          return NS_ERROR_FAILURE;
        }

        mInfo.mAudioRate = mOpusParser->mRate;

        mInfo.mAudioChannels = mOpusParser->mChannels;
        mInfo.mAudioChannels = mInfo.mAudioChannels > 2 ? 2 : mInfo.mAudioChannels;
#endif
      } else {
        Cleanup();
        return NS_ERROR_FAILURE;
      }
    }
  }

#ifdef MOZ_DASH
  // Byte range for cues has been specified; load them.
  if (!mCuesByteRange.IsNull()) {
    maxOffset = mCuesByteRange.mEnd;

    // Iterate through cluster ranges until nestegg returns the last one
    NS_ENSURE_TRUE(mClusterByteRanges.IsEmpty(),
                   NS_ERROR_ALREADY_INITIALIZED);
    int clusterNum = 0;
    bool done = false;
    uint64_t timestamp;
    do {
      mClusterByteRanges.AppendElement();
      r = nestegg_get_cue_point(mContext, clusterNum, maxOffset,
                                &(mClusterByteRanges[clusterNum].mStart),
                                &(mClusterByteRanges[clusterNum].mEnd),
                                &timestamp);
      if (r != 0) {
        Cleanup();
        return NS_ERROR_FAILURE;
      }
      LOG(PR_LOG_DEBUG, ("Reader [%p] for Decoder [%p]: Cluster [%d]: "
                         "start [%lld] end [%lld], timestamp [%.2llfs]",
                         this, mDecoder, clusterNum,
                         mClusterByteRanges[clusterNum].mStart,
                         mClusterByteRanges[clusterNum].mEnd,
                         timestamp/NS_PER_S));
      mClusterByteRanges[clusterNum].mStartTime = timestamp/NS_PER_USEC;
      // Last cluster will have '-1' as end value
      if (mClusterByteRanges[clusterNum].mEnd == -1) {
        mClusterByteRanges[clusterNum].mEnd = (mCuesByteRange.mStart-1);
        done = true;
      } else {
        clusterNum++;
      }
    } while (!done);
  }
#endif

  // We can't seek in buffered regions if we have no cues.
  mDecoder->SetMediaSeekable(nestegg_has_cues(mContext) == 1);

  *aInfo = mInfo;

  *aTags = nullptr;

#ifdef MOZ_DASH
  mDecoder->OnReadMetadataCompleted();
#endif

  return NS_OK;
}

#ifdef MOZ_OPUS
bool WebMReader::InitOpusDecoder()
{
  int r;

  NS_ASSERTION(mOpusDecoder == nullptr, "leaking OpusDecoder");

  mOpusDecoder = opus_multistream_decoder_create(mOpusParser->mRate,
                                             mOpusParser->mChannels,
                                             mOpusParser->mStreams,
                                             mOpusParser->mCoupledStreams,
                                             mOpusParser->mMappingTable,
                                             &r);
  mSkip = mOpusParser->mPreSkip;

  return r == OPUS_OK;
}
#endif

ogg_packet WebMReader::InitOggPacket(unsigned char* aData,
                                       size_t aLength,
                                       bool aBOS,
                                       bool aEOS,
                                       int64_t aGranulepos)
{
  ogg_packet packet;
  packet.packet = aData;
  packet.bytes = aLength;
  packet.b_o_s = aBOS;
  packet.e_o_s = aEOS;
  packet.granulepos = aGranulepos;
  packet.packetno = mPacketCount++;
  return packet;
}
 
bool WebMReader::DecodeAudioPacket(nestegg_packet* aPacket, int64_t aOffset)
{
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");

  int r = 0;
  unsigned int count = 0;
  r = nestegg_packet_count(aPacket, &count);
  if (r == -1) {
    return false;
  }

  uint64_t tstamp = 0;
  r = nestegg_packet_tstamp(aPacket, &tstamp);
  if (r == -1) {
    return false;
  }

  const uint32_t rate = mInfo.mAudioRate;
  uint64_t tstamp_usecs = tstamp / NS_PER_USEC;
  if (mAudioStartUsec == -1) {
    // This is the first audio chunk. Assume the start time of our decode
    // is the start of this chunk.
    mAudioStartUsec = tstamp_usecs;
  }
  // If there's a gap between the start of this audio chunk and the end of
  // the previous audio chunk, we need to increment the packet count so that
  // the vorbis decode doesn't use data from before the gap to help decode
  // from after the gap.
  CheckedInt64 tstamp_frames = UsecsToFrames(tstamp_usecs, rate);
  CheckedInt64 decoded_frames = UsecsToFrames(mAudioStartUsec, rate);
  if (!tstamp_frames.isValid() || !decoded_frames.isValid()) {
    NS_WARNING("Int overflow converting WebM times to frames");
    return false;
  }
  decoded_frames += mAudioFrames;
  if (!decoded_frames.isValid()) {
    NS_WARNING("Int overflow adding decoded_frames");
    return false;
  }
  if (tstamp_frames.value() > decoded_frames.value()) {
#ifdef DEBUG
    CheckedInt64 usecs = FramesToUsecs(tstamp_frames.value() - decoded_frames.value(), rate);
    LOG(PR_LOG_DEBUG, ("WebMReader detected gap of %lld, %lld frames, in audio stream\n",
      usecs.isValid() ? usecs.value() : -1,
      tstamp_frames.value() - decoded_frames.value()));
#endif
    mPacketCount++;
    mAudioStartUsec = tstamp_usecs;
    mAudioFrames = 0;
  }

  int32_t total_frames = 0;
  for (uint32_t i = 0; i < count; ++i) {
    unsigned char* data;
    size_t length;
    r = nestegg_packet_data(aPacket, i, &data, &length);
    if (r == -1) {
      return false;
    }
    if (mAudioCodec == NESTEGG_CODEC_VORBIS) {
      ogg_packet opacket = InitOggPacket(data, length, false, false, -1);

      if (vorbis_synthesis(&mVorbisBlock, &opacket) != 0) {
        return false;
      }

      if (vorbis_synthesis_blockin(&mVorbisDsp,
                                   &mVorbisBlock) != 0) {
        return false;
      }

      VorbisPCMValue** pcm = 0;
      int32_t frames = 0;
      while ((frames = vorbis_synthesis_pcmout(&mVorbisDsp, &pcm)) > 0) {
        nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frames * mChannels]);
        for (uint32_t j = 0; j < mChannels; ++j) {
          VorbisPCMValue* channel = pcm[j];
          for (uint32_t i = 0; i < uint32_t(frames); ++i) {
            buffer[i*mChannels + j] = MOZ_CONVERT_VORBIS_SAMPLE(channel[i]);
          }
        }

        CheckedInt64 duration = FramesToUsecs(frames, rate);
        if (!duration.isValid()) {
          NS_WARNING("Int overflow converting WebM audio duration");
          return false;
        }
        CheckedInt64 total_duration = FramesToUsecs(total_frames, rate);
        if (!total_duration.isValid()) {
          NS_WARNING("Int overflow converting WebM audio total_duration");
          return false;
        }

        CheckedInt64 time = total_duration + tstamp_usecs;
        if (!time.isValid()) {
          NS_WARNING("Int overflow adding total_duration and tstamp_usecs");
          nestegg_free_packet(aPacket);
          return false;
        };

        total_frames += frames;
        AudioQueue().Push(new AudioData(aOffset,
                                       time.value(),
                                       duration.value(),
                                       frames,
                                       buffer.forget(),
                                       mChannels));
        mAudioFrames += frames;
        if (vorbis_synthesis_read(&mVorbisDsp, frames) != 0) {
          return false;
        }
      }
    } else if (mAudioCodec == NESTEGG_CODEC_OPUS) {
#ifdef MOZ_OPUS
      uint32_t channels = mOpusParser->mChannels;

      // Maximum value is 63*2880, so there's no chance of overflow.
      int32_t frames_number = opus_packet_get_nb_frames(data, length);

      if (frames_number <= 0)
        return false; // Invalid packet header.
      int32_t samples = opus_packet_get_samples_per_frame(data,
                                                          (opus_int32) rate);
      int32_t frames = frames_number*samples;

      // A valid Opus packet must be between 2.5 and 120 ms long.
      if (frames < 120 || frames > 5760)
        return false;
      nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[frames * channels]);

      // Decode to the appropriate sample type.
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
      int ret = opus_multistream_decode_float(mOpusDecoder,
                                              data, length,
                                              buffer, frames, false);
#else
      int ret = opus_multistream_decode(mOpusDecoder,
                                        data, length,
                                        buffer, frames, false);
#endif
      if (ret < 0)
        return false;
      NS_ASSERTION(ret == frames, "Opus decoded too few audio samples");

      // Trim the initial frames while the decoder is settling.
      if (mSkip > 0) {
        int32_t skipFrames = std::min(mSkip, frames);
        if (skipFrames == frames) {
          // discard the whole packet
          mSkip -= frames;
          LOG(PR_LOG_DEBUG, ("Opus decoder skipping %d frames"
                             " (whole packet)", frames));
          return true;
        }
        int32_t keepFrames = frames - skipFrames;
        int samples = keepFrames * channels;
        nsAutoArrayPtr<AudioDataValue> trimBuffer(new AudioDataValue[samples]);
        for (int i = 0; i < samples; i++)
          trimBuffer[i] = buffer[skipFrames*channels + i];

        frames = keepFrames;
        buffer = trimBuffer;

        mSkip -= skipFrames;
        LOG(PR_LOG_DEBUG, ("Opus decoder skipping %d frames", skipFrames));
      }

      int64_t discardPadding = 0;
      r = nestegg_packet_discard_padding(aPacket, &discardPadding);
      if (r == -1) {
        return false;
      }
      if (discardPadding > 0) {
        CheckedInt64 discardFrames = UsecsToFrames(discardPadding * NS_PER_USEC, rate);
        if (!discardFrames.isValid()) {
          NS_WARNING("Int overflow in DiscardPadding");
          return false;
        }
        int32_t keepFrames = frames - discardFrames.value();
        if (keepFrames > 0) {
          int samples = keepFrames * channels;
          nsAutoArrayPtr<AudioDataValue> trimBuffer(new AudioDataValue[samples]);
          for (int i = 0; i < samples; i++)
            trimBuffer[i] = buffer[i];
          frames = keepFrames;
          buffer = trimBuffer;
        } else {
          LOG(PR_LOG_DEBUG, ("Opus decoder discarding whole packet"
                             " ( %d frames) as padding", frames));
          return true;
        }
      }

      // Apply the header gain if one was specified.
#ifdef MOZ_SAMPLE_TYPE_FLOAT32
      if (mOpusParser->mGain != 1.0f) {
        float gain = mOpusParser->mGain;
        int samples = frames * channels;
        for (int i = 0; i < samples; i++) {
          buffer[i] *= gain;
        }
      }
#else
      if (mOpusParser->mGain_Q16 != 65536) {
        int64_t gain_Q16 = mOpusParser->mGain_Q16;
        int samples = frames * channels;
        for (int i = 0; i < samples; i++) {
          int32_t val = static_cast<int32_t>((gain_Q16*buffer[i] + 32768)>>16);
          buffer[i] = static_cast<AudioDataValue>(MOZ_CLIP_TO_15(val));
        }
      }
#endif

      // More than 2 decoded channels must be downmixed to stereo.
      if (channels > 2) {
        // Opus doesn't provide a channel mapping for more than 8 channels,
        // so we can't downmix more than that.
        if (channels > 8)
          return false;
        OggReader::DownmixToStereo(buffer, channels, frames);
      }

      CheckedInt64 duration = FramesToUsecs(frames, rate);
      if (!duration.isValid()) {
        NS_WARNING("Int overflow converting WebM audio duration");
        return false;
      }

      CheckedInt64 time = tstamp_usecs;
      if (!time.isValid()) {
        NS_WARNING("Int overflow adding total_duration and tstamp_usecs");
        nestegg_free_packet(aPacket);
        return false;
      };

      AudioQueue().Push(new AudioData(mDecoder->GetResource()->Tell(),
                                     time.value(),
                                     duration.value(),
                                     frames,
                                     buffer.forget(),
                                     mChannels));

      mAudioFrames += frames;
#else
      return false;
#endif /* MOZ_OPUS */
    }
  }

  return true;
}

nsReturnRef<NesteggPacketHolder> WebMReader::NextPacket(TrackType aTrackType)
#ifdef MOZ_DASH
{
  nsAutoRef<NesteggPacketHolder> holder;
  // It is possible that following a seek, a requested switching offset could
  // be reached before |DASHReader| calls |RequestSwitchAtSubsegment|. In this
  // case |mNextReader| will be null, so check its value and at every possible
  // switch access point, i.e. cluster boundary, ask |mMainReader| to
  // |GetReaderForSubsegment|.
  if (mMainReader && !mNextReader && aTrackType == VIDEO) {
    WebMReader* nextReader = nullptr;
    LOG(PR_LOG_DEBUG,
      ("WebMReader[%p] for decoder [%p] NextPacket mNextReader not set: "
       "mCurrentOffset[%lld] nextCluster [%d] comparing with offset[%lld]",
       this, mDecoder, mCurrentOffset, mNextCluster,
       mClusterByteRanges[mNextCluster].mStart));

    if (mNextCluster < mClusterByteRanges.Length() &&
        mCurrentOffset == mClusterByteRanges[mNextCluster].mStart) {
      DASHRepReader* nextDASHRepReader =
        mMainReader->GetReaderForSubsegment(mNextCluster);
      nextReader = static_cast<WebMReader*>(nextDASHRepReader);
      LOG(PR_LOG_DEBUG,
          ("WebMReader[%p] for decoder [%p] reached SAP at cluster [%d]: next "
         "reader is [%p]", this, mDecoder, mNextCluster, nextReader));
      if (nextReader && nextReader != this) {
        {
          ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
          // Ensure this reader is set to switch for the next packet.
          RequestSwitchAtSubsegment(mNextCluster, nextReader);
          NS_ASSERTION(mNextReader == nextReader, "mNextReader should be set");
          // Ensure the next reader seeks to |mNextCluster|. |PrepareToDecode|
          // must be called to ensure the reader's variables are set correctly.
          nextReader->RequestSeekToSubsegment(mNextCluster);
          nextReader->PrepareToDecode();
        }
      }
      // Keep mNextCluster up-to-date with the |mCurrentOffset|.
      if (mNextCluster+1 < mClusterByteRanges.Length()) {
        // At least one more cluster to go.
        mNextCluster++;
      } else {
        // Reached last cluster; prepare for being in cluster 0 again.
        mNextCluster = 1;
      }
      LOG(PR_LOG_DEBUG,
          ("WebMReader [%p] for decoder [%p] updating mNextCluster to [%d] "
           "at offset [%lld]", this, mDecoder, mNextCluster, mCurrentOffset));
    }
  }

  // Get packet from next reader if we're at a switching point; most likely we
  // did not download the next packet for this reader's stream, so we have to
  // get it from the next one. Note: Switch to next reader only for video;
  // audio switching is not supported in the DASH-WebM On Demand profile.
  if (aTrackType == VIDEO &&
      (uint32_t)mSwitchingCluster < mClusterByteRanges.Length() &&
      mCurrentOffset == mClusterByteRanges[mSwitchingCluster].mStart) {

    if (mVideoPackets.GetSize() > 0) {
      holder = NextPacketInternal(VIDEO);
      LOG(PR_LOG_DEBUG,
          ("WebMReader[%p] got packet from mVideoPackets @[%lld]",
           this, holder->mOffset));
    } else {
      mReachedSwitchAccessPoint = true;
      NS_ASSERTION(mNextReader,
                   "Stream switch has been requested but mNextReader is null");
      holder = mNextReader->NextPacket(aTrackType);
      mPushVideoPacketToNextReader = true;
      // Reset for possible future switches.
      mSwitchingCluster = -1;
      LOG(PR_LOG_DEBUG,
          ("WebMReader[%p] got packet from mNextReader[%p] @[%lld]",
           this, mNextReader.get(), (holder ? holder->mOffset : 0)));
    }
  } else {
    holder = NextPacketInternal(aTrackType);
    if (holder) {
      mCurrentOffset = holder->mOffset;
    }
  }
  return holder.out();
}

nsReturnRef<NesteggPacketHolder>
WebMReader::NextPacketInternal(TrackType aTrackType)
#endif
{
  // The packet queue that packets will be pushed on if they
  // are not the type we are interested in.
  WebMPacketQueue& otherPackets =
    aTrackType == VIDEO ? mAudioPackets : mVideoPackets;

  // The packet queue for the type that we are interested in.
  WebMPacketQueue &packets =
    aTrackType == VIDEO ? mVideoPackets : mAudioPackets;

  // Flag to indicate that we do need to playback these types of
  // packets.
  bool hasType = aTrackType == VIDEO ? mHasVideo : mHasAudio;

  // Flag to indicate that we do need to playback the other type
  // of track.
  bool hasOtherType = aTrackType == VIDEO ? mHasAudio : mHasVideo;

  // Track we are interested in
  uint32_t ourTrack = aTrackType == VIDEO ? mVideoTrack : mAudioTrack;

  // Value of other track
  uint32_t otherTrack = aTrackType == VIDEO ? mAudioTrack : mVideoTrack;

  nsAutoRef<NesteggPacketHolder> holder;

  if (packets.GetSize() > 0) {
    holder.own(packets.PopFront());
  } else {
    // Keep reading packets until we find a packet
    // for the track we want.
    do {
      nestegg_packet* packet;
      int r = nestegg_read_packet(mContext, &packet);
      if (r <= 0) {
        return nsReturnRef<NesteggPacketHolder>();
      }
      int64_t offset = mDecoder->GetResource()->Tell();
      holder.own(new NesteggPacketHolder(packet, offset));

      unsigned int track = 0;
      r = nestegg_packet_track(packet, &track);
      if (r == -1) {
        return nsReturnRef<NesteggPacketHolder>();
      }

      if (hasOtherType && otherTrack == track) {
        // Save the packet for when we want these packets
        otherPackets.Push(holder.disown());
        continue;
      }

      // The packet is for the track we want to play
      if (hasType && ourTrack == track) {
        break;
      }
    } while (true);
  }

  return holder.out();
}

bool WebMReader::DecodeAudioData()
{
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");

  nsAutoRef<NesteggPacketHolder> holder(NextPacket(AUDIO));
  if (!holder) {
    AudioQueue().Finish();
    return false;
  }

  return DecodeAudioPacket(holder->mPacket, holder->mOffset);
}

bool WebMReader::DecodeVideoFrame(bool &aKeyframeSkip,
                                      int64_t aTimeThreshold)
{
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");

  // Record number of frames decoded and parsed. Automatically update the
  // stats counters using the AutoNotifyDecoded stack-based class.
  uint32_t parsed = 0, decoded = 0;
  AbstractMediaDecoder::AutoNotifyDecoded autoNotify(mDecoder, parsed, decoded);

  nsAutoRef<NesteggPacketHolder> holder(NextPacket(VIDEO));
  if (!holder) {
    VideoQueue().Finish();
    return false;
  }

  nestegg_packet* packet = holder->mPacket;
  unsigned int track = 0;
  int r = nestegg_packet_track(packet, &track);
  if (r == -1) {
    return false;
  }

  unsigned int count = 0;
  r = nestegg_packet_count(packet, &count);
  if (r == -1) {
    return false;
  }

  uint64_t tstamp = 0;
  r = nestegg_packet_tstamp(packet, &tstamp);
  if (r == -1) {
    return false;
  }

  // The end time of this frame is the start time of the next frame.  Fetch
  // the timestamp of the next packet for this track.  If we've reached the
  // end of the resource, use the file's duration as the end time of this
  // video frame.
  uint64_t next_tstamp = 0;
  {
    nsAutoRef<NesteggPacketHolder> next_holder(NextPacket(VIDEO));
    if (next_holder) {
      r = nestegg_packet_tstamp(next_holder->mPacket, &next_tstamp);
      if (r == -1) {
        return false;
      }
      PushVideoPacket(next_holder.disown());
    } else {
      ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
      int64_t endTime = mDecoder->GetEndMediaTime();
      if (endTime == -1) {
        return false;
      }
      next_tstamp = endTime * NS_PER_USEC;
    }
  }

  int64_t tstamp_usecs = tstamp / NS_PER_USEC;
  for (uint32_t i = 0; i < count; ++i) {
    unsigned char* data;
    size_t length;
    r = nestegg_packet_data(packet, i, &data, &length);
    if (r == -1) {
      return false;
    }

    vpx_codec_stream_info_t si;
    memset(&si, 0, sizeof(si));
    si.sz = sizeof(si);
    if (mVideoCodec == NESTEGG_CODEC_VP8) {
      vpx_codec_peek_stream_info(vpx_codec_vp8_dx(), data, length, &si);
    } else if (mVideoCodec == NESTEGG_CODEC_VP9) {
      vpx_codec_peek_stream_info(vpx_codec_vp9_dx(), data, length, &si);
    }
    if (aKeyframeSkip && (!si.is_kf || tstamp_usecs < aTimeThreshold)) {
      // Skipping to next keyframe...
      parsed++; // Assume 1 frame per chunk.
      continue;
    }

    if (aKeyframeSkip && si.is_kf) {
      aKeyframeSkip = false;
    }

    if (vpx_codec_decode(&mVPX, data, length, NULL, 0)) {
      return false;
    }

    // If the timestamp of the video frame is less than
    // the time threshold required then it is not added
    // to the video queue and won't be displayed.
    if (tstamp_usecs < aTimeThreshold) {
      parsed++; // Assume 1 frame per chunk.
      continue;
    }

    vpx_codec_iter_t  iter = NULL;
    vpx_image_t      *img;

    while ((img = vpx_codec_get_frame(&mVPX, &iter))) {
      NS_ASSERTION(img->fmt == IMG_FMT_I420, "WebM image format is not I420");

      // Chroma shifts are rounded down as per the decoding examples in the VP8 SDK
      VideoData::YCbCrBuffer b;
      b.mPlanes[0].mData = img->planes[0];
      b.mPlanes[0].mStride = img->stride[0];
      b.mPlanes[0].mHeight = img->d_h;
      b.mPlanes[0].mWidth = img->d_w;
      b.mPlanes[0].mOffset = b.mPlanes[0].mSkip = 0;

      b.mPlanes[1].mData = img->planes[1];
      b.mPlanes[1].mStride = img->stride[1];
      b.mPlanes[1].mHeight = (img->d_h + 1) >> img->y_chroma_shift;
      b.mPlanes[1].mWidth = (img->d_w + 1) >> img->x_chroma_shift;
      b.mPlanes[1].mOffset = b.mPlanes[1].mSkip = 0;
 
      b.mPlanes[2].mData = img->planes[2];
      b.mPlanes[2].mStride = img->stride[2];
      b.mPlanes[2].mHeight = (img->d_h + 1) >> img->y_chroma_shift;
      b.mPlanes[2].mWidth = (img->d_w + 1) >> img->x_chroma_shift;
      b.mPlanes[2].mOffset = b.mPlanes[2].mSkip = 0;
  
      nsIntRect picture = mPicture;
      if (img->d_w != static_cast<uint32_t>(mInitialFrame.width) ||
          img->d_h != static_cast<uint32_t>(mInitialFrame.height)) {
        // Frame size is different from what the container reports. This is legal
        // in WebM, and we will preserve the ratio of the crop rectangle as it
        // was reported relative to the picture size reported by the container.
        picture.x = (mPicture.x * img->d_w) / mInitialFrame.width;
        picture.y = (mPicture.y * img->d_h) / mInitialFrame.height;
        picture.width = (img->d_w * mPicture.width) / mInitialFrame.width;
        picture.height = (img->d_h * mPicture.height) / mInitialFrame.height;
      }

      VideoData *v = VideoData::Create(mInfo,
                                       mDecoder->GetImageContainer(),
                                       holder->mOffset,
                                       tstamp_usecs,
                                       next_tstamp / NS_PER_USEC,
                                       b,
                                       si.is_kf,
                                       -1,
                                       picture);
      if (!v) {
        return false;
      }
      parsed++;
      decoded++;
      NS_ASSERTION(decoded <= parsed,
        "Expect only 1 frame per chunk per packet in WebM...");
      VideoQueue().Push(v);
    }
  }

  return true;
}

void
WebMReader::PushVideoPacket(NesteggPacketHolder* aItem)
{
#ifdef MOZ_DASH
  if (mPushVideoPacketToNextReader) {
    NS_ASSERTION(mNextReader,
                 "Stream switch has been requested but mNextReader is null");
    mNextReader->mVideoPackets.PushFront(aItem);
    mPushVideoPacketToNextReader = false;
  } else {
#endif
    mVideoPackets.PushFront(aItem);
#ifdef MOZ_DASH
  }
#endif
}

nsresult WebMReader::Seek(int64_t aTarget, int64_t aStartTime, int64_t aEndTime,
                            int64_t aCurrentTime)
{
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");

  LOG(PR_LOG_DEBUG, ("Reader [%p] for Decoder [%p]: About to seek to %fs",
                     this, mDecoder, aTarget/1000000.0));
  if (NS_FAILED(ResetDecode())) {
    return NS_ERROR_FAILURE;
  }
  uint32_t trackToSeek = mHasVideo ? mVideoTrack : mAudioTrack;
  int r = nestegg_track_seek(mContext, trackToSeek, aTarget * NS_PER_USEC);
  if (r != 0) {
    return NS_ERROR_FAILURE;
  }
#ifdef MOZ_DASH
  // Find next cluster index;
  MediaResource* resource = mDecoder->GetResource();
  int64_t newOffset = resource->Tell();
  for (uint32_t i = 1; i < mClusterByteRanges.Length(); i++) {
    if (newOffset < mClusterByteRanges[i].mStart) {
      mNextCluster = i;
      LOG(PR_LOG_DEBUG,
          ("WebMReader [%p] for decoder [%p] updating mNextCluster to [%d] "
           "after seek to offset [%lld]",
           this, mDecoder, mNextCluster, resource->Tell()));
      break;
    }
  }
#endif
  return DecodeToTarget(aTarget);
}

#ifdef MOZ_DASH
bool WebMReader::IsDataCachedAtEndOfSubsegments()
{
  MediaResource* resource = mDecoder->GetResource();
  NS_ENSURE_TRUE(resource, false);
  if (resource->IsDataCachedToEndOfResource(0)) {
     return true;
  }

  if (mClusterByteRanges.IsEmpty()) {
    return false;
  }

  nsTArray<MediaByteRange> ranges;
  nsresult rv = resource->GetCachedRanges(ranges);
  NS_ENSURE_SUCCESS(rv, false);
  if (ranges.IsEmpty()) {
    return false;
  }

  // Return true if data at the end of the final subsegment is cached.
  uint32_t finalSubsegmentIndex = mClusterByteRanges.Length()-1;
  uint64_t finalSubEndOffset = mClusterByteRanges[finalSubsegmentIndex].mEnd;
  uint32_t finalRangeIndex = ranges.Length()-1;
  uint64_t finalRangeStartOffset = ranges[finalRangeIndex].mStart;
  uint64_t finalRangeEndOffset = ranges[finalRangeIndex].mEnd;

  return (finalRangeStartOffset < finalSubEndOffset &&
          finalSubEndOffset <= finalRangeEndOffset);
}
#endif

nsresult WebMReader::GetBuffered(dom::TimeRanges* aBuffered, int64_t aStartTime)
{
  MediaResource* resource = mDecoder->GetResource();

  uint64_t timecodeScale;
  if (!mContext || nestegg_tstamp_scale(mContext, &timecodeScale) == -1) {
    return NS_OK;
  }

  // Special case completely cached files.  This also handles local files.
  bool isFullyCached = resource->IsDataCachedToEndOfResource(0);
  if (isFullyCached) {
    uint64_t duration = 0;
    if (nestegg_duration(mContext, &duration) == 0) {
      aBuffered->Add(0, duration / NS_PER_S);
    }
  }

  uint32_t bufferedLength = 0;
  aBuffered->GetLength(&bufferedLength);

  // Either we the file is not fully cached, or we couldn't find a duration in
  // the WebM bitstream.
  if (!isFullyCached || !bufferedLength) {
    MediaResource* resource = mDecoder->GetResource();
    nsTArray<MediaByteRange> ranges;
    nsresult res = resource->GetCachedRanges(ranges);
    NS_ENSURE_SUCCESS(res, res);

    for (uint32_t index = 0; index < ranges.Length(); index++) {
      uint64_t start, end;
      bool rv = mBufferedState->CalculateBufferedForRange(ranges[index].mStart,
                                                          ranges[index].mEnd,
                                                          &start, &end);
      if (rv) {
        double startTime = start * timecodeScale / NS_PER_S - aStartTime;
        double endTime = end * timecodeScale / NS_PER_S - aStartTime;
#ifdef MOZ_DASH
        // If this range extends to the end of a cluster, the true end time is
        // the cluster's end timestamp. Since WebM frames do not have an end
        // timestamp, a fully cached cluster must be reported with the correct
        // end time of its final frame. Otherwise, buffered ranges could be
        // reported with missing frames at cluster boundaries, specifically
        // boundaries where stream switching has occurred.
        if (!mClusterByteRanges.IsEmpty()) {
          for (uint32_t clusterIndex = 0;
               clusterIndex < (mClusterByteRanges.Length()-1);
               clusterIndex++) {
            if (ranges[index].mEnd >= mClusterByteRanges[clusterIndex].mEnd) {
              double clusterEndTime =
                  mClusterByteRanges[clusterIndex+1].mStartTime / USEC_PER_S;
              if (endTime < clusterEndTime) {
                LOG(PR_LOG_DEBUG, ("End of cluster: endTime becoming %0.3fs",
                                   clusterEndTime));
                endTime = clusterEndTime;
              }
            }
          }
        }
#endif
        // If this range extends to the end of the file, the true end time
        // is the file's duration.
        if (resource->IsDataCachedToEndOfResource(ranges[index].mStart)) {
          uint64_t duration = 0;
          if (nestegg_duration(mContext, &duration) == 0) {
            endTime = duration / NS_PER_S;
          }
        }

        aBuffered->Add(startTime, endTime);
      }
    }
  }

  return NS_OK;
}

void WebMReader::NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset)
{
  mBufferedState->NotifyDataArrived(aBuffer, aLength, aOffset);
}

#ifdef MOZ_DASH
int64_t
WebMReader::GetSubsegmentForSeekTime(int64_t aSeekToTime)
{
  NS_ENSURE_TRUE(0 <= aSeekToTime, -1);
  // Check the first n-1 subsegments. End time is the start time of the next
  // subsegment.
  for (uint32_t i = 1; i < (mClusterByteRanges.Length()); i++) {
    if (aSeekToTime < mClusterByteRanges[i].mStartTime) {
      return i-1;
    }
  }
  // Check the last subsegment. End time is the end time of the file.
  NS_ASSERTION(mDecoder, "Decoder should not be null!");
  if (aSeekToTime <= mDecoder->GetMediaDuration()) {
    return mClusterByteRanges.Length()-1;
  }

  return (-1);
}
nsresult
WebMReader::GetSubsegmentByteRanges(nsTArray<MediaByteRange>& aByteRanges)
{
  NS_ENSURE_TRUE(mContext, NS_ERROR_NULL_POINTER);
  NS_ENSURE_TRUE(aByteRanges.IsEmpty(), NS_ERROR_ALREADY_INITIALIZED);
  NS_ENSURE_FALSE(mClusterByteRanges.IsEmpty(), NS_ERROR_NOT_INITIALIZED);
  NS_ENSURE_FALSE(mCuesByteRange.IsNull(), NS_ERROR_NOT_INITIALIZED);

  for (uint32_t i = 0; i < mClusterByteRanges.Length(); i++) {
    aByteRanges.AppendElement();
    aByteRanges[i] = mClusterByteRanges[i];
  }

  return NS_OK;
}

void
WebMReader::RequestSwitchAtSubsegment(int32_t aSubsegmentIdx,
                                      MediaDecoderReader* aNextReader)
{
  NS_ASSERTION(NS_IsMainThread() || mDecoder->OnDecodeThread(),
               "Should be on main thread or decode thread.");
  mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();

  // Only allow one switch at a time; ignore if one is already requested.
  if (mSwitchingCluster != -1) {
    return;
  }
  NS_ENSURE_TRUE_VOID((uint32_t)aSubsegmentIdx < mClusterByteRanges.Length());
  mSwitchingCluster = aSubsegmentIdx;
  NS_ENSURE_TRUE_VOID(aNextReader);
  NS_ENSURE_TRUE_VOID(aNextReader != this);
  mNextReader = static_cast<WebMReader*>(aNextReader);
}

void
WebMReader::RequestSeekToSubsegment(uint32_t aIdx)
{
  NS_ASSERTION(NS_IsMainThread() || mDecoder->OnDecodeThread(),
               "Should be on main thread or decode thread.");
  NS_ASSERTION(mDecoder, "decoder should not be null!");
  mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();

  // Don't seek if we're about to switch to another reader.
  if (mSwitchingCluster != -1) {
    return;
  }
  // Only allow seeking if a request was not already made.
  if (mSeekToCluster != -1) {
    return;
  }
  NS_ENSURE_TRUE_VOID(aIdx < mClusterByteRanges.Length());
  mSeekToCluster = aIdx;

  // XXX Hack to get the resource to seek to the correct offset if the decode
  // thread is in shutdown, e.g. if the video is not autoplay.
  if (mDecoder->IsShutdown()) {
    ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
    mDecoder->GetResource()->Seek(PR_SEEK_SET,
                                  mClusterByteRanges[mSeekToCluster].mStart);
  }
}

void
WebMReader::PrepareToDecode()
{
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
  if (mSeekToCluster != -1) {
    ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
    SeekToCluster(mSeekToCluster);
  }
}

void
WebMReader::SeekToCluster(uint32_t aIdx)
{
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
  NS_ENSURE_TRUE_VOID(aIdx < mClusterByteRanges.Length());
  LOG(PR_LOG_DEBUG, ("Reader [%p] for Decoder [%p]: seeking to "
                     "subsegment [%lld] at offset [%lld]",
                     this, mDecoder, aIdx, mClusterByteRanges[aIdx].mStart));
  int r = nestegg_offset_seek(mContext, mClusterByteRanges[aIdx].mStart);
  NS_ENSURE_TRUE_VOID(r == 0);
  if (aIdx + 1 < mClusterByteRanges.Length()) {
    mNextCluster = aIdx + 1;
  } else {
    mNextCluster = 1;
  }
  mSeekToCluster = -1;
}

bool
WebMReader::HasReachedSubsegment(uint32_t aSubsegmentIndex)
{
  NS_ASSERTION(mDecoder, "Decoder is null.");
  NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread.");
  NS_ENSURE_TRUE(aSubsegmentIndex < mClusterByteRanges.Length(), false);

  NS_ASSERTION(mDecoder->GetResource(), "Decoder has no media resource.");
  if (mReachedSwitchAccessPoint) {
    LOG(PR_LOG_DEBUG,
        ("Reader [%p] for Decoder [%p]: reached switching offset [%lld] = "
         "mClusterByteRanges[%d].mStart[%lld]",
         this, mDecoder, mCurrentOffset, aSubsegmentIndex,
         mClusterByteRanges[aSubsegmentIndex].mStart));
    mReachedSwitchAccessPoint = false;
    return true;
  }
  return false;
}
#endif /* MOZ_DASH */

} // namespace mozilla