Skip to content

Commit

Permalink
[video_player_avplay] Minor refactor (flutter-tizen#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowei-guan authored Feb 18, 2024
1 parent fc42419 commit a893eeb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 109 deletions.
4 changes: 4 additions & 0 deletions packages/video_player_avplay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

* Minor refactor.

## 0.3.3

* Check httper_headers pointer when creating media player.
Expand Down
77 changes: 34 additions & 43 deletions packages/video_player_avplay/tizen/src/media_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,54 +86,45 @@ int64_t MediaPlayer::Create(const std::string &uri,
return -1;
}

if (create_message.http_headers() != nullptr &&
!create_message.http_headers()->empty()) {
std::string cookie = flutter_common::GetValue(create_message.http_headers(),
"Cookie", std::string());
if (!cookie.empty()) {
int ret =
player_set_streaming_cookie(player_, cookie.c_str(), cookie.size());
if (ret != PLAYER_ERROR_NONE) {
LOG_ERROR("[MediaPlayer] player_set_streaming_cookie failed: %s.",
get_error_message(ret));
}
}
std::string user_agent = flutter_common::GetValue(
create_message.http_headers(), "User-Agent", std::string());
if (!user_agent.empty()) {
int ret = player_set_streaming_user_agent(player_, user_agent.c_str(),
user_agent.size());
if (ret != PLAYER_ERROR_NONE) {
LOG_ERROR("[MediaPlayer] player_set_streaming_user_agent failed: %s.",
get_error_message(ret));
}
std::string cookie = flutter_common::GetValue(create_message.http_headers(),
"Cookie", std::string());
if (!cookie.empty()) {
int ret =
player_set_streaming_cookie(player_, cookie.c_str(), cookie.size());
if (ret != PLAYER_ERROR_NONE) {
LOG_ERROR("[MediaPlayer] player_set_streaming_cookie failed: %s.",
get_error_message(ret));
}
}

if (create_message.streaming_property() != nullptr &&
!create_message.streaming_property()->empty()) {
std::string adaptive_info = flutter_common::GetValue(
create_message.streaming_property(), "ADAPTIVE_INFO", std::string());
if (!adaptive_info.empty()) {
media_player_proxy_->player_set_adaptive_streaming_info(
player_,
const_cast<void *>(
reinterpret_cast<const void *>(adaptive_info.c_str())),
PLAYER_ADAPTIVE_INFO_URL_CUSTOM);
std::string user_agent = flutter_common::GetValue(
create_message.http_headers(), "User-Agent", std::string());
if (!user_agent.empty()) {
int ret = player_set_streaming_user_agent(player_, user_agent.c_str(),
user_agent.size());
if (ret != PLAYER_ERROR_NONE) {
LOG_ERROR("[MediaPlayer] player_set_streaming_user_agent failed: %s.",
get_error_message(ret));
}
}

if (create_message.drm_configs() != nullptr &&
!create_message.drm_configs()->empty()) {
int drm_type =
flutter_common::GetValue(create_message.drm_configs(), "drmType", 0);
std::string license_server_url = flutter_common::GetValue(
create_message.drm_configs(), "licenseServerUrl", std::string());
if (drm_type != 0) {
if (!SetDrm(uri, drm_type, license_server_url)) {
LOG_ERROR("[MediaPlayer] Fail to set drm.");
return -1;
}
std::string adaptive_info = flutter_common::GetValue(
create_message.streaming_property(), "ADAPTIVE_INFO", std::string());
if (!adaptive_info.empty()) {
media_player_proxy_->player_set_adaptive_streaming_info(
player_,
const_cast<void *>(
reinterpret_cast<const void *>(adaptive_info.c_str())),
PLAYER_ADAPTIVE_INFO_URL_CUSTOM);
}

int drm_type =
flutter_common::GetValue(create_message.drm_configs(), "drmType", 0);
std::string license_server_url = flutter_common::GetValue(
create_message.drm_configs(), "licenseServerUrl", std::string());
if (drm_type != 0) {
if (!SetDrm(uri, drm_type, license_server_url)) {
LOG_ERROR("[MediaPlayer] Fail to set drm.");
return -1;
}
}

Expand Down
79 changes: 36 additions & 43 deletions packages/video_player_avplay/tizen/src/plus_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ static plusplayer::TrackType ConvertTrackType(std::string track_type) {
if (track_type == "text") {
return plusplayer::TrackType::kTrackTypeSubtitle;
}
return plusplayer::TrackType::kTrackTypeMax;
}

PlusPlayer::PlusPlayer(flutter::BinaryMessenger *messenger,
FlutterDesktopViewRef flutter_view,
std::string &video_format)
: VideoPlayer(messenger, flutter_view), video_format_(video_format) {}
FlutterDesktopViewRef flutter_view)
: VideoPlayer(messenger, flutter_view) {}

PlusPlayer::~PlusPlayer() {
if (player_) {
Expand Down Expand Up @@ -70,7 +70,13 @@ int64_t PlusPlayer::Create(const std::string &uri,
const CreateMessage &create_message) {
LOG_INFO("[PlusPlayer] Create player.");

if (video_format_ == "dash") {
std::string video_format;

if (create_message.format_hint() && !create_message.format_hint()->empty()) {
video_format = *create_message.format_hint();
}

if (video_format == "dash") {
player_ = CreatePlayer(plusplayer::PlayerType::kDASH);
} else {
player_ = CreatePlayer(plusplayer::PlayerType::kDefault);
Expand All @@ -81,27 +87,20 @@ int64_t PlusPlayer::Create(const std::string &uri,
return -1;
}

if (create_message.http_headers() != nullptr &&
!create_message.http_headers()->empty()) {
std::string cookie = flutter_common::GetValue(create_message.http_headers(),
"Cookie", std::string());
if (!cookie.empty()) {
SetStreamingProperty(player_, "COOKIE", cookie);
}
std::string user_agent = flutter_common::GetValue(
create_message.http_headers(), "User-Agent", std::string());
if (!user_agent.empty()) {
SetStreamingProperty(player_, "USER_AGENT", user_agent);
}
std::string cookie = flutter_common::GetValue(create_message.http_headers(),
"Cookie", std::string());
if (!cookie.empty()) {
SetStreamingProperty(player_, "COOKIE", cookie);
}

if (create_message.streaming_property() != nullptr &&
!create_message.streaming_property()->empty()) {
std::string adaptive_info = flutter_common::GetValue(
create_message.streaming_property(), "ADAPTIVE_INFO", std::string());
if (!adaptive_info.empty()) {
SetStreamingProperty(player_, "ADAPTIVE_INFO", adaptive_info);
}
std::string user_agent = flutter_common::GetValue(
create_message.http_headers(), "User-Agent", std::string());
if (!user_agent.empty()) {
SetStreamingProperty(player_, "USER_AGENT", user_agent);
}
std::string adaptive_info = flutter_common::GetValue(
create_message.streaming_property(), "ADAPTIVE_INFO", std::string());
if (!adaptive_info.empty()) {
SetStreamingProperty(player_, "ADAPTIVE_INFO", adaptive_info);
}

if (!Open(player_, uri)) {
Expand All @@ -121,17 +120,14 @@ int64_t PlusPlayer::Create(const std::string &uri,

RegisterListener();

if (create_message.drm_configs() != nullptr &&
!create_message.drm_configs()->empty()) {
int drm_type =
flutter_common::GetValue(create_message.drm_configs(), "drmType", 0);
std::string license_server_url = flutter_common::GetValue(
create_message.drm_configs(), "licenseServerUrl", std::string());
if (drm_type != 0) {
if (!SetDrm(uri, drm_type, license_server_url)) {
LOG_ERROR("[PlusPlayer] Fail to set drm.");
return -1;
}
int drm_type =
flutter_common::GetValue(create_message.drm_configs(), "drmType", 0);
std::string license_server_url = flutter_common::GetValue(
create_message.drm_configs(), "licenseServerUrl", std::string());
if (drm_type != 0) {
if (!SetDrm(uri, drm_type, license_server_url)) {
LOG_ERROR("[PlusPlayer] Fail to set drm.");
return -1;
}
}

Expand All @@ -142,14 +138,11 @@ int64_t PlusPlayer::Create(const std::string &uri,

SetDisplayRoi(0, 0, 1, 1);

if (create_message.player_options() != nullptr &&
!create_message.player_options()->empty()) {
bool is_prebuffer_mode = flutter_common::GetValue(
create_message.player_options(), "prebufferMode", false);
if (is_prebuffer_mode) {
SetPrebufferMode(player_, true);
is_prebuffer_mode_ = true;
}
bool is_prebuffer_mode = flutter_common::GetValue(
create_message.player_options(), "prebufferMode", false);
if (is_prebuffer_mode) {
SetPrebufferMode(player_, true);
is_prebuffer_mode_ = true;
}

if (!PrepareAsync(player_)) {
Expand Down
4 changes: 1 addition & 3 deletions packages/video_player_avplay/tizen/src/plus_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
class PlusPlayer : public VideoPlayer {
public:
explicit PlusPlayer(flutter::BinaryMessenger *messenger,
FlutterDesktopViewRef flutter_view,
std::string &video_format);
FlutterDesktopViewRef flutter_view);
~PlusPlayer();

int64_t Create(const std::string &uri,
Expand Down Expand Up @@ -82,7 +81,6 @@ class PlusPlayer : public VideoPlayer {
PlusplayerRef player_ = nullptr;
PlusplayerListener listener_;
std::unique_ptr<DrmManager> drm_manager_;
std::string video_format_;
bool is_buffering_ = false;
bool is_prebuffer_mode_ = false;
SeekCompletedCallback on_seek_completed_;
Expand Down
6 changes: 5 additions & 1 deletion packages/video_player_avplay/tizen/src/video_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ namespace flutter_common {
template <typename T>
inline const T GetValue(const flutter::EncodableMap *map,
const std::string &key, T &&default_value) {
if (map == nullptr || map->empty()) {
return std::move(default_value);
}

auto it = map->find(flutter::EncodableValue(key));
if (it != map->end() && std::holds_alternative<T>(it->second)) {
return std::get<T>(it->second);
}
return default_value;
return std::move(default_value);
}

} // namespace flutter_common
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ ErrorOr<PlayerMessage> VideoPlayerTizenPlugin::Create(
return FlutterError("Operation failed", "Could not get a Flutter view.");
}
std::string uri;
std::string format;

if (msg.asset() && !msg.asset()->empty()) {
char *res_path = app_get_resource_path();
Expand All @@ -121,34 +120,25 @@ ErrorOr<PlayerMessage> VideoPlayerTizenPlugin::Create(
}
} else if (msg.uri() && !msg.uri()->empty()) {
uri = *msg.uri();
if (msg.format_hint() && !msg.format_hint()->empty()) {
format = *msg.format_hint();
}
} else {
return FlutterError("Invalid argument", "Either asset or uri must be set.");
}

int64_t player_id = 0;
std::unique_ptr<VideoPlayer> player = nullptr;
if (uri.substr(0, 4) == "http") {
auto player = std::make_unique<PlusPlayer>(
player = std::make_unique<PlusPlayer>(
plugin_registrar_->messenger(),
FlutterDesktopPluginRegistrarGetView(registrar_ref_), format);
player_id = player->Create(uri, msg);
if (player_id == -1) {
return FlutterError("Operation failed", "Failed to create a player.");
}
players_[player_id] = std::move(player);
FlutterDesktopPluginRegistrarGetView(registrar_ref_));
} else {
auto player = std::make_unique<MediaPlayer>(
player = std::make_unique<MediaPlayer>(
plugin_registrar_->messenger(),
FlutterDesktopPluginRegistrarGetView(registrar_ref_));
player_id = player->Create(uri, msg);
if (player_id == -1) {
return FlutterError("Operation failed", "Failed to create a player.");
}
players_[player_id] = std::move(player);
}

int64_t player_id = player->Create(uri, msg);
if (player_id == -1) {
return FlutterError("Operation failed", "Failed to create a player.");
}
players_[player_id] = std::move(player);
PlayerMessage result(player_id);
return result;
}
Expand Down

0 comments on commit a893eeb

Please sign in to comment.