Skip to content

Commit

Permalink
fix building
Browse files Browse the repository at this point in the history
  • Loading branch information
Mond1c committed Apr 9, 2024
1 parent c5ad9c7 commit 4b82da8
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 131 deletions.
15 changes: 11 additions & 4 deletions packages/frontend-sdk/lib/grabber_capture.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import {GrabberSocket} from "./sockets";
import {IOfferReceiveResponder, StreamType} from "./api";
import { TypedEmitter } from "tiny-typed-emitter";

export class GrabberCaptureClient { // FIXME inherit from the other client
private peerName: string;
public target: EventTarget; // FIXME: make this private and make proper on function
private socket: GrabberSocket<any>;
private offerReceiveResponder?: IOfferReceiveResponder;
private emitter: TypedEmitter;

constructor(peerName: string, signallingUrl?: string) {
this.emitter = new TypedEmitter();
this.peerName = peerName;
this.target = new EventTarget();
this.target.dispatchEvent(new CustomEvent('hi', {}));
this.emitter.emit("hi", {});
// this.target.dispatchEvent(new CustomEvent('hi', {}));

const socketPath = (signallingUrl ?? "") + "/ws/peers/" + peerName;

Expand All @@ -20,18 +24,21 @@ export class GrabberCaptureClient { // FIXME inherit from the other client
});

const init_peer_handle = ({initPeer: {pcConfig, pingInterval}}: any) => { // FIXME: remove once proper types are implemented
this.target.dispatchEvent(new CustomEvent('init_peer', {detail: {pcConfig, pingInterval}}));
this.emitter.emit("init_peer", {detail: {pcConfig, pingInterval}});
// this.target.dispatchEvent(new CustomEvent('init_peer', {detail: {pcConfig, pingInterval}}));
};
const offer_handle = async ({offer: {peerId, offer, streamType}}: any) => { // FIXME: remove once proper types are implemented
// if (this.offerReceiveResponder === undefined) {
// console.warn("Got an incomming call, even though onOfferReceived is not called.");
// return;
// }
// await this.offerReceiveResponder(peerId, offer, streamType);
this.target.dispatchEvent(new CustomEvent('offer', {detail: {playerId: peerId, offer, streamType}}));
this.emitter.emit("offer", {detail: {playerId: peerId, offer, streamType}});
// this.target.dispatchEvent(new CustomEvent('offer', {detail: {playerId: peerId, offer, streamType}}));
}
const player_ice_handle = async ({ice: {peerId, candidate}}: any) => { // FIXME: remove once proper types are implemented
this.target.dispatchEvent(new CustomEvent('player_ice', {detail: {peerId, candidate}}));
this.emitter.emit("player_ice", {detail: {peerId, candidate}});
// this.target.dispatchEvent(new CustomEvent('player_ice', {detail: {peerId, candidate}}));
}

this.socket.on("init_peer", init_peer_handle);
Expand Down
62 changes: 34 additions & 28 deletions packages/frontend-sdk/lib/grabber_player.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {GrabberSocket} from "./sockets";
import { TypedEmitter } from "tiny-typed-emitter"

type PeerInfo = any;
type GrabberPlayerClientEventTypes = "auth:request" | "auth:failed" | "initialized" | "peers"
Expand All @@ -7,51 +8,57 @@ export class GrabberPlayerClient {
private pc?: RTCPeerConnection;
private peerConnectionConfig?: RTCConfiguration
private _peersStatus?: any[]; // Why is this stored in Client?
private emitter: TypedEmitter;

get peersStatus() { // FIXME: remove once proper types are implemented
return this._peersStatus
}
private _participantsStatus?: any[]; // Why is this stored in Client?
get participantsStatus() {
return this._participantsStatus
}
private target: EventTarget;
// private target: EventTarget;
constructor(mode: "play" | "admin" = "admin", url?: string) {
this.target = new EventTarget();
// this.target = new EventTarget();
this.emitter = new TypedEmitter();

this.ws = new GrabberSocket((url ?? "") + "/ws/player/" + (mode === "play" ? "play" : "admin"));
this._setupWS();
}

_setupWS() {
const _client = this;
this.ws.on("auth:request", () => {
_client.target.dispatchEvent(new CustomEvent("auth:request", {}));
this.emitter.emit("auth:request", {});
// this.target.dispatchEvent(new CustomEvent("auth:request", {}));
});
_client.ws.on("auth:failed", () => {
_client.ws.close();
_client.target.dispatchEvent(new CustomEvent("auth:failed", {}));
this.ws.on("auth:failed", () => {
this.ws.close();
this.emitter.emit("auth:failed", {});
// this.target.dispatchEvent(new CustomEvent("auth:failed", {}));
});

_client.ws.on("init_peer", ({ initPeer: { pcConfig } }) => {
_client.peerConnectionConfig = pcConfig;
this.ws.on("init_peer", ({ initPeer: { pcConfig } }) => {
this.peerConnectionConfig = pcConfig;
console.debug("WebRTCGrabber: connection initialized");
_client.target.dispatchEvent(new CustomEvent("initialized", {}));
this.emitter.emit("initialized", {});
// this.target.dispatchEvent(new CustomEvent("initialized", {}));
});

_client.ws.on("peers", ({ peersStatus, participantsStatus }) => {
_client._peersStatus = peersStatus ?? [];
_client._participantsStatus = participantsStatus ?? [];
_client.target.dispatchEvent(new CustomEvent("peers", { detail: [ peersStatus, participantsStatus ] }));
this.ws.on("peers", ({ peersStatus, participantsStatus }) => {
this._peersStatus = peersStatus ?? [];
this._participantsStatus = participantsStatus ?? [];
this.emitter.emit("peers", { detail: [ peersStatus, participantsStatus ]});
// this.target.dispatchEvent(new CustomEvent("peers", { detail: [ peersStatus, participantsStatus ] }));
});

_client.ws.on("offer_answer", async ({ offerAnswer: { peerId, answer } }) => {
this.ws.on("offer_answer", async ({ offerAnswer: { peerId, answer } }) => {
console.debug(`WebRTCGrabber: got offer_answer from ${peerId}`);
await _client?.pc?.setRemoteDescription(answer);
await this?.pc?.setRemoteDescription(answer);
});

_client.ws.on("grabber_ice", async ({ ice: { peerId, candidate } }) => {
this.ws.on("grabber_ice", async ({ ice: { peerId, candidate } }) => {
console.debug(`WebRTCGrabber: got grabber_ice from ${peerId}`);
await _client?.pc?.addIceCandidate(candidate);
await this?.pc?.addIceCandidate(candidate);
});
}

Expand All @@ -64,9 +71,7 @@ export class GrabberPlayerClient {
}

connect(peerInfo: PeerInfo, streamType: any, onVideoTrack: (arg0: MediaStream, arg1: any, arg2: any) => void) {
const _client = this;

const pc = new RTCPeerConnection(_client.peerConnectionConfig);
const pc = new RTCPeerConnection(this.peerConnectionConfig);
pc.addTransceiver("video");
pc.addTransceiver("audio");

Expand All @@ -78,22 +83,23 @@ export class GrabberPlayerClient {
});

pc.addEventListener("icecandidate", (event) => {
console.debug(`WebRTCGrabber: sending ice to ${_client.formatPeerInfo(peerInfo)}`);
_client.ws.emit("player_ice", { ice: { ...peerInfo, candidate: event.candidate } });
console.debug(`WebRTCGrabber: sending ice to ${this.formatPeerInfo(peerInfo)}`);
this.ws.emit("player_ice", { ice: { ...peerInfo, candidate: event.candidate } });
});

_client.close();
_client.pc = pc;
this.close();
this.pc = pc;

pc.createOffer().then(offer => {
pc.setLocalDescription(offer);
_client.ws.emit("offer", { offer: { ...peerInfo, offer, streamType } });
console.debug(`WebRTCGrabber: sending offer to ${_client.formatPeerInfo(peerInfo)} ${streamType} ...`);
this.ws.emit("offer", { offer: { ...peerInfo, offer, streamType } });
console.debug(`WebRTCGrabber: sending offer to ${this.formatPeerInfo(peerInfo)} ${streamType} ...`);
});
}

on(eventName: GrabberPlayerClientEventTypes, callback: (arg0: any) => void) {
this.target.addEventListener(eventName, ({detail}: any) => callback(detail));
this.emitter.on(eventName, ({detail}: any) => callback(detail));
// this.target.addEventListener(eventName, ({detail}: any) => callback(detail));
}

close() {
Expand Down
16 changes: 13 additions & 3 deletions packages/frontend-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"target": ["node18", "es2020"],
"target": [
"node18",
"es2020"
],
"tsup": {
"entry": [
"index.ts"
],
"target": ["node18", "es2020"],
"target": [
"node18",
"es2020"
],
"splitting": false,
"sourcemap": true,
"clean": true,
"dts": true,
"format": ["cjs", "esm"]
"format": [
"cjs",
"esm"
]
},
"scripts": {
"build": "tsup",
Expand All @@ -37,6 +46,7 @@
},
"homepage": "https://github.com/irdkwmnsb/webrtc-grabber#readme",
"devDependencies": {
"tiny-typed-emitter": "^2.1.0",
"tsup": "^8.0.1"
}
}
9 changes: 9 additions & 0 deletions packages/frontend-sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"moduleResolution": "node",
"paths": {
"*": ["./node_modules/*"]
}
}
}

6 changes: 3 additions & 3 deletions packages/frontend/pages/capture/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const capture = async () => {
}
pingTimerId = setInterval((() => {
console.log("ping");
client.send_ping(pcs.size, Object.keys(streams));
client.sendPing(pcs.size, Object.keys(streams));
}) as TimerHandler, pingInterval);
console.log(`init peer (pingInterval = ${pingInterval})`);
updateState("active");
Expand Down Expand Up @@ -102,7 +102,7 @@ const capture = async () => {

pc.addEventListener("icecandidate", (event: any) => { // FIXME: remove once proper types are implemented
console.log(`send ice for player ${playerId}`);
client.send_grabber_ice(playerId, event.candidate);
client.sendGrabberICE(playerId, event.candidate);
})

pc.addEventListener('connectionstatechange', ({target: connection}: any) => { // FIXME: remove once proper types are implemented
Expand All @@ -118,7 +118,7 @@ const capture = async () => {
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);

client.send_offer_answer(playerId, answer);
client.sendOfferAnswer(playerId, answer);
console.log(`send offer_answer for ${playerId}`);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
lib: {
// Could also be a dictionary or array of multiple entry points
entry: {
sdk: resolve(__dirname, 'lib/main.ts'),
// sdk: resolve(__dirname, 'lib/main.ts'),
capture: resolve(__dirname, 'pages/capture/capture.html'),
index: resolve(__dirname, 'pages/index/index.html'),
player: resolve(__dirname, 'pages/player/player.html')
Expand Down
16 changes: 8 additions & 8 deletions packages/grabber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
},
"scripts": {
"start": "electron .",
"build_win32_x64": "pnpx electron-packager . --platform=win32 --arch=x64 --ignore=\"^(/build|/scripts)\" --out build",
"build_win32_arm64": "pnpx electron-packager . --platform=win32 --arch=arm64 --ignore=\"^(/build|/scripts)\" --out build",
"build_linux_x64": "pnpx electron-packager . --platform=linux --arch=x64 --ignore=\"^(/build|/scripts)\" --out build",
"build_linux_arm64": "pnpx electron-packager . --platform=linux --arch=arm64 --ignore=\"^(/build|/scripts)\" --out build",
"build_darwin_x64": "pnpx electron-packager . --platform=darwin --arch=x64 --ignore=\"^(/build|/scripts)\" --out build",
"build_darwin_arm64": "pnpx electron-packager . --platform=darwin --arch=arm64 --ignore=\"^(/build|/scripts)\" --out build",
"build_win32_x64": "pnpx @electron/packager . --platform=win32 --arch=x64 --no-prune --ignore=/node_modules --ignore=\"^(/build|/scripts)\" --out build",
"build_win32_arm64": "pnpx @electron/packager . --platform=win32 --arch=arm64 --no-prune --ignore=/node_modules --ignore=\"^(/build|/scripts)\" --out build",
"build_linux_x64": "pnpx @electron/packager . --platform=linux --arch=x64 --no-prune --ignore=/node_modules --ignore=\"^(/build|/scripts)\" --out build",
"build_linux_arm64": "pnpx @electron/packager . --platform=linux --arch=arm64 --no-prune --ignore=/node_modules --ignore=\"^(/build|/scripts)\" --out build",
"build_darwin_x64": "pnpx @electron/packager . --platform=darwin --arch=x64 --no-prune --ignore=/node_modules --ignore=\"^(/build|/scripts)\" --out build",
"build_darwin_arm64": "pnpx @electron/packager . --platform=darwin --arch=arm64 --no-prune --ignore=/node_modules --out build",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"electron": "^28.1.1",
"electron-packager": "^17.1.2",
"electron": "^29.1.0",
"@electron/packager": "^18.3.2",
"command-line-args": "^5.2.1",
"socket.io-client": "^4.5.2",
"webrtc": "^1.14.1",
Expand Down
Loading

0 comments on commit 4b82da8

Please sign in to comment.