135 lines
3.6 KiB
C++
135 lines
3.6 KiB
C++
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
// Review the TestHost_1 class below to observe how the CDM interfaces with
|
|
// the host application.
|
|
|
|
#include "test_host_1.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <sys/time.h>
|
|
|
|
#include "test_util.h"
|
|
|
|
static double GetCurrentTime() {
|
|
struct timeval tv;
|
|
tv.tv_sec = tv.tv_usec = 0;
|
|
gettimeofday(&tv, NULL);
|
|
return tv.tv_sec + (tv.tv_usec / (1000.0 * 1000.0));
|
|
}
|
|
|
|
TestHost_1::TestHost_1()
|
|
: current_time_(GetCurrentTime()),
|
|
has_new_key_message_(false),
|
|
has_new_key_error_(false),
|
|
cdm_(NULL) {
|
|
}
|
|
|
|
TestHost_1::~TestHost_1() {
|
|
if (cdm_)
|
|
cdm_->Destroy();
|
|
}
|
|
|
|
cdm::Buffer* TestHost_1::Allocate(int32_t capacity) {
|
|
return TestBuffer::Create(capacity);
|
|
}
|
|
|
|
void TestHost_1::SetTimer(int64_t delay_ms, void* context) {
|
|
double expiry_time = current_time_ + (delay_ms / 1000.0);
|
|
timers_.push(Timer(expiry_time, context));
|
|
}
|
|
|
|
double TestHost_1::GetCurrentWallTimeInSeconds() {
|
|
return current_time_;
|
|
}
|
|
|
|
void TestHost_1::SendKeyMessage(const char* session_id,
|
|
int32_t session_id_length, const char* message,
|
|
int32_t message_length, const char* default_url,
|
|
int32_t default_url_length) {
|
|
KeyMessage key_message;
|
|
key_message.session_id.assign(session_id, session_id_length);
|
|
key_message.message.assign(message, message_length);
|
|
key_message.default_url.assign(default_url, default_url_length);
|
|
key_messages_.push_back(key_message);
|
|
has_new_key_message_ = true;
|
|
}
|
|
|
|
void TestHost_1::SendKeyError(const char* session_id, int32_t session_id_length,
|
|
cdm::MediaKeyError error_code,
|
|
uint32_t system_code) {
|
|
KeyError key_error;
|
|
key_error.session_id.assign(session_id, session_id_length);
|
|
key_error.error_code = error_code;
|
|
key_error.system_code = system_code;
|
|
key_errors_.push_back(key_error);
|
|
has_new_key_error_ = true;
|
|
}
|
|
|
|
void TestHost_1::FastForwardTime(double seconds) {
|
|
double goal_time = current_time_ + seconds;
|
|
while (current_time_ < goal_time) {
|
|
if (timers_.empty()) {
|
|
current_time_ = goal_time;
|
|
} else {
|
|
Timer t = timers_.top();
|
|
timers_.pop();
|
|
ASSERT_GE(t.expiry_time, current_time_);
|
|
current_time_ = t.expiry_time;
|
|
cdm_->TimerExpired(t.context);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TestHost_1::GetPlatformString(const std::string& name,
|
|
std::string* value) {
|
|
*value = platform_strings_[name];
|
|
}
|
|
|
|
void TestHost_1::SetPlatformString(const std::string& name,
|
|
const std::string& value) {
|
|
platform_strings_[name] = value;
|
|
}
|
|
|
|
int TestHost_1::KeyMessagesSize() const { return key_messages_.size(); }
|
|
|
|
int TestHost_1::KeyErrorsSize() const { return key_errors_.size(); }
|
|
|
|
int TestHost_1::NumTimers() const { return timers_.size(); }
|
|
|
|
TestHost_1::KeyMessage TestHost_1::GetLastKeyMessage() {
|
|
if (!has_new_key_message_) {
|
|
return KeyMessage();
|
|
}
|
|
|
|
if (key_messages_.empty()) {
|
|
return KeyMessage();
|
|
}
|
|
|
|
has_new_key_message_ = false;
|
|
return key_messages_.back();
|
|
}
|
|
|
|
TestHost_1::KeyError TestHost_1::GetLastKeyError() {
|
|
if (!has_new_key_error_) return KeyError();
|
|
|
|
if (key_errors_.empty()) return KeyError();
|
|
|
|
has_new_key_error_ = false;
|
|
return key_errors_.back();
|
|
}
|
|
|
|
TestHost_1::KeyMessage TestHost_1::GetKeyMessage(int index) const {
|
|
return key_messages_[index];
|
|
}
|
|
|
|
TestHost_1::KeyError TestHost_1::GetKeyError(int index) const {
|
|
return key_errors_[index];
|
|
}
|
|
|
|
void TestHost_1::SetCdmPtr(cdm::ContentDecryptionModule_1* cdm) {
|
|
if (cdm_) {
|
|
cdm_->Destroy();
|
|
}
|
|
cdm_ = cdm;
|
|
}
|