Commit fb9c11df authored by Ronan Abhamon's avatar Ronan Abhamon

fix(src/components/sound-player/SoundPlayer): handle eof

parent b4739025
...@@ -401,6 +401,7 @@ ...@@ -401,6 +401,7 @@
<file>ui/views/App/Styles/Main/MainWindowStyle.qml</file> <file>ui/views/App/Styles/Main/MainWindowStyle.qml</file>
<file>ui/views/App/Styles/Main/ManageAccountsStyle.qml</file> <file>ui/views/App/Styles/Main/ManageAccountsStyle.qml</file>
<file>ui/views/App/Styles/qmldir</file> <file>ui/views/App/Styles/qmldir</file>
<file>ui/views/App/Styles/Settings/SettingsAudioStyle.qml</file>
<file>ui/views/App/Styles/Settings/SettingsSipAccountsEditStyle.qml</file> <file>ui/views/App/Styles/Settings/SettingsSipAccountsEditStyle.qml</file>
<file>ui/views/App/Styles/Settings/SettingsVideoPreviewStyle.qml</file> <file>ui/views/App/Styles/Settings/SettingsVideoPreviewStyle.qml</file>
<file>ui/views/App/Styles/Settings/SettingsWindowStyle.qml</file> <file>ui/views/App/Styles/Settings/SettingsWindowStyle.qml</file>
......
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
#include <QtConcurrent> #include <QtConcurrent>
#include <QTimer> #include <QTimer>
#define CBS_CALL_INTERVAL 20
using namespace std; using namespace std;
// ============================================================================= // =============================================================================
...@@ -66,7 +68,7 @@ void CoreManager::init (QObject *parent, const QString &configPath) { ...@@ -66,7 +68,7 @@ void CoreManager::init (QObject *parent, const QString &configPath) {
mInstance = new CoreManager(parent, configPath); mInstance = new CoreManager(parent, configPath);
QTimer *timer = mInstance->mCbsTimer = new QTimer(mInstance); QTimer *timer = mInstance->mCbsTimer = new QTimer(mInstance);
timer->setInterval(20); timer->setInterval(CBS_CALL_INTERVAL);
QObject::connect(timer, &QTimer::timeout, mInstance, &CoreManager::iterate); QObject::connect(timer, &QTimer::timeout, mInstance, &CoreManager::iterate);
} }
......
...@@ -20,11 +20,15 @@ ...@@ -20,11 +20,15 @@
* Author: Ronan Abhamon * Author: Ronan Abhamon
*/ */
#include <QTimer>
#include "../../Utils.hpp" #include "../../Utils.hpp"
#include "../core/CoreManager.hpp" #include "../core/CoreManager.hpp"
#include "SoundPlayer.hpp" #include "SoundPlayer.hpp"
#define FORCE_CLOSE_TIMER_INTERVAL 20
using namespace std; using namespace std;
// ============================================================================= // =============================================================================
...@@ -37,7 +41,14 @@ public: ...@@ -37,7 +41,14 @@ public:
private: private:
void onEofReached (const shared_ptr<linphone::Player> &) override { void onEofReached (const shared_ptr<linphone::Player> &) override {
mSoundPlayer->stop(); QMutex &mutex = mSoundPlayer->mForceCloseMutex;
// Workaround.
// This callback is called in a standard thread of mediastreamer, not a QThread.
// Signals, connect functions, timers... are unavailable.
mutex.lock();
mSoundPlayer->mForceClose = true;
mutex.unlock();
} }
SoundPlayer *mSoundPlayer; SoundPlayer *mSoundPlayer;
...@@ -46,6 +57,11 @@ private: ...@@ -46,6 +57,11 @@ private:
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
SoundPlayer::SoundPlayer (QObject *parent) : QObject(parent) { SoundPlayer::SoundPlayer (QObject *parent) : QObject(parent) {
mForceCloseTimer = new QTimer(this);
mForceCloseTimer->setInterval(FORCE_CLOSE_TIMER_INTERVAL);
QObject::connect(mForceCloseTimer, &QTimer::timeout, this, &SoundPlayer::handleEof);
mHandlers = make_shared<SoundPlayer::Handlers>(this); mHandlers = make_shared<SoundPlayer::Handlers>(this);
mInternalPlayer = CoreManager::getInstance()->getCore()->createLocalPlayer("", "", nullptr); mInternalPlayer = CoreManager::getInstance()->getCore()->createLocalPlayer("", "", nullptr);
...@@ -63,6 +79,7 @@ void SoundPlayer::pause () { ...@@ -63,6 +79,7 @@ void SoundPlayer::pause () {
return; return;
} }
mForceCloseTimer->stop();
mPlaybackState = SoundPlayer::PausedState; mPlaybackState = SoundPlayer::PausedState;
emit paused(); emit paused();
...@@ -87,6 +104,7 @@ void SoundPlayer::play () { ...@@ -87,6 +104,7 @@ void SoundPlayer::play () {
return; return;
} }
mForceCloseTimer->start();
mPlaybackState = SoundPlayer::PlayingState; mPlaybackState = SoundPlayer::PlayingState;
emit playing(); emit playing();
...@@ -97,9 +115,11 @@ void SoundPlayer::stop () { ...@@ -97,9 +115,11 @@ void SoundPlayer::stop () {
if (mPlaybackState == SoundPlayer::StoppedState) if (mPlaybackState == SoundPlayer::StoppedState)
return; return;
mInternalPlayer->close(); mForceCloseTimer->stop();
mPlaybackState = SoundPlayer::StoppedState; mPlaybackState = SoundPlayer::StoppedState;
mInternalPlayer->close();
emit stopped(); emit stopped();
emit playbackStateChanged(mPlaybackState); emit playbackStateChanged(mPlaybackState);
} }
...@@ -118,6 +138,19 @@ int SoundPlayer::getPosition () const { ...@@ -118,6 +138,19 @@ int SoundPlayer::getPosition () const {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void SoundPlayer::handleEof () {
mForceCloseMutex.lock();
if (mForceClose) {
mForceClose = false;
stop();
}
mForceCloseMutex.unlock();
}
// -----------------------------------------------------------------------------
void SoundPlayer::setError (const QString &message) { void SoundPlayer::setError (const QString &message) {
qWarning() << message; qWarning() << message;
mInternalPlayer->close(); mInternalPlayer->close();
......
...@@ -25,10 +25,13 @@ ...@@ -25,10 +25,13 @@
#include <memory> #include <memory>
#include <QMutex>
#include <QObject> #include <QObject>
// ============================================================================= // =============================================================================
class QTimer;
namespace linphone { namespace linphone {
class Player; class Player;
} }
...@@ -55,13 +58,13 @@ public: ...@@ -55,13 +58,13 @@ public:
SoundPlayer (QObject *parent = Q_NULLPTR); SoundPlayer (QObject *parent = Q_NULLPTR);
~SoundPlayer () = default; ~SoundPlayer () = default;
void pause (); Q_INVOKABLE void pause ();
void play (); Q_INVOKABLE void play ();
void stop (); Q_INVOKABLE void stop ();
void seek (int offset); Q_INVOKABLE void seek (int offset);
int getPosition () const; Q_INVOKABLE int getPosition () const;
signals: signals:
void sourceChanged (const QString &source); void sourceChanged (const QString &source);
...@@ -73,6 +76,8 @@ signals: ...@@ -73,6 +76,8 @@ signals:
void playbackStateChanged (PlaybackState playbackState); void playbackStateChanged (PlaybackState playbackState);
private: private:
void handleEof ();
void setError (const QString &message); void setError (const QString &message);
QString getSource () const; QString getSource () const;
...@@ -86,6 +91,11 @@ private: ...@@ -86,6 +91,11 @@ private:
QString mSource; QString mSource;
PlaybackState mPlaybackState = StoppedState; PlaybackState mPlaybackState = StoppedState;
bool mForceClose = false;
QMutex mForceCloseMutex;
QTimer *mForceCloseTimer;
std::shared_ptr<linphone::Player> mInternalPlayer; std::shared_ptr<linphone::Player> mInternalPlayer;
std::shared_ptr<Handlers> mHandlers; std::shared_ptr<Handlers> mHandlers;
}; };
......
...@@ -73,7 +73,40 @@ TabContainer { ...@@ -73,7 +73,40 @@ TabContainer {
FileChooserButton { FileChooserButton {
selectedFile: SettingsModel.ringPath selectedFile: SettingsModel.ringPath
onAccepted: SettingsModel.ringPath = selectedFile onAccepted: {
ringPlayer.stop()
SettingsModel.ringPath = selectedFile
}
ActionSwitch {
anchors {
left: parent.right
leftMargin: SettingsAudioStyle.ringPlayer.leftMargin
}
enabled: ringPlayer.playbackState === SoundPlayer.PlayingState
icon: 'pause'
onClicked: {
if (enabled) {
ringPlayer.stop()
} else {
ringPlayer.play()
}
}
SoundPlayer {
id: ringPlayer
source: SettingsModel.ringPath
}
Connections {
target: window
onClosing: ringPlayer.stop()
}
}
} }
} }
} }
......
pragma Singleton
import QtQuick 2.7
// =============================================================================
QtObject {
property QtObject ringPlayer: QtObject {
property int leftMargin: 10
}
}
...@@ -25,6 +25,7 @@ singleton MainWindowMenuBarStyle 1.0 Main/MainWindowMenuBarSty ...@@ -25,6 +25,7 @@ singleton MainWindowMenuBarStyle 1.0 Main/MainWindowMenuBarSty
singleton MainWindowStyle 1.0 Main/MainWindowStyle.qml singleton MainWindowStyle 1.0 Main/MainWindowStyle.qml
singleton ManageAccountsStyle 1.0 Main/ManageAccountsStyle.qml singleton ManageAccountsStyle 1.0 Main/ManageAccountsStyle.qml
singleton SettingsAudioStyle 1.0 Settings/SettingsAudioStyle.qml
singleton SettingsSipAccountsEditStyle 1.0 Settings/SettingsSipAccountsEditStyle.qml singleton SettingsSipAccountsEditStyle 1.0 Settings/SettingsSipAccountsEditStyle.qml
singleton SettingsVideoPreviewStyle 1.0 Settings/SettingsVideoPreviewStyle.qml singleton SettingsVideoPreviewStyle 1.0 Settings/SettingsVideoPreviewStyle.qml
singleton SettingsWindowStyle 1.0 Settings/SettingsWindowStyle.qml singleton SettingsWindowStyle 1.0 Settings/SettingsWindowStyle.qml
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment