UNPKG

dpg.broadcast-client

Version:
222 lines (221 loc) 9.34 kB
import Peer from "peerjs"; import io from "socket.io-client"; import { writable, get } from "svelte/store"; function InstanceVisitor(config) { const data = writable({ onAir: false, isOwner: false, status: "disable", connection: { socket: io(config.socket.host, { ...config.socket.config, autoConnect: false, query: { ...(config?.socket?.config?.query ?? {}), cid: config.socket.cid }, }), media: { camera: { active: false, id: "", disable: false }, audio: { active: false, id: "", disable: false } }, peer: { pids: {}, calls: {}, connections: { self: {}, visitors: {} }, streams: { self: new MediaStream, visitors: {} } } }, SetMediaConfig(_config) { updateData(_data => { _data.connection.media = _config; return _data; }); }, SetStream(_media) { updateData(_data => { _data.connection.peer.streams.self = _media; if (_data.onAir) { const { socket } = _data.connection; for (const socketID in _data.connection.peer.connections.self) { /** * Get Data */ const peer = _data.connection.peer.connections.self[socketID]; const pid = _data.connection.peer.pids[socketID]; if (!pid) continue; const _call = peer.call(pid, getData().connection.peer.streams.self); _call.on('stream', _stream => { updateData(_data => { _data.connection.peer.streams.visitors[socketID] = _stream; return _data; }); }); updateData(_data => { _data.connection.peer.calls[socketID] = _call; return _data; }); } } return _data; }); }, JoinBroadcast() { const { socket, media } = getData().connection; socket.open(); socket.emit('media/config', media); }, InitStore(name, val) { const store = writable(val); const { socket } = getData().connection; const { set, update, subscribe } = store; socket.on(`store/change/${name}`, _val => { set(_val); }); return { set, update, subscribe }; } }); const getData = () => get(data); const updateData = (callback) => data.update(callback); function SettingsSocket() { const { socket } = getData().connection; socket.on('conference/join', (_config) => { const peer = new Peer(config.peer); peer.on('open', _id => { updateData(_data => { _data.connection.peer.connections.visitors[_config.socketID] = peer; return _data; }); socket.emit('conference/call', { socketID: _config.socketID, sender: socket.id, pid: _id }); peer.on('call', _call => { updateData(_data => { _data.connection.peer.calls[_config.socketID] = _call; return _data; }); if (config.debug) console.log(`[Peer | Visitor] Call without recall`); _call.answer(getData().connection.peer.streams.self.getVideoTracks().length > 0 ? getData().connection.peer.streams.self : undefined); _call.on('stream', _media => { if (config.debug) console.log(`[Peer | Visitor] Get media without recall`, { _media }); updateData(_data => { _data.connection.peer.streams.visitors[_config.socketID] = _media; return _data; }); }); }); }); }); socket.on('conference/recall', (_config) => { const peer = getData().connection.peer.connections.visitors[_config.socketID]; if (config.debug) console.log(`[Peer | Visitor] Is try recall`, { peer }); if (!peer) return; const _call = peer.call(_config.pid, getData().connection.peer.streams.self); _call.on('stream', _stream => { if (config.debug) console.log(`[Peer | Visitor] Recall get stream`, { _stream }); updateData(_data => { _data.connection.peer.streams.visitors[_config.socketID] = _stream; _data.connection.peer.pids[_config.socketID] = _config.pid; return _data; }); }); updateData(_data => { _data.connection.peer.calls[_config.socketID] = _call; _data.connection.peer.pids[_config.socketID] = _config.pid; return _data; }); }); socket.on('conference/call', (_config) => { const { socket } = getData().connection; const peer = new Peer(config.peer); peer.on('open', _id => { updateData(_data => { _data.connection.peer.connections.self[_config.socketID] = peer; _data.connection.peer.pids[_config.socketID] = _config.pid; return _data; }); if (getData().connection.peer.streams.self.getVideoTracks().length === 0) { if (config.debug) console.log(`[Peer | Visitor] Call send to recall`); socket.emit('conference/recall', { pid: _id }); peer.on('call', _call => { updateData(_data => { _data.connection.peer.calls[_config.socketID] = _call; _data.connection.peer.pids[_config.socketID] = _config.pid; return _data; }); if (config.debug) console.log(`[Peer | Visitor] Recall get call`); _call.answer(); _call.on('stream', _media => { if (config.debug) console.log(`[Peer | Visitor] Recall get stream`, { _media }); updateData(_data => { _data.connection.peer.streams.visitors[_config.socketID] = _media; return _data; }); }); }); } else { if (config.debug) console.log(`[Peer | Visitor] Not need recall`); const _call = peer.call(_config.pid, getData().connection.peer.streams.self); _call.on('stream', _stream => { updateData(_data => { if (config.debug) console.log(`[Peer | Visitor] Call get stream`, { _stream }); _data.connection.peer.streams.visitors[_config.socketID] = _stream; return _data; }); }); updateData(_data => { _data.connection.peer.calls[_config.socketID] = _call; _data.connection.peer.pids[_config.socketID] = _config.pid; return _data; }); } }); }); socket.on('conference/data', (_config) => { updateData(_data => { _data.conference = _config; return _data; }); }); socket.on('disconnect', () => { updateData(_data => { _data.onAir = false; _data.status = "end"; return _data; }); }); } SettingsSocket(); return data; } export { InstanceVisitor };