28 const double kBufferTarget = 2;
31 const double kSyncLimit = 0.1;
34 const uint8_t kSilenceBuffer[4096] = {0};
37 uint8_t BytesPerSample(std::shared_ptr<DecodedFrame>
frame) {
38 switch (get<SampleFormat>(frame->format)) {
58 LOG(DFATAL) <<
"Unsupported sample format: " << frame->format;
63 size_t BytesToSamples(std::shared_ptr<DecodedFrame> frame,
size_t bytes) {
64 size_t bytes_per_sample =
65 BytesPerSample(frame) * frame->stream_info->channel_count;
66 CHECK_EQ(bytes % bytes_per_sample, 0);
67 return bytes / bytes_per_sample;
70 double BytesToSeconds(std::shared_ptr<DecodedFrame> frame,
size_t bytes) {
71 return static_cast<double>(BytesToSamples(frame, bytes)) /
72 frame->stream_info->sample_rate;
84 int64_t GetSyncBytes(
double prev_time,
size_t bytes_written,
85 std::shared_ptr<DecodedFrame> next) {
86 const int64_t bytes_per_sample = BytesPerSample(next);
87 const double buffer_end = prev_time + BytesToSeconds(next, bytes_written);
89 if (std::abs(buffer_end - next->pts) < kSyncLimit)
93 const int64_t sample_delta =
static_cast<int64_t
>(
94 (buffer_end - next->pts) * next->stream_info->sample_rate);
95 return sample_delta * next->stream_info->channel_count * bytes_per_sample;
101 : mutex_(
"AudioRendererCommon"),
102 on_play_(
"AudioRendererCommon"),
103 clock_(&util::Clock::Instance),
110 thread_(
"AudioRenderer",
114 CHECK(shutdown_) <<
"Must call Stop before destroying";
121 std::unique_lock<Mutex> lock(mutex_);
126 needs_resync_ =
true;
134 std::unique_lock<Mutex> lock(mutex_);
136 needs_resync_ =
true;
142 std::unique_lock<Mutex> lock(mutex_);
144 SetDeviceState(
false);
148 std::unique_lock<Mutex> lock(mutex_);
153 std::unique_lock<Mutex> lock(mutex_);
155 UpdateVolume(muted_ ? 0 : volume_);
159 std::unique_lock<Mutex> lock(mutex_);
164 std::unique_lock<Mutex> lock(mutex_);
166 UpdateVolume(muted ? 0 : volume_);
171 std::unique_lock<Mutex> lock(mutex_);
177 bool AudioRendererCommon::FillSilence(
size_t bytes) {
179 const size_t to_write = std::min(bytes,
sizeof(kSilenceBuffer));
180 if (!AppendBuffer(kSilenceBuffer, to_write))
182 bytes_written_ += to_write;
188 bool AudioRendererCommon::IsFrameSimilar(
189 std::shared_ptr<DecodedFrame> frame1,
190 std::shared_ptr<DecodedFrame> frame2)
const {
191 return frame1 && frame2 && frame1->stream_info == frame2->stream_info &&
192 frame1->format == frame2->format;
195 bool AudioRendererCommon::WriteFrame(std::shared_ptr<DecodedFrame> frame,
204 const size_t channel_count = frame->stream_info->channel_count;
205 const size_t sample_size = BytesPerSample(frame);
206 const size_t sample_count = frame->linesize[0] / sample_size;
207 const size_t per_channel_sync = sync_bytes / channel_count;
208 const size_t skipped_samples = per_channel_sync / sample_size;
209 if (sample_count > skipped_samples) {
210 std::vector<uint8_t> temp(frame->linesize[0] * channel_count -
212 uint8_t* output = temp.data();
213 for (
size_t sample = skipped_samples; sample < sample_count; sample++) {
214 for (
size_t channel = 0; channel < channel_count; channel++) {
215 std::memcpy(output, frame->data[channel] + sample * sample_size,
217 output += sample_size;
220 if (!AppendBuffer(temp.data(), temp.size()))
222 bytes_written_ += temp.size();
225 if (frame->linesize[0] > sync_bytes) {
226 if (!AppendBuffer(frame->data[0] + sync_bytes,
227 frame->linesize[0] - sync_bytes)) {
230 bytes_written_ += frame->linesize[0] - sync_bytes;
236 void AudioRendererCommon::SetClock(
const util::Clock* clock) {
240 void AudioRendererCommon::ThreadMain() {
241 std::unique_lock<Mutex> lock(mutex_);
243 if (!player_ || !input_) {
249 const bool is_playing =
252 SetDeviceState(is_playing);
259 const size_t buffered_bytes = GetBytesBuffered();
260 std::shared_ptr<DecodedFrame> next;
261 if (needs_resync_ || !cur_frame_) {
265 const double buffered_extra =
266 BytesToSeconds(cur_frame_, buffered_bytes) - kBufferTarget;
267 if (buffered_extra > 0) {
282 if (!IsFrameSimilar(cur_frame_, next)) {
288 const double buffered = BytesToSeconds(cur_frame_, buffered_bytes);
289 const double delay = std::max(buffered - 0.1, 0.0);
297 if (!InitDevice(next, muted_ ? 0 : volume_))
299 SetDeviceState(
true);
300 needs_resync_ =
true;
304 if (needs_resync_ || !cur_frame_) {
305 sync_bytes = GetSyncBytes(time, 0, next);
309 sync_bytes = GetSyncBytes(sync_time_, bytes_written_, next);
311 if (sync_bytes < 0) {
312 if (!FillSilence(-sync_bytes))
316 if (!WriteFrame(next, sync_bytes))
319 needs_resync_ =
false;
325 std::unique_lock<Mutex> lock(mutex_);
326 needs_resync_ =
true;
330 void AudioRendererCommon::OnPlaybackRateChanged(
double old_rate,
332 std::unique_lock<Mutex> lock(mutex_);
333 needs_resync_ =
true;
337 void AudioRendererCommon::OnSeeking() {
338 std::unique_lock<Mutex> lock(mutex_);
339 needs_resync_ =
true;
std::shared_ptr< shaka::media::DecodedFrame > frame
T ResetAndWaitWhileUnlocked(std::unique_lock< _Mutex > &lock)
virtual void SleepSeconds(double seconds) const