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/. */
6 
7 #include "MediaDataDecoderProxy.h"
8 
9 namespace mozilla {
10 
Init()11 RefPtr<MediaDataDecoder::InitPromise> MediaDataDecoderProxy::Init() {
12   MOZ_ASSERT(!mIsShutdown);
13 
14   if (!mProxyThread) {
15     return mProxyDecoder->Init();
16   }
17   return InvokeAsync(mProxyThread, __func__, [self = RefPtr{this}] {
18     return self->mProxyDecoder->Init();
19   });
20 }
21 
Decode(MediaRawData * aSample)22 RefPtr<MediaDataDecoder::DecodePromise> MediaDataDecoderProxy::Decode(
23     MediaRawData* aSample) {
24   MOZ_ASSERT(!mIsShutdown);
25 
26   if (!mProxyThread) {
27     return mProxyDecoder->Decode(aSample);
28   }
29   RefPtr<MediaRawData> sample = aSample;
30   return InvokeAsync(mProxyThread, __func__, [self = RefPtr{this}, sample] {
31     return self->mProxyDecoder->Decode(sample);
32   });
33 }
34 
CanDecodeBatch() const35 bool MediaDataDecoderProxy::CanDecodeBatch() const {
36   return mProxyDecoder->CanDecodeBatch();
37 }
38 
DecodeBatch(nsTArray<RefPtr<MediaRawData>> && aSamples)39 RefPtr<MediaDataDecoder::DecodePromise> MediaDataDecoderProxy::DecodeBatch(
40     nsTArray<RefPtr<MediaRawData>>&& aSamples) {
41   MOZ_ASSERT(!mIsShutdown);
42   if (!mProxyThread) {
43     return mProxyDecoder->DecodeBatch(std::move(aSamples));
44   }
45   return InvokeAsync(
46       mProxyThread, __func__,
47       [self = RefPtr{this}, samples = std::move(aSamples)]() mutable {
48         return self->mProxyDecoder->DecodeBatch(std::move(samples));
49       });
50 }
51 
Flush()52 RefPtr<MediaDataDecoder::FlushPromise> MediaDataDecoderProxy::Flush() {
53   MOZ_ASSERT(!mIsShutdown);
54 
55   if (!mProxyThread) {
56     return mProxyDecoder->Flush();
57   }
58   return InvokeAsync(mProxyThread, __func__, [self = RefPtr{this}] {
59     return self->mProxyDecoder->Flush();
60   });
61 }
62 
Drain()63 RefPtr<MediaDataDecoder::DecodePromise> MediaDataDecoderProxy::Drain() {
64   MOZ_ASSERT(!mIsShutdown);
65 
66   if (!mProxyThread) {
67     return mProxyDecoder->Drain();
68   }
69   return InvokeAsync(mProxyThread, __func__, [self = RefPtr{this}] {
70     return self->mProxyDecoder->Drain();
71   });
72 }
73 
Shutdown()74 RefPtr<ShutdownPromise> MediaDataDecoderProxy::Shutdown() {
75   MOZ_ASSERT(!mIsShutdown);
76 
77 #if defined(DEBUG)
78   mIsShutdown = true;
79 #endif
80 
81   if (!mProxyThread) {
82     return mProxyDecoder->Shutdown();
83   }
84   // We chain another promise to ensure that the proxied decoder gets destructed
85   // on the proxy thread.
86   return InvokeAsync(mProxyThread, __func__, [self = RefPtr{this}] {
87     RefPtr<ShutdownPromise> p = self->mProxyDecoder->Shutdown()->Then(
88         self->mProxyThread, __func__,
89         [self](const ShutdownPromise::ResolveOrRejectValue& aResult) {
90           self->mProxyDecoder = nullptr;
91           return ShutdownPromise::CreateAndResolveOrReject(aResult, __func__);
92         });
93     return p;
94   });
95 }
96 
GetDescriptionName() const97 nsCString MediaDataDecoderProxy::GetDescriptionName() const {
98   MOZ_ASSERT(!mIsShutdown);
99 
100   return mProxyDecoder->GetDescriptionName();
101 }
102 
IsHardwareAccelerated(nsACString & aFailureReason) const103 bool MediaDataDecoderProxy::IsHardwareAccelerated(
104     nsACString& aFailureReason) const {
105   MOZ_ASSERT(!mIsShutdown);
106 
107   return mProxyDecoder->IsHardwareAccelerated(aFailureReason);
108 }
109 
SetSeekThreshold(const media::TimeUnit & aTime)110 void MediaDataDecoderProxy::SetSeekThreshold(const media::TimeUnit& aTime) {
111   MOZ_ASSERT(!mIsShutdown);
112 
113   if (!mProxyThread) {
114     mProxyDecoder->SetSeekThreshold(aTime);
115     return;
116   }
117   media::TimeUnit time = aTime;
118   mProxyThread->Dispatch(NS_NewRunnableFunction(
119       "MediaDataDecoderProxy::SetSeekThreshold", [self = RefPtr{this}, time] {
120         self->mProxyDecoder->SetSeekThreshold(time);
121       }));
122 }
123 
SupportDecoderRecycling() const124 bool MediaDataDecoderProxy::SupportDecoderRecycling() const {
125   MOZ_ASSERT(!mIsShutdown);
126 
127   return mProxyDecoder->SupportDecoderRecycling();
128 }
129 
NeedsConversion() const130 MediaDataDecoder::ConversionRequired MediaDataDecoderProxy::NeedsConversion()
131     const {
132   MOZ_ASSERT(!mIsShutdown);
133 
134   return mProxyDecoder->NeedsConversion();
135 }
136 
137 }  // namespace mozilla
138