Skip to content

Commit

Permalink
use own mixer
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Nov 26, 2020
1 parent f3e7c13 commit 7eefe6e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 467 deletions.
56 changes: 56 additions & 0 deletions packages/core/src/domains/mcu/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export class Mixer {
inputs: { [id: string]: Input } = {};
pcm: { [id: string]: Buffer } = {};
onData: (buf: Buffer) => void = () => {};

constructor(private args: { bit: number }) {}

input() {
const input = new Input(this);
this.inputs[input.id] = input;

return input;
}

mixing() {
if (Object.keys(this.pcm).length === Object.keys(this.inputs).length) {
const inputs = Object.values(this.pcm);
const base = inputs.shift();
this.pcm = {};
const res = inputs.reduce(
(acc: number[], cur) => {
const next = acc.map((v, i) => this.mix(v, cur[i]));
return next;
},
[...base]
);
this.onData(Buffer.from(res));
}
}

private mix(a: number, b: number) {
const res = a + b;
const max = 1 << (this.args.bit - 1);
if (max < res) {
return max;
} else if (0 > res) {
return res;
}
return res;
}
}

export class Input {
id = Math.random().toString();
constructor(private mixer: Mixer) {}

write(buf: Buffer) {
this.mixer.pcm[this.id] = buf;
this.mixer.mixing();
}

remove() {
delete this.mixer.inputs[this.id];
delete this.mixer.pcm[this.id];
}
}
16 changes: 7 additions & 9 deletions packages/core/src/domains/mcu/mixer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { OpusEncoder } from "@discordjs/opus";
import { Input, Mixer } from "../../../../mixer";
import { performance } from "perf_hooks";
import { v4 } from "uuid";
import { RTCRtpTransceiver } from "../../../../werift";
Expand All @@ -11,15 +10,13 @@ import {
} from "../../../../werift/utils";
import { RtpHeader, RtpPacket } from "../../../../werift/vendor/rtp";
import { Media } from "../media/media";

const MAX_FRAME_SIZE = (48000 * 60) / 1000;
const PCM_LENGTH = MAX_FRAME_SIZE * 2 * 2;
import { Input, Mixer } from "./lib";

export class MCUMixer {
id = "mix_" + v4();

private encoder = new OpusEncoder(48000, 2);
private mixer = new Mixer({ sampleRate: 48000, channels: 2, sleep: 20 });
private mixer = new Mixer({ bit: 16 });
private sequenceNumber = random16();
private timestamp = random32();
private now = performance.now();
Expand All @@ -34,7 +31,7 @@ export class MCUMixer {
}

inputMedia(media: Media) {
const input = this.mixer.input({ channels: 2, maxBuffer: PCM_LENGTH / 2 });
const input = this.mixer.input();
const { unSubscribe } = media.tracks[0].track.onRtp.subscribe((packet) => {
if (Object.values(this.disposer)[0].id === media.mediaId) {
this.header = packet.header;
Expand All @@ -51,13 +48,14 @@ export class MCUMixer {

removeMedia = (mediaId: string) => {
const { stop, input } = this.disposer[mediaId];
this.mixer.removeInput(input);
input.remove();
delete this.disposer[mediaId];
stop();
};

listen() {
this.mixer.on("data", (data) => {
this.mixer.onData = (data) => {
if (!this.header) return;
const encoded = this.encoder.encode(data);

this.sequenceNumber = uint16Add(this.sequenceNumber, 1);
Expand All @@ -79,7 +77,7 @@ export class MCUMixer {
});
const rtp = new RtpPacket(header, encoded);
this.sender.sendRtp(rtp);
});
};
}

close() {
Expand Down
3 changes: 0 additions & 3 deletions packages/mixer/index.ts

This file was deleted.

221 changes: 0 additions & 221 deletions packages/mixer/input.ts

This file was deleted.

47 changes: 0 additions & 47 deletions packages/mixer/mixer-interleaved.ts

This file was deleted.

Loading

0 comments on commit 7eefe6e

Please sign in to comment.