This source file includes following definitions.
- core_interface_
- Init
- RunTests
- TestCreation
- TestDestroyNoStop
- TestFailures
- TestAudioCallback1
- TestAudioCallback2
- TestAudioCallback3
- TestAudioCallback4
- Crash
- AudioCallbackTrampoline
- AudioCallbackTrampoline1_0
- AudioCallbackTrivial
- AudioCallbackTest
- CreateAudioConfig
#include "ppapi/tests/test_audio.h"
#include <string.h>
#include "ppapi/c/ppb_audio_config.h"
#include "ppapi/c/ppb_audio.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/testing_instance.h"
#include "ppapi/tests/test_utils.h"
#define ARRAYSIZE_UNSAFE(a) \
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
REGISTER_TEST_CASE(Audio);
TestAudio::TestAudio(TestingInstance* instance)
: TestCase(instance),
audio_callback_method_(NULL),
audio_callback_event_(instance->pp_instance()),
test_done_(false),
audio_interface_(NULL),
audio_interface_1_0_(NULL),
audio_config_interface_(NULL),
core_interface_(NULL) {
}
TestAudio::~TestAudio() {
}
bool TestAudio::Init() {
audio_interface_ = static_cast<const PPB_Audio_1_1*>(
pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_INTERFACE_1_1));
audio_interface_1_0_ = static_cast<const PPB_Audio_1_0*>(
pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_INTERFACE_1_0));
audio_config_interface_ = static_cast<const PPB_AudioConfig*>(
pp::Module::Get()->GetBrowserInterface(PPB_AUDIO_CONFIG_INTERFACE));
core_interface_ = static_cast<const PPB_Core*>(
pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
return audio_interface_ && audio_interface_1_0_ && audio_config_interface_ &&
core_interface_;
}
void TestAudio::RunTests(const std::string& filter) {
RUN_TEST(Creation, filter);
RUN_TEST(DestroyNoStop, filter);
RUN_TEST(Failures, filter);
RUN_TEST(AudioCallback1, filter);
RUN_TEST(AudioCallback2, filter);
RUN_TEST(AudioCallback3, filter);
RUN_TEST(AudioCallback4, filter);
}
std::string TestAudio::TestCreation() {
static const PP_AudioSampleRate kSampleRates[] = {
PP_AUDIOSAMPLERATE_44100,
PP_AUDIOSAMPLERATE_48000
};
static const uint32_t kRequestFrameCounts[] = {
PP_AUDIOMINSAMPLEFRAMECOUNT,
PP_AUDIOMAXSAMPLEFRAMECOUNT,
PP_AUDIOSAMPLERATE_44100 / 100,
PP_AUDIOSAMPLERATE_48000 / 100,
2 * PP_AUDIOSAMPLERATE_44100 / 100,
2 * PP_AUDIOSAMPLERATE_48000 / 100,
1024,
2048,
4096
};
PP_AudioSampleRate sample_rate = audio_config_interface_->RecommendSampleRate(
instance_->pp_instance());
ASSERT_TRUE(sample_rate == PP_AUDIOSAMPLERATE_NONE ||
sample_rate == PP_AUDIOSAMPLERATE_44100 ||
sample_rate == PP_AUDIOSAMPLERATE_48000);
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSampleRates); i++) {
PP_AudioSampleRate sample_rate = kSampleRates[i];
for (size_t j = 0; j < ARRAYSIZE_UNSAFE(kRequestFrameCounts); j++) {
uint32_t request_frame_count = kRequestFrameCounts[j];
uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount(
instance_->pp_instance(), sample_rate, request_frame_count);
PP_Resource ac = audio_config_interface_->CreateStereo16Bit(
instance_->pp_instance(), sample_rate, frame_count);
ASSERT_TRUE(ac);
PP_Resource audio = audio_interface_->Create(
instance_->pp_instance(), ac, AudioCallbackTrampoline, this);
core_interface_->ReleaseResource(ac);
ac = 0;
ASSERT_TRUE(audio);
ASSERT_TRUE(audio_interface_->IsAudio(audio));
ac = audio_interface_->GetCurrentConfig(audio);
ASSERT_TRUE(ac);
ASSERT_TRUE(audio_config_interface_->IsAudioConfig(ac));
ASSERT_EQ(sample_rate, audio_config_interface_->GetSampleRate(ac));
ASSERT_EQ(frame_count, audio_config_interface_->GetSampleFrameCount(ac));
core_interface_->ReleaseResource(ac);
ac = 0;
audio_callback_method_ = &TestAudio::AudioCallbackTrivial;
ASSERT_TRUE(audio_interface_->StartPlayback(audio));
ASSERT_TRUE(audio_interface_->StopPlayback(audio));
audio_callback_method_ = NULL;
core_interface_->ReleaseResource(audio);
}
}
PASS();
}
std::string TestAudio::TestDestroyNoStop() {
PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 2048);
ASSERT_TRUE(ac);
audio_callback_method_ = NULL;
PP_Resource audio = audio_interface_->Create(
instance_->pp_instance(), ac, AudioCallbackTrampoline, this);
core_interface_->ReleaseResource(ac);
ac = 0;
ASSERT_TRUE(audio);
ASSERT_TRUE(audio_interface_->IsAudio(audio));
audio_callback_method_ = &TestAudio::AudioCallbackTrivial;
ASSERT_TRUE(audio_interface_->StartPlayback(audio));
core_interface_->ReleaseResource(audio);
audio_callback_method_ = NULL;
PASS();
}
std::string TestAudio::TestFailures() {
PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 2048);
ASSERT_TRUE(ac);
audio_callback_method_ = NULL;
PP_Resource audio = audio_interface_->Create(
0, ac, AudioCallbackTrampoline, this);
ASSERT_EQ(0, audio);
audio = audio_interface_->Create(
instance_->pp_instance(), 0, AudioCallbackTrampoline, this);
ASSERT_EQ(0, audio);
audio = audio_interface_->Create(
instance_->pp_instance(), ac, NULL, NULL);
ASSERT_EQ(0, audio);
core_interface_->ReleaseResource(ac);
ac = 0;
ASSERT_FALSE(audio_interface_->IsAudio(0));
ASSERT_EQ(0, audio_interface_->GetCurrentConfig(0));
ASSERT_FALSE(audio_interface_->StartPlayback(0));
ASSERT_FALSE(audio_interface_->StopPlayback(0));
PASS();
}
std::string TestAudio::TestAudioCallback1() {
PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024);
ASSERT_TRUE(ac);
audio_callback_method_ = NULL;
PP_Resource audio = audio_interface_->Create(
instance_->pp_instance(), ac, AudioCallbackTrampoline, this);
core_interface_->ReleaseResource(ac);
ac = 0;
audio_callback_event_.Reset();
test_done_ = false;
audio_callback_method_ = &TestAudio::AudioCallbackTest;
ASSERT_TRUE(audio_interface_->StartPlayback(audio));
audio_callback_event_.Wait();
ASSERT_TRUE(audio_interface_->StopPlayback(audio));
test_done_ = true;
audio_callback_method_ = NULL;
core_interface_->ReleaseResource(audio);
PASS();
}
std::string TestAudio::TestAudioCallback2() {
PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024);
ASSERT_TRUE(ac);
audio_callback_method_ = NULL;
PP_Resource audio = audio_interface_->Create(
instance_->pp_instance(), ac, AudioCallbackTrampoline, this);
core_interface_->ReleaseResource(ac);
ac = 0;
audio_callback_event_.Reset();
test_done_ = false;
audio_callback_method_ = &TestAudio::AudioCallbackTest;
ASSERT_TRUE(audio_interface_->StartPlayback(audio));
audio_callback_event_.Wait();
core_interface_->ReleaseResource(audio);
test_done_ = true;
audio_callback_method_ = NULL;
PASS();
}
std::string TestAudio::TestAudioCallback3() {
PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024);
ASSERT_TRUE(ac);
audio_callback_method_ = NULL;
PP_Resource audio = audio_interface_->Create(
instance_->pp_instance(), ac, AudioCallbackTrampoline, this);
core_interface_->ReleaseResource(ac);
ac = 0;
audio_callback_event_.Reset();
test_done_ = false;
audio_callback_method_ = &TestAudio::AudioCallbackTest;
ASSERT_TRUE(audio_interface_->StartPlayback(audio));
audio_callback_event_.Wait();
ASSERT_TRUE(audio_interface_->StopPlayback(audio));
audio_callback_event_.Reset();
ASSERT_TRUE(audio_interface_->StartPlayback(audio));
audio_callback_event_.Wait();
ASSERT_TRUE(audio_interface_->StopPlayback(audio));
test_done_ = true;
audio_callback_method_ = NULL;
core_interface_->ReleaseResource(audio);
PASS();
}
std::string TestAudio::TestAudioCallback4() {
PP_Resource ac = CreateAudioConfig(PP_AUDIOSAMPLERATE_44100, 1024);
ASSERT_TRUE(ac);
audio_callback_method_ = NULL;
PP_Resource audio = audio_interface_1_0_->Create(
instance_->pp_instance(), ac, AudioCallbackTrampoline1_0, this);
core_interface_->ReleaseResource(ac);
ac = 0;
audio_callback_event_.Reset();
test_done_ = false;
audio_callback_method_ = &TestAudio::AudioCallbackTest;
ASSERT_TRUE(audio_interface_1_0_->StartPlayback(audio));
audio_callback_event_.Wait();
ASSERT_TRUE(audio_interface_1_0_->StopPlayback(audio));
test_done_ = true;
audio_callback_method_ = NULL;
core_interface_->ReleaseResource(audio);
PASS();
}
static void Crash() {
*static_cast<volatile unsigned*>(NULL) = 0xdeadbeef;
}
void TestAudio::AudioCallbackTrampoline(void* sample_buffer,
uint32_t buffer_size_in_bytes,
PP_TimeDelta latency,
void* user_data) {
TestAudio* thiz = static_cast<TestAudio*>(user_data);
if (thiz->core_interface_->IsMainThread())
Crash();
AudioCallbackMethod method = thiz->audio_callback_method_;
(thiz->*method)(sample_buffer, buffer_size_in_bytes, latency);
}
void TestAudio::AudioCallbackTrampoline1_0(void* sample_buffer,
uint32_t buffer_size_in_bytes,
void* user_data) {
AudioCallbackTrampoline(sample_buffer, buffer_size_in_bytes, 0.0, user_data);
}
void TestAudio::AudioCallbackTrivial(void* sample_buffer,
uint32_t buffer_size_in_bytes,
PP_TimeDelta latency) {
if (latency < 0)
Crash();
memset(sample_buffer, 0, buffer_size_in_bytes);
}
void TestAudio::AudioCallbackTest(void* sample_buffer,
uint32_t buffer_size_in_bytes,
PP_TimeDelta latency) {
if (test_done_ || latency < 0)
Crash();
memset(sample_buffer, 0, buffer_size_in_bytes);
audio_callback_event_.Signal();
}
PP_Resource TestAudio::CreateAudioConfig(
PP_AudioSampleRate sample_rate,
uint32_t requested_sample_frame_count) {
uint32_t frame_count = audio_config_interface_->RecommendSampleFrameCount(
instance_->pp_instance(), sample_rate, requested_sample_frame_count);
return audio_config_interface_->CreateStereo16Bit(
instance_->pp_instance(), sample_rate, frame_count);
}