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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/* 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/. */
#include "WebrtcMediaDataDecoderCodec.h"
#include "ImageContainer.h"
#include "MediaDataDecoderProxy.h"
#include "PDMFactory.h"
#include "VideoUtils.h"
#include "mozilla/layers/ImageBridgeChild.h"
#include "mozilla/media/MediaUtils.h"
#include "mozilla/StaticPrefs_media.h"
namespace mozilla {
WebrtcMediaDataDecoder::WebrtcMediaDataDecoder(nsACString& aCodecMimeType,
TrackingId aTrackingId)
: mThreadPool(GetMediaThreadPool(MediaThreadType::SUPERVISOR)),
mTaskQueue(TaskQueue::Create(do_AddRef(mThreadPool),
"WebrtcMediaDataDecoder::mTaskQueue")),
mImageContainer(MakeAndAddRef<layers::ImageContainer>(
layers::ImageContainer::ASYNCHRONOUS)),
mFactory(new PDMFactory()),
mTrackType(TrackInfo::kUndefinedTrack),
mCodecType(aCodecMimeType),
mTrackingId(std::move(aTrackingId)) {}
WebrtcMediaDataDecoder::~WebrtcMediaDataDecoder() {}
bool WebrtcMediaDataDecoder::Configure(
const webrtc::VideoDecoder::Settings& settings) {
nsCString codec;
mTrackType = TrackInfo::kVideoTrack;
mInfo = VideoInfo(settings.max_render_resolution().Width(),
settings.max_render_resolution().Height());
mInfo.mMimeType = mCodecType;
#ifdef MOZ_WIDGET_GTK
if (mInfo.mMimeType.EqualsLiteral("video/vp8") &&
!StaticPrefs::media_navigator_mediadatadecoder_vp8_hardware_enabled()) {
mDisabledHardwareAcceleration = true;
}
#endif
return WEBRTC_VIDEO_CODEC_OK == CreateDecoder();
}
int32_t WebrtcMediaDataDecoder::Decode(const webrtc::EncodedImage& aInputImage,
bool aMissingFrames,
int64_t aRenderTimeMs) {
if (!mCallback || !mDecoder) {
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
if (!aInputImage.data() || !aInputImage.size()) {
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}
// Always start with a complete key frame.
if (mNeedKeyframe) {
if (aInputImage._frameType != webrtc::VideoFrameType::kVideoFrameKey)
return WEBRTC_VIDEO_CODEC_ERROR;
// We have a key frame - is it complete?
mNeedKeyframe = false;
}
auto disabledHardwareAcceleration =
MakeScopeExit([&] { mDisabledHardwareAcceleration = true; });
RefPtr<MediaRawData> compressedFrame =
new MediaRawData(aInputImage.data(), aInputImage.size());
if (!compressedFrame->Data()) {
return WEBRTC_VIDEO_CODEC_MEMORY;
}
compressedFrame->mTime =
media::TimeUnit::FromMicroseconds(aInputImage.Timestamp());
compressedFrame->mTimecode =
media::TimeUnit::FromMicroseconds(aRenderTimeMs * 1000);
compressedFrame->mKeyframe =
aInputImage._frameType == webrtc::VideoFrameType::kVideoFrameKey;
{
media::Await(
do_AddRef(mThreadPool), mDecoder->Decode(compressedFrame),
[&](const MediaDataDecoder::DecodedData& aResults) {
mResults = aResults.Clone();
mError = NS_OK;
},
[&](const MediaResult& aError) { mError = aError; });
for (auto& frame : mResults) {
MOZ_ASSERT(frame->mType == MediaData::Type::VIDEO_DATA);
RefPtr<VideoData> video = frame->As<VideoData>();
MOZ_ASSERT(video);
if (!video->mImage) {
// Nothing to display.
continue;
}
rtc::scoped_refptr<ImageBuffer> image(
new rtc::RefCountedObject<ImageBuffer>(std::move(video->mImage)));
auto videoFrame = webrtc::VideoFrame::Builder()
.set_video_frame_buffer(image)
.set_timestamp_rtp(aInputImage.Timestamp())
.set_rotation(aInputImage.rotation_)
.build();
mCallback->Decoded(videoFrame);
}
mResults.Clear();
}
if (NS_FAILED(mError) && mError != NS_ERROR_DOM_MEDIA_CANCELED) {
CreateDecoder();
return WEBRTC_VIDEO_CODEC_ERROR;
}
if (NS_FAILED(mError)) {
return WEBRTC_VIDEO_CODEC_ERROR;
}
disabledHardwareAcceleration.release();
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t WebrtcMediaDataDecoder::RegisterDecodeCompleteCallback(
webrtc::DecodedImageCallback* aCallback) {
mCallback = aCallback;
return WEBRTC_VIDEO_CODEC_OK;
}
int32_t WebrtcMediaDataDecoder::Release() {
if (mDecoder) {
RefPtr<MediaDataDecoder> decoder = std::move(mDecoder);
decoder->Flush()->Then(mTaskQueue, __func__,
[decoder]() { decoder->Shutdown(); });
}
mNeedKeyframe = true;
mError = NS_OK;
return WEBRTC_VIDEO_CODEC_OK;
}
bool WebrtcMediaDataDecoder::OnTaskQueue() const {
return mTaskQueue->IsOnCurrentThread();
}
int32_t WebrtcMediaDataDecoder::CreateDecoder() {
RefPtr<layers::KnowsCompositor> knowsCompositor =
layers::ImageBridgeChild::GetSingleton();
if (mDecoder) {
Release();
}
RefPtr<TaskQueue> tq =
TaskQueue::Create(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER),
"webrtc decode TaskQueue");
RefPtr<MediaDataDecoder> decoder;
media::Await(do_AddRef(mThreadPool), InvokeAsync(tq, __func__, [&] {
RefPtr<GenericPromise> p =
mFactory
->CreateDecoder(
{mInfo,
CreateDecoderParams::OptionSet(
CreateDecoderParams::Option::LowLatency,
CreateDecoderParams::Option::FullH264Parsing,
CreateDecoderParams::Option::
ErrorIfNoInitializationData,
mDisabledHardwareAcceleration
? CreateDecoderParams::Option::
HardwareDecoderNotAllowed
: CreateDecoderParams::Option::Default),
mTrackType, mImageContainer, knowsCompositor,
Some(mTrackingId)})
->Then(
tq, __func__,
[&](RefPtr<MediaDataDecoder>&& aDecoder) {
decoder = std::move(aDecoder);
return GenericPromise::CreateAndResolve(
true, __func__);
},
[](const MediaResult& aResult) {
return GenericPromise::CreateAndReject(
NS_ERROR_DOM_MEDIA_FATAL_ERR, __func__);
});
return p;
}));
if (!decoder) {
return WEBRTC_VIDEO_CODEC_ERROR;
}
// We need to wrap our decoder in a MediaDataDecoderProxy so that it always
// run on an nsISerialEventTarget (which the webrtc code doesn't do)
mDecoder = new MediaDataDecoderProxy(decoder.forget(), tq.forget());
media::Await(
do_AddRef(mThreadPool), mDecoder->Init(),
[&](TrackInfo::TrackType) { mError = NS_OK; },
[&](const MediaResult& aError) { mError = aError; });
return NS_SUCCEEDED(mError) ? WEBRTC_VIDEO_CODEC_OK
: WEBRTC_VIDEO_CODEC_ERROR;
}
} // namespace mozilla
|