summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_audio_encoder.cc
blob: cc629b5cdee07c2428e0c76b49d0e9997835816c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/tests/test_audio_encoder.h"

#include "ppapi/c/pp_codecs.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/audio_encoder.h"
#include "ppapi/tests/testing_instance.h"

REGISTER_TEST_CASE(AudioEncoder);

bool TestAudioEncoder::Init() {
  audio_encoder_interface_ = static_cast<const PPB_AudioEncoder_0_1*>(
      pp::Module::Get()->GetBrowserInterface(PPB_AUDIOENCODER_INTERFACE_0_1));
  return audio_encoder_interface_ && CheckTestingInterface();
}

void TestAudioEncoder::RunTests(const std::string& filter) {
  RUN_CALLBACK_TEST(TestAudioEncoder, AvailableCodecs, filter);
  RUN_CALLBACK_TEST(TestAudioEncoder, IncorrectParametersFails, filter);
  RUN_CALLBACK_TEST(TestAudioEncoder, InitializeTwiceFails, filter);
  RUN_CALLBACK_TEST(TestAudioEncoder, InitializeOpusMono, filter);
  RUN_CALLBACK_TEST(TestAudioEncoder, InitializeOpusStereo, filter);
}

std::string TestAudioEncoder::TestAvailableCodecs() {
  // Test that we get results for supported formats. We should at
  // least get the software supported formats.
  pp::AudioEncoder audio_encoder(instance_);
  ASSERT_FALSE(audio_encoder.is_null());

  TestCompletionCallbackWithOutput<std::vector<PP_AudioProfileDescription> >
      callback(instance_->pp_instance(), false);
  callback.WaitForResult(
      audio_encoder.GetSupportedProfiles(callback.GetCallback()));
  ASSERT_GE(callback.result(), 1U);

  const std::vector<PP_AudioProfileDescription> audio_profiles =
      callback.output();
  ASSERT_GE(audio_profiles.size(), 1U);

  bool found_opus_48hz = false;
  for (uint32_t i = 0; i < audio_profiles.size(); ++i) {
    const PP_AudioProfileDescription& description = audio_profiles[i];
    if (description.profile == PP_AUDIOPROFILE_OPUS &&
        description.sample_rate == PP_AUDIOBUFFER_SAMPLERATE_48000 &&
        description.max_channels >= 2)
      found_opus_48hz = true;
  }
  ASSERT_TRUE(found_opus_48hz);

  PASS();
}

std::string TestAudioEncoder::TestIncorrectParametersFails() {
  // Test that initializing the encoder with incorrect size fails.
  pp::AudioEncoder audio_encoder(instance_);
  ASSERT_FALSE(audio_encoder.is_null());

  // Invalid number of channels.
  TestCompletionCallback callback(instance_->pp_instance(), false);
  callback.WaitForResult(audio_encoder.Initialize(
      42, PP_AUDIOBUFFER_SAMPLERATE_48000, PP_AUDIOBUFFER_SAMPLESIZE_16_BITS,
      PP_AUDIOPROFILE_OPUS, 10000, PP_HARDWAREACCELERATION_WITHFALLBACK,
      callback.GetCallback()));
  ASSERT_EQ(PP_ERROR_NOTSUPPORTED, callback.result());

  // Invalid sampling rate.
  callback.WaitForResult(audio_encoder.Initialize(
      2, static_cast<PP_AudioBuffer_SampleRate>(123456),
      PP_AUDIOBUFFER_SAMPLESIZE_16_BITS, PP_AUDIOPROFILE_OPUS, 10000,
      PP_HARDWAREACCELERATION_WITHFALLBACK, callback.GetCallback()));
  ASSERT_EQ(PP_ERROR_NOTSUPPORTED, callback.result());

  // Invalid sample size.
  callback.WaitForResult(audio_encoder.Initialize(
      2, static_cast<PP_AudioBuffer_SampleRate>(48000),
      static_cast<PP_AudioBuffer_SampleSize>(72), PP_AUDIOPROFILE_OPUS, 10000,
      PP_HARDWAREACCELERATION_WITHFALLBACK, callback.GetCallback()));
  ASSERT_EQ(PP_ERROR_NOTSUPPORTED, callback.result());

  PASS();
}

std::string TestAudioEncoder::TestInitializeTwiceFails() {
  // Test that initializing the encoder with incorrect size fails.
  pp::AudioEncoder audio_encoder(instance_);
  ASSERT_FALSE(audio_encoder.is_null());

  TestCompletionCallback callback(instance_->pp_instance(), false);
  callback.WaitForResult(audio_encoder.Initialize(
      2, PP_AUDIOBUFFER_SAMPLERATE_48000, PP_AUDIOBUFFER_SAMPLESIZE_16_BITS,
      PP_AUDIOPROFILE_OPUS, 10000, PP_HARDWAREACCELERATION_WITHFALLBACK,
      callback.GetCallback()));
  ASSERT_EQ(PP_OK, callback.result());

  callback.WaitForResult(audio_encoder.Initialize(
      2, PP_AUDIOBUFFER_SAMPLERATE_48000, PP_AUDIOBUFFER_SAMPLESIZE_16_BITS,
      PP_AUDIOPROFILE_OPUS, 10000, PP_HARDWAREACCELERATION_WITHFALLBACK,
      callback.GetCallback()));
  ASSERT_EQ(PP_ERROR_FAILED, callback.result());

  PASS();
}

std::string TestAudioEncoder::TestInitializeOpusMono() {
  return TestInitializeCodec(1, PP_AUDIOBUFFER_SAMPLERATE_48000,
                             PP_AUDIOBUFFER_SAMPLESIZE_16_BITS,
                             PP_AUDIOPROFILE_OPUS);
}

std::string TestAudioEncoder::TestInitializeOpusStereo() {
  return TestInitializeCodec(2, PP_AUDIOBUFFER_SAMPLERATE_48000,
                             PP_AUDIOBUFFER_SAMPLESIZE_16_BITS,
                             PP_AUDIOPROFILE_OPUS);
}

std::string TestAudioEncoder::TestInitializeCodec(
    uint32_t channels,
    PP_AudioBuffer_SampleRate input_sample_rate,
    PP_AudioBuffer_SampleSize input_sample_size,
    PP_AudioProfile output_profile) {
  pp::AudioEncoder audio_encoder(instance_);
  ASSERT_FALSE(audio_encoder.is_null());
  pp::Size audio_size(640, 480);

  TestCompletionCallback callback(instance_->pp_instance(), false);
  callback.WaitForResult(audio_encoder.Initialize(
      channels, input_sample_rate, input_sample_size, output_profile, 10000,
      PP_HARDWAREACCELERATION_WITHFALLBACK, callback.GetCallback()));
  ASSERT_EQ(PP_OK, callback.result());

  ASSERT_GE(audio_encoder.GetNumberOfSamples(), 2U);

  TestCompletionCallbackWithOutput<pp::AudioBuffer> get_buffer(
      instance_->pp_instance(), false);
  get_buffer.WaitForResult(audio_encoder.GetBuffer(get_buffer.GetCallback()));
  ASSERT_EQ(PP_OK, get_buffer.result());

  PASS();
}