// Copyright 2013 Google Inc. All Rights Reserved. // Review the TestHost_4 class below to observe how the CDM interfaces with // the host application. #include "test_host_4.h" #include #include #include "test_host_4_file_io.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_4::TestHost_4() : current_time_(GetCurrentTime()), has_new_session_message_(false), has_new_session_error_(false), cdm_(NULL) {} TestHost_4::~TestHost_4() { if (cdm_) cdm_->Destroy(); } cdm::Buffer* TestHost_4::Allocate(uint32_t capacity) { return TestBuffer::Create(capacity); } void TestHost_4::SetTimer(int64_t delay_ms, void* context) { double expiry_time = current_time_ + (delay_ms / 1000.0); timers_.push(Timer(expiry_time, context)); } double TestHost_4::GetCurrentWallTimeInSeconds() { return current_time_; } void TestHost_4::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); } } } int TestHost_4::SessionMessagesSize() const { return session_messages_.size(); } int TestHost_4::SessionErrorsSize() const { return session_errors_.size(); } int TestHost_4::NumTimers() const { return timers_.size(); } TestHost_4::SessionMessage TestHost_4::GetLastSessionMessage() { if (!has_new_session_message_) { return SessionMessage(); } if (session_messages_.empty()) { return SessionMessage(); } has_new_session_message_ = false; return session_messages_.back(); } TestHost_4::SessionError TestHost_4::GetLastSessionError() { if (!has_new_session_error_) return SessionError(); if (session_errors_.empty()) return SessionError(); has_new_session_error_ = false; return session_errors_.back(); } TestHost_4::SessionMessage TestHost_4::GetSessionMessage(int index) const { return session_messages_[index]; } TestHost_4::SessionError TestHost_4::GetSessionError(int index) const { return session_errors_[index]; } void TestHost_4::SetCdmPtr(cdm::ContentDecryptionModule_4* cdm) { if (cdm_) { cdm_->Destroy(); } cdm_ = cdm; } void TestHost_4::OnSessionCreated(uint32_t session_id, const char* web_session_id, uint32_t web_session_id_length) { std::string webid(web_session_id, web_session_id_length); session_map[session_id] = webid; // keep a parallel map with cdm. } void TestHost_4::OnSessionMessage(uint32_t session_id, const char* message, uint32_t message_length, const char* destination_url, uint32_t destination_url_length) { SessionMessage session_message; session_message.session_id = session_id; session_message.message.assign(message, message_length); session_message.default_url.assign(destination_url, destination_url_length); session_messages_.push_back(session_message); has_new_session_message_ = true; } void TestHost_4::OnSessionUpdated(uint32_t session_id) {} void TestHost_4::OnSessionClosed(uint32_t session_id) { session_map.erase(session_id); } void TestHost_4::OnSessionError(uint32_t session_id, cdm::Status error_code, uint32_t system_code) { SessionError session_error; session_error.session_id = session_id; session_error.error_code = error_code; session_error.system_code = system_code; session_errors_.push_back(session_error); has_new_session_error_ = true; } cdm::FileIO* TestHost_4::CreateFileIO(cdm::FileIOClient* client) { return new TestHost_4_FileIO(this, client); }