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

Expose frame early time to Px #4609

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions starboard/android/shared/system_get_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "starboard/android/shared/platform_service.h"
#include "starboard/android/shared/player_set_max_video_input_size.h"
#include "starboard/android/shared/system_info_api.h"
#include "starboard/android/shared/video_render_algorithm.h"
#include "starboard/common/log.h"
#include "starboard/common/string.h"
#if SB_IS(EVERGREEN_COMPATIBLE)
Expand All @@ -42,6 +43,7 @@
#include "starboard/extension/platform_service.h"
#include "starboard/extension/player_set_max_video_input_size.h"
#include "starboard/extension/system_info.h"
#include "starboard/extension/video_render_algorithm.h"

const void* SbSystemGetExtension(const char* name) {
#if SB_IS(EVERGREEN_COMPATIBLE)
Expand Down Expand Up @@ -103,5 +105,8 @@ const void* SbSystemGetExtension(const char* name) {
if (strcmp(name, kStarboardExtensionSystemInfoName) == 0) {
return starboard::android::shared::GetSystemInfoApi();
}
if (strcmp(name, kStarboardExtensionVideoRenderAlgorithmName) == 0) {
return starboard::android::shared::GetAvgVideoRenderAlgorithmApi();
}
return NULL;
}
31 changes: 31 additions & 0 deletions starboard/android/shared/video_render_algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
#include "starboard/android/shared/jni_utils.h"
#include "starboard/android/shared/media_common.h"
#include "starboard/common/log.h"
#include "starboard/extension/video_render_algorithm.h"

namespace starboard {
namespace android {
namespace shared {

long VideoRenderAlgorithm::circular_early_times_arr_[kCircularEarlyTimeSamples];
int VideoRenderAlgorithm::circular_sample_pos_ = 0;

namespace {

const int64_t kBufferTooLateThreshold = -32'000; // -32ms
Expand Down Expand Up @@ -104,6 +108,11 @@ void VideoRenderAlgorithm::Render(

early_us = (adjusted_release_time_ns - system_time_ns) / 1000;

circular_early_times_arr_[circular_sample_pos_] = early_us;
if (++circular_sample_pos_ >= kCircularEarlyTimeSamples) {
circular_sample_pos_ = 0;
}

if (early_us < kBufferTooLateThreshold) {
frames->pop_front();
++dropped_frames_;
Expand Down Expand Up @@ -160,6 +169,28 @@ jlong VideoRenderAlgorithm::VideoFrameReleaseTimeHelper::AdjustReleaseTime(
frame_presentation_time_us, unadjusted_release_time_ns, playback_rate);
}

int64_t GetAvgFrameEarlyTime() {
int64_t total = 0;
for (int i = 0; i < kCircularEarlyTimeSamples; i++) {
total += VideoRenderAlgorithm::circular_early_times_arr_[i] /
kCircularEarlyTimeSamples;
}
return total;
}

// Definitions of any functions included as components in the extension
// are added here.

const StarboardExtensionVideoRenderAlgorithmApi kVideoRenderAlgorithmApi = {
kStarboardExtensionVideoRenderAlgorithmName,
1, // API version that's implemented.
&GetAvgFrameEarlyTime,
};

const void* GetAvgVideoRenderAlgorithmApi() {
return &kVideoRenderAlgorithmApi;
}

} // namespace shared
} // namespace android
} // namespace starboard
8 changes: 8 additions & 0 deletions starboard/android/shared/video_render_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace starboard {
namespace android {
namespace shared {

const int kCircularEarlyTimeSamples = 30; // average early time for 30 frames

class VideoRenderAlgorithm : public ::starboard::shared::starboard::player::
filter::VideoRenderAlgorithm {
public:
Expand All @@ -36,6 +38,7 @@ class VideoRenderAlgorithm : public ::starboard::shared::starboard::player::
VideoRendererSink::DrawFrameCB draw_frame_cb) override;
void Seek(int64_t seek_to_time) override;
int GetDroppedFrames() override;
friend int64_t GetAvgFrameEarlyTime();

private:
class VideoFrameReleaseTimeHelper {
Expand All @@ -55,8 +58,13 @@ class VideoRenderAlgorithm : public ::starboard::shared::starboard::player::
double playback_rate_ = 1.0;
VideoFrameReleaseTimeHelper video_frame_release_time_helper_;
int dropped_frames_ = 0;
static long circular_early_times_arr_[kCircularEarlyTimeSamples];
static int circular_sample_pos_;
};

const void* GetAvgVideoRenderAlgorithmApi();
int64_t GetAvgFrameEarlyTime();

} // namespace shared
} // namespace android
} // namespace starboard
Expand Down
21 changes: 21 additions & 0 deletions starboard/extension/extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "starboard/extension/time_zone.h"
#include "starboard/extension/updater_notification.h"
#include "starboard/extension/url_fetcher_observer.h"
#include "starboard/extension/video_render_algorithm.h"
#include "starboard/system.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -622,5 +623,25 @@ TEST(ExtensionTest, StarboardSystemInfoExtension) {
<< "Extension struct should be a singleton";
}

TEST(ExtensionTest, StarboardViedoRenderAlgorithmGetEarlyTimeExtension) {
typedef StarboardExtensionVideoRenderAlgorithmApi ExtensionApi;
const char* kExtensionName = kStarboardExtensionVideoRenderAlgorithmName;

const ExtensionApi* extension_api =
static_cast<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
if (!extension_api) {
return;
}

EXPECT_STREQ(extension_api->name, kExtensionName);
EXPECT_EQ(extension_api->version, 1u);
EXPECT_NE(extension_api->GetAvgFrameEarlyTime, nullptr);

const ExtensionApi* second_extension_api =
static_cast<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
EXPECT_EQ(second_extension_api, extension_api)
<< "Extension struct should be a singleton";
}

} // namespace extension
} // namespace starboard
44 changes: 44 additions & 0 deletions starboard/extension/video_render_algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 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.

#ifndef STARBOARD_EXTENSION_VIDEO_RENDER_ALGORITHM_H_
#define STARBOARD_EXTENSION_VIDEO_RENDER_ALGORITHM_H_

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define kStarboardExtensionVideoRenderAlgorithmName \
"dev.starboard.extension.VideoRenderAlgorithm"

typedef struct StarboardExtensionVideoRenderAlgorithmApi {
// Name should be the string |kStarboardExtensionVideoRenderName|.
// This helps to validate that the extension API is correct.
const char* name;

// This specifies the version of the API that is implemented.
uint32_t version;

// The fields below this point were added in version 1 or later.

int64_t (*GetAvgFrameEarlyTime)();
} StarboardExtensionVideoRenderAlgorithmApi;

#ifdef __cplusplus
} // extern "C"
#endif

#endif // STARBOARD_EXTENSION_VIDEO_RENDER_ALGORITHM_H_
Loading