diff --git a/lib/session/call-session.js b/lib/session/call-session.js index 645fedcb..9f23dd2c 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -114,6 +114,10 @@ class CallSession extends Emitter { return this.callInfo.applicationSid; } + get callStatus() { + return this.callInfo.callStatus; + } + /** * SIP call-id for the call */ diff --git a/lib/tasks/play.js b/lib/tasks/play.js index 07b8f48e..8d917d7e 100644 --- a/lib/tasks/play.js +++ b/lib/tasks/play.js @@ -1,5 +1,5 @@ const Task = require('./task'); -const {TaskName, TaskPreconditions} = require('../utils/constants'); +const {TaskName, TaskPreconditions, CallStatus} = require('../utils/constants'); class TaskPlay extends Task { constructor(logger, opts) { @@ -22,6 +22,20 @@ class TaskPlay extends Task { async exec(cs, {ep}) { await super.exec(cs); this.ep = ep; + let timeout; + let playbackSeconds = 0; + let playbackMilliseconds = 0; + let completed = !(this.timeoutSecs > 0 || this.loop); + if (this.timeoutSecs > 0) { + timeout = setTimeout(async() => { + completed = true; + try { + await this.kill(cs); + } catch (err) { + this.logger.info(err, 'Error killing audio on timeoutSecs'); + } + }, this.timeoutSecs * 1000); + } try { while (!this.killed && (this.loop === 'forever' || this.loop--) && this.ep.connected) { if (cs.isInConference) { @@ -34,14 +48,24 @@ class TaskPlay extends Task { await this.playToConfMember(this.ep, memberId, confName, confUuid, this.url); } } else { - const file = (this.timeoutSecs >= 0 || this.seekOffset >= 0) ? - {file: this.url, seekOffset: this.seekOffset, timeoutSecs: this.timeoutSecs} : this.url; + let file = this.url; + if (this.seekOffset >= 0) { + file = {file: this.url, seekOffset: this.seekOffset}; + this.seekOffset = -1; + } const result = await ep.play(file); - await this.performAction(Object.assign(result, {reason: 'playCompleted'}), - !(this.parentTask || cs.isConfirmCallSession)); + playbackSeconds += parseInt(result.playbackSeconds); + playbackMilliseconds += parseInt(result.playbackMilliseconds); + if (this.killed || !this.loop || completed) { + if (timeout) clearTimeout(timeout); + await this.performAction( + Object.assign(result, {reason: 'playCompleted', playbackSeconds, playbackMilliseconds}), + !(this.parentTask || cs.isConfirmCallSession)); + } } } } catch (err) { + if (timeout) clearTimeout(timeout); this.logger.info(err, `TaskPlay:exec - error playing ${this.url}`); } this.emit('playDone');