ruby implementation

This commit is contained in:
Beatrice Olivera
2018-09-03 16:24:43 +01:00
parent 598d59319e
commit 523125bea0
11 changed files with 243 additions and 4 deletions

View File

@@ -0,0 +1,55 @@
const AUDIO_DATA = "AUDIO_DATA";
export default class AudioData {
constructor(host, reciever, room) {
this.host = host;
this.reciever = reciever;
this.room = room;
this.decoder = new TextDecoder("ascii");
}
async intercept(stream) { // MediaStream
AudioContext = window.AudioContext || window.webkitAudioContext;
const ctx = new AudioContext();
const processor = ctx.createScriptProcessor(4096, 1, 1);
processor.connect(ctx.destination);
processor.onaudioprocess = e => this.handleBuffer(e);
ctx.createMediaStreamSource(stream).connect(processor);
ctx.resume();
}
broadcast(data) {
fetch("chat_room_sessions", {
method: "POST",
body: JSON.stringify({
type: AUDIO_DATA,
from: this.host,
to: this.reciever,
room: this.room,
audio: data.toString()
}),
headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content }
})
}
handleBuffer(e) {
const l = e.inputBuffer.getChannelData(0)
const l16 = convertF32ToInt16(l);
this.broadcast(this.decoder.decode(l16));
function convertF32ToInt16(buffer) {
let l = buffer.length;
let buf = new Int16Array(l / 3);
while (l--) {
if (l % 3 == 0) {
buf[l / 3] = buffer[l] * 0xFFFF;
}
}
return buf;
}
}
}

View File

@@ -1,3 +1,5 @@
import AudioData from './audio';
// Broadcast Types
const JOIN_ROOM = "JOIN_ROOM";
@@ -110,6 +112,8 @@ const createPC = (userId, isOffer) => {
let test = userId
pcPeers[userId] = pc;
pc.addStream(localstream);
const audio = new AudioData(currentUser, userId, chatroomId);
if (isOffer) {
pc
.createOffer()
@@ -146,6 +150,7 @@ const createPC = (userId, isOffer) => {
element.height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
remoteVideoContainer.appendChild(element);
localVideo.classList.add("video-sm");
audio.intercept(localstream);
};
pc.oniceconnectionstatechange = event => {