culturered_client/Common/VlcMediaListPlayer.cpp
2024-09-07 11:34:44 +08:00

169 lines
5.2 KiB
C++

#include "VlcMediaListPlayer.h"
#if USE_VLC
#include <assert.h>
#ifdef _WIN32
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#endif
#include <vlc/vlc.h>
#include <QDebug>
#include <QFileInfo>
#include <QImage>
#include "VlcMediaPlayer.h"
void* MediaListMediaCallbakLocak(void* opaque, void** planes) {
VlcMediaListPlayer* player = reinterpret_cast<VlcMediaListPlayer*>(opaque);
player->mutx_.lock();
*planes = player->pixels_.data();
return nullptr;
}
void MediaListMediaCallbakUnLocak(void* opaque, void* picture, void* const* planes) {
VlcMediaListPlayer* player = reinterpret_cast<VlcMediaListPlayer*>(opaque);
unsigned char* data = (unsigned char*)*planes;
QImage image(data, player->videoWidth_, player->videoHeight_, QImage::Format_ARGB32);
emit player->VideoDataOutput(std::move(image));
player->mutx_.unlock();
}
void MediaListMediaCallbakDisplay(void* opaque, void* picture) {
}
void OnMediaListVLCEvent(const libvlc_event_t* event, void* data) {
VlcMediaListPlayer* player = reinterpret_cast<VlcMediaListPlayer*>(data);
switch (event->type) {
case libvlc_MediaListPlayerStopped:
player->Stop();
break;
case libvlc_MediaPlayerEndReached:
emit player->Stopped();
break;
case libvlc_MediaPlayerStopped:
emit player->Stopped();
break;
case libvlc_MediaPlayerPositionChanged: {
float pos = event->u.media_player_position_changed.new_position;
player->OnPostionChangedCallback(pos);
}
break;
case libvlc_MediaPlayerTimeChanged: {
qint64 pos = event->u.media_player_time_changed.new_time;
player->OnTimeChangedCallback(pos);
}
break;
default:
break;
}
}
VlcMediaListPlayer::VlcMediaListPlayer() {
libvlc_instance_t* inst = VlcMediaPlayer::GetVLCInstance();
vlcListPlayer_ = libvlc_media_list_player_new(inst);
vlcPlayer_ = libvlc_media_player_new(inst);
libvlc_video_set_key_input(vlcPlayer_, false);
libvlc_video_set_mouse_input(vlcPlayer_, false);
libvlc_audio_set_volume(vlcPlayer_, int(200));
videoWidth_ = 1920;
videoHeight_ = 1080;
pixels_.resize(videoWidth_ * videoHeight_ * 4, 0);
libvlc_video_set_callbacks(vlcPlayer_, MediaListMediaCallbakLocak, MediaListMediaCallbakUnLocak,
MediaListMediaCallbakDisplay, this);
libvlc_video_set_format(vlcPlayer_, "RV32", videoWidth_, videoHeight_, videoWidth_ * 4);
libvlc_event_manager_t* vlc_evt_man = libvlc_media_player_event_manager(vlcPlayer_);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPlaying, ::OnMediaListVLCEvent, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPositionChanged, ::OnMediaListVLCEvent, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerTimeChanged, ::OnMediaListVLCEvent, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerStopped, ::OnMediaListVLCEvent, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEndReached, ::OnMediaListVLCEvent, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaListPlayerStopped, ::OnMediaListVLCEvent, this);
vlcMediaList_ = libvlc_media_list_new(inst);
// libvlc_media_list_player_set_media_list(vlcListPlayer_, vlcMediaList_);
}
VlcMediaListPlayer::~VlcMediaListPlayer() {
if (nullptr != vlcMediaList_) {
libvlc_media_list_release(vlcMediaList_);
vlcMediaList_ = nullptr;
}
if (nullptr != vlcPlayer_) {
libvlc_media_player_release(vlcPlayer_);
vlcPlayer_ = nullptr;
}
if (nullptr != vlcListPlayer_) {
libvlc_media_list_player_release(vlcListPlayer_);
vlcListPlayer_ = nullptr;
}
}
void VlcMediaListPlayer::SetResolution(int width, int height) {
videoWidth_ = width;
videoHeight_ = height;
pixels_.resize(videoWidth_ * videoHeight_ * 4, 0);
libvlc_video_set_format(vlcPlayer_, "RV32", videoWidth_, videoHeight_, videoWidth_ * 4);
}
void VlcMediaListPlayer::SetMediaList(const QStringList& paths) {
libvlc_instance_t* inst = VlcMediaPlayer::GetVLCInstance();
for (int i = 0; i < paths.size(); i++) {
const QString& path = paths[i];
libvlc_media_t* media = libvlc_media_new_path(inst, path.toUtf8().replace("/", "\\").data());
libvlc_media_list_add_media(vlcMediaList_, media);
libvlc_media_parse(media);
libvlc_media_release(media);
}
libvlc_media_list_player_set_media_list(vlcListPlayer_, vlcMediaList_);
libvlc_media_list_player_set_media_player(vlcListPlayer_, vlcPlayer_);
}
bool VlcMediaListPlayer::Play() {
libvlc_media_list_player_play(vlcListPlayer_);
return true;
}
void VlcMediaListPlayer::Stop() {
libvlc_media_list_player_stop(vlcListPlayer_);
}
bool VlcMediaListPlayer::IsPlaying() const {
if (nullptr != vlcPlayer_) {
return 1 == libvlc_media_list_player_is_playing(vlcListPlayer_);
}
return false;
}
void VlcMediaListPlayer::SetPlayMode(PlayMode model) {
libvlc_media_list_player_set_playback_mode(vlcListPlayer_, static_cast<libvlc_playback_mode_t>(model));
}
void VlcMediaListPlayer::OnPostionChangedCallback(float pos) {}
void VlcMediaListPlayer::OnTimeChangedCallback(qint64 pos) {
emit TimeChanged(pos);
}
#endif USE_VLC