-
Notifications
You must be signed in to change notification settings - Fork 96
/
audioplayerfactory.cc
80 lines (71 loc) · 2.71 KB
/
audioplayerfactory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* This file is (c) 2018 Igor Kushnir <igorkuo@gmail.com>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include <QScopedPointer>
#include <QObject>
#include "audioplayerfactory.hh"
#include "ffmpegaudioplayer.hh"
#include "multimediaaudioplayer.hh"
#include "externalaudioplayer.hh"
#include "gddebug.hh"
AudioPlayerFactory::AudioPlayerFactory( Config::Preferences const & p ):
useInternalPlayer( p.useInternalPlayer ),
internalPlayerBackend( p.internalPlayerBackend ),
audioPlaybackProgram( p.audioPlaybackProgram )
{
reset();
}
void AudioPlayerFactory::setPreferences( Config::Preferences const & p )
{
if ( p.useInternalPlayer != useInternalPlayer ) {
useInternalPlayer = p.useInternalPlayer;
internalPlayerBackend = p.internalPlayerBackend;
audioPlaybackProgram = p.audioPlaybackProgram;
reset();
}
else if ( useInternalPlayer && p.internalPlayerBackend != internalPlayerBackend ) {
internalPlayerBackend = p.internalPlayerBackend;
reset();
}
else if ( !useInternalPlayer && p.audioPlaybackProgram != audioPlaybackProgram ) {
audioPlaybackProgram = p.audioPlaybackProgram;
ExternalAudioPlayer * const externalPlayer = qobject_cast< ExternalAudioPlayer * >( playerPtr.data() );
if ( externalPlayer ) {
setAudioPlaybackProgram( *externalPlayer );
}
else {
gdWarning( "External player was expected, but it does not exist.\n" );
}
}
}
void AudioPlayerFactory::reset()
{
if ( useInternalPlayer ) {
// qobject_cast checks below account for the case when an unsupported backend
// is stored in config. After this backend is replaced with the default one
// upon preferences saving, the code below does not reset playerPtr with
// another object of the same type.
#ifdef MAKE_FFMPEG_PLAYER
Q_ASSERT( Config::InternalPlayerBackend::defaultBackend().isFfmpeg()
&& "Adjust the code below after changing the default backend." );
if ( !internalPlayerBackend.isQtmultimedia() ) {
if ( !playerPtr || !qobject_cast< Ffmpeg::AudioPlayer * >( playerPtr.data() ) ) {
playerPtr.reset( new Ffmpeg::AudioPlayer );
}
return;
}
#endif
#ifdef MAKE_QTMULTIMEDIA_PLAYER
if ( !playerPtr || !qobject_cast< MultimediaAudioPlayer * >( playerPtr.data() ) ) {
playerPtr.reset( new MultimediaAudioPlayer );
}
return;
#endif
}
std::unique_ptr< ExternalAudioPlayer > externalPlayer( new ExternalAudioPlayer );
setAudioPlaybackProgram( *externalPlayer );
playerPtr.reset( externalPlayer.release() );
}
void AudioPlayerFactory::setAudioPlaybackProgram( ExternalAudioPlayer & externalPlayer )
{
externalPlayer.setPlayerCommandLine( audioPlaybackProgram.trimmed() );
}