From a306e8d5b1fe3b1d5bd9f5a17cada6398a5d20ce Mon Sep 17 00:00:00 2001 From: AlessandroSigna Date: Thu, 19 Nov 2015 19:36:19 -0800 Subject: [PATCH 1/4] master can upload performance file on asset --- .../entityScripts/recordingEntityScript.js | 28 +++--- examples/entityScripts/recordingMaster.js | 86 +++++++++++++++++-- 2 files changed, 97 insertions(+), 17 deletions(-) diff --git a/examples/entityScripts/recordingEntityScript.js b/examples/entityScripts/recordingEntityScript.js index e423de9afd..dfbaf0a172 100644 --- a/examples/entityScripts/recordingEntityScript.js +++ b/examples/entityScripts/recordingEntityScript.js @@ -16,9 +16,11 @@ var _this; var isAvatarRecording = false; - var channel = "groupRecordingChannel"; - var startMessage = "RECONDING STARTED"; - var stopMessage = "RECONDING ENDED"; + var MASTER_TO_CLIENTS_CHANNEL = "startStopChannel"; + var CLIENTS_TO_MASTER_CHANNEL = "resultsChannel"; + var START_MESSAGE = "recordingStarted"; + var STOP_MESSAGE = "recordingEnded"; + var PARTICIPATING_MESSAGE = "participatingToRecording"; function recordingEntity() { _this = this; @@ -26,11 +28,13 @@ } function receivingMessage(channel, message, senderID) { - print("message received on channel:" + channel + ", message:" + message + ", senderID:" + senderID); - if(message === startMessage) { - _this.startRecording(); - } else if(message === stopMessage) { - _this.stopRecording(); + if(channel === MASTER_TO_CLIENTS_CHANNEL){ + print("CLIENT received message:" + message); + if(message === START_MESSAGE) { + _this.startRecording(); + } else if(message === STOP_MESSAGE) { + _this.stopRecording(); + } } }; @@ -50,19 +54,20 @@ enterEntity: function (entityID) { print("entering in the recording area"); - Messages.subscribe(channel); + Messages.subscribe(MASTER_TO_CLIENTS_CHANNEL); }, leaveEntity: function (entityID) { print("leaving the recording area"); _this.stopRecording(); - Messages.unsubscribe(channel); + Messages.unsubscribe(MASTER_TO_CLIENTS_CHANNEL); }, startRecording: function (entityID) { if (!isAvatarRecording) { print("RECORDING STARTED"); + Messages.sendMessage(CLIENTS_TO_MASTER_CHANNEL, PARTICIPATING_MESSAGE); //tell to master that I'm participating Recording.startRecording(); isAvatarRecording = true; } @@ -77,13 +82,14 @@ if (!(recordingFile === "null" || recordingFile === null || recordingFile === "")) { Recording.saveRecording(recordingFile); } + Messages.sendMessage(CLIENTS_TO_MASTER_CHANNEL, recordingFile); //send back to the master the url } }, unload: function (entityID) { print("RECORDING ENTITY UNLOAD"); _this.stopRecording(); - Messages.unsubscribe(channel); + Messages.unsubscribe(MASTER_TO_CLIENTS_CHANNEL); Messages.messageReceived.disconnect(receivingMessage); } } diff --git a/examples/entityScripts/recordingMaster.js b/examples/entityScripts/recordingMaster.js index 27f84f44c2..c757f94300 100644 --- a/examples/entityScripts/recordingMaster.js +++ b/examples/entityScripts/recordingMaster.js @@ -22,11 +22,24 @@ var TOOL_ICON_URL = HIFI_PUBLIC_BUCKET + "images/tools/"; var ALPHA_ON = 1.0; var ALPHA_OFF = 0.7; var COLOR_TOOL_BAR = { red: 0, green: 0, blue: 0 }; +var MASTER_TO_CLIENTS_CHANNEL = "startStopChannel"; +var CLIENTS_TO_MASTER_CHANNEL = "resultsChannel"; +var START_MESSAGE = "recordingStarted"; +var STOP_MESSAGE = "recordingEnded"; +var PARTICIPATING_MESSAGE = "participatingToRecording"; +var TIMEOUT = 20; var toolBar = null; var recordIcon; var isRecording = false; -var channel = "groupRecordingChannel"; +var results = []; +var performanceJSON = null; +var responsesExpected = 0; +var waitingForPerformanceFile = true; +var totalWaitingTime = 0; +var extension = "txt"; + +Messages.subscribe(CLIENTS_TO_MASTER_CHANNEL); setupToolBar(); function setupToolBar() { @@ -55,22 +68,83 @@ function mousePressEvent(event) { if (recordIcon === toolBar.clicked(clickedOverlay, false)) { if (!isRecording) { print("I'm the master. I want to start recording"); - var message = "RECONDING STARTED"; - Messages.sendMessage(channel, message); + Messages.sendMessage(MASTER_TO_CLIENTS_CHANNEL, START_MESSAGE); isRecording = true; } else { print("I want to stop recording"); - var message = "RECONDING ENDED"; - Messages.sendMessage(channel, message); + waitingForPerformanceFile = true; + Script.update.connect(update); + Messages.sendMessage(MASTER_TO_CLIENTS_CHANNEL, STOP_MESSAGE); isRecording = false; } } } +function masterReceivingMessage(channel, message, senderID) { + if(channel === CLIENTS_TO_MASTER_CHANNEL) { + print("MASTER received message:" + message ); + if(message === PARTICIPATING_MESSAGE){ + //increment the counter of all the participants + responsesExpected++; + } else if(waitingForPerformanceFile) { + //I get a atp url from one participant + results[results.length] = message; + var textJSON = '{ "results" : ['; + for (var index = 0; index < results.length; index++) { + var newRecord = index === (results.length - 1) ? '{ "hashATP":"' + results[index] + '" }' : '{ "hashATP":"' + results[index] + '" },'; + textJSON += newRecord; + } + textJSON += ']}'; + performanceJSON = JSON.parse(textJSON); + } + + } + } + +function update(deltaTime) { + if(waitingForPerformanceFile) { + totalWaitingTime += deltaTime; + if(totalWaitingTime > TIMEOUT || results.length === responsesExpected) { + //I can upload the performance file on the asset + print("UPLOADING PERFORMANCE FILE"); + print(JSON.stringify(performanceJSON)); + if(performanceJSON !== null) { + //upload + Assets.uploadData(JSON.stringify(performanceJSON), extension, uploadFinished); + } + //clean things after upload performance file to asset + waitingForPerformanceFile = false; + responsesExpected = 0; + totalWaitingTime = 0; + Script.update.disconnect(update); + results = []; + performanceJSON = null; + } + } +} + +function uploadFinished(url){ + print("data uploaded to:" + url); + uploadedFile = url; + Assets.downloadData(url, function (data) { + print("data downloaded from:" + url + " the data is: "); + printPerformanceJSON(JSON.parse(data)); + }); +} + +function printPerformanceJSON(obj) { + var results = obj.results; + results.forEach(function(param) { + var hash = param.hashATP; + print("url obtained: " + hash); + }); +} + function cleanup() { toolBar.cleanup(); - Messages.unsubscribe(channel); + Messages.unsubscribe(CLIENTS_TO_MASTER_CHANNEL); } Script.scriptEnding.connect(cleanup); Controller.mousePressEvent.connect(mousePressEvent); +Messages.messageReceived.connect(masterReceivingMessage); \ No newline at end of file From 7069f7d6ab00e5e5783f573ad872086a9ca56c47 Mon Sep 17 00:00:00 2001 From: AlessandroSigna Date: Fri, 20 Nov 2015 15:22:18 -0800 Subject: [PATCH 2/4] added clip load to asset + clean code --- .../entityScripts/recordingEntityScript.js | 21 +++++--- examples/entityScripts/recordingMaster.js | 50 ++++++++----------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/examples/entityScripts/recordingEntityScript.js b/examples/entityScripts/recordingEntityScript.js index dfbaf0a172..8f5e9a03b9 100644 --- a/examples/entityScripts/recordingEntityScript.js +++ b/examples/entityScripts/recordingEntityScript.js @@ -25,19 +25,24 @@ function recordingEntity() { _this = this; return; - } + }; function receivingMessage(channel, message, senderID) { - if(channel === MASTER_TO_CLIENTS_CHANNEL){ + if (channel === MASTER_TO_CLIENTS_CHANNEL) { print("CLIENT received message:" + message); - if(message === START_MESSAGE) { + if (message === START_MESSAGE) { _this.startRecording(); - } else if(message === STOP_MESSAGE) { + } else if (message === STOP_MESSAGE) { _this.stopRecording(); } } }; + function getClipUrl(url) { + Messages.sendMessage(CLIENTS_TO_MASTER_CHANNEL, url); //send back the url to the master + print("clip uploaded and url sent to master"); + }; + recordingEntity.prototype = { preload: function (entityID) { @@ -55,7 +60,6 @@ enterEntity: function (entityID) { print("entering in the recording area"); Messages.subscribe(MASTER_TO_CLIENTS_CHANNEL); - }, leaveEntity: function (entityID) { @@ -78,11 +82,12 @@ print("RECORDING ENDED"); Recording.stopRecording(); isAvatarRecording = false; - recordingFile = Window.save("Save recording to file", "./groupRecording", "Recordings (*.hfr)"); + Recording.saveRecordingToAsset(getClipUrl); //save the clip to the asset and link a callback to get its url + + var recordingFile = Window.save("Save recording to file", "./groupRecording", "Recordings (*.hfr)"); if (!(recordingFile === "null" || recordingFile === null || recordingFile === "")) { - Recording.saveRecording(recordingFile); + Recording.saveRecording(recordingFile); //save the clip locally } - Messages.sendMessage(CLIENTS_TO_MASTER_CHANNEL, recordingFile); //send back to the master the url } }, diff --git a/examples/entityScripts/recordingMaster.js b/examples/entityScripts/recordingMaster.js index c757f94300..cc3fa79026 100644 --- a/examples/entityScripts/recordingMaster.js +++ b/examples/entityScripts/recordingMaster.js @@ -32,13 +32,14 @@ var TIMEOUT = 20; var toolBar = null; var recordIcon; var isRecording = false; -var results = []; -var performanceJSON = null; +var avatarClips = []; +var performanceJSON = { "avatarClips" : [] }; var responsesExpected = 0; var waitingForPerformanceFile = true; var totalWaitingTime = 0; var extension = "txt"; + Messages.subscribe(CLIENTS_TO_MASTER_CHANNEL); setupToolBar(); @@ -81,35 +82,26 @@ function mousePressEvent(event) { } function masterReceivingMessage(channel, message, senderID) { - if(channel === CLIENTS_TO_MASTER_CHANNEL) { + if (channel === CLIENTS_TO_MASTER_CHANNEL) { print("MASTER received message:" + message ); - if(message === PARTICIPATING_MESSAGE){ + if (message === PARTICIPATING_MESSAGE) { //increment the counter of all the participants responsesExpected++; - } else if(waitingForPerformanceFile) { - //I get a atp url from one participant - results[results.length] = message; - var textJSON = '{ "results" : ['; - for (var index = 0; index < results.length; index++) { - var newRecord = index === (results.length - 1) ? '{ "hashATP":"' + results[index] + '" }' : '{ "hashATP":"' + results[index] + '" },'; - textJSON += newRecord; - } - textJSON += ']}'; - performanceJSON = JSON.parse(textJSON); + } else if (waitingForPerformanceFile) { + //I get an atp url from one participant + performanceJSON.avatarClips[performanceJSON.avatarClips.length] = message; } } } function update(deltaTime) { - if(waitingForPerformanceFile) { + if (waitingForPerformanceFile) { totalWaitingTime += deltaTime; - if(totalWaitingTime > TIMEOUT || results.length === responsesExpected) { - //I can upload the performance file on the asset + if (totalWaitingTime > TIMEOUT || performanceJSON.avatarClips.length === responsesExpected) { print("UPLOADING PERFORMANCE FILE"); - print(JSON.stringify(performanceJSON)); - if(performanceJSON !== null) { - //upload + if (performanceJSON.avatarClips.length !== 0) { + //I can upload the performance file on the asset Assets.uploadData(JSON.stringify(performanceJSON), extension, uploadFinished); } //clean things after upload performance file to asset @@ -117,26 +109,28 @@ function update(deltaTime) { responsesExpected = 0; totalWaitingTime = 0; Script.update.disconnect(update); - results = []; - performanceJSON = null; + avatarClips = []; + performanceJSON = { "avatarClips" : [] }; } } } function uploadFinished(url){ - print("data uploaded to:" + url); + print("some info:"); + print("performance file uploaded to: " + url); uploadedFile = url; Assets.downloadData(url, function (data) { - print("data downloaded from:" + url + " the data is: "); printPerformanceJSON(JSON.parse(data)); }); + //need to print somehow the url here //this way the master can copy the url + //Window.prompt(url); } function printPerformanceJSON(obj) { - var results = obj.results; - results.forEach(function(param) { - var hash = param.hashATP; - print("url obtained: " + hash); + print("downloaded performance file from asset and examinating its content..."); + var avatarClips = obj.avatarClips; + avatarClips.forEach(function(param) { + print("clip url obtained: " + param); }); } From a1053ee5a09af175178c2267ed904d462dacf259 Mon Sep 17 00:00:00 2001 From: AlessandroSigna Date: Fri, 20 Nov 2015 16:21:42 -0800 Subject: [PATCH 3/4] pop up a prompt with url --- examples/entityScripts/recordingEntityScript.js | 2 +- examples/entityScripts/recordingMaster.js | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/entityScripts/recordingEntityScript.js b/examples/entityScripts/recordingEntityScript.js index 8f5e9a03b9..132b064997 100644 --- a/examples/entityScripts/recordingEntityScript.js +++ b/examples/entityScripts/recordingEntityScript.js @@ -82,12 +82,12 @@ print("RECORDING ENDED"); Recording.stopRecording(); isAvatarRecording = false; - Recording.saveRecordingToAsset(getClipUrl); //save the clip to the asset and link a callback to get its url var recordingFile = Window.save("Save recording to file", "./groupRecording", "Recordings (*.hfr)"); if (!(recordingFile === "null" || recordingFile === null || recordingFile === "")) { Recording.saveRecording(recordingFile); //save the clip locally } + Recording.saveRecordingToAsset(getClipUrl); //save the clip to the asset and link a callback to get its url } }, diff --git a/examples/entityScripts/recordingMaster.js b/examples/entityScripts/recordingMaster.js index cc3fa79026..70ae93075f 100644 --- a/examples/entityScripts/recordingMaster.js +++ b/examples/entityScripts/recordingMaster.js @@ -32,7 +32,6 @@ var TIMEOUT = 20; var toolBar = null; var recordIcon; var isRecording = false; -var avatarClips = []; var performanceJSON = { "avatarClips" : [] }; var responsesExpected = 0; var waitingForPerformanceFile = true; @@ -109,7 +108,6 @@ function update(deltaTime) { responsesExpected = 0; totalWaitingTime = 0; Script.update.disconnect(update); - avatarClips = []; performanceJSON = { "avatarClips" : [] }; } } @@ -122,8 +120,8 @@ function uploadFinished(url){ Assets.downloadData(url, function (data) { printPerformanceJSON(JSON.parse(data)); }); - //need to print somehow the url here //this way the master can copy the url - //Window.prompt(url); + //need to print somehow the url here this way the master can copy the url + Window.prompt("Performance file url: ", url); } function printPerformanceJSON(obj) { From 05d311cff31893bfb3a8e17b1fcde24ae3ef643a Mon Sep 17 00:00:00 2001 From: AlessandroSigna Date: Fri, 20 Nov 2015 18:57:51 -0800 Subject: [PATCH 4/4] remove Windows.prompt cause of crash --- examples/entityScripts/recordingMaster.js | 27 ++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/examples/entityScripts/recordingMaster.js b/examples/entityScripts/recordingMaster.js index 70ae93075f..732521cff7 100644 --- a/examples/entityScripts/recordingMaster.js +++ b/examples/entityScripts/recordingMaster.js @@ -81,18 +81,17 @@ function mousePressEvent(event) { } function masterReceivingMessage(channel, message, senderID) { - if (channel === CLIENTS_TO_MASTER_CHANNEL) { - print("MASTER received message:" + message ); - if (message === PARTICIPATING_MESSAGE) { - //increment the counter of all the participants - responsesExpected++; - } else if (waitingForPerformanceFile) { - //I get an atp url from one participant - performanceJSON.avatarClips[performanceJSON.avatarClips.length] = message; - } - + if (channel === CLIENTS_TO_MASTER_CHANNEL) { + print("MASTER received message:" + message ); + if (message === PARTICIPATING_MESSAGE) { + //increment the counter of all the participants + responsesExpected++; + } else if (waitingForPerformanceFile) { + //I get an atp url from one participant + performanceJSON.avatarClips[performanceJSON.avatarClips.length] = message; } } +} function update(deltaTime) { if (waitingForPerformanceFile) { @@ -114,17 +113,15 @@ function update(deltaTime) { } function uploadFinished(url){ - print("some info:"); - print("performance file uploaded to: " + url); - uploadedFile = url; + //need to print somehow the url here this way the master can copy the url + print("PERFORMANCE FILE URL: " + url); Assets.downloadData(url, function (data) { printPerformanceJSON(JSON.parse(data)); }); - //need to print somehow the url here this way the master can copy the url - Window.prompt("Performance file url: ", url); } function printPerformanceJSON(obj) { + print("some info:"); print("downloaded performance file from asset and examinating its content..."); var avatarClips = obj.avatarClips; avatarClips.forEach(function(param) {