OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/gpu/android_video_decode_accelerator.h" | 5 #include "media/gpu/android_video_decode_accelerator.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/memory/weak_ptr.h" | |
14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
17 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h" | 19 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h" |
18 #include "media/base/android/media_codec_util.h" | 20 #include "media/base/android/media_codec_util.h" |
19 #include "media/base/android/media_jni_registrar.h" | 21 #include "media/base/android/media_jni_registrar.h" |
22 #include "media/base/android/mock_android_overlay.h" | |
23 #include "media/base/android/mock_media_codec_bridge.h" | |
20 #include "media/gpu/android_video_decode_accelerator.h" | 24 #include "media/gpu/android_video_decode_accelerator.h" |
25 #include "media/gpu/android_video_surface_chooser.h" | |
21 #include "media/gpu/avda_codec_allocator.h" | 26 #include "media/gpu/avda_codec_allocator.h" |
22 #include "media/video/picture.h" | 27 #include "media/video/picture.h" |
23 #include "media/video/video_decode_accelerator.h" | 28 #include "media/video/video_decode_accelerator.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
31 #include "ui/gl/android/surface_texture.h" | |
26 #include "ui/gl/gl_context.h" | 32 #include "ui/gl/gl_context.h" |
27 #include "ui/gl/gl_surface.h" | 33 #include "ui/gl/gl_surface.h" |
28 #include "ui/gl/init/gl_factory.h" | 34 #include "ui/gl/init/gl_factory.h" |
29 | 35 |
30 using ::testing::_; | 36 using ::testing::_; |
31 using ::testing::NiceMock; | 37 using ::testing::NiceMock; |
38 using ::testing::NotNull; | |
39 using ::testing::Return; | |
32 | 40 |
33 namespace media { | 41 namespace media { |
34 namespace { | 42 namespace { |
35 | 43 |
36 #define SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE() \ | 44 #define SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE() \ |
37 do { \ | 45 do { \ |
38 if (!MediaCodecUtil::IsMediaCodecAvailable()) \ | 46 if (!MediaCodecUtil::IsMediaCodecAvailable()) \ |
39 return; \ | 47 return; \ |
40 } while (false) | 48 } while (false) |
41 | 49 |
42 bool MakeContextCurrent() { | 50 bool MakeContextCurrent() { |
43 return true; | 51 return true; |
44 } | 52 } |
45 | 53 |
46 base::WeakPtr<gpu::gles2::GLES2Decoder> GetGLES2Decoder( | 54 base::WeakPtr<gpu::gles2::GLES2Decoder> GetGLES2Decoder( |
47 const base::WeakPtr<gpu::gles2::GLES2Decoder>& decoder) { | 55 const base::WeakPtr<gpu::gles2::GLES2Decoder>& decoder) { |
48 return decoder; | 56 return decoder; |
49 } | 57 } |
50 | 58 |
51 ACTION(PostNullCodec) { | |
52 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
53 FROM_HERE, base::Bind(&media::AVDACodecAllocatorClient::OnCodecConfigured, | |
54 arg0, nullptr)); | |
55 } | |
56 | |
57 } // namespace | 59 } // namespace |
58 | 60 |
59 class MockVDAClient : public VideoDecodeAccelerator::Client { | |
60 public: | |
61 MOCK_METHOD1(NotifyInitializationComplete, void(bool)); | |
62 MOCK_METHOD5( | |
63 ProvidePictureBuffers, | |
64 void(uint32_t, VideoPixelFormat, uint32_t, const gfx::Size&, uint32_t)); | |
65 MOCK_METHOD1(DismissPictureBuffer, void(int32_t)); | |
66 MOCK_METHOD1(PictureReady, void(const Picture&)); | |
67 MOCK_METHOD1(NotifyEndOfBitstreamBuffer, void(int32_t)); | |
68 MOCK_METHOD0(NotifyFlushDone, void()); | |
69 MOCK_METHOD0(NotifyResetDone, void()); | |
70 MOCK_METHOD1(NotifyError, void(VideoDecodeAccelerator::Error)); | |
71 }; | |
72 | |
73 class MockCodecAllocator : public AVDACodecAllocator { | |
74 public: | |
75 MOCK_METHOD2(CreateMediaCodecAsync, | |
76 void(base::WeakPtr<AVDACodecAllocatorClient>, | |
77 scoped_refptr<CodecConfig>)); | |
78 }; | |
79 | |
80 class AndroidVideoDecodeAcceleratorTest : public testing::Test { | 61 class AndroidVideoDecodeAcceleratorTest : public testing::Test { |
81 public: | 62 public: |
63 class MockVDAClient : public VideoDecodeAccelerator::Client { | |
watk
2017/05/02 21:00:01
I'd prefer if these classes were in an anonymous n
liberato (no reviews please)
2017/05/02 21:50:04
Done.
| |
64 public: | |
65 MockVDAClient() {} | |
66 | |
67 MOCK_METHOD1(NotifyInitializationComplete, void(bool)); | |
68 MOCK_METHOD5( | |
69 ProvidePictureBuffers, | |
70 void(uint32_t, VideoPixelFormat, uint32_t, const gfx::Size&, uint32_t)); | |
71 MOCK_METHOD1(DismissPictureBuffer, void(int32_t)); | |
72 MOCK_METHOD1(PictureReady, void(const Picture&)); | |
73 MOCK_METHOD1(NotifyEndOfBitstreamBuffer, void(int32_t)); | |
74 MOCK_METHOD0(NotifyFlushDone, void()); | |
75 MOCK_METHOD0(NotifyResetDone, void()); | |
76 MOCK_METHOD1(NotifyError, void(VideoDecodeAccelerator::Error)); | |
77 | |
78 private: | |
79 DISALLOW_COPY_AND_ASSIGN(MockVDAClient); | |
80 }; | |
81 | |
82 // Codec allocator that lets you count calls on Mock* methods, and also | |
83 // lets you answer the codec. | |
84 class FakeCodecAllocator : public NiceMock<AVDACodecAllocator> { | |
85 public: | |
86 FakeCodecAllocator() {} | |
87 | |
88 // These are called with some parameters of the codec config by our | |
89 // implementatin of their respective functions. This allows tests to set | |
90 // expectations on them. | |
91 MOCK_METHOD2(MockCreateMediaCodecSync, | |
92 void(AndroidOverlay*, gl::SurfaceTexture*)); | |
93 MOCK_METHOD2(MockCreateMediaCodecAsync, | |
94 void(AndroidOverlay*, gl::SurfaceTexture*)); | |
95 | |
96 // Note that this doesn't exactly match the signature, since unique_ptr | |
97 // doesn't work. plus, we expand |surface_bundle| a bit to make it more | |
98 // convenient to set expectations. | |
99 MOCK_METHOD3(MockReleaseMediaCodec, | |
100 void(MediaCodecBridge*, AndroidOverlay*, gl::SurfaceTexture*)); | |
101 | |
102 std::unique_ptr<MediaCodecBridge> CreateMediaCodecSync( | |
103 scoped_refptr<CodecConfig> codec_config) override { | |
104 MockCreateMediaCodecSync( | |
105 codec_config->surface_bundle->overlay.get(), | |
106 codec_config->surface_bundle->surface_texture.get()); | |
107 | |
108 CopyCodecAllocParams(codec_config); | |
109 | |
110 std::unique_ptr<MockMediaCodecBridge> codec; | |
111 if (allow_sync_creation) | |
112 codec = base::MakeUnique<MockMediaCodecBridge>(); | |
113 | |
114 if (codec) { | |
115 most_recent_codec_ = codec.get(); | |
116 most_recent_codec_destruction_observer_ = | |
117 codec->CreateDestructionObserver(); | |
118 most_recent_codec_destruction_observer_->DoNotAllowDestruction(); | |
119 } else { | |
120 most_recent_codec_ = nullptr; | |
121 most_recent_codec_destruction_observer_ = nullptr; | |
122 } | |
123 | |
124 return std::move(codec); | |
125 } | |
126 | |
127 void CreateMediaCodecAsync(base::WeakPtr<AVDACodecAllocatorClient> client, | |
128 scoped_refptr<CodecConfig> config) override { | |
129 // Clear |most_recent_codec_| until somebody calls Provide*CodecAsync(). | |
130 most_recent_codec_ = nullptr; | |
131 most_recent_codec_destruction_observer_ = nullptr; | |
132 CopyCodecAllocParams(config); | |
133 client_ = client; | |
134 | |
135 MockCreateMediaCodecAsync(most_recent_overlay(), | |
136 most_recent_surface_texture()); | |
137 } | |
138 | |
139 void ReleaseMediaCodec( | |
140 std::unique_ptr<MediaCodecBridge> media_codec, | |
141 TaskType task_type, | |
142 const scoped_refptr<AVDASurfaceBundle>& surface_bundle) override { | |
143 MockReleaseMediaCodec(media_codec.get(), surface_bundle->overlay.get(), | |
144 surface_bundle->surface_texture.get()); | |
145 } | |
146 | |
147 // Provide AVDA with a mock codec. | |
148 void ProvideMockCodecAsync() { | |
149 // If AVDA hasn't requested a codec, then get mad. | |
150 ASSERT_TRUE(client_); | |
151 if (!client_) | |
152 return; | |
153 | |
154 std::unique_ptr<MockMediaCodecBridge> codec = | |
155 base::MakeUnique<MockMediaCodecBridge>(); | |
156 most_recent_codec_ = codec.get(); | |
157 most_recent_codec_destruction_observer_ = | |
158 codec->CreateDestructionObserver(); | |
159 most_recent_codec_destruction_observer_->DoNotAllowDestruction(); | |
160 client_->OnCodecConfigured(std::move(codec)); | |
161 } | |
162 | |
163 // Provide AVDA will a null codec. | |
164 void ProvideNullCodecAsync() { | |
165 // If AVDA hasn't requested a codec, then get mad. | |
166 ASSERT_TRUE(client_); | |
167 if (!client_) | |
168 return; | |
169 | |
170 most_recent_codec_ = nullptr; | |
171 client_->OnCodecConfigured(nullptr); | |
172 } | |
173 | |
174 // Return the most recent bundle that we've received for codec allocation. | |
175 scoped_refptr<AVDASurfaceBundle> most_recent_bundle() { | |
176 return most_recent_bundle_; | |
177 } | |
178 | |
179 // Return the most recent codec that we provided, which might already have | |
180 // been freed. By default, the destruction observer will fail the test | |
181 // if this happens, unless the expectation is explicitly changed. If you | |
182 // change it, then use this with caution. | |
183 MockMediaCodecBridge* most_recent_codec() { return most_recent_codec_; } | |
184 | |
185 // Return the destruction observer for the most recent codec. We retain | |
186 // ownership of it. | |
187 DestructionObservable::DestructionObserver* codec_destruction_observer() { | |
188 return most_recent_codec_destruction_observer_.get(); | |
189 } | |
190 | |
191 // Return the most recent overlay / etc. that we were given during codec | |
192 // allocation (sync or async). | |
193 AndroidOverlay* most_recent_overlay() { return most_recent_overlay_; } | |
194 gl::SurfaceTexture* most_recent_surface_texture() { | |
195 return most_recent_surface_texture_; | |
196 } | |
197 | |
198 // Most recent codec that we've created via CreateMockCodec, since we have | |
199 // to assign ownership. It may be freed already. | |
200 MockMediaCodecBridge* most_recent_codec_; | |
201 | |
202 // DestructionObserver for |most_recent_codec_|. | |
203 std::unique_ptr<DestructionObservable::DestructionObserver> | |
204 most_recent_codec_destruction_observer_; | |
205 | |
206 // Most recent surface bundle that we've gotten during codec allocation. | |
207 // This should be the same as |config_->surface_bundle| initially, but AVDA | |
208 // might change it. | |
209 scoped_refptr<AVDASurfaceBundle> most_recent_bundle_; | |
210 | |
211 // Most recent overlay provided during codec allocation. | |
212 AndroidOverlay* most_recent_overlay_ = nullptr; | |
213 | |
214 // Most recent surface texture provided during codec allocation. | |
215 gl::SurfaceTexture* most_recent_surface_texture_ = nullptr; | |
216 | |
217 // Should a call to CreateMediaCodecSync succeed? | |
218 bool allow_sync_creation = true; | |
219 | |
220 private: | |
221 // Copy |config| and all of the parameters we care about. We don't leave | |
222 // them all in config, since AVDA could change them at any time. | |
223 void CopyCodecAllocParams(scoped_refptr<CodecConfig> config) { | |
224 config_ = config; | |
225 most_recent_bundle_ = config->surface_bundle; | |
226 most_recent_overlay_ = config->surface_bundle->overlay.get(); | |
227 | |
228 most_recent_surface_texture_ = | |
229 config->surface_bundle->surface_texture.get(); | |
230 } | |
231 | |
232 base::WeakPtr<AVDACodecAllocatorClient> client_; | |
233 scoped_refptr<CodecConfig> config_; | |
234 | |
235 DISALLOW_COPY_AND_ASSIGN(FakeCodecAllocator); | |
236 }; | |
237 | |
238 // Overlay helper that calls mocked functions to allow counting calls, but | |
239 // also records arguments. Note that gmock can't mock out unique_ptrs | |
240 // anyway, so we can't mock AndroidVideoSurfaceChooser directly. | |
241 class FakeOverlayHelper : public NiceMock<AndroidVideoSurfaceChooser> { | |
242 public: | |
243 FakeOverlayHelper() : weak_factory_(this) {} | |
244 | |
245 // These are called by the real functions. You may set expectations on | |
246 // them if you like. | |
247 MOCK_METHOD0(MockInitialize, void()); | |
248 MOCK_METHOD0(MockReplaceOverlayFactory, void()); | |
249 | |
250 // We guarantee that we'll only clear this during destruction, so that you | |
251 // may treat it as "pointer that lasts as long as |this| does". | |
252 base::WeakPtr<FakeOverlayHelper> GetWeakPtrForTesting() { | |
253 return weak_factory_.GetWeakPtr(); | |
254 } | |
255 | |
256 void Initialize( | |
257 UseOverlayCB use_overlay_cb, | |
258 UseSurfaceTextureCB use_surface_texture_cb, | |
259 StopUsingOverlayImmediatelyCB stop_immediately_cb, | |
260 std::unique_ptr<AndroidOverlayFactory> initial_factory) override { | |
261 MockInitialize(); | |
262 | |
263 factory_ = std::move(initial_factory); | |
264 use_overlay_cb_ = std::move(use_overlay_cb); | |
265 use_surface_texture_cb_ = std::move(use_surface_texture_cb); | |
266 stop_immediately_cb_ = std::move(stop_immediately_cb); | |
267 } | |
268 | |
269 void ReplaceOverlayFactory( | |
270 std::unique_ptr<AndroidOverlayFactory> factory) override { | |
271 MockReplaceOverlayFactory(); | |
272 factory_ = std::move(factory); | |
273 } | |
274 | |
275 // Notify AVDA to use a surface texture. | |
276 void ProvideSurfaceTexture() { | |
277 use_surface_texture_cb_.Run(); | |
278 base::RunLoop().RunUntilIdle(); | |
279 } | |
280 | |
281 void ProvideOverlay(std::unique_ptr<AndroidOverlay> overlay) { | |
282 use_overlay_cb_.Run(std::move(overlay)); | |
283 base::RunLoop().RunUntilIdle(); | |
284 } | |
285 | |
286 void StopImmediately(AndroidOverlay* overlay) { | |
287 stop_immediately_cb_.Run(overlay); | |
288 base::RunLoop().RunUntilIdle(); | |
289 } | |
290 | |
291 UseOverlayCB use_overlay_cb_; | |
292 UseSurfaceTextureCB use_surface_texture_cb_; | |
293 StopUsingOverlayImmediatelyCB stop_immediately_cb_; | |
294 | |
295 std::unique_ptr<AndroidOverlayFactory> factory_; | |
296 | |
297 base::WeakPtrFactory<FakeOverlayHelper> weak_factory_; | |
298 | |
299 private: | |
300 DISALLOW_COPY_AND_ASSIGN(FakeOverlayHelper); | |
301 }; | |
302 | |
303 // We pick this profile since it's always supported. | |
304 AndroidVideoDecodeAcceleratorTest() : config_(H264PROFILE_BASELINE) {} | |
305 | |
82 ~AndroidVideoDecodeAcceleratorTest() override {} | 306 ~AndroidVideoDecodeAcceleratorTest() override {} |
83 | 307 |
84 void SetUp() override { | 308 void SetUp() override { |
85 JNIEnv* env = base::android::AttachCurrentThread(); | 309 JNIEnv* env = base::android::AttachCurrentThread(); |
86 RegisterJni(env); | 310 RegisterJni(env); |
87 | 311 |
312 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
313 | |
88 gl::init::ShutdownGL(); | 314 gl::init::ShutdownGL(); |
89 ASSERT_TRUE(gl::init::InitializeGLOneOff()); | 315 ASSERT_TRUE(gl::init::InitializeGLOneOff()); |
90 surface_ = gl::init::CreateOffscreenGLSurface(gfx::Size(16, 16)); | 316 surface_ = gl::init::CreateOffscreenGLSurface(gfx::Size(16, 16)); |
91 context_ = gl::init::CreateGLContext(nullptr, surface_.get(), | 317 context_ = gl::init::CreateGLContext(nullptr, surface_.get(), |
92 gl::GLContextAttribs()); | 318 gl::GLContextAttribs()); |
93 context_->MakeCurrent(surface_.get()); | 319 context_->MakeCurrent(surface_.get()); |
94 | 320 |
95 vda_.reset(new AndroidVideoDecodeAccelerator( | 321 helper_that_is_usually_null_ = base::MakeUnique<FakeOverlayHelper>(); |
96 &codec_allocator_, base::Bind(&MakeContextCurrent), | 322 helper_ = helper_that_is_usually_null_->GetWeakPtrForTesting(); |
97 base::Bind(&GetGLES2Decoder, gl_decoder_.AsWeakPtr()))); | 323 |
324 platform_config_.sdk_int = base::android::SDK_VERSION_MARSHMALLOW; | |
325 platform_config_.allow_setsurface = true; | |
326 platform_config_.force_deferred_surface_creation = false; | |
327 | |
328 // By default, allow deferred init. | |
329 config_.is_deferred_initialization_allowed = true; | |
330 } | |
331 | |
332 // Create and initialize AVDA with |config_|, and return the result. | |
333 bool InitializeAVDA() { | |
334 // Because VDA has a custom deleter, we must assign it to |vda_| carefully. | |
335 AndroidVideoDecodeAccelerator* avda = new AndroidVideoDecodeAccelerator( | |
336 &codec_allocator_, std::move(helper_that_is_usually_null_), | |
337 base::Bind(&MakeContextCurrent), | |
338 base::Bind(&GetGLES2Decoder, gl_decoder_.AsWeakPtr()), | |
339 platform_config_); | |
340 vda_.reset(avda); | |
341 | |
342 bool result = vda_->Initialize(config_, &client_); | |
343 base::RunLoop().RunUntilIdle(); | |
344 return result; | |
345 } | |
346 | |
347 // Initialize |vda_|, providing a new surface for it. You may get the surface | |
348 // by asking |codec_allocator_|. | |
349 void InitializeAVDAWithOverlay() { | |
350 config_.surface_id = 123; | |
351 ASSERT_TRUE(InitializeAVDA()); | |
352 base::RunLoop().RunUntilIdle(); | |
353 ASSERT_TRUE(helper_->factory_ != nullptr); | |
354 | |
355 // Have the factory provide an overlay, and verify that codec creation is | |
356 // provided with that overlay. | |
357 std::unique_ptr<MockAndroidOverlay> overlay = | |
358 base::MakeUnique<MockAndroidOverlay>(); | |
359 // Set the expectations first, since ProvideOverlay might cause callbacks. | |
360 EXPECT_CALL(codec_allocator_, | |
361 MockCreateMediaCodecAsync(overlay.get(), nullptr)); | |
362 helper_->ProvideOverlay(std::move(overlay)); | |
363 | |
364 // Provide the codec so that we can check if it's freed properly. | |
365 EXPECT_CALL(client_, NotifyInitializationComplete(true)); | |
366 codec_allocator_.ProvideMockCodecAsync(); | |
367 base::RunLoop().RunUntilIdle(); | |
368 } | |
369 | |
370 void InitializeAVDAWithSurfaceTexture() { | |
371 ASSERT_TRUE(InitializeAVDA()); | |
372 base::RunLoop().RunUntilIdle(); | |
373 // We do not expect a factory, since we are using SurfaceTexture. | |
374 ASSERT_TRUE(helper_->factory_ == nullptr); | |
375 | |
376 // Set the expectations first, since ProvideOverlay might cause callbacks. | |
377 EXPECT_CALL(codec_allocator_, | |
378 MockCreateMediaCodecAsync(nullptr, NotNull())); | |
379 helper_->ProvideSurfaceTexture(); | |
380 | |
381 // Provide the codec so that we can check if it's freed properly. | |
382 EXPECT_CALL(client_, NotifyInitializationComplete(true)); | |
383 codec_allocator_.ProvideMockCodecAsync(); | |
384 base::RunLoop().RunUntilIdle(); | |
385 } | |
386 | |
387 // Set whether HasUnrendereredPictureBuffers will return true or false. | |
388 // TODO(liberato): We can't actually do this yet. It turns out to be okay, | |
389 // because AVDA doesn't actually SetSurface before DequeueOutput. It could do | |
390 // so, though, if there aren't unrendered buffers. Should AVDA ever start | |
391 // switching surfaces immediately upon receiving them, rather than waiting for | |
392 // DequeueOutput, then we'll want to be able to indicate that it has | |
393 // unrendered pictures to prevent that behavior. | |
394 void SetHasUnrenderedPictureBuffers(bool flag) {} | |
395 | |
396 // Tell |avda_| to switch surfaces to its incoming surface. This is a method | |
397 // since we're a friend of AVDA, and the tests are subclasses. It's also | |
398 // somewhat hacky, but much less hacky than trying to run it via a timer. | |
399 void LetAVDAUpdateSurface() { | |
400 SetHasUnrenderedPictureBuffers(false); | |
401 avda()->DequeueOutput(); | |
98 } | 402 } |
99 | 403 |
100 base::MessageLoop message_loop_; | 404 base::MessageLoop message_loop_; |
101 scoped_refptr<gl::GLSurface> surface_; | 405 scoped_refptr<gl::GLSurface> surface_; |
102 scoped_refptr<gl::GLContext> context_; | 406 scoped_refptr<gl::GLContext> context_; |
407 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
103 NiceMock<gpu::gles2::MockGLES2Decoder> gl_decoder_; | 408 NiceMock<gpu::gles2::MockGLES2Decoder> gl_decoder_; |
104 NiceMock<MockVDAClient> client_; | 409 NiceMock<MockVDAClient> client_; |
105 NiceMock<MockCodecAllocator> codec_allocator_; | 410 FakeCodecAllocator codec_allocator_; |
411 VideoDecodeAccelerator::Config config_; | |
412 | |
413 AndroidVideoDecodeAccelerator::PlatformConfig platform_config_; | |
414 | |
415 // We maintain a weak ref to this since AVDA owns it. | |
416 base::WeakPtr<FakeOverlayHelper> helper_; | |
106 | 417 |
107 // This must be a unique pointer to a VDA, not an AVDA, to ensure the | 418 // This must be a unique pointer to a VDA, not an AVDA, to ensure the |
108 // the default_delete specialization that calls Destroy() will be used. | 419 // the default_delete specialization that calls Destroy() will be used. |
109 std::unique_ptr<VideoDecodeAccelerator> vda_; | 420 std::unique_ptr<VideoDecodeAccelerator> vda_; |
421 | |
422 AndroidVideoDecodeAccelerator* avda() { | |
423 return reinterpret_cast<AndroidVideoDecodeAccelerator*>(vda_.get()); | |
424 } | |
425 | |
426 private: | |
427 // This is the object that |helper_| points to, or nullptr once we assign | |
428 // ownership to AVDA. | |
429 std::unique_ptr<FakeOverlayHelper> helper_that_is_usually_null_; | |
110 }; | 430 }; |
111 | 431 |
112 TEST_F(AndroidVideoDecodeAcceleratorTest, ConfigureUnsupportedCodec) { | 432 TEST_F(AndroidVideoDecodeAcceleratorTest, ConfigureUnsupportedCodec) { |
113 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | 433 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); |
114 | 434 |
115 ASSERT_FALSE(vda_->Initialize( | 435 config_ = VideoDecodeAccelerator::Config(VIDEO_CODEC_PROFILE_UNKNOWN); |
116 VideoDecodeAccelerator::Config(VIDEO_CODEC_PROFILE_UNKNOWN), &client_)); | 436 ASSERT_FALSE(InitializeAVDA()); |
117 } | 437 } |
118 | 438 |
119 TEST_F(AndroidVideoDecodeAcceleratorTest, ConfigureSupportedCodec) { | 439 TEST_F(AndroidVideoDecodeAcceleratorTest, |
120 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | 440 ConfigureSupportedCodecSynchronously) { |
121 | 441 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); |
122 // H264 is always supported by AVDA. | 442 |
123 ASSERT_TRUE(vda_->Initialize( | 443 config_.is_deferred_initialization_allowed = false; |
124 VideoDecodeAccelerator::Config(H264PROFILE_BASELINE), &client_)); | 444 |
125 } | 445 EXPECT_CALL(codec_allocator_, MockCreateMediaCodecSync(_, _)); |
126 | 446 ASSERT_TRUE(InitializeAVDA()); |
127 TEST_F(AndroidVideoDecodeAcceleratorTest, FailingToCreateACodecIsAnError) { | 447 } |
128 EXPECT_CALL(codec_allocator_, CreateMediaCodecAsync(_, _)) | 448 |
129 .WillOnce(PostNullCodec()); | 449 TEST_F(AndroidVideoDecodeAcceleratorTest, FailingToCreateACodecSyncIsAnError) { |
450 // Failuew to create a codec during sync init should cause Initialize to fail. | |
451 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
452 | |
453 config_.is_deferred_initialization_allowed = false; | |
454 codec_allocator_.allow_sync_creation = false; | |
455 | |
456 EXPECT_CALL(codec_allocator_, MockCreateMediaCodecSync(nullptr, NotNull())); | |
457 ASSERT_FALSE(InitializeAVDA()); | |
458 } | |
459 | |
460 TEST_F(AndroidVideoDecodeAcceleratorTest, FailingToCreateACodecAsyncIsAnError) { | |
461 // Verify that a null codec signals error for async init when it doesn't get a | |
462 // mediacodec instance. | |
463 // | |
464 // Also assert that there's only one call to CreateMediaCodecAsync. And since | |
465 // it replies with a null codec, AVDA will be in an error state when it shuts | |
466 // down. Since we know that it's constructed before we destroy the VDA, we | |
467 // verify that AVDA doens't create codecs during destruction. | |
468 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
469 | |
470 // Note that if we somehow end up deferring surface creation, then this would | |
471 // no longer be expected to fail. It would signal success before asking for a | |
472 // surface or codec. | |
473 EXPECT_CALL(codec_allocator_, MockCreateMediaCodecAsync(_, NotNull())); | |
130 EXPECT_CALL(client_, NotifyInitializationComplete(false)); | 474 EXPECT_CALL(client_, NotifyInitializationComplete(false)); |
131 | 475 |
132 VideoDecodeAccelerator::Config config(H264PROFILE_BASELINE); | 476 ASSERT_TRUE(InitializeAVDA()); |
133 config.is_deferred_initialization_allowed = true; | 477 helper_->ProvideSurfaceTexture(); |
134 vda_->Initialize(config, &client_); | 478 codec_allocator_.ProvideNullCodecAsync(); |
135 base::RunLoop().RunUntilIdle(); | 479 |
136 } | 480 // Make sure that codec allocation has happened before destroying the VDA. |
137 | 481 testing::Mock::VerifyAndClearExpectations(&codec_allocator_); |
138 TEST_F(AndroidVideoDecodeAcceleratorTest, NoCodecsAreCreatedDuringDestruction) { | 482 } |
139 // Assert that there's only one call to CreateMediaCodecAsync. And since it | 483 |
140 // replies with a null codec, AVDA will be in an error state when it shuts | 484 TEST_F(AndroidVideoDecodeAcceleratorTest, |
141 // down. | 485 LowEndDevicesSucceedInitWithoutASurface) { |
142 EXPECT_CALL(codec_allocator_, CreateMediaCodecAsync(_, _)) | 486 // If AVDA decides that we should defer surface creation, then it should |
143 .WillOnce(PostNullCodec()); | 487 // signal success before we provide a surface. It should still ask for a |
144 | 488 // surface, though. |
145 VideoDecodeAccelerator::Config config(H264PROFILE_BASELINE); | 489 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); |
146 config.is_deferred_initialization_allowed = true; | 490 |
147 vda_->Initialize(config, &client_); | 491 // It would be nicer if we didn't just force this on, since we might do so |
148 base::RunLoop().RunUntilIdle(); | 492 // in a state that AVDA isn't supposed to handle (e.g., if we give it a |
149 } | 493 // surface, then it would never decide to defer surface creation). |
150 | 494 platform_config_.force_deferred_surface_creation = true; |
495 config_.surface_id = SurfaceManager::kNoSurfaceID; | |
496 | |
497 EXPECT_CALL(*helper_, MockInitialize()).Times(0); | |
498 EXPECT_CALL(client_, NotifyInitializationComplete(true)); | |
499 | |
500 InitializeAVDA(); | |
501 } | |
502 | |
503 TEST_F(AndroidVideoDecodeAcceleratorTest, | |
504 AsyncInitWithSurfaceTextureAndDelete) { | |
505 // When configuring with a SurfaceTexture and deferred init, we should be | |
506 // asked for a codec, and be notified of init success if we provide one. When | |
507 // AVDA is destroyed, it should release the codec and surface texture. | |
508 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
509 | |
510 InitializeAVDAWithSurfaceTexture(); | |
511 | |
512 // Delete the VDA, and make sure that it tries to free the codec and the right | |
513 // surface texture. | |
514 EXPECT_CALL( | |
515 codec_allocator_, | |
516 MockReleaseMediaCodec(codec_allocator_.most_recent_codec(), | |
517 codec_allocator_.most_recent_overlay(), | |
518 codec_allocator_.most_recent_surface_texture())); | |
519 codec_allocator_.codec_destruction_observer()->ExpectDestruction(); | |
520 vda_ = nullptr; | |
521 base::RunLoop().RunUntilIdle(); | |
522 } | |
523 | |
524 TEST_F(AndroidVideoDecodeAcceleratorTest, AsyncInitWithSurfaceAndDelete) { | |
525 // When |config_| specifies a surface, we should be given a factory during | |
526 // startup for it. When |helper_| provides an overlay, the codec should be | |
527 // allocated using it. Shutdown should provide the overlay when releasing the | |
528 // media codec. | |
529 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
530 | |
531 InitializeAVDAWithOverlay(); | |
532 | |
533 // Delete the VDA, and make sure that it tries to free the codec and the | |
534 // overlay that it provided to us. | |
535 EXPECT_CALL( | |
536 codec_allocator_, | |
537 MockReleaseMediaCodec(codec_allocator_.most_recent_codec(), | |
538 codec_allocator_.most_recent_overlay(), | |
539 codec_allocator_.most_recent_surface_texture())); | |
540 codec_allocator_.codec_destruction_observer()->ExpectDestruction(); | |
541 vda_ = nullptr; | |
542 base::RunLoop().RunUntilIdle(); | |
543 } | |
544 | |
545 TEST_F(AndroidVideoDecodeAcceleratorTest, | |
546 SwitchesToSurfaceTextureWhenSurfaceDestroyed) { | |
547 // Provide a surface, and a codec, then destroy the surface. AVDA should use | |
548 // SetSurface to switch to SurfaceTexture. | |
549 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
550 | |
551 InitializeAVDAWithOverlay(); | |
552 | |
553 // It would be nice if we knew that this was a surface texture. As it is, we | |
554 // just destroy the VDA and expect that we're provided with one. Hopefully, | |
555 // AVDA is actually calling SetSurface properly. | |
556 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)) | |
557 .WillOnce(Return(true)); | |
558 codec_allocator_.codec_destruction_observer()->DestructionIsOptional(); | |
559 helper_->StopImmediately(codec_allocator_.most_recent_overlay()); | |
560 | |
561 EXPECT_CALL(codec_allocator_, | |
562 MockReleaseMediaCodec(codec_allocator_.most_recent_codec(), | |
563 nullptr, NotNull())); | |
564 vda_ = nullptr; | |
565 base::RunLoop().RunUntilIdle(); | |
566 } | |
567 | |
568 TEST_F(AndroidVideoDecodeAcceleratorTest, SwitchesToSurfaceTextureEventually) { | |
569 // Provide a surface, and a codec, then request that AVDA switches to a | |
570 // surface texture. Verify that it does. | |
571 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
572 | |
573 InitializeAVDAWithOverlay(); | |
574 | |
575 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)) | |
576 .WillOnce(Return(true)); | |
577 | |
578 // Note that it's okay if |avda_| switches before ProvideSurfaceTexture | |
579 // returns, since it has no queued output anyway. | |
580 helper_->ProvideSurfaceTexture(); | |
581 LetAVDAUpdateSurface(); | |
582 | |
583 // Verify that we're now using some surface texture. | |
584 EXPECT_CALL(codec_allocator_, | |
585 MockReleaseMediaCodec(codec_allocator_.most_recent_codec(), | |
586 nullptr, NotNull())); | |
587 codec_allocator_.codec_destruction_observer()->ExpectDestruction(); | |
588 vda_ = nullptr; | |
589 base::RunLoop().RunUntilIdle(); | |
590 } | |
591 | |
592 TEST_F(AndroidVideoDecodeAcceleratorTest, | |
593 SetSurfaceFailureDoesntSwitchSurfaces) { | |
594 // Initialize AVDA with a surface, then request that AVDA switches to a | |
595 // surface texture. When it tries to UpdateSurface, pretend to fail. AVDA | |
596 // should notify error, and also release the original surface. | |
597 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
598 | |
599 InitializeAVDAWithOverlay(); | |
600 | |
601 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)) | |
602 .WillOnce(Return(false)); | |
603 EXPECT_CALL(client_, | |
604 NotifyError(AndroidVideoDecodeAccelerator::PLATFORM_FAILURE)) | |
605 .Times(1); | |
606 codec_allocator_.codec_destruction_observer()->DestructionIsOptional(); | |
607 helper_->ProvideSurfaceTexture(); | |
608 LetAVDAUpdateSurface(); | |
609 } | |
610 | |
611 TEST_F(AndroidVideoDecodeAcceleratorTest, | |
612 SwitchToSurfaceAndBackBeforeSetSurface) { | |
613 // Ask AVDA to switch from ST to overlay, then back to ST before it has a | |
614 // chance to do the first switch. It should simply drop the overlay. | |
615 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
616 | |
617 InitializeAVDAWithSurfaceTexture(); | |
618 | |
619 // Don't let AVDA switch immediately, else it could choose to SetSurface when | |
620 // it first gets the overlay. | |
621 SetHasUnrenderedPictureBuffers(true); | |
622 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)).Times(0); | |
623 std::unique_ptr<MockAndroidOverlay> overlay = | |
624 base::MakeUnique<MockAndroidOverlay>(); | |
625 // Make sure that the overlay is not destroyed too soon. | |
626 std::unique_ptr<DestructionObservable::DestructionObserver> observer = | |
627 overlay->CreateDestructionObserver(); | |
628 observer->DoNotAllowDestruction(); | |
629 | |
630 helper_->ProvideOverlay(std::move(overlay)); | |
631 | |
632 // Now it is expected to drop the overlay. | |
633 observer->ExpectDestruction(); | |
634 | |
635 // While the incoming surface is pending, switch back to SurfaceTexture. | |
636 helper_->ProvideSurfaceTexture(); | |
637 } | |
638 | |
639 TEST_F(AndroidVideoDecodeAcceleratorTest, | |
640 ChangingOutputSurfaceVoluntarilyWithoutSetSurfaceIsIgnored) { | |
641 // If we ask AVDA to change to SurfaceTexture should be ignored on platforms | |
642 // that don't support SetSurface (pre-M or blacklisted). It should also | |
643 // ignore SurfaceTexture => overlay, but we don't check that. | |
644 // | |
645 // Also note that there are other probably reasonable things to do (like | |
646 // signal an error), but we want to be sure that it doesn't try to SetSurface. | |
647 // We also want to be sure that, if it doesn't signal an error, that it also | |
648 // doesn't get confused about which surface is in use. So, we assume that it | |
649 // doesn't signal an error, and we check that it releases the right surface | |
650 // with the codec. | |
651 EXPECT_CALL(client_, NotifyError(_)).Times(0); | |
652 | |
653 platform_config_.allow_setsurface = false; | |
654 InitializeAVDAWithOverlay(); | |
655 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)).Times(0); | |
656 | |
657 // This should not switch to SurfaceTexture. | |
658 helper_->ProvideSurfaceTexture(); | |
659 LetAVDAUpdateSurface(); | |
660 } | |
661 | |
662 TEST_F(AndroidVideoDecodeAcceleratorTest, | |
663 OnSurfaceDestroyedWithoutSetSurfaceFreesTheCodec) { | |
664 // If AVDA receives OnSurfaceDestroyed without support for SetSurface, then it | |
665 // should free the codec. | |
666 platform_config_.allow_setsurface = false; | |
667 InitializeAVDAWithOverlay(); | |
668 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)).Times(0); | |
669 | |
670 // This should free the codec. | |
671 EXPECT_CALL( | |
672 codec_allocator_, | |
673 MockReleaseMediaCodec(codec_allocator_.most_recent_codec(), | |
674 codec_allocator_.most_recent_overlay(), nullptr)); | |
675 codec_allocator_.codec_destruction_observer()->ExpectDestruction(); | |
676 helper_->StopImmediately(codec_allocator_.most_recent_overlay()); | |
677 base::RunLoop().RunUntilIdle(); | |
678 | |
679 // Verify that the codec has been released, since |vda_| will be destroyed | |
680 // soon. The expectations must be met before that. | |
681 testing::Mock::VerifyAndClearExpectations(&codec_allocator_); | |
682 codec_allocator_.codec_destruction_observer()->DestructionIsOptional(); | |
683 } | |
684 | |
685 TEST_F(AndroidVideoDecodeAcceleratorTest, | |
686 MultipleSurfaceTextureCallbacksAreIgnored) { | |
687 // Ask AVDA to switch to ST when it's already using ST, nothing should happen. | |
688 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | |
689 | |
690 InitializeAVDAWithSurfaceTexture(); | |
691 | |
692 // This should do nothing. | |
693 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)).Times(0); | |
694 helper_->ProvideSurfaceTexture(); | |
695 | |
696 base::RunLoop().RunUntilIdle(); | |
697 } | |
698 | |
151 } // namespace media | 699 } // namespace media |
OLD | NEW |