Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(690)

Side by Side Diff: ppapi/tests/test_audio.cc

Issue 240523002: Add test to make sure if PPB_Audio_Shared::StartThread() works. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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;
Mark Seaborn 2014/04/18 15:28:20 Could you also check that thread_join() is called?
hidehiko 2014/04/18 18:16:30 Done.
41
42 int CoutingThreadCreate(uintptr_t* tid,
43 void (*func)(void* thread_argument),
44 void* thread_argument) {
45 ++g_num_thread_create_called;
46 return g_thread_funcs.thread_create(tid, func, thread_argument);
47 }
48
49 // Sets NULL for PP_ThreadFunctions to emulate the situation that
50 // ppapi_register_thread_creator() is not yet called.
51 void SetNullThreadFunctions() {
52 nacl_irt_ppapihook hooks;
53 GetNaClIrtPpapiHook(&hooks);
54 PP_ThreadFunctions thread_functions = {};
55 hooks.ppapi_register_thread_creator(&thread_functions);
56 }
57
58 void InjectCountingThreadFunctions() {
59 // First of all, we extract the system default thread functions.
60 // Internally, __nacl_register_thread_creator calls
61 // hooks.ppapi_register_thread_creator with default PP_ThreadFunctions
62 // instance. ThreadFunctionGetter stores it to g_thread_funcs.
63 nacl_irt_ppapihook hooks = { NULL, ThreadFunctionsGetter };
64 __nacl_register_thread_creator(&hooks);
65
66 // Here g_thread_funcs stores the thread functions.
67 // Inject the CountingThreadCreate.
68 PP_ThreadFunctions thread_functions = {
69 CoutingThreadCreate,
70 g_thread_funcs.thread_join,
71 };
72 GetNaClIrtPpapiHook(&hooks);
73 hooks.ppapi_register_thread_creator(&thread_functions);
74 }
75
76 // Resets the PP_ThreadFunctions on exit from the scope.
77 class ScopedThreadFunctionsResetter {
78 public:
79 ScopedThreadFunctionsResetter() {}
80 ~ScopedThreadFunctionsResetter() {
81 nacl_irt_ppapihook hooks;
82 GetNaClIrtPpapiHook(&hooks);
83 __nacl_register_thread_creator(&hooks);
84 }
85 };
86
87 } // namespace
88 #endif // __native_client__
89
19 REGISTER_TEST_CASE(Audio); 90 REGISTER_TEST_CASE(Audio);
20 91
21 TestAudio::TestAudio(TestingInstance* instance) 92 TestAudio::TestAudio(TestingInstance* instance)
22 : TestCase(instance), 93 : TestCase(instance),
23 audio_callback_method_(NULL), 94 audio_callback_method_(NULL),
24 audio_callback_event_(instance->pp_instance()), 95 audio_callback_event_(instance->pp_instance()),
25 test_done_(false), 96 test_done_(false),
26 audio_interface_(NULL), 97 audio_interface_(NULL),
27 audio_interface_1_0_(NULL), 98 audio_interface_1_0_(NULL),
28 audio_config_interface_(NULL), 99 audio_config_interface_(NULL),
(...skipping 17 matching lines...) Expand all
46 } 117 }
47 118
48 void TestAudio::RunTests(const std::string& filter) { 119 void TestAudio::RunTests(const std::string& filter) {
49 RUN_TEST(Creation, filter); 120 RUN_TEST(Creation, filter);
50 RUN_TEST(DestroyNoStop, filter); 121 RUN_TEST(DestroyNoStop, filter);
51 RUN_TEST(Failures, filter); 122 RUN_TEST(Failures, filter);
52 RUN_TEST(AudioCallback1, filter); 123 RUN_TEST(AudioCallback1, filter);
53 RUN_TEST(AudioCallback2, filter); 124 RUN_TEST(AudioCallback2, filter);
54 RUN_TEST(AudioCallback3, filter); 125 RUN_TEST(AudioCallback3, filter);
55 RUN_TEST(AudioCallback4, filter); 126 RUN_TEST(AudioCallback4, filter);
127
128 #if defined(__native_client__)
129 RUN_TEST(AudioThreadCreator, filter);
130 #endif
56 } 131 }
57 132
58 // Test creating audio resources for all guaranteed sample rates and various 133 // Test creating audio resources for all guaranteed sample rates and various
59 // frame counts. 134 // frame counts.
60 std::string TestAudio::TestCreation() { 135 std::string TestAudio::TestCreation() {
61 static const PP_AudioSampleRate kSampleRates[] = { 136 static const PP_AudioSampleRate kSampleRates[] = {
62 PP_AUDIOSAMPLERATE_44100, 137 PP_AUDIOSAMPLERATE_44100,
63 PP_AUDIOSAMPLERATE_48000 138 PP_AUDIOSAMPLERATE_48000
64 }; 139 };
65 static const uint32_t kRequestFrameCounts[] = { 140 static const uint32_t kRequestFrameCounts[] = {
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 test_done_ = true; 387 test_done_ = true;
313 388
314 // If any more audio callbacks are generated, we should crash (which is good). 389 // If any more audio callbacks are generated, we should crash (which is good).
315 audio_callback_method_ = NULL; 390 audio_callback_method_ = NULL;
316 391
317 core_interface_->ReleaseResource(audio); 392 core_interface_->ReleaseResource(audio);
318 393
319 PASS(); 394 PASS();
320 } 395 }
321 396
397 #if defined(__native_client__)
398 // Makes sure the behavior of the thread_create functions.
399 // To work PPB_Audio_Shared properly, the user code must call
400 // ppapi_register_thread_creator(). This test checks the error handling for the
401 // case when user code doesn't call ppapi_register_thread_creator(). Also,
402 // it checks whether the thread functions which is passed from the user code
403 // are actually used.
404 std::string TestAudio::TestAudioThreadCreator() {
405 // We'll inject some thread functions in this test case.
406 // Reset them at the end of this case.
407 ScopedThreadFunctionsResetter thread_resetter;
408
409 // First of all, set NULLs to the thread functions to emulate
410 // the situation that ppapi_register_thread_creator() is not called
411 // by user code.
412 SetNullThreadFunctions();
413
414 PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024);
415 ASSERT_TRUE(ac);
416 audio_callback_method_ = NULL;
417 PP_Resource audio = audio_interface_->Create(
418 instance_->pp_instance(), ac, AudioCallbackTrampoline, this);
419 core_interface_->ReleaseResource(ac);
420 ac = 0;
421
422 // First, StartPlayback() fails, because no thread creating funciton
423 // is available.
424 ASSERT_FALSE(audio_interface_->StartPlayback(audio));
425
426 // Inject the thread counting function. In the injected function,
427 // when called, g_num_thread_create_called is incremented.
428 g_num_thread_create_called = 0;
429 InjectCountingThreadFunctions();
430
431 audio_callback_event_.Reset();
432 test_done_ = false;
433
434 audio_callback_method_ = &TestAudio::AudioCallbackTest;
435 // This time, StartPlayback() should succeed.
436 ASSERT_TRUE(audio_interface_->StartPlayback(audio));
437
438 // Wait for the audio callback to be called.
439 audio_callback_event_.Wait();
440 ASSERT_TRUE(audio_interface_->StopPlayback(audio));
441
442 test_done_ = true;
443
444 // Here, the injected thread_create function must have been called.
445 ASSERT_EQ(1, g_num_thread_create_called);
446
447 // If any more audio callbacks are generated,
448 // we should crash (which is good).
449 audio_callback_method_ = NULL;
450
451 core_interface_->ReleaseResource(audio);
452
453 PASS();
454 }
455 #endif
456
322 // TODO(raymes): Test that actually playback happens correctly, etc. 457 // TODO(raymes): Test that actually playback happens correctly, etc.
323 458
324 static void Crash() { 459 static void Crash() {
325 *static_cast<volatile unsigned*>(NULL) = 0xdeadbeef; 460 *static_cast<volatile unsigned*>(NULL) = 0xdeadbeef;
326 } 461 }
327 462
328 // static 463 // static
329 void TestAudio::AudioCallbackTrampoline(void* sample_buffer, 464 void TestAudio::AudioCallbackTrampoline(void* sample_buffer,
330 uint32_t buffer_size_in_bytes, 465 uint32_t buffer_size_in_bytes,
331 PP_TimeDelta latency, 466 PP_TimeDelta latency,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 502 }
368 503
369 PP_Resource TestAudio::CreateAudioConfig( 504 PP_Resource TestAudio::CreateAudioConfig(
370 PP_AudioSampleRate sample_rate, 505 PP_AudioSampleRate sample_rate,
371 uint32_t requested_sample_frame_count) { 506 uint32_t requested_sample_frame_count) {
372 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount( 507 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount(
373 instance_->pp_instance(), sample_rate, requested_sample_frame_count); 508 instance_->pp_instance(), sample_rate, requested_sample_frame_count);
374 return audio_config_interface_->CreateStereo16Bit( 509 return audio_config_interface_->CreateStereo16Bit(
375 instance_->pp_instance(), sample_rate, frame_count); 510 instance_->pp_instance(), sample_rate, frame_count);
376 } 511 }
OLDNEW
« ppapi/shared_impl/ppb_audio_shared.cc ('K') | « ppapi/tests/test_audio.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698