Debian Codegraph @

src/decrypters/widevine/WVCdmAdapter.cpp

raw
1 /*
2 * Copyright (C) 2023 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "WVCdmAdapter.h"
10
11 #include "CdmFixedBuffer.h"
12 #include "WVCencSingleSampleDecrypter.h"
13 #include "WVDecrypter.h"
14 #include "cdm/debug.h"
15 #include "decrypters/Helpers.h"
16 #include "utils/FileUtils.h"
17 #include "utils/log.h"
18
19 #include <kodi/Filesystem.h>
20
21 using namespace UTILS;
22
23 namespace
24 {
25 #if WIN32
26 constexpr const char* LIBRARY_FILENAME = "widevinecdm.dll";
27 #elif TARGET_DARWIN
28 constexpr const char* LIBRARY_FILENAME = "libwidevinecdm.dylib";
29 #else
30 constexpr const char* LIBRARY_FILENAME = "libwidevinecdm.so";
31 #endif
32
33 void DebugLog(const CDM_DBG::LogLevel level, const char* msg)
34 {
35 switch (level)
36 {
37 case CDM_DBG::LogLevel::ERROR:
38 LOG::Log(LOGERROR, msg);
39 break;
40 case CDM_DBG::LogLevel::WARNING:
41 LOG::Log(LOGWARNING, msg);
42 break;
43 case CDM_DBG::LogLevel::INFO:
44 LOG::Log(LOGINFO, msg);
45 break;
46 case CDM_DBG::LogLevel::DEBUG:
47 LOG::Log(LOGDEBUG, msg);
48 break;
49 case CDM_DBG::LogLevel::FATAL:
50 LOG::Log(LOGFATAL, msg);
51 break;
52 default:
53 break;
54 }
55 }
56 } // unnamed namespace
57
58 CWVCdmAdapter::CWVCdmAdapter(std::string_view licenseURL,
59 const std::vector<uint8_t>& serverCert,
60 const uint8_t config,
61 CWVDecrypter* host)
62 : m_licenseUrl(licenseURL), m_host(host), m_codecInstance(nullptr)
63 {
64 CDM_DBG::SetDBGMsgCallback(DebugLog);
65
66 if (m_host->GetLibraryPath().empty())
67 {
68 LOG::LogF(LOGERROR, "Widevine CDM library path not specified");
69 return;
70 }
71 std::string cdmPath = FILESYS::PathCombine(m_host->GetLibraryPath(), LIBRARY_FILENAME);
72
73 if (licenseURL.empty())
74 {
75 LOG::LogF(LOGERROR, "No license URL path specified");
76 return;
77 }
78
79 // The license url come from license_key kodi property
80 // we have to kept only the url without the parameters specified after pipe "|" char
81 std::string licUrl = m_licenseUrl;
82 const size_t urlPipePos = licUrl.find('|');
83 if (urlPipePos != std::string::npos)
84 licUrl.erase(urlPipePos);
85
86 // Build up a CDM path to store decrypter specific stuff, each domain gets it own path
87 // the domain name is hashed to generate a short folder name
88 std::string basePath = FILESYS::PathCombine(FILESYS::GetAddonUserPath(), "widevine");
89 basePath = FILESYS::PathCombine(basePath, DRM::GenerateUrlDomainHash(licUrl));
90 basePath += FILESYS::SEPARATOR;
91
92 wv_adapter = std::shared_ptr<media::CdmAdapter>(new media::CdmAdapter(
93 "com.widevine.alpha", cdmPath, basePath,
94 media::CdmConfig(false, (config & DRM::IDecrypter::CONFIG_PERSISTENTSTORAGE) != 0),
95 dynamic_cast<media::CdmAdapterClient*>(this)));
96 if (!wv_adapter->valid())
97 {
98 LOG::Log(LOGERROR, "Unable to load widevine shared library (%s)", cdmPath.c_str());
99 wv_adapter = nullptr;
100 return;
101 }
102
103 if (!serverCert.empty())
104 wv_adapter->SetServerCertificate(0, serverCert.data(), serverCert.size());
105
106 // For backward compatibility: If no | is found in URL, use the most common working config
107 if (m_licenseUrl.find('|') == std::string::npos)
108 m_licenseUrl += "|Content-Type=application%2Foctet-stream|R{SSM}|";
109
110 //wv_adapter->GetStatusForPolicy();
111 //wv_adapter->QueryOutputProtectionStatus();
112 }
113
114 CWVCdmAdapter::~CWVCdmAdapter()
115 {
116 if (wv_adapter)
117 {
118 wv_adapter->RemoveClient();
119 LOG::Log(LOGERROR, "Instances: %u", wv_adapter.use_count());
120 wv_adapter = nullptr;
121 }
122 }
123
124 void CWVCdmAdapter::OnCDMMessage(const char* session,
125 uint32_t session_size,
126 CDMADPMSG msg,
127 const uint8_t* data,
128 size_t data_size,
129 uint32_t status)
130 {
131 LOG::Log(LOGDEBUG, "CDMMessage: %u arrived!", msg);
132 std::vector<CWVCencSingleSampleDecrypter*>::iterator b(ssds.begin()), e(ssds.end());
133 for (; b != e; ++b)
134 if (!(*b)->GetSessionId() || strncmp((*b)->GetSessionId(), session, session_size) == 0)
135 break;
136
137 if (b == ssds.end())
138 return;
139
140 if (msg == CDMADPMSG::kSessionMessage)
141 {
142 (*b)->SetSession(session, session_size, data, data_size);
143 }
144 else if (msg == CDMADPMSG::kSessionKeysChange)
145 (*b)->AddSessionKey(data, data_size, status);
146 }
147
148 cdm::Buffer* CWVCdmAdapter::AllocateBuffer(size_t sz)
149 {
150 VIDEOCODEC_PICTURE pic;
151 pic.decodedDataSize = sz;
152 if (m_host->GetBuffer(m_codecInstance, pic))
153 {
154 CdmFixedBuffer* buf = new CdmFixedBuffer;
155 buf->initialize(m_codecInstance, pic.decodedData, pic.decodedDataSize, pic.videoBufferHandle, m_host);
156 return buf;
157 }
158 return nullptr;
159 ;
160 }