lolka.js 0.4.3 → 0.5.2

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.
Files changed (45) hide show
  1. package/README.md +279 -11
  2. package/package.json +3 -5
  3. package/src/client/voice/AudioPlayer.js +358 -0
  4. package/src/client/voice/AudioResource.js +137 -0
  5. package/src/client/voice/VoiceConnection.js +419 -290
  6. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-libdatachannel.txt +373 -0
  7. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-libjuice.txt +373 -0
  8. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-libsrtp.txt +35 -0
  9. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-nlohmann-json.txt +21 -0
  10. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-node-addon-api.txt +9 -0
  11. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-node-api-headers.txt +21 -0
  12. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-openssl.txt +177 -0
  13. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-opus.txt +44 -0
  14. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-plog.txt +21 -0
  15. package/src/client/voice/_lolka_voice/LICENSES/LICENSE-usrsctp.txt +27 -0
  16. package/src/client/voice/_lolka_voice/LICENSES/THIRD-PARTY.txt +16 -0
  17. package/src/client/voice/_lolka_voice/MANIFEST.json +47 -0
  18. package/src/client/voice/_lolka_voice/_loader.js +103 -0
  19. package/src/client/voice/_lolka_voice/_native/linux-aarch64/core.node +0 -0
  20. package/src/client/voice/_lolka_voice/_native/linux-aarch64-musl/core.node +0 -0
  21. package/src/client/voice/_lolka_voice/_native/linux-x86_64/core.node +0 -0
  22. package/src/client/voice/_lolka_voice/_native/linux-x86_64-musl/core.node +0 -0
  23. package/src/client/voice/_lolka_voice/_native/macos-arm64/core.node +0 -0
  24. package/src/client/voice/_lolka_voice/_native/macos-x86_64/core.node +0 -0
  25. package/src/client/voice/_lolka_voice/_native/windows-x86_64/core.node +0 -0
  26. package/src/client/voice/_lolka_voice/_version.js +4 -0
  27. package/src/client/voice/_lolka_voice/caps.js +56 -0
  28. package/src/client/voice/_lolka_voice/core.d.ts +200 -0
  29. package/src/client/voice/_lolka_voice/errors.js +22 -0
  30. package/src/client/voice/_lolka_voice/events.js +119 -0
  31. package/src/client/voice/_lolka_voice/index.d.ts +226 -0
  32. package/src/client/voice/_lolka_voice/index.js +35 -0
  33. package/src/client/voice/_lolka_voice/player.js +606 -0
  34. package/src/client/voice/_lolka_voice/receive.js +387 -0
  35. package/src/client/voice/_lolka_voice/sdp.js +137 -0
  36. package/src/client/voice/_lolka_voice/session.js +335 -0
  37. package/src/client/voice/_lolka_voice/signaling.js +120 -0
  38. package/src/client/voice/_lolka_voice/transport.js +260 -0
  39. package/src/client/voice/media/OpusPlayback.js +199 -0
  40. package/src/client/voice/media/Receiver.js +245 -0
  41. package/src/client/voice/media/deps.js +27 -0
  42. package/src/index.js +11 -0
  43. package/src/structures/Guild.js +5 -2
  44. package/typings/index.d.mts +256 -4
  45. package/typings/index.d.ts +256 -4
@@ -229,7 +229,7 @@ import {
229
229
  } from 'discord-api-types/v10';
230
230
  import { ChildProcess } from 'node:child_process';
231
231
  import { EventEmitter } from 'node:events';
232
- import { Stream } from 'node:stream';
232
+ import { Readable, Stream } from 'node:stream';
233
233
  import { MessagePort, Worker } from 'node:worker_threads';
