1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
7 #if !defined(HLSDemuxer_h_)
10 #include "AutoTaskQueue.h"
11 #include "GeneratedJNINatives.h"
12 #include "GeneratedJNIWrappers.h"
13 #include "MediaCodec.h"
14 #include "MediaDataDemuxer.h"
15 #include "MediaDecoder.h"
16 #include "mozilla/Atomics.h"
17 #include "mozilla/Maybe.h"
18 #include "mozilla/Mutex.h"
20 #include "VideoUtils.h"
26 class HLSTrackDemuxer
;
28 DDLoggedTypeDeclNameAndBase(HLSDemuxer
, MediaDataDemuxer
);
29 DDLoggedTypeNameAndBase(HLSTrackDemuxer
, MediaTrackDemuxer
);
31 class HLSDemuxer final
32 : public MediaDataDemuxer
33 , public DecoderDoctorLifeLogger
<HLSDemuxer
>
35 class HLSDemuxerCallbacksSupport
;
37 explicit HLSDemuxer(int aPlayerId
);
39 RefPtr
<InitPromise
> Init() override
;
41 uint32_t GetNumberTracks(TrackInfo::TrackType aType
) const override
;
43 already_AddRefed
<MediaTrackDemuxer
>
44 GetTrackDemuxer(TrackInfo::TrackType aType
, uint32_t aTrackNumber
) override
;
46 bool IsSeekable() const override
;
48 UniquePtr
<EncryptionInfo
> GetCrypto() override
;
50 bool ShouldComputeStartTime() const override
{ return true; }
52 void NotifyDataArrived() override
;
54 AutoTaskQueue
* GetTaskQueue() const { return mTaskQueue
; }
55 void OnInitialized(bool aHasAudio
, bool aHasVideo
);
56 void OnError(int aErrorCode
);
59 media::TimeUnit
GetNextKeyFrameTime();
61 bool OnTaskQueue() const;
63 friend class HLSTrackDemuxer
;
65 const RefPtr
<AutoTaskQueue
> mTaskQueue
;
66 RefPtr
<HLSTrackDemuxer
> mAudioDemuxer
;
67 RefPtr
<HLSTrackDemuxer
> mVideoDemuxer
;
69 MozPromiseHolder
<InitPromise
> mInitPromise
;
70 RefPtr
<HLSDemuxerCallbacksSupport
> mCallbackSupport
;
72 java::GeckoHLSDemuxerWrapper::Callbacks::GlobalRef mJavaCallbacks
;
73 java::GeckoHLSDemuxerWrapper::GlobalRef mHLSDemuxerWrapper
;
77 : public MediaTrackDemuxer
78 , public DecoderDoctorLifeLogger
<HLSTrackDemuxer
>
81 HLSTrackDemuxer(HLSDemuxer
* aParent
,
82 TrackInfo::TrackType aType
,
83 UniquePtr
<TrackInfo
> aTrackInfo
);
85 UniquePtr
<TrackInfo
> GetInfo() const override
;
87 RefPtr
<SeekPromise
> Seek(const media::TimeUnit
& aTime
) override
;
89 RefPtr
<SamplesPromise
> GetSamples(int32_t aNumSamples
= 1) override
;
91 void UpdateMediaInfo(int index
);
93 void Reset() override
;
95 nsresult
GetNextRandomAccessPoint(media::TimeUnit
* aTime
) override
;
97 RefPtr
<SkipAccessPointPromise
> SkipToNextRandomAccessPoint(
98 const media::TimeUnit
& aTimeThreshold
) override
;
100 media::TimeIntervals
GetBuffered() override
;
102 void BreakCycles() override
;
104 bool GetSamplesMayBlock() const override
109 bool IsTrackValid() const
111 MutexAutoLock
lock(mMutex
);
112 return mTrackInfo
->IsValid();
115 // Update the timestamp of the next keyframe if there's one.
116 void UpdateNextKeyFrameTime();
118 // Runs on HLSDemuxer's task queue.
119 RefPtr
<SeekPromise
> DoSeek(const media::TimeUnit
& aTime
);
120 RefPtr
<SamplesPromise
> DoGetSamples(int32_t aNumSamples
);
121 RefPtr
<SkipAccessPointPromise
> DoSkipToNextRandomAccessPoint(
122 const media::TimeUnit
& aTimeThreshold
);
124 CryptoSample
ExtractCryptoSample(size_t aSampleSize
,
125 java::sdk::CryptoInfo::LocalRef aCryptoInfo
);
126 RefPtr
<MediaRawData
> ConvertToMediaRawData(java::GeckoHLSSample::LocalRef aSample
);
128 RefPtr
<HLSDemuxer
> mParent
;
129 TrackInfo::TrackType mType
;
130 Maybe
<media::TimeUnit
> mNextKeyframeTime
;
131 int32_t mLastFormatIndex
= -1;
132 // Queued samples extracted by the demuxer, but not yet returned.
133 RefPtr
<MediaRawData
> mQueuedSample
;
135 // Mutex to protect members below across multiple threads.
136 mutable Mutex mMutex
;
137 UniquePtr
<TrackInfo
> mTrackInfo
;
140 } // namespace mozilla