-
Notifications
You must be signed in to change notification settings - Fork 23
/
python-streamlink.cpp
296 lines (250 loc) · 9.56 KB
/
python-streamlink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// ReSharper disable CppMemberFunctionMayBeConst
#include "python-streamlink.h"
#include <obs-module.h>
#include <Windows.h>
namespace streamlink {
bool loaded = false;
bool loadingFailed = false;
PyObject* module;
namespace methods
{
PyObject* new_session;
}
using ThreadState = PyGILState_STATE;
ThreadState AcquireThreadState()
{
return PyGILState_Ensure();
}
void ReleaseThreadState(ThreadState state) {
PyGILState_Release(state);
}
std::string PyStringToString(PyObject* pyStr)
{
ssize_t len;
auto cstr = PyUnicode_AsUTF8AndSize(pyStr, &len);
auto str = std::string(cstr, len);
return str;
}
std::string GetExceptionInfo()
{
if (!PyErr_Occurred()) return "";
PyObject* type, * value, * traceback;
PyErr_Fetch(&type, &value, &traceback);
auto strObj = PyObject_ASCII(value);
std::string message = PyStringToString(strObj);
Py_DECREF(strObj);
PyErr_Clear();
return message;
}
void LogFailure()
{
blog(LOG_ERROR, ("[Streamlink Source]: Failed to initialize streamlink plugin: " + GetExceptionInfo()).c_str());
}
void Initialize()
{
auto FireInitializationFailure = [](bool log = true) -> void
{
loaded = false;
loadingFailed = true;
if (log)
LogFailure();
PyEval_ReleaseThread(PyThreadState_Get());
};
if (!Py_IsInitialized())
{
// TODO make this configurable via properties.
std::string data_path = obs_get_module_data_path(obs_current_module());
std::string python_path = data_path.append("/Python38");
std::wstring widstr = std::wstring(python_path.begin(), python_path.end());
Py_SetPythonHome(widstr.c_str());
Py_Initialize();
}
module = PyImport_ImportModule("streamlink");
if (module == nullptr) return FireInitializationFailure();
methods::new_session = PyObject_GetAttrString(module, static_cast<const char*>("Streamlink"));
if (methods::new_session == nullptr) return FireInitializationFailure();
if (!PyCallable_Check(methods::new_session)) return FireInitializationFailure(false);
loaded = true;
PyEval_ReleaseThread(PyThreadState_Get());
}
PyObjectHolder::PyObjectHolder(PyObject* underlying, bool inc) : underlying(underlying)
{
if (inc)
Py_INCREF(underlying);
}
PyObjectHolder::~PyObjectHolder()
{
if (underlying != nullptr)
{
ThreadGIL state = ThreadGIL();
Py_DECREF(underlying);
}
}
PyObjectHolder::PyObjectHolder(PyObjectHolder&& another) noexcept
{
underlying = another.underlying;
another.underlying = nullptr;
}
PyObjectHolder& PyObjectHolder::operator=(PyObjectHolder&& another) noexcept
{
underlying = another.underlying;
another.underlying = nullptr;
return *this;
}
Stream::Stream(PyObject* u) : PyObjectHolder(u)
{
}
Stream::Stream(Stream&& another) noexcept : PyObjectHolder(std::move(another))
{
}
int Stream::Read(unsigned char* buf, const int len)
{
auto iucallable = PyObject_GetAttrString(underlying, "read");
if (iucallable == nullptr)
throw invalid_underlying_object();
auto iucallableHolder = PyObjectHolder(iucallable, false);
if (!PyCallable_Check(iucallable)) throw invalid_underlying_object();
auto args = PyTuple_Pack(1, PyLong_FromLong(len));
auto argsGuard = PyObjectHolder(args, false);
auto result = PyObject_Call(iucallable, args, nullptr);
if (result == nullptr)
throw call_failure(GetExceptionInfo().c_str());
auto resultGuard = PyObjectHolder(result, false);
char* buf1;
ssize_t readLen;
PyBytes_AsStringAndSize(result, &buf1, &readLen);
std::memcpy(buf, buf1, readLen);
if (readLen == 0) throw stream_ended();
return static_cast<int>(readLen);
}
void Stream::Close()
{
auto args = PyTuple_New(0);
auto argsGuard = PyObjectHolder(args, false);
auto closeCallable = PyObject_GetAttrString(underlying, "close");
if (closeCallable == nullptr)
throw invalid_underlying_object();
auto closeCallableHolder = PyObjectHolder(closeCallable, false);
if (!PyCallable_Check(closeCallable)) throw invalid_underlying_object();
auto result = PyObject_Call(closeCallable, args, nullptr);
if (result == nullptr)
throw call_failure(GetExceptionInfo().c_str());
}
StreamInfo::StreamInfo(std::string name, PyObject* u) : PyObjectHolder(u), name(std::move(name))
{
}
StreamInfo::StreamInfo(StreamInfo&& another) noexcept : PyObjectHolder(std::move(another))
{
name = another.name;
}
PyObject* StreamInfo::Open()
{
auto callable = PyObject_GetAttrString(underlying, "open");
if (!callable) throw invalid_underlying_object();
auto callableGuard = PyObjectHolder(callable, false);
if (!PyCallable_Check(callable)) throw invalid_underlying_object();
auto args = PyTuple_New(0);
auto argsGuard = PyObjectHolder(args, false);
auto result = PyObject_Call(callable, args, nullptr);
if (result == nullptr)
throw call_failure(GetExceptionInfo().c_str());
return result;
}
ThreadGIL::ThreadGIL()
{
state = PyGILState_Ensure();
}
ThreadGIL::~ThreadGIL()
{
PyGILState_Release(state);
}
}
streamlink::Session::Session()
{
//while (!IsDebuggerPresent())
//Sleep(100);
//DebugBreak();
if (!loaded) throw not_loaded();
auto args = PyTuple_New(0);
underlying = PyObject_Call(methods::new_session, args, nullptr);
Py_DECREF(args);
if (underlying == nullptr)
throw call_failure(GetExceptionInfo().c_str());
//Py_INCREF(underlying);
set_option = PyObject_GetAttrString(underlying, "set_option");
if (set_option == nullptr) throw call_failure(GetExceptionInfo().c_str());
set_optionGuard = PyObjectHolder(set_option, false);
if (!PyCallable_Check(set_option)) throw invalid_underlying_object();
}
streamlink::Session::~Session()
{
}
namespace streamlink {
std::map<std::string, StreamInfo> Session::GetStreamsFromUrl(const std::string& url)
{
auto streamsCallable = PyObject_GetAttrString(underlying, "streams");
if (streamsCallable == nullptr) throw call_failure(GetExceptionInfo().c_str());
auto streamsCallableGuard = PyObjectHolder(streamsCallable, false);
if (!PyCallable_Check(streamsCallable)) throw invalid_underlying_object();
if (!loaded) throw not_loaded();
auto urlStrObj = PyUnicode_FromStringAndSize(url.c_str(), static_cast<ssize_t>(url.size()));
auto urlStrObjGuard = PyObjectHolder(urlStrObj, false);
auto args = PyTuple_Pack(1, urlStrObj);
auto argsGuard = PyObjectHolder(args, false);
auto result = PyObject_Call(streamsCallable, args, nullptr);
if (result == nullptr)
throw call_failure(GetExceptionInfo().c_str());
auto resultGuard = PyObjectHolder(result, false);
auto items = PyDict_Items(result);
auto itemsGuard = PyObjectHolder(items, false);
auto size = PyList_Size(items);
auto ret = std::map<std::string, StreamInfo>();
for (int i = 0; i < size; i++)
{
auto itemTuple = PyList_GetItem(items, i); // borrowed
if (PyTuple_Size(itemTuple) != 2) continue;
auto nameObj = PyTuple_GetItem(itemTuple, 0);
auto valueObj = PyTuple_GetItem(itemTuple, 1); // no need to +ref, done by `StreamInfo` ctor
auto name = PyStringToString(nameObj);
ret.emplace(std::piecewise_construct, std::forward_as_tuple(name), std::forward_as_tuple(name, valueObj));
//ret.emplace(name, StreamInfo(name, valueObj));
}
return ret;
}
}
void streamlink::Session::SetOption(std::string const& name, PyObject* value)
{
if (!loaded) throw not_loaded();
auto nameObj = PyUnicode_FromStringAndSize(name.c_str(), static_cast<ssize_t>(name.size()));
auto nameObjGuard = PyObjectHolder(nameObj, false);
auto args = PyTuple_Pack(2, nameObj, value);
auto argsGuard = PyObjectHolder(args, false);
auto result = PyObject_Call(set_option, args, nullptr);
if (result == nullptr)
throw call_failure(GetExceptionInfo().c_str());
Py_DECREF(result);
}
void streamlink::Session::SetOptionString(std::string const& name, std::string const& value)
{
auto valueObj = PyUnicode_FromStringAndSize(value.c_str(), static_cast<ssize_t>(value.size()));
auto valueObjGuard = PyObjectHolder(valueObj, false);
SetOption(name, valueObj);
}
void streamlink::Session::SetOptionDouble(std::string const& name, double value)
{
auto valueObj = PyFloat_FromDouble(value);
auto valueObjGuard = PyObjectHolder(valueObj, false);
SetOption(name, valueObj);
}
void streamlink::Session::SetOptionInt(std::string const& name, long long value)
{
auto valueObj = PyLong_FromLongLong(value);
auto valueObjGuard = PyObjectHolder(valueObj, false);
SetOption(name, valueObj);
}
void streamlink::Session::SetOptionBool(std::string const& name, bool value)
{
auto valueObj = PyBool_FromLong(value);
auto valueObjGuard = PyObjectHolder(valueObj, false);
SetOption(name, valueObj);
}