Shaka Player Embedded
media_keys.cc
Go to the documentation of this file.
1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/js/eme/media_keys.h"
16 
18 #include "src/js/js_error.h"
19 #include "src/mapping/convert_js.h"
20 
21 namespace shaka {
22 namespace js {
23 namespace eme {
24 
25 MediaKeys::MediaKeys(std::shared_ptr<ImplementationFactory> factory,
26  const std::string& key_system,
27  const MediaKeySystemConfiguration& config)
28  : key_system(key_system), helper_(key_system, this), factory_(factory) {
29  std::vector<std::string> audio_robustness, video_robustness;
30 
31  for (auto& item : config.audioCapabilities) {
32  audio_robustness.push_back(item.robustness);
33  }
34  for (auto& item : config.videoCapabilities) {
35  video_robustness.push_back(item.robustness);
36  }
37 
38  implementation_ = factory_->CreateImplementation(
39  &helper_, config.distinctiveIdentifier, config.persistentState,
40  audio_robustness, video_robustness);
41 }
42 
43 // \cond Doxygen_Skip
44 MediaKeys::~MediaKeys() {}
45 // \endcond Doxygen_Skip
46 
47 void MediaKeys::Trace(memory::HeapTracer* tracer) const {
48  std::unique_lock<std::mutex> lock(mutex_);
49 
50  BackingObject::Trace(tracer);
51  tracer->Trace(&sessions_);
52 }
53 
54 bool MediaKeys::valid() const {
55  return implementation_ != nullptr;
56 }
57 
59  optional<MediaKeySessionType> session_type) {
61  session_type.value_or(MediaKeySessionType::Temporary);
62  if (!factory_->SupportsSessionType(type)) {
64  "The given session type is not supported.");
65  }
66 
67  std::unique_lock<std::mutex> lock(mutex_);
68  RefPtr<MediaKeySession> session =
69  new MediaKeySession(type, factory_, implementation_);
70  sessions_.emplace_back(session);
71  return session;
72 }
73 
76  implementation_->SetServerCertificate(EmePromise(ret, /* has_value */ true),
77  Data(&cert));
78  return ret;
79 }
80 
81 RefPtr<MediaKeySession> MediaKeys::GetSession(const std::string& session_id) {
82  std::unique_lock<std::mutex> lock(mutex_);
83  for (auto& session : sessions_) {
84  if (session->SessionId() == session_id)
85  return session;
86  }
87  return nullptr;
88 }
89 
90 
92  AddMemberFunction("createSession", &MediaKeys::CreateSession);
93  AddMemberFunction("setServerCertificate", &MediaKeys::SetServerCertificate);
94 }
95 
96 } // namespace eme
97 } // namespace js
98 } // namespace shaka
Promise SetServerCertificate(ByteBuffer cert)
Definition: media_keys.cc:74
void Trace(memory::HeapTracer *tracer) const override
Definition: media_keys.cc:47
void Trace(memory::HeapTracer *tracer) const override
MediaKeys(std::shared_ptr< ImplementationFactory > factory, const std::string &key_system, const MediaKeySystemConfiguration &config)
Definition: media_keys.cc:25
ExceptionCode type
void Trace(const Traceable *ptr)
Definition: heap_tracer.cc:43
T value_or(U &&default_value) const &
Definition: optional.h:165
RefPtr< MediaKeySession > GetSession(const std::string &session_id)
Definition: media_keys.cc:81
static Promise PendingPromise()
Definition: promise.h:54
ExceptionOr< RefPtr< MediaKeySession > > CreateSession(optional< MediaKeySessionType > session_type)
Definition: media_keys.cc:58
static JsError DOMException(ExceptionCode code)
Definition: js_error.cc:115