|
| 1 | +// Copyright 2023 The Cobalt Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <string> |
| 16 | + |
| 17 | +#include "starboard/common/string.h" |
| 18 | +#include "starboard/nplb/player_test_fixture.h" |
| 19 | +#include "starboard/nplb/thread_helpers.h" |
| 20 | +#include "starboard/testing/fake_graphics_context_provider.h" |
| 21 | +#include "testing/gtest/include/gtest/gtest.h" |
| 22 | + |
| 23 | +namespace starboard { |
| 24 | +namespace nplb { |
| 25 | +namespace { |
| 26 | + |
| 27 | +using ::starboard::testing::FakeGraphicsContextProvider; |
| 28 | +using ::testing::ValuesIn; |
| 29 | + |
| 30 | +typedef SbPlayerTestFixture::GroupedSamples GroupedSamples; |
| 31 | +typedef std::vector<SbPlayerTestConfig> SbPlayerMultiplePlayerTestConfig; |
| 32 | + |
| 33 | +std::string GetTestConfigName(const SbPlayerTestConfig& config) { |
| 34 | + std::string name; |
| 35 | + |
| 36 | + if (std::get<0>(config)) { |
| 37 | + name += FormatString("audio_%s_", std::get<0>(config)); |
| 38 | + } |
| 39 | + if (std::get<1>(config)) { |
| 40 | + name += FormatString("video_%s_", std::get<1>(config)); |
| 41 | + } |
| 42 | + return name; |
| 43 | +} |
| 44 | + |
| 45 | +std::map<SbPlayerTestConfig, int> TestConfigsToMap( |
| 46 | + const SbPlayerMultiplePlayerTestConfig& test_configs) { |
| 47 | + std::map<SbPlayerTestConfig, int> map; |
| 48 | + for (const auto& test_config : test_configs) { |
| 49 | + ++map[test_config]; |
| 50 | + } |
| 51 | + return map; |
| 52 | +} |
| 53 | + |
| 54 | +std::string GetMultipleSbPlayerTestConfigName( |
| 55 | + ::testing::TestParamInfo<SbPlayerMultiplePlayerTestConfig> info) { |
| 56 | + const auto test_configs = info.param; |
| 57 | + std::string name; |
| 58 | + |
| 59 | + const auto& map = TestConfigsToMap(test_configs); |
| 60 | + for (const auto& it : map) { |
| 61 | + name += GetTestConfigName(it.first); |
| 62 | + name += "num_"; |
| 63 | + name += std::to_string(it.second); |
| 64 | + } |
| 65 | + name += |
| 66 | + (std::get<2>(test_configs.front()) == kSbPlayerOutputModeDecodeToTexture |
| 67 | + ? "_DecodeToTexture" |
| 68 | + : "_Punchout"); |
| 69 | + std::replace(name.begin(), name.end(), '.', '_'); |
| 70 | + std::replace(name.begin(), name.end(), '(', '_'); |
| 71 | + std::replace(name.begin(), name.end(), ')', '_'); |
| 72 | + return name; |
| 73 | +} |
| 74 | + |
| 75 | +std::vector<SbPlayerMultiplePlayerTestConfig> GetSupportedTestConfigs() { |
| 76 | + std::vector<SbPlayerMultiplePlayerTestConfig> configs_to_return; |
| 77 | + const int num_of_players = 5; |
| 78 | + |
| 79 | + std::vector<SbPlayerTestConfig> supported_configs = |
| 80 | + GetSupportedSbPlayerTestConfigs(); |
| 81 | + for (auto& config : supported_configs) { |
| 82 | + SbPlayerMultiplePlayerTestConfig multiplayer_test_config(num_of_players, |
| 83 | + config); |
| 84 | + configs_to_return.emplace_back(multiplayer_test_config); |
| 85 | + } |
| 86 | + |
| 87 | + return configs_to_return; |
| 88 | +} |
| 89 | + |
| 90 | +void SeekAndDestroy(SbPlayerTestFixture* player_fixture) { |
| 91 | + ASSERT_NO_FATAL_FAILURE(player_fixture->Seek(kSbTimeSecond)); |
| 92 | +} |
| 93 | + |
| 94 | +void NoInput(SbPlayerTestFixture* player_fixture) { |
| 95 | + GroupedSamples samples; |
| 96 | + if (player_fixture->HasAudio()) { |
| 97 | + samples.AddAudioSamplesWithEOS(0, 0); |
| 98 | + } |
| 99 | + if (player_fixture->HasVideo()) { |
| 100 | + samples.AddVideoSamplesWithEOS(0, 0); |
| 101 | + } |
| 102 | + ASSERT_NO_FATAL_FAILURE(player_fixture->Write(samples)); |
| 103 | + ASSERT_NO_FATAL_FAILURE(player_fixture->WaitForPlayerEndOfStream()); |
| 104 | +} |
| 105 | + |
| 106 | +void WriteSingleBatch(SbPlayerTestFixture* player_fixture) { |
| 107 | + GroupedSamples samples; |
| 108 | + if (player_fixture->HasAudio()) { |
| 109 | + int samples_to_write = SbPlayerGetMaximumNumberOfSamplesPerWrite( |
| 110 | + player_fixture->GetPlayer(), kSbMediaTypeAudio); |
| 111 | + samples.AddAudioSamplesWithEOS(0, samples_to_write); |
| 112 | + } |
| 113 | + if (player_fixture->HasVideo()) { |
| 114 | + int samples_to_write = SbPlayerGetMaximumNumberOfSamplesPerWrite( |
| 115 | + player_fixture->GetPlayer(), kSbMediaTypeVideo); |
| 116 | + samples.AddVideoSamplesWithEOS(0, samples_to_write); |
| 117 | + } |
| 118 | + |
| 119 | + ASSERT_NO_FATAL_FAILURE(player_fixture->Write(samples)); |
| 120 | + ASSERT_NO_FATAL_FAILURE(player_fixture->WaitForPlayerEndOfStream()); |
| 121 | +} |
| 122 | + |
| 123 | +void WriteMultipleBatches(SbPlayerTestFixture* player_fixture) { |
| 124 | + int samples_to_write = 0; |
| 125 | + // Try to write multiple batches for both audio and video. |
| 126 | + if (player_fixture->HasAudio()) { |
| 127 | + samples_to_write = std::max( |
| 128 | + samples_to_write, SbPlayerGetMaximumNumberOfSamplesPerWrite( |
| 129 | + player_fixture->GetPlayer(), kSbMediaTypeAudio) + |
| 130 | + 1); |
| 131 | + } |
| 132 | + if (player_fixture->HasVideo()) { |
| 133 | + samples_to_write = std::max( |
| 134 | + samples_to_write, SbPlayerGetMaximumNumberOfSamplesPerWrite( |
| 135 | + player_fixture->GetPlayer(), kSbMediaTypeVideo) + |
| 136 | + 1); |
| 137 | + } |
| 138 | + // TODO(b/283533109): We'd better to align the written audio and video samples |
| 139 | + // to a same timestamp. Currently, we simply cap the batch size to 8 samples. |
| 140 | + samples_to_write = std::min(samples_to_write, 8); |
| 141 | + |
| 142 | + GroupedSamples samples; |
| 143 | + if (player_fixture->HasAudio()) { |
| 144 | + samples.AddAudioSamplesWithEOS(0, samples_to_write); |
| 145 | + } |
| 146 | + if (player_fixture->HasVideo()) { |
| 147 | + samples.AddVideoSamplesWithEOS(0, samples_to_write); |
| 148 | + } |
| 149 | + |
| 150 | + ASSERT_NO_FATAL_FAILURE(player_fixture->Write(samples)); |
| 151 | + ASSERT_NO_FATAL_FAILURE(player_fixture->WaitForPlayerEndOfStream()); |
| 152 | +} |
| 153 | + |
| 154 | +class PlayerThread : public AbstractTestThread { |
| 155 | + public: |
| 156 | + PlayerThread(const SbPlayerTestConfig& config, |
| 157 | + FakeGraphicsContextProvider* fake_graphics_context_provider, |
| 158 | + const std::function<void(SbPlayerTestFixture*)>& functor) |
| 159 | + : config_(config), |
| 160 | + functor_(functor), |
| 161 | + fake_graphics_context_provider_(fake_graphics_context_provider) {} |
| 162 | + |
| 163 | + void Run() override { |
| 164 | + SbPlayerTestFixture player(config_, fake_graphics_context_provider_); |
| 165 | + functor_(&player); |
| 166 | + } |
| 167 | + |
| 168 | + private: |
| 169 | + SbPlayerTestConfig config_; |
| 170 | + std::function<void(SbPlayerTestFixture*)> functor_; |
| 171 | + FakeGraphicsContextProvider* fake_graphics_context_provider_; |
| 172 | +}; |
| 173 | + |
| 174 | +class MultiplePlayerTest |
| 175 | + : public ::testing::TestWithParam<SbPlayerMultiplePlayerTestConfig> { |
| 176 | + protected: |
| 177 | + void RunTest(const std::function<void(SbPlayerTestFixture*)>& functor); |
| 178 | + FakeGraphicsContextProvider fake_graphics_context_provider_; |
| 179 | +}; |
| 180 | + |
| 181 | +void MultiplePlayerTest::RunTest( |
| 182 | + const std::function<void(SbPlayerTestFixture*)>& functor) { |
| 183 | + const auto& test_configs = GetParam(); |
| 184 | + |
| 185 | + std::vector<std::unique_ptr<PlayerThread>> player_threads; |
| 186 | + for (const auto& test_config : test_configs) { |
| 187 | + player_threads.emplace_back(std::make_unique<PlayerThread>( |
| 188 | + test_config, &fake_graphics_context_provider_, functor)); |
| 189 | + } |
| 190 | + for (const auto& player_thread : player_threads) { |
| 191 | + player_thread->Start(); |
| 192 | + } |
| 193 | + for (const auto& player_thread : player_threads) { |
| 194 | + player_thread->Join(); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +TEST_P(MultiplePlayerTest, NoInput) { |
| 199 | + RunTest(NoInput); |
| 200 | +} |
| 201 | + |
| 202 | +TEST_P(MultiplePlayerTest, SeekAndDestroy) { |
| 203 | + RunTest(SeekAndDestroy); |
| 204 | +} |
| 205 | + |
| 206 | +TEST_P(MultiplePlayerTest, WriteSingleBatch) { |
| 207 | + RunTest(WriteSingleBatch); |
| 208 | +} |
| 209 | + |
| 210 | +TEST_P(MultiplePlayerTest, WriteMultipleBatches) { |
| 211 | + RunTest(WriteMultipleBatches); |
| 212 | +} |
| 213 | + |
| 214 | +INSTANTIATE_TEST_CASE_P(MultiplePlayerTests, |
| 215 | + MultiplePlayerTest, |
| 216 | + ValuesIn(GetSupportedTestConfigs()), |
| 217 | + GetMultipleSbPlayerTestConfigName); |
| 218 | + |
| 219 | +} // namespace |
| 220 | +} // namespace nplb |
| 221 | +} // namespace starboard |
0 commit comments