Skip to content

Commit

Permalink
lib: boostrap WebService /remote-mediaplayer route
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Mar 15, 2021
1 parent 5b31068 commit fb4cf3d
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ else()
endif()

PKG_CHECK_MODULES(GSSDP gssdp-1.2 REQUIRED)
PKG_CHECK_MODULES(JSON jsoncpp REQUIRED)

set (LIBGINGA_SOURCES
./lib/ginga.h
Expand All @@ -142,14 +143,15 @@ set (LIBGINGA_SOURCES
./lib/ParserLua.cpp
./lib/Predicate.cpp
./lib/Switch.cpp

./lib/Player.cpp
./lib/PlayerAnimator.cpp
./lib/PlayerImage.cpp
./lib/PlayerLua.cpp
./lib/PlayerSigGen.cpp
./lib/PlayerText.cpp
./lib/PlayerVideo.cpp
./lib/PlayerRemote.cpp

./lib/WebServices.cpp

Expand Down Expand Up @@ -178,6 +180,7 @@ set (LIBGINGA_INCLUDE_DIRS

${LIBSOUP_INCLUDE_DIRS}
${GSSDP_INCLUDE_DIRS}
${JSON_INCLUDE_DIRS}

${NCLUA_INCLUDE_DIRS}
${LUA_INCLUDE_DIR}
Expand All @@ -203,6 +206,7 @@ set (LIBGINGA_LIBS

${LIBSOUP_LIBRARIES}
${GSSDP_LIBRARIES}
${JSON_LIBRARIES}

${NCLUA_LIBRARIES}
${LUA_LIBRARIES}
Expand Down
9 changes: 9 additions & 0 deletions lib/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ Document::addObject (Object *obj)
Media *media = cast (Media *, obj);
g_assert_nonnull (media);
_medias.insert (media);
if (Player::mayUsePlayerRemote(media)){
_mediasRemote.insert (media);
}
}
else if (instanceof (Context *, obj))
{
Expand Down Expand Up @@ -180,6 +183,12 @@ Document::getMedias ()
return &_medias;
}

const set<Media *> *
Document::getMediasRemote ()
{
return &_mediasRemote;
}

const set<Context *> *
Document::getContexts ()
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Document
Context *getRoot ();
MediaSettings *getSettings ();
const set<Media *> *getMedias ();
const set<Media *> *getMediasRemote ();
const set<Context *> *getContexts ();
const set<Switch *> *getSwitches ();

Expand All @@ -63,6 +64,7 @@ class Document
Context *_root; ///< Root context (body).
MediaSettings *_settings; ///< Settings object.
set<Media *> _medias; ///< Media objects.
set<Media *> _mediasRemote; ///< Media objects.
set<Context *> _contexts; ///< Context objects.
set<Switch *> _switches; ///< Switch objects.
UserData _udata; ///< Attached user data.
Expand Down
1 change: 1 addition & 0 deletions lib/Formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ along with Ginga. If not, see <https://www.gnu.org/licenses/>. */

#include "Parser.h"
#include "PlayerText.h"
#include "WebServices.h"

/**
* @file Formatter.cpp
Expand Down
2 changes: 1 addition & 1 deletion lib/Formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ along with Ginga. If not, see <https://www.gnu.org/licenses/>. */
#include "aux-ginga.h"

#include "Document.h"
#include "WebServices.h"

GINGA_NAMESPACE_BEGIN

class WebServices;
class Context;
class Event;
class Media;
Expand Down
41 changes: 33 additions & 8 deletions lib/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ along with Ginga. If not, see <https://www.gnu.org/licenses/>. */
#include "PlayerText.h"
#include "PlayerVideo.h"
#include "PlayerSigGen.h"
#include "PlayerRemote.h"

#if defined WITH_NCLUA && WITH_NCLUA
#include "PlayerLua.h"
Expand Down Expand Up @@ -120,6 +121,7 @@ static map<string, PlayerPropertyInfo> player_property_map = {
{ "fontWeight", { Player::PROP_FONT_WEIGHT, true, "" } },
{ "freeze", { Player::PROP_FREEZE, true, "false" } },
{ "freq", { Player::PROP_FREQ, true, "440" } },
{ "remotePlayerBaseURL", { Player::PROP_REMOTE_PLAYER_BASE_URL, false, "" } },
{ "height", { Player::PROP_HEIGHT, true, "100%" } },
{ "horzAlign", { Player::PROP_HORZ_ALIGN, true, "left" } },
{ "left", { Player::PROP_LEFT, true, "0" } },
Expand All @@ -128,7 +130,7 @@ static map<string, PlayerPropertyInfo> player_property_map = {
{ "right", { Player::PROP_RIGHT, false, "0%" } },
{ "size", { Player::PROP_SIZE, false, "100%,100%" } },
{ "speed", { Player::PROP_SPEED, false, "1" } },
{"currentTime", {Player::PROP_TIME, false, "indefinite"} },
{ "currentTime", { Player::PROP_TIME, false, "indefinite" } },
{ "time", { Player::PROP_TIME, false, "indefinite" } },
{ "top", { Player::PROP_TOP, true, "0" } },
{ "transparency", { Player::PROP_TRANSPARENCY, true, "0%" } },
Expand All @@ -142,8 +144,9 @@ static map<string, PlayerPropertyInfo> player_property_map = {
{ "zOrder", { Player::PROP_Z_ORDER, true, "0" } },
{ "uri", { Player::PROP_URI, true, "" } },
{ "type", { Player::PROP_TYPE, true, "application/x-ginga-timer" } },
{"offsetBuffer", {Player::PROP_BUFFER_OFFSET, true, "0"} },
{"endOffsetBuffer", {Player::PROP_BUFFER_OFFSET_END, true, "indefinite"} },
{ "offsetBuffer", { Player::PROP_BUFFER_OFFSET, true, "0" } },
{ "endOffsetBuffer",
{ Player::PROP_BUFFER_OFFSET_END, true, "indefinite" } },
};

static map<string, string> player_property_aliases = {
Expand Down Expand Up @@ -320,7 +323,8 @@ Player::setProperty (const string &name, const string &value)
code = Player::getPlayerProperty (name, &defval);

// NCLua media should perform the doSetProperty to trigger registred funcs
if (code == Player::PROP_UNKNOWN && this->getProperty("type") != "application/x-ginga-NCLua")
if (code == Player::PROP_UNKNOWN
&& this->getProperty ("type") != "application/x-ginga-NCLua")
goto done;

if (_value == "")
Expand Down Expand Up @@ -495,8 +499,7 @@ Player::redraw (cairo_t *cr)
}
}

void
Player::sendKeyEvent (unused (const string &key), unused (bool press))
void Player::sendKeyEvent (unused (const string &key), unused (bool press))
{
}

Expand All @@ -517,6 +520,21 @@ Player::setCurrentFocus (const string &index)
_currentFocus = index;
}

/**
* @brief Evaluates if Media uses PlayerRemote
*/
bool
Player::mayUsePlayerRemote (Media *media)
{
string mime = media->getProperty ("type");
if (mime == "application/x-ncl-360")
return true;
string device = media->getProperty ("device");
if (!device.empty())
return true;
return false;
}

Player::Property
Player::getPlayerProperty (const string &name, string *defval)
{
Expand Down Expand Up @@ -593,6 +611,12 @@ Player::createPlayer (Formatter *formatter, Media *media, const string &uri,
{
player = new PlayerText (formatter, media);
}
// if has WS setted a remotePlayerBaseURL
else if (media->getProperty("remotePlayerBaseURL") != "")
{
player = new PlayerRemote (formatter, media);
WARNING ("Create a PlayerRemote for Media '%s'", media->getId ().c_str());
}
#if defined WITH_CEF && WITH_CEF
else if (xstrhasprefix (mime, "text/html"))
{
Expand All @@ -618,8 +642,9 @@ Player::createPlayer (Formatter *formatter, Media *media, const string &uri,
{
WARNING ("unknown mime '%s': creating an empty player",
mime.c_str ());
if (!media->getProperty("uri").empty ())
ERROR ("media from \"application/x-ginga-timer\" type should not have src");
if (!media->getProperty ("uri").empty ())
ERROR ("media from \"application/x-ginga-timer\" type should "
"not have src");
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Player
PROP_Z_ORDER,
PROP_BUFFER_OFFSET,
PROP_BUFFER_OFFSET_END,
PROP_REMOTE_PLAYER_BASE_URL,
};

Player (Formatter *, Media *);
Expand Down Expand Up @@ -127,6 +128,7 @@ class Player
// Static.
static string getCurrentFocus ();
static void setCurrentFocus (const string &);
static bool mayUsePlayerRemote (Media *);
static Property getPlayerProperty (const string &, string *);
static Player *createPlayer (Formatter *, Media *, const string &,
const string &type = "");
Expand Down
126 changes: 126 additions & 0 deletions lib/PlayerRemote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* Copyright (C) 2006-2018 PUC-Rio/Laboratorio TeleMidia
This file is part of Ginga (Ginga-NCL).
Ginga 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.
Ginga 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 Ginga. If not, see <https://www.gnu.org/licenses/>. */

#include "PlayerRemote.h"
#include "aux-ginga.h"
#include "aux-gl.h"
#include "WebServices.h"
#include "Media.h"
#include "Formatter.h"
#include <libsoup/soup.h>

GINGA_NAMESPACE_BEGIN

#define REMOTE_PLAYER_ACT_JSON "{ \"action\" : \"%s\", \"delay\" : \"%d\"}"

/**
* @brief Creates PlayerRemote
* @param fmt Formatter
* @param media Media Element
*/

PlayerRemote::PlayerRemote (Formatter *fmt, Media *media)
: Player (fmt, media)
{
_sessionStarted = false;
_url = nullptr;
}

bool
PlayerRemote::doSetProperty (Property code, const string &name,
const string &value)
{
switch (code)
{
case PROP_REMOTE_PLAYER_BASE_URL:
if (_url != nullptr)
g_free (_url);
_url = g_strdup_printf ("http://%s/scene/nodes/%s", value.c_str (),
_media->getId ().c_str());
break;
default:
return Player::doSetProperty (code, name, value);
}
return true;
}

PlayerRemote::~PlayerRemote ()
{
g_free (_url);
}

bool
PlayerRemote::sendAction (const string &action)
{
guint status;
SoupMessage *msg;
char *body;

if (!_sessionStarted)
{
_session
= soup_session_new_with_options (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
SOUP_TYPE_CONTENT_SNIFFER, NULL);
_sessionStarted = true;
}
g_assert_nonnull (_url);
msg = soup_message_new ("POST", _url);
body = g_strdup_printf (REMOTE_PLAYER_ACT_JSON, action.c_str (), 0);
soup_message_set_request (msg, "application/json", SOUP_MEMORY_COPY, body,
strlen (body));
status = soup_session_send_message (_session, msg);
if(!status){
WARNING ("Failed perform request %s", _url);
}

g_free (msg);
g_free (body);
g_free (msg);
return true;
}

void
PlayerRemote::start ()
{
g_assert (sendAction ("start"));
}

void
PlayerRemote::startPreparation ()
{
g_assert (sendAction ("prepare"));
}

void
PlayerRemote::stop ()
{
g_assert (sendAction ("stop"));
}

void
PlayerRemote::pause ()
{
g_assert (sendAction ("pause"));
}

void
PlayerRemote::resume ()
{
g_assert (sendAction ("resume"));
}

GINGA_NAMESPACE_END
50 changes: 50 additions & 0 deletions lib/PlayerRemote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (C) 2006-2018 PUC-Rio/Laboratorio TeleMidia
This file is part of Ginga (Ginga-NCL).
Ginga 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.
Ginga 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 Ginga. If not, see <https://www.gnu.org/licenses/>. */

#ifndef MEDIA_REMOTE_H
#define MEDIA_REMOTE_H

#include "Player.h"
#include <libsoup/soup.h>

GINGA_NAMESPACE_BEGIN

class WebServices;

class PlayerRemote : public Player
{
public:
explicit PlayerRemote (Formatter *, Media *);
~PlayerRemote ();
void start ();
void startPreparation ();
void stop ();
void pause ();
void resume ();

protected:
bool doSetProperty (Property, const string &, const string &);
bool sendAction (const string &);
char * _url;
bool _sessionStarted;
WebServices *_ws;
SoupSession *_session;
};

GINGA_NAMESPACE_END

#endif // MEDIA_REMOTE_H
Loading

0 comments on commit fb4cf3d

Please sign in to comment.