Skip to content

Commit 4320230

Browse files
committed
Properly compiles
1 parent 54b3f50 commit 4320230

22 files changed

+2056
-2
lines changed

lib/mfcdm/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(mfcdm_library)
3+
4+
add_library(mfcdm_library STATIC
5+
mfcdm/MediaFoundationCdm.h
6+
mfcdm/MediaFoundationCdm.cpp
7+
mfcdm/MediaFoundationCdmFactory.cpp
8+
mfcdm/MediaFoundationCdmSession.cpp
9+
mfcdm/MediaFoundationSession.cpp
10+
mfcdm/Log.cpp
11+
)
12+
13+
target_include_directories(mfcdm_library PUBLIC ${PROJECT_SOURCE_DIR})
14+
target_link_libraries(mfcdm_library PRIVATE cdm_library propsys mf mfplat mfplay mfreadwrite mfuuid wmcodecdspuuid)
15+
set_target_properties(mfcdm_library PROPERTIES POSITION_INDEPENDENT_CODE True)

lib/mfcdm/mfcdm/Log.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "Log.h"
2+
3+
#include <cstdarg>
4+
#include <cstdio>
5+
6+
typedef struct
7+
{
8+
const char* name;
9+
int cur_level;
10+
void (*msg_callback)(int level, char* msg);
11+
} debug_ctx_t;
12+
13+
static debug_ctx_t debug_ctx = {"MF", MFCDM::MFLOG_NONE, NULL};
14+
15+
16+
static inline void __dbg(debug_ctx_t* ctx, int level, const char* fmt, va_list ap)
17+
{
18+
if (ctx != NULL && level <= ctx->cur_level)
19+
{
20+
char msg[4096];
21+
int len = snprintf(msg, sizeof(msg), "[%s] ", ctx->name);
22+
vsnprintf(msg + len, sizeof(msg) - len, fmt, ap);
23+
if (ctx->msg_callback)
24+
{
25+
ctx->msg_callback(level, msg);
26+
}
27+
}
28+
}
29+
30+
void MFCDM::LogAll()
31+
{
32+
debug_ctx.cur_level = MFLOG_ALL;
33+
}
34+
35+
void MFCDM::Log(LogLevel level, const char* fmt, ...)
36+
{
37+
va_list ap;
38+
39+
va_start(ap, fmt);
40+
__dbg(&debug_ctx, level, fmt, ap);
41+
va_end(ap);
42+
}
43+
44+
void MFCDM::SetMFMsgCallback(void (*msgcb)(int level, char*))
45+
{
46+
debug_ctx.msg_callback = msgcb;
47+
}

