diff --git a/linphone-desktop/CMakeLists.txt b/linphone-desktop/CMakeLists.txt index 2395a8cf1..00fcff17b 100644 --- a/linphone-desktop/CMakeLists.txt +++ b/linphone-desktop/CMakeLists.txt @@ -114,6 +114,7 @@ set(SOURCES src/components/sip-addresses/SipAddressesModel.cpp src/components/sip-addresses/SipAddressObserver.cpp src/components/smart-search-bar/SmartSearchBarModel.cpp + src/components/sound-player/SoundPlayer.cpp src/components/timeline/TimelineModel.cpp src/externals/single-application/SingleApplication.cpp src/main.cpp @@ -154,6 +155,7 @@ set(HEADERS src/components/sip-addresses/SipAddressesModel.hpp src/components/sip-addresses/SipAddressObserver.hpp src/components/smart-search-bar/SmartSearchBarModel.hpp + src/components/sound-player/SoundPlayer.hpp src/components/timeline/TimelineModel.hpp src/externals/single-application/SingleApplication.hpp src/externals/single-application/SingleApplicationPrivate.hpp diff --git a/linphone-desktop/src/app/App.cpp b/linphone-desktop/src/app/App.cpp index afbab76a4..568185eac 100644 --- a/linphone-desktop/src/app/App.cpp +++ b/linphone-desktop/src/app/App.cpp @@ -293,6 +293,7 @@ void App::registerTypes () { qmlRegisterType("Linphone", 1, 0, "ChatProxyModel"); qmlRegisterType("Linphone", 1, 0, "ContactsListProxyModel"); qmlRegisterType("Linphone", 1, 0, "SmartSearchBarModel"); + qmlRegisterType("Linphone", 1, 0, "SoundPlayer"); qRegisterMetaType("ChatModel::EntryType"); diff --git a/linphone-desktop/src/components/Components.hpp b/linphone-desktop/src/components/Components.hpp index f4537e528..cffeea0d9 100644 --- a/linphone-desktop/src/components/Components.hpp +++ b/linphone-desktop/src/components/Components.hpp @@ -20,6 +20,9 @@ * Author: Ronan Abhamon */ +#ifndef COMPONENTS_H_ +#define COMPONENTS_H_ + #include "assistant/AssistantModel.hpp" #include "authentication/Authentication.hpp" #include "calls/CallsListModel.hpp" @@ -33,4 +36,7 @@ #include "presence/OwnPresenceModel.hpp" #include "settings/AccountSettingsModel.hpp" #include "smart-search-bar/SmartSearchBarModel.hpp" +#include "sound-player/SoundPlayer.hpp" #include "timeline/TimelineModel.hpp" + +#endif // COMPONENTS_H_ diff --git a/linphone-desktop/src/components/sound-player/SoundPlayer.cpp b/linphone-desktop/src/components/sound-player/SoundPlayer.cpp new file mode 100644 index 000000000..a9854b51d --- /dev/null +++ b/linphone-desktop/src/components/sound-player/SoundPlayer.cpp @@ -0,0 +1,152 @@ +/* + * SoundPlayer.cpp + * Copyright (C) 2017 Belledonne Communications, Grenoble, France + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Created on: April 25, 2017 + * Author: Ronan Abhamon + */ + +#include "../../Utils.hpp" +#include "../core/CoreManager.hpp" + +#include "SoundPlayer.hpp" + +// ============================================================================= + +SoundPlayer::SoundPlayer (QObject *parent) : QObject(parent) { + mInternalPlayer = CoreManager::getInstance()->getCore()->createLocalPlayer("", "", nullptr); +} + +// ----------------------------------------------------------------------------- + +void SoundPlayer::pause () { + if (mPlaybackState == SoundPlayer::PausedState) + return; + + if (mInternalPlayer->pause()) { + setError(QStringLiteral("Unable to pause: `%1`").arg(mSource)); + return; + } + + mPlaybackState = SoundPlayer::PausedState; + + emit paused(); + emit playbackStateChanged(mPlaybackState); +} + +void SoundPlayer::play () { + if (mPlaybackState == SoundPlayer::PlayingState) + return; + + if ( + (mPlaybackState == SoundPlayer::StoppedState || mPlaybackState == SoundPlayer::ErrorState) && + mInternalPlayer->open(::Utils::qStringToLinphoneString(mSource)) + ) { + qWarning() << QStringLiteral("Unable to open: `%1`").arg(mSource); + return; + } + + if (mInternalPlayer->start() + ) { + setError(QStringLiteral("Unable to play: `%1`").arg(mSource)); + return; + } + + mPlaybackState = SoundPlayer::PlayingState; + + emit playing(); + emit playbackStateChanged(mPlaybackState); +} + +void SoundPlayer::stop () { + if (mPlaybackState == SoundPlayer::StoppedState) + return; + + mInternalPlayer->close(); + mPlaybackState = SoundPlayer::StoppedState; + + emit stopped(); + emit playbackStateChanged(mPlaybackState); +} + +// ----------------------------------------------------------------------------- + +void SoundPlayer::seek (int offset) { + mInternalPlayer->seek(offset); +} + +// ----------------------------------------------------------------------------- + +int SoundPlayer::getPosition () const { + return mInternalPlayer->getCurrentPosition(); +} + +// ----------------------------------------------------------------------------- + +void SoundPlayer::setError (const QString &message) { + qWarning() << message; + mInternalPlayer->close(); + + if (mPlaybackState != SoundPlayer::ErrorState) { + mPlaybackState = SoundPlayer::ErrorState; + emit playbackStateChanged(mPlaybackState); + } +} + +// ----------------------------------------------------------------------------- + +QString SoundPlayer::getSource () const { + return mSource; +} + +void SoundPlayer::setSource (const QString &source) { + if (source == mSource) + return; + + stop(); + mSource = source; + + emit sourceChanged(source); +} + +// ----------------------------------------------------------------------------- + +SoundPlayer::PlaybackState SoundPlayer::getPlaybackState () const { + return mPlaybackState; +} + +void SoundPlayer::setPlaybackState (PlaybackState playbackState) { + switch (playbackState) { + case PlayingState: + play(); + break; + case PausedState: + pause(); + break; + case StoppedState: + stop(); + break; + case ErrorState: + break; + } +} + +// ----------------------------------------------------------------------------- + +int SoundPlayer::getDuration () const { + return mInternalPlayer->getDuration(); +} diff --git a/linphone-desktop/src/components/sound-player/SoundPlayer.hpp b/linphone-desktop/src/components/sound-player/SoundPlayer.hpp new file mode 100644 index 000000000..1f58a6354 --- /dev/null +++ b/linphone-desktop/src/components/sound-player/SoundPlayer.hpp @@ -0,0 +1,89 @@ +/* + * SoundPlayer.hpp + * Copyright (C) 2017 Belledonne Communications, Grenoble, France + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Created on: April 25, 2017 + * Author: Ronan Abhamon + */ + +#ifndef SOUND_PLAYER_H_ +#define SOUND_PLAYER_H_ + +#include + +#include + +// ============================================================================= + +namespace linphone { + class Player; +} + +class SoundPlayer : public QObject { + Q_OBJECT; + + Q_PROPERTY(QString source READ getSource WRITE setSource NOTIFY sourceChanged); + Q_PROPERTY(PlaybackState playbackState READ getPlaybackState WRITE setPlaybackState NOTIFY playbackStateChanged); + Q_PROPERTY(int duration READ getDuration NOTIFY sourceChanged); + +public: + enum PlaybackState { + PlayingState, + PausedState, + StoppedState, + ErrorState + }; + + Q_ENUM(PlaybackState); + + SoundPlayer (QObject *parent = Q_NULLPTR); + ~SoundPlayer () = default; + + void pause (); + void play (); + void stop (); + + void seek (int offset); + + int getPosition () const; + +signals: + void sourceChanged (const QString &source); + + void paused (); + void playing (); + void stopped (); + + void playbackStateChanged (PlaybackState playbackState); + +private: + void setError (const QString &message); + + QString getSource () const; + void setSource (const QString &source); + + PlaybackState getPlaybackState () const; + void setPlaybackState (PlaybackState playbackState); + + int getDuration () const; + + std::shared_ptr mInternalPlayer; + QString mSource; + PlaybackState mPlaybackState = StoppedState; +}; + +#endif // SOUND_PLAYER_H_