strawberry-audio-player-win.../src/engine/vlcengine.cpp

350 lines
10 KiB
C++
Raw Normal View History

/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2017-2018, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
2018-02-27 18:06:05 +01:00
#include "config.h"
2023-07-21 05:25:57 +02:00
#include <algorithm>
#include <optional>
#include <vlc/vlc.h>
2018-02-27 18:06:05 +01:00
#include <QtGlobal>
#include <QMetaType>
2020-02-09 02:29:35 +01:00
#include <QVariant>
#include <QByteArray>
#include <QUrl>
2018-02-27 18:06:05 +01:00
#include "core/shared_ptr.h"
#include "core/taskmanager.h"
#include "core/logging.h"
#include "utilities/timeconstants.h"
#include "enginebase.h"
2018-02-27 18:06:05 +01:00
#include "vlcengine.h"
#include "vlcscopedref.h"
using namespace Qt::StringLiterals;
VLCEngine::VLCEngine(SharedPtr<TaskManager> task_manager, QObject *parent)
2023-04-22 19:13:42 +02:00
: EngineBase(parent),
2021-07-11 07:40:57 +02:00
instance_(nullptr),
player_(nullptr),
2023-04-22 19:13:42 +02:00
state_(State::Empty) {
2019-09-15 20:27:32 +02:00
Q_UNUSED(task_manager);
2018-07-01 01:29:52 +02:00
ReloadSettings();
}
VLCEngine::~VLCEngine() {
2018-10-02 00:38:52 +02:00
2023-04-22 19:13:42 +02:00
if (state_ == State::Playing || state_ == State::Paused) {
2019-04-21 22:24:24 +02:00
libvlc_media_player_stop(player_);
}
2019-04-25 00:54:57 +02:00
libvlc_event_manager_t *player_em = libvlc_media_player_event_manager(player_);
if (player_em) {
libvlc_event_detach(player_em, libvlc_MediaPlayerEncounteredError, StateChangedCallback, this);
libvlc_event_detach(player_em, libvlc_MediaPlayerNothingSpecial, StateChangedCallback, this);
libvlc_event_detach(player_em, libvlc_MediaPlayerOpening, StateChangedCallback, this);
libvlc_event_detach(player_em, libvlc_MediaPlayerBuffering, StateChangedCallback, this);
libvlc_event_detach(player_em, libvlc_MediaPlayerPlaying, StateChangedCallback, this);
libvlc_event_detach(player_em, libvlc_MediaPlayerPaused, StateChangedCallback, this);
libvlc_event_detach(player_em, libvlc_MediaPlayerStopped, StateChangedCallback, this);
libvlc_event_detach(player_em, libvlc_MediaPlayerEndReached, StateChangedCallback, this);
}
libvlc_media_player_release(player_);
libvlc_release(instance_);
}
bool VLCEngine::Init() {
2018-02-27 18:06:05 +01:00
// Create the VLC instance
2019-04-21 22:24:24 +02:00
instance_ = libvlc_new(0, nullptr);
2018-10-17 22:55:36 +02:00
if (!instance_) return false;
2018-02-27 18:06:05 +01:00
// Create the media player
player_ = libvlc_media_player_new(instance_);
2018-10-17 22:55:36 +02:00
if (!player_) return false;
2018-02-27 18:06:05 +01:00
// Add event handlers
libvlc_event_manager_t *player_em = libvlc_media_player_event_manager(player_);
2018-10-17 22:55:36 +02:00
if (!player_em) return false;
2018-02-27 18:06:05 +01:00
AttachCallback(player_em, libvlc_MediaPlayerEncounteredError, StateChangedCallback);
AttachCallback(player_em, libvlc_MediaPlayerNothingSpecial, StateChangedCallback);
AttachCallback(player_em, libvlc_MediaPlayerOpening, StateChangedCallback);
AttachCallback(player_em, libvlc_MediaPlayerBuffering, StateChangedCallback);
AttachCallback(player_em, libvlc_MediaPlayerPlaying, StateChangedCallback);
AttachCallback(player_em, libvlc_MediaPlayerPaused, StateChangedCallback);
AttachCallback(player_em, libvlc_MediaPlayerStopped, StateChangedCallback);
AttachCallback(player_em, libvlc_MediaPlayerEndReached, StateChangedCallback);
return true;
}
bool VLCEngine::Load(const QUrl &media_url, const QUrl &stream_url, const EngineBase::TrackChangeFlags change, const bool force_stop_at_end, const quint64 beginning_nanosec, const qint64 end_nanosec, const std::optional<double> ebur128_integrated_loudness_lufs) {
// FIXME: why is this not calling `EngineBase::Load()`?
2019-09-15 20:27:32 +02:00
2023-04-21 16:20:00 +02:00
Q_UNUSED(media_url);
Q_UNUSED(ebur128_integrated_loudness_lufs);
2019-09-15 20:27:32 +02:00
Q_UNUSED(change);
Q_UNUSED(force_stop_at_end);
Q_UNUSED(beginning_nanosec);
Q_UNUSED(end_nanosec);
2020-10-17 17:29:09 +02:00
if (!Initialized()) return false;
2018-02-27 18:06:05 +01:00
// Create the media object
VlcScopedRef<libvlc_media_t> media(libvlc_media_new_location(instance_, stream_url.toEncoded().constData()));
2018-02-27 18:06:05 +01:00
libvlc_media_player_set_media(player_, media);
return true;
2018-02-27 18:06:05 +01:00
}
bool VLCEngine::Play(const bool pause, const quint64 offset_nanosec) {
Q_UNUSED(pause);
2020-10-17 17:29:09 +02:00
if (!Initialized()) return false;
// Set audio output
if (!output_.isEmpty() && output_ != "auto"_L1) {
int result = libvlc_audio_output_set(player_, output_.toUtf8().constData());
2019-04-23 19:20:46 +02:00
if (result != 0) qLog(Error) << "Failed to set output to" << output_;
}
// Set audio device
if (device_.isValid() && device_.metaType().id() == QMetaType::QString && !device_.toString().isEmpty()) {
libvlc_audio_output_device_set(player_, nullptr, device_.toString().toLocal8Bit().data());
}
int result = libvlc_media_player_play(player_);
if (result != 0) return false;
2018-02-27 18:06:05 +01:00
Seek(offset_nanosec);
return true;
}
2019-09-15 20:27:32 +02:00
void VLCEngine::Stop(const bool stop_after) {
Q_UNUSED(stop_after);
2020-10-17 17:29:09 +02:00
if (!Initialized()) return;
2018-02-27 18:06:05 +01:00
libvlc_media_player_stop(player_);
2018-02-27 18:06:05 +01:00
}
void VLCEngine::Pause() {
2020-10-17 17:29:09 +02:00
if (!Initialized()) return;
2018-02-27 18:06:05 +01:00
libvlc_media_player_pause(player_);
2018-02-27 18:06:05 +01:00
}
void VLCEngine::Unpause() {
2020-10-17 17:29:09 +02:00
if (!Initialized()) return;
2018-02-27 18:06:05 +01:00
libvlc_media_player_play(player_);
}
2019-09-15 20:27:32 +02:00
void VLCEngine::Seek(const quint64 offset_nanosec) {
2020-10-17 17:29:09 +02:00
if (!Initialized()) return;
2021-03-21 18:53:02 +01:00
int offset = static_cast<int>(offset_nanosec / kNsecPerMsec);
uint len = length();
if (len == 0) return;
2021-10-30 02:21:29 +02:00
float pos = static_cast<float>(offset) / static_cast<float>(len);
libvlc_media_player_set_position(player_, pos);
}
2019-09-15 20:27:32 +02:00
void VLCEngine::SetVolumeSW(const uint percent) {
2021-08-23 21:21:08 +02:00
2020-10-17 17:29:09 +02:00
if (!Initialized()) return;
2019-03-09 16:48:45 +01:00
if (!volume_control_ && percent != 100) return;
2021-10-30 02:21:29 +02:00
libvlc_audio_set_volume(player_, static_cast<int>(percent));
2021-08-23 21:21:08 +02:00
}
qint64 VLCEngine::position_nanosec() const {
2021-08-23 21:21:08 +02:00
2023-04-22 19:13:42 +02:00
if (state_ == State::Empty) return 0;
const qint64 result = (position() * kNsecPerMsec);
return qMax(0LL, result);
2021-08-23 21:21:08 +02:00
}
qint64 VLCEngine::length_nanosec() const {
2021-08-23 21:21:08 +02:00
2023-04-22 19:13:42 +02:00
if (state_ == State::Empty) return 0;
2021-10-30 02:21:29 +02:00
const qint64 result = (end_nanosec_ - static_cast<qint64>(beginning_nanosec_));
if (result > 0) {
return result;
}
2024-06-12 17:38:58 +02:00
// Get the length from the pipeline if we don't know.
return (length() * kNsecPerMsec);
2021-08-23 21:21:08 +02:00
}
EngineBase::OutputDetailsList VLCEngine::GetOutputsList() const {
2023-04-22 17:18:29 +02:00
OutputDetailsList outputs;
OutputDetails output_auto;
output_auto.name = "auto"_L1;
output_auto.description = "Automatically detected"_L1;
2023-04-22 17:18:29 +02:00
outputs << output_auto;
libvlc_audio_output_t *audio_output_list = libvlc_audio_output_list_get(instance_);
for (libvlc_audio_output_t *audio_output = audio_output_list; audio_output; audio_output = audio_output->p_next) {
OutputDetails output;
2023-04-22 17:18:29 +02:00
output.name = QString::fromUtf8(audio_output->psz_name);
output.description = QString::fromUtf8(audio_output->psz_description);
if (output.name == "auto"_L1) output.iconname = "soundcard"_L1;
else if ((output.name == "alsa"_L1)||(output.name == "oss"_L1)) output.iconname = "alsa"_L1;
else if (output.name== "jack"_L1) output.iconname = "jack"_L1;
else if (output.name == "pulse"_L1) output.iconname = "pulseaudio"_L1;
else if (output.name == "afile"_L1) output.iconname = "document-new"_L1;
else output.iconname = "soundcard"_L1;
2023-04-22 17:18:29 +02:00
outputs << output;
}
2023-04-22 17:18:29 +02:00
libvlc_audio_output_list_release(audio_output_list);
2023-04-22 17:18:29 +02:00
return outputs;
}
2018-07-01 01:29:52 +02:00
bool VLCEngine::ValidOutput(const QString &output) {
2023-04-22 17:18:29 +02:00
const OutputDetailsList output_details = GetOutputsList();
return std::any_of(output_details.begin(), output_details.end(), [output](const OutputDetails &output_detail) { return output_detail.name == output; });
2018-07-01 01:29:52 +02:00
}
2024-08-02 23:35:52 +02:00
bool VLCEngine::CustomDeviceSupport(const QString &output) const {
return output != "auto"_L1;
2018-02-27 18:06:05 +01:00
}
2024-08-02 23:35:52 +02:00
bool VLCEngine::ALSADeviceSupport(const QString &output) const {
return output == "alsa"_L1;
}
2024-08-02 23:35:52 +02:00
bool VLCEngine::ExclusiveModeSupport(const QString &output) const {
2024-02-20 01:08:00 +01:00
Q_UNUSED(output);
return false;
}
2018-02-27 18:06:05 +01:00
uint VLCEngine::position() const {
2021-06-22 13:54:58 +02:00
if (!Initialized() || !libvlc_media_player_is_playing(player_)) return 0;
2018-02-27 18:06:05 +01:00
float pos = libvlc_media_player_get_position(player_);
return static_cast<uint>(pos * static_cast<float>(length()));
2018-02-27 18:06:05 +01:00
}
uint VLCEngine::length() const {
2021-06-22 13:54:58 +02:00
if (!Initialized() || !libvlc_media_player_is_playing(player_)) return 0;
2018-02-27 18:06:05 +01:00
return libvlc_media_player_get_length(player_);
2018-02-27 18:06:05 +01:00
}
2019-09-15 20:27:32 +02:00
bool VLCEngine::CanDecode(const QUrl &url) { Q_UNUSED(url); return true; }
2018-02-27 18:06:05 +01:00
void VLCEngine::AttachCallback(libvlc_event_manager_t *em, libvlc_event_type_t type, libvlc_callback_t callback) {
2019-04-23 19:20:46 +02:00
if (libvlc_event_attach(em, type, callback, this) != 0) {
qLog(Error) << "Failed to attach callback.";
}
2018-02-27 18:06:05 +01:00
}
void VLCEngine::StateChangedCallback(const libvlc_event_t *e, void *data) {
2018-10-02 00:38:52 +02:00
VLCEngine *engine = reinterpret_cast<VLCEngine*>(data);
2018-02-27 18:06:05 +01:00
switch (e->type) {
case libvlc_MediaPlayerNothingSpecial:
case libvlc_MediaPlayerOpening:
case libvlc_MediaPlayerBuffering:
2018-10-30 23:40:05 +01:00
break;
case libvlc_MediaPlayerStopped:{
2023-04-22 19:13:42 +02:00
const EngineBase::State state = engine->state_;
engine->state_ = EngineBase::State::Empty;
if (state == EngineBase::State::Playing) {
2024-08-25 01:06:30 +02:00
Q_EMIT engine->StateChanged(engine->state_);
}
2018-10-30 23:40:05 +01:00
break;
}
2018-10-30 23:40:05 +01:00
case libvlc_MediaPlayerEncounteredError:
2023-04-22 19:13:42 +02:00
engine->state_ = EngineBase::State::Error;
2024-08-25 01:06:30 +02:00
Q_EMIT engine->StateChanged(engine->state_);
Q_EMIT engine->FatalError();
break;
2018-02-27 18:06:05 +01:00
case libvlc_MediaPlayerPlaying:
2023-04-22 19:13:42 +02:00
engine->state_ = EngineBase::State::Playing;
2024-08-25 01:06:30 +02:00
Q_EMIT engine->StateChanged(engine->state_);
break;
2018-02-27 18:06:05 +01:00
case libvlc_MediaPlayerPaused:
2023-04-22 19:13:42 +02:00
engine->state_ = EngineBase::State::Paused;
2024-08-25 01:06:30 +02:00
Q_EMIT engine->StateChanged(engine->state_);
break;
2018-02-27 18:06:05 +01:00
case libvlc_MediaPlayerEndReached:
2023-04-22 19:13:42 +02:00
engine->state_ = EngineBase::State::Idle;
2024-08-25 01:06:30 +02:00
Q_EMIT engine->TrackEnded();
break;
}
2018-02-27 18:06:05 +01:00
}
2018-02-27 18:06:05 +01:00
2019-09-15 20:27:32 +02:00
void VLCEngine::GetDevicesList(const QString &output) const {
Q_UNUSED(output);
libvlc_audio_output_device_t *audio_output_device_list = libvlc_audio_output_device_list_get(instance_, output_.toUtf8().constData());
2021-08-23 21:21:08 +02:00
for (libvlc_audio_output_device_t *audio_device = audio_output_device_list; audio_device; audio_device = audio_device->p_next) {
qLog(Debug) << audio_device->psz_device << audio_device->psz_description;
}
libvlc_audio_output_device_list_release(audio_output_device_list);
2018-02-27 18:06:05 +01:00
}