Skip to content

Commit

Permalink
Extract a new class as shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowei-guan committed Dec 28, 2023
1 parent a38394e commit c1a9429
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@

using OnLicenseRequestDone = std::function<void(
const std::string& session_id, const std::vector<uint8_t>& response_data)>;
class DrmLicenseRequest
: public std::enable_shared_from_this<DrmLicenseRequest> {

struct DataForLicenseProcess {
DataForLicenseProcess(void* session_id, void* message, int message_length)
: session_id(static_cast<char*>(session_id)),
message(static_cast<char*>(message), message_length) {}
std::string session_id;
std::string message;
};
class DrmLicenseRequest {
public:
explicit DrmLicenseRequest(
OnLicenseRequestDone on_license_request_done_callback)
: on_license_request_done_callback_(on_license_request_done_callback) {}
virtual ~DrmLicenseRequest() {}
virtual void RequestLicense(void* session_id, int message_type, void* message,
int message_length) = 0;
virtual void OnLicenseResponse(const std::string& session_id,
const std::vector<uint8_t>& response_data) = 0;
void OnLicenseResponse(const std::string& session_id,
const std::vector<uint8_t>& response_data) {
if (on_license_request_done_callback_) {
on_license_request_done_callback_(session_id, response_data);
}
}

protected:
std::shared_ptr<DrmLicenseRequest> getShared() { return shared_from_this(); }
OnLicenseRequestDone on_license_request_done_callback_ = nullptr;
};
#endif // FLUTTER_PLUGIN_DRM_LICENSE_REQUEST_H_
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void DrmLicenseRequestChannel::RequestLicense(void *session_id,
int message_type, void *message,
int message_length) {
DataForLicenseProcess process_message(session_id, message, message_length);
PushLicenseRequestData(process_message);
PushLicenseRequest(process_message);
}

void DrmLicenseRequestChannel::ExecuteRequest() {
Expand All @@ -43,7 +43,7 @@ void DrmLicenseRequestChannel::ExecuteRequest() {
}
}

void DrmLicenseRequestChannel::PushLicenseRequestData(
void DrmLicenseRequestChannel::PushLicenseRequest(
const DataForLicenseProcess &data) {
std::lock_guard<std::mutex> lock(queue_mutex_);
license_request_queue_.push(data);
Expand Down Expand Up @@ -87,13 +87,6 @@ void DrmLicenseRequestChannel::RequestLicense(const std::string &session_id,
std::move(result_handler));
}

void DrmLicenseRequestChannel::OnLicenseResponse(
const std::string &session_id, const std::vector<uint8_t> &response_data) {
if (on_license_request_done_callback_) {
on_license_request_done_callback_(session_id, response_data);
}
}

DrmLicenseRequestChannel::~DrmLicenseRequestChannel() {
if (license_request_pipe_) {
ecore_pipe_del(license_request_pipe_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

#include "drm_license_request.h"

struct DataForLicenseProcess {
DataForLicenseProcess(void *session_id, void *message, int message_length)
: session_id(static_cast<char *>(session_id)),
message(static_cast<char *>(message), message_length) {}
std::string session_id;
std::string message;
};

class DrmLicenseRequestChannel : public DrmLicenseRequest {
public:
explicit DrmLicenseRequestChannel(
Expand All @@ -32,12 +24,10 @@ class DrmLicenseRequestChannel : public DrmLicenseRequest {
void RequestLicense(void *session_id, int message_type, void *message,
int message_length) override;
~DrmLicenseRequestChannel();
void OnLicenseResponse(const std::string &session_id,
const std::vector<uint8_t> &response_data) override;

private:
void ExecuteRequest();
void PushLicenseRequestData(const DataForLicenseProcess &data);
void PushLicenseRequest(const DataForLicenseProcess &data);
void RequestLicense(const std::string &session_id,
const std::string &message);
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,59 @@ constexpr int kMessageRequestLicense = 0;
struct Message {
Eina_Thread_Queue_Msg head;
int event;
std::string license_server_url;
std::string challenge;
std::string session_id;
int drm_type;
DataForLicenseProcess* data_for_request = nullptr;
};

LicenseResponsePipe::LicenseResponsePipe(
OnLicenseRequestDone on_license_request_done_callback)
: on_license_request_done_callback_(on_license_request_done_callback) {
license_response_pipe_ = ecore_pipe_add(
[](void* data, void* buffer, unsigned int nbyte) -> void {
auto* self = static_cast<LicenseResponsePipe*>(data);
self->ExecuteResponse();
},
this);
}

void LicenseResponsePipe::ExecuteResponse() {
std::lock_guard<std::mutex> lock(queue_mutex_);
while (!license_response_queue_.empty()) {
if (on_license_request_done_callback_) {
auto response_data = license_response_queue_.front();
on_license_request_done_callback_(response_data.first,
response_data.second);
}
license_response_queue_.pop();
}
}

void LicenseResponsePipe::PushLicenseResponse(
const std::string& session_id, const std::vector<uint8_t>& response_data) {
std::lock_guard<std::mutex> lock(queue_mutex_);
license_response_queue_.push(std::make_pair(session_id, response_data));
ecore_pipe_write(license_response_pipe_, nullptr, 0);
}

LicenseResponsePipe::~LicenseResponsePipe() {
if (license_response_pipe_) {
ecore_pipe_del(license_response_pipe_);
license_response_pipe_ = nullptr;
}
}

DrmLicenseRequestNative::DrmLicenseRequestNative(
int drm_type, const std::string& license_server_url,
OnLicenseRequestDone on_license_request_done_callback)
: DrmLicenseRequest(on_license_request_done_callback),
drm_type_(drm_type),
license_server_url_(license_server_url) {
license_response_pipe_ = std::make_shared<LicenseResponsePipe>(
[this](const std::string& session_id,
const std::vector<uint8_t>& response_data) {
OnLicenseResponse(session_id, response_data);
});
license_request_thread_ = ecore_thread_feedback_run(RunLoop, nullptr, nullptr,
nullptr, this, EINA_TRUE);
license_response_pipe_ = ecore_pipe_add(
[](void* data, void* buffer, unsigned int nbyte) -> void {
auto* self = static_cast<DrmLicenseRequestNative*>(data);
self->ExecuteResponse();
},
this);
}

void DrmLicenseRequestNative::RequestLicense(void* session_id, int message_type,
Expand All @@ -55,11 +88,8 @@ void DrmLicenseRequestNative::RequestLicense(void* session_id, int message_type,
Message* request_message = static_cast<Message*>(
eina_thread_queue_send(license_request_queue_, sizeof(Message), &ref));
request_message->event = kMessageRequestLicense;
request_message->drm_type = drm_type_;
request_message->challenge =
std::string(static_cast<char*>(message), message_length);
request_message->license_server_url = license_server_url_;
request_message->session_id = std::string(static_cast<char*>(session_id));
request_message->data_for_request =
new DataForLicenseProcess(session_id, message, message_length);
eina_thread_queue_send_done(license_request_queue_, ref);
}

Expand Down Expand Up @@ -90,19 +120,25 @@ void DrmLicenseRequestNative::RunLoop(void* data, Ecore_Thread* thread) {
}
auto* self = static_cast<DrmLicenseRequestNative*>(data);
self->license_request_queue_ = license_request_queue;
std::weak_ptr<DrmLicenseRequest> weak_self = self->getShared();
std::weak_ptr<LicenseResponsePipe> weak_pipe = self->license_response_pipe_;
std::string license_server_url = self->license_server_url_;
int drm_type = self->drm_type_;
while (!ecore_thread_check(thread)) {
void* ref;
Message* message = static_cast<Message*>(
eina_thread_queue_wait(license_request_queue, &ref));
if (message->event == kMessageQuit) {
LOG_DEBUG("Receive kMessageQuit message");
eina_thread_queue_wait_done(license_request_queue, ref);
break;
}
std::string license_server_url = message->license_server_url;
std::string challenge = message->challenge;
std::string session_id = message->session_id;
int drm_type = message->drm_type;
if (message->data_for_request == nullptr) {
LOG_ERROR("data_for_request is null");
continue;
}
std::string challenge = message->data_for_request->message;
std::string session_id = message->data_for_request->session_id;
delete message->data_for_request;
eina_thread_queue_wait_done(license_request_queue, ref);
// Get license via the license server.
unsigned char* response_data = nullptr;
Expand All @@ -112,44 +148,27 @@ void DrmLicenseRequestNative::RunLoop(void* data, Ecore_Thread* thread) {
license_server_url.c_str(), challenge.c_str(), challenge.size(),
&response_data, &response_len,
static_cast<DrmLicenseHelper::DrmType>(drm_type), nullptr, nullptr);

if (DRM_SUCCESS != ret || nullptr == response_data || 0 == response_len) {
LOG_ERROR("Fail to get respone by license server url.");
continue;
}
LOG_INFO("Response length : %lu", response_len);
if (weak_self.expired()) {
if (weak_pipe.expired()) {
LOG_DEBUG("weak_pipe expired");
free(response_data);
break;
}
auto response_vec =
std::vector<uint8_t>(response_data, response_data + response_len);
weak_self.lock()->OnLicenseResponse(session_id, response_vec);
weak_pipe.lock()->PushLicenseResponse(session_id, response_vec);
free(response_data);
}
if (license_request_queue) {
eina_thread_queue_free(license_request_queue);
}
}

void DrmLicenseRequestNative::ExecuteResponse() {
std::lock_guard<std::mutex> lock(queue_mutex_);
while (!license_response_queue_.empty()) {
if (on_license_request_done_callback_) {
auto response_data = license_response_queue_.front();
on_license_request_done_callback_(response_data.first,
response_data.second);
}
license_response_queue_.pop();
}
}

void DrmLicenseRequestNative::OnLicenseResponse(
const std::string& session_id, const std::vector<uint8_t>& response_data) {
std::lock_guard<std::mutex> lock(queue_mutex_);
license_response_queue_.push(std::make_pair(session_id, response_data));
ecore_pipe_write(license_response_pipe_, nullptr, 0);
}

DrmLicenseRequestNative::~DrmLicenseRequestNative() {
StopMessageQueue();
if (license_request_thread_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@

#include "drm_license_request.h"

class LicenseResponsePipe {
public:
explicit LicenseResponsePipe(
OnLicenseRequestDone on_license_request_done_callback);
virtual ~LicenseResponsePipe();
void ExecuteResponse();
void PushLicenseResponse(const std::string& session_id,
const std::vector<uint8_t>& response_data);

private:
std::mutex queue_mutex_;
Ecore_Pipe* license_response_pipe_ = nullptr;
std::queue<std::pair<std::string, std::vector<uint8_t>>>
license_response_queue_;
OnLicenseRequestDone on_license_request_done_callback_ = nullptr;
};

class DrmLicenseRequestNative : public DrmLicenseRequest {
public:
explicit DrmLicenseRequestNative(
Expand All @@ -24,22 +41,14 @@ class DrmLicenseRequestNative : public DrmLicenseRequest {
void RequestLicense(void* session_id, int message_type, void* message,
int message_length) override;

protected:
void OnLicenseResponse(const std::string& session_id,
const std::vector<uint8_t>& response_data) override;

private:
void StopMessageQueue();
void ExecuteResponse();
static void RunLoop(void* data, Ecore_Thread* thread);
void StopMessageQueue();
int drm_type_;
Ecore_Thread* license_request_thread_ = nullptr;
Eina_Thread_Queue* license_request_queue_ = nullptr;
int drm_type_;
std::string license_server_url_;
std::mutex queue_mutex_;
Ecore_Pipe* license_response_pipe_ = nullptr;
std::queue<std::pair<std::string, std::vector<uint8_t>>>
license_response_queue_;
std::shared_ptr<LicenseResponsePipe> license_response_pipe_;
};

#endif
4 changes: 2 additions & 2 deletions packages/video_player_videohole/tizen/src/drm/drm_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool DrmManager::CreateDrmSession(int drm_type, bool local_mode) {

bool DrmManager::SetChallenge(const std::string &media_url,
flutter::BinaryMessenger *binary_messenger) {
drm_license_request_ = std::make_shared<DrmLicenseRequestChannel>(
drm_license_request_ = std::make_unique<DrmLicenseRequestChannel>(
binary_messenger, [this](const std::string &session_id,
const std::vector<uint8_t> &response_data) {
InstallKey(session_id, response_data);
Expand All @@ -95,7 +95,7 @@ bool DrmManager::SetChallenge(const std::string &media_url,
bool DrmManager::SetChallenge(const std::string &media_url,
const std::string &license_server_url) {
license_server_url_ = license_server_url;
drm_license_request_ = std::make_shared<DrmLicenseRequestNative>(
drm_license_request_ = std::make_unique<DrmLicenseRequestNative>(
drm_type_, license_server_url,
[this](const std::string &session_id,
const std::vector<uint8_t> &response_data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DrmManager {
std::string license_server_url_;
bool initialized_ = false;

std::shared_ptr<DrmLicenseRequest> drm_license_request_;
std::unique_ptr<DrmLicenseRequest> drm_license_request_;
};

#endif // FLUTTER_PLUGIN_DRM_MANAGER_H_

0 comments on commit c1a9429

Please sign in to comment.