Skip to content

Commit

Permalink
Disable MediaPlaybackService by default.
Browse files Browse the repository at this point in the history
Use H5vcc.settings.set(Media.BackgroundPlaybackEnabled, 1) to
enable it.

b/319146388
  • Loading branch information
Colin Liang committed Jan 10, 2024
1 parent aef8f7b commit baed17c
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>

<head>
<title>Test Enable Background Playback</title>
</head>

<body style="color: white;">
<script>
function main() {
if (window.h5vcc && window.h5vcc.settings) {
h5vcc.settings.set('Media.BackgroundPlaybackEnabled', 1);
}
}
main();
</script>
</body>
12 changes: 12 additions & 0 deletions cobalt/media/media_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "base/synchronization/waitable_event.h"
#include "cobalt/media/base/format_support_query_metrics.h"
#include "starboard/common/string.h"
#include "starboard/extension/h5vcc_config.h"
#include "starboard/media.h"
#include "starboard/window.h"
#include "third_party/chromium/media/base/mime_util.h"
Expand Down Expand Up @@ -209,6 +210,17 @@ bool MediaModule::SetConfiguration(const std::string& name, int32 value) {
LOG(INFO) << "Set AudioWriteDurationRemote to "
<< audio_write_duration_remote_;
return true;
} else if (name == "BackgroundPlaybackEnabled" && value > 0) {
const StarboardExtensionH5vccConfigApi* h5vcc_config_api_ =
static_cast<const StarboardExtensionH5vccConfigApi*>(
SbSystemGetExtension(kStarboardExtensionH5vccConfigName));
if (h5vcc_config_api_) {
bool enable_background_playback = value;
LOG(INFO) << "Set BackgroundPlaybackEnabled to "
<< (enable_background_playback ? "enabled" : "disabled");
h5vcc_config_api_->EnableBackgroundPlayback(enable_background_playback);
}
return true;
#endif // SB_API_VERSION >= 15
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public void run() {
private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("America/Los_Angeles");
private final long timeNanosecondsPerMicrosecond = 1000;

public static boolean enableBackgroundPlayback = false;

public StarboardBridge(
Context appContext,
Holder<Activity> activityHolder,
Expand Down Expand Up @@ -189,6 +191,11 @@ protected void onServiceDestroy(Service service) {
@SuppressWarnings("unused")
@UsedByNative
protected void startMediaPlaybackService() {
if (!enableBackgroundPlayback) {
Log.v(TAG, "Media Playback Service is disabled. Skip startMediaPlaybackService().");
return;
}

if (cobaltMediaSession == null || !cobaltMediaSession.isActive()) {
Log.w(TAG, "Do not start a MediaPlaybackService when the MediSsession is null or inactive.");
return;
Expand Down Expand Up @@ -221,6 +228,11 @@ protected void startMediaPlaybackService() {
@SuppressWarnings("unused")
@UsedByNative
protected void stopMediaPlaybackService() {
if (!enableBackgroundPlayback) {
Log.v(TAG, "Media Playback Service is disabled. Skip stopMediaPlaybackService().");
return;
}

Service service = serviceHolder.get();
if (service != null) {
Log.i(TAG, "Stopping the MediaPlaybackService.");
Expand Down Expand Up @@ -846,4 +858,11 @@ protected boolean getIsAmatiDevice() {
protected String getBuildFingerprint() {
return Build.FINGERPRINT;
}

@SuppressWarnings("unused")
@UsedByNative
protected void enableBackgroundPlayback(boolean value) {
enableBackgroundPlayback = value;
Log.v(TAG, "StarboardBridge set enableBackgroundPlayback: %b", value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.support.v4.media.session.PlaybackStateCompat;
import android.view.WindowManager;
import androidx.annotation.RequiresApi;
import dev.cobalt.coat.StarboardBridge;
import dev.cobalt.util.Holder;
import dev.cobalt.util.Log;

Expand Down Expand Up @@ -129,6 +130,10 @@ public CobaltMediaSession(
}

public boolean isActive() {
if (!StarboardBridge.enableBackgroundPlayback) {
return false;
}

if (this.mediaSession == null) {
return false;
} else {
Expand Down Expand Up @@ -254,7 +259,9 @@ private void configureMediaFocus(int playbackState, long positionMs, float speed
Log.i(TAG, "MediaSession already released");
return;
}
mediaSession.setActive(playbackState != PLAYBACK_STATE_NONE);
if (StarboardBridge.enableBackgroundPlayback) {
mediaSession.setActive(playbackState != PLAYBACK_STATE_NONE);
}
if (lifecycleCallback != null) {
lifecycleCallback.onMediaSessionLifecycle(
this.mediaSession.isActive(), this.mediaSession.getSessionToken());
Expand Down
2 changes: 2 additions & 0 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ static_library("starboard_platform") {
"get_home_directory.cc",
"graphics.cc",
"graphics.h",
"h5vcc_config.cc",
"h5vcc_config.h",
"input_events_generator.cc",
"input_events_generator.h",
"jni_env_ext.cc",
Expand Down
50 changes: 50 additions & 0 deletions starboard/android/shared/h5vcc_config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.

#include "starboard/extension/h5vcc_config.h"
#include "starboard/android/shared/h5vcc_config.h"
#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"
#include "starboard/common/log.h"

namespace starboard {
namespace android {
namespace shared {

namespace {

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

void EnableBackgroundPlayback(bool value) {
JniEnvExt* env = JniEnvExt::Get();
JniEnvExt::Get()->CallStarboardVoidMethodOrAbort("enableBackgroundPlayback",
"(Z)V", (jboolean)value);
}

const StarboardExtensionH5vccConfigApi kH5vccConfigApi = {
kStarboardExtensionH5vccConfigName,
1, // API version that's implemented.
&EnableBackgroundPlayback,
};

} // namespace

const void* GetH5vccConfigApi() {
return &kH5vccConfigApi;
}

} // namespace shared
} // namespace android
} // namespace starboard
29 changes: 29 additions & 0 deletions starboard/android/shared/h5vcc_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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_ANDROID_SHARED_H5VCC_CONFIG_H_
#define STARBOARD_ANDROID_SHARED_H5VCC_CONFIG_H_

// Omit namespace linux due to symbol name conflict.
namespace starboard {
namespace android {
namespace shared {

const void* GetH5vccConfigApi();

} // namespace shared
} // namespace android
} // namespace starboard

#endif // STARBOARD_ANDROID_SHARED_H5VCC_CONFIG_H_
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 @@ -17,6 +17,7 @@
#include "starboard/android/shared/android_media_session_client.h"
#include "starboard/android/shared/configuration.h"
#include "starboard/android/shared/graphics.h"
#include "starboard/android/shared/h5vcc_config.h"
#include "starboard/android/shared/platform_info.h"
#include "starboard/android/shared/platform_service.h"
#include "starboard/common/log.h"
Expand All @@ -30,6 +31,7 @@
#include "starboard/extension/configuration.h"
#include "starboard/extension/crash_handler.h"
#include "starboard/extension/graphics.h"
#include "starboard/extension/h5vcc_config.h"
#include "starboard/extension/media_session.h"
#include "starboard/extension/platform_info.h"
#include "starboard/extension/platform_service.h"
Expand Down Expand Up @@ -68,5 +70,8 @@ const void* SbSystemGetExtension(const char* name) {
if (strcmp(name, kCobaltExtensionPlatformInfoName) == 0) {
return starboard::android::shared::GetPlatformInfoApi();
}
if (strcmp(name, kStarboardExtensionH5vccConfigName) == 0) {
return starboard::android::shared::GetH5vccConfigApi();
}
return NULL;
}
21 changes: 21 additions & 0 deletions starboard/extension/extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "starboard/extension/font.h"
#include "starboard/extension/free_space.h"
#include "starboard/extension/graphics.h"
#include "starboard/extension/h5vcc_config.h"
#include "starboard/extension/ifa.h"
#include "starboard/extension/installation_manager.h"
#include "starboard/extension/javascript_cache.h"
Expand Down Expand Up @@ -481,5 +482,25 @@ TEST(ExtensionTest, Ifa) {
<< "Extension struct should be a singleton";
}

TEST(ExtensionTest, H5vccConfig) {
typedef StarboardExtensionH5vccConfigApi ExtensionApi;
const char* kExtensionName = kStarboardExtensionH5vccConfigName;

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->SetTimeZone, 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
43 changes: 43 additions & 0 deletions starboard/extension/h5vcc_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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_H5VCC_CONFIG_H_
#define STARBOARD_EXTENSION_H5VCC_CONFIG_H_

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define kStarboardExtensionH5vccConfigName "dev.starboard.extension.H5vccConfig"

typedef struct StarboardExtensionH5vccConfigApi {
// Name should be the string |kStarboardExtensionSetTimeZoneName|.
// 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;

// This API enable the Cobalt background playback mode on Android TV.
void (*EnableBackgroundPlayback)(bool value);

} StarboardExtensionH5vccConfigApi;

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

#endif // STARBOARD_EXTENSION_H5VCC_CONFIG_H_

0 comments on commit baed17c

Please sign in to comment.