Skip to content

Commit

Permalink
Deprecate SbTime APIs
Browse files Browse the repository at this point in the history
- Replace all remaining usages of SbTime
- Deprecate the APIs in starboard/time.h

b/302733082

Test-On-Device: true
  • Loading branch information
gbournou committed Jan 7, 2024
1 parent 98da637 commit 6e1c894
Show file tree
Hide file tree
Showing 301 changed files with 1,663 additions and 1,605 deletions.
1 change: 0 additions & 1 deletion base/time/time_now_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "starboard/client_porting/poem/eztime_poem.h"
#include "starboard/common/log.h"
#include "starboard/common/time.h"
#include "starboard/time.h"
#include "starboard/types.h"

namespace base {
Expand Down
1 change: 0 additions & 1 deletion base/time/time_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "base/logging.h"
#include "starboard/client_porting/eztime/eztime.h"
#include "starboard/time.h"

namespace base {

Expand Down
1 change: 0 additions & 1 deletion components/update_client/unzip/unzip_impl_cobalt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "base/callback.h"
#include "base/files/file_path.h"
#include "starboard/time.h"
#include "third_party/zlib/google/zip.h"

namespace update_client {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public StarboardBridge(

private native boolean nativeInitialize();

private native long nativeSbTimeGetMonotonicNow();
private native long nativeCurrentMonotonicTime();

protected void onActivityStart(Activity activity, KeyboardEditor keyboardEditor) {
activityHolder.set(activity);
Expand Down Expand Up @@ -800,7 +800,7 @@ protected long getAppStartTimestamp() {
Activity activity = activityHolder.get();
if (activity instanceof CobaltActivity) {
long javaStartTimestamp = ((CobaltActivity) activity).getAppStartTimestamp();
long cppTimestamp = nativeSbTimeGetMonotonicNow();
long cppTimestamp = nativeCurrentMonotonicTime();
long javaStopTimestamp = System.nanoTime();
return cppTimestamp
- (javaStartTimestamp - javaStopTimestamp) / timeNanosecondsPerMicrosecond;
Expand Down
10 changes: 5 additions & 5 deletions starboard/android/shared/android_media_session_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,23 @@ void OnMediaSessionStateChanged(
}

jlong durationInMilliseconds;
if (session_state.duration == kSbTimeMax) {
if (session_state.duration == kSbInt64Max) {
// Set duration to negative if duration is unknown or infinite, as with live
// playback.
// https://developer.android.com/reference/android/support/v4/media/MediaMetadataCompat#METADATA_KEY_DURATION
durationInMilliseconds = -1;
} else {
// SbTime is measured in microseconds while Android MediaSession expects
// duration in milliseconds.
durationInMilliseconds = session_state.duration / kSbTimeMillisecond;
// Starboard time is measured in microseconds while Android MediaSession
// expects duration in milliseconds.
durationInMilliseconds = session_state.duration / 1000;
}

env->CallStarboardVoidMethodOrAbort(
"updateMediaSession",
"(IJJFLjava/lang/String;Ljava/lang/String;Ljava/lang/String;"
"[Ldev/cobalt/media/MediaImage;J)V",
playback_state, playback_state_actions,
session_state.current_playback_position / kSbTimeMillisecond,
session_state.current_playback_position / 1000,
static_cast<jfloat>(session_state.actual_playback_rate), j_title.Get(),
j_artist.Get(), j_album.Get(), j_artwork.Get(), durationInMilliseconds);
}
Expand Down
13 changes: 7 additions & 6 deletions starboard/android/shared/application_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/common/string.h"
#include "starboard/common/time.h"
#include "starboard/event.h"
#include "starboard/key.h"
#include "starboard/shared/starboard/audio_sink/audio_sink_internal.h"
Expand Down Expand Up @@ -206,14 +207,14 @@ bool ApplicationAndroid::DestroyWindow(SbWindow window) {
return true;
}

Event* ApplicationAndroid::WaitForSystemEventWithTimeout(SbTime time) {
Event* ApplicationAndroid::WaitForSystemEventWithTimeout(int64_t time) {
// Limit the polling time in case some non-system event is injected.
const int kMaxPollingTimeMillisecond = 10;

// Convert from microseconds to milliseconds, taking the ceiling value.
// If we take the floor, or round, then we end up busy looping every time
// the next event time is less than one millisecond.
int timeout_millis = (time + kSbTimeMillisecond - 1) / kSbTimeMillisecond;
int timeout_millis = (time + 1000 - 1) / 1000;
int looper_events;
int ident = ALooper_pollAll(
std::min(std::max(timeout_millis, 0), kMaxPollingTimeMillisecond), NULL,
Expand Down Expand Up @@ -371,7 +372,7 @@ void ApplicationAndroid::ProcessAndroidCommand() {
free(static_cast<void*>(deep_link));
} else {
SB_LOG(INFO) << "ApplicationAndroid Inject: kSbEventTypeLink";
Inject(new Event(kSbEventTypeLink, SbTimeGetMonotonicNow(), deep_link,
Inject(new Event(kSbEventTypeLink, CurrentMonotonicTime(), deep_link,
free));
}
}
Expand Down Expand Up @@ -667,19 +668,19 @@ void ApplicationAndroid::OsNetworkStatusChange(bool became_online) {
}
}

SbTimeMonotonic ApplicationAndroid::GetAppStartTimestamp() {
int64_t ApplicationAndroid::GetAppStartTimestamp() {
JniEnvExt* env = JniEnvExt::Get();
jlong app_start_timestamp =
env->CallStarboardLongMethodOrAbort("getAppStartTimestamp", "()J");
return app_start_timestamp;
}

extern "C" SB_EXPORT_PLATFORM jlong
Java_dev_cobalt_coat_StarboardBridge_nativeSbTimeGetMonotonicNow(
Java_dev_cobalt_coat_StarboardBridge_nativeCurrentMonotonicTime(
JNIEnv* env,
jobject jcaller,
jboolean online) {
return SbTimeGetMonotonicNow();
return CurrentMonotonicTime();
}

void ApplicationAndroid::SendDateTimeConfigurationChangedEvent() {
Expand Down
4 changes: 2 additions & 2 deletions starboard/android/shared/application_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class ApplicationAndroid
void SbWindowSendInputEvent(const char* input_text, bool is_composing);
void SendLowMemoryEvent();
void OsNetworkStatusChange(bool became_online);
SbTimeMonotonic GetAppStartTimestamp();
int64_t GetAppStartTimestamp(); // microseconds

void SendDateTimeConfigurationChangedEvent();

Expand All @@ -129,7 +129,7 @@ class ApplicationAndroid

// --- QueueApplication overrides ---
bool MayHaveSystemEvents() override { return handle_system_events_; }
Event* WaitForSystemEventWithTimeout(SbTime time) override;
Event* WaitForSystemEventWithTimeout(int64_t time) override;
void WakeSystemEventWait() override;

private:
Expand Down
39 changes: 20 additions & 19 deletions starboard/android/shared/audio_renderer_passthrough.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"
#include "starboard/common/string.h"
#include "starboard/common/time.h"
#include "starboard/memory.h"

namespace starboard {
Expand All @@ -32,7 +33,7 @@ namespace {
// pushing data when there are enough decoded audio buffers.
constexpr int kMaxDecodedAudios = 64;

constexpr SbTime kAudioTrackUpdateInternal = kSbTimeMillisecond * 5;
constexpr int64_t kAudioTrackUpdateInternal = 5'000; // 5ms

constexpr int kPreferredBufferSizeInBytes = 16 * 1024;
// TODO: Enable passthrough with tunnel mode.
Expand Down Expand Up @@ -247,7 +248,7 @@ void AudioRendererPassthrough::SetPlaybackRate(double playback_rate) {
playback_rate_ = playback_rate;
}

void AudioRendererPassthrough::Seek(SbTime seek_to_time) {
void AudioRendererPassthrough::Seek(int64_t seek_to_time) {
SB_DCHECK(BelongsToCurrentThread());

SB_LOG(INFO) << "Seek to " << seek_to_time;
Expand Down Expand Up @@ -292,10 +293,10 @@ void AudioRendererPassthrough::Seek(SbTime seek_to_time) {
}

// This function can be called from *any* threads.
SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
bool* is_eos_played,
bool* is_underflow,
double* playback_rate) {
int64_t AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
bool* is_eos_played,
bool* is_underflow,
double* playback_rate) {
SB_DCHECK(is_playing);
SB_DCHECK(is_eos_played);
SB_DCHECK(is_underflow);
Expand All @@ -311,33 +312,33 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
return seek_to_time_;
}

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

SbTime playback_time;
int64_t playback_time;
if (stop_called_) {
// When AudioTrackBridge::Stop() is called, the playback will continue until
// all the frames written are played, as the AudioTrack is created in
// MODE_STREAM.
auto now = SbTimeGetMonotonicNow();
auto now = CurrentMonotonicTime();
SB_DCHECK(now >= stopped_at_);
auto time_elapsed = now - stopped_at_;
int64_t frames_played =
time_elapsed * audio_stream_info_.samples_per_second / kSbTimeSecond;
time_elapsed * audio_stream_info_.samples_per_second / 1'000'000LL;
int64_t total_frames_played =
frames_played + playback_head_position_when_stopped_;
total_frames_played = std::min(total_frames_played, total_frames_written_);
playback_time =
audio_start_time + total_frames_played * kSbTimeSecond /
audio_start_time + total_frames_played * 1'000'000LL /
audio_stream_info_.samples_per_second;
return std::max(playback_time, seek_to_time_);
}

SbTime updated_at;
int64_t updated_at;
auto playback_head_position =
audio_track_bridge_->GetAudioTimestamp(&updated_at);
if (playback_head_position <= 0) {
Expand All @@ -348,7 +349,7 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,

// TODO: This may cause time regression, because the unadjusted time will be
// returned on pause, after an adjusted time has been returned.
playback_time = audio_start_time + playback_head_position * kSbTimeSecond /
playback_time = audio_start_time + playback_head_position * 1'000'000LL /
audio_stream_info_.samples_per_second;

// When underlying AudioTrack is paused, we use returned playback time
Expand All @@ -364,15 +365,15 @@ SbTime AudioRendererPassthrough::GetCurrentMediaTime(bool* is_playing,
}

// TODO: Cap this to the maximum frames written to the AudioTrack.
auto now = SbTimeGetMonotonicNow();
auto now = CurrentMonotonicTime();
SB_LOG_IF(WARNING, now < updated_at)
<< "now (" << now << ") is not greater than updated_at (" << updated_at
<< ").";
SB_LOG_IF(WARNING, now - updated_at > kSbTimeSecond)
SB_LOG_IF(WARNING, now - updated_at > 1'000'000LL)
<< "Elapsed time (" << now - updated_at
<< ") is greater than 1s. (playback_time " << playback_time << ")";

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

return std::max(playback_time, seek_to_time_);
}
Expand Down Expand Up @@ -419,7 +420,7 @@ void AudioRendererPassthrough::CreateAudioTrackAndStartProcessing() {
}

void AudioRendererPassthrough::FlushAudioTrackAndStopProcessing(
SbTime seek_to_time) {
int64_t seek_to_time) {
SB_DCHECK(audio_track_thread_);
SB_DCHECK(audio_track_thread_->BelongsToCurrentThread());

Expand Down Expand Up @@ -573,9 +574,9 @@ void AudioRendererPassthrough::UpdateStatusAndWriteData(
// EOS is handled on this thread instead of in GetCurrentMediaTime(), because
// GetCurrentMediaTime() is not guaranteed to be called.
if (stop_called_ && !end_of_stream_played_.load()) {
auto time_elapsed = SbTimeGetMonotonicNow() - stopped_at_;
auto time_elapsed = CurrentMonotonicTime() - stopped_at_;
auto frames_played =
time_elapsed * audio_stream_info_.samples_per_second / kSbTimeSecond;
time_elapsed * audio_stream_info_.samples_per_second / 1'000'000LL;
if (frames_played + playback_head_position_when_stopped_ >=
total_frames_written_on_audio_track_thread_) {
end_of_stream_played_.store(true);
Expand Down
19 changes: 9 additions & 10 deletions starboard/android/shared/audio_renderer_passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "starboard/shared/starboard/player/input_buffer_internal.h"
#include "starboard/shared/starboard/player/job_queue.h"
#include "starboard/shared/starboard/player/job_thread.h"
#include "starboard/time.h"

namespace starboard {
namespace android {
Expand Down Expand Up @@ -76,11 +75,11 @@ class AudioRendererPassthrough
void Play() override;
void Pause() override;
void SetPlaybackRate(double playback_rate) override;
void Seek(SbTime seek_to_time) override;
SbTime GetCurrentMediaTime(bool* is_playing,
bool* is_eos_played,
bool* is_underflow,
double* playback_rate) override;
void Seek(int64_t seek_to_time) override;
int64_t GetCurrentMediaTime(bool* is_playing,
bool* is_eos_played,
bool* is_underflow,
double* playback_rate) override;

private:
typedef ::starboard::shared::starboard::player::DecodedAudio DecodedAudio;
Expand All @@ -96,7 +95,7 @@ class AudioRendererPassthrough
};

void CreateAudioTrackAndStartProcessing();
void FlushAudioTrackAndStopProcessing(SbTime seek_to_time);
void FlushAudioTrackAndStopProcessing(int64_t seek_to_time);
void UpdateStatusAndWriteData(const AudioTrackState previous_state);
void OnDecoderConsumed();
void OnDecoderOutput();
Expand Down Expand Up @@ -126,9 +125,9 @@ class AudioRendererPassthrough
bool stop_called_ = false;
int64_t total_frames_written_ = 0;
int64_t playback_head_position_when_stopped_ = 0;
SbTimeMonotonic stopped_at_ = 0;
SbTime seek_to_time_ = 0;
SbTime first_audio_timestamp_ = -1;
int64_t stopped_at_ = 0; // microseconds
int64_t seek_to_time_ = 0; // microseconds
int64_t first_audio_timestamp_ = -1; // microseconds
double volume_ = 1.0;
bool paused_ = true;
double playback_rate_ = 1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void MinRequiredFramesTester::TesterThreadFunc() {
&MinRequiredFramesTester::ErrorFunc, 0, -1, false, this);
{
ScopedLock scoped_lock(mutex_);
wait_timeout = !condition_variable_.WaitTimed(kSbTimeSecond * 5);
wait_timeout = !condition_variable_.WaitTimed(5'000'000);
}

// Get start threshold before release the audio sink.
Expand Down Expand Up @@ -191,7 +191,7 @@ void MinRequiredFramesTester::UpdateSourceStatusFunc(int* frames_in_buffer,

// static
void MinRequiredFramesTester::ConsumeFramesFunc(int frames_consumed,
SbTime frames_consumed_at,
int64_t frames_consumed_at,
void* context) {
MinRequiredFramesTester* tester =
static_cast<MinRequiredFramesTester*>(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MinRequiredFramesTester {
bool* is_eos_reached,
void* context);
static void ConsumeFramesFunc(int frames_consumed,
SbTime frames_consumed_at,
int64_t frames_consumed_at,
void* context);
static void ErrorFunc(bool capability_changed,
const std::string& error_message,
Expand Down
Loading

0 comments on commit 6e1c894

Please sign in to comment.