Skip to content

Commit

Permalink
Expose frame early time to Px
Browse files Browse the repository at this point in the history
Calculate average frame early time of the past 30 frames, and return
the result through starboard extension upon request.

b/362607402
  • Loading branch information
maxz-lab committed Dec 18, 2024
1 parent 05ae7c0 commit e13c93a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
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_

0 comments on commit e13c93a

Please sign in to comment.