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

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
« no previous file with comments | « ppapi/tests/test_audio.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "ppapi/native_client/src/untrusted/irt_stub/thread_creator.h"
15 #include "native_client/src/include/elf_auxv.h"
16
17 #include <unistd.h>
14 18
15 #define ARRAYSIZE_UNSAFE(a) \ 19 #define ARRAYSIZE_UNSAFE(a) \
16 ((sizeof(a) / sizeof(*(a))) / \ 20 ((sizeof(a) / sizeof(*(a))) / \
17 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 21 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
18 22
23 extern char **environ;
24
25 namespace {
26
27 typedef uint32_t Elf32_Word;
28
29 typedef struct {
30 Elf32_Word a_type; // Entry type
31 union {
32 Elf32_Word a_val; // Integer value
33 } a_un;
34 } Elf32_auxv_t;
35
36 Elf32_auxv_t* FindAuxv(void) {
Mark Seaborn 2014/04/16 20:00:00 You should be able to use nacl_interface_query() i
hidehiko 2014/04/17 14:05:15 Great to know. Fixed.
37 // This presumes environ has its startup-time value on the stack.
38 char **ep = environ;
39 while (*ep != NULL)
40 ++ep;
41 return (Elf32_auxv_t *) (ep + 1);
42 }
43
44 TYPE_nacl_irt_query GrokAuxv(const Elf32_auxv_t* auxv) {
45 for (const Elf32_auxv_t* av = auxv; av->a_type != AT_NULL; ++av) {
46 if (av->a_type == AT_SYSINFO)
47 return (TYPE_nacl_irt_query) av->a_un.a_val;
48 }
49 return NULL;
50 }
51
52 void GetNaClIrtPpapiHook(struct nacl_irt_ppapihook* hooks) {
53 TYPE_nacl_irt_query query_func = GrokAuxv(FindAuxv());
54 query_func(NACL_IRT_PPAPIHOOK_v0_1, hooks, sizeof(*hooks));
55 }
56
57 struct PP_ThreadFunctions g_thread_funcs = {};
58 void ThreadFunctionsGetter(const struct PP_ThreadFunctions* thread_funcs) {
59 g_thread_funcs = *thread_funcs;
60 }
61
62 int g_num_thread_create_called = 0;
63 int CoutingThreadCreate(uintptr_t* tid,
64 void (*func)(void* thread_argument),
65 void* thread_argument) {
66 ++g_num_thread_create_called;
67 return g_thread_funcs.thread_create(tid, func, thread_argument);
68 }
69
70 void SetNullThreadFunctions() {
71 nacl_irt_ppapihook hooks;
72 GetNaClIrtPpapiHook(&hooks);
73 PP_ThreadFunctions thread_functions = {};
74 hooks.ppapi_register_thread_creator(&thread_functions);
75 }
76
77 void InjectCountingThreadFunctions() {
78 // First of all, we extract the system default thread functions.
79 nacl_irt_ppapihook hooks = { NULL, ThreadFunctionsGetter };
80 __nacl_register_thread_creator(&hooks);
81
82 // Here g_thread_funcs stores the thread functions.
83 // Inject the CountingThreadCreate.
84 PP_ThreadFunctions thread_functions = {
85 CoutingThreadCreate,
86 g_thread_funcs.thread_join,
87 };
88 GetNaClIrtPpapiHook(&hooks);
89 hooks.ppapi_register_thread_creator(&thread_functions);
90 }
91
92 class ScopedThreadFunctionsResetter {
93 public:
94 ScopedThreadFunctionsResetter() {}
95 ~ScopedThreadFunctionsResetter() {
96 nacl_irt_ppapihook hooks;
97 GetNaClIrtPpapiHook(&hooks);
98 __nacl_register_thread_creator(&hooks);
99 }
100 private:
101 DISALLOW_COPY_AND_ASSIGN(ScopedThreadFunctionsResetter);
hidehiko 2014/04/17 14:05:15 Removed DISALLOW_COPY_AND_ASSIGN here, because, ac
102 };
103
104 } // namespace
105
19 REGISTER_TEST_CASE(Audio); 106 REGISTER_TEST_CASE(Audio);
20 107
21 TestAudio::TestAudio(TestingInstance* instance) 108 TestAudio::TestAudio(TestingInstance* instance)
22 : TestCase(instance), 109 : TestCase(instance),
23 audio_callback_method_(NULL), 110 audio_callback_method_(NULL),
24 audio_callback_event_(instance->pp_instance()), 111 audio_callback_event_(instance->pp_instance()),
25 test_done_(false), 112 test_done_(false),
26 audio_interface_(NULL), 113 audio_interface_(NULL),
27 audio_interface_1_0_(NULL), 114 audio_interface_1_0_(NULL),
28 audio_config_interface_(NULL), 115 audio_config_interface_(NULL),
(...skipping 17 matching lines...) Expand all
46 } 133 }
47 134
48 void TestAudio::RunTests(const std::string& filter) { 135 void TestAudio::RunTests(const std::string& filter) {
49 RUN_TEST(Creation, filter); 136 RUN_TEST(Creation, filter);
50 RUN_TEST(DestroyNoStop, filter); 137 RUN_TEST(DestroyNoStop, filter);
51 RUN_TEST(Failures, filter); 138 RUN_TEST(Failures, filter);
52 RUN_TEST(AudioCallback1, filter); 139 RUN_TEST(AudioCallback1, filter);
53 RUN_TEST(AudioCallback2, filter); 140 RUN_TEST(AudioCallback2, filter);
54 RUN_TEST(AudioCallback3, filter); 141 RUN_TEST(AudioCallback3, filter);
55 RUN_TEST(AudioCallback4, filter); 142 RUN_TEST(AudioCallback4, filter);
143 RUN_TEST(AudioThreadCreator, filter);
56 } 144 }
57 145
58 // Test creating audio resources for all guaranteed sample rates and various 146 // Test creating audio resources for all guaranteed sample rates and various
59 // frame counts. 147 // frame counts.
60 std::string TestAudio::TestCreation() { 148 std::string TestAudio::TestCreation() {
61 static const PP_AudioSampleRate kSampleRates[] = { 149 static const PP_AudioSampleRate kSampleRates[] = {
62 PP_AUDIOSAMPLERATE_44100, 150 PP_AUDIOSAMPLERATE_44100,
63 PP_AUDIOSAMPLERATE_48000 151 PP_AUDIOSAMPLERATE_48000
64 }; 152 };
65 static const uint32_t kRequestFrameCounts[] = { 153 static const uint32_t kRequestFrameCounts[] = {
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 test_done_ = true; 400 test_done_ = true;
313 401
314 // If any more audio callbacks are generated, we should crash (which is good). 402 // If any more audio callbacks are generated, we should crash (which is good).
315 audio_callback_method_ = NULL; 403 audio_callback_method_ = NULL;
316 404
317 core_interface_->ReleaseResource(audio); 405 core_interface_->ReleaseResource(audio);
318 406
319 PASS(); 407 PASS();
320 } 408 }
321 409
410 // Makes sure the behavior of the thread_create functions.
411 // To work PPB_Audio_Shared properly, the user code must call
412 // ppapi_register_thread_creator(). This test checks the error handling for the
413 // case when user code doesn't call ppapi_register_thread_creator(). Also,
414 // it checks whether the thread functions which is passed from the user code
415 // are actually used.
416 std::string TestAudio::TestAudioThreadCreator() {
417 // We'll inject some thread functions in this test case.
418 // Reset them at the end of this case.
419 ScopedThreadFunctionsResetter thread_resetter;
420
421 // First of all, set NULLs to the thread functions to emulate
422 // the situation that ppapi_register_thread_creator() is not called
423 // by user code.
424 SetNullThreadFunctions();
425
426 PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024);
427 ASSERT_TRUE(ac);
428 audio_callback_method_ = NULL;
429 PP_Resource audio = audio_interface_->Create(
430 instance_->pp_instance(), ac, AudioCallbackTrampoline, this);
431 core_interface_->ReleaseResource(ac);
432 ac = 0;
433
434 // First, StartPlayback() fails, because no thread creating funciton
435 // is available.
436 ASSERT_FALSE(audio_interface_->StartPlayback(audio));
437
438 // Inject the thread counting function. In the injected function,
439 // when called, g_num_thread_create_called is incremented.
440 g_num_thread_create_called = 0;
441 InjectCountingThreadFunctions();
442
443 audio_callback_event_.Reset();
444 test_done_ = false;
445
446 audio_callback_method_ = &TestAudio::AudioCallbackTest;
447 // This time, StartPlayback() should succeed.
448 ASSERT_TRUE(audio_interface_->StartPlayback(audio));
449
450 // Wait for the audio callback to be called.
451 audio_callback_event_.Wait();
452 ASSERT_TRUE(audio_interface_->StopPlayback(audio));
453
454 test_done_ = true;
455
456 // Here, the injected thread_create function must have been called.
457 ASSERT_EQ(1, g_num_thread_create_called);
458
459 // If any more audio callbacks are generated,
460 // we should crash (which is good).
461 audio_callback_method_ = NULL;
462
463 core_interface_->ReleaseResource(audio);
464
465 PASS();
466 }
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
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 }
OLDNEW
« no previous file with comments | « ppapi/tests/test_audio.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698