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(AudioThreadCreator, filter); | |
136 #endif | |
56 } | 137 } |
57 | 138 |
58 // Test creating audio resources for all guaranteed sample rates and various | 139 // Test creating audio resources for all guaranteed sample rates and various |
59 // frame counts. | 140 // frame counts. |
60 std::string TestAudio::TestCreation() { | 141 std::string TestAudio::TestCreation() { |
61 static const PP_AudioSampleRate kSampleRates[] = { | 142 static const PP_AudioSampleRate kSampleRates[] = { |
62 PP_AUDIOSAMPLERATE_44100, | 143 PP_AUDIOSAMPLERATE_44100, |
63 PP_AUDIOSAMPLERATE_48000 | 144 PP_AUDIOSAMPLERATE_48000 |
64 }; | 145 }; |
65 static const uint32_t kRequestFrameCounts[] = { | 146 static const uint32_t kRequestFrameCounts[] = { |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 test_done_ = true; | 393 test_done_ = true; |
313 | 394 |
314 // If any more audio callbacks are generated, we should crash (which is good). | 395 // If any more audio callbacks are generated, we should crash (which is good). |
315 audio_callback_method_ = NULL; | 396 audio_callback_method_ = NULL; |
316 | 397 |
317 core_interface_->ReleaseResource(audio); | 398 core_interface_->ReleaseResource(audio); |
318 | 399 |
319 PASS(); | 400 PASS(); |
320 } | 401 } |
321 | 402 |
403 #if defined(__native_client__) | |
404 // Makes sure the behavior of the thread_create functions. | |
bbudge
2014/04/18 18:44:14
s/Makes sure/Tests
hidehiko
2014/04/18 19:01:37
Done.
| |
405 // To work PPB_Audio_Shared properly, the user code must call | |
bbudge
2014/04/18 18:44:14
s/To work PPB_Audio_Shared properly/For PPB_Audio_
hidehiko
2014/04/18 19:01:37
Done.
| |
406 // ppapi_register_thread_creator(). This test checks the error handling for the | |
407 // case when user code doesn't call ppapi_register_thread_creator(). Also, | |
408 // it checks whether the thread functions which is passed from the user code | |
bbudge
2014/04/18 18:44:14
s/is/are
hidehiko
2014/04/18 19:01:37
Done.
| |
409 // are actually used. | |
410 std::string TestAudio::TestAudioThreadCreator() { | |
411 // We'll inject some thread functions in this test case. | |
412 // Reset them at the end of this case. | |
413 ScopedThreadFunctionsResetter thread_resetter; | |
414 | |
415 // First of all, set NULLs to the thread functions to emulate | |
416 // the situation that ppapi_register_thread_creator() is not called | |
417 // by user code. | |
bbudge
2014/04/18 18:44:14
// First of all, set the thread functions to NULLs
hidehiko
2014/04/18 19:01:37
Done.
| |
418 SetNullThreadFunctions(); | |
419 | |
420 PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024); | |
421 ASSERT_TRUE(ac); | |
422 audio_callback_method_ = NULL; | |
423 PP_Resource audio = audio_interface_->Create( | |
424 instance_->pp_instance(), ac, AudioCallbackTrampoline, this); | |
425 core_interface_->ReleaseResource(ac); | |
426 ac = 0; | |
427 | |
428 // First, StartPlayback() fails, because no thread creating funciton | |
bbudge
2014/04/18 18:44:14
sp function
hidehiko
2014/04/18 19:01:37
Done.
| |
429 // is available. | |
430 ASSERT_FALSE(audio_interface_->StartPlayback(audio)); | |
431 | |
432 // Inject the thread counting function. In the injected function, | |
433 // when called, g_num_thread_create_called is incremented. | |
434 g_num_thread_create_called = 0; | |
435 g_num_thread_join_called = 0; | |
436 InjectCountingThreadFunctions(); | |
437 | |
438 audio_callback_event_.Reset(); | |
439 test_done_ = false; | |
440 | |
441 audio_callback_method_ = &TestAudio::AudioCallbackTest; | |
442 // This time, StartPlayback() should succeed. | |
443 ASSERT_TRUE(audio_interface_->StartPlayback(audio)); | |
444 | |
445 // Wait for the audio callback to be called. | |
446 audio_callback_event_.Wait(); | |
447 // Here, the injected thread_create is called, but thread_join is not yet. | |
448 ASSERT_EQ(1, g_num_thread_create_called); | |
449 ASSERT_EQ(0, g_num_thread_join_called); | |
450 | |
451 ASSERT_TRUE(audio_interface_->StopPlayback(audio)); | |
452 | |
453 test_done_ = true; | |
454 | |
455 // Here, the injected thread_join is called. | |
456 ASSERT_EQ(1, g_num_thread_join_called); | |
457 | |
458 // If any more audio callbacks are generated, | |
459 // we should crash (which is good). | |
460 audio_callback_method_ = NULL; | |
461 | |
462 core_interface_->ReleaseResource(audio); | |
463 | |
464 PASS(); | |
465 } | |
466 #endif | |
467 | |
322 // TODO(raymes): Test that actually playback happens correctly, etc. | 468 // TODO(raymes): Test that actually playback happens correctly, etc. |
323 | 469 |
324 static void Crash() { | 470 static void Crash() { |
325 *static_cast<volatile unsigned*>(NULL) = 0xdeadbeef; | 471 *static_cast<volatile unsigned*>(NULL) = 0xdeadbeef; |
326 } | 472 } |
327 | 473 |
328 // static | 474 // static |
329 void TestAudio::AudioCallbackTrampoline(void* sample_buffer, | 475 void TestAudio::AudioCallbackTrampoline(void* sample_buffer, |
330 uint32_t buffer_size_in_bytes, | 476 uint32_t buffer_size_in_bytes, |
331 PP_TimeDelta latency, | 477 PP_TimeDelta latency, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 } | 513 } |
368 | 514 |
369 PP_Resource TestAudio::CreateAudioConfig( | 515 PP_Resource TestAudio::CreateAudioConfig( |
370 PP_AudioSampleRate sample_rate, | 516 PP_AudioSampleRate sample_rate, |
371 uint32_t requested_sample_frame_count) { | 517 uint32_t requested_sample_frame_count) { |
372 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( | 518 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( |
373 instance_->pp_instance(), sample_rate, requested_sample_frame_count); | 519 instance_->pp_instance(), sample_rate, requested_sample_frame_count); |
374 return audio_config_interface_->CreateStereo16Bit( | 520 return audio_config_interface_->CreateStereo16Bit( |
375 instance_->pp_instance(), sample_rate, frame_count); | 521 instance_->pp_instance(), sample_rate, frame_count); |
376 } | 522 } |
OLD | NEW |