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

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

Issue 8749017: Pepper: Add some basic tests for PPB_Audio. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 9 years 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
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ppapi/tests/test_audio.h"
6
7 #include <string.h>
8
9 #include "ppapi/c/ppb_audio_config.h"
10 #include "ppapi/c/ppb_audio.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/tests/testing_instance.h"
13
14 #define ARRAYSIZE_UNSAFE(a) \
15 ((sizeof(a) / sizeof(*(a))) / \
16 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
17
18 REGISTER_TEST_CASE(Audio);
19
20 bool TestAudio::Init() {
21 audio_interface_ = static_cast<PPB_Audio const*>(
22 pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_INTERFACE));
23 audio_config_interface_ = static_cast<PPB_AudioConfig const*>(
24 pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_CONFIG_INTERFACE));
25 core_interface_ = static_cast<const PPB_Core*>(
26 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
27 return audio_interface_ && audio_config_interface_ && core_interface_;
28 }
29
30 void TestAudio::RunTests(const std::string& filter) {
31 RUN_TEST(Creation, filter);
32 RUN_TEST(DestroyNoStop, filter);
33 RUN_TEST(Failures, filter);
34 }
35
36 // A trivial audio callback to provide when we're no interested in whether it
37 // gets called or not. It just clears the buffer so we don't play noise.
38 static void TrivialAudioCallback(void* sample_buffer,
39 uint32_t buffer_size_in_bytes,
40 void* user_data) {
41 memset(sample_buffer, 0, buffer_size_in_bytes);
42 }
43
44 // Test creating audio resources for all guaranteed sample rates and various
45 // frame counts.
46 std::string TestAudio::TestCreation() {
47 static const PP_AudioSampleRate kSampleRates[] = {
48 PP_AUDIOSAMPLERATE_44100,
49 PP_AUDIOSAMPLERATE_48000
50 };
51 static const uint32_t kRequestFrameCounts[] = {
52 PP_AUDIOMINSAMPLEFRAMECOUNT,
53 PP_AUDIOMAXSAMPLEFRAMECOUNT,
54 // Include some "okay-looking" frame counts; check their validity below.
55 1024,
56 2048,
57 4096
58 };
59
60 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSampleRates); i++) {
61 PP_AudioSampleRate sample_rate = kSampleRates[i];
62
63 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(kRequestFrameCounts); j++) {
64 // Make a config, create the audio resource, and release the config.
65 uint32_t request_frame_count = kRequestFrameCounts[j];
66 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount(
67 sample_rate, request_frame_count);
68 PP_Resource ac = audio_config_interface_->CreateStereo16Bit(
69 instance_->pp_instance(), sample_rate, frame_count);
70 ASSERT_TRUE(ac);
71 PP_Resource audio = audio_interface_->Create(
72 instance_->pp_instance(), ac, TrivialAudioCallback, NULL);
73 core_interface_->ReleaseResource(ac);
74 ac = 0;
75
76 ASSERT_TRUE(audio);
77 ASSERT_TRUE(audio_interface_->IsAudio(audio));
78
79 // Check that the config returned for |audio| matches what we gave it.
80 ac = audio_interface_->GetCurrentConfig(audio);
81 ASSERT_TRUE(ac);
82 ASSERT_TRUE(audio_config_interface_->IsAudioConfig(ac));
83 ASSERT_EQ(sample_rate, audio_config_interface_->GetSampleRate(ac));
84 ASSERT_EQ(frame_count, audio_config_interface_->GetSampleFrameCount(ac));
85 core_interface_->ReleaseResource(ac);
86 ac = 0;
87
88 // Start and stop audio playback. The documentation indicates that
89 // |StartPlayback()| and |StopPlayback()| may fail, but gives no
90 // indication as to why ... so check that they succeed.
91 ASSERT_TRUE(audio_interface_->StartPlayback(audio));
92 ASSERT_TRUE(audio_interface_->StopPlayback(audio));
93
94 core_interface_->ReleaseResource(audio);
95 }
96 }
97
98 PASS();
99 }
100
101 // Test that releasing the resource without calling |StopPlayback()| "works".
102 // TODO(viettrungluu): Figure out how to check that |StopPlayback()| properly
103 // waits for in-flight callbacks.
104 std::string TestAudio::TestDestroyNoStop() {
105 const PP_AudioSampleRate kSampleRate = PP_AUDIOSAMPLERATE_44100;
106 const uint32_t kRequestFrameCount = 2048;
107
108 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount(
109 kSampleRate, kRequestFrameCount);
110 PP_Resource ac = audio_config_interface_->CreateStereo16Bit(
111 instance_->pp_instance(), kSampleRate, frame_count);
112 ASSERT_TRUE(ac);
113 PP_Resource audio = audio_interface_->Create(
114 instance_->pp_instance(), ac, TrivialAudioCallback, NULL);
115 core_interface_->ReleaseResource(ac);
116 ac = 0;
117
118 ASSERT_TRUE(audio);
119 ASSERT_TRUE(audio_interface_->IsAudio(audio));
120
121 // Start playback and release the resource.
122 ASSERT_TRUE(audio_interface_->StartPlayback(audio));
123 core_interface_->ReleaseResource(audio);
124
125 PASS();
126 }
127
128 std::string TestAudio::TestFailures() {
129 const PP_AudioSampleRate kSampleRate = PP_AUDIOSAMPLERATE_44100;
130 const uint32_t kRequestFrameCount = 2048;
131
132 // Test invalid parameters to |Create()|.
133
134 // We want a valid config for some of our tests of |Create()|.
135 uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount(
136 kSampleRate, kRequestFrameCount);
137 PP_Resource ac = audio_config_interface_->CreateStereo16Bit(
138 instance_->pp_instance(), kSampleRate, frame_count);
139 ASSERT_TRUE(ac);
140
141 // Invalid instance -> failure.
142 PP_Resource audio = audio_interface_->Create(
143 0, ac, TrivialAudioCallback, NULL);
144 ASSERT_EQ(0, audio);
145
146 // Invalid config -> failure.
147 audio = audio_interface_->Create(
148 instance_->pp_instance(), 0, TrivialAudioCallback, NULL);
149 ASSERT_EQ(0, audio);
150
151 // Null callback -> failure.
152 audio = audio_interface_->Create(
153 instance_->pp_instance(), ac, NULL, NULL);
154 ASSERT_EQ(0, audio);
155
156 core_interface_->ReleaseResource(ac);
157 ac = 0;
158
159 // Test the other functions with an invalid audio resource.
160 ASSERT_FALSE(audio_interface_->IsAudio(0));
161 ASSERT_EQ(0, audio_interface_->GetCurrentConfig(0));
162 ASSERT_FALSE(audio_interface_->StartPlayback(0));
163 ASSERT_FALSE(audio_interface_->StopPlayback(0));
164
165 PASS();
166 }
167
168 // TODO(viettrungluu): Test that callbacks get called, playback happens, etc.
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