Repository navigation
Expand file tree
/
Copy pathPlayer.cpp
More file actions
381 lines (336 loc) · 12.5 KB
/
Copy pathPlayer.cpp
File metadata and controls
381 lines (336 loc) · 12.5 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <chrono>
#include <cctype>
#include <cstring>
#include "prx/libSceAvPlayer/include/AvPlayer.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/GuestHeap.hpp"
namespace AvPlayer {
namespace {
bool EqualsIgnoreCase(std::string_view left, std::string_view right) {
return std::ranges::equal(left, right, [](unsigned char a, unsigned char b) { return std::tolower(a) == std::tolower(b); });
}
bool SameLanguage(const char* preferred, const char* code) {
return preferred[0] != '\0' && std::strncmp(preferred, code, 4) == 0;
}
void* APS5_VABI GuestAllocate_nid_no_patch(void*, std::uint32_t alignment, std::uint32_t size) {
return GuestHeap::GuestHeapAlign_nid_postfix(std::max<std::uint32_t>(alignment, 16), size);
}
void APS5_VABI GuestDeallocate_nid_no_patch(void*, void* memory) {
GuestHeap::GuestHeapFree_nid_postfix(memory);
}
void* APS5_VABI GuestAllocateTexture_nid_no_patch(void*, std::uint32_t alignment, std::uint32_t size) {
return GuestHeap::GuestHeapAlignForGpu_nid_postfix(std::max<std::uint32_t>(alignment, 16), size);
}
AvPlayerMemAllocator WithGuestHeapFallback(AvPlayerMemAllocator memory) {
if (!memory.allocate || !memory.deallocate) {
memory.allocate = GuestAllocate_nid_no_patch;
memory.deallocate = GuestDeallocate_nid_no_patch;
}
if (!memory.allocate_texture || !memory.deallocate_texture) {
memory.allocate_texture = GuestAllocateTexture_nid_no_patch;
memory.deallocate_texture = GuestDeallocate_nid_no_patch;
}
return memory;
}
}
std::uint32_t DetectSourceType(std::string_view path) {
std::string_view name = path;
if (name.find("://") != std::string_view::npos) name = name.substr(0, name.find_first_of("?#"));
const auto dot = name.rfind('.');
if (dot == std::string_view::npos) return SourceTypeUnknown;
auto extension = name.substr(dot);
extension = extension.substr(0, extension.find('/'));
for (const auto mp4 : {".mp4", ".m4v", ".m3d", ".m4a", ".mov"}) {
if (EqualsIgnoreCase(extension, mp4)) return SourceTypeFileMp4;
}
if (EqualsIgnoreCase(extension, ".m3u8")) return SourceTypeHls;
return SourceTypeUnknown;
}
Player::Player(const AvPlayerInitData& init, std::uint32_t bufferCount)
: memory(WithGuestHeapFallback(init.memory_replacement)), callback(init.event_replacement), videoBuffers(bufferCount) {
if (init.file_replacement.open && init.file_replacement.close && init.file_replacement.read_offset && init.file_replacement.size) {
file = init.file_replacement;
}
autoStartEnabled = init.auto_start || callback.event_callback == nullptr;
if (init.default_language) {
for (std::size_t index = 0; index < 3 && init.default_language[index] != '\0'; ++index) language[index] = init.default_language[index];
}
controller = std::thread([this] { controllerLoop(); });
}
Player::~Player() {
{
std::lock_guard lock(eventMutex);
quit = true;
}
eventCondition.NotifyAll();
controller.join();
std::lock_guard lock(mutex);
source.reset();
}
void Player::queue(const Event& event) {
{
std::lock_guard lock(eventMutex);
events.push_back(event);
}
eventCondition.NotifyAll();
}
void Player::OnWarning(std::int32_t code) {
queue({EventWarningId, code, false, false});
}
void Player::OnError() {
queue({0, 0, false, true});
}
void Player::controllerLoop() {
for (;;) {
Event event{};
bool pending = false;
{
std::unique_lock lock(eventMutex);
eventCondition.WaitUntil(lock, TimedWait::DeadlineNanos(5000), [this] { return quit || !events.empty(); });
if (quit) return;
if (!events.empty()) {
event = events.front();
events.pop_front();
pending = true;
}
}
if (pending) {
deliver(event);
} else {
checkEndOfFile();
}
}
}
void Player::deliver(const Event& event) {
if (event.error) {
std::lock_guard lock(mutex);
if (source && state != State::Stop) {
previous = state;
state = State::Error;
}
return;
}
if (event.autoStart) {
autoStart();
return;
}
if (!callback.event_callback) return;
std::int32_t warning = event.warning;
callback.event_callback(callback.object_ptr, event.id, 0, event.id == EventWarningId ? &warning : nullptr);
}
bool Player::readyLocked() const {
return source && state == State::Ready;
}
void Player::autoStart() {
{
std::lock_guard lock(mutex);
if (!readyLocked()) return;
}
const int count = StreamCount();
if (count <= 0) {
Stop();
return;
}
int video = -1;
int audio = -1;
for (int index = 0; index < count; ++index) {
AvPlayerStreamInfo info{};
if (GetStreamInfo(static_cast<std::uint32_t>(index), info) != SCE_OK) {
Stop();
return;
}
if (info.type == StreamTypeVideo && (video == -1 || SameLanguage(language, info.details.video.language_code))) video = index;
if (info.type == StreamTypeAudio && (audio == -1 || SameLanguage(language, info.details.audio.language_code))) audio = index;
}
if (video != -1) EnableStream(static_cast<std::uint32_t>(video));
if (audio != -1) EnableStream(static_cast<std::uint32_t>(audio));
std::lock_guard lock(mutex);
if (readyLocked()) startLocked();
}
void Player::checkEndOfFile() {
{
std::lock_guard lock(mutex);
if (!source || state != State::Play || !source->Finished()) return;
previous = state;
state = State::EndOfFile;
}
deliver({EventStateStop, 0, false, false});
}
int Player::PostInit(const AvPlayerPostInitData& data) {
std::lock_guard lock(mutex);
demuxVideoBytes = data.demux_video_buffer_size;
if (source) source->SetDemuxVideoBufferSize(demuxVideoBytes);
return SCE_OK;
}
int Player::AddSource(std::string_view path, std::uint32_t sourceType) {
if (path.empty()) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
if (sourceType == SourceTypeUnknown) sourceType = DetectSourceType(path);
if (sourceType == SourceTypeHls) NotImplemented_nid_no_patch("sceAvPlayerAddSource (HLS)");
std::lock_guard lock(mutex);
if (source) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
auto opened = OpenSource({memory, file, videoBuffers}, std::string(path), *this);
if (!opened) {
previous = state;
state = State::Error;
return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
}
if (opened->StreamCount() == 0) {
source = std::move(opened);
previous = state;
state = State::Error;
queue({EventWarningId, SCE_AVPLAYER_ERROR_NOT_SUPPORTED, false, false});
return SCE_OK;
}
opened->SetLooping(looping);
opened->SetSpeed(speed);
opened->SetSyncMode(syncMode);
opened->SetDemuxVideoBufferSize(demuxVideoBytes);
source = std::move(opened);
previous = state;
state = State::Ready;
queue(autoStartEnabled ? Event{0, 0, true, false} : Event{EventStateReady, 0, false, false});
return SCE_OK;
}
int Player::StreamCount() {
std::lock_guard lock(mutex);
if (!source) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
return static_cast<int>(source->StreamCount());
}
int Player::GetStreamInfo(std::uint32_t index, AvPlayerStreamInfo& info) {
std::lock_guard lock(mutex);
if (!source || !source->GetStreamInfo(index, info)) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
return SCE_OK;
}
int Player::GetStreamInfoEx(std::uint32_t index, AvPlayerStreamInfoEx& info) {
std::lock_guard lock(mutex);
AvPlayerStreamInfoEx described{};
if (!source || !source->GetStreamInfoEx(index, described)) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
described.this_size = info.this_size;
info = described;
return SCE_OK;
}
int Player::EnableStream(std::uint32_t index) {
std::lock_guard lock(mutex);
if (!source || !source->EnableStream(index)) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
return SCE_OK;
}
int Player::DisableStream(std::uint32_t index) {
std::lock_guard lock(mutex);
if (!source || !source->DisableStream(index)) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
return SCE_OK;
}
int Player::ChangeStream(std::uint32_t from, std::uint32_t to) {
std::lock_guard lock(mutex);
if (!source || state == State::Error || !source->ChangeStream(from, to)) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
return SCE_OK;
}
int Player::Start() {
std::lock_guard lock(mutex);
return startLocked();
}
int Player::startLocked() {
if (!source) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
if (state != State::Ready && state != State::Stop && stopLocked() != SCE_OK) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
const int result = source->Start();
if (result != SCE_OK) {
previous = state;
state = State::Error;
return result;
}
previous = state;
state = State::Play;
queue({EventStatePlay, 0, false, false});
return SCE_OK;
}
int Player::stopLocked() {
if (!source || state == State::Initial || state == State::Ready || state == State::Stop) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
source->Stop();
previous = state;
state = State::Stop;
queue({EventStateStop, 0, false, false});
return SCE_OK;
}
int Player::Stop() {
std::lock_guard lock(mutex);
return stopLocked();
}
int Player::Pause() {
std::lock_guard lock(mutex);
if (state == State::EndOfFile) return SCE_OK;
if (!source || state != State::Play) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
source->Pause();
previous = state;
state = State::Pause;
queue({EventStatePause, 0, false, false});
return SCE_OK;
}
int Player::Resume() {
std::lock_guard lock(mutex);
if (!source || state != State::Pause) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
source->Resume();
std::swap(state, previous);
queue({EventStatePlay, 0, false, false});
return SCE_OK;
}
int Player::JumpToTime(std::uint64_t milliseconds) {
std::lock_guard lock(mutex);
if (!source || (state != State::Ready && state != State::Play && state != State::Pause)) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
source->Jump(milliseconds);
return SCE_OK;
}
int Player::SetLooping(bool enabled) {
std::lock_guard lock(mutex);
looping = enabled;
if (source) source->SetLooping(enabled);
return SCE_OK;
}
int Player::SetTrickSpeed(std::int32_t trickSpeed) {
if (trickSpeed == 0) return SCE_AVPLAYER_ERROR_INVALID_PARAMS;
std::lock_guard lock(mutex);
if (state == State::Stop || state == State::EndOfFile || state == State::Error) return SCE_AVPLAYER_ERROR_OPERATION_FAILED;
speed = trickSpeed;
if (source) source->SetSpeed(trickSpeed);
return SCE_OK;
}
int Player::SetAvSyncMode(std::uint32_t mode) {
if (mode != SyncModeDefault && mode != SyncModeNone) return SCE_AVPLAYER_ERROR_INVALID_PARAMS;
std::lock_guard lock(mutex);
syncMode = mode;
if (source) source->SetSyncMode(mode);
return SCE_OK;
}
bool Player::GetVideoData(AvPlayerFrameInfoEx& info) {
std::lock_guard lock(mutex);
if (!source || (state != State::Play && state != State::Pause)) return false;
return source->GetVideoData(info);
}
bool Player::GetVideoData(AvPlayerFrameInfo& info) {
AvPlayerFrameInfoEx extended{};
if (!GetVideoData(extended)) return false;
info = {};
info.p_data = static_cast<std::uint8_t*>(extended.p_data);
info.timestamp = extended.timestamp;
info.details.video.width = extended.details.video.width;
info.details.video.height = extended.details.video.height;
info.details.video.aspect_ratio = extended.details.video.aspect_ratio;
std::memcpy(info.details.video.language_code, extended.details.video.language_code, sizeof(info.details.video.language_code));
return true;
}
bool Player::GetAudioData(AvPlayerFrameInfo& info) {
std::lock_guard lock(mutex);
if (!source || state != State::Play) return false;
return source->GetAudioData(info);
}
bool Player::IsActive() {
std::lock_guard lock(mutex);
if (!source || state == State::Initial || state == State::Stop || state == State::EndOfFile || state == State::Error) return false;
return !source->Finished();
}
std::uint64_t Player::CurrentTime() {
std::lock_guard lock(mutex);
return source ? source->CurrentTime() : 0;
}
}