forked from youtube/cobalt_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_sink_helpers.cc
237 lines (202 loc) · 7.91 KB
/
audio_sink_helpers.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
// Copyright 2016 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/nplb/audio_sink_helpers.h"
#include <algorithm>
#include "starboard/common/log.h"
#include "starboard/configuration_constants.h"
namespace starboard {
namespace nplb {
namespace {
SbMediaAudioSampleType GetAnySupportedSampleType() {
if (SbAudioSinkIsAudioSampleTypeSupported(
kSbMediaAudioSampleTypeInt16Deprecated)) {
return kSbMediaAudioSampleTypeInt16Deprecated;
}
return kSbMediaAudioSampleTypeFloat32;
}
SbMediaAudioFrameStorageType GetAnySupportedFrameStorageType() {
if (SbAudioSinkIsAudioFrameStorageTypeSupported(
kSbMediaAudioFrameStorageTypeInterleaved)) {
return kSbMediaAudioFrameStorageTypeInterleaved;
}
return kSbMediaAudioFrameStorageTypePlanar;
}
} // namespace
AudioSinkTestFrameBuffers::AudioSinkTestFrameBuffers(int channels)
: channels_(channels),
sample_type_(GetAnySupportedSampleType()),
storage_type_(GetAnySupportedFrameStorageType()) {
Init();
}
AudioSinkTestFrameBuffers::AudioSinkTestFrameBuffers(
int channels,
SbMediaAudioSampleType sample_type)
: channels_(channels),
sample_type_(sample_type),
storage_type_(GetAnySupportedFrameStorageType()) {
Init();
}
AudioSinkTestFrameBuffers::AudioSinkTestFrameBuffers(
int channels,
SbMediaAudioFrameStorageType storage_type)
: channels_(channels),
sample_type_(GetAnySupportedSampleType()),
storage_type_(storage_type) {
Init();
}
AudioSinkTestFrameBuffers::AudioSinkTestFrameBuffers(
int channels,
SbMediaAudioSampleType sample_type,
SbMediaAudioFrameStorageType storage_type)
: channels_(channels),
sample_type_(sample_type),
storage_type_(storage_type) {
Init();
}
void AudioSinkTestFrameBuffers::Init() {
frames_per_channel_ = SbAudioSinkGetMinBufferSizeInFrames(
channels_, sample_type_, AudioSinkTestEnvironment::sample_rate());
if (storage_type_ == kSbMediaAudioFrameStorageTypeInterleaved) {
frame_buffer_.resize(bytes_per_frame() * channels_ * frames_per_channel());
frame_buffers_.resize(1);
frame_buffers_[0] = &frame_buffer_[0];
} else {
// We make all planar audio channels share the same frame buffer.
frame_buffer_.resize(bytes_per_frame() * frames_per_channel());
frame_buffers_.resize(channels_, &frame_buffer_[0]);
}
}
AudioSinkTestEnvironment::AudioSinkTestEnvironment(
const AudioSinkTestFrameBuffers& frame_buffers)
: frame_buffers_(frame_buffers), condition_variable_(mutex_) {
sink_ = SbAudioSinkCreate(
frame_buffers_.channels(), sample_rate(), frame_buffers_.sample_type(),
frame_buffers_.storage_type(), frame_buffers_.frame_buffers(),
frame_buffers_.frames_per_channel(), UpdateSourceStatusFunc,
ConsumeFramesFunc, this);
}
AudioSinkTestEnvironment::~AudioSinkTestEnvironment() {
SbAudioSinkDestroy(sink_);
}
void AudioSinkTestEnvironment::SetIsPlaying(bool is_playing) {
ScopedLock lock(mutex_);
is_playing_ = is_playing;
}
void AudioSinkTestEnvironment::AppendFrame(int frames_to_append) {
ScopedLock lock(mutex_);
AppendFrame_Locked(frames_to_append);
}
int AudioSinkTestEnvironment::GetFrameBufferFreeSpaceInFrames() const {
ScopedLock lock(mutex_);
return GetFrameBufferFreeSpaceInFrames_Locked();
}
bool AudioSinkTestEnvironment::WaitUntilUpdateStatusCalled() {
ScopedLock lock(mutex_);
int update_source_status_call_count = update_source_status_call_count_;
SbTimeMonotonic start = SbTimeGetMonotonicNow();
while (update_source_status_call_count == update_source_status_call_count_) {
SbTime time_elapsed = SbTimeGetMonotonicNow() - start;
if (time_elapsed >= kTimeToTry) {
return false;
}
SbTime time_to_wait = kTimeToTry - time_elapsed;
condition_variable_.WaitTimed(time_to_wait);
}
return true;
}
bool AudioSinkTestEnvironment::WaitUntilSomeFramesAreConsumed() {
ScopedLock lock(mutex_);
int frames_consumed = frames_consumed_;
SbTimeMonotonic start = SbTimeGetMonotonicNow();
while (frames_consumed == frames_consumed_) {
SbTime time_elapsed = SbTimeGetMonotonicNow() - start;
if (time_elapsed >= kTimeToTry) {
return false;
}
SbTime time_to_wait = kTimeToTry - time_elapsed;
condition_variable_.WaitTimed(time_to_wait);
}
return true;
}
bool AudioSinkTestEnvironment::WaitUntilAllFramesAreConsumed() {
const int kMaximumFramesPerAppend = 1024;
ScopedLock lock(mutex_);
is_eos_reached_ = true;
int frames_appended_before_eos = frames_appended_;
SbTimeMonotonic start = SbTimeGetMonotonicNow();
int silence_frames_appended = 0;
while (frames_consumed_ < frames_appended_before_eos) {
SbTime time_elapsed = SbTimeGetMonotonicNow() - start;
if (time_elapsed >= kTimeToTry) {
return false;
}
SbTime time_to_wait = kTimeToTry - time_elapsed;
// Append silence as some audio sink implementations won't be able to finish
// playback to the last frames filled.
int silence_frames_to_append =
std::min({GetFrameBufferFreeSpaceInFrames_Locked(),
frame_buffers_.frames_per_channel() - silence_frames_appended,
kMaximumFramesPerAppend});
AppendFrame_Locked(silence_frames_to_append);
silence_frames_appended += silence_frames_to_append;
condition_variable_.WaitTimed(time_to_wait);
}
return true;
}
void AudioSinkTestEnvironment::AppendFrame_Locked(int frames_to_append) {
mutex_.DCheckAcquired();
frames_appended_ += frames_to_append;
}
int AudioSinkTestEnvironment::GetFrameBufferFreeSpaceInFrames_Locked() const {
mutex_.DCheckAcquired();
int frames_in_buffer = frames_appended_ - frames_consumed_;
return frame_buffers_.frames_per_channel() - frames_in_buffer;
}
void AudioSinkTestEnvironment::OnUpdateSourceStatus(int* frames_in_buffer,
int* offset_in_frames,
bool* is_playing,
bool* is_eos_reached) {
ScopedLock lock(mutex_);
*frames_in_buffer = frames_appended_ - frames_consumed_;
*offset_in_frames = frames_consumed_ % frame_buffers_.frames_per_channel();
*is_playing = is_playing_;
*is_eos_reached = is_eos_reached_;
++update_source_status_call_count_;
condition_variable_.Signal();
}
void AudioSinkTestEnvironment::OnConsumeFrames(int frames_consumed) {
ScopedLock lock(mutex_);
frames_consumed_ += frames_consumed;
condition_variable_.Signal();
}
// static
void AudioSinkTestEnvironment::UpdateSourceStatusFunc(int* frames_in_buffer,
int* offset_in_frames,
bool* is_playing,
bool* is_eos_reached,
void* context) {
AudioSinkTestEnvironment* environment =
reinterpret_cast<AudioSinkTestEnvironment*>(context);
environment->OnUpdateSourceStatus(frames_in_buffer, offset_in_frames,
is_playing, is_eos_reached);
}
// static
void AudioSinkTestEnvironment::ConsumeFramesFunc(int frames_consumed,
void* context) {
AudioSinkTestEnvironment* environment =
reinterpret_cast<AudioSinkTestEnvironment*>(context);
environment->OnConsumeFrames(frames_consumed);
}
} // namespace nplb
} // namespace starboard