Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ jobs:
env:
BASE_URL: https://github.com/${{ github.repository }}/releases/download/vita-packages
run: |
for pkg in mbedtls-3.6.8-1 curl-8.16.0-3 mupdf-1.28.6-1 ffmpeg-n6.0-3 mpv-0.36.0-3; do
for pkg in mbedtls-3.6.8-1 curl-8.16.0-3 mupdf-1.28.6-1 ffmpeg-n6.0-3 mpv-0.36.0-4; do
vdpm pacman --noconfirm -U $BASE_URL/${pkg}-vita.pkg.tar.xz
done
git config --system --add safe.directory $GITHUB_WORKSPACE
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ elseif (PLATFORM_PSV)
if (USE_VITA_SHARK)
list(APPEND PSV_ASSETS_FILES "${CMAKE_BINARY_DIR}/vendor/SceShaccCg" module)
endif ()
# newlib lacks C99 printf length modifiers (%zd...), see utils/psv_printf.cpp
target_link_options(${PROJECT_NAME} PRIVATE
-Wl,--wrap=_svfprintf_r -Wl,--wrap=_vfprintf_r -Wl,--wrap=vfprintf)
vita_create_self(${PROJECT_NAME}.self ${PROJECT_NAME} UNSAFE NOASLR)
vita_create_vpk(${PROJECT_NAME}.vpk ${VITA_TITLEID} ${PROJECT_NAME}.self
VERSION ${VITA_VERSION}
Expand Down
115 changes: 115 additions & 0 deletions app/src/utils/psv_printf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// VitaSDK's newlib is built without _WANT_IO_C99_FORMATS, so its printf
// family does not understand the C99 length modifiers z, t and j. Such a
// conversion is skipped without consuming its argument, which shifts every
// following argument: a later %s then dereferences an integer and crashes.
// mpv and FFmpeg use %zd/%zu in log messages (e.g. mpv's "Too many packets
// in the demuxer packet queues" warning), so this crashed the app.
//
// The string and stream formatters are wrapped at link time
// (-Wl,--wrap=..., see CMakeLists.txt) and the format is rewritten first:
// size_t and ptrdiff_t are 32-bit like int on the Vita, so z and t are
// dropped, and intmax_t is 64-bit, so j becomes ll.

#ifdef __PSV__

#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>

namespace {

bool needsRewrite(const char* fmt) {
for (const char* p = fmt; (p = std::strchr(p, '%')) != nullptr;) {
++p;
if (*p == '%') {
++p;
continue;
}
while (*p && std::strchr("-+ #0'123456789.*", *p)) ++p;
while (*p && std::strchr("hlLqjzt", *p)) {
if (*p == 'z' || *p == 't' || *p == 'j') return true;
++p;
}
}
return false;
}

void rewrite(const char* fmt, char* out) {
const char* p = fmt;
while (*p) {
if (*p != '%') {
*out++ = *p++;
continue;
}
*out++ = *p++;
if (*p == '%') {
*out++ = *p++;
continue;
}
while (*p && std::strchr("-+ #0'123456789.*", *p)) *out++ = *p++;
while (*p && std::strchr("hlLqjzt", *p)) {
if (*p == 'j') {
*out++ = 'l';
*out++ = 'l';
} else if (*p != 'z' && *p != 't') {
*out++ = *p;
}
++p;
}
}
*out = '\0';
}

/// Holds the rewritten format; small formats stay on the stack.
class Format {
public:
explicit Format(const char* fmt) : str(fmt) {
if (!fmt || !needsRewrite(fmt)) return;
// only j grows (to ll), so twice the length is always enough
size_t size = std::strlen(fmt) * 2 + 1;
char* out = size <= sizeof(stack) ? stack : heap = static_cast<char*>(std::malloc(size));
if (!out) return;
rewrite(fmt, out);
str = out;
}
~Format() { std::free(heap); }
Format(const Format&) = delete;
Format& operator=(const Format&) = delete;

const char* str;

private:
char stack[256];
char* heap = nullptr;
};

} // namespace

extern "C" {

int __real__svfprintf_r(struct _reent* r, FILE* fp, const char* fmt, va_list ap);
int __real__vfprintf_r(struct _reent* r, FILE* fp, const char* fmt, va_list ap);
int __real_vfprintf(FILE* fp, const char* fmt, va_list ap);

// sprintf, snprintf, vsnprintf, asprintf, vasprintf...
int __wrap__svfprintf_r(struct _reent* r, FILE* fp, const char* fmt, va_list ap) {
Format f(fmt);
return __real__svfprintf_r(r, fp, f.str, ap);
}

// printf, fprintf, vprintf
int __wrap__vfprintf_r(struct _reent* r, FILE* fp, const char* fmt, va_list ap) {
Format f(fmt);
return __real__vfprintf_r(r, fp, f.str, ap);
}

// vfprintf calls _vfprintf_r from its own object, which --wrap can't reach
int __wrap_vfprintf(FILE* fp, const char* fmt, va_list ap) {
Format f(fmt);
return __real_vfprintf(fp, f.str, ap);
}

} // extern "C"

#endif // __PSV__
32 changes: 23 additions & 9 deletions app/src/view/player_setting.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <algorithm>

#include "utils/config.hpp"
#include "view/button_close.hpp"
#include "view/mpv_core.hpp"
Expand Down Expand Up @@ -26,6 +28,8 @@ PlayerSetting::PlayerSetting(const jellyfin::Source* src) {

std::vector<std::string> audioTrack, audioSource;
std::vector<int> audioStream;
// mpv track ids, parallel to audioTrack / subTrack (0 = no subtitle)
std::vector<int64_t> audioId, subId = {0};
std::vector<std::string> subTrack = {"main/player/none"_i18n};
std::vector<std::string> subSource = {"main/player/none"_i18n};
std::vector<int> subStream = {0};
Expand All @@ -40,10 +44,14 @@ PlayerSetting::PlayerSetting(const jellyfin::Source* src) {
else if (title.empty())
title = lang;
if (title.empty()) title = fmt::format("{} track {}", type, n);
if (type == "sub")
int64_t id = mpv.getInt(fmt::format("track-list/{}/id", n));
if (type == "sub") {
subTrack.push_back(title);
else if (type == "audio")
subId.push_back(id);
} else if (type == "audio") {
audioTrack.push_back(title);
audioId.push_back(id);
}
}

if (src != nullptr) {
Expand All @@ -59,10 +67,14 @@ PlayerSetting::PlayerSetting(const jellyfin::Source* src) {
}
// 字幕选择
if (subTrack.size() > 1) {
int64_t value = mpv.getInt("sid");
this->subtitleTrack->init("main/player/subtitle"_i18n, subTrack, value, [&mpv](int selected) {
selectedSubtitle = selected;
mpv.setInt("sid", selected);
// sid may name a track missing from the list (e.g. a sub-add that
// finished after track-list was read), so map ids instead of indexing
int64_t sid = mpv.getInt("sid");
auto it = std::find(subId.begin(), subId.end(), sid);
int value = it != subId.end() ? it - subId.begin() : 0;
this->subtitleTrack->init("main/player/subtitle"_i18n, subTrack, value, [&mpv, subId](int selected) {
selectedSubtitle = subId[selected];
mpv.setInt("sid", selectedSubtitle);
});
} else if (subSource.size() > 1) {
int value = 0;
Expand All @@ -77,9 +89,11 @@ PlayerSetting::PlayerSetting(const jellyfin::Source* src) {
}
// 音轨选择
if (audioTrack.size() > 1) {
int64_t value = mpv.getInt("aid", 1) - 1;
this->audioTrack->init("main/player/audio"_i18n, audioTrack, value, [&mpv](int selected) {
selectedAudio = selected + 1;
int64_t aid = mpv.getInt("aid", 1);
auto it = std::find(audioId.begin(), audioId.end(), aid);
int value = it != audioId.end() ? it - audioId.begin() : 0;
this->audioTrack->init("main/player/audio"_i18n, audioTrack, value, [&mpv, audioId](int selected) {
selectedAudio = audioId[selected];
mpv.setInt("aid", selectedAudio);
});
this->audioTrack->detail->setVisibility(brls::Visibility::GONE);
Expand Down
7 changes: 4 additions & 3 deletions scripts/vita/mpv/VITABUILD
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
pkgname=mpv
pkgver=0.36.0
pkgrel=3
pkgrel=4
url="https://mpv.io/"
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/mpv-player/mpv/archive/v${pkgver}.tar.gz" "gxm.patch")
sha256sums=('SKIP' 'SKIP')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/mpv-player/mpv/archive/v${pkgver}.tar.gz" "gxm.patch" "loadfile.patch")
sha256sums=('SKIP' 'SKIP' 'SKIP')
depends=('libass' 'ffmpeg')

prepare() {
cd $pkgname-$pkgver
patch --strip=1 --input=${srcdir}/gxm.patch
patch --strip=1 --input=${srcdir}/loadfile.patch
}

build() {
Expand Down
16 changes: 16 additions & 0 deletions scripts/vita/mpv/loadfile.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- a/player/loadfile.c
+++ b/player/loadfile.c
@@ -1276,6 +1276,13 @@
if (!mpctx->open_active)
start_open(mpctx, url, mpctx->playing->stream_flags, false);

+ // start_open() gives up when the opener thread cannot be created (e.g. out
+ // of memory), leaving open_cancel NULL. Fail the load instead of crashing.
+ if (!mpctx->open_active) {
+ MP_ERR(mpctx, "Failed to create the demuxer open thread.\n");
+ return;
+ }
+
// User abort should cancel the opener now.
mp_cancel_set_parent(mpctx->open_cancel, mpctx->playback_abort);