diff options
-rw-r--r-- | CMakeLists.txt | 5 | ||||
-rw-r--r-- | mediaplayer.cpp | 171 | ||||
-rw-r--r-- | mediaplayer.h | 65 | ||||
-rw-r--r-- | mediaplayermessage.cpp | 31 | ||||
-rw-r--r-- | mediaplayermessage.h | 31 | ||||
-rw-r--r-- | message.h | 1 | ||||
-rw-r--r-- | messageengine.cpp | 4 |
7 files changed, 306 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a3f5dcf..b9124ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,12 +16,13 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/qtappfw.pc add_library(qtappfw SHARED message.cpp messageengine.cpp bluetooth.cpp bluetoothmessage.cpp - telephony.cpp telephonymessage.cpp) + telephony.cpp telephonymessage.cpp + mediaplayer.cpp mediaplayermessage.cpp) target_link_libraries(qtappfw Qt5::WebSockets) set_target_properties(qtappfw PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1 - PUBLIC_HEADER "message.h;messageengine.h;bluetooth.h;bluetoothmessage.h;telephony.h;telephonymessage.h") + PUBLIC_HEADER "message.h;messageengine.h;mediaplayer.h;mediaplayermessage.h;bluetooth.h;bluetoothmessage.h;telephony.h;telephonymessage.h") target_include_directories(qtappfw PRIVATE .) install(TARGETS qtappfw LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/mediaplayer.cpp b/mediaplayer.cpp new file mode 100644 index 0000000..58aad8a --- /dev/null +++ b/mediaplayer.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2018 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "message.h" +#include "messageengine.h" +#include "mediaplayer.h" +#include "mediaplayermessage.h" + +Mediaplayer::Mediaplayer (QUrl &url, QObject * parent) : + QObject(parent), + m_mloop(nullptr) +{ + m_mloop = new MessageEngine(url); + QObject::connect(m_mloop, &MessageEngine::connected, this, &Mediaplayer::onConnected); + QObject::connect(m_mloop, &MessageEngine::disconnected, this, &Mediaplayer::onDisconnected); + QObject::connect(m_mloop, &MessageEngine::messageReceived, this, &Mediaplayer::onMessageReceived); +} + +Mediaplayer::~Mediaplayer() +{ + delete m_mloop; +} + + +// Control Verb methods + +void Mediaplayer::control(QString control, QJsonObject parameter) +{ + MediaplayerMessage *tmsg = new MediaplayerMessage(); + + parameter.insert("value", control); + + tmsg->createRequest("controls", parameter); + m_mloop->sendMessage(tmsg); + tmsg->deleteLater(); +} + + +void Mediaplayer::control(QString control) +{ + QJsonObject parameter; + + Mediaplayer::control(control, parameter); +} + +void Mediaplayer::play() +{ + control("play"); +} + +void Mediaplayer::pause() +{ + control("pause"); +} + +void Mediaplayer::previous() +{ + control("previous"); +} + +void Mediaplayer::next() +{ + control("next"); +} + +void Mediaplayer::seek(int milliseconds) +{ + QJsonObject parameter; + parameter.insert("position", QString::number(milliseconds)); + + control("seek", parameter); +} + +void Mediaplayer::fastforward(int milliseconds) +{ + QJsonObject parameter; + parameter.insert("position", QString::number(milliseconds)); + + control("fast-forward", parameter); +} + +void Mediaplayer::rewind(int milliseconds) +{ + QJsonObject parameter; + parameter.insert("position", QString::number(milliseconds)); + + control("rewind", parameter); +} + +void Mediaplayer::picktrack(int track) +{ + QJsonObject parameter; + parameter.insert("index", QString::number(track)); + + control("pick-track", parameter); +} + +void Mediaplayer::volume(int volume) +{ + QJsonObject parameter; + parameter.insert("volume", QString(volume)); + + control("volume", parameter); +} + +void Mediaplayer::loop(int state) +{ + QJsonObject parameter; + parameter.insert("state", state ? "true" : "false"); + + control("loop", parameter); +} + +void Mediaplayer::onConnected() +{ + QStringListIterator eventIterator(events); + MediaplayerMessage *tmsg; + + while (eventIterator.hasNext()) { + tmsg = new MediaplayerMessage(); + QJsonObject parameter; + parameter.insert("value", eventIterator.next()); + tmsg->createRequest("subscribe", parameter); + m_mloop->sendMessage(tmsg); + tmsg->deleteLater(); + } +} + +void Mediaplayer::onDisconnected() +{ + QStringListIterator eventIterator(events); + MediaplayerMessage *tmsg; + + while (eventIterator.hasNext()) { + tmsg = new MediaplayerMessage(); + QJsonObject parameter; + parameter.insert("value", eventIterator.next()); + tmsg->createRequest("unsubscribe", parameter); + m_mloop->sendMessage(tmsg); + tmsg->deleteLater(); + } +} + +void Mediaplayer::onMessageReceived(MessageType type, Message *message) +{ + if (type == MediaplayerEventMessage) { + MediaplayerMessage *tmsg = qobject_cast<MediaplayerMessage*>(message); + + if (tmsg->isEvent()) { + if (tmsg->isPlaylistEvent()) { + emit playlistChanged(tmsg->eventData()); + } else if (tmsg->isMetadataEvent()) { + emit metadataChanged(tmsg->eventData()); + } + } + } + message->deleteLater(); +} diff --git a/mediaplayer.h b/mediaplayer.h new file mode 100644 index 0000000..107cfa3 --- /dev/null +++ b/mediaplayer.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2018 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MEDIAPLAYER_H +#define MEDIAPLAYER_H + +#include <QDebug> +#include <QObject> + +#include "messageengine.h" + +class Mediaplayer : public QObject +{ + Q_OBJECT + + public: + explicit Mediaplayer(QUrl &url, QObject * parent = Q_NULLPTR); + virtual ~Mediaplayer(); + + // controls + Q_INVOKABLE void play(); + Q_INVOKABLE void pause(); + Q_INVOKABLE void previous(); + Q_INVOKABLE void next(); + Q_INVOKABLE void seek(int); + Q_INVOKABLE void fastforward(int); + Q_INVOKABLE void rewind(int); + Q_INVOKABLE void picktrack(int); + Q_INVOKABLE void volume(int); + Q_INVOKABLE void loop(int); + + signals: + void playlistChanged(QJsonObject playlist); + void metadataChanged(QJsonObject metadata); + + private: + MessageEngine *m_mloop; + + void control(QString, QJsonObject); + void control(QString); + + void onConnected(); + void onDisconnected(); + void onMessageReceived(MessageType, Message*); + + const QStringList events { + "playlist", + "metadata", + }; +}; + +#endif // MEDIAPLAYER_H diff --git a/mediaplayermessage.cpp b/mediaplayermessage.cpp new file mode 100644 index 0000000..cda09d2 --- /dev/null +++ b/mediaplayermessage.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2018 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <QDebug> +#include <QJsonArray> +#include <QJsonDocument> +#include <QJsonObject> + +#include "mediaplayermessage.h" + +bool MediaplayerMessage::createRequest(QString verb, QJsonObject parameter) +{ + QStringList verbs {"playlist", "metadata", "controls", "subscribe", "unsubscribe"}; + if (!verbs.contains(verb)) + return false; + + return Message::createRequest("mediaplayer", verb, parameter); +} diff --git a/mediaplayermessage.h b/mediaplayermessage.h new file mode 100644 index 0000000..c164cde --- /dev/null +++ b/mediaplayermessage.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2018 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MEDIAPLAYER_MESSAGE_H +#define MEDIAPLAYER_MESSAGE_H + +#include "message.h" + +class MediaplayerMessage : public Message +{ + Q_OBJECT + public: + bool isPlaylistEvent() { return (this->eventName() == "playlist"); }; + bool isMetadataEvent() { return (this->eventName() == "metadata"); }; + bool createRequest(QString verb, QJsonObject parameter); +}; + +#endif // MEDIAPLAYER_MESSAGE_H @@ -33,6 +33,7 @@ enum MessageType { GenericMessage, TelephonyEventMessage, BluetoothEventMessage, + MediaplayerEventMessage, }; class Message : public QObject diff --git a/messageengine.cpp b/messageengine.cpp index c68093d..427c557 100644 --- a/messageengine.cpp +++ b/messageengine.cpp @@ -17,6 +17,7 @@ #include "message.h" #include "messageengine.h" #include "bluetoothmessage.h" +#include "mediaplayermessage.h" #include "telephonymessage.h" #include <QJsonArray> @@ -78,6 +79,9 @@ void MessageEngine::onTextMessageReceived(QString jsonStr) } else if (api == "telephony") { message = new TelephonyMessage; type = TelephonyEventMessage; + } else if (api == "mediaplayer") { + message = new MediaplayerMessage; + type = MediaplayerEventMessage; } else { message = new Message; type = GenericMessage; |