Skip to content

Commit

Permalink
Added some logs, fixed some crashes, fixed align.
Browse files Browse the repository at this point in the history
  • Loading branch information
roamic committed Aug 13, 2024
1 parent bd32423 commit 587027a
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 323 deletions.
2 changes: 1 addition & 1 deletion src/audio_core/sdl_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
int result = SDL_PutAudioStreamData(port.stream, ptr,
port.samples_num * port.sample_size * port.channels_num);
// TODO find a correct value 8192 is estimated
while (SDL_GetAudioStreamAvailable(port.stream) > 8192) {
while (SDL_GetAudioStreamAvailable(port.stream) > 65536) {
SDL_Delay(0);
}

Expand Down
16 changes: 7 additions & 9 deletions src/core/libraries/avplayer/avplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "core/libraries/kernel/thread_management.h"
#include "core/libraries/libs.h"

#include <mutex>
#include <algorithm> // std::max, std::min

#include <stdarg.h> // va_list

namespace Libraries::AvPlayer {

Expand Down Expand Up @@ -148,9 +150,7 @@ SceAvPlayerHandle PS4_SYSV_ABI sceAvPlayerInit(SceAvPlayerInitData* data) {
// priorities.file_streaming_priority = GetPriority(priorities.http_streaming_priority, 15);
// priorities.maxPriority = priorities.http_streaming_priority;

const auto player = new AvPlayer();
player->Init(*data, priorities);
return player;
return new AvPlayer(*data, priorities);
}

s32 PS4_SYSV_ABI sceAvPlayerInitEx(const SceAvPlayerInitDataEx* p_data,
Expand Down Expand Up @@ -225,9 +225,7 @@ s32 PS4_SYSV_ABI sceAvPlayerInitEx(const SceAvPlayerInitDataEx* p_data,
// }
// priorities.http_streaming_affinity = p_data->http_streaming_affinity;

const auto player = new AvPlayer();
player->Init(data, priorities);
*p_player = player;
*p_player = new AvPlayer(data, priorities);
return ORBIS_OK;
}

Expand Down Expand Up @@ -290,13 +288,13 @@ s32 PS4_SYSV_ABI sceAvPlayerSetAvSyncMode(SceAvPlayerHandle handle,
return ORBIS_OK;
}

s32 PS4_SYSV_ABI sceAvPlayerSetLogCallback(SceAvPlayerLogCallback logCb, void* user_data) {
s32 PS4_SYSV_ABI sceAvPlayerSetLogCallback(SceAvPlayerLogCallback log_cb, void* user_data) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
return ORBIS_OK;
}

s32 PS4_SYSV_ABI sceAvPlayerSetLooping(SceAvPlayerHandle handle, bool loop_flag) {
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called");
LOG_ERROR(Lib_AvPlayer, "(STUBBED) called, looping = {}", loop_flag);
if (handle == nullptr) {
return ORBIS_AVPLAYER_ERROR_INVALID_PARAMS;
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/libraries/avplayer/avplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "common/types.h"

#include <stddef.h> // size_t

namespace Core::Loader {
class SymbolsResolver;
}
Expand Down
33 changes: 13 additions & 20 deletions src/core/libraries/avplayer/avplayer_file_streamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,37 @@ extern "C" {
#include <libavformat/avio.h>
}

#include <algorithm> // std::max, std::min

#define AVPLAYER_AVIO_BUFFER_SIZE 4096

namespace Libraries::AvPlayer {

AvPlayerFileStreamer::AvPlayerFileStreamer(SceAvPlayerFileReplacement& file_replacement,
AvPlayerFileStreamer::AvPlayerFileStreamer(const SceAvPlayerFileReplacement& file_replacement,
std::string_view path)
: m_file_replacement(file_replacement) {
Init(path);
const auto ptr = m_file_replacement.object_ptr;
m_fd = m_file_replacement.open(ptr, path.data());
ASSERT(m_fd >= 0);
m_file_size = m_file_replacement.size(ptr);
// avio_buffer is deallocated in `avio_context_free`
const auto avio_buffer = reinterpret_cast<u8*>(av_malloc(AVPLAYER_AVIO_BUFFER_SIZE));
m_avio_context =
avio_alloc_context(avio_buffer, AVPLAYER_AVIO_BUFFER_SIZE, 0, this,
&AvPlayerFileStreamer::ReadPacket, nullptr, &AvPlayerFileStreamer::Seek);
}

AvPlayerFileStreamer::~AvPlayerFileStreamer() {
if (m_avio_context != nullptr) {
avio_context_free(&m_avio_context);
}
if (m_avio_buffer != nullptr) {
av_free(m_avio_buffer);
}
if (m_file_replacement.close != nullptr && m_fd >= 0) {
const auto close = m_file_replacement.close;
const auto ptr = m_file_replacement.object_ptr;
close(ptr);
}
}

s32 AvPlayerFileStreamer::Init(std::string_view path) {
const auto ptr = m_file_replacement.object_ptr;
m_fd = m_file_replacement.open(ptr, path.data());
if (m_fd < 0) {
return -1;
}
m_file_size = m_file_replacement.size(ptr);
m_avio_buffer = reinterpret_cast<u8*>(av_malloc(AVPLAYER_AVIO_BUFFER_SIZE));
m_avio_context =
avio_alloc_context(m_avio_buffer, AVPLAYER_AVIO_BUFFER_SIZE, 0, this,
&AvPlayerFileStreamer::ReadPacket, nullptr, &AvPlayerFileStreamer::Seek);
return 0;
}

s32 AvPlayerFileStreamer::ReadPacket(void* opaque, u8* buffer, s32 size) {
const auto self = reinterpret_cast<AvPlayerFileStreamer*>(opaque);
if (self->m_position >= self->m_file_size) {
Expand All @@ -61,7 +54,7 @@ s32 AvPlayerFileStreamer::ReadPacket(void* opaque, u8* buffer, s32 size) {
const auto read_offset = self->m_file_replacement.readOffset;
const auto ptr = self->m_file_replacement.object_ptr;
const auto bytes_read = read_offset(ptr, buffer, self->m_position, size);
if (size != 0 && bytes_read == 0) {
if (bytes_read == 0 && size != 0) {
return AVERROR_EOF;
}
self->m_position += bytes_read;
Expand Down
5 changes: 1 addition & 4 deletions src/core/libraries/avplayer/avplayer_file_streamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ namespace Libraries::AvPlayer {

class AvPlayerFileStreamer : public IDataStreamer {
public:
AvPlayerFileStreamer(SceAvPlayerFileReplacement& file_replacement, std::string_view path);
AvPlayerFileStreamer(const SceAvPlayerFileReplacement& file_replacement, std::string_view path);
~AvPlayerFileStreamer();

AVIOContext* GetContext() override {
return m_avio_context;
}

private:
s32 Init(std::string_view path);

static s32 ReadPacket(void* opaque, u8* buffer, s32 size);
static s64 Seek(void* opaque, s64 buffer, int whence);

Expand All @@ -33,7 +31,6 @@ class AvPlayerFileStreamer : public IDataStreamer {
int m_fd = -1;
u64 m_position{};
u64 m_file_size{};
u8* m_avio_buffer{};
AVIOContext* m_avio_context{};
};

Expand Down
18 changes: 11 additions & 7 deletions src/core/libraries/avplayer/avplayer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ u64 PS4_SYSV_ABI AvPlayer::SizeFile(void* handle) {
return size(ptr);
}

AvPlayer::AvPlayer() : m_file_io_mutex(PTHREAD_MUTEX_ERRORCHECK, "SceAvPlayerFileIOLock") {}

void AvPlayer::Init(const SceAvPlayerInitData& data, const ThreadPriorities& priorities) {
m_init_data = data;
m_init_data_original = data;
AvPlayer::AvPlayer(const SceAvPlayerInitData& data, const ThreadPriorities& priorities)
: m_file_io_mutex(PTHREAD_MUTEX_ERRORCHECK, "AvPlayer_FileIO"), m_init_data(data),
m_init_data_original(data) {

m_init_data.memory_replacement.object_ptr = this;
m_init_data.memory_replacement.allocate = &AvPlayer::Allocate;
Expand Down Expand Up @@ -120,6 +118,9 @@ s32 AvPlayer::AddSource(std::string_view path) {
}

s32 AvPlayer::GetStreamCount() {
if (m_state == nullptr) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return m_state->GetStreamCount();
}

Expand All @@ -130,11 +131,14 @@ s32 AvPlayer::GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info) {
return ORBIS_OK;
}

s32 AvPlayer::EnableStream(u32 stream_id) {
s32 AvPlayer::EnableStream(u32 stream_index) {
if (m_state == nullptr) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return m_state->EnableStream(stream_id);
if (!m_state->EnableStream(stream_index)) {
return ORBIS_AVPLAYER_ERROR_OPERATION_FAILED;
}
return ORBIS_OK;
}

s32 AvPlayer::Start() {
Expand Down
6 changes: 2 additions & 4 deletions src/core/libraries/avplayer/avplayer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ class AvPlayer {
static int PS4_SYSV_ABI ReadOffsetFile(void* handle, u8* buffer, u64 position, u32 length);
static u64 PS4_SYSV_ABI SizeFile(void* handle);

AvPlayer();

void Init(const SceAvPlayerInitData& data, const ThreadPriorities& priorities);
AvPlayer(const SceAvPlayerInitData& data, const ThreadPriorities& priorities);

s32 PostInit(const SceAvPlayerPostInitData& data);
s32 AddSource(std::string_view filename);
s32 GetStreamCount();
s32 GetStreamInfo(u32 stream_index, SceAvPlayerStreamInfo& info);
s32 EnableStream(u32 stream_id);
s32 EnableStream(u32 stream_index);
s32 Start();
bool GetAudioData(SceAvPlayerFrameInfo& audio_info);
bool GetVideoData(SceAvPlayerFrameInfo& video_info);
Expand Down
Loading

0 comments on commit 587027a

Please sign in to comment.