From 75dfef067c790fd5c250bbaef74dc11d69713207 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 20 Nov 2015 14:40:27 -0800 Subject: [PATCH 1/3] test script --- examples/acScripts/acSessionID.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/acScripts/acSessionID.js diff --git a/examples/acScripts/acSessionID.js b/examples/acScripts/acSessionID.js new file mode 100644 index 0000000000..d32185daa1 --- /dev/null +++ b/examples/acScripts/acSessionID.js @@ -0,0 +1 @@ +print("sessionID:" + Agent.sessionUUID); \ No newline at end of file From f56d2fba7f651af6b06b22dec4a6260f6d3c499a Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 20 Nov 2015 16:00:43 -0800 Subject: [PATCH 2/3] allow playbackAgents to auto-register --- assignment-client/src/Agent.cpp | 6 +++- assignment-client/src/Agent.h | 4 +++ examples/acScripts/playbackAgents.js | 46 ++++++++++++++++++++++------ examples/acScripts/playbackMaster.js | 34 +++++++++++++++++--- 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index 2fdba9e256..5cdceb06ae 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -223,7 +223,6 @@ void Agent::executeScript() { AvatarData::fromFrame(frame->data, *scriptedAvatar); }); - using namespace recording; static const FrameType AUDIO_FRAME_TYPE = Frame::registerFrameType(AudioConstants::AUDIO_FRAME_NAME); Frame::registerFrameHandler(AUDIO_FRAME_TYPE, [this, &scriptedAvatar](Frame::ConstPointer frame) { @@ -280,6 +279,11 @@ void Agent::executeScript() { setFinished(true); } +QUuid Agent::getSessionUUID() const { + return DependencyManager::get()->getSessionUUID(); +} + + void Agent::setIsAvatar(bool isAvatar) { _isAvatar = isAvatar; diff --git a/assignment-client/src/Agent.h b/assignment-client/src/Agent.h index 6819976633..274e5f3b55 100644 --- a/assignment-client/src/Agent.h +++ b/assignment-client/src/Agent.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,8 @@ class Agent : public ThreadedAssignment { Q_PROPERTY(bool isPlayingAvatarSound READ isPlayingAvatarSound) Q_PROPERTY(bool isListeningToAudioStream READ isListeningToAudioStream WRITE setIsListeningToAudioStream) Q_PROPERTY(float lastReceivedAudioLoudness READ getLastReceivedAudioLoudness) + Q_PROPERTY(QUuid sessionUUID READ getSessionUUID) + public: Agent(NLPacket& packet); @@ -47,6 +50,7 @@ public: void setIsListeningToAudioStream(bool isListeningToAudioStream) { _isListeningToAudioStream = isListeningToAudioStream; } float getLastReceivedAudioLoudness() const { return _lastReceivedAudioLoudness; } + QUuid getSessionUUID() const; virtual void aboutToFinish(); diff --git a/examples/acScripts/playbackAgents.js b/examples/acScripts/playbackAgents.js index c50c727277..66c3696fe3 100644 --- a/examples/acScripts/playbackAgents.js +++ b/examples/acScripts/playbackAgents.js @@ -10,7 +10,7 @@ // // Set the following variables to the values needed -var channel = "PlaybackChannel1"; +var commandChannel = "com.highfidelity.PlaybackChannel1"; var clip_url = null; var playFromCurrentLocation = true; var useDisplayName = true; @@ -18,7 +18,9 @@ var useAttachments = true; var useAvatarModel = true; // ID of the agent. Two agents can't have the same ID. -var id = 0; +var announceIDChannel = "com.highfidelity.playbackAgent.announceID"; +var UNKNOWN_AGENT_ID = -2; +var id = UNKNOWN_AGENT_ID; // unknown until aknowledged // Set position/orientation/scale here if playFromCurrentLocation is true Avatar.position = { x:0, y: 0, z: 0 }; @@ -45,7 +47,6 @@ Recording.setPlayerUseHeadModel(false); Recording.setPlayerUseSkeletonModel(useAvatarModel); function getAction(channel, message, senderID) { - if(subscribed) { var command = JSON.parse(message); print("I'm the agent " + id + " and I received this: ID: " + command.id_key + " Action: " + command.action_key + " URL: " + command.clip_url_key); @@ -139,17 +140,42 @@ function getAction(channel, message, senderID) { } -function update(deltaTime) { +function update(deltaTime) { totalTime += deltaTime; - if (totalTime > WAIT_FOR_AUDIO_MIXER && !subscribed) { - Messages.subscribe(channel); - subscribed = true; - print("I'm the agent and I am ready to receive!") + if (totalTime > WAIT_FOR_AUDIO_MIXER) { + if (!subscribed) { + Messages.subscribe(commandChannel); // command channel + Messages.subscribe(announceIDChannel); // id announce channel + subscribed = true; + print("I'm the agent and I am ready to receive!"); + } + if (subscribed && id == UNKNOWN_AGENT_ID) { + print("sending ready, id:" + id); + Messages.sendMessage(announceIDChannel, "ready"); + } } + } -Script.update.connect(update); -Messages.messageReceived.connect(getAction); +Messages.messageReceived.connect(function (channel, message, senderID) { + if (channel == announceIDChannel && message != "ready") { + // If I don't yet know if my ID has been recieved, then check to see if the master has acknowledged me + if (id == UNKNOWN_AGENT_ID) { + var parts = message.split("."); + var agentID = parts[0]; + var agentIndex = parts[1]; + if (agentID == Agent.sessionUUID) { + id = agentIndex; + Messages.unsubscribe(announceIDChannel); // id announce channel + } + } + } + if (channel == commandChannel) { + getAction(channel, message, senderID); + } +}); + +Script.update.connect(update); diff --git a/examples/acScripts/playbackMaster.js b/examples/acScripts/playbackMaster.js index e3448c0256..4703f0e4fd 100644 --- a/examples/acScripts/playbackMaster.js +++ b/examples/acScripts/playbackMaster.js @@ -14,11 +14,16 @@ HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; var ac_number = 1; // This is the default number of ACs. Their ID need to be unique and between 0 (included) and ac_number (excluded) var names = new Array(); // It is possible to specify the name of the ACs in this array. ACs names ordered by IDs (Default name is "ACx", x = ID + 1)) -var channel = "PlaybackChannel1"; +var channel = "com.highfidelity.PlaybackChannel1"; var subscribed = false; var clip_url = null; var input_text = null; +var knownAgents = new Array; // We will add our known agents here when we discover them + +// available playbackAgents will announce their sessionID here. +var announceIDChannel = "com.highfidelity.playbackAgent.announceID"; + // Script. DO NOT MODIFY BEYOND THIS LINE. Script.include("../libraries/toolBars.js"); @@ -51,8 +56,9 @@ setupPlayback(); function setupPlayback() { ac_number = Window.prompt("Insert number of agents: ","1"); - if (ac_number === "" || ac_number === null) + if (ac_number === "" || ac_number === null) { ac_number = 1; + } Messages.subscribe(channel); subscribed = true; setupToolBars(); @@ -134,7 +140,6 @@ function setupToolBars() { } function sendCommand(id, action) { - if (action === SHOW) { toolBars[id].selectTool(onOffIcon[id], false); toolBars[id].setAlpha(ALPHA_ON, playIcon[id]); @@ -151,8 +156,9 @@ function sendCommand(id, action) { return; } - if (id == (toolBars.length - 1)) + if (id == (toolBars.length - 1)) { id = -1; // Master command becomes broadcast. + } var message = { id_key: id, @@ -249,12 +255,30 @@ function scriptEnding() { Overlays.deleteOverlay(nameOverlays[i]); } - if(subscribed) + if (subscribed) { Messages.unsubscribe(channel); + } + Messages.unsubscribe(announceIDChannel); } Controller.mousePressEvent.connect(mousePressEvent); Script.update.connect(update); Script.scriptEnding.connect(scriptEnding); + + +Messages.subscribe(announceIDChannel); +Messages.messageReceived.connect(function (channel, message, senderID) { + if (channel == announceIDChannel && message == "ready") { + // check to see if we know about this agent + if (knownAgents.indexOf(senderID) < 0) { + var indexOfNewAgent = knownAgents.length; + knownAgents[indexOfNewAgent] = senderID; + var acknowledgeMessage = senderID + "." + indexOfNewAgent; + Messages.sendMessage(announceIDChannel, acknowledgeMessage); + } + + } +}); + moveUI(); \ No newline at end of file From 985fcf7c4b0619e6414d466bb41d045fc466b0a4 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 20 Nov 2015 16:02:01 -0800 Subject: [PATCH 3/3] remove unneeded example --- examples/acScripts/acSessionID.js | 1 - 1 file changed, 1 deletion(-) delete mode 100644 examples/acScripts/acSessionID.js diff --git a/examples/acScripts/acSessionID.js b/examples/acScripts/acSessionID.js deleted file mode 100644 index d32185daa1..0000000000 --- a/examples/acScripts/acSessionID.js +++ /dev/null @@ -1 +0,0 @@ -print("sessionID:" + Agent.sessionUUID); \ No newline at end of file