lib/mfcdm/mfcdm/Log.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MFCDM
2+
{
3+
enum LogLevel
4+
{
5+
MFLOG_NONE = -1,
6+
MFLOG_ERROR,
7+
MFLOG_WARN,
8+
MFLOG_INFO,
9+
MFLOG_DEBUG,
10+
MFLOG_ALL = 100
11+
};
12+
13+
void LogAll();
14+
void Log(LogLevel level, const char* fmt, ...);
15+
void SetMFMsgCallback(void (*msgcb)(int level, char*));
16+
17+
} // namespace MFCDM
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 "MediaFoundationCdm.h"
10+
11+
#include "Log.h"
12+
#include "MediaFoundationCdmFactory.h"
13+
#include "MediaFoundationCdmModule.h"
14+
#include "MediaFoundationCdmSession.h"
15+
16+
#include "utils/PMPHostWrapper.h"
17+
18+
MediaFoundationCdm::~MediaFoundationCdm() = default;
19+
20+
bool MediaFoundationCdm::Initialize(const std::string &keySystem,
21+
const std::string &basePath,
22+
const media::CdmConfig &cdmConfig,
23+
media::CdmAdapterClient* client)
24+
{
25+
bool ret = true;
26+
27+
m_session.Startup();
28+
29+
ret = m_session.HasMediaFoundation();
30+
if (!ret)
31+
{
32+
Log(MFCDM::MFLOG_ERROR, "MF doesn't exist on current system");
33+
return false;
34+
}
35+
36+
const auto m_factory = std::make_unique<MediaFoundationCdmFactory>(keySystem);
37+
38+
ret = m_factory->Initialize();
39+
if (!ret)
40+
{
41+
Log(MFCDM::MFLOG_ERROR, "MFFactory failed to initialize.");
42+
return false;
43+
}
44+
45+
ret = m_factory->CreateMfCdm(cdmConfig, basePath, m_module);
46+
if (!ret)
47+
{
48+
Log(MFCDM::MFLOG_ERROR, "MFFactory failed to create MF CDM.");
49+
return false;
50+
}
51+
52+
Log(MFCDM::MFLOG_DEBUG, "MF CDM created.");
53+
54+
SetupPMPServer();
55+
56+
m_client = client;
57+
return true;
58+
}
59+
60+
/*!
61+
* \brief Setup PMPHostApp
62+
* IMFContentDecryptionModule->SetPMPHostApp
63+
* needs to be set if not under UWP or else GenerateChallenge will fail
64+
* \link https://github.com/microsoft/media-foundation/issues/37#issuecomment-1194534228
65+
*/
66+
void MediaFoundationCdm::SetupPMPServer() const
67+
{
68+
if (!m_module)
69+
return;
70+
71+
const winrt::com_ptr<IMFGetService> spIMFGetService = m_module->As<IMFGetService>();
72+
winrt::com_ptr<IMFPMPHost> pmpHostApp;
73+
74+
if(FAILED(spIMFGetService->GetService(
75+
MF_CONTENTDECRYPTIONMODULE_SERVICE, IID_PPV_ARGS(pmpHostApp.put()))))
76+
{
77+
Log(MFCDM::MFLOG_ERROR, "Failed to get MF CDM service.");
78+
return;
79+
}
80+
81+
winrt::com_ptr<PMPHostWrapper> spIMFPMPHostApp = winrt::make_self<PMPHostWrapper>(pmpHostApp);
82+
m_module->SetPMPHostApp(spIMFPMPHostApp.get());
83+
}
84+
85+
void MediaFoundationCdm::SetServerCertificate(uint32_t promise_id,
86+
const uint8_t* serverCertificateData,
87+
uint32_t serverCertificateDataSize) const
88+
{
89+
m_module->SetServerCertificate(serverCertificateData, serverCertificateDataSize);
90+
}
91+
92+
void MediaFoundationCdm::CreateSessionAndGenerateRequest(uint32_t promise_id, cdm::SessionType sessionType,
93+
cdm::InitDataType initDataType, const uint8_t *init_data,
94+
uint32_t init_data_size)
95+
{
96+
auto session = std::make_unique<MediaFoundationCdmSession>();
97+
98+
if (!session->Initialize(sessionType, m_module.get()))
99+
{
100+
Log(MFCDM::MFLOG_ERROR, "Failed to create session.");
101+
return;
102+
}
103+
104+
int session_token = next_session_token_++;
105+
session->GenerateRequest(initDataType, init_data, init_data_size);
106+
107+
m_cdm_sessions.emplace(session_token, std::move(session));
108+
}
109+
110+
void MediaFoundationCdm::LoadSession(cdm::SessionType session_type,
111+
const std::string &session_id)
112+
{
113+
114+
}
115+
116+
void MediaFoundationCdm::UpdateSession(const std::string &session_id)
117+
{
118+
119+
}
120+
121+
122+
123+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
#pragma once
10+
11+
#include "MediaFoundationSession.h"
12+
13+
#include <string>
14+
#include <map>
15+
#include <memory>
16+
17+
#include "cdm/media/base/cdm_config.h"
18+
#include "cdm/media/cdm/api/content_decryption_module.h"
19+
#include "cdm/media/cdm/cdm_adapter.h"
20+
21+
class MediaFoundationCdmSession;
22+
class MediaFoundationCdmFactory;
23+
class MediaFoundationCdmModule;
24+
25+
class MediaFoundationCdm {
26+
public:
27+
~MediaFoundationCdm();
28+
29+
bool IsInitialized() const { return m_module != nullptr; }
30+
31+
bool Initialize(const std::string& keySystem,
32+
const std::string &basePath,
33+
const media::CdmConfig &cdmConfig,
34+
media::CdmAdapterClient* client);
35+
36+
void SetServerCertificate(uint32_t promise_id,
37+
const uint8_t* serverCertificateData,
38+
uint32_t serverCertificateDataSize) const;
39+
40+
void CreateSessionAndGenerateRequest(uint32_t promise_id,
41+
cdm::SessionType sessionType,
42+
cdm::InitDataType initDataType,
43+
const uint8_t* init_data,
44+
uint32_t init_data_size);
45+
46+
void LoadSession(cdm::SessionType session_type, const std::string& session_id);
47+
48+
void UpdateSession(const std::string& session_id);
49+
private:
50+
void SetupPMPServer() const;
51+
52+
MediaFoundationSession m_session;
53+
54+
std::unique_ptr<MediaFoundationCdmModule> m_module{nullptr};
55+
56+
int next_session_token_{0};
57+
std::map<int, std::unique_ptr<MediaFoundationCdmSession>> m_cdm_sessions;
58+
59+
media::CdmAdapterClient* m_client{nullptr};
60+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#pragma once
10+
11+
/*!
12+
* \brief The runtime configuration for the CDM instance
13+
*/
14+
struct MediaFoundationCdmConfig
15+
{
16+
MediaFoundationCdmConfig(bool distinctive_identifier = false, bool persistent_state = false)
17+
: allow_distinctive_identifier(distinctive_identifier),
18+
allow_persistent_state(persistent_state),
19+
use_hw_secure_codecs(false)
20+
{
21+
22+
}
23+
24+
// Allow access to a distinctive identifier.
25+
bool allow_distinctive_identifier;
26+
27+
// Allow access to persistent state.
28+
bool allow_persistent_state;
29+
30+
// Use hardware-secure codecs.
31+
bool use_hw_secure_codecs;
32+
};

0 commit comments

Comments
 (0)