RemoteDataDecoder.h (3723B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef RemoteDataDecoder_h_ 6 #define RemoteDataDecoder_h_ 7 8 #include "AndroidDecoderModule.h" 9 #include "SurfaceTexture.h" 10 #include "TimeUnits.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/Monitor.h" 13 #include "mozilla/java/CodecProxyWrappers.h" 14 15 namespace mozilla { 16 17 DDLoggedTypeDeclNameAndBase(RemoteDataDecoder, MediaDataDecoder); 18 19 class RemoteDataDecoder : public MediaDataDecoder, 20 public DecoderDoctorLifeLogger<RemoteDataDecoder> { 21 public: 22 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RemoteDataDecoder, final); 23 24 static already_AddRefed<MediaDataDecoder> CreateAudioDecoder( 25 const CreateDecoderParams& aParams, const nsString& aDrmStubId, 26 CDMProxy* aProxy); 27 28 static already_AddRefed<MediaDataDecoder> CreateVideoDecoder( 29 const CreateDecoderParams& aParams, const nsString& aDrmStubId, 30 CDMProxy* aProxy); 31 32 RefPtr<DecodePromise> Decode(MediaRawData* aSample) override; 33 RefPtr<DecodePromise> Drain() override; 34 RefPtr<FlushPromise> Flush() override; 35 RefPtr<ShutdownPromise> Shutdown() override; 36 nsCString GetDescriptionName() const override { 37 return "android decoder (remote)"_ns; 38 } 39 40 protected: 41 virtual ~RemoteDataDecoder() = default; 42 RemoteDataDecoder(MediaData::Type aType, const nsACString& aMimeType, 43 java::sdk::MediaFormat::Param aFormat, 44 const nsString& aDrmStubId); 45 46 // Methods only called on mThread. 47 void UpdateInputStatus(int64_t aTimestamp, bool aProcessed); 48 void UpdateOutputStatus(RefPtr<MediaData>&& aSample); 49 void ReturnDecodedData(); 50 void DrainComplete(); 51 void Error(const MediaResult& aError); 52 void AssertOnThread() const { 53 // mThread may not be set if Init hasn't been called first. 54 MOZ_ASSERT(!mThread || mThread->IsOnCurrentThread()); 55 } 56 57 enum class State { DRAINED, DRAINABLE, DRAINING, SHUTDOWN }; 58 void SetState(State aState); 59 State GetState() const { 60 AssertOnThread(); 61 return mState; 62 } 63 64 // Whether the sample will be used. 65 virtual bool IsUsefulData(const RefPtr<MediaData>& aSample) { return true; } 66 67 MediaData::Type mType; 68 69 nsAutoCString mMimeType; 70 java::sdk::MediaFormat::GlobalRef mFormat; 71 72 java::CodecProxy::GlobalRef mJavaDecoder; 73 java::CodecProxy::NativeCallbacks::GlobalRef mJavaCallbacks; 74 nsString mDrmStubId; 75 76 nsCOMPtr<nsISerialEventTarget> mThread; 77 78 // Preallocated Java object used as a reusable storage for input buffer 79 // information. Contents must be changed only on mThread. 80 java::sdk::MediaCodec::BufferInfo::GlobalRef mInputBufferInfo; 81 82 // Session ID attached to samples. It is returned by CodecProxy::Input(). 83 // Accessed on mThread only. 84 int64_t mSession; 85 86 private: 87 enum class PendingOp { INCREASE, DECREASE, CLEAR }; 88 void UpdatePendingInputStatus(PendingOp aOp); 89 size_t HasPendingInputs() { 90 AssertOnThread(); 91 return mNumPendingInputs > 0; 92 } 93 94 // Returns true if we are in a state which requires a new decoder to be 95 // created. In this case all errors will be reported as 96 // NS_ERROR_DOM_MEDIA_NEED_NEW_DECODER to avoid reporting errors as fatal when 97 // they can be fixed with a new decoder. 98 virtual bool NeedsNewDecoder() const { return false; } 99 100 // The following members must only be accessed on mThread. 101 MozPromiseHolder<DecodePromise> mDecodePromise; 102 MozPromiseHolder<DecodePromise> mDrainPromise; 103 DecodedData mDecodedData; 104 Maybe<MediaResult> mDecodeError; 105 State mState = State::DRAINED; 106 size_t mNumPendingInputs; 107 }; 108 109 } // namespace mozilla 110 111 #endif