forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_set_audio_write_duration_test.cc
267 lines (222 loc) · 9.17 KB
/
media_set_audio_write_duration_test.cc
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2019 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "starboard/media.h"
#include <unistd.h>
#include <atomic>
#include "starboard/common/optional.h"
#include "starboard/common/spin_lock.h"
#include "starboard/common/time.h"
#include "starboard/configuration_constants.h"
#include "starboard/nplb/player_creation_param_helpers.h"
#include "starboard/nplb/player_test_util.h"
#include "starboard/player.h"
#include "starboard/shared/starboard/media/media_support_internal.h"
#include "starboard/shared/starboard/media/media_util.h"
#include "starboard/shared/starboard/player/video_dmp_reader.h"
#include "starboard/testing/fake_graphics_context_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
using shared::starboard::player::video_dmp::VideoDmpReader;
using ::starboard::testing::FakeGraphicsContextProvider;
using ::testing::ValuesIn;
const int64_t kDuration = 500'000; // 0.5 seconds
const int64_t kSmallWaitInterval = 10'000; // 10 ms
class SbMediaSetAudioWriteDurationTest
: public ::testing::TestWithParam<const char*> {
public:
SbMediaSetAudioWriteDurationTest() : dmp_reader_(GetParam()) {}
void TryToWritePendingSample() {
{
starboard::ScopedSpinLock lock(&pending_decoder_status_lock_);
if (!pending_decoder_status_.has_engaged()) {
return;
}
}
// If we have played the full duration required, then stop.
if ((last_input_timestamp_ - first_input_timestamp_) >= total_duration_) {
return;
}
// Check if we're about to input too far beyond the current playback time.
SbPlayerInfo info;
SbPlayerGetInfo(pending_decoder_status_->player, &info);
if ((last_input_timestamp_ - info.current_media_timestamp) > kDuration) {
// Postpone writing samples.
return;
}
SbPlayer player = pending_decoder_status_->player;
SbMediaType type = pending_decoder_status_->type;
int ticket = pending_decoder_status_->ticket;
{
starboard::ScopedSpinLock lock(&pending_decoder_status_lock_);
pending_decoder_status_ = nullopt;
}
CallSbPlayerWriteSamples(player, kSbMediaTypeAudio, &dmp_reader_, index_,
1);
last_input_timestamp_ =
dmp_reader_.GetPlayerSampleInfo(kSbMediaTypeAudio, index_).timestamp;
++index_;
}
void PlayerStatus(SbPlayer player, SbPlayerState state, int ticket) {
player_state_ = state;
}
void StorePendingDecoderStatus(SbPlayer player,
SbMediaType type,
int ticket) {
starboard::ScopedSpinLock lock(&pending_decoder_status_lock_);
SB_DCHECK(!pending_decoder_status_.has_engaged());
PendingDecoderStatus pending_decoder_status = {};
pending_decoder_status.player = player;
pending_decoder_status.type = type;
pending_decoder_status.ticket = ticket;
pending_decoder_status_ = pending_decoder_status;
}
SbPlayer CreatePlayer() {
SbMediaAudioCodec audio_codec = dmp_reader_.audio_codec();
PlayerCreationParam creation_param = CreatePlayerCreationParam(
audio_codec, kSbMediaVideoCodecNone, kSbPlayerOutputModeInvalid);
SbPlayerCreationParam param = {};
creation_param.ConvertTo(¶m);
creation_param.output_mode = SbPlayerGetPreferredOutputMode(¶m);
EXPECT_NE(creation_param.output_mode, kSbPlayerOutputModeInvalid);
SbPlayer player = CallSbPlayerCreate(
fake_graphics_context_provider_.window(), kSbMediaVideoCodecNone,
audio_codec, kSbDrmSystemInvalid, &dmp_reader_.audio_stream_info(), "",
DummyDeallocateSampleFunc, DecoderStatusFunc, PlayerStatusFunc,
DummyErrorFunc, this /* context */, creation_param.output_mode,
fake_graphics_context_provider_.decoder_target_provider());
EXPECT_TRUE(SbPlayerIsValid(player));
last_input_timestamp_ =
dmp_reader_.GetPlayerSampleInfo(kSbMediaTypeAudio, 0).timestamp;
first_input_timestamp_ = last_input_timestamp_;
return player;
}
void WaitForPlayerState(SbPlayerState desired_state) {
int64_t start_of_wait = CurrentMonotonicTime();
const int64_t kMaxWaitTime = 3'000'000LL; // 3 seconds
while (player_state_ != desired_state &&
(CurrentMonotonicTime() - start_of_wait) < kMaxWaitTime) {
usleep(kSmallWaitInterval);
TryToWritePendingSample();
}
ASSERT_EQ(desired_state, player_state_);
}
protected:
struct PendingDecoderStatus {
SbPlayer player;
SbMediaType type;
int ticket;
};
FakeGraphicsContextProvider fake_graphics_context_provider_;
VideoDmpReader dmp_reader_;
SbPlayerState player_state_ = kSbPlayerStateInitialized;
int64_t last_input_timestamp_ = 0;
int64_t first_input_timestamp_ = 0;
int index_ = 0;
int64_t total_duration_ = kDuration;
// Guard access to |pending_decoder_status_|.
mutable std::atomic_int pending_decoder_status_lock_{
starboard::kSpinLockStateReleased};
optional<PendingDecoderStatus> pending_decoder_status_;
private:
static void DecoderStatusFunc(SbPlayer player,
void* context,
SbMediaType type,
SbPlayerDecoderState state,
int ticket) {
if (state != kSbPlayerDecoderStateNeedsData) {
return;
}
static_cast<SbMediaSetAudioWriteDurationTest*>(context)
->StorePendingDecoderStatus(player, type, ticket);
}
static void PlayerStatusFunc(SbPlayer player,
void* context,
SbPlayerState state,
int ticket) {
static_cast<SbMediaSetAudioWriteDurationTest*>(context)->PlayerStatus(
player, state, ticket);
}
};
TEST_P(SbMediaSetAudioWriteDurationTest, WriteLimitedInput) {
ASSERT_NE(dmp_reader_.audio_codec(), kSbMediaAudioCodecNone);
ASSERT_GT(dmp_reader_.number_of_audio_buffers(), 0);
SbPlayer player = CreatePlayer();
WaitForPlayerState(kSbPlayerStateInitialized);
// Seek to preroll.
SbPlayerSeek(player, first_input_timestamp_, /* ticket */ 1);
WaitForPlayerState(kSbPlayerStatePresenting);
// Wait until the playback time is > 0.
const int64_t kMaxWaitTime = 5'000'000; // 5 seconds
int64_t start_of_wait = CurrentMonotonicTime();
SbPlayerInfo info = {};
while (CurrentMonotonicTime() - start_of_wait < kMaxWaitTime &&
info.current_media_timestamp == 0) {
usleep(500'000);
SbPlayerGetInfo(player, &info);
}
EXPECT_GT(info.current_media_timestamp, 0);
SbPlayerDestroy(player);
}
TEST_P(SbMediaSetAudioWriteDurationTest, WriteContinuedLimitedInput) {
ASSERT_NE(dmp_reader_.audio_codec(), kSbMediaAudioCodecNone);
ASSERT_GT(dmp_reader_.number_of_audio_buffers(), 0);
// This directly impacts the runtime of the test.
total_duration_ = 15'000'000LL; // 15 seconds
SbPlayer player = CreatePlayer();
WaitForPlayerState(kSbPlayerStateInitialized);
// Seek to preroll.
SbPlayerSeek(player, first_input_timestamp_, /* ticket */ 1);
WaitForPlayerState(kSbPlayerStatePresenting);
// Wait for the player to play far enough. It may not play all the way to
// the end, but it should leave off no more than |kDuration|.
int64_t min_ending_playback_time = total_duration_ - kDuration;
int64_t start_of_wait = CurrentMonotonicTime();
const int64_t kMaxWaitTime = total_duration_ + 5'000'000LL;
SbPlayerInfo info;
SbPlayerGetInfo(player, &info);
while (info.current_media_timestamp < min_ending_playback_time &&
(CurrentMonotonicTime() - start_of_wait) < kMaxWaitTime) {
SbPlayerGetInfo(player, &info);
usleep(kSmallWaitInterval);
TryToWritePendingSample();
}
EXPECT_GE(info.current_media_timestamp, min_ending_playback_time);
SbPlayerDestroy(player);
}
std::vector<const char*> GetSupportedTests() {
const char* kFilenames[] = {"beneath_the_canopy_aac_stereo.dmp",
"beneath_the_canopy_opus_stereo.dmp"};
static std::vector<const char*> test_params;
if (!test_params.empty()) {
return test_params;
}
for (auto filename : kFilenames) {
VideoDmpReader dmp_reader(filename, VideoDmpReader::kEnableReadOnDemand);
SB_DCHECK(dmp_reader.number_of_audio_buffers() > 0);
if (SbMediaCanPlayMimeAndKeySystem(dmp_reader.audio_mime_type().c_str(),
"")) {
test_params.push_back(filename);
}
}
SB_DCHECK(!test_params.empty());
return test_params;
}
INSTANTIATE_TEST_CASE_P(SbMediaSetAudioWriteDurationTests,
SbMediaSetAudioWriteDurationTest,
ValuesIn(GetSupportedTests()));
} // namespace
} // namespace nplb
} // namespace starboard