Skip to content

Commit

Permalink
lib: refactory and add test-WebServices-RemotePlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed May 11, 2021
1 parent ea192af commit a5588ac
Show file tree
Hide file tree
Showing 12 changed files with 540 additions and 217 deletions.
3 changes: 2 additions & 1 deletion lib/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ along with Ginga. If not, see <https://www.gnu.org/licenses/>. */
#include "MediaSettings.h"
#include "Object.h"
#include "Switch.h"
#include "PlayerRemote.h"

GINGA_NAMESPACE_BEGIN

Expand Down Expand Up @@ -152,7 +153,7 @@ Document::addObject (Object *obj)
Media *media = cast (Media *, obj);
g_assert_nonnull (media);
_medias.insert (media);
if (Player::mayUsePlayerRemote (media))
if (PlayerRemote::usesPlayerRemote (media))
{
_mediasRemote.insert (media);
}
Expand Down
15 changes: 0 additions & 15 deletions lib/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,21 +520,6 @@ 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
1 change: 0 additions & 1 deletion lib/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ 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
84 changes: 56 additions & 28 deletions lib/PlayerRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,25 @@ GINGA_NAMESPACE_BEGIN
PlayerRemote::PlayerRemote (Formatter *fmt, Media *media)
: Player (fmt, media)
{
_sessionStarted = false;
_session = nullptr;
_url = nullptr;
}

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

bool
PlayerRemote::doSetProperty (Property code, const string &name,
const string &value)
Expand All @@ -47,8 +62,8 @@ PlayerRemote::doSetProperty (Property code, const string &name,
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 ());
_url = g_strdup_printf ("%s" REMOTE_PLAYER_ROUTE_NODES "%s",
value.c_str (), _media->getId ().c_str ());
break;
default:
return Player::doSetProperty (code, name, value);
Expand All @@ -63,70 +78,83 @@ PlayerRemote::~PlayerRemote ()
}

static void
ws_action_callback (SoupSession *session, SoupMessage *msg,
gpointer user_data)
cb_action (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
string url;
auto player = (PlayerRemote *) user_data;
url = player->getProperty ("remotePlayerBaseURL");
if (!msg->status_code == SOUP_STATUS_OK)
WARNING ("Failed to perform request %s", url.c_str ());
TRACE_SOUP_REQ_MSG (msg);
WARNING ("Failed to perform request to %s",
player->getProperty ("remotePlayerBaseURL").c_str ());
}

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

g_assert_nonnull (_url);
if (!_sessionStarted)
{
_session
= soup_session_new_with_options (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
SOUP_TYPE_CONTENT_SNIFFER, NULL);
_sessionStarted = true;
}
msg = soup_message_new ("POST", _url);
body = g_strdup_printf (REMOTE_PLAYER_JSON_ACT, action.c_str (), 0);
if (!_session)
_session = soup_session_new ();
msg = soup_message_new (SOUP_METHOD_POST, _url);
soup_message_set_request (msg, "application/json", SOUP_MEMORY_COPY, body,
strlen (body));
soup_session_queue_message (_session, msg, ws_action_callback, this);

g_free (msg);
g_free (body);
soup_session_queue_message (_session, msg, cb_action, this);
}

void
PlayerRemote::start ()
{
sendAction ("start");
string zOrder = getProperty ("zOrder");
string props = xstrbuild (
REMOTE_PLAYER_JSON_ACT_WITH_PROPS, "start", 0,
getProperty ("top").c_str (), getProperty ("left").c_str (),
getProperty ("width").c_str (), getProperty ("height").c_str (),
getProperty ("zOrder").c_str (), "0%");
sendAction (props.c_str ());
Player::start ();
}

void
PlayerRemote::startPreparation ()
{
sendAction ("prepare");
string body = xstrbuild (REMOTE_PLAYER_JSON_ACT, "prepare", 0);
sendAction (body.c_str ());
Player::startPreparation ();
}

void
PlayerRemote::stop ()
{
sendAction ("stop");
string body = xstrbuild (REMOTE_PLAYER_JSON_ACT, "stop", 0);
sendAction (body.c_str ());
Player::stop ();
}

void
PlayerRemote::pause ()
{
sendAction ("pause");
string body = xstrbuild (REMOTE_PLAYER_JSON_ACT, "pause", 0);
sendAction (body.c_str ());
Player::pause ();
}

void
PlayerRemote::resume ()
{
sendAction ("resume");
string body = xstrbuild (REMOTE_PLAYER_JSON_ACT, "resume", 0);
sendAction (body.c_str ());
Player::resume ();
}

void
PlayerRemote::reload ()
{
}

void
PlayerRemote::redraw (cairo_t *)
{
}

GINGA_NAMESPACE_END
36 changes: 32 additions & 4 deletions lib/PlayerRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,34 @@ class WebServices;

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

#define REMOTE_PLAYER_JSON_ACT_WITH_PROPS \
"{\
\"action\": \"%s\",\
\"delay\": \"%d\",\
\"properties\": \
{\
\"bounds\": {\"top\": \"%s\",\"left\": \"%s\",\"width\": \"%s\",\"height\": \"%s\"},\
\"zIndex\" : \"%s\",\
\"transparency\" : \"%s\"\
}\
}"

#define REMOTE_PLAYER_JSON_MEDIA \
"{\
\"appId\": \"%s\",\
\"documentId\": \"%s\",\
\"sceneNode\": \"%s\",\
\"type\": \"%s\",\
\"notifyEvents\": %s\
}"

#define REMOTE_PLAYER_ROUTE_NODES "/scene/nodes/"

#define REMOTE_PLAYER_MIME_NCL360 "application/x-ncl360"

class PlayerRemote : public Player
{
Expand All @@ -42,11 +66,15 @@ class PlayerRemote : public Player
void stop ();
void pause ();
void resume ();
void reload ();
void redraw (cairo_t *);

static bool usesPlayerRemote (Media *);

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

0 comments on commit a5588ac

Please sign in to comment.