Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick PR #899: [android] Adjust media time during 5.1 playback #1000

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions starboard/android/shared/audio_renderer_passthrough.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ void AudioRendererPassthrough::Seek(SbTime seek_to_time) {
stop_called_ = false;
playback_head_position_when_stopped_ = 0;
stopped_at_ = 0;
first_audio_timestamp_ = -1;
if (!seek_to_time_set) {
seek_to_time_ = seek_to_time;
}
Expand Down Expand Up @@ -312,9 +313,17 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
return seek_to_time_;
}

SbTime audio_start_time;
if (first_audio_timestamp_ > -1) {
audio_start_time = first_audio_timestamp_;
} else {
audio_start_time = seek_to_time_;
}

SbTime playback_time;
if (stop_called_) {
// When AudioTrackBridge::Stop() is called, the playback will continue until
// all the frames written are played, as the AudioTrack in created in
// all the frames written are played, as the AudioTrack is created in
// MODE_STREAM.
auto now = SbTimeGetMonotonicNow();
SB_DCHECK(now >= stopped_at_);
Expand All @@ -324,8 +333,15 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
int64_t total_frames_played =
frames_played + playback_head_position_when_stopped_;
total_frames_played = std::min(total_frames_played, total_frames_written_);
<<<<<<< HEAD
return seek_to_time_ + total_frames_played * kSbTimeSecond /
audio_sample_info_.samples_per_second;
=======
playback_time =
audio_start_time + total_frames_played * kSbTimeSecond /
audio_stream_info_.samples_per_second;
return std::max(playback_time, seek_to_time_);
>>>>>>> 88dc47508d1 ([android] Adjust media time during 5.1 playback (#899))
}

SbTime updated_at;
Expand All @@ -334,14 +350,19 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
if (playback_head_position <= 0) {
// The playback is warming up, don't adjust the media time by the monotonic
// system time.
return seek_to_time_;
return std::max(audio_start_time, seek_to_time_);
}

// TODO: This may cause time regression, because the unadjusted time will be
// returned on pause, after an adjusted time has been returned.
<<<<<<< HEAD
SbTime playback_time =
seek_to_time_ + playback_head_position * kSbTimeSecond /
audio_sample_info_.samples_per_second;
=======
playback_time = audio_start_time + playback_head_position * kSbTimeSecond /
audio_stream_info_.samples_per_second;
>>>>>>> 88dc47508d1 ([android] Adjust media time during 5.1 playback (#899))

// When underlying AudioTrack is paused, we use returned playback time
// directly. Note that we should not use |paused_| or |playback_rate_| here.
Expand All @@ -352,7 +373,7 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
// before calling AudioTrack.Play(), the returned playback time and last frame
// consumed time would be the same as at when we pause the video.
if (audio_track_paused_) {
return playback_time;
return std::max(playback_time, seek_to_time_);
}

// TODO: Cap this to the maximum frames written to the AudioTrack.
Expand All @@ -366,7 +387,7 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,

playback_time += std::max<SbTime>(now - updated_at, 0);

return playback_time;
return std::max(playback_time, seek_to_time_);
}

void AudioRendererPassthrough::CreateAudioTrackAndStartProcessing() {
Expand Down Expand Up @@ -541,6 +562,11 @@ void AudioRendererPassthrough::UpdateStatusAndWriteData(
audio_track_bridge_->PauseAndFlush();
return;
}

if (first_audio_timestamp_ < 0) {
first_audio_timestamp_ = sync_time;
}

decoded_audio_writing_offset_ += samples_written;

if (decoded_audio_writing_offset_ ==
Expand Down
1 change: 1 addition & 0 deletions starboard/android/shared/audio_renderer_passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class AudioRendererPassthrough
int64_t playback_head_position_when_stopped_ = 0;
SbTimeMonotonic stopped_at_ = 0;
SbTime seek_to_time_ = 0;
SbTime first_audio_timestamp_ = -1;
double volume_ = 1.0;
bool paused_ = true;
double playback_rate_ = 1.0;
Expand Down
2 changes: 1 addition & 1 deletion starboard/android/shared/video_render_algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace shared {

namespace {

const SbTimeMonotonic kBufferTooLateThreshold = -30 * kSbTimeMillisecond;
const SbTimeMonotonic kBufferTooLateThreshold = -32 * kSbTimeMillisecond;
const SbTimeMonotonic kBufferReadyThreshold = 50 * kSbTimeMillisecond;

jlong GetSystemNanoTime() {
Expand Down