From 13994c82529b829989d19a9f1b3105d0ff19c04b Mon Sep 17 00:00:00 2001 From: devops Date: Sun, 17 Mar 2019 16:05:35 +0800 Subject: [PATCH] server done --- .../.vscode/launch.json | 9 +- .../record/AgoraCloudRecord.js | 6 +- .../record/src/recorder_manager.cc | 8 + .../record/src/recorder_manager.h | 1 + Agora-Cloud-Recording-Nodejs/server/app.js | 52 +++ .../server/package-lock.json | 363 ++++++++++++++++++ .../server/package.json | 15 + .../server/recordManager.js | 90 +++++ 8 files changed, 542 insertions(+), 2 deletions(-) create mode 100644 Agora-Cloud-Recording-Nodejs/server/app.js create mode 100644 Agora-Cloud-Recording-Nodejs/server/package-lock.json create mode 100644 Agora-Cloud-Recording-Nodejs/server/package.json create mode 100644 Agora-Cloud-Recording-Nodejs/server/recordManager.js diff --git a/Agora-Cloud-Recording-Nodejs/.vscode/launch.json b/Agora-Cloud-Recording-Nodejs/.vscode/launch.json index d13ef15..413fec2 100644 --- a/Agora-Cloud-Recording-Nodejs/.vscode/launch.json +++ b/Agora-Cloud-Recording-Nodejs/.vscode/launch.json @@ -8,7 +8,14 @@ "type": "node", "request": "launch", "name": "Launch Program", - "program": "${workspaceFolder}/recordtest.js", + "program": "${workspaceFolder}/record/recordtest.js", + "console": "integratedTerminal" + }, + { + "type": "node", + "request": "launch", + "name": "Launch Server", + "program": "${workspaceFolder}/server/app.js", "console": "integratedTerminal" }, { diff --git a/Agora-Cloud-Recording-Nodejs/record/AgoraCloudRecord.js b/Agora-Cloud-Recording-Nodejs/record/AgoraCloudRecord.js index 5936915..c537915 100644 --- a/Agora-Cloud-Recording-Nodejs/record/AgoraCloudRecord.js +++ b/Agora-Cloud-Recording-Nodejs/record/AgoraCloudRecord.js @@ -8,6 +8,10 @@ class AgoraCloudRecord extends EventEmitter { this.subscribeEvents() } + GetRecordingId() { + return this.sdk.GetRecordingId(); + } + StartCloudRecording(appid, cname, token, uid, recordConfig, storageConfig) { if(!appid) { throw new Error('appid is mandatory') @@ -19,7 +23,7 @@ class AgoraCloudRecord extends EventEmitter { token = token || "" uid = parseInt(uid) - if(isNaN(uid) && uid < 0) { + if(isNaN(uid) && uid <= 0) { throw new Error('invalid uid') } diff --git a/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.cc b/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.cc index cc66e03..4f90e5f 100644 --- a/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.cc +++ b/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.cc @@ -156,6 +156,7 @@ void RecorderManager::Init(Local exports) Nan::SetPrototypeMethod(tpl, "StartCloudRecording", StartCloudRecording); Nan::SetPrototypeMethod(tpl, "StopCloudRecording", StopCloudRecording); + Nan::SetPrototypeMethod(tpl, "GetRecordingId", GetRecordingId); Nan::SetPrototypeMethod(tpl, "onEvent", onEvent); Nan::SetPrototypeMethod(tpl, "Release", Release); @@ -186,6 +187,13 @@ void RecorderManager::Release(const Nan::FunctionCallbackInfo &args) napi_set_int_result(args, true); } +void RecorderManager::GetRecordingId(const Nan::FunctionCallbackInfo &args) +{ + RecorderManager *instance = ObjectWrap::Unwrap(args.Holder()); + RecordingId recording_id = instance->controller_->GetRecordingId(); + napi_set_string_result(args, recording_id); +} + void RecorderManager::StartCloudRecording(const Nan::FunctionCallbackInfo &args) { do diff --git a/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.h b/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.h index 8081f14..063c81f 100644 --- a/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.h +++ b/Agora-Cloud-Recording-Nodejs/record/src/recorder_manager.h @@ -56,6 +56,7 @@ class RecorderManager : public Nan::ObjectWrap, static void Release( const Nan::FunctionCallbackInfo &args); static void onEvent(const Nan::FunctionCallbackInfo &args); + static void GetRecordingId(const Nan::FunctionCallbackInfo &args); void OnRecordingConnecting(RecordingId recording_id); void OnRecordingStarted(RecordingId recording_id); diff --git a/Agora-Cloud-Recording-Nodejs/server/app.js b/Agora-Cloud-Recording-Nodejs/server/app.js new file mode 100644 index 0000000..c6f8d69 --- /dev/null +++ b/Agora-Cloud-Recording-Nodejs/server/app.js @@ -0,0 +1,52 @@ +const express = require('express') +const app = express() +const port = 3000 +const RecordManager = require('./recordManager') +const bodyParser = require('body-parser') + +app.use(bodyParser.json()); + +app.post('/recorder/v1/start', (req, res, next) => { + let { body } = req; + let { appid, channel, key, uid } = body; + if (!appid) { + throw new Error("appid is mandatory"); + } + if (!channel) { + throw new Error("channel is mandatory"); + } + + RecordManager.start(key, appid, channel, uid).then(recorder => { + //start recorder success + res.status(200).json({ + success: true, + sid: recorder.sid + }); + }).catch((e) => { + //start recorder failed + next(e); + }); +}) + +app.post('/recorder/v1/stop', (req, res, next) => { + let { body } = req; + let { sid } = body; + if (!sid) { + throw new Error("sid is mandatory"); + } + + RecordManager.stop(sid); + res.status(200).json({ + success: true + }); +}) + +app.use( (err, req, res, next) => { + console.error(err.stack) + res.status(500).json({ + success: false, + err: err.message || 'generic error' + }) +}) + +app.listen(port) \ No newline at end of file diff --git a/Agora-Cloud-Recording-Nodejs/server/package-lock.json b/Agora-Cloud-Recording-Nodejs/server/package-lock.json new file mode 100644 index 0000000..5423975 --- /dev/null +++ b/Agora-Cloud-Recording-Nodejs/server/package-lock.json @@ -0,0 +1,363 @@ +{ + "name": "server", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "accepts": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.5.tgz", + "integrity": "sha1-63d99gEXI6OxTopywIBcjoZ0a9I=", + "requires": { + "mime-types": "~2.1.18", + "negotiator": "0.6.1" + } + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, + "body-parser": { + "version": "1.18.3", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", + "integrity": "sha1-WykhmP/dVTs6DyDe0FkrlWlVyLQ=", + "requires": { + "bytes": "3.0.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "~1.6.3", + "iconv-lite": "0.4.23", + "on-finished": "~2.3.0", + "qs": "6.5.2", + "raw-body": "2.3.3", + "type-is": "~1.6.16" + } + }, + "bytes": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", + "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" + }, + "content-disposition": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz", + "integrity": "sha1-DPaLud318r55YcOoUXjLhdunjLQ=" + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, + "cookie": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", + "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, + "express": { + "version": "4.16.4", + "resolved": "https://registry.npmjs.org/express/-/express-4.16.4.tgz", + "integrity": "sha512-j12Uuyb4FMrd/qQAm6uCHAkPtO8FDTRJZBDd5D2KOL2eLaz1yUNdUB/NOIyq0iU4q4cFarsUCrnFDPBcnksuOg==", + "requires": { + "accepts": "~1.3.5", + "array-flatten": "1.1.1", + "body-parser": "1.18.3", + "content-disposition": "0.5.2", + "content-type": "~1.0.4", + "cookie": "0.3.1", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "1.1.1", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.2", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.4", + "qs": "6.5.2", + "range-parser": "~1.2.0", + "safe-buffer": "5.1.2", + "send": "0.16.2", + "serve-static": "1.13.2", + "setprototypeof": "1.1.0", + "statuses": "~1.4.0", + "type-is": "~1.6.16", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + } + }, + "finalhandler": { + "version": "1.1.1", + "resolved": "http://registry.npmjs.org/finalhandler/-/finalhandler-1.1.1.tgz", + "integrity": "sha512-Y1GUDo39ez4aHAw7MysnUD5JzYX+WaIj8I57kO3aEPT1fFRL4sr7mjei97FgnwhAyyzRYmQZaTHb2+9uZ1dPtg==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.2", + "statuses": "~1.4.0", + "unpipe": "~1.0.0" + } + }, + "forwarded": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", + "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, + "http-errors": { + "version": "1.6.3", + "resolved": "http://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz", + "integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.0", + "statuses": ">= 1.4.0 < 2" + } + }, + "iconv-lite": { + "version": "0.4.23", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz", + "integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "ipaddr.js": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz", + "integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4=" + }, + "media-typer": { + "version": "0.3.0", + "resolved": "http://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, + "mime": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.4.1.tgz", + "integrity": "sha512-KI1+qOZu5DcW6wayYHSzR/tXKCDC5Om4s1z2QJjDULzLcmf3DvzS7oluY4HCTrc+9FiKmWUgeNLg7W3uIQvxtQ==" + }, + "mime-db": { + "version": "1.37.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.37.0.tgz", + "integrity": "sha512-R3C4db6bgQhlIhPU48fUtdVmKnflq+hRdad7IyKhtFj06VPNVdk2RhiYL3UjQIlso8L+YxAtFkobT0VK+S/ybg==" + }, + "mime-types": { + "version": "2.1.21", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.21.tgz", + "integrity": "sha512-3iL6DbwpyLzjR3xHSFNFeb9Nz/M8WDkX33t1GFQnFOllWk8pOrh/LSrB5OXlnlW5P9LH73X6loW/eogc+F5lJg==", + "requires": { + "mime-db": "~1.37.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "negotiator": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", + "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=" + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.1" + } + }, + "parseurl": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz", + "integrity": "sha1-/CidTtiZMRlGDBViUyYs3I3mW/M=" + }, + "path-to-regexp": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", + "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" + }, + "proxy-addr": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.4.tgz", + "integrity": "sha512-5erio2h9jp5CHGwcybmxmVqHmnCBZeewlfJ0pex+UW7Qny7OOZXTtH56TGNyBizkgiOwhJtMKrVzDTeKcySZwA==", + "requires": { + "forwarded": "~0.1.2", + "ipaddr.js": "1.8.0" + } + }, + "qs": { + "version": "6.5.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", + "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" + }, + "range-parser": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.0.tgz", + "integrity": "sha1-9JvmtIeJTdxA3MlKMi9hEJLgDV4=" + }, + "raw-body": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.3.tgz", + "integrity": "sha512-9esiElv1BrZoI3rCDuOuKCBRbuApGGaDPQfjSflGxdy4oyzqghxu6klEkkVIvBje+FF0BX9coEv8KqW6X/7njw==", + "requires": { + "bytes": "3.0.0", + "http-errors": "1.6.3", + "iconv-lite": "0.4.23", + "unpipe": "1.0.0" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "send": { + "version": "0.16.2", + "resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz", + "integrity": "sha512-E64YFPUssFHEFBvpbbjr44NCLtI1AohxQ8ZSiJjQLskAdKuriYEP6VyGEsRDH8ScozGpkaX1BGvhanqCwkcEZw==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "~1.6.2", + "mime": "1.4.1", + "ms": "2.0.0", + "on-finished": "~2.3.0", + "range-parser": "~1.2.0", + "statuses": "~1.4.0" + } + }, + "serve-static": { + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.13.2.tgz", + "integrity": "sha512-p/tdJrO4U387R9oMjb1oj7qSMaMfmOyd4j9hOFoxZe2baQszgHcSWjuya/CiT5kgZZKRudHNOA0pYXOl8rQ5nw==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.2", + "send": "0.16.2" + } + }, + "setprototypeof": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", + "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" + }, + "statuses": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", + "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" + }, + "type-is": { + "version": "1.6.16", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.16.tgz", + "integrity": "sha512-HRkVv/5qY2G6I8iab9cI7v1bOIdhm94dVjQCPFElW9W+3GeDOSHmy2EBYe4VTApuzolPcmgFTN3ftVJRKR2J9Q==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.18" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, + "uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + } + } +} diff --git a/Agora-Cloud-Recording-Nodejs/server/package.json b/Agora-Cloud-Recording-Nodejs/server/package.json new file mode 100644 index 0000000..db08418 --- /dev/null +++ b/Agora-Cloud-Recording-Nodejs/server/package.json @@ -0,0 +1,15 @@ +{ + "name": "server", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.16.4", + "uuid": "^3.3.2" + } +} diff --git a/Agora-Cloud-Recording-Nodejs/server/recordManager.js b/Agora-Cloud-Recording-Nodejs/server/recordManager.js new file mode 100644 index 0000000..19c0584 --- /dev/null +++ b/Agora-Cloud-Recording-Nodejs/server/recordManager.js @@ -0,0 +1,90 @@ +const AgoraCloudRecord = require("../record/AgoraCloudRecord"); +const path = require("path"); +const fs = require("fs"); +const uuidv4 = require('uuid/v4'); +const storageConfig = require('./storage.json') + +class RecordManager{ + constructor() { + this.recorders = {}; + } + + //find existing recorder + find(sid) { + return this.recorders[sid]; + } + + start(key, appid, channel, uid, recordConfig) { + return new Promise((resolve, reject) => { + let sdk = new AgoraCloudRecord(); + let recorder = { + appid: appid, + channel: channel, + sdk: sdk, + sid: sdk.GetRecordingId(), + }; + this.recorders[recorder.sid] = recorder + this.subscribeEvents(recorder) + sdk.once('RecordStarted', recording_id => { + console.log(`record started: ${recording_id}`) + resolve(recorder) + }) + sdk.StartCloudRecording(appid, channel, key || "", uid || 0, recordConfig, storageConfig) + }); + } + + subscribeEvents(recorder) { + let { sdk, sid, appid, channel } = recorder; + sdk.on('RecordConnecting', recording_id => { + console.log(`record connecting: ${recording_id}`) + }) + sdk.on('RecordStopped', recording_id => { + console.log(`record stopped: ${recording_id}`) + }) + sdk.on('RecordUploaded', (recording_id, file_name) => { + console.log(`record uploaded: ${recording_id} ${file_name}`) + }) + sdk.on('RecordBackedUp', (recording_id, file_name) => { + console.log(`record backed up: ${recording_id} ${file_name}`) + }) + sdk.on('RecordUpdateProgress', (recording_id, progress, recording_playlist_filename) => { + console.log(`record update progress: ${recording_id} ${progress} ${recording_playlist_filename}`) + }) + sdk.on('RecordFailure', (recording_id, code, msg) => { + console.log(`record failure: ${recording_id} ${code} ${msg}`) + }) + sdk.on('UploadFailure', (recording_id, code, msg) => { + console.log(`upload failure: ${recording_id} ${code} ${msg}`) + }) + sdk.on('RecordFatalError', (recording_id, code) => { + console.log(`record fatal error: ${recording_id} ${code}`) + }) + } + + stop(sid) { + let recorder = this.recorders[sid]; + if(recorder) { + let {appid, channel} = recorder; + console.log(`stop recorder ${appid} ${channel} ${sid}`) + this.onCleanup(sid); + } else { + throw new Error('recorder not exists'); + } + } + + onCleanup(sid) { + let recorder = this.recorders[sid]; + if(recorder) { + let {sdk} = recorder; + console.log(`releasing ${sid}`) + sdk.Release() + delete this.recorders[`${sid}`]; + } else { + throw new Error('recorder not exists'); + } + } +} + + + +module.exports = new RecordManager(); \ No newline at end of file