Skip to content

Commit

Permalink
feat/ion-cluster (#181)
Browse files Browse the repository at this point in the history
* Add proto files.

* Add more interface.

* update *_pb.

* add IonConnector.

* update.
  • Loading branch information
cloudwebrtc authored Mar 27, 2021
1 parent fcd3724 commit b351608
Show file tree
Hide file tree
Showing 22 changed files with 4,795 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Client from './client';
import { LocalStream, RemoteStream, Constraints } from './stream';
import { Signal, Trickle } from './signal';
export { Client, LocalStream, RemoteStream, Constraints, Signal, Trickle };
export { Client, LocalStream, RemoteStream, Constraints, Signal, Trickle };
156 changes: 156 additions & 0 deletions src/ion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import Client, { Configuration } from './client';
import { LocalStream, RemoteStream, Constraints } from './stream';
import { BizClient } from './signal/biz';
import { IonSFUGRPCWebSignal } from './signal/grpc-web-impl';
import { Signal, Trickle } from './signal';
export { Client, LocalStream, RemoteStream, Constraints, Signal, Trickle };

export interface JoinResult {
success: boolean;
reason: string;
}

export enum PeerState {
NONE,
JOIN,
UPDATE,
LEAVE,
}

export interface Peer {
uid: string;
sid: string;
info: Map<string, any>;
}

export interface PeerEvent {
state: PeerState;
peer: Peer;
}

export enum StreamState {
NONE,
ADD,
REMOVE,
}

export interface StreamEvent {
uid: string;
state: StreamState;
streams: Stream[];
}

export interface Track {
id: string;
label: string;
kind: string;
simulcast: Map<string,string>;
}

export interface Stream {
id: string;
tracks: Track[];
}

export interface Message {
from: string ;
to: string;
data: Map<string, any>;
}

export class IonConnector {
private _biz: BizClient;
private _sfu: Client | undefined;
private _sid: string;
private _uid: string;

onerror?:(err: Error) => void;

onjoin?:(success: boolean, reason: string) => void;

onleave?:(reason: string) => void;

onpeerevent?: (ev: PeerEvent) => void;

onstreamevent?: (ev: StreamEvent) => void;

onmessage?:(msg: Message) => void;

ontrack?: (track: MediaStreamTrack, stream: RemoteStream) => void;

ondatachannel?: (ev: RTCDataChannelEvent) => void;

onspeaker?: (ev: string[]) => void;

constructor(url: string, config?: Configuration) {
this._sid = "";
this._uid = "";
this._sfu = undefined;
this._biz = new BizClient(url);

this._biz.on("join-reply", async (success: boolean, reason: string) => {
if (this.onjoin) {
this.onjoin(success, reason);
}
if (success && !this._sfu) {
const signal = new IonSFUGRPCWebSignal(url);
const sfu = new Client(signal, config);

sfu.ontrack = (track: MediaStreamTrack, stream: RemoteStream) =>
this.ontrack?.call(this, track, stream);
sfu.ondatachannel = (ev: RTCDataChannelEvent) =>
this.ondatachannel?.call(this, ev);
sfu.onspeaker = (ev: string[]) => this.onspeaker?.call(this, ev);

await sfu.join(this._sid, this._uid);

this._sfu = sfu;
}
});

this._biz.on("leave-reply", (reason: string) => {
if (this.onleave) {
this.onleave(reason);
}
});

this._biz.on("peer-event", (ev: PeerEvent) => {
if(this.onpeerevent) {
this.onpeerevent(ev);
}
})

this._biz.on("stream-event", (ev: StreamEvent) => {
if(this.onstreamevent) {
this.onstreamevent(ev);
}
})

this._biz.on("message", (msg: Message) => {
if(this.onmessage) {
this.onmessage(msg);
}
})
}

get sfu() { return this._sfu; }

async join(sid: string, uid: string, info: Map<string, any>, token: string | undefined): Promise<JoinResult> {
this._sid = sid;
this._uid = uid;
return this._biz.join(sid, uid, info, token);
}

async leave(uid: string): Promise<string> {
return this._biz.leave(uid)
}

async message(from: string, to: string, data: Map<string, any>): Promise<void> {
return this._biz.sendMessage(from, to, data);
}

close() {
this._sfu?.close();
this._biz.close();
}
}
2 changes: 1 addition & 1 deletion src/signal/_proto/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM mhart/alpine-node:12

RUN npm i -g ts-protoc-gen@0.10.0
RUN npm i -g ts-protoc-gen@0.14.0
RUN apk --no-cache add protobuf

WORKDIR /workspace
Expand Down
47 changes: 47 additions & 0 deletions src/signal/_proto/biz.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
syntax = "proto3";

import "ion.proto";

option go_package = "github.com/pion/ion/pkg/grpc/biz";

package biz;

service Biz {
rpc Signal(stream SignalRequest) returns (stream SignalReply);
}

message Join {
ion.Peer peer = 1;
string token = 2;
}

message JoinReply {
bool success = 1;
string reason = 2;
}

message Leave {
string uid = 1;
}

message LeaveReply {
string reason = 1;
}

message SignalRequest {
oneof payload {
Join join = 1;
Leave leave = 2;
ion.Message msg = 4;
}
}

message SignalReply {
oneof payload {
JoinReply joinReply = 1;
LeaveReply leaveReply = 2;
ion.PeerEvent peerEvent = 3;
ion.StreamEvent streamEvent = 4;
ion.Message msg = 5;
}
}
18 changes: 15 additions & 3 deletions src/signal/_proto/gen-proto-ts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

echo "Compiling proto file(s)..."

protoc /workspace/*.proto \
protoc /workspace/sfu.proto \
--proto_path=/workspace \
--plugin=protoc-gen-ts=/usr/bin/protoc-gen-ts \
--js_out=import_style=commonjs,binary:/workspace/library \
--ts_out=service=grpc-web:/workspace/library
--js_out=import_style=commonjs,binary:/workspace/library/sfu \
--ts_out=service=grpc-web:/workspace/library/sfu

protoc /workspace/ion.proto \
--proto_path=/workspace \
--plugin=protoc-gen-ts=/usr/bin/protoc-gen-ts \
--js_out=import_style=commonjs,binary:/workspace/library/biz \
--ts_out=service=grpc-web:/workspace/library/biz

protoc /workspace/biz.proto \
--proto_path=/workspace \
--plugin=protoc-gen-ts=/usr/bin/protoc-gen-ts \
--js_out=import_style=commonjs,binary:/workspace/library/biz \
--ts_out=service=grpc-web:/workspace/library/biz
90 changes: 90 additions & 0 deletions src/signal/_proto/ion.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
syntax = "proto3";

option go_package = "github.com/pion/ion/pkg/grpc/ion";

package ion;

message Empty {}

message Error {
int32 code = 1;
string reason = 2;
}

message Track {
string id = 1;
string label = 2;
string kind = 3;
map<string, string> simulcast = 4;
}

message Stream {
string id = 1;
repeated Track tracks = 2;
}

message Peer {
string sid = 1;
string uid = 2;
bytes info = 3;
}

// Describe the basic media info in the session of sfu.
// Write session state key/value to redis
// key = dc1/${nid}/${sid}/${uid}
// key => dc1/sfu-node-01/room1/peer1
// value => [
// ${msid}: [{id: ${trackId}, kind:audio}, {id: ${trackId}, kind:video, rid: 'f'}, {id: ${trackId}, kind:video, rid: 'h'},{id: ${trackId}, kind:video, rid: 'q'}]
// ${msid}: [{id: ${trackId}, kind:audio}, {id: ${trackId}, kind:video}]
// ]

message SessionEvent {
enum State {
ADD = 0;
REMOVE = 1;
}
State state = 2;
string nid = 3;
string sid = 4;
}

message StreamEvent {
enum State {
ADD = 0;
REMOVE = 1;
}
State state = 2;
string nid = 3;
string sid = 4;
string uid = 5;
repeated ion.Stream streams = 6;
}

message PeerEvent {
enum State {
JOIN = 0;
UPDATE = 1;
LEAVE = 2;
}
State state = 3;
ion.Peer peer = 4;
}

message Message {
string from = 1;
string to = 2;
bytes data = 3;
}

message RPC {
string protocol = 1;
string addr = 2;
map<string, string> params = 3;
}

message Node {
string dc = 1;
string nid = 2;
string service = 3;
RPC rpc = 4;
}
Loading

0 comments on commit b351608

Please sign in to comment.