| 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 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 227 |
| 228 base::WeakPtr<AVDACodecAllocatorClient> client_; | 228 base::WeakPtr<AVDACodecAllocatorClient> client_; |
| 229 scoped_refptr<CodecConfig> config_; | 229 scoped_refptr<CodecConfig> config_; |
| 230 | 230 |
| 231 DISALLOW_COPY_AND_ASSIGN(FakeCodecAllocator); | 231 DISALLOW_COPY_AND_ASSIGN(FakeCodecAllocator); |
| 232 }; | 232 }; |
| 233 | 233 |
| 234 // Surface chooser that calls mocked functions to allow counting calls, but | 234 // Surface chooser that calls mocked functions to allow counting calls, but |
| 235 // also records arguments. Note that gmock can't mock out unique_ptrs | 235 // also records arguments. Note that gmock can't mock out unique_ptrs |
| 236 // anyway, so we can't mock AndroidVideoSurfaceChooser directly. | 236 // anyway, so we can't mock AndroidVideoSurfaceChooser directly. |
| 237 class FakeOverlayHelper : public NiceMock<AndroidVideoSurfaceChooser> { | 237 class FakeOverlayChooser : public NiceMock<AndroidVideoSurfaceChooser> { |
| 238 public: | 238 public: |
| 239 FakeOverlayHelper() : weak_factory_(this) {} | 239 FakeOverlayChooser() : weak_factory_(this) {} |
| 240 | 240 |
| 241 // These are called by the real functions. You may set expectations on | 241 // These are called by the real functions. You may set expectations on |
| 242 // them if you like. | 242 // them if you like. |
| 243 MOCK_METHOD0(MockInitialize, void()); | 243 MOCK_METHOD0(MockInitialize, void()); |
| 244 MOCK_METHOD0(MockReplaceOverlayFactory, void()); | 244 MOCK_METHOD0(MockReplaceOverlayFactory, void()); |
| 245 | 245 |
| 246 // We guarantee that we'll only clear this during destruction, so that you | 246 // We guarantee that we'll only clear this during destruction, so that you |
| 247 // may treat it as "pointer that lasts as long as |this| does". | 247 // may treat it as "pointer that lasts as long as |this| does". |
| 248 base::WeakPtr<FakeOverlayHelper> GetWeakPtrForTesting() { | 248 base::WeakPtr<FakeOverlayChooser> GetWeakPtrForTesting() { |
| 249 return weak_factory_.GetWeakPtr(); | 249 return weak_factory_.GetWeakPtr(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 void Initialize( | 252 void Initialize(UseOverlayCB use_overlay_cb, |
| 253 UseOverlayCB use_overlay_cb, | 253 UseSurfaceTextureCB use_surface_texture_cb, |
| 254 UseSurfaceTextureCB use_surface_texture_cb, | 254 StopUsingOverlayImmediatelyCB stop_immediately_cb, |
| 255 StopUsingOverlayImmediatelyCB stop_immediately_cb, | 255 AndroidOverlayFactoryCB initial_factory) override { |
| 256 std::unique_ptr<AndroidOverlayFactory> initial_factory) override { | |
| 257 MockInitialize(); | 256 MockInitialize(); |
| 258 | 257 |
| 259 factory_ = std::move(initial_factory); | 258 factory_ = std::move(initial_factory); |
| 260 use_overlay_cb_ = std::move(use_overlay_cb); | 259 use_overlay_cb_ = std::move(use_overlay_cb); |
| 261 use_surface_texture_cb_ = std::move(use_surface_texture_cb); | 260 use_surface_texture_cb_ = std::move(use_surface_texture_cb); |
| 262 stop_immediately_cb_ = std::move(stop_immediately_cb); | 261 stop_immediately_cb_ = std::move(stop_immediately_cb); |
| 263 } | 262 } |
| 264 | 263 |
| 265 void ReplaceOverlayFactory( | 264 void ReplaceOverlayFactory(AndroidOverlayFactoryCB factory) override { |
| 266 std::unique_ptr<AndroidOverlayFactory> factory) override { | |
| 267 MockReplaceOverlayFactory(); | 265 MockReplaceOverlayFactory(); |
| 268 factory_ = std::move(factory); | 266 factory_ = std::move(factory); |
| 269 } | 267 } |
| 270 | 268 |
| 271 // Notify AVDA to use a surface texture. | 269 // Notify AVDA to use a surface texture. |
| 272 void ProvideSurfaceTexture() { | 270 void ProvideSurfaceTexture() { |
| 273 use_surface_texture_cb_.Run(); | 271 use_surface_texture_cb_.Run(); |
| 274 base::RunLoop().RunUntilIdle(); | 272 base::RunLoop().RunUntilIdle(); |
| 275 } | 273 } |
| 276 | 274 |
| 277 void ProvideOverlay(std::unique_ptr<AndroidOverlay> overlay) { | 275 void ProvideOverlay(std::unique_ptr<AndroidOverlay> overlay) { |
| 278 use_overlay_cb_.Run(std::move(overlay)); | 276 use_overlay_cb_.Run(std::move(overlay)); |
| 279 base::RunLoop().RunUntilIdle(); | 277 base::RunLoop().RunUntilIdle(); |
| 280 } | 278 } |
| 281 | 279 |
| 282 void StopImmediately(AndroidOverlay* overlay) { | 280 void StopImmediately(AndroidOverlay* overlay) { |
| 283 stop_immediately_cb_.Run(overlay); | 281 stop_immediately_cb_.Run(overlay); |
| 284 base::RunLoop().RunUntilIdle(); | 282 base::RunLoop().RunUntilIdle(); |
| 285 } | 283 } |
| 286 | 284 |
| 287 UseOverlayCB use_overlay_cb_; | 285 UseOverlayCB use_overlay_cb_; |
| 288 UseSurfaceTextureCB use_surface_texture_cb_; | 286 UseSurfaceTextureCB use_surface_texture_cb_; |
| 289 StopUsingOverlayImmediatelyCB stop_immediately_cb_; | 287 StopUsingOverlayImmediatelyCB stop_immediately_cb_; |
| 290 | 288 |
| 291 std::unique_ptr<AndroidOverlayFactory> factory_; | 289 AndroidOverlayFactoryCB factory_; |
| 292 | 290 |
| 293 base::WeakPtrFactory<FakeOverlayHelper> weak_factory_; | 291 base::WeakPtrFactory<FakeOverlayChooser> weak_factory_; |
| 294 | 292 |
| 295 private: | 293 private: |
| 296 DISALLOW_COPY_AND_ASSIGN(FakeOverlayHelper); | 294 DISALLOW_COPY_AND_ASSIGN(FakeOverlayChooser); |
| 297 }; | 295 }; |
| 298 | 296 |
| 299 } // namespace | 297 } // namespace |
| 300 | 298 |
| 301 class AndroidVideoDecodeAcceleratorTest : public testing::Test { | 299 class AndroidVideoDecodeAcceleratorTest : public testing::Test { |
| 302 public: | 300 public: |
| 303 // We pick this profile since it's always supported. | 301 // We pick this profile since it's always supported. |
| 304 AndroidVideoDecodeAcceleratorTest() : config_(H264PROFILE_BASELINE) {} | 302 AndroidVideoDecodeAcceleratorTest() : config_(H264PROFILE_BASELINE) {} |
| 305 | 303 |
| 306 ~AndroidVideoDecodeAcceleratorTest() override {} | 304 ~AndroidVideoDecodeAcceleratorTest() override {} |
| 307 | 305 |
| 308 void SetUp() override { | 306 void SetUp() override { |
| 309 JNIEnv* env = base::android::AttachCurrentThread(); | 307 JNIEnv* env = base::android::AttachCurrentThread(); |
| 310 RegisterJni(env); | 308 RegisterJni(env); |
| 311 | 309 |
| 312 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 310 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 313 | 311 |
| 314 gl::init::ShutdownGL(); | 312 gl::init::ShutdownGL(); |
| 315 ASSERT_TRUE(gl::init::InitializeGLOneOff()); | 313 ASSERT_TRUE(gl::init::InitializeGLOneOff()); |
| 316 surface_ = gl::init::CreateOffscreenGLSurface(gfx::Size(16, 16)); | 314 surface_ = gl::init::CreateOffscreenGLSurface(gfx::Size(16, 16)); |
| 317 context_ = gl::init::CreateGLContext(nullptr, surface_.get(), | 315 context_ = gl::init::CreateGLContext(nullptr, surface_.get(), |
| 318 gl::GLContextAttribs()); | 316 gl::GLContextAttribs()); |
| 319 context_->MakeCurrent(surface_.get()); | 317 context_->MakeCurrent(surface_.get()); |
| 320 | 318 |
| 321 chooser_that_is_usually_null_ = base::MakeUnique<FakeOverlayHelper>(); | 319 chooser_that_is_usually_null_ = base::MakeUnique<FakeOverlayChooser>(); |
| 322 chooser_ = chooser_that_is_usually_null_->GetWeakPtrForTesting(); | 320 chooser_ = chooser_that_is_usually_null_->GetWeakPtrForTesting(); |
| 323 | 321 |
| 324 platform_config_.sdk_int = base::android::SDK_VERSION_MARSHMALLOW; | 322 platform_config_.sdk_int = base::android::SDK_VERSION_MARSHMALLOW; |
| 325 platform_config_.allow_setsurface = true; | 323 platform_config_.allow_setsurface = true; |
| 326 platform_config_.force_deferred_surface_creation = false; | 324 platform_config_.force_deferred_surface_creation = false; |
| 327 | 325 |
| 328 // By default, allow deferred init. | 326 // By default, allow deferred init. |
| 329 config_.is_deferred_initialization_allowed = true; | 327 config_.is_deferred_initialization_allowed = true; |
| 330 } | 328 } |
| 331 | 329 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 343 base::RunLoop().RunUntilIdle(); | 341 base::RunLoop().RunUntilIdle(); |
| 344 return result; | 342 return result; |
| 345 } | 343 } |
| 346 | 344 |
| 347 // Initialize |vda_|, providing a new surface for it. You may get the surface | 345 // Initialize |vda_|, providing a new surface for it. You may get the surface |
| 348 // by asking |codec_allocator_|. | 346 // by asking |codec_allocator_|. |
| 349 void InitializeAVDAWithOverlay() { | 347 void InitializeAVDAWithOverlay() { |
| 350 config_.surface_id = 123; | 348 config_.surface_id = 123; |
| 351 ASSERT_TRUE(InitializeAVDA()); | 349 ASSERT_TRUE(InitializeAVDA()); |
| 352 base::RunLoop().RunUntilIdle(); | 350 base::RunLoop().RunUntilIdle(); |
| 353 ASSERT_TRUE(chooser_->factory_ != nullptr); | 351 ASSERT_TRUE(chooser_->factory_); |
| 354 | 352 |
| 355 // Have the factory provide an overlay, and verify that codec creation is | 353 // Have the factory provide an overlay, and verify that codec creation is |
| 356 // provided with that overlay. | 354 // provided with that overlay. |
| 357 std::unique_ptr<MockAndroidOverlay> overlay = | 355 std::unique_ptr<MockAndroidOverlay> overlay = |
| 358 base::MakeUnique<MockAndroidOverlay>(); | 356 base::MakeUnique<MockAndroidOverlay>(); |
| 359 // Set the expectations first, since ProvideOverlay might cause callbacks. | 357 // Set the expectations first, since ProvideOverlay might cause callbacks. |
| 360 EXPECT_CALL(codec_allocator_, | 358 EXPECT_CALL(codec_allocator_, |
| 361 MockCreateMediaCodecAsync(overlay.get(), nullptr)); | 359 MockCreateMediaCodecAsync(overlay.get(), nullptr)); |
| 362 chooser_->ProvideOverlay(std::move(overlay)); | 360 chooser_->ProvideOverlay(std::move(overlay)); |
| 363 | 361 |
| 364 // Provide the codec so that we can check if it's freed properly. | 362 // Provide the codec so that we can check if it's freed properly. |
| 365 EXPECT_CALL(client_, NotifyInitializationComplete(true)); | 363 EXPECT_CALL(client_, NotifyInitializationComplete(true)); |
| 366 codec_allocator_.ProvideMockCodecAsync(); | 364 codec_allocator_.ProvideMockCodecAsync(); |
| 367 base::RunLoop().RunUntilIdle(); | 365 base::RunLoop().RunUntilIdle(); |
| 368 } | 366 } |
| 369 | 367 |
| 370 void InitializeAVDAWithSurfaceTexture() { | 368 void InitializeAVDAWithSurfaceTexture() { |
| 371 ASSERT_TRUE(InitializeAVDA()); | 369 ASSERT_TRUE(InitializeAVDA()); |
| 372 base::RunLoop().RunUntilIdle(); | 370 base::RunLoop().RunUntilIdle(); |
| 373 // We do not expect a factory, since we are using SurfaceTexture. | 371 // We do not expect a factory, since we are using SurfaceTexture. |
| 374 ASSERT_TRUE(chooser_->factory_ == nullptr); | 372 ASSERT_FALSE(chooser_->factory_); |
| 375 | 373 |
| 376 // Set the expectations first, since ProvideOverlay might cause callbacks. | 374 // Set the expectations first, since ProvideOverlay might cause callbacks. |
| 377 EXPECT_CALL(codec_allocator_, | 375 EXPECT_CALL(codec_allocator_, |
| 378 MockCreateMediaCodecAsync(nullptr, NotNull())); | 376 MockCreateMediaCodecAsync(nullptr, NotNull())); |
| 379 chooser_->ProvideSurfaceTexture(); | 377 chooser_->ProvideSurfaceTexture(); |
| 380 | 378 |
| 381 // Provide the codec so that we can check if it's freed properly. | 379 // Provide the codec so that we can check if it's freed properly. |
| 382 EXPECT_CALL(client_, NotifyInitializationComplete(true)); | 380 EXPECT_CALL(client_, NotifyInitializationComplete(true)); |
| 383 codec_allocator_.ProvideMockCodecAsync(); | 381 codec_allocator_.ProvideMockCodecAsync(); |
| 384 base::RunLoop().RunUntilIdle(); | 382 base::RunLoop().RunUntilIdle(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 406 scoped_refptr<gl::GLContext> context_; | 404 scoped_refptr<gl::GLContext> context_; |
| 407 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 405 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 408 NiceMock<gpu::gles2::MockGLES2Decoder> gl_decoder_; | 406 NiceMock<gpu::gles2::MockGLES2Decoder> gl_decoder_; |
| 409 NiceMock<MockVDAClient> client_; | 407 NiceMock<MockVDAClient> client_; |
| 410 FakeCodecAllocator codec_allocator_; | 408 FakeCodecAllocator codec_allocator_; |
| 411 VideoDecodeAccelerator::Config config_; | 409 VideoDecodeAccelerator::Config config_; |
| 412 | 410 |
| 413 AndroidVideoDecodeAccelerator::PlatformConfig platform_config_; | 411 AndroidVideoDecodeAccelerator::PlatformConfig platform_config_; |
| 414 | 412 |
| 415 // We maintain a weak ref to this since AVDA owns it. | 413 // We maintain a weak ref to this since AVDA owns it. |
| 416 base::WeakPtr<FakeOverlayHelper> chooser_; | 414 base::WeakPtr<FakeOverlayChooser> chooser_; |
| 417 | 415 |
| 418 // This must be a unique pointer to a VDA, not an AVDA, to ensure the | 416 // This must be a unique pointer to a VDA, not an AVDA, to ensure the |
| 419 // the default_delete specialization that calls Destroy() will be used. | 417 // the default_delete specialization that calls Destroy() will be used. |
| 420 std::unique_ptr<VideoDecodeAccelerator> vda_; | 418 std::unique_ptr<VideoDecodeAccelerator> vda_; |
| 421 | 419 |
| 422 AndroidVideoDecodeAccelerator* avda() { | 420 AndroidVideoDecodeAccelerator* avda() { |
| 423 return reinterpret_cast<AndroidVideoDecodeAccelerator*>(vda_.get()); | 421 return reinterpret_cast<AndroidVideoDecodeAccelerator*>(vda_.get()); |
| 424 } | 422 } |
| 425 | 423 |
| 426 private: | 424 private: |
| 427 // This is the object that |chooser_| points to, or nullptr once we assign | 425 // This is the object that |chooser_| points to, or nullptr once we assign |
| 428 // ownership to AVDA. | 426 // ownership to AVDA. |
| 429 std::unique_ptr<FakeOverlayHelper> chooser_that_is_usually_null_; | 427 std::unique_ptr<FakeOverlayChooser> chooser_that_is_usually_null_; |
| 430 }; | 428 }; |
| 431 | 429 |
| 432 TEST_F(AndroidVideoDecodeAcceleratorTest, ConfigureUnsupportedCodec) { | 430 TEST_F(AndroidVideoDecodeAcceleratorTest, ConfigureUnsupportedCodec) { |
| 433 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); | 431 SKIP_IF_MEDIACODEC_IS_NOT_AVAILABLE(); |
| 434 | 432 |
| 435 config_ = VideoDecodeAccelerator::Config(VIDEO_CODEC_PROFILE_UNKNOWN); | 433 config_ = VideoDecodeAccelerator::Config(VIDEO_CODEC_PROFILE_UNKNOWN); |
| 436 ASSERT_FALSE(InitializeAVDA()); | 434 ASSERT_FALSE(InitializeAVDA()); |
| 437 } | 435 } |
| 438 | 436 |
| 439 TEST_F(AndroidVideoDecodeAcceleratorTest, | 437 TEST_F(AndroidVideoDecodeAcceleratorTest, |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 InitializeAVDAWithSurfaceTexture(); | 688 InitializeAVDAWithSurfaceTexture(); |
| 691 | 689 |
| 692 // This should do nothing. | 690 // This should do nothing. |
| 693 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)).Times(0); | 691 EXPECT_CALL(*codec_allocator_.most_recent_codec(), SetSurface(_)).Times(0); |
| 694 chooser_->ProvideSurfaceTexture(); | 692 chooser_->ProvideSurfaceTexture(); |
| 695 | 693 |
| 696 base::RunLoop().RunUntilIdle(); | 694 base::RunLoop().RunUntilIdle(); |
| 697 } | 695 } |
| 698 | 696 |
| 699 } // namespace media | 697 } // namespace media |
| OLD | NEW |