234
234
  import {
235
235
  RawActivityData,
@@ -8134,11 +8134,81 @@ export interface VoiceConnectionOptions {
8134
8134
  adapterCreator: InternalDiscordGatewayAdapterCreator;
8135
8135
  selfMute?: boolean;
8136
8136
  selfDeaf?: boolean;
8137
+ /** Сразу включить приём чужого звука (иначе его включают слушатель `'track'` или `receiver.subscribe()`). */
8138
+ receive?: boolean;
8139
+ }
8140
+
8141
+ export interface VoiceConnectionRejoinOptions {
8142
+ channelId?: string;
8143
+ selfMute?: boolean;
8144
+ selfDeaf?: boolean;
8137
8145
  }
8138
8146
 
8139
8147
  export type VoiceConnectionState = 'connecting' | 'ready' | 'error' | 'destroyed';
8140
8148
 
8149
+ export type VoiceInputType = 'arbitrary' | 'raw' | 'ogg/opus' | 'webm/opus' | 'opus';
8150
+
8151
+ export interface VoicePlayOptions {
8152
+ /** `'arbitrary'` (по умолчанию) – любой формат через ffmpeg; `'raw'` – PCM s16le 48 кГц стерео без заголовка через ffmpeg; `'ogg/opus'` и `'webm/opus'` – без ffmpeg; `'opus'` – объектный поток готовых Opus-пакетов. */
8153
+ inputType?: VoiceInputType;
8154
+ /** Битрейт кодирования в кбит/с, только для `'arbitrary'` и `'raw'` (по умолчанию 128). */
8155
+ bitrate?: number;
8156
+ }
8157
+
8158
+ export interface VoiceConnectionStats {
8159
+ state: VoiceConnectionState;
8160
+ playing: boolean;
8161
+ paused: boolean;
8162
+ queuedFrames: number;
8163
+ transportState: string | null;
8164
+ receiving: boolean;
8165
+ consumers: number;
8166
+ packetsSent: number;
8167
+ bytesSent: number;
8168
+ underruns: number;
8169
+ lateTicks: number;
8170
+ bursts: number;
8171
+ resyncs: number;
8172
+ silentSkipped: number;
8173
+ drops: number;
8174
+ /** Кадры плеера, пропущенные соединением из-за отставания. */
8175
+ skipped: number;
8176
+ /** Соединение подписано на плеер. */
8177
+ subscribed: boolean;
8178
+ }
8179
+
8180
+ export interface RemoteAudioPacketInfo {
8181
+ sequence: number;
8182
+ timestamp: number;
8183
+ ssrc: number;
8184
+ }
8185
+
8186
+ export class RemoteAudioTrack extends EventEmitter {
8187
+ private constructor();
8188
+ public readonly userId: string;
8189
+ public readonly producerId: string;
8190
+ public readonly consumerId: string;
8191
+ public readonly ssrc: number;
8192
+ public readonly readyState: 'live' | 'ended';
8193
+ public createReadStream(): Readable;
8194
+ public stop(): void;
8195
+ public on(event: 'packet', listener: (opus: Buffer, info: RemoteAudioPacketInfo) => void): this;
8196
+ public on(event: 'ended', listener: () => void): this;
8197
+ public once(event: 'packet', listener: (opus: Buffer, info: RemoteAudioPacketInfo) => void): this;
8198
+ public once(event: 'ended', listener: () => void): this;
8199
+ }
8200
+
8201
+ export type VoiceSubscriptionStream = Readable & { dropped: number };
8202
+
8203
+ export class VoiceReceiver {
8204
+ private constructor();
8205
+ public readonly tracks: ReadonlyMap<string, RemoteAudioTrack>;
8206
+ public subscribe(userId: string): VoiceSubscriptionStream;
8207
+ }
8208
+
8141
8209
  export class VoiceConnection extends EventEmitter {
8210
+ public static kickGraceMs: number;
8211
+ public static recvRebuildThreshold: number;
8142
8212
  private constructor(options: VoiceConnectionOptions);
8143
8213
  public channelId: string;
8144
8214
  public guildId: string;
@@ -8146,20 +8216,202 @@ export class VoiceConnection extends EventEmitter {
8146
8216
  public sessionId: string | null;
8147
8217
  public token: string | null;
8148
8218
  public endpoint: string | null;
8219
+ public readonly receiver: VoiceReceiver;
8220
+ public readonly paused: boolean;
8149
8221
  public awaitReady(timeout?: number): Promise<void>;
8150
- public play(input: string | NodeJS.ReadableStream): void;
8222
+ public rejoin(options?: VoiceConnectionRejoinOptions): boolean;
8223
+ public play(input: string | NodeJS.ReadableStream, options?: VoicePlayOptions): void;
8151
8224
  public stop(): void;
8225
+ public pause(): boolean;
8226
+ public resume(): boolean;
8227
+ public unpause(): boolean;
8228
+ public stats(): VoiceConnectionStats;
8229
+ /** Подписка на плеер. `state` здесь строка, поэтому подписка лежит отдельно, а не в `state.subscription`. */
8230
+ public readonly subscription: PlayerSubscription | undefined;
8231
+ /** Подписаться на плеер; `undefined` у уничтоженного соединения. Новая подписка снимает прежнюю. */
8232
+ public subscribe(player: AudioPlayer): PlayerSubscription | undefined;
8152
8233
  public destroy(): void;
8153
8234
  public on(event: 'ready' | 'playing' | 'idle' | 'destroyed', listener: () => void): this;
8154
8235
  public on(event: 'error', listener: (error: Error) => void): this;
8155
- public on(event: 'track', listener: (track: unknown, userId: string, producerId: string) => void): this;
8236
+ public on(event: 'track', listener: (track: RemoteAudioTrack, userId: string, producerId: string) => void): this;
8237
+ public on(event: 'speaking', listener: (userId: string, speaking: boolean) => void): this;
8156
8238
  public once(event: 'ready' | 'playing' | 'idle' | 'destroyed', listener: () => void): this;
8157
8239
  public once(event: 'error', listener: (error: Error) => void): this;
8158
- public once(event: 'track', listener: (track: unknown, userId: string, producerId: string) => void): this;
8240
+ public once(event: 'track', listener: (track: RemoteAudioTrack, userId: string, producerId: string) => void): this;
8241
+ public once(event: 'speaking', listener: (userId: string, speaking: boolean) => void): this;
8159
8242
  }
8160
8243
 
8161
8244
  export function joinVoiceChannel(options: VoiceConnectionOptions): VoiceConnection;
8162
8245
 
8246
+ export enum AudioPlayerStatus {
8247
+ AutoPaused = 'autopaused',
8248
+ Buffering = 'buffering',
8249
+ Idle = 'idle',
8250
+ Paused = 'paused',
8251
+ Playing = 'playing',
8252
+ }
8253
+
8254
+ export enum NoSubscriberBehavior {
8255
+ Pause = 'pause',
8256
+ Play = 'play',
8257
+ Stop = 'stop',
8258
+ }
8259
+
8260
+ /** `Raw` – PCM s16le 48 кГц стерео без заголовка: ffmpeg кодирует его в Opus, громкость меняют в ffmpeg. */
8261
+ export enum StreamType {
8262
+ Arbitrary = 'arbitrary',
8263
+ OggOpus = 'ogg/opus',
8264
+ Opus = 'opus',
8265
+ Raw = 'raw',
8266
+ WebmOpus = 'webm/opus',
8267
+ }
8268
+
8269
+ export interface AudioPlayerBehaviors {
8270
+ /** Что делать, когда некому слать звук (по умолчанию `Pause`). */
8271
+ noSubscriber: NoSubscriberBehavior;
8272
+ /** Сколько кадров подряд плеер может не получить из ресурса, прежде чем остановиться (по умолчанию 5). */
8273
+ maxMissedFrames: number;
8274
+ }
8275
+
8276
+ export interface CreateAudioPlayerOptions {
8277
+ /** `false` отключает событие `'debug'`. */
8278
+ debug?: boolean;
8279
+ behaviors?: Partial<AudioPlayerBehaviors>;
8280
+ }
8281
+
8282
+ export interface CreateAudioResourceOptions<T> {
8283
+ /** Для строки по умолчанию `Arbitrary`, для потока тип выводится по классу потока prism-media, иначе `Arbitrary`. */
8284
+ inputType?: StreamType;
8285
+ metadata?: T;
8286
+ /** Принимается для совместимости: хвост тишины у каждого канала свой, всегда 5 кадров. */
8287
+ silencePaddingFrames?: number;
8288
+ /** Битрейт кодирования в кбит/с, только для `Arbitrary` и `Raw` (по умолчанию 128). */
8289
+ bitrate?: number;
8290
+ /** Не поддержано: громкость меняют в ffmpeg (`-af volume=…`). `true` даёт `TypeError`. */
8291
+ inlineVolume?: false;
8292
+ }
8293
+
8294
+ export class AudioResource<T = unknown> {
8295
+ private constructor();
8296
+ public readonly edges: readonly never[];
8297
+ public readonly playStream: Readable;
8298
+ public readonly metadata: T;
8299
+ /** Всегда `undefined`: громкость меняют в ffmpeg. */
8300
+ public readonly volume?: undefined;
8301
+ /** Всегда `undefined`: звук в процессе не кодируется. */
8302
+ public readonly encoder?: undefined;
8303
+ public audioPlayer?: AudioPlayer;
8304
+ public readonly playbackDuration: number;
8305
+ public readonly started: boolean;
8306
+ public readonly silencePaddingFrames: number;
8307
+ public readonly readable: boolean;
8308
+ public readonly ended: boolean;
8309
+ }
8310
+
8311
+ export function createAudioResource<T>(
8312
+ input: string | Readable,
8313
+ options: CreateAudioResourceOptions<T> & Required<Pick<CreateAudioResourceOptions<T>, 'metadata'>>,
8314
+ ): AudioResource<T>;
8315
+ export function createAudioResource(
8316
+ input: string | Readable,
8317
+ options?: Omit<CreateAudioResourceOptions<null>, 'metadata'>,
8318
+ ): AudioResource<null>;
8319
+
8320
+ export interface AudioPlayerIdleState {
8321
+ status: AudioPlayerStatus.Idle;
8322
+ }
8323
+
8324
+ export interface AudioPlayerBufferingState {
8325
+ status: AudioPlayerStatus.Buffering;
8326
+ resource: AudioResource;
8327
+ }
8328
+
8329
+ export interface AudioPlayerPlayingState {
8330
+ status: AudioPlayerStatus.Playing;
8331
+ missedFrames: number;
8332
+ playbackDuration: number;
8333
+ resource: AudioResource;
8334
+ }
8335
+
8336
+ export interface AudioPlayerPausedState {
8337
+ status: AudioPlayerStatus.Paused | AudioPlayerStatus.AutoPaused;
8338
+ missedFrames: number;
8339
+ playbackDuration: number;
8340
+ resource: AudioResource;
8341
+ }
8342
+
8343
+ export type AudioPlayerState =
8344
+ | AudioPlayerBufferingState
8345
+ | AudioPlayerIdleState
8346
+ | AudioPlayerPausedState
8347
+ | AudioPlayerPlayingState;
8348
+
8349
+ export interface AudioPlayerStats {
8350
+ status: AudioPlayerStatus;
8351
+ subscribers: number;
8352
+ /** Подписки, которым плеер может слать звук прямо сейчас. */
8353
+ activeSubscribers: number;
8354
+ queuedFrames: number;
8355
+ framesPublished: number;
8356
+ underruns: number;
8357
+ resyncs: number;
8358
+ }
8359
+
8360
+ export class AudioPlayerError extends Error {
8361
+ public constructor(error: Error, resource: AudioResource);
8362
+ public resource: AudioResource;
8363
+ }
8364
+
8365
+ export class PlayerSubscription {
8366
+ private constructor();
8367
+ public readonly connection: VoiceConnection;
8368
+ public readonly player: AudioPlayer;
8369
+ /** Отписать только это соединение. */
8370
+ public unsubscribe(): void;
8371
+ }
8372
+
8373
+ export class AudioPlayer extends EventEmitter {
8374
+ public constructor(options?: CreateAudioPlayerOptions);
8375
+ public readonly behaviors: AudioPlayerBehaviors;
8376
+ public readonly debug: ((message: string) => void) | null;
8377
+ public readonly subscribers: PlayerSubscription[];
8378
+ /** Соединения, которым плеер может слать звук прямо сейчас. */
8379
+ public readonly playable: VoiceConnection[];
8380
+ public readonly state: AudioPlayerState;
8381
+ public play<T>(resource: AudioResource<T>): void;
8382
+ public pause(interpolateSilence?: boolean): boolean;
8383
+ public unpause(): boolean;
8384
+ public stop(force?: boolean): boolean;
8385
+ public checkPlayable(): boolean;
8386
+ public stats(): AudioPlayerStats;
8387
+ public on(event: 'error', listener: (error: AudioPlayerError) => void): this;
8388
+ public on(event: 'debug', listener: (message: string) => void): this;
8389
+ public on(event: 'stateChange', listener: (oldState: AudioPlayerState, newState: AudioPlayerState) => void): this;
8390
+ public on(event: 'subscribe' | 'unsubscribe', listener: (subscription: PlayerSubscription) => void): this;
8391
+ public on<T extends AudioPlayerStatus>(
8392
+ event: T,
8393
+ listener: (oldState: AudioPlayerState, newState: AudioPlayerState & { status: T }) => void,
8394
+ ): this;
8395
+ public once(event: 'error', listener: (error: AudioPlayerError) => void): this;
8396
+ public once(event: 'debug', listener: (message: string) => void): this;
8397
+ public once(event: 'stateChange', listener: (oldState: AudioPlayerState, newState: AudioPlayerState) => void): this;
8398
+ public once(event: 'subscribe' | 'unsubscribe', listener: (subscription: PlayerSubscription) => void): this;
8399
+ public once<T extends AudioPlayerStatus>(
8400
+ event: T,
8401
+ listener: (oldState: AudioPlayerState, newState: AudioPlayerState & { status: T }) => void,
8402
+ ): this;
8403
+ public off(event: 'error', listener: (error: AudioPlayerError) => void): this;
8404
+ public off(event: 'debug', listener: (message: string) => void): this;
8405
+ public off(event: 'stateChange', listener: (oldState: AudioPlayerState, newState: AudioPlayerState) => void): this;
8406
+ public off(event: 'subscribe' | 'unsubscribe', listener: (subscription: PlayerSubscription) => void): this;
8407
+ public off<T extends AudioPlayerStatus>(
8408
+ event: T,
8409
+ listener: (oldState: AudioPlayerState, newState: AudioPlayerState & { status: T }) => void,
8410
+ ): this;
8411
+ }
8412
+
8413
+ export function createAudioPlayer(options?: CreateAudioPlayerOptions): AudioPlayer;
8414
+
8163
8415
  //#endregion
8164
8416
 
8165
8417
  // Lolka