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

Fix non-platform thread issue. #622

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
4 changes: 4 additions & 0 deletions packages/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.4.9

* Fix event channel issue, sending messages from native to Flutter on the platform thread.

## 2.4.8

* Disable screensaver when player is playing.
Expand Down
2 changes: 1 addition & 1 deletion packages/video_player/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This package is not an _endorsed_ implementation of `video_player`. Therefore, y
```yaml
dependencies:
video_player: ^2.4.2
video_player_tizen: ^2.4.8
video_player_tizen: ^2.4.9
```

Then you can import `video_player` in your Dart code:
Expand Down
2 changes: 1 addition & 1 deletion packages/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: video_player_tizen
description: Tizen implementation of the video_player plugin.
homepage: https://github.com/flutter-tizen/plugins
repository: https://github.com/flutter-tizen/plugins/tree/master/packages/video_player
version: 2.4.8
version: 2.4.9

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
83 changes: 62 additions & 21 deletions packages/video_player/tizen/src/video_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ void VideoPlayer::InitScreenSaverApi() {
VideoPlayer::VideoPlayer(flutter::PluginRegistrar *plugin_registrar,
flutter::TextureRegistrar *texture_registrar,
const std::string &uri, VideoPlayerOptions &options) {
sink_event_pipe_ = ecore_pipe_add(
[](void *data, void *buffer, unsigned int nbyte) -> void {
auto *self = static_cast<VideoPlayer *>(data);
self->ExecuteSinkEvents();
},
this);

texture_registrar_ = texture_registrar;

texture_variant_ =
Expand Down Expand Up @@ -205,6 +212,45 @@ VideoPlayer::~VideoPlayer() {
}
}

void VideoPlayer::ExecuteSinkEvents() {
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
std::lock_guard<std::mutex> lock(queue_mutex_);
while (!encodable_event_queue_.empty()) {
if (event_sink_) {
event_sink_->Success(encodable_event_queue_.front());
}
encodable_event_queue_.pop();
}

while (!error_event_queue_.empty()) {
if (event_sink_) {
event_sink_->Error(error_event_queue_.front().first,
error_event_queue_.front().second);
}
error_event_queue_.pop();
}
}

void VideoPlayer::PushEvent(flutter::EncodableValue encodable_value) {
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
if (event_sink_ == nullptr) {
swift-kim marked this conversation as resolved.
Show resolved Hide resolved
LOG_ERROR("[VideoPlayer] event sink is nullptr.");
return;
}
std::lock_guard<std::mutex> lock(queue_mutex_);
encodable_event_queue_.push(encodable_value);
ecore_pipe_write(sink_event_pipe_, nullptr, 0);
}

void VideoPlayer::SendError(const std::string &error_code,
const std::string &error_message) {
if (event_sink_ == nullptr) {
LOG_ERROR("[VideoPlayer] event sink is nullptr.");
return;
}
std::lock_guard<std::mutex> lock(queue_mutex_);
error_event_queue_.push(std::make_pair(error_code, error_message));
ecore_pipe_write(sink_event_pipe_, nullptr, 0);
}

void VideoPlayer::Play() {
LOG_DEBUG("[VideoPlayer] Player starting.");

Expand Down Expand Up @@ -306,6 +352,11 @@ void VideoPlayer::Dispose() {

std::lock_guard<std::mutex> lock(mutex_);
is_initialized_ = false;

if (sink_event_pipe_) {
ecore_pipe_del(sink_event_pipe_);
}

event_sink_ = nullptr;
event_channel_->SetStreamHandler(nullptr);

Expand Down Expand Up @@ -384,25 +435,23 @@ void VideoPlayer::SendInitialized() {
int duration = 0;
int ret = player_get_duration(player_, &duration);
if (ret != PLAYER_ERROR_NONE) {
event_sink_->Error("player_get_duration failed", get_error_message(ret));
SendError("player_get_duration failed", get_error_message(ret));
return;
}
LOG_DEBUG("[VideoPlayer] Video duration: %d", duration);

int width = 0, height = 0;
ret = player_get_video_size(player_, &width, &height);
if (ret != PLAYER_ERROR_NONE) {
event_sink_->Error("player_get_video_size failed",
get_error_message(ret));
SendError("player_get_video_size failed", get_error_message(ret));
return;
}
LOG_DEBUG("[VideoPlayer] Video width: %d, height: %d", width, height);

player_display_rotation_e rotation = PLAYER_DISPLAY_ROTATION_NONE;
ret = player_get_display_rotation(player_, &rotation);
if (ret != PLAYER_ERROR_NONE) {
event_sink_->Error("player_get_display_rotation failed",
get_error_message(ret));
SendError("player_get_display_rotation failed", get_error_message(ret));
} else {
LOG_DEBUG("[VideoPlayer] rotation: %s",
RotationToString(rotation).c_str());
Expand All @@ -421,7 +470,7 @@ void VideoPlayer::SendInitialized() {
{flutter::EncodableValue("width"), flutter::EncodableValue(width)},
{flutter::EncodableValue("height"), flutter::EncodableValue(height)},
};
event_sink_->Success(flutter::EncodableValue(result));
PushEvent(flutter::EncodableValue(result));
}
}

Expand Down Expand Up @@ -468,13 +517,10 @@ void VideoPlayer::OnPlayCompleted(void *data) {
LOG_DEBUG("[VideoPlayer] Play completed.");

auto *player = static_cast<VideoPlayer *>(data);
if (player->event_sink_) {
flutter::EncodableMap result = {
{flutter::EncodableValue("event"),
flutter::EncodableValue("completed")},
};
player->event_sink_->Success(flutter::EncodableValue(result));
}
flutter::EncodableMap result = {
{flutter::EncodableValue("event"), flutter::EncodableValue("completed")},
};
player->PushEvent(flutter::EncodableValue(result));

player->Pause();
}
Expand All @@ -483,21 +529,16 @@ void VideoPlayer::OnInterrupted(player_interrupted_code_e code, void *data) {
LOG_ERROR("[VideoPlayer] Interrupt code: %d", code);

auto *player = static_cast<VideoPlayer *>(data);
if (player->event_sink_) {
player->event_sink_->Error("Interrupted error",
"Video player has been interrupted.");
}
player->SendError("Interrupted error", "Video player has been interrupted.");
}

void VideoPlayer::OnError(int error_code, void *data) {
LOG_ERROR("[VideoPlayer] Error code: %d (%s)", error_code,
get_error_message(error_code));

auto *player = static_cast<VideoPlayer *>(data);
if (player->event_sink_) {
player->event_sink_->Error(
"Player error", std::string("Error: ") + get_error_message(error_code));
}
player->SendError("Player error",
std::string("Error: ") + get_error_message(error_code));
}

void VideoPlayer::OnVideoFrameDecoded(media_packet_h packet, void *data) {
Expand Down
9 changes: 9 additions & 0 deletions packages/video_player/tizen/src/video_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class VideoPlayer {
int64_t GetTextureId() { return texture_id_; }

private:
void ExecuteSinkEvents();
void PushEvent(flutter::EncodableValue encodable_value);
void SendError(const std::string &error_code,
const std::string &error_message);
FlutterDesktopGpuSurfaceDescriptor *ObtainGpuSurface(size_t width,
size_t height);

Expand Down Expand Up @@ -89,6 +93,11 @@ class VideoPlayer {
void *screensaver_handle_;
ScreensaverResetTimeout screensaver_reset_timeout_;
Ecore_Timer *timer_;

Ecore_Pipe *sink_event_pipe_ = nullptr;
std::mutex queue_mutex_;
std::queue<flutter::EncodableValue> encodable_event_queue_;
std::queue<std::pair<std::string, std::string>> error_event_queue_;
};

#endif // FLUTTER_PLUGIN_VIDEO_PLAYER_H_
Loading