Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ppapi / tests / test_media_stream_audio_track.cc
blobe58d4f0cc4109869b329abe7017418aee9c2bfd3
1 // Copyright 2014 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 // Tests PPB_MediaStreamAudioTrack interface.
7 #include "ppapi/tests/test_media_stream_audio_track.h"
9 #include "ppapi/c/private/ppb_testing_private.h"
10 #include "ppapi/cpp/audio_buffer.h"
11 #include "ppapi/cpp/completion_callback.h"
12 #include "ppapi/cpp/instance.h"
13 #include "ppapi/cpp/var.h"
14 #include "ppapi/tests/test_utils.h"
15 #include "ppapi/tests/testing_instance.h"
17 REGISTER_TEST_CASE(MediaStreamAudioTrack);
19 namespace {
21 const int32_t kTimes = 3;
22 const char kJSCode[] =
23 "function gotStream(stream) {"
24 " test_stream = stream;"
25 " var track = stream.getAudioTracks()[0];"
26 " var plugin = document.getElementById('plugin');"
27 " plugin.postMessage(track);"
28 "}"
29 "var constraints = {"
30 " audio: true,"
31 " video: false,"
32 "};"
33 "navigator.getUserMedia = "
34 " navigator.getUserMedia || navigator.webkitGetUserMedia;"
35 "navigator.getUserMedia(constraints,"
36 " gotStream, function() {});";
38 // Helper to check if the |sample_rate| is listed in PP_AudioBuffer_SampleRate
39 // enum.
40 bool IsSampleRateValid(PP_AudioBuffer_SampleRate sample_rate) {
41 switch (sample_rate) {
42 case PP_AUDIOBUFFER_SAMPLERATE_8000:
43 case PP_AUDIOBUFFER_SAMPLERATE_16000:
44 case PP_AUDIOBUFFER_SAMPLERATE_22050:
45 case PP_AUDIOBUFFER_SAMPLERATE_32000:
46 case PP_AUDIOBUFFER_SAMPLERATE_44100:
47 case PP_AUDIOBUFFER_SAMPLERATE_48000:
48 case PP_AUDIOBUFFER_SAMPLERATE_96000:
49 case PP_AUDIOBUFFER_SAMPLERATE_192000:
50 return true;
51 default:
52 return false;
58 TestMediaStreamAudioTrack::TestMediaStreamAudioTrack(TestingInstance* instance)
59 : TestCase(instance),
60 event_(instance_->pp_instance()) {
63 bool TestMediaStreamAudioTrack::Init() {
64 return true;
67 TestMediaStreamAudioTrack::~TestMediaStreamAudioTrack() {
70 void TestMediaStreamAudioTrack::RunTests(const std::string& filter) {
71 RUN_TEST(Create, filter);
72 RUN_TEST(GetBuffer, filter);
75 void TestMediaStreamAudioTrack::HandleMessage(const pp::Var& message) {
76 if (message.is_resource()) {
77 audio_track_ = pp::MediaStreamAudioTrack(message.AsResource());
79 event_.Signal();
82 std::string TestMediaStreamAudioTrack::TestCreate() {
83 // Create a track.
84 instance_->EvalScript(kJSCode);
85 event_.Wait();
86 event_.Reset();
88 ASSERT_FALSE(audio_track_.is_null());
89 ASSERT_FALSE(audio_track_.HasEnded());
90 ASSERT_FALSE(audio_track_.GetId().empty());
92 // Close the track.
93 audio_track_.Close();
94 ASSERT_TRUE(audio_track_.HasEnded());
95 audio_track_ = pp::MediaStreamAudioTrack();
96 PASS();
99 std::string TestMediaStreamAudioTrack::TestGetBuffer() {
100 // Create a track.
101 instance_->EvalScript(kJSCode);
102 event_.Wait();
103 event_.Reset();
105 ASSERT_FALSE(audio_track_.is_null());
106 ASSERT_FALSE(audio_track_.HasEnded());
107 ASSERT_FALSE(audio_track_.GetId().empty());
109 PP_TimeDelta timestamp = 0.0;
111 // Get |kTimes| buffers.
112 for (int i = 0; i < kTimes; ++i) {
113 TestCompletionCallbackWithOutput<pp::AudioBuffer> cc(
114 instance_->pp_instance(), false);
115 cc.WaitForResult(audio_track_.GetBuffer(cc.GetCallback()));
116 ASSERT_EQ(PP_OK, cc.result());
117 pp::AudioBuffer buffer = cc.output();
118 ASSERT_FALSE(buffer.is_null());
119 ASSERT_TRUE(IsSampleRateValid(buffer.GetSampleRate()));
120 ASSERT_EQ(buffer.GetSampleSize(), PP_AUDIOBUFFER_SAMPLESIZE_16_BITS);
122 ASSERT_GE(buffer.GetTimestamp(), timestamp);
123 timestamp = buffer.GetTimestamp();
125 ASSERT_GT(buffer.GetDataBufferSize(), 0U);
126 ASSERT_TRUE(buffer.GetDataBuffer() != NULL);
128 audio_track_.RecycleBuffer(buffer);
130 // A recycled buffer should be invalidated.
131 ASSERT_EQ(buffer.GetSampleRate(), PP_AUDIOBUFFER_SAMPLERATE_UNKNOWN);
132 ASSERT_EQ(buffer.GetSampleSize(), PP_AUDIOBUFFER_SAMPLESIZE_UNKNOWN);
133 ASSERT_EQ(buffer.GetDataBufferSize(), 0U);
134 ASSERT_TRUE(buffer.GetDataBuffer() == NULL);
137 // Close the track.
138 audio_track_.Close();
139 ASSERT_TRUE(audio_track_.HasEnded());
140 audio_track_ = pp::MediaStreamAudioTrack();
141 PASS();