OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ppapi/tests/test_audio.h" | 5 #include "ppapi/tests/test_audio.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "ppapi/c/ppb_audio_config.h" | 9 #include "ppapi/c/ppb_audio_config.h" |
10 #include "ppapi/c/ppb_audio.h" | 10 #include "ppapi/c/ppb_audio.h" |
11 #include "ppapi/cpp/module.h" | 11 #include "ppapi/cpp/module.h" |
12 #include "ppapi/tests/testing_instance.h" | 12 #include "ppapi/tests/testing_instance.h" |
13 #include "ppapi/tests/test_utils.h" | 13 #include "ppapi/tests/test_utils.h" |
14 | 14 |
| 15 #if defined(__native_client__) |
| 16 #include "native_client/src/untrusted/irt/irt.h" |
| 17 #include "ppapi/native_client/src/untrusted/irt_stub/thread_creator.h" |
| 18 #endif |
| 19 |
15 #define ARRAYSIZE_UNSAFE(a) \ | 20 #define ARRAYSIZE_UNSAFE(a) \ |
16 ((sizeof(a) / sizeof(*(a))) / \ | 21 ((sizeof(a) / sizeof(*(a))) / \ |
17 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) | 22 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) |
18 | 23 |
| 24 #if defined(__native_client__) |
| 25 namespace { |
| 26 |
| 27 void GetNaClIrtPpapiHook(struct nacl_irt_ppapihook* hooks) { |
| 28 nacl_interface_query(NACL_IRT_PPAPIHOOK_v0_1, hooks, sizeof(*hooks)); |
| 29 } |
| 30 |
| 31 struct PP_ThreadFunctions g_thread_funcs = {}; |
| 32 |
| 33 void ThreadFunctionsGetter(const struct PP_ThreadFunctions* thread_funcs) { |
| 34 g_thread_funcs = *thread_funcs; |
| 35 } |
| 36 |
| 37 // In order to check if the thread_create is called, CountingThreadCreate() |
| 38 // increments this variable. Callers can check if the function is actually |
| 39 // called by looking at this value. |
| 40 int g_num_thread_create_called = 0; |
| 41 int g_num_thread_join_called = 0; |
| 42 |
| 43 int CountingThreadCreate(uintptr_t* tid, |
| 44 void (*func)(void* thread_argument), |
| 45 void* thread_argument) { |
| 46 ++g_num_thread_create_called; |
| 47 return g_thread_funcs.thread_create(tid, func, thread_argument); |
| 48 } |
| 49 |
| 50 int CountingThreadJoin(uintptr_t tid) { |
| 51 ++g_num_thread_join_called; |
| 52 return g_thread_funcs.thread_join(tid); |
| 53 } |
| 54 |
| 55 // Sets NULL for PP_ThreadFunctions to emulate the situation that |
| 56 // ppapi_register_thread_creator() is not yet called. |
| 57 void SetNullThreadFunctions() { |
| 58 nacl_irt_ppapihook hooks; |
| 59 GetNaClIrtPpapiHook(&hooks); |
| 60 PP_ThreadFunctions thread_functions = {}; |
| 61 hooks.ppapi_register_thread_creator(&thread_functions); |
| 62 } |
| 63 |
| 64 void InjectCountingThreadFunctions() { |
| 65 // First of all, we extract the system default thread functions. |
| 66 // Internally, __nacl_register_thread_creator calls |
| 67 // hooks.ppapi_register_thread_creator with default PP_ThreadFunctions |
| 68 // instance. ThreadFunctionGetter stores it to g_thread_funcs. |
| 69 nacl_irt_ppapihook hooks = { NULL, ThreadFunctionsGetter }; |
| 70 __nacl_register_thread_creator(&hooks); |
| 71 |
| 72 // Here g_thread_funcs stores the thread functions. |
| 73 // Inject the CountingThreadCreate. |
| 74 PP_ThreadFunctions thread_functions = { |
| 75 CountingThreadCreate, |
| 76 CountingThreadJoin, |
| 77 }; |
| 78 GetNaClIrtPpapiHook(&hooks); |
| 79 hooks.ppapi_register_thread_creator(&thread_functions); |
| 80 } |
| 81 |
| 82 // Resets the PP_ThreadFunctions on exit from the scope. |
| 83 class ScopedThreadFunctionsResetter { |
| 84 public: |
| 85 ScopedThreadFunctionsResetter() {} |
| 86 ~ScopedThreadFunctionsResetter() { |
| 87 nacl_irt_ppapihook hooks; |
| 88 GetNaClIrtPpapiHook(&hooks); |
| 89 __nacl_register_thread_creator(&hooks); |
| 90 } |
| 91 }; |
| 92 |
| 93 } // namespace |
| 94 #endif // __native_client__ |
| 95 |
19 REGISTER_TEST_CASE(Audio); | 96 REGISTER_TEST_CASE(Audio); |
20 | 97 |
21 TestAudio::TestAudio(TestingInstance* instance) | 98 TestAudio::TestAudio(TestingInstance* instance) |
22 : TestCase(instance), | 99 : TestCase(instance), |
23 audio_callback_method_(NULL), | 100 audio_callback_method_(NULL), |
24 audio_callback_event_(instance->pp_instance()), | 101 audio_callback_event_(instance->pp_instance()), |
25 test_done_(false), | 102 test_done_(false), |
26 audio_interface_(NULL), | 103 audio_interface_(NULL), |
27 audio_interface_1_0_(NULL), | 104 audio_interface_1_0_(NULL), |
28 audio_config_interface_(NULL), | 105 audio_config_interface_(NULL), |
(...skipping 17 matching lines...) Expand all Loading... |
46 } | 123 } |
47 | 124 |
48 void TestAudio::RunTests(const std::string& filter) { | 125 void TestAudio::RunTests(const std::string& filter) { |
49 RUN_TEST(Creation, filter); | 126 RUN_TEST(Creation, filter); |
50 RUN_TEST(DestroyNoStop, filter); | 127 RUN_TEST(DestroyNoStop, filter); |
51 RUN_TEST(Failures, filter); | 128 RUN_TEST(Failures, filter); |
52 RUN_TEST(AudioCallback1, filter); | 129 RUN_TEST(AudioCallback1, filter); |
53 RUN_TEST(AudioCallback2, filter); | 130 RUN_TEST(AudioCallback2, filter); |
54 RUN_TEST(AudioCallback3, filter); | 131 RUN_TEST(AudioCallback3, filter); |
55 RUN_TEST(AudioCallback4, filter); | 132 RUN_TEST(AudioCallback4, filter); |
| 133 |
| 134 #if defined(__native_client__) |
| 135 RUN_TEST(AudioThreadCreatorIsRequired, filter); |
| 136 RUN_TEST(AudioThreadCreatorIsCalled, filter); |
| 137 #endif |
56 } | 138 } |
57 | 139 |
58 // Test creating audio resources for all guaranteed sample rates and various | 140 // Test creating audio resources for all guaranteed sample rates and various |
59 // frame counts. | 141 // frame counts. |
60 std::string TestAudio::TestCreation() { | 142 std::string TestAudio::TestCreation() { |
61 static const PP_AudioSampleRate kSampleRates[] = { | 143 static const PP_AudioSampleRate kSampleRates[] = { |
62 PP_AUDIOSAMPLERATE_44100, | 144 PP_AUDIOSAMPLERATE_44100, |
63 PP_AUDIOSAMPLERATE_48000 | 145 PP_AUDIOSAMPLERATE_48000 |
64 }; | 146 }; |
65 static const uint32_t kRequestFrameCounts[] = { | 147 static const uint32_t kRequestFrameCounts[] = { |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 test_done_ = true; | 394 test_done_ = true; |
313 | 395 |
314 // If any more audio callbacks are generated, we should crash (which is good). | 396 // If any more audio callbacks are generated, we should crash (which is good). |
315 audio_callback_method_ = NULL; | 397 audio_callback_method_ = NULL; |
316 | 398 |
317 core_interface_->ReleaseResource(audio); | 399 core_interface_->ReleaseResource(audio); |
318 | 400 |
319 PASS(); | 401 PASS(); |
320 } | 402 } |
321 | 403 |
| 404 #if defined(__native_client__) |
| 405 // Tests the behavior of the thread_create functions. |
| 406 // For PPB_Audio_Shared to work properly, the user code must call |
| 407 // ppapi_register_thread_creator(). This test checks the error handling for the |
| 408 // case when user code doesn't call ppapi_register_thread_creator(). |
| 409 std::string TestAudio::TestAudioThreadCreatorIsRequired() { |
| 410 // We'll inject some thread functions in this test case. |
| 411 // Reset them at the end of this case. |
| 412 ScopedThreadFunctionsResetter thread_resetter; |
| 413 |
| 414 // Set the thread functions to NULLs to emulate the situation where |
| 415 // ppapi_register_thread_creator() is not called by user code. |
| 416 SetNullThreadFunctions(); |
| 417 |
| 418 PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024); |
| 419 ASSERT_TRUE(ac); |
| 420 audio_callback_method_ = NULL; |
| 421 PP_Resource audio = audio_interface_->Create( |
| 422 instance_->pp_instance(), ac, AudioCallbackTrampoline, this); |
| 423 core_interface_->ReleaseResource(ac); |
| 424 ac = 0; |
| 425 |
| 426 // StartPlayback() fails, because no thread creating function |
| 427 // is available. |
| 428 ASSERT_FALSE(audio_interface_->StartPlayback(audio)); |
| 429 |
| 430 // If any more audio callbacks are generated, |
| 431 // we should crash (which is good). |
| 432 audio_callback_method_ = NULL; |
| 433 |
| 434 core_interface_->ReleaseResource(audio); |
| 435 |
| 436 PASS(); |
| 437 } |
| 438 |
| 439 // Tests whether the thread functions passed from the user code are actually |
| 440 // called. |
| 441 std::string TestAudio::TestAudioThreadCreatorIsCalled() { |
| 442 // We'll inject some thread functions in this test case. |
| 443 // Reset them at the end of this case. |
| 444 ScopedThreadFunctionsResetter thread_resetter; |
| 445 |
| 446 // Inject the thread counting function. In the injected function, |
| 447 // when called, g_num_thread_create_called is incremented. |
| 448 g_num_thread_create_called = 0; |
| 449 g_num_thread_join_called = 0; |
| 450 InjectCountingThreadFunctions(); |
| 451 |
| 452 PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024); |
| 453 ASSERT_TRUE(ac); |
| 454 audio_callback_method_ = NULL; |
| 455 PP_Resource audio = audio_interface_->Create( |
| 456 instance_->pp_instance(), ac, AudioCallbackTrampoline, this); |
| 457 core_interface_->ReleaseResource(ac); |
| 458 ac = 0; |
| 459 |
| 460 audio_callback_event_.Reset(); |
| 461 test_done_ = false; |
| 462 |
| 463 audio_callback_method_ = &TestAudio::AudioCallbackTest; |
| 464 ASSERT_TRUE(audio_interface_->StartPlayback(audio)); |
| 465 |
| 466 // Wait for the audio callback to be called. |
| 467 audio_callback_event_.Wait(); |
| 468 // Here, the injected thread_create is called, but thread_join is not yet. |
| 469 ASSERT_EQ(1, g_num_thread_create_called); |
| 470 ASSERT_EQ(0, g_num_thread_join_called); |
| 471 |
| 472 ASSERT_TRUE(audio_interface_->StopPlayback(audio)); |
| 473 |
| 474 test_done_ = true; |
| 475 |
| 476 // Here, the injected thread_join is called. |
| 477 ASSERT_EQ(1, g_num_thread_join_called); |
| 478 |
| 479 // If any more audio callbacks are generated, |
| 480 // we should crash (which is good). |
| 481 audio_callback_method_ = NULL; |
| 482 |
| 483 core_interface_->ReleaseResource(audio); |
| 484 |
| 485 PASS(); |
| 486 } |
| 487 #endif |
| 488 |
322 // TODO(raymes): Test that actually playback happens correctly, etc. | 489 // TODO(raymes): Test that actually playback happens correctly, etc. |
323 | 490 |
324 static void Crash() { | 491 static void Crash() { |
325 *static_cast<volatile unsigned*>(NULL) = 0xdeadbeef; | 492 *static_cast<volatile unsigned*>(NULL) = 0xdeadbeef; |
326 } | 493 } |
327 | 494 |
328 // static | 495 // static |
329 void TestAudio::AudioCallbackTrampoline(void* sample_buffer, | 496 void TestAudio::AudioCallbackTrampoline(void* sample_buffer, |
330 uint32_t buffer_size_in_bytes, | 497 uint32_t buffer_size_in_bytes, |
331 PP_TimeDelta latency, | 498 PP_TimeDelta latency, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 } | 534 } |
368 | 535 |
369 PP_Resource TestAudio::CreateAudioConfig( | 536 PP_Resource TestAudio::CreateAudioConfig( |
370 PP_AudioSampleRate sample_rate, | 537 PP_AudioSampleRate sample_rate, |
371 uint32_t requested_sample_frame_count) { | 538 uint32_t requested_sample_frame_count) { |
372 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( | 539 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( |
373 instance_->pp_instance(), sample_rate, requested_sample_frame_count); | 540 instance_->pp_instance(), sample_rate, requested_sample_frame_count); |
374 return audio_config_interface_->CreateStereo16Bit( | 541 return audio_config_interface_->CreateStereo16Bit( |
375 instance_->pp_instance(), sample_rate, frame_count); | 542 instance_->pp_instance(), sample_rate, frame_count); |
376 } | 543 } |
OLD | NEW |