Skip to content

Commit

Permalink
Implement setting httpheaders cookie (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyue7 authored Oct 27, 2023
1 parent 3fe4631 commit c505ae8
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/video_player_avplayer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ TV emulator support is experimental. DRM content playback is not supported on TV

The following options are not currently supported.

- The `httpHeaders` option of `VideoPlayerController.network`
- `VideoPlayerOptions.allowBackgroundPlayback`
- `VideoPlayerOptions.mixWithOthers`

This plugin has the following limitations.

- The `httpHeaders` option of `VideoPlayerController.network` only support `Cookie` and `User-Agent`.
- The `setPlaybackSpeed` method will fail if triggered within the last 3 seconds of the video.
- The playback speed will reset to 1.0 when the video is replayed in loop mode.
- The `seekTo` method works only when the playback speed is 1.0, and it sets the video position to the nearest keyframe, not the exact value passed.
Expand Down
31 changes: 30 additions & 1 deletion packages/video_player_avplayer/tizen/src/media_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ MediaPlayer::~MediaPlayer() { Dispose(); }

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

if (uri.empty()) {
Expand All @@ -47,6 +48,34 @@ int64_t MediaPlayer::Create(const std::string &uri, int drm_type,
return -1;
}

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));
}
}
}

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));
}
}
}
}

if (drm_type != 0) {
if (!SetDrm(uri, drm_type, license_server_url)) {
LOG_ERROR("[MediaPlayer] Failed to set drm.");
Expand Down
4 changes: 2 additions & 2 deletions packages/video_player_avplayer/tizen/src/media_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class MediaPlayer : public VideoPlayer {
~MediaPlayer();

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

void SetDisplayRoi(int32_t x, int32_t y, int32_t width,
Expand Down
21 changes: 20 additions & 1 deletion packages/video_player_avplayer/tizen/src/plus_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ PlusPlayer::~PlusPlayer() { Dispose(); }

int64_t PlusPlayer::Create(const std::string &uri, int drm_type,
const std::string &license_server_url,
bool is_prebuffer_mode) {
bool is_prebuffer_mode,
flutter::EncodableMap &http_headers) {
LOG_INFO("[PlusPlayer] Create player.");
if (!video_format_.compare("dash")) {
player_ = plusplayer::PlusPlayer::Create(plusplayer::PlayerType::kDASH);
Expand All @@ -46,6 +47,24 @@ int64_t PlusPlayer::Create(const std::string &uri, int drm_type,
return -1;
}

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);
player_->SetStreamingProperty("COOKIE", cookie);
}
}

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);
player_->SetStreamingProperty("USER_AGENT", user_agent);
}
}
}

if (!player_->Open(uri)) {
LOG_ERROR("[PlusPlayer] Fail to open uri : %s.", uri.c_str());
return -1;
Expand Down
4 changes: 2 additions & 2 deletions packages/video_player_avplayer/tizen/src/plus_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class PlusPlayer : public VideoPlayer, public plusplayer::EventListener {
~PlusPlayer();

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

void SetDisplayRoi(int32_t x, int32_t y, int32_t width,
Expand Down
3 changes: 2 additions & 1 deletion packages/video_player_avplayer/tizen/src/video_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class VideoPlayer {

virtual int64_t Create(const std::string &uri, int drm_type,
const std::string &license_server_url,
bool is_prebuffer_mode) = 0;
bool is_prebuffer_mode,
flutter::EncodableMap &http_headers) = 0;
virtual void Dispose() = 0;

virtual void SetDisplayRoi(int32_t x, int32_t y, int32_t width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ ErrorOr<PlayerMessage> VideoPlayerTizenPlugin::Create(
std::string license_server_url;
bool prebuffer_mode;
std::string format;
flutter::EncodableMap http_headers = {};

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

const flutter::EncodableMap *http_headers_map = msg.http_headers();
if (http_headers_map) {
http_headers = *http_headers_map;
}

} else {
return FlutterError("Invalid argument", "Either asset or uri must be set.");
}
Expand All @@ -169,14 +176,14 @@ ErrorOr<PlayerMessage> VideoPlayerTizenPlugin::Create(
if (uri.substr(0, 4) == "http") {
auto player = std::make_unique<PlusPlayer>(plugin_registrar_->messenger(),
native_window, format);
player_id =
player->Create(uri, drm_type, license_server_url, prebuffer_mode);
player_id = player->Create(uri, drm_type, license_server_url,
prebuffer_mode, http_headers);
players_[player_id] = std::move(player);
} else {
auto player = std::make_unique<MediaPlayer>(plugin_registrar_->messenger(),
native_window);
player_id =
player->Create(uri, drm_type, license_server_url, prebuffer_mode);
player_id = player->Create(uri, drm_type, license_server_url,
prebuffer_mode, http_headers);
players_[player_id] = std::move(player);
}

Expand Down

0 comments on commit c505ae8

Please sign in to comment.