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 a84701354e..918d8a1e1d 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); @@ -134,17 +135,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