@fonoster/streams 0.16.0 → 0.16.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ export declare class AudioPlayer {
7
7
  private activeStream;
8
8
  private socket;
9
9
  private isPlaying;
10
+ private currentSessionId;
10
11
  /**
11
12
  * Creates a new AudioPlayer.
12
13
  *
@@ -63,6 +63,7 @@ exports.AudioPlayer = void 0;
63
63
  */
64
64
  const fs = __importStar(require("fs"));
65
65
  const promises_1 = require("node:timers/promises");
66
+ const stream_1 = require("stream");
66
67
  const logger_1 = require("@fonoster/logger");
67
68
  const Message_1 = require("./Message");
68
69
  const logger = (0, logger_1.getLogger)({ service: "streams", filePath: __filename });
@@ -79,6 +80,7 @@ class AudioPlayer {
79
80
  constructor(socket) {
80
81
  this.activeStream = null;
81
82
  this.isPlaying = false;
83
+ this.currentSessionId = 0;
82
84
  this.socket = socket;
83
85
  }
84
86
  /**
@@ -89,16 +91,9 @@ class AudioPlayer {
89
91
  */
90
92
  play(filePath) {
91
93
  return __awaiter(this, void 0, void 0, function* () {
92
- const fileStream = fs.readFileSync(filePath);
93
94
  logger.verbose("playing audio file", { filePath });
94
- let offset = 0;
95
- // eslint-disable-next-line no-loops/no-loops
96
- while (offset < fileStream.length) {
97
- const sliceSize = Math.min(fileStream.length - offset, MAX_CHUNK_SIZE);
98
- const slicedChunk = fileStream.subarray(offset, offset + sliceSize);
99
- yield this._processAudioChunk(slicedChunk);
100
- offset += sliceSize;
101
- }
95
+ const fileData = fs.readFileSync(filePath);
96
+ return this.playStream(stream_1.Readable.from(fileData));
102
97
  });
103
98
  }
104
99
  /**
@@ -110,16 +105,26 @@ class AudioPlayer {
110
105
  */
111
106
  playStream(inputStream) {
112
107
  return __awaiter(this, void 0, void 0, function* () {
108
+ // Stop any currently playing stream before starting a new one
109
+ this.stop();
113
110
  this.isPlaying = true;
111
+ // Increment session ID to invalidate any in-flight processing from previous streams
112
+ const sessionId = ++this.currentSessionId;
114
113
  this.activeStream = inputStream;
115
114
  const buffer = [];
116
115
  let isProcessing = false;
117
116
  const processBuffer = () => __awaiter(this, void 0, void 0, function* () {
118
- if (!this.isPlaying || isProcessing || buffer.length === 0)
117
+ // Check both isPlaying AND that this session is still current
118
+ if (!this.isPlaying ||
119
+ sessionId !== this.currentSessionId ||
120
+ isProcessing ||
121
+ buffer.length === 0)
119
122
  return;
120
123
  isProcessing = true;
121
124
  try {
122
- while (buffer.length > 0 && this.isPlaying) {
125
+ while (buffer.length > 0 &&
126
+ this.isPlaying &&
127
+ sessionId === this.currentSessionId) {
123
128
  const chunk = buffer.shift();
124
129
  yield this._processAudioChunk(chunk);
125
130
  }
@@ -130,7 +135,8 @@ class AudioPlayer {
130
135
  });
131
136
  return new Promise((resolve, reject) => {
132
137
  inputStream.on("data", (chunk) => __awaiter(this, void 0, void 0, function* () {
133
- if (!this.isPlaying || this.activeStream !== inputStream)
138
+ // Check session ID to ensure this stream is still active
139
+ if (!this.isPlaying || sessionId !== this.currentSessionId)
134
140
  return;
135
141
  for (let offset = 0; offset < chunk.length; offset += MAX_CHUNK_SIZE) {
136
142
  const sliceSize = Math.min(chunk.length - offset, MAX_CHUNK_SIZE);
@@ -172,9 +178,7 @@ class AudioPlayer {
172
178
  this.activeStream.removeAllListeners("data");
173
179
  this.activeStream.removeAllListeners("error");
174
180
  this.activeStream.removeAllListeners("end");
175
- if (typeof this.activeStream.pause === "function") {
176
- this.activeStream.pause();
177
- }
181
+ this.activeStream.pause();
178
182
  this.activeStream = null;
179
183
  }
180
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/streams",
3
- "version": "0.16.0",
3
+ "version": "0.16.5",
4
4
  "description": "Core support for Fonoster Streams",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -18,7 +18,7 @@
18
18
  "generate:readme": "node ../../.scripts/gen-readme.cjs"
19
19
  },
20
20
  "dependencies": {
21
- "@fonoster/logger": "^0.16.0",
21
+ "@fonoster/logger": "^0.16.5",
22
22
  "uuid": "^11.0.3"
23
23
  },
24
24
  "devDependencies": {
@@ -37,5 +37,5 @@
37
37
  "bugs": {
38
38
  "url": "https://github.com/fonoster/fonoster/issues"
39
39
  },
40
- "gitHead": "ad4212a7938ad2e219b9d1ae27a430ec60d9f841"
40
+ "gitHead": "a5ccfd263e40899643d1bbd298d89e729d8c4802"
41
41
  }