1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef ReaderProxy_h_
8 #define ReaderProxy_h_
9 
10 #include "mozilla/AbstractThread.h"
11 #include "mozilla/RefPtr.h"
12 #include "mozilla/Variant.h"
13 #include "nsISupportsImpl.h"
14 
15 #include "MediaEventSource.h"
16 #include "MediaFormatReader.h"
17 
18 namespace mozilla {
19 
20 /**
21  * A wrapper around MediaFormatReader to offset the timestamps of Audio/Video
22  * samples by the start time to ensure MDSM can always assume zero start time.
23  * It also adjusts the seek target passed to Seek() to ensure correct seek time
24  * is passed to the underlying reader.
25  */
26 class ReaderProxy {
27   using MetadataPromise = MediaFormatReader::MetadataPromise;
28   using AudioDataPromise = MediaFormatReader::AudioDataPromise;
29   using VideoDataPromise = MediaFormatReader::VideoDataPromise;
30   using SeekPromise = MediaFormatReader::SeekPromise;
31   using WaitForDataPromise = MediaFormatReader::WaitForDataPromise;
32   using TrackSet = MediaFormatReader::TrackSet;
33   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ReaderProxy);
34 
35  public:
36   ReaderProxy(AbstractThread* aOwnerThread, MediaFormatReader* aReader);
37 
38   media::TimeUnit StartTime() const;
39   RefPtr<MetadataPromise> ReadMetadata();
40 
41   RefPtr<AudioDataPromise> RequestAudioData();
42 
43   RefPtr<VideoDataPromise> RequestVideoData(
44       const media::TimeUnit& aTimeThreshold);
45 
46   RefPtr<WaitForDataPromise> WaitForData(MediaData::Type aType);
47 
48   RefPtr<SeekPromise> Seek(const SeekTarget& aTarget);
49   RefPtr<ShutdownPromise> Shutdown();
50 
51   void ReleaseResources();
52   void ResetDecode(TrackSet aTracks);
53 
Init()54   nsresult Init() { return mReader->Init(); }
UseBufferingHeuristics()55   bool UseBufferingHeuristics() const {
56     return mReader->UseBufferingHeuristics();
57   }
58 
VideoIsHardwareAccelerated()59   bool VideoIsHardwareAccelerated() const {
60     return mReader->VideoIsHardwareAccelerated();
61   }
TimedMetadataEvent()62   TimedMetadataEventSource& TimedMetadataEvent() {
63     return mReader->TimedMetadataEvent();
64   }
OnMediaNotSeekable()65   MediaEventSource<void>& OnMediaNotSeekable() {
66     return mReader->OnMediaNotSeekable();
67   }
SizeOfAudioQueueInFrames()68   size_t SizeOfAudioQueueInFrames() const {
69     return mReader->SizeOfAudioQueueInFrames();
70   }
SizeOfVideoQueueInFrames()71   size_t SizeOfVideoQueueInFrames() const {
72     return mReader->SizeOfVideoQueueInFrames();
73   }
ReadUpdatedMetadata(MediaInfo * aInfo)74   void ReadUpdatedMetadata(MediaInfo* aInfo) {
75     mReader->ReadUpdatedMetadata(aInfo);
76   }
CanonicalBuffered()77   AbstractCanonical<media::TimeIntervals>* CanonicalBuffered() {
78     return mReader->CanonicalBuffered();
79   }
80 
SetCDMProxy(CDMProxy * aProxy)81   void SetCDMProxy(CDMProxy* aProxy) { mReader->SetCDMProxy(aProxy); }
82 
83   void SetVideoBlankDecode(bool aIsBlankDecode);
84 
85   void SetCanonicalDuration(
86       AbstractCanonical<media::NullableTimeUnit>* aCanonical);
87 
88  private:
89   ~ReaderProxy();
90   RefPtr<MetadataPromise> OnMetadataRead(MetadataHolder&& aMetadata);
91   RefPtr<MetadataPromise> OnMetadataNotRead(const MediaResult& aError);
92   void UpdateDuration();
93   RefPtr<SeekPromise> SeekInternal(const SeekTarget& aTarget);
94 
95   RefPtr<ReaderProxy::AudioDataPromise> OnAudioDataRequestCompleted(
96       RefPtr<AudioData> aAudio);
97   RefPtr<ReaderProxy::AudioDataPromise> OnAudioDataRequestFailed(
98       const MediaResult& aError);
99 
100   const RefPtr<AbstractThread> mOwnerThread;
101   const RefPtr<MediaFormatReader> mReader;
102 
103   bool mShutdown = false;
104   Maybe<media::TimeUnit> mStartTime;
105 
106   // State-watching manager.
107   WatchManager<ReaderProxy> mWatchManager;
108 
109   // Duration, mirrored from the state machine task queue.
110   Mirror<media::NullableTimeUnit> mDuration;
111 };
112 
113 }  // namespace mozilla
114 
115 #endif  // ReaderProxy_h_
116