From 9ef3931c8c85093c5d2e98677861db7aa71ba796 Mon Sep 17 00:00:00 2001 From: Louis-Joseph Fournier Date: Wed, 13 Jan 2016 14:55:07 +0100 Subject: [PATCH] Player try resource API to get playback resource Don't work ; try audioresource instead.. --- harbour-sailtuner.pro | 8 +++- rpm/Makefile | 7 +++- src/Tuner.hpp | 4 +- src/TunerSailfish.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++ src/TunerSailfish.hpp | 41 ++++++++++++++++++++ src/sailfish.cpp | 4 +- 6 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 src/TunerSailfish.cpp create mode 100644 src/TunerSailfish.hpp diff --git a/harbour-sailtuner.pro b/harbour-sailtuner.pro index 6a1b22b..1c3fce6 100644 --- a/harbour-sailtuner.pro +++ b/harbour-sailtuner.pro @@ -1,11 +1,13 @@ QT += qml quick gui multimedia dbus TARGET = harbour-sailtuner -CONFIG += c++11 link_pkgconfig sailfishapp sailfishapp_i18n sailfishapp_no_deploy_qml +CONFIG += c++11 qt link_pkgconfig sailfishapp sailfishapp_i18n sailfishapp_no_deploy_qml DEFINES += TARGET=\""$(TARGET")\" -PKGCONFIG += libpulse-simple +## see rpm/Makefile for installing libpulse and libresource + +PKGCONFIG += libpulse-simple libresource RESOURCES += \ qml/sailfish.qrc \ @@ -16,6 +18,7 @@ SOURCES += \ src/PitchDetection.cpp \ src/ObjectSaver.cpp \ src/Tuner.cpp \ + src/TunerSailfish.cpp \ src/TunerWorker.cpp \ src/audio/FreqPlayer.cpp \ src/audio/LinearFilter.cpp \ @@ -28,6 +31,7 @@ HEADERS += \ src/ObjectSaver.hpp \ src/Tuner.cpp \ src/Tuner.hpp \ + src/TunerSailfish.hpp \ src/TunerWorker.hpp \ src/audio/FreqPlayer.hpp \ src/audio/LinearFilter.hpp \ diff --git a/rpm/Makefile b/rpm/Makefile index 6478d84..8edc2e6 100644 --- a/rpm/Makefile +++ b/rpm/Makefile @@ -2,9 +2,14 @@ # to add pulseaudio dev to MerSDK: # sudo zypper in pulseaudio-devel -# sb2 -t SailfishOS-i486-x86 -m sdk-install -R zypper in pulseaudio-devel +# sb2 -t SailfishOS-i486 -m sdk-install -R zypper in pulseaudio-devel # sb2 -t SailfishOS-armv7hl -m sdk-install -R zypper in pulseaudio-devel +# for libresource: +# sudo zypper in libresource-devel +# sb2 -t SailfishOS-i486 -m sdk-install -R zypper in libresource-devel +# sb2 -t SailfishOS-armv7hl -m sdk-install -R zypper in libresource-devel + SAILFISHOS_DIR = $(HOME)/SailfishOS SSH_KEYS_DIR = $(SAILFISHOS_DIR)/vmshare/ssh/private_keys diff --git a/src/Tuner.hpp b/src/Tuner.hpp index 689beeb..1317245 100644 --- a/src/Tuner.hpp +++ b/src/Tuner.hpp @@ -36,7 +36,7 @@ class Tuner : public QObject { Q_PROPERTY(QStringList temperament_list READ GetTemperamentList NOTIFY temperamentListChanged) Q_PROPERTY(double la READ GetLa WRITE SetLa NOTIFY laChanged) - private: + protected: TunerWorker *worker; QThread workerThread; @@ -51,7 +51,7 @@ class Tuner : public QObject { ~Tuner(); bool GetPlaying(); - void SetPlaying(bool p); + virtual void SetPlaying(bool p); bool GetRunning(); void SetRunning(bool r); double GetFreq(); diff --git a/src/TunerSailfish.cpp b/src/TunerSailfish.cpp new file mode 100644 index 0000000..cd7974e --- /dev/null +++ b/src/TunerSailfish.cpp @@ -0,0 +1,87 @@ +/* Copyright 2016 (C) Louis-Joseph Fournier + * louisjoseph.fournier@gmail.com + * + * This file is part of SailTuner. + * + * SailTuner 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. + * + * SailTuner 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. + * + */ + +#include +#include /* for getpid() */ +#include + +#include "TunerSailfish.hpp" + +TunerSailfish::~TunerSailfish() +{ + // release resource if needed + if (r_set) { + resource_set_destroy(r_set); + r_set = nullptr; + r_granted = false; + } +} + +void TunerSailfish::g_advice_cb(resource_set_t *resource_set, uint32_t resources, void *data) +{ + ((TunerSailfish*) data)->advice_cb(resource_set, resources); +} + +void TunerSailfish::g_grant_cb(resource_set_t *resource_set, uint32_t resources, void *data) +{ + ((TunerSailfish*) data)->grant_cb(resource_set, resources); +} + +void TunerSailfish::advice_cb(resource_set_t *resource_set, uint32_t resources) +{ + (void) resource_set; + std::cout << __func__ << " " << resources << std::endl; +} + +void TunerSailfish::grant_cb(resource_set_t *resource_set, uint32_t resources) +{ + (void) resource_set; + std::cout << __func__ << " " << resources << std::endl; + + if (resources & RESOURCE_AUDIO_PLAYBACK) { + r_granted = true; + if (playing) worker->SetPlaying(true); + } + else { + r_granted = false; + if (!playing) worker->SetPlaying(false); + } +} + +void TunerSailfish::get_resource() +{ + if (r_set) return; + r_set = resource_set_create("player", RESOURCE_AUDIO_PLAYBACK, 0, 0, g_grant_cb, this); + resource_set_configure_advice_callback(r_set, g_advice_cb, this); + resource_set_configure_audio (r_set, "player", getpid(), "*"); + resource_set_acquire (r_set); +} + +void TunerSailfish::SetPlaying(bool p) +{ + if (p == playing) return; + playing = p; + + // get resource only the first playback start + if (p && !r_set) get_resource(); + + // if not granted, don't start, and don't stop because not started + if (!r_granted) return; + + worker->SetPlaying(p); + emit playingChanged(); +} diff --git a/src/TunerSailfish.hpp b/src/TunerSailfish.hpp new file mode 100644 index 0000000..2b35a5c --- /dev/null +++ b/src/TunerSailfish.hpp @@ -0,0 +1,41 @@ +/* Copyright 2016 (C) Louis-Joseph Fournier + * louisjoseph.fournier@gmail.com + * + * This file is part of SailTuner. + * + * SailTuner 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. + * + * SailTuner 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. + * + */ + +#include +#include "Tuner.hpp" + +/** Tuner object + * with resource handling for audio playback + */ +class TunerSailfish : public Tuner { + private: + resource_set_t *r_set = nullptr; + bool r_granted = false; + + /// callbacks from resource system + static void g_advice_cb(resource_set_t *resource_set, uint32_t resources, void *data); + static void g_grant_cb(resource_set_t *resource_set, uint32_t resources, void *data); + void advice_cb(resource_set_t *resource_set, uint32_t resources); + void grant_cb(resource_set_t *resource_set, uint32_t resources); + + /// main function to get resources + void get_resource(); + + public: + ~TunerSailfish(); + void SetPlaying(bool p); +}; diff --git a/src/sailfish.cpp b/src/sailfish.cpp index 9182442..bd76a62 100644 --- a/src/sailfish.cpp +++ b/src/sailfish.cpp @@ -20,7 +20,7 @@ #include #include #include "ObjectSaver.hpp" -#include "Tuner.hpp" +#include "TunerSailfish.hpp" class Main { private: @@ -53,7 +53,7 @@ Q_DECL_EXPORT int main(int argc, char* argv[]) TunerWorker::set_record(argv[2]); } - qmlRegisterType("harbour.sailtuner.tuner", 1, 0, "Tuner"); + qmlRegisterType("harbour.sailtuner.tuner", 1, 0, "TunerSailfish"); qmlRegisterType("harbour.sailtuner.objectsaver", 1, 0, "ObjectSaver"); Main *appli = new Main(argc, argv);