2010-04-04 22:45:03 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vlcengine.h"
|
|
|
|
#include "vlcscopedref.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QtDebug>
|
2010-04-05 04:21:53 +02:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QTime>
|
2010-04-04 22:45:03 +02:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2010-04-05 04:21:53 +02:00
|
|
|
VlcEngine* VlcEngine::sInstance = NULL;
|
|
|
|
|
2010-04-04 22:45:03 +02:00
|
|
|
VlcEngine::VlcEngine()
|
|
|
|
: instance_(NULL),
|
|
|
|
player_(NULL),
|
2010-04-05 04:21:53 +02:00
|
|
|
scope_data_(4096),
|
2010-04-04 22:45:03 +02:00
|
|
|
state_(Engine::Empty)
|
|
|
|
{
|
|
|
|
static const char * const args[] = {
|
|
|
|
"-I", "dummy", // Don't use any interface
|
|
|
|
"--ignore-config", // Don't use VLC's config
|
|
|
|
"--extraintf=logger", // log anything
|
|
|
|
"--verbose=2", // be much more verbose then normal for debugging purpose
|
2010-04-05 04:21:53 +02:00
|
|
|
|
|
|
|
// Our scope plugin
|
|
|
|
"--audio-filter=clementine_scope",
|
|
|
|
"--no-plugins-cache",
|
2010-04-04 22:45:03 +02:00
|
|
|
|
2010-04-05 15:38:12 +02:00
|
|
|
// Try to stop audio stuttering
|
|
|
|
"--file-caching=500", // msec
|
|
|
|
"--http-caching=500",
|
|
|
|
|
2010-04-04 22:45:03 +02:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
|
|
|
|
"--aout=alsa", // The default, pulseaudio, is buggy
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the VLC instance
|
|
|
|
libvlc_exception_init(&exception_);
|
|
|
|
instance_ = libvlc_new(sizeof(args) / sizeof(args[0]), args, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
|
|
|
|
// Create the media player
|
|
|
|
player_ = libvlc_media_player_new(instance_, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
|
|
|
|
// Add event handlers
|
|
|
|
libvlc_event_manager_t* player_em =
|
|
|
|
libvlc_media_player_event_manager(player_, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
|
|
|
|
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);
|
|
|
|
HandleErrors();
|
2010-04-05 04:21:53 +02:00
|
|
|
|
|
|
|
sInstance = this;
|
2010-04-04 22:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VlcEngine::~VlcEngine() {
|
|
|
|
libvlc_media_player_stop(player_, &exception_);
|
|
|
|
libvlc_media_player_release(player_);
|
|
|
|
libvlc_release(instance_);
|
|
|
|
HandleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VlcEngine::AttachCallback(libvlc_event_manager_t* em, libvlc_event_type_t type,
|
|
|
|
libvlc_callback_t callback) {
|
|
|
|
libvlc_event_attach(em, type, callback, this, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VlcEngine::StateChangedCallback(const libvlc_event_t* e, void* data) {
|
|
|
|
VlcEngine* engine = reinterpret_cast<VlcEngine*>(data);
|
|
|
|
|
|
|
|
switch (e->type) {
|
|
|
|
case libvlc_MediaPlayerNothingSpecial:
|
|
|
|
case libvlc_MediaPlayerStopped:
|
|
|
|
case libvlc_MediaPlayerEncounteredError:
|
|
|
|
engine->state_ = Engine::Empty;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case libvlc_MediaPlayerOpening:
|
|
|
|
case libvlc_MediaPlayerBuffering:
|
|
|
|
case libvlc_MediaPlayerPlaying:
|
|
|
|
engine->state_ = Engine::Playing;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case libvlc_MediaPlayerPaused:
|
|
|
|
engine->state_ = Engine::Paused;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case libvlc_MediaPlayerEndReached:
|
|
|
|
engine->state_ = Engine::Idle;
|
2010-04-15 14:39:34 +02:00
|
|
|
emit engine->TrackEnded();
|
2010-04-05 15:26:11 +02:00
|
|
|
return; // Don't emit state changed here
|
2010-04-04 22:45:03 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
emit engine->StateChanged(engine->state_);
|
2010-04-04 22:45:03 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
bool VlcEngine::Init() {
|
2010-04-04 22:45:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
bool VlcEngine::CanDecode(const QUrl &url) {
|
2010-04-04 22:45:03 +02:00
|
|
|
// TODO
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
bool VlcEngine::Load(const QUrl &url, Engine::TrackChangeType change) {
|
2010-04-05 21:16:48 +02:00
|
|
|
// Create the media object
|
2010-04-04 22:45:03 +02:00
|
|
|
VlcScopedRef<libvlc_media_t> media(
|
|
|
|
libvlc_media_new(instance_, url.toEncoded().constData(), &exception_));
|
|
|
|
if (libvlc_exception_raised(&exception_))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
libvlc_media_player_set_media(player_, media, &exception_);
|
|
|
|
if (libvlc_exception_raised(&exception_))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
bool VlcEngine::Play(uint offset) {
|
2010-04-04 22:45:03 +02:00
|
|
|
libvlc_media_player_play(player_, &exception_);
|
|
|
|
if (libvlc_exception_raised(&exception_))
|
|
|
|
return false;
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
Seek(offset);
|
2010-04-04 22:45:03 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
void VlcEngine::Stop() {
|
2010-04-04 22:45:03 +02:00
|
|
|
libvlc_media_player_stop(player_, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
void VlcEngine::Pause() {
|
2010-04-04 22:45:03 +02:00
|
|
|
libvlc_media_player_pause(player_, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
void VlcEngine::Unpause() {
|
2010-04-04 22:45:03 +02:00
|
|
|
libvlc_media_player_play(player_, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint VlcEngine::position() const {
|
|
|
|
bool is_playing = libvlc_media_player_is_playing(
|
|
|
|
player_, const_cast<libvlc_exception_t*>(&exception_));
|
|
|
|
HandleErrors();
|
|
|
|
|
|
|
|
if (!is_playing)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
float pos = libvlc_media_player_get_position(
|
|
|
|
player_, const_cast<libvlc_exception_t*>(&exception_));
|
|
|
|
HandleErrors();
|
|
|
|
|
|
|
|
return pos * length();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint VlcEngine::length() const {
|
|
|
|
bool is_playing = libvlc_media_player_is_playing(
|
|
|
|
player_, const_cast<libvlc_exception_t*>(&exception_));
|
|
|
|
HandleErrors();
|
|
|
|
|
|
|
|
if (!is_playing)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
libvlc_time_t len = libvlc_media_player_get_length(
|
|
|
|
player_, const_cast<libvlc_exception_t*>(&exception_));
|
|
|
|
HandleErrors();
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
void VlcEngine::Seek(uint ms) {
|
2010-04-04 22:45:03 +02:00
|
|
|
uint len = length();
|
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
float pos = float(ms) / len;
|
|
|
|
|
|
|
|
libvlc_media_player_set_position(player_, pos, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
void VlcEngine::SetVolumeSW(uint volume) {
|
2010-04-04 22:45:03 +02:00
|
|
|
libvlc_audio_set_volume(instance_, volume, &exception_);
|
|
|
|
HandleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VlcEngine::HandleErrors() const {
|
|
|
|
if (libvlc_exception_raised(&exception_)) {
|
|
|
|
qFatal("libvlc error: %s", libvlc_exception_get_message(&exception_));
|
|
|
|
}
|
|
|
|
}
|
2010-04-05 04:21:53 +02:00
|
|
|
|
|
|
|
void VlcEngine::SetScopeData(float* data, int size) {
|
|
|
|
if (!sInstance)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMutexLocker l(&sInstance->scope_mutex_);
|
|
|
|
|
|
|
|
// This gets called by our VLC plugin. Just push the data on to the end of
|
|
|
|
// the circular buffer and let it get consumed by scope()
|
|
|
|
for (int i=0 ; i<size ; ++i) {
|
|
|
|
sInstance->scope_data_.push_back(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
const Engine::Scope& VlcEngine::Scope() {
|
2010-04-05 04:21:53 +02:00
|
|
|
QMutexLocker l(&scope_mutex_);
|
|
|
|
|
|
|
|
// Leave the scope unchanged if there's not enough data
|
2010-04-15 14:39:34 +02:00
|
|
|
if (scope_data_.size() < uint(kScopeSize))
|
|
|
|
return scope_;
|
2010-04-05 04:21:53 +02:00
|
|
|
|
|
|
|
// Take the samples off the front of the circular buffer
|
2010-04-15 14:39:34 +02:00
|
|
|
for (uint i=0 ; i<uint(kScopeSize) ; ++i)
|
|
|
|
scope_[i] = scope_data_[i] * (1 << 15);
|
2010-04-05 04:21:53 +02:00
|
|
|
|
|
|
|
// Remove the samples from the buffer. Unfortunately I think this is O(n) :(
|
2010-04-15 14:39:34 +02:00
|
|
|
scope_data_.rresize(qMax(0, int(scope_data_.size()) - kScopeSize*2));
|
2010-04-05 04:21:53 +02:00
|
|
|
|
2010-04-15 14:39:34 +02:00
|
|
|
return scope_;
|
2010-04-05 04:21:53 +02:00
|
|
|
}
|