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

[video_player] Disable screensaver when player is playing #610

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
123 changes: 78 additions & 45 deletions packages/video_player/tizen/src/video_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <dlfcn.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/standard_method_codec.h>
#include <system_info.h>
swift-kim marked this conversation as resolved.
Show resolved Hide resolved

#include <algorithm>

Expand Down Expand Up @@ -43,6 +44,18 @@ static std::string StateToString(player_state_e state) {
return std::string();
}

static std::string GetPlatformVersion() {
char *version = nullptr;
std::string value;
const char *key = "http://tizen.org/feature/platform.version";
int ret = system_info_get_platform_string(key, &version);
if (ret == SYSTEM_INFO_ERROR_NONE) {
value = std::string(version);
free(version);
}
return value;
}

void VideoPlayer::ReleaseMediaPacket(void *data) {
auto *player = static_cast<VideoPlayer *>(data);

Expand Down Expand Up @@ -79,6 +92,35 @@ FlutterDesktopGpuSurfaceDescriptor *VideoPlayer::ObtainGpuSurface(
return gpu_surface_.get();
}

void VideoPlayer::InitScreenSaverApi() {
screensaver_handle_ = dlopen("libcapi-screensaver.so", RTLD_LAZY);
if (!screensaver_handle_) {
LOG_INFO("[VideoPlayer] dlopen failed: %s", dlerror());
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
return;
}

screensaver_reset_timeout_ = reinterpret_cast<ScreensaverResetTimeout>(
dlsym(screensaver_handle_, "screensaver_reset_timeout"));

double version = atof(GetPlatformVersion().c_str());
if (version > 6.0) {
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
ScreensaverOverrideReset screensaver_override_reset =
reinterpret_cast<ScreensaverOverrideReset>(
dlsym(screensaver_handle_, "screensaver_override_reset"));

if (!screensaver_override_reset) {
LOG_INFO("[VideoPlayer] Symbol not found: %s", dlerror());
dlclose(screensaver_handle_);
return;
}
int ret = screensaver_override_reset(false);
if (ret != 0) {
throw VideoPlayerError("screensaver_override_reset failed",
get_error_message(ret));
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

VideoPlayer::VideoPlayer(flutter::PluginRegistrar *plugin_registrar,
flutter::TextureRegistrar *texture_registrar,
const std::string &uri, VideoPlayerOptions &options) {
Expand Down Expand Up @@ -156,6 +198,9 @@ VideoPlayer::VideoPlayer(flutter::PluginRegistrar *plugin_registrar,
get_error_message(ret));
}

#ifdef TV_PROFILE
InitScreenSaverApi();
#endif
SetUpEventChannel(plugin_registrar->messenger());
}

Expand Down Expand Up @@ -189,49 +234,9 @@ void VideoPlayer::Play() {
if (ret != PLAYER_ERROR_NONE) {
throw VideoPlayerError("player_start failed", get_error_message(ret));
}

timer = ecore_timer_add(30, ScreenSaverBlock, NULL);
}

Eina_Bool VideoPlayer::ScreenSaverBlock(void *data) {
LOG_DEBUG("[VideoPlayer] Screen saver blocking.");

void *handle = dlopen("libcapi-screensaver.so", RTLD_LAZY);
if (!handle) {
LOG_INFO("[VideoPlayer] dlopen failed: %s", dlerror());
return ECORE_CALLBACK_CANCEL;
}

ScreensaverOverrideReset screensaver_override_reset =
reinterpret_cast<ScreensaverOverrideReset>(
dlsym(handle, "screensaver_override_reset"));
if (!screensaver_override_reset) {
LOG_INFO("[VideoPlayer] Symbol not found: %s", dlerror());
dlclose(handle);
return ECORE_CALLBACK_CANCEL;
}
int ret = screensaver_override_reset(false);
if (ret != 0) {
throw VideoPlayerError("screensaver_override_reset failed",
get_error_message(ret));
}

ScreensaverResetTimeout screensaver_reset_timeout =
reinterpret_cast<ScreensaverResetTimeout>(
dlsym(handle, "screensaver_reset_timeout"));
if (!screensaver_reset_timeout) {
LOG_INFO("[VideoPlayer] Symbol not found: %s", dlerror());
dlclose(handle);
return ECORE_CALLBACK_CANCEL;
}
ret = screensaver_reset_timeout();
if (ret != 0) {
throw VideoPlayerError("screensaver_reset_timeout failed",
get_error_message(ret));
}

dlclose(handle);
return ECORE_CALLBACK_RENEW;
#ifdef TV_PROFILE
timer_ = ecore_timer_add(30, ResetScreensaverTimeout, this);
#endif
}

void VideoPlayer::Pause() {
Expand All @@ -251,9 +256,9 @@ void VideoPlayer::Pause() {
throw VideoPlayerError("player_pause failed", get_error_message(ret));
}

if (timer) {
if (timer_) {
LOG_DEBUG("[VideoPlayer] Delete ecore timer.");
ecore_timer_del(timer);
ecore_timer_del(timer_);
}
}

Expand Down Expand Up @@ -334,6 +339,14 @@ void VideoPlayer::Dispose() {
texture_registrar_->UnregisterTexture(texture_id_, nullptr);
texture_registrar_ = nullptr;
}

if (screensaver_handle_) {
dlclose(screensaver_handle_);
}

if (timer_) {
ecore_timer_del(timer_);
}
}

void VideoPlayer::SetUpEventChannel(flutter::BinaryMessenger *messenger) {
Expand Down Expand Up @@ -422,6 +435,26 @@ void VideoPlayer::SendInitialized() {
}
}

Eina_Bool VideoPlayer::ResetScreensaverTimeout(void *data) {
LOG_DEBUG("[VideoPlayer] Reset screen saver timeout.");

auto *player = static_cast<VideoPlayer *>(data);
if (!player->screensaver_reset_timeout_) {
LOG_INFO("[VideoPlayer] Symbol not found: %s", dlerror());
dlclose(player->screensaver_handle_);
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
return ECORE_CALLBACK_CANCEL;
}

int ret = player->screensaver_reset_timeout_();
if (ret != 0) {
LOG_INFO("screensaver_reset_timeout failed", get_error_message(ret));
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
dlclose(player->screensaver_handle_);
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
return ECORE_CALLBACK_CANCEL;
}

return ECORE_CALLBACK_RENEW;
}

void VideoPlayer::OnPrepared(void *data) {
LOG_DEBUG("[VideoPlayer] Player prepared.");

Expand Down
8 changes: 6 additions & 2 deletions packages/video_player/tizen/src/video_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class VideoPlayer {
void SetUpEventChannel(flutter::BinaryMessenger *messenger);
void Initialize();
void SendInitialized();
void InitScreenSaverApi();

static void OnPrepared(void *data);
static void OnBuffering(int percent, void *data);
Expand All @@ -59,7 +60,7 @@ class VideoPlayer {
static void OnError(int error_code, void *data);
static void OnVideoFrameDecoded(media_packet_h packet, void *data);
static void ReleaseMediaPacket(void *packet);
static Eina_Bool ScreenSaverBlock(void *data);
static Eina_Bool ResetScreensaverTimeout(void *data);

void RequestRendering();
void OnRenderingCompleted();
Expand All @@ -84,7 +85,10 @@ class VideoPlayer {
std::queue<media_packet_h> packet_queue_;

SeekCompletedCallback on_seek_completed_;
Ecore_Timer *timer;

void *screensaver_handle_;
ScreensaverResetTimeout screensaver_reset_timeout_;
Ecore_Timer *timer_;
};

#endif // FLUTTER_PLUGIN_VIDEO_PLAYER_H_