Skip to content

Commit

Permalink
lib: WS support inteface actions and fix ncl360 mime
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed May 14, 2021
1 parent 4e069d6 commit 633e922
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 145 deletions.
8 changes: 5 additions & 3 deletions lib/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2315,7 +2315,7 @@ bool
ParserState::pushNcl (ParserState *st, ParserElt *elt)
{
Context *root;
string *id = new string();
string *id = new string ();

root = st->_doc->getRoot ();
g_assert_nonnull (root);
Expand Down Expand Up @@ -2791,8 +2791,10 @@ borderColor='%s'}",
}
case Event::LOOKAT:
{
obj->addLookAtEvent ("@lambda");
act.event = obj->getLookAtEvent ("@lambda");
string eventId = evt->getId ();
if (obj->getLookAtEvent (eventId) == nullptr)
obj->addLookAtEvent (eventId);
act.event = obj->getLookAtEvent (eventId);
g_assert_nonnull (act.event);
break;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static map<string, string> mime_table = {
{ "mpeg", "video/mpeg" },
{ "mpg", "video/mpeg" },
{ "mpv", "video/mpv" },
{ "ncl360", REMOTE_PLAYER_MIME_NCL360 },
{ "ncl", "application/x-ginga-ncl" },
{ "oga", "audio/ogg" },
{ "ogg", "audio/ogg" },
Expand Down
2 changes: 1 addition & 1 deletion lib/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Player

virtual void sendKeyEvent (const string &, bool);

// For now, only for the lua player (which reimplements it).
// For now, only for the PlayerLua and PlayerRemote (which reimplements it).
virtual void
sendPresentationEvent (const string &, const string &)
{
Expand Down
53 changes: 23 additions & 30 deletions lib/PlayerRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ PlayerRemote::PlayerRemote (Formatter *fmt, Media *media)
: Player (fmt, media)
{
_session = nullptr;
_url = nullptr;
}

/**
Expand All @@ -53,27 +52,8 @@ PlayerRemote::usesPlayerRemote (Media *media)
return false;
}

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 ("%s" REMOTE_PLAYER_ROUTE_NODES "%s",
value.c_str (), _media->getId ().c_str ());
break;
default:
return Player::doSetProperty (code, name, value);
}
return true;
}

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

Expand All @@ -88,54 +68,59 @@ cb_action (SoupSession *session, SoupMessage *msg, gpointer user_data)
}

void
PlayerRemote::sendAction (const char *body)
PlayerRemote::sendAction (const string &body, const string &label = "")
{
guint status;
SoupMessage *msg;

g_assert_nonnull (_url);
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));

string url
= xstrbuild ("%s" REMOTE_PLAYER_ROUTE_NODES "%s",
getProperty ("remotePlayerBaseURL").c_str (),
(label == "") ? _media->getId ().c_str () : label.c_str ());

msg = soup_message_new (SOUP_METHOD_POST, url.c_str ());
soup_message_set_request (msg, "application/json", SOUP_MEMORY_COPY,
body.c_str (), strlen (body.c_str ()));
soup_session_queue_message (_session, msg, cb_action, this);
}

void
PlayerRemote::start ()
{
string zOrder = getProperty ("zOrder");
string props = xstrbuild (
string body_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 ());
sendAction (body_props);
Player::start ();
}

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

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

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

Expand All @@ -147,6 +132,14 @@ PlayerRemote::resume ()
Player::resume ();
}

void
PlayerRemote::sendPresentationEvent (const string &action,
const string &label)
{
string body = xstrbuild (REMOTE_PLAYER_JSON_ACT, action.c_str (), 0);
sendAction (body, label);
}

void
PlayerRemote::reload ()
{
Expand Down
7 changes: 3 additions & 4 deletions lib/PlayerRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WebServices;

#define REMOTE_PLAYER_ROUTE_NODES "/scene/nodes/"

#define REMOTE_PLAYER_MIME_NCL360 "application/x-ncl360"
#define REMOTE_PLAYER_MIME_NCL360 "application/x-ginga-ncl360"

class PlayerRemote : public Player
{
Expand All @@ -71,11 +71,10 @@ class PlayerRemote : public Player
void redraw (cairo_t *);

static bool usesPlayerRemote (Media *);
void sendPresentationEvent (const string &, const string &);

protected:
bool doSetProperty (Property, const string &, const string &);
void sendAction (const char *);
char *_url;
void sendAction (const string &, const string &);
bool _sessionStarted;
WebServices *_ws;
SoupSession *_session;
Expand Down
24 changes: 19 additions & 5 deletions lib/WebServices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ cb_apps (SoupServer *server, SoupMessage *msg, const char *path,
bool status = false;
string action, interface, value;
Object *node;
Event *evt;
Event *evt = nullptr;
Document *doc;
const char *target = path + strlen (WS_ROUTE_APPS);
gchar **params = g_strsplit (target, "/", 3);
Expand Down Expand Up @@ -180,13 +180,27 @@ cb_apps (SoupServer *server, SoupMessage *msg, const char *path,
}
else if (action == "lookAt")
{
evt = node->getLookAtEvent ("@lambda");
doc->evalAction (evt, Event::START);
if (!interface.empty ())
evt = node->getLookAtEvent (interface);
else
evt = node->getLookAtEvent ("@lambda");

if (evt != nullptr)
doc->evalAction (evt, Event::START);
else
goto fail;
}
else if (action == "lookAway")
{
evt = node->getLookAtEvent ("@lambda");
doc->evalAction (evt, Event::STOP);
if (!interface.empty ())
evt = node->getLookAtEvent (interface);
else
evt = node->getLookAtEvent ("@lambda");

if (evt != nullptr)
doc->evalAction (evt, Event::STOP);
else
goto fail;
}
else
goto fail;
Expand Down
16 changes: 16 additions & 0 deletions lib/WebServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ class PlayerRemoteData
\"recognizableEvents\" : %s, \
}"

#define WS_JSON_ACT \
"{\
\"action\": \"%s\",\
}"
#define WS_JSON_ACT_WITH_INTERFACE \
"{\
\"action\": \"%s\",\
\"interface\": \"%s\",\
}"
#define WS_JSON_ACT_WITH_INTERFACE_VALUE \
"{\
\"action\": \"%s\",\
\"interface\": \"%s\",\
\"value\": \"%s\"\
}"

#define TRACE_SOUP_REQ_MSG(msg) \
G_STMT_START \
{ \
Expand Down
2 changes: 1 addition & 1 deletion tests-ncl/test-ws-RemotePlayer.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<port id="entry2" component="m2"/>
<port id="entry3" component="m3"/>
<media id="hidden"/>
<media id="m1" descriptor="desc1" type="application/x-ncl360"/>
<media id="m1" descriptor="desc1" type="application/x-ginga-ncl360"/>
<media id="m2" descriptor="desc2">
<property name="background" value="green"/>
</media>
Expand Down
Loading

0 comments on commit 633e922

Please sign in to comment.