Skip to content

Commit fa3e323

Browse files
kaidokertWeiChungChang
authored andcommitted
Fix scorecards runs (youtube#478)
Scorecards verifies the contents of its own executed schema, and fails with `500 server error` if there are modifications from expected structure. See ossf/scorecard-action#1150 b/263900290 Change-Id: Iafe3735a40fab26c25681e78fe54749af810f408
1 parent 9fb7b24 commit fa3e323

File tree

5 files changed

+248
-12
lines changed

5 files changed

+248
-12
lines changed

starboard/nplb/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ target(gtest_target_type, "nplb") {
110110
"microphone_is_sample_rate_supported_test.cc",
111111
"microphone_open_test.cc",
112112
"microphone_read_test.cc",
113+
"multiple_player_test.cc",
113114
"murmurhash2_test.cc",
114115
"mutex_acquire_test.cc",
115116
"mutex_acquire_try_test.cc",
+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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

starboard/nplb/player_test_fixture.cc

+9-5
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ SbPlayerTestFixture::CallbackEvent::CallbackEvent(SbPlayer player,
4646
player_state(state),
4747
ticket(ticket) {}
4848

49-
SbPlayerTestFixture::SbPlayerTestFixture(const SbPlayerTestConfig& config)
50-
: output_mode_(std::get<2>(config)), key_system_(std::get<3>(config)) {
49+
SbPlayerTestFixture::SbPlayerTestFixture(
50+
const SbPlayerTestConfig& config,
51+
FakeGraphicsContextProvider* fake_graphics_context_provider)
52+
: output_mode_(std::get<2>(config)),
53+
key_system_(std::get<3>(config)),
54+
fake_graphics_context_provider_(fake_graphics_context_provider) {
5155
SB_DCHECK(output_mode_ == kSbPlayerOutputModeDecodeToTexture ||
5256
output_mode_ == kSbPlayerOutputModePunchOut);
5357

@@ -257,10 +261,10 @@ void SbPlayerTestFixture::Initialize() {
257261
video_codec = video_dmp_reader_->video_codec();
258262
}
259263
player_ = CallSbPlayerCreate(
260-
fake_graphics_context_provider_.window(), video_codec, audio_codec,
264+
fake_graphics_context_provider_->window(), video_codec, audio_codec,
261265
drm_system_, audio_stream_info, "", DummyDeallocateSampleFunc,
262266
DecoderStatusCallback, PlayerStatusCallback, ErrorCallback, this,
263-
output_mode_, fake_graphics_context_provider_.decoder_target_provider());
267+
output_mode_, fake_graphics_context_provider_->decoder_target_provider());
264268
ASSERT_TRUE(SbPlayerIsValid(player_));
265269
ASSERT_NO_FATAL_FAILURE(WaitForPlayerState(kSbPlayerStateInitialized));
266270
ASSERT_NO_FATAL_FAILURE(Seek(0));
@@ -432,7 +436,7 @@ void SbPlayerTestFixture::GetDecodeTargetWhenSupported() {
432436
return;
433437
}
434438
#if SB_HAS(GLES2)
435-
fake_graphics_context_provider_.RunOnGlesContextThread([&]() {
439+
fake_graphics_context_provider_->RunOnGlesContextThread([&]() {
436440
ASSERT_TRUE(SbPlayerIsValid(player_));
437441
if (output_mode_ != kSbPlayerOutputModeDecodeToTexture) {
438442
ASSERT_EQ(SbPlayerGetCurrentFrame(player_), kSbDecodeTargetInvalid);

starboard/nplb/player_test_fixture.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class SbPlayerTestFixture {
7474
bool write_video_eos_ = false;
7575
};
7676

77-
explicit SbPlayerTestFixture(const SbPlayerTestConfig& config);
77+
explicit SbPlayerTestFixture(
78+
const SbPlayerTestConfig& config,
79+
testing::FakeGraphicsContextProvider* fake_graphics_context_provider);
7880
~SbPlayerTestFixture();
7981

8082
void Seek(const SbTime time);
@@ -188,7 +190,7 @@ class SbPlayerTestFixture {
188190
std::string key_system_;
189191
scoped_ptr<VideoDmpReader> audio_dmp_reader_;
190192
scoped_ptr<VideoDmpReader> video_dmp_reader_;
191-
testing::FakeGraphicsContextProvider fake_graphics_context_provider_;
193+
testing::FakeGraphicsContextProvider* fake_graphics_context_provider_;
192194

193195
SbPlayer player_ = kSbPlayerInvalid;
194196
SbDrmSystem drm_system_ = kSbDrmSystemInvalid;

starboard/nplb/player_write_sample_test.cc

+13-5
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,26 @@ namespace {
2626
using ::testing::ValuesIn;
2727

2828
typedef SbPlayerTestFixture::GroupedSamples GroupedSamples;
29+
typedef testing::FakeGraphicsContextProvider FakeGraphicsContextProvider;
2930

3031
class SbPlayerWriteSampleTest
31-
: public ::testing::TestWithParam<SbPlayerTestConfig> {};
32+
: public ::testing::TestWithParam<SbPlayerTestConfig> {
33+
protected:
34+
FakeGraphicsContextProvider fake_graphics_context_provider_;
35+
};
3236

3337
TEST_P(SbPlayerWriteSampleTest, SeekAndDestroy) {
34-
SbPlayerTestFixture player_fixture(GetParam());
38+
SbPlayerTestFixture player_fixture(GetParam(),
39+
&fake_graphics_context_provider_);
3540
if (HasFatalFailure()) {
3641
return;
3742
}
3843
ASSERT_NO_FATAL_FAILURE(player_fixture.Seek(kSbTimeSecond));
3944
}
4045

4146
TEST_P(SbPlayerWriteSampleTest, NoInput) {
42-
SbPlayerTestFixture player_fixture(GetParam());
47+
SbPlayerTestFixture player_fixture(GetParam(),
48+
&fake_graphics_context_provider_);
4349
if (HasFatalFailure()) {
4450
return;
4551
}
@@ -56,7 +62,8 @@ TEST_P(SbPlayerWriteSampleTest, NoInput) {
5662
}
5763

5864
TEST_P(SbPlayerWriteSampleTest, WriteSingleBatch) {
59-
SbPlayerTestFixture player_fixture(GetParam());
65+
SbPlayerTestFixture player_fixture(GetParam(),
66+
&fake_graphics_context_provider_);
6067
if (HasFatalFailure()) {
6168
return;
6269
}
@@ -78,7 +85,8 @@ TEST_P(SbPlayerWriteSampleTest, WriteSingleBatch) {
7885
}
7986

8087
TEST_P(SbPlayerWriteSampleTest, WriteMultipleBatches) {
81-
SbPlayerTestFixture player_fixture(GetParam());
88+
SbPlayerTestFixture player_fixture(GetParam(),
89+
&fake_graphics_context_provider_);
8290
if (HasFatalFailure()) {
8391
return;
8492
}

0 commit comments

Comments
 (0)