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_factory.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "chromecast/base/metrics/cast_metrics_helper.h"
12 #include "chromecast/media/cdm/cast_cdm.h"
13 #include "media/base/bind_to_current_loop.h"
14 #include "media/base/cdm_config.h"
15 #include "media/base/cdm_key_information.h"
16 #include "url/origin.h"
17
18 namespace chromecast {
19 namespace media {
20
CastCdmFactory(scoped_refptr<base::SingleThreadTaskRunner> task_runner,const url::Origin & cdm_origin,MediaResourceTracker * media_resource_tracker)21 CastCdmFactory::CastCdmFactory(
22 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
23 const url::Origin& cdm_origin,
24 MediaResourceTracker* media_resource_tracker)
25 : media_resource_tracker_(media_resource_tracker),
26 task_runner_(task_runner),
27 cdm_origin_(cdm_origin) {
28 DCHECK(media_resource_tracker_);
29 DCHECK(task_runner_);
30 }
31
~CastCdmFactory()32 CastCdmFactory::~CastCdmFactory() {}
33
Create(const std::string & key_system,const::media::CdmConfig & cdm_config,const::media::SessionMessageCB & session_message_cb,const::media::SessionClosedCB & session_closed_cb,const::media::SessionKeysChangeCB & session_keys_change_cb,const::media::SessionExpirationUpdateCB & session_expiration_update_cb,::media::CdmCreatedCB cdm_created_cb)34 void CastCdmFactory::Create(
35 const std::string& key_system,
36 const ::media::CdmConfig& cdm_config,
37 const ::media::SessionMessageCB& session_message_cb,
38 const ::media::SessionClosedCB& session_closed_cb,
39 const ::media::SessionKeysChangeCB& session_keys_change_cb,
40 const ::media::SessionExpirationUpdateCB& session_expiration_update_cb,
41 ::media::CdmCreatedCB cdm_created_cb) {
42 // Bound |cdm_created_cb| so we always fire it asynchronously.
43 ::media::CdmCreatedCB bound_cdm_created_cb =
44 ::media::BindToCurrentLoop(std::move(cdm_created_cb));
45
46 CastKeySystem cast_key_system(GetKeySystemByName(key_system));
47
48 DCHECK((cast_key_system == chromecast::media::KEY_SYSTEM_PLAYREADY) ||
49 (cast_key_system == chromecast::media::KEY_SYSTEM_WIDEVINE));
50
51 scoped_refptr<chromecast::media::CastCdm> cast_cdm =
52 CreatePlatformBrowserCdm(cast_key_system, cdm_origin_, cdm_config);
53
54 if (!cast_cdm) {
55 LOG(INFO) << "No matching key system found: " << cast_key_system;
56 std::move(bound_cdm_created_cb)
57 .Run(nullptr, "No matching key system found.");
58 return;
59 }
60
61 const int packed_cdm_config = (cdm_config.allow_distinctive_identifier << 2) |
62 (cdm_config.allow_persistent_state << 1) |
63 cdm_config.use_hw_secure_codecs;
64 metrics::CastMetricsHelper::GetInstance()->RecordApplicationEventWithValue(
65 "Cast.Platform.CreateCdm." + key_system, packed_cdm_config);
66
67 task_runner_->PostTask(
68 FROM_HERE,
69 base::BindOnce(&CastCdm::Initialize, base::Unretained(cast_cdm.get()),
70 ::media::BindToCurrentLoop(session_message_cb),
71 ::media::BindToCurrentLoop(session_closed_cb),
72 ::media::BindToCurrentLoop(session_keys_change_cb),
73 ::media::BindToCurrentLoop(session_expiration_update_cb)));
74 std::move(bound_cdm_created_cb).Run(cast_cdm, "");
75 }
76
CreatePlatformBrowserCdm(const CastKeySystem & cast_key_system,const url::Origin & cdm_origin,const::media::CdmConfig & cdm_config)77 scoped_refptr<CastCdm> CastCdmFactory::CreatePlatformBrowserCdm(
78 const CastKeySystem& cast_key_system,
79 const url::Origin& cdm_origin,
80 const ::media::CdmConfig& cdm_config) {
81 return nullptr;
82 }
83
84 } // namespace media
85 } // namespace chromecast
86