| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromecast/media/cdm/cast_cdm_proxy.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "media/base/cdm_key_information.h" | |
| 15 #include "media/base/cdm_promise.h" | |
| 16 | |
| 17 namespace chromecast { | |
| 18 namespace media { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // media::CdmPromiseTemplate implementation that wraps a promise so as to | |
| 23 // allow passing to other threads. | |
| 24 template <typename... T> | |
| 25 class CdmPromiseInternal : public ::media::CdmPromiseTemplate<T...> { | |
| 26 public: | |
| 27 CdmPromiseInternal(std::unique_ptr<::media::CdmPromiseTemplate<T...>> promise) | |
| 28 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 29 promise_(std::move(promise)) {} | |
| 30 | |
| 31 ~CdmPromiseInternal() final { | |
| 32 if (IsPromiseSettled()) | |
| 33 return; | |
| 34 | |
| 35 DCHECK(promise_); | |
| 36 RejectPromiseOnDestruction(); | |
| 37 } | |
| 38 | |
| 39 // CdmPromiseTemplate<> implementation. | |
| 40 void resolve(const T&... result) final; | |
| 41 | |
| 42 void reject(::media::CdmPromise::Exception exception, | |
| 43 uint32_t system_code, | |
| 44 const std::string& error_message) final { | |
| 45 MarkPromiseSettled(); | |
| 46 task_runner_->PostTask( | |
| 47 FROM_HERE, base::Bind(&::media::CdmPromiseTemplate<T...>::reject, | |
| 48 base::Owned(promise_.release()), exception, | |
| 49 system_code, error_message)); | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 using ::media::CdmPromiseTemplate<T...>::IsPromiseSettled; | |
| 54 using ::media::CdmPromiseTemplate<T...>::MarkPromiseSettled; | |
| 55 using ::media::CdmPromiseTemplate<T...>::RejectPromiseOnDestruction; | |
| 56 | |
| 57 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 58 std::unique_ptr<::media::CdmPromiseTemplate<T...>> promise_; | |
| 59 }; | |
| 60 | |
| 61 template <typename... T> | |
| 62 void CdmPromiseInternal<T...>::resolve(const T&... result) { | |
| 63 MarkPromiseSettled(); | |
| 64 task_runner_->PostTask( | |
| 65 FROM_HERE, base::Bind(&::media::CdmPromiseTemplate<T...>::resolve, | |
| 66 base::Owned(promise_.release()), result...)); | |
| 67 } | |
| 68 | |
| 69 template <typename... T> | |
| 70 std::unique_ptr<CdmPromiseInternal<T...>> BindPromiseToCurrentLoop( | |
| 71 std::unique_ptr<::media::CdmPromiseTemplate<T...>> promise) { | |
| 72 return base::MakeUnique<CdmPromiseInternal<T...>>(std::move(promise)); | |
| 73 } | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 // A macro runs current member function on |task_runner_| thread. | |
| 78 #define FORWARD_ON_CDM_THREAD(param_fn, ...) \ | |
| 79 task_runner_->PostTask( \ | |
| 80 FROM_HERE, base::Bind(&CastCdm::param_fn, \ | |
| 81 base::Unretained(cast_cdm_.get()), ##__VA_ARGS__)) | |
| 82 | |
| 83 CastCdmProxy::CastCdmProxy( | |
| 84 const scoped_refptr<CastCdm>& cast_cdm, | |
| 85 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | |
| 86 : cast_cdm_(cast_cdm), task_runner_(task_runner) {} | |
| 87 | |
| 88 CastCdmProxy::~CastCdmProxy() { | |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 90 cast_cdm_->AddRef(); | |
| 91 CastCdm* raw_cdm = cast_cdm_.get(); | |
| 92 cast_cdm_ = nullptr; | |
| 93 task_runner_->ReleaseSoon(FROM_HERE, raw_cdm); | |
| 94 } | |
| 95 | |
| 96 CastCdm* CastCdmProxy::cast_cdm() const { | |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 98 return cast_cdm_.get(); | |
| 99 } | |
| 100 | |
| 101 void CastCdmProxy::SetServerCertificate( | |
| 102 const std::vector<uint8_t>& certificate, | |
| 103 std::unique_ptr<::media::SimpleCdmPromise> promise) { | |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 105 FORWARD_ON_CDM_THREAD( | |
| 106 SetServerCertificate, certificate, | |
| 107 base::Passed(BindPromiseToCurrentLoop(std::move(promise)))); | |
| 108 } | |
| 109 | |
| 110 void CastCdmProxy::CreateSessionAndGenerateRequest( | |
| 111 ::media::MediaKeys::SessionType session_type, | |
| 112 ::media::EmeInitDataType init_data_type, | |
| 113 const std::vector<uint8_t>& init_data, | |
| 114 std::unique_ptr<::media::NewSessionCdmPromise> promise) { | |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 116 FORWARD_ON_CDM_THREAD( | |
| 117 CreateSessionAndGenerateRequest, session_type, init_data_type, init_data, | |
| 118 base::Passed(BindPromiseToCurrentLoop(std::move(promise)))); | |
| 119 } | |
| 120 | |
| 121 void CastCdmProxy::LoadSession( | |
| 122 ::media::MediaKeys::SessionType session_type, | |
| 123 const std::string& session_id, | |
| 124 std::unique_ptr<::media::NewSessionCdmPromise> promise) { | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 126 FORWARD_ON_CDM_THREAD( | |
| 127 LoadSession, session_type, session_id, | |
| 128 base::Passed(BindPromiseToCurrentLoop(std::move(promise)))); | |
| 129 } | |
| 130 | |
| 131 void CastCdmProxy::UpdateSession( | |
| 132 const std::string& session_id, | |
| 133 const std::vector<uint8_t>& response, | |
| 134 std::unique_ptr<::media::SimpleCdmPromise> promise) { | |
| 135 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 136 FORWARD_ON_CDM_THREAD( | |
| 137 UpdateSession, session_id, response, | |
| 138 base::Passed(BindPromiseToCurrentLoop(std::move(promise)))); | |
| 139 } | |
| 140 | |
| 141 void CastCdmProxy::CloseSession( | |
| 142 const std::string& session_id, | |
| 143 std::unique_ptr<::media::SimpleCdmPromise> promise) { | |
| 144 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 145 FORWARD_ON_CDM_THREAD( | |
| 146 CloseSession, session_id, | |
| 147 base::Passed(BindPromiseToCurrentLoop(std::move(promise)))); | |
| 148 } | |
| 149 | |
| 150 void CastCdmProxy::RemoveSession( | |
| 151 const std::string& session_id, | |
| 152 std::unique_ptr<::media::SimpleCdmPromise> promise) { | |
| 153 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 154 FORWARD_ON_CDM_THREAD( | |
| 155 RemoveSession, session_id, | |
| 156 base::Passed(BindPromiseToCurrentLoop(std::move(promise)))); | |
| 157 } | |
| 158 | |
| 159 ::media::CdmContext* CastCdmProxy::GetCdmContext() { | |
| 160 // This will be recast as a CastCdmService pointer before being passed to the | |
| 161 // media pipeline. The returned object should only be called on the CMA | |
| 162 // renderer thread. | |
| 163 return cast_cdm_->GetCdmContext(); | |
| 164 } | |
| 165 | |
| 166 } // namespace media | |
| 167 } // namespace chromecast | |
| OLD | NEW |