summaryrefslogtreecommitdiff
path: root/dom/media/MediaDecoderReaderWrapper.h
blob: a0f845538e9c3b7e869a8ce228a1d6ed81b9aee6 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef MediaDecoderReaderWrapper_h_
#define MediaDecoderReaderWrapper_h_

#include "mozilla/AbstractThread.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Variant.h"
#include "nsISupportsImpl.h"

#include "MediaDecoderReader.h"
#include "MediaEventSource.h"

namespace mozilla {

class StartTimeRendezvous;

typedef MozPromise<bool, bool, /* isExclusive = */ false> HaveStartTimePromise;

typedef Variant<MediaData*, MediaResult> AudioCallbackData;
typedef Variant<Tuple<MediaData*, TimeStamp>, MediaResult> VideoCallbackData;
typedef Variant<MediaData::Type, WaitForDataRejectValue> WaitCallbackData;

/**
 * A wrapper around MediaDecoderReader to offset the timestamps of Audio/Video
 * samples by the start time to ensure MDSM can always assume zero start time.
 * It also adjusts the seek target passed to Seek() to ensure correct seek time
 * is passed to the underlying reader.
 */
class MediaDecoderReaderWrapper {
  typedef MediaDecoderReader::MetadataPromise MetadataPromise;
  typedef MediaDecoderReader::MediaDataPromise MediaDataPromise;
  typedef MediaDecoderReader::SeekPromise SeekPromise;
  typedef MediaDecoderReader::WaitForDataPromise WaitForDataPromise;
  typedef MediaDecoderReader::BufferedUpdatePromise BufferedUpdatePromise;
  typedef MediaDecoderReader::TrackSet TrackSet;
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDecoderReaderWrapper);

private:
  MediaCallbackExc<AudioCallbackData> mAudioCallback;
  MediaCallbackExc<VideoCallbackData> mVideoCallback;
  MediaCallbackExc<WaitCallbackData> mAudioWaitCallback;
  MediaCallbackExc<WaitCallbackData> mVideoWaitCallback;

public:
  MediaDecoderReaderWrapper(AbstractThread* aOwnerThread,
                            MediaDecoderReader* aReader);

  media::TimeUnit StartTime() const;
  RefPtr<MetadataPromise> ReadMetadata();
  RefPtr<HaveStartTimePromise> AwaitStartTime();

  decltype(mAudioCallback)& AudioCallback() { return mAudioCallback; }
  decltype(mVideoCallback)& VideoCallback() { return mVideoCallback; }
  decltype(mAudioWaitCallback)& AudioWaitCallback() { return mAudioWaitCallback; }
  decltype(mVideoWaitCallback)& VideoWaitCallback() { return mVideoWaitCallback; }

  // NOTE: please set callbacks before requesting audio/video data!
  void RequestAudioData();
  void RequestVideoData(bool aSkipToNextKeyframe, media::TimeUnit aTimeThreshold);

  // NOTE: please set callbacks before invoking WaitForData()!
  void WaitForData(MediaData::Type aType);

  bool IsRequestingAudioData() const;
  bool IsRequestingVideoData() const;
  bool IsWaitingAudioData() const;
  bool IsWaitingVideoData() const;

  RefPtr<SeekPromise> Seek(SeekTarget aTarget, media::TimeUnit aEndTime);
  RefPtr<BufferedUpdatePromise> UpdateBufferedWithPromise();
  RefPtr<ShutdownPromise> Shutdown();

  void ReleaseResources();
  void SetIdle();
  void ResetDecode(TrackSet aTracks);

  nsresult Init() { return mReader->Init(); }
  bool IsWaitForDataSupported() const { return mReader->IsWaitForDataSupported(); }
  bool IsAsync() const { return mReader->IsAsync(); }
  bool UseBufferingHeuristics() const { return mReader->UseBufferingHeuristics(); }
  bool ForceZeroStartTime() const { return mReader->ForceZeroStartTime(); }

  bool VideoIsHardwareAccelerated() const {
    return mReader->VideoIsHardwareAccelerated();
  }
  TimedMetadataEventSource& TimedMetadataEvent() {
    return mReader->TimedMetadataEvent();
  }
  MediaEventSource<void>& OnMediaNotSeekable() {
    return mReader->OnMediaNotSeekable();
  }
  size_t SizeOfVideoQueueInBytes() const {
    return mReader->SizeOfVideoQueueInBytes();
  }
  size_t SizeOfAudioQueueInBytes() const {
    return mReader->SizeOfAudioQueueInBytes();
  }
  size_t SizeOfAudioQueueInFrames() const {
    return mReader->SizeOfAudioQueueInFrames();
  }
  size_t SizeOfVideoQueueInFrames() const {
    return mReader->SizeOfVideoQueueInFrames();
  }
  void ReadUpdatedMetadata(MediaInfo* aInfo) {
    mReader->ReadUpdatedMetadata(aInfo);
  }
  AbstractCanonical<media::TimeIntervals>* CanonicalBuffered() {
    return mReader->CanonicalBuffered();
  }

#ifdef MOZ_EME
  void SetCDMProxy(CDMProxy* aProxy) { mReader->SetCDMProxy(aProxy); }
#endif

  void SetVideoBlankDecode(bool aIsBlankDecode);

private:
  ~MediaDecoderReaderWrapper();

  void OnMetadataRead(MetadataHolder* aMetadata);
  void OnMetadataNotRead() {}
  MediaCallbackExc<WaitCallbackData>& WaitCallbackRef(MediaData::Type aType);
  MozPromiseRequestHolder<WaitForDataPromise>& WaitRequestRef(MediaData::Type aType);

  const bool mForceZeroStartTime;
  const RefPtr<AbstractThread> mOwnerThread;
  const RefPtr<MediaDecoderReader> mReader;

  bool mShutdown = false;
  RefPtr<StartTimeRendezvous> mStartTimeRendezvous;

  MozPromiseRequestHolder<MediaDataPromise> mAudioDataRequest;
  MozPromiseRequestHolder<MediaDataPromise> mVideoDataRequest;
  MozPromiseRequestHolder<WaitForDataPromise> mAudioWaitRequest;
  MozPromiseRequestHolder<WaitForDataPromise> mVideoWaitRequest;
};

} // namespace mozilla

#endif // MediaDecoderReaderWrapper_h_