rubjs 2.7.3 → 2.7.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.
package/package.json
CHANGED
@@ -14,13 +14,16 @@ declare class VoiceChatClient {
|
|
14
14
|
private isPaused;
|
15
15
|
private lastPosition;
|
16
16
|
private intervalId?;
|
17
|
-
|
17
|
+
private isManualSkip;
|
18
|
+
private getAnyMusicCallback;
|
19
|
+
constructor(getAnyMusicCallback?: () => Promise<string>);
|
18
20
|
play(filePath?: string): Promise<void>;
|
19
21
|
stop(): void;
|
20
22
|
resume(): void;
|
21
|
-
next(): void
|
23
|
+
next(): Promise<void>;
|
22
24
|
previous(): void;
|
23
25
|
addToPlaylist(filePath: string): void;
|
26
|
+
removeFromPlaylist(filePath: string): void;
|
24
27
|
joinVoiceChat(chatGuid: string, voiceChatId: string, client: Client): Promise<void>;
|
25
28
|
private clearIntervals;
|
26
29
|
}
|
@@ -12,43 +12,60 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
const child_process_1 = require("child_process");
|
13
13
|
const wrtc_1 = require("wrtc");
|
14
14
|
class VoiceChatClient {
|
15
|
-
constructor() {
|
15
|
+
constructor(getAnyMusicCallback) {
|
16
16
|
this.buffer = Buffer.alloc(0);
|
17
17
|
this.frameSize = 960;
|
18
18
|
this.playlist = [];
|
19
19
|
this.currentIndex = 0;
|
20
20
|
this.isPaused = false;
|
21
21
|
this.lastPosition = 0;
|
22
|
+
this.isManualSkip = false;
|
22
23
|
this.pc = new wrtc_1.RTCPeerConnection();
|
23
24
|
this.source = new wrtc_1.nonstandard.RTCAudioSource();
|
24
25
|
this.track = this.source.createTrack();
|
25
26
|
this.pc.addTrack(this.track);
|
27
|
+
this.getAnyMusicCallback = getAnyMusicCallback;
|
26
28
|
}
|
27
29
|
play(filePath) {
|
28
30
|
return __awaiter(this, void 0, void 0, function* () {
|
29
31
|
if (this.ffmpeg)
|
30
32
|
this.stop();
|
31
33
|
if (!filePath) {
|
32
|
-
if (this.playlist.length === 0)
|
33
|
-
|
34
|
+
if (this.playlist.length === 0) {
|
35
|
+
if (this.getAnyMusicCallback) {
|
36
|
+
this.playlist = [yield this.getAnyMusicCallback()];
|
37
|
+
}
|
38
|
+
else
|
39
|
+
return;
|
40
|
+
}
|
34
41
|
filePath = this.playlist[this.currentIndex];
|
35
42
|
}
|
36
43
|
if (!this.chatGuid || !this.voiceChatId || !this.client) {
|
37
44
|
throw new Error("Voice chat not initialized. Call `joinVoiceChat` first.");
|
38
45
|
}
|
39
46
|
this.isPaused = false;
|
47
|
+
this.isManualSkip = false;
|
40
48
|
this.buffer = Buffer.alloc(0);
|
41
49
|
const seekArgs = this.lastPosition > 0 ? ["-ss", this.lastPosition.toString()] : [];
|
42
50
|
this.ffmpeg = (0, child_process_1.spawn)("ffmpeg", [
|
43
|
-
"-loglevel",
|
51
|
+
"-loglevel",
|
52
|
+
"quiet",
|
44
53
|
"-re",
|
45
|
-
"-threads",
|
54
|
+
"-threads",
|
55
|
+
"1",
|
56
|
+
"-protocol_whitelist",
|
57
|
+
"file,http,https,tcp,tls",
|
46
58
|
...seekArgs,
|
47
|
-
"-i",
|
48
|
-
|
49
|
-
"-
|
50
|
-
"
|
51
|
-
"-
|
59
|
+
"-i",
|
60
|
+
filePath,
|
61
|
+
"-acodec",
|
62
|
+
"pcm_s16le",
|
63
|
+
"-ar",
|
64
|
+
"48000",
|
65
|
+
"-ac",
|
66
|
+
"1",
|
67
|
+
"-f",
|
68
|
+
"s16le",
|
52
69
|
"pipe:1",
|
53
70
|
]);
|
54
71
|
this.ffmpeg.stdout.on("data", (chunk) => {
|
@@ -64,8 +81,27 @@ class VoiceChatClient {
|
|
64
81
|
this.lastPosition += 0.02;
|
65
82
|
}
|
66
83
|
});
|
67
|
-
this.ffmpeg.on("close", () => this
|
68
|
-
|
84
|
+
this.ffmpeg.on("close", (code) => __awaiter(this, void 0, void 0, function* () {
|
85
|
+
if (!this.isManualSkip && code !== null && !this.isPaused) {
|
86
|
+
yield this.next();
|
87
|
+
}
|
88
|
+
this.isManualSkip = false;
|
89
|
+
}));
|
90
|
+
this.clearIntervals();
|
91
|
+
this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
92
|
+
try {
|
93
|
+
yield this.client.getGroupVoiceChatUpdates(this.chatGuid, this.voiceChatId);
|
94
|
+
}
|
95
|
+
catch (_a) { }
|
96
|
+
}), 10 * 1000);
|
97
|
+
this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
98
|
+
try {
|
99
|
+
if (this.isPaused)
|
100
|
+
return;
|
101
|
+
yield this.client.sendVoiceChatActivity(this.chatGuid, this.voiceChatId, this.client.userGuid);
|
102
|
+
}
|
103
|
+
catch (_a) { }
|
104
|
+
}), 2 * 1000);
|
69
105
|
});
|
70
106
|
}
|
71
107
|
stop() {
|
@@ -73,7 +109,7 @@ class VoiceChatClient {
|
|
73
109
|
this.ffmpeg.kill();
|
74
110
|
this.ffmpeg = undefined;
|
75
111
|
this.isPaused = true;
|
76
|
-
this.buffer = Buffer.alloc(0);
|
112
|
+
this.buffer = Buffer.alloc(0);
|
77
113
|
}
|
78
114
|
}
|
79
115
|
resume() {
|
@@ -81,22 +117,45 @@ class VoiceChatClient {
|
|
81
117
|
this.play();
|
82
118
|
}
|
83
119
|
next() {
|
84
|
-
|
85
|
-
this.currentIndex
|
86
|
-
|
87
|
-
|
88
|
-
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
121
|
+
if (this.playlist.length > this.currentIndex + 1) {
|
122
|
+
this.isManualSkip = true;
|
123
|
+
this.stop();
|
124
|
+
this.currentIndex += 1;
|
125
|
+
this.lastPosition = 0;
|
126
|
+
this.isPaused = true;
|
127
|
+
this.play(this.playlist[this.currentIndex]);
|
128
|
+
}
|
129
|
+
else {
|
130
|
+
if (this.getAnyMusicCallback) {
|
131
|
+
this.playlist = [...this.playlist, yield this.getAnyMusicCallback()];
|
132
|
+
yield this.next();
|
133
|
+
}
|
134
|
+
}
|
135
|
+
});
|
89
136
|
}
|
90
137
|
previous() {
|
91
138
|
if (this.currentIndex > 0) {
|
139
|
+
this.isManualSkip = true;
|
140
|
+
this.stop();
|
92
141
|
this.currentIndex--;
|
93
142
|
this.lastPosition = 0;
|
143
|
+
this.isPaused = true;
|
94
144
|
this.play();
|
95
145
|
}
|
96
146
|
}
|
97
147
|
addToPlaylist(filePath) {
|
98
148
|
this.playlist.push(filePath);
|
99
149
|
}
|
150
|
+
removeFromPlaylist(filePath) {
|
151
|
+
const index = this.playlist.indexOf(filePath);
|
152
|
+
if (index !== -1) {
|
153
|
+
this.playlist.splice(index, 1);
|
154
|
+
if (index <= this.currentIndex && this.currentIndex > 0) {
|
155
|
+
this.currentIndex--;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
}
|
100
159
|
joinVoiceChat(chatGuid, voiceChatId, client) {
|
101
160
|
return __awaiter(this, void 0, void 0, function* () {
|
102
161
|
this.chatGuid = chatGuid;
|
@@ -108,19 +167,6 @@ class VoiceChatClient {
|
|
108
167
|
const sdpAnswer = connect.sdp_answer_data;
|
109
168
|
yield this.pc.setRemoteDescription(new wrtc_1.RTCSessionDescription({ type: "answer", sdp: sdpAnswer }));
|
110
169
|
yield client.setVoiceChatState(chatGuid, voiceChatId);
|
111
|
-
this.clearIntervals();
|
112
|
-
this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
113
|
-
try {
|
114
|
-
yield client.getGroupVoiceChatUpdates(chatGuid, voiceChatId);
|
115
|
-
}
|
116
|
-
catch (_a) { }
|
117
|
-
}), 10 * 1000);
|
118
|
-
this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
119
|
-
try {
|
120
|
-
yield client.sendVoiceChatActivity(chatGuid, voiceChatId, client.userGuid);
|
121
|
-
}
|
122
|
-
catch (_a) { }
|
123
|
-
}), 2 * 1000);
|
124
170
|
});
|
125
171
|
}
|
126
172
|
clearIntervals() {
|