Skip to content

Commit

Permalink
Extract a method to get value from EncodableMap
Browse files Browse the repository at this point in the history
  • Loading branch information
hyue7 committed Nov 21, 2023
1 parent 3c59fd3 commit 9725753
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
50 changes: 28 additions & 22 deletions packages/video_player_videohole/tizen/src/video_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@

static int64_t player_index = 1;

template <typename T>
static bool GetValueFromEncodableMap(const flutter::EncodableMap *map,
const char *key, T &out) {
auto iter = map->find(flutter::EncodableValue(key));
if (iter != map->end()) {
if (std::holds_alternative<T>(iter->second)) {
out = std::get<T>(iter->second);
return true;
}
}
return false;
}

VideoPlayer::VideoPlayer(flutter::PluginRegistrar *plugin_registrar,
void *native_window)
: plugin_registrar_(plugin_registrar), native_window_(native_window) {
Expand Down Expand Up @@ -122,7 +135,7 @@ bool VideoPlayer::SetDisplay() {

int64_t VideoPlayer::Create(const std::string &uri, int drm_type,
const std::string &license_server_url,
flutter::EncodableMap &http_headers) {
const flutter::EncodableMap *http_headers) {
LOG_INFO("[VideoPlayer] uri: %s, drm_type: %d", uri.c_str(), drm_type);

player_id_ = player_index++;
Expand Down Expand Up @@ -152,30 +165,23 @@ int64_t VideoPlayer::Create(const std::string &uri, int drm_type,
}
}

if (!http_headers.empty()) {
auto iter = http_headers.find(flutter::EncodableValue("Cookie"));
if (iter != http_headers.end()) {
if (std::holds_alternative<std::string>(iter->second)) {
std::string cookie = std::get<std::string>(iter->second);
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 (http_headers && !http_headers->empty()) {
std::string cookie;
if (GetValueFromEncodableMap(http_headers, "Cookie", cookie)) {
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));
}
}

iter = http_headers.find(flutter::EncodableValue("User-Agent"));
if (iter != http_headers.end()) {
if (std::holds_alternative<std::string>(iter->second)) {
std::string user_agent = std::get<std::string>(iter->second);
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 user_agent;
if (GetValueFromEncodableMap(http_headers, "User-Agent", user_agent)) {
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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/video_player_videohole/tizen/src/video_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class VideoPlayer {

int64_t Create(const std::string &uri, int drm_type,
const std::string &license_server_url,
flutter::EncodableMap &http_headers);
const flutter::EncodableMap *http_headers);
void Dispose();

void SetDisplayRoi(int32_t x, int32_t y, int32_t width, int32_t height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ ErrorOr<PlayerMessage> VideoPlayerTizenPlugin::Create(
std::string uri;
int32_t drm_type = 0; // DRM_TYPE_NONE
std::string license_server_url;
flutter::EncodableMap http_headers = {};
const flutter::EncodableMap *http_headers = nullptr;

if (msg.asset() && !msg.asset()->empty()) {
char *res_path = app_get_resource_path();
Expand Down Expand Up @@ -147,10 +147,7 @@ ErrorOr<PlayerMessage> VideoPlayerTizenPlugin::Create(
}
}

const flutter::EncodableMap *http_headers_map = msg.http_headers();
if (http_headers_map) {
http_headers = *http_headers_map;
}
http_headers = msg.http_headers();
} else {
return FlutterError("Invalid argument", "Either asset or uri must be set.");
}
Expand Down

0 comments on commit 9725753

Please sign in to comment.