webrtc-myip
Advanced tools
Comparing version 1.0.2 to 2.0.0
export { default } from './RTC'; | ||
export { STATE_EVENTS } from './RTC'; | ||
export { STREAM_EVENTS } from './RTC'; | ||
export { CLIENT_EVENTS } from './RTC'; |
export { default } from './RTC'; | ||
export { STATE_EVENTS } from './RTC'; | ||
export { STREAM_EVENTS } from './RTC'; | ||
export { CLIENT_EVENTS } from './RTC'; | ||
//# sourceMappingURL=index.js.map |
import { ICECandidateMessage, MediaType, NegotiationMessage } from "./RTCMessage"; | ||
import { SignalingDelegate } from "./SignalingChannel"; | ||
import * as EventEmitter from "eventemitter3"; | ||
export declare enum STATE_EVENTS { | ||
CONNECTED = "state-events/connected", | ||
DISCONNECTED = "state-events/disconnected", | ||
FAILED = "playback-events/failed", | ||
CLOSED = "playback-events/closed" | ||
} | ||
export declare enum CLIENT_EVENTS { | ||
COUNT_CHANGED = "client-events/count-changed" | ||
COUNT_CHANGED = "clientsCountChanged", | ||
NEW_CLIENT = "newClientConnected" | ||
} | ||
export declare enum STREAM_EVENTS { | ||
REMOTE_USER_MEDIA = "remoteUserMedia", | ||
REMOTE_DISPLAY = "remoteDisplay" | ||
} | ||
export declare type LogLevel = 'log' | 'trace'; | ||
declare class RTC implements SignalingDelegate { | ||
private config?; | ||
_debug: LogLevel; | ||
private sourceStream; | ||
@@ -20,5 +21,2 @@ private destStream; | ||
private readonly peerConnections; | ||
private streamDestination; | ||
private remoteDestination; | ||
private emitters; | ||
private eventEmitter; | ||
@@ -47,2 +45,3 @@ private connectionsCount; | ||
private createPeerConnection; | ||
private handleConnectionStateChangeEvent; | ||
private addTracksToPC; | ||
@@ -60,3 +59,2 @@ private removeTracksFromPC; | ||
handleOfferMsg({ mediaType, sdp, from }: NegotiationMessage): Promise<null>; | ||
private handleConnectionStateChangeEvent; | ||
/** | ||
@@ -84,3 +82,2 @@ * {@link SignalingDelegate} method to handle socket message of type {@link NegotiationMessage} | ||
private handleSourceTrack; | ||
private handleTrackDisconnected; | ||
/** | ||
@@ -109,3 +106,3 @@ * {@link SignalingDelegate} method to handle socket event 'other' (other user joined) | ||
*/ | ||
join(room: string, isStreamer: boolean): Promise<void>; | ||
join(room: string, isStreamer: boolean): Promise<EventEmitter<string | symbol>>; | ||
/** | ||
@@ -134,10 +131,9 @@ * Stops streams and disconnects from the socket server | ||
* const userMedia = document.getElementById("userMedia"); | ||
* rtc.setSourceVideo('userMedia', userMedia); | ||
* rtc.setupMedia('userMedia', userMedia); | ||
* ``` | ||
* @param type | ||
* @param videoElement | ||
* @param mediaConstraints - optional parameter. Defaults to `{ audio: true, video: true }` for user media (webcam) | ||
* and is *NOT* configurable for screen sharing as audio is not supported https://blog.mozilla.org/webrtc/getdisplaymedia-now-available-in-adapter-js/ | ||
*/ | ||
setSourceVideo(type: MediaType, videoElement: HTMLVideoElement, mediaConstraints?: MediaStreamConstraints): Promise<MediaStream>; | ||
setupMedia(type?: MediaType, mediaConstraints?: MediaStreamConstraints): Promise<MediaStream>; | ||
/** | ||
@@ -148,3 +144,3 @@ * Closes PeerConnection of the specified {@link MediaType} on Presenter's side. | ||
*/ | ||
removeConnectionType(type?: MediaType): void; | ||
private removeConnectionType; | ||
/** | ||
@@ -154,26 +150,4 @@ * Opens PeerConnection of the specified {@link MediaType} on Presenter's side. | ||
*/ | ||
addConnectionType(type?: MediaType): void; | ||
private addConnectionType; | ||
/** | ||
* Connects an HTMLVideoElement to an event emitter on *Viewer's* side. | ||
* Returns an event emitter to listen to events of {@link STATE_EVENTS} types | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* const userMedia = document.getElementById("userMedia"); | ||
* const webcam = this.rtc.connectDestinationVideo('userMedia', userMedia); | ||
* webcam.on(STATE_EVENTS.CONNECTED, () => { | ||
* console.log('webcam connected') | ||
* }); | ||
* ``` | ||
* | ||
* @param type | ||
* @param videoElement | ||
*/ | ||
connectDestinationVideo(type: MediaType, videoElement: HTMLVideoElement): EventEmitter; | ||
disconnectDestinationVideo(type?: MediaType): void; | ||
on(event: string, fn: any, context?: any): EventEmitter<string | symbol>; | ||
off(event: string, fn: any, context?: any, once?: boolean): EventEmitter<string | symbol>; | ||
destroy(): void; | ||
/** | ||
* {@link SignalingDelegate} method to handle socket event of type 'disconnect'. | ||
@@ -186,3 +160,6 @@ * @param userId | ||
private log; | ||
startScreenSharing(): Promise<MediaStream>; | ||
stopScreenSharing(): void; | ||
private handlePeerDisconnected; | ||
} | ||
export default RTC; |
@@ -44,13 +44,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
export var STATE_EVENTS; | ||
(function (STATE_EVENTS) { | ||
STATE_EVENTS["CONNECTED"] = "state-events/connected"; | ||
STATE_EVENTS["DISCONNECTED"] = "state-events/disconnected"; | ||
STATE_EVENTS["FAILED"] = "playback-events/failed"; | ||
STATE_EVENTS["CLOSED"] = "playback-events/closed"; | ||
})(STATE_EVENTS || (STATE_EVENTS = {})); | ||
export var CLIENT_EVENTS; | ||
(function (CLIENT_EVENTS) { | ||
CLIENT_EVENTS["COUNT_CHANGED"] = "client-events/count-changed"; | ||
CLIENT_EVENTS["COUNT_CHANGED"] = "clientsCountChanged"; | ||
CLIENT_EVENTS["NEW_CLIENT"] = "newClientConnected"; | ||
})(CLIENT_EVENTS || (CLIENT_EVENTS = {})); | ||
export var STREAM_EVENTS; | ||
(function (STREAM_EVENTS) { | ||
STREAM_EVENTS["REMOTE_USER_MEDIA"] = "remoteUserMedia"; | ||
STREAM_EVENTS["REMOTE_DISPLAY"] = "remoteDisplay"; | ||
})(STREAM_EVENTS || (STREAM_EVENTS = {})); | ||
var RTC = /** @class */ (function () { | ||
@@ -72,15 +71,16 @@ /** | ||
this.destStream = {}; | ||
this.streamDestination = {}; | ||
this.remoteDestination = {}; | ||
this.emitters = {}; | ||
this.peerConnections = { | ||
userMedia: {}, | ||
displayMedia: {} | ||
}; | ||
this.peerConnections = {}; | ||
this.signaling = new SignalingChannel(wsURL); | ||
this.signaling.delegate = this; | ||
this.signaling._debug = this.__debug; | ||
this.connectionsCount = 0; | ||
this.eventEmitter = new EventEmitter(); | ||
} | ||
Object.defineProperty(RTC.prototype, "_debug", { | ||
set: function (value) { | ||
this.__debug = value; | ||
this.signaling._debug = !!this.__debug; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
RTC.prototype.getMedia = function (constraints) { | ||
@@ -133,7 +133,7 @@ if (constraints === void 0) { constraints = { audio: true, video: true }; } | ||
_this.log(event.type, pc_1.connectionState, _this.id, event); | ||
_this.handleConnectionStateChangeEvent(pc_1.connectionState, mediaType); | ||
_this.handleConnectionStateChangeEvent(pc_1, mediaType); | ||
}; | ||
pc_1.onsignalingstatechange = function (event) { | ||
_this.log(event.type, pc_1.signalingState, _this.id, event); | ||
_this.handleConnectionStateChangeEvent(pc_1.signalingState, mediaType); | ||
_this.handleConnectionStateChangeEvent(pc_1, mediaType); | ||
}; | ||
@@ -162,2 +162,17 @@ pc_1.ondatachannel = function () { | ||
}; | ||
RTC.prototype.handleConnectionStateChangeEvent = function (pc, mediaType) { | ||
switch (pc.connectionState) { | ||
case 'connected': | ||
break; | ||
case 'failed': | ||
break; | ||
case 'closed': | ||
case 'disconnected': | ||
this.handlePeerDisconnected(pc, mediaType); | ||
break; | ||
default: | ||
break; | ||
} | ||
}; | ||
; | ||
RTC.prototype.addTracksToPC = function (peerConnection, stream) { | ||
@@ -179,4 +194,5 @@ if (!stream) { | ||
RTC.prototype.getOrCreatePeerConnection = function (mediaType, userId) { | ||
var pc = this.peerConnections[mediaType][userId] || this.createPeerConnection(mediaType); | ||
this.peerConnections[mediaType][userId] = pc; | ||
var peerConnection = this.peerConnections[mediaType] || (this.peerConnections[mediaType] = {}); | ||
var pc = peerConnection[userId] || this.createPeerConnection(mediaType); | ||
peerConnection[userId] = pc; | ||
return pc; | ||
@@ -289,26 +305,2 @@ }; | ||
}; | ||
RTC.prototype.handleConnectionStateChangeEvent = function (state, mediaType) { | ||
var stateEvent; | ||
switch (state) { | ||
case 'connected': | ||
stateEvent = STATE_EVENTS.CONNECTED; | ||
break; | ||
case 'disconnected': | ||
stateEvent = STATE_EVENTS.DISCONNECTED; | ||
break; | ||
case 'failed': | ||
stateEvent = STATE_EVENTS.FAILED; | ||
break; | ||
case 'closed': | ||
stateEvent = STATE_EVENTS.CLOSED; | ||
break; | ||
default: | ||
break; | ||
} | ||
this.eventEmitter.emit(stateEvent); | ||
if (this.emitters[mediaType]) { | ||
this.emitters[mediaType].emit(stateEvent); | ||
} | ||
}; | ||
; | ||
/** | ||
@@ -374,18 +366,6 @@ * {@link SignalingDelegate} method to handle socket message of type {@link NegotiationMessage} | ||
this.destStream[mediaType] = stream; | ||
var video = this.remoteDestination[mediaType]; | ||
if (video) { | ||
video.srcObject = stream; | ||
} | ||
else { | ||
console.warn("No video element for " + mediaType); | ||
} | ||
stream.addTrack(track); | ||
var eventType = (mediaType == 'userMedia') ? STREAM_EVENTS.REMOTE_USER_MEDIA : STREAM_EVENTS.REMOTE_DISPLAY; | ||
this.eventEmitter.emit(eventType, stream); | ||
}; | ||
RTC.prototype.handleTrackDisconnected = function (mediaType) { | ||
this.destStream[mediaType] = null; | ||
var video = this.remoteDestination[mediaType]; | ||
if (video) { | ||
video.srcObject = null; | ||
} | ||
}; | ||
/** | ||
@@ -404,3 +384,3 @@ * {@link SignalingDelegate} method to handle socket event 'other' (other user joined) | ||
this.eventEmitter.emit(CLIENT_EVENTS.COUNT_CHANGED, this.connectionsCount); | ||
_i = 0, _a = Object.keys(this.streamDestination); | ||
_i = 0, _a = Object.keys(this.sourceStream); | ||
_b.label = 1; | ||
@@ -448,2 +428,3 @@ case 1: | ||
case 0: | ||
this.log('join', room, isStreamer); | ||
this.__isStreamer = isStreamer; | ||
@@ -459,3 +440,3 @@ return [4 /*yield*/, this.signaling.setupSocket()]; | ||
this.signaling.sendMessage(msg); | ||
return [2 /*return*/]; | ||
return [2 /*return*/, this.eventEmitter]; | ||
} | ||
@@ -512,10 +493,9 @@ }); | ||
* const userMedia = document.getElementById("userMedia"); | ||
* rtc.setSourceVideo('userMedia', userMedia); | ||
* rtc.setupMedia('userMedia', userMedia); | ||
* ``` | ||
* @param type | ||
* @param videoElement | ||
* @param mediaConstraints - optional parameter. Defaults to `{ audio: true, video: true }` for user media (webcam) | ||
* and is *NOT* configurable for screen sharing as audio is not supported https://blog.mozilla.org/webrtc/getdisplaymedia-now-available-in-adapter-js/ | ||
*/ | ||
RTC.prototype.setSourceVideo = function (type, videoElement, mediaConstraints) { | ||
RTC.prototype.setupMedia = function (type, mediaConstraints) { | ||
if (type === void 0) { type = 'userMedia'; } | ||
@@ -539,7 +519,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
stream = _a; | ||
if (videoElement) { | ||
this.streamDestination[type] = videoElement; | ||
this.streamDestination[type]['srcObject'] = stream; | ||
this.sourceStream[type] = stream; | ||
} | ||
this.sourceStream[type] = stream; | ||
return [2 /*return*/, stream]; | ||
@@ -564,5 +540,3 @@ case 5: | ||
stream && this.stopTracks(stream.getTracks()); | ||
//remove stream from a video element | ||
this.streamDestination[type]['srcObject'] = null; | ||
delete this.streamDestination[type]; | ||
delete this.sourceStream[type]; | ||
//close all connections of the type | ||
@@ -583,48 +557,9 @@ for (var _i = 0, _a = Object.keys(this.peerConnections[type]); _i < _a.length; _i++) { | ||
var existingType = type == 'userMedia' ? 'displayMedia' : 'userMedia'; | ||
//invite all viewers of the existing media to a stream of a new type | ||
for (var _i = 0, _a = Object.keys(this.peerConnections[existingType]); _i < _a.length; _i++) { | ||
var pcId = _a[_i]; | ||
this.createOffer(type, pcId).catch(console.error); | ||
var peerId = _a[_i]; | ||
this.createOffer(type, peerId).catch(console.error); | ||
} | ||
}; | ||
/** | ||
* Connects an HTMLVideoElement to an event emitter on *Viewer's* side. | ||
* Returns an event emitter to listen to events of {@link STATE_EVENTS} types | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* const userMedia = document.getElementById("userMedia"); | ||
* const webcam = this.rtc.connectDestinationVideo('userMedia', userMedia); | ||
* webcam.on(STATE_EVENTS.CONNECTED, () => { | ||
* console.log('webcam connected') | ||
* }); | ||
* ``` | ||
* | ||
* @param type | ||
* @param videoElement | ||
*/ | ||
RTC.prototype.connectDestinationVideo = function (type, videoElement) { | ||
if (type === void 0) { type = 'userMedia'; } | ||
this.remoteDestination[type] = videoElement; | ||
var emitter = new EventEmitter(); | ||
this.emitters[type] = emitter; | ||
return emitter; | ||
}; | ||
RTC.prototype.disconnectDestinationVideo = function (type) { | ||
if (type === void 0) { type = 'userMedia'; } | ||
this.remoteDestination[type]['srcObject'] = null; | ||
delete this.remoteDestination[type]; | ||
var emitter = this.emitters[type]; | ||
emitter.removeAllListeners(); | ||
}; | ||
RTC.prototype.on = function (event, fn, context) { | ||
return this.eventEmitter.on(event, fn, context); | ||
}; | ||
RTC.prototype.off = function (event, fn, context, once) { | ||
return this.eventEmitter.off(event, fn, context, once); | ||
}; | ||
RTC.prototype.destroy = function () { | ||
this.eventEmitter.removeAllListeners(); | ||
}; | ||
/** | ||
* {@link SignalingDelegate} method to handle socket event of type 'disconnect'. | ||
@@ -648,15 +583,14 @@ * @param userId | ||
var type = _a[_i]; | ||
var pc = this.peerConnections[type][userId]; | ||
var peerConnection = this.peerConnections[type]; | ||
var pc = peerConnection[userId]; | ||
pc && pc.close(); | ||
delete this.peerConnections[type][userId]; | ||
delete peerConnection[userId]; | ||
} | ||
}; | ||
RTC.prototype.closeAllConnections = function () { | ||
for (var _i = 0, _a = Object.keys(this.peerConnections); _i < _a.length; _i++) { | ||
var type = _a[_i]; | ||
for (var _b = 0, _c = Object.values(this.peerConnections[type]); _b < _c.length; _b++) { | ||
var pc = _c[_b]; | ||
pc.close(); | ||
} | ||
this.peerConnections[type] = {}; | ||
//reduce peer connections map to peer connections array | ||
var allPC = Object.values(this.peerConnections).reduce(function (arr, obj) { return arr.concat(Object.values(obj)); }, []); | ||
for (var _i = 0, allPC_1 = allPC; _i < allPC_1.length; _i++) { | ||
var pc = allPC_1[_i]; | ||
pc.close(); | ||
} | ||
@@ -672,3 +606,37 @@ }; | ||
} | ||
if (this.__debug == 'trace') { | ||
console.groupCollapsed('trace'); | ||
console.trace(); | ||
console.groupEnd(); | ||
} | ||
}; | ||
RTC.prototype.startScreenSharing = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var media; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.setupMedia('displayMedia')]; | ||
case 1: | ||
media = _a.sent(); | ||
this.addConnectionType('displayMedia'); | ||
return [2 /*return*/, media]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RTC.prototype.stopScreenSharing = function () { | ||
this.removeConnectionType('displayMedia'); | ||
}; | ||
RTC.prototype.handlePeerDisconnected = function (pc, mediaType) { | ||
this.destStream[mediaType] = null; | ||
for (var _i = 0, _a = Object.keys(this.peerConnections[mediaType]); _i < _a.length; _i++) { | ||
var peerId = _a[_i]; | ||
if (this.peerConnections[mediaType][peerId] == pc) { | ||
this.log("delete " + mediaType + " connection with " + peerId); | ||
this.peerConnections[mediaType][peerId] = null; | ||
delete this.peerConnections[mediaType][peerId]; | ||
} | ||
} | ||
this.eventEmitter.emit((mediaType == 'userMedia') ? STREAM_EVENTS.REMOTE_USER_MEDIA : STREAM_EVENTS.REMOTE_DISPLAY, null); | ||
}; | ||
return RTC; | ||
@@ -675,0 +643,0 @@ }()); |
@@ -20,2 +20,3 @@ import * as io from 'socket.io-client'; | ||
socket.on('connect', function () { | ||
_this.log('socket connected'); | ||
resolve(); | ||
@@ -22,0 +23,0 @@ }); |
export { default } from './RTC'; | ||
export { STATE_EVENTS } from './RTC'; | ||
export { STREAM_EVENTS } from './RTC'; | ||
export { CLIENT_EVENTS } from './RTC'; |
@@ -6,5 +6,5 @@ "use strict"; | ||
var RTC_2 = require("./RTC"); | ||
exports.STATE_EVENTS = RTC_2.STATE_EVENTS; | ||
exports.STREAM_EVENTS = RTC_2.STREAM_EVENTS; | ||
var RTC_3 = require("./RTC"); | ||
exports.CLIENT_EVENTS = RTC_3.CLIENT_EVENTS; | ||
//# sourceMappingURL=index.js.map |
import { ICECandidateMessage, MediaType, NegotiationMessage } from "./RTCMessage"; | ||
import { SignalingDelegate } from "./SignalingChannel"; | ||
import * as EventEmitter from "eventemitter3"; | ||
export declare enum STATE_EVENTS { | ||
CONNECTED = "state-events/connected", | ||
DISCONNECTED = "state-events/disconnected", | ||
FAILED = "playback-events/failed", | ||
CLOSED = "playback-events/closed" | ||
} | ||
export declare enum CLIENT_EVENTS { | ||
COUNT_CHANGED = "client-events/count-changed" | ||
COUNT_CHANGED = "clientsCountChanged", | ||
NEW_CLIENT = "newClientConnected" | ||
} | ||
export declare enum STREAM_EVENTS { | ||
REMOTE_USER_MEDIA = "remoteUserMedia", | ||
REMOTE_DISPLAY = "remoteDisplay" | ||
} | ||
export declare type LogLevel = 'log' | 'trace'; | ||
declare class RTC implements SignalingDelegate { | ||
private config?; | ||
_debug: LogLevel; | ||
private sourceStream; | ||
@@ -20,5 +21,2 @@ private destStream; | ||
private readonly peerConnections; | ||
private streamDestination; | ||
private remoteDestination; | ||
private emitters; | ||
private eventEmitter; | ||
@@ -47,2 +45,3 @@ private connectionsCount; | ||
private createPeerConnection; | ||
private handleConnectionStateChangeEvent; | ||
private addTracksToPC; | ||
@@ -60,3 +59,2 @@ private removeTracksFromPC; | ||
handleOfferMsg({ mediaType, sdp, from }: NegotiationMessage): Promise<null>; | ||
private handleConnectionStateChangeEvent; | ||
/** | ||
@@ -84,3 +82,2 @@ * {@link SignalingDelegate} method to handle socket message of type {@link NegotiationMessage} | ||
private handleSourceTrack; | ||
private handleTrackDisconnected; | ||
/** | ||
@@ -109,3 +106,3 @@ * {@link SignalingDelegate} method to handle socket event 'other' (other user joined) | ||
*/ | ||
join(room: string, isStreamer: boolean): Promise<void>; | ||
join(room: string, isStreamer: boolean): Promise<EventEmitter<string | symbol>>; | ||
/** | ||
@@ -134,10 +131,9 @@ * Stops streams and disconnects from the socket server | ||
* const userMedia = document.getElementById("userMedia"); | ||
* rtc.setSourceVideo('userMedia', userMedia); | ||
* rtc.setupMedia('userMedia', userMedia); | ||
* ``` | ||
* @param type | ||
* @param videoElement | ||
* @param mediaConstraints - optional parameter. Defaults to `{ audio: true, video: true }` for user media (webcam) | ||
* and is *NOT* configurable for screen sharing as audio is not supported https://blog.mozilla.org/webrtc/getdisplaymedia-now-available-in-adapter-js/ | ||
*/ | ||
setSourceVideo(type: MediaType, videoElement: HTMLVideoElement, mediaConstraints?: MediaStreamConstraints): Promise<MediaStream>; | ||
setupMedia(type?: MediaType, mediaConstraints?: MediaStreamConstraints): Promise<MediaStream>; | ||
/** | ||
@@ -148,3 +144,3 @@ * Closes PeerConnection of the specified {@link MediaType} on Presenter's side. | ||
*/ | ||
removeConnectionType(type?: MediaType): void; | ||
private removeConnectionType; | ||
/** | ||
@@ -154,26 +150,4 @@ * Opens PeerConnection of the specified {@link MediaType} on Presenter's side. | ||
*/ | ||
addConnectionType(type?: MediaType): void; | ||
private addConnectionType; | ||
/** | ||
* Connects an HTMLVideoElement to an event emitter on *Viewer's* side. | ||
* Returns an event emitter to listen to events of {@link STATE_EVENTS} types | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* const userMedia = document.getElementById("userMedia"); | ||
* const webcam = this.rtc.connectDestinationVideo('userMedia', userMedia); | ||
* webcam.on(STATE_EVENTS.CONNECTED, () => { | ||
* console.log('webcam connected') | ||
* }); | ||
* ``` | ||
* | ||
* @param type | ||
* @param videoElement | ||
*/ | ||
connectDestinationVideo(type: MediaType, videoElement: HTMLVideoElement): EventEmitter; | ||
disconnectDestinationVideo(type?: MediaType): void; | ||
on(event: string, fn: any, context?: any): EventEmitter<string | symbol>; | ||
off(event: string, fn: any, context?: any, once?: boolean): EventEmitter<string | symbol>; | ||
destroy(): void; | ||
/** | ||
* {@link SignalingDelegate} method to handle socket event of type 'disconnect'. | ||
@@ -186,3 +160,6 @@ * @param userId | ||
private log; | ||
startScreenSharing(): Promise<MediaStream>; | ||
stopScreenSharing(): void; | ||
private handlePeerDisconnected; | ||
} | ||
export default RTC; |
212
lib/RTC.js
@@ -46,13 +46,12 @@ "use strict"; | ||
} | ||
var STATE_EVENTS; | ||
(function (STATE_EVENTS) { | ||
STATE_EVENTS["CONNECTED"] = "state-events/connected"; | ||
STATE_EVENTS["DISCONNECTED"] = "state-events/disconnected"; | ||
STATE_EVENTS["FAILED"] = "playback-events/failed"; | ||
STATE_EVENTS["CLOSED"] = "playback-events/closed"; | ||
})(STATE_EVENTS = exports.STATE_EVENTS || (exports.STATE_EVENTS = {})); | ||
var CLIENT_EVENTS; | ||
(function (CLIENT_EVENTS) { | ||
CLIENT_EVENTS["COUNT_CHANGED"] = "client-events/count-changed"; | ||
CLIENT_EVENTS["COUNT_CHANGED"] = "clientsCountChanged"; | ||
CLIENT_EVENTS["NEW_CLIENT"] = "newClientConnected"; | ||
})(CLIENT_EVENTS = exports.CLIENT_EVENTS || (exports.CLIENT_EVENTS = {})); | ||
var STREAM_EVENTS; | ||
(function (STREAM_EVENTS) { | ||
STREAM_EVENTS["REMOTE_USER_MEDIA"] = "remoteUserMedia"; | ||
STREAM_EVENTS["REMOTE_DISPLAY"] = "remoteDisplay"; | ||
})(STREAM_EVENTS = exports.STREAM_EVENTS || (exports.STREAM_EVENTS = {})); | ||
var RTC = /** @class */ (function () { | ||
@@ -74,15 +73,16 @@ /** | ||
this.destStream = {}; | ||
this.streamDestination = {}; | ||
this.remoteDestination = {}; | ||
this.emitters = {}; | ||
this.peerConnections = { | ||
userMedia: {}, | ||
displayMedia: {} | ||
}; | ||
this.peerConnections = {}; | ||
this.signaling = new SignalingChannel_1.default(wsURL); | ||
this.signaling.delegate = this; | ||
this.signaling._debug = this.__debug; | ||
this.connectionsCount = 0; | ||
this.eventEmitter = new EventEmitter(); | ||
} | ||
Object.defineProperty(RTC.prototype, "_debug", { | ||
set: function (value) { | ||
this.__debug = value; | ||
this.signaling._debug = !!this.__debug; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
RTC.prototype.getMedia = function (constraints) { | ||
@@ -135,7 +135,7 @@ if (constraints === void 0) { constraints = { audio: true, video: true }; } | ||
_this.log(event.type, pc_1.connectionState, _this.id, event); | ||
_this.handleConnectionStateChangeEvent(pc_1.connectionState, mediaType); | ||
_this.handleConnectionStateChangeEvent(pc_1, mediaType); | ||
}; | ||
pc_1.onsignalingstatechange = function (event) { | ||
_this.log(event.type, pc_1.signalingState, _this.id, event); | ||
_this.handleConnectionStateChangeEvent(pc_1.signalingState, mediaType); | ||
_this.handleConnectionStateChangeEvent(pc_1, mediaType); | ||
}; | ||
@@ -164,2 +164,17 @@ pc_1.ondatachannel = function () { | ||
}; | ||
RTC.prototype.handleConnectionStateChangeEvent = function (pc, mediaType) { | ||
switch (pc.connectionState) { | ||
case 'connected': | ||
break; | ||
case 'failed': | ||
break; | ||
case 'closed': | ||
case 'disconnected': | ||
this.handlePeerDisconnected(pc, mediaType); | ||
break; | ||
default: | ||
break; | ||
} | ||
}; | ||
; | ||
RTC.prototype.addTracksToPC = function (peerConnection, stream) { | ||
@@ -181,4 +196,5 @@ if (!stream) { | ||
RTC.prototype.getOrCreatePeerConnection = function (mediaType, userId) { | ||
var pc = this.peerConnections[mediaType][userId] || this.createPeerConnection(mediaType); | ||
this.peerConnections[mediaType][userId] = pc; | ||
var peerConnection = this.peerConnections[mediaType] || (this.peerConnections[mediaType] = {}); | ||
var pc = peerConnection[userId] || this.createPeerConnection(mediaType); | ||
peerConnection[userId] = pc; | ||
return pc; | ||
@@ -291,26 +307,2 @@ }; | ||
}; | ||
RTC.prototype.handleConnectionStateChangeEvent = function (state, mediaType) { | ||
var stateEvent; | ||
switch (state) { | ||
case 'connected': | ||
stateEvent = STATE_EVENTS.CONNECTED; | ||
break; | ||
case 'disconnected': | ||
stateEvent = STATE_EVENTS.DISCONNECTED; | ||
break; | ||
case 'failed': | ||
stateEvent = STATE_EVENTS.FAILED; | ||
break; | ||
case 'closed': | ||
stateEvent = STATE_EVENTS.CLOSED; | ||
break; | ||
default: | ||
break; | ||
} | ||
this.eventEmitter.emit(stateEvent); | ||
if (this.emitters[mediaType]) { | ||
this.emitters[mediaType].emit(stateEvent); | ||
} | ||
}; | ||
; | ||
/** | ||
@@ -376,18 +368,6 @@ * {@link SignalingDelegate} method to handle socket message of type {@link NegotiationMessage} | ||
this.destStream[mediaType] = stream; | ||
var video = this.remoteDestination[mediaType]; | ||
if (video) { | ||
video.srcObject = stream; | ||
} | ||
else { | ||
console.warn("No video element for " + mediaType); | ||
} | ||
stream.addTrack(track); | ||
var eventType = (mediaType == 'userMedia') ? STREAM_EVENTS.REMOTE_USER_MEDIA : STREAM_EVENTS.REMOTE_DISPLAY; | ||
this.eventEmitter.emit(eventType, stream); | ||
}; | ||
RTC.prototype.handleTrackDisconnected = function (mediaType) { | ||
this.destStream[mediaType] = null; | ||
var video = this.remoteDestination[mediaType]; | ||
if (video) { | ||
video.srcObject = null; | ||
} | ||
}; | ||
/** | ||
@@ -406,3 +386,3 @@ * {@link SignalingDelegate} method to handle socket event 'other' (other user joined) | ||
this.eventEmitter.emit(CLIENT_EVENTS.COUNT_CHANGED, this.connectionsCount); | ||
_i = 0, _a = Object.keys(this.streamDestination); | ||
_i = 0, _a = Object.keys(this.sourceStream); | ||
_b.label = 1; | ||
@@ -450,2 +430,3 @@ case 1: | ||
case 0: | ||
this.log('join', room, isStreamer); | ||
this.__isStreamer = isStreamer; | ||
@@ -461,3 +442,3 @@ return [4 /*yield*/, this.signaling.setupSocket()]; | ||
this.signaling.sendMessage(msg); | ||
return [2 /*return*/]; | ||
return [2 /*return*/, this.eventEmitter]; | ||
} | ||
@@ -514,10 +495,9 @@ }); | ||
* const userMedia = document.getElementById("userMedia"); | ||
* rtc.setSourceVideo('userMedia', userMedia); | ||
* rtc.setupMedia('userMedia', userMedia); | ||
* ``` | ||
* @param type | ||
* @param videoElement | ||
* @param mediaConstraints - optional parameter. Defaults to `{ audio: true, video: true }` for user media (webcam) | ||
* and is *NOT* configurable for screen sharing as audio is not supported https://blog.mozilla.org/webrtc/getdisplaymedia-now-available-in-adapter-js/ | ||
*/ | ||
RTC.prototype.setSourceVideo = function (type, videoElement, mediaConstraints) { | ||
RTC.prototype.setupMedia = function (type, mediaConstraints) { | ||
if (type === void 0) { type = 'userMedia'; } | ||
@@ -541,7 +521,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
stream = _a; | ||
if (videoElement) { | ||
this.streamDestination[type] = videoElement; | ||
this.streamDestination[type]['srcObject'] = stream; | ||
this.sourceStream[type] = stream; | ||
} | ||
this.sourceStream[type] = stream; | ||
return [2 /*return*/, stream]; | ||
@@ -566,5 +542,3 @@ case 5: | ||
stream && this.stopTracks(stream.getTracks()); | ||
//remove stream from a video element | ||
this.streamDestination[type]['srcObject'] = null; | ||
delete this.streamDestination[type]; | ||
delete this.sourceStream[type]; | ||
//close all connections of the type | ||
@@ -585,48 +559,9 @@ for (var _i = 0, _a = Object.keys(this.peerConnections[type]); _i < _a.length; _i++) { | ||
var existingType = type == 'userMedia' ? 'displayMedia' : 'userMedia'; | ||
//invite all viewers of the existing media to a stream of a new type | ||
for (var _i = 0, _a = Object.keys(this.peerConnections[existingType]); _i < _a.length; _i++) { | ||
var pcId = _a[_i]; | ||
this.createOffer(type, pcId).catch(console.error); | ||
var peerId = _a[_i]; | ||
this.createOffer(type, peerId).catch(console.error); | ||
} | ||
}; | ||
/** | ||
* Connects an HTMLVideoElement to an event emitter on *Viewer's* side. | ||
* Returns an event emitter to listen to events of {@link STATE_EVENTS} types | ||
* | ||
* Example: | ||
* | ||
* ``` | ||
* const userMedia = document.getElementById("userMedia"); | ||
* const webcam = this.rtc.connectDestinationVideo('userMedia', userMedia); | ||
* webcam.on(STATE_EVENTS.CONNECTED, () => { | ||
* console.log('webcam connected') | ||
* }); | ||
* ``` | ||
* | ||
* @param type | ||
* @param videoElement | ||
*/ | ||
RTC.prototype.connectDestinationVideo = function (type, videoElement) { | ||
if (type === void 0) { type = 'userMedia'; } | ||
this.remoteDestination[type] = videoElement; | ||
var emitter = new EventEmitter(); | ||
this.emitters[type] = emitter; | ||
return emitter; | ||
}; | ||
RTC.prototype.disconnectDestinationVideo = function (type) { | ||
if (type === void 0) { type = 'userMedia'; } | ||
this.remoteDestination[type]['srcObject'] = null; | ||
delete this.remoteDestination[type]; | ||
var emitter = this.emitters[type]; | ||
emitter.removeAllListeners(); | ||
}; | ||
RTC.prototype.on = function (event, fn, context) { | ||
return this.eventEmitter.on(event, fn, context); | ||
}; | ||
RTC.prototype.off = function (event, fn, context, once) { | ||
return this.eventEmitter.off(event, fn, context, once); | ||
}; | ||
RTC.prototype.destroy = function () { | ||
this.eventEmitter.removeAllListeners(); | ||
}; | ||
/** | ||
* {@link SignalingDelegate} method to handle socket event of type 'disconnect'. | ||
@@ -650,15 +585,14 @@ * @param userId | ||
var type = _a[_i]; | ||
var pc = this.peerConnections[type][userId]; | ||
var peerConnection = this.peerConnections[type]; | ||
var pc = peerConnection[userId]; | ||
pc && pc.close(); | ||
delete this.peerConnections[type][userId]; | ||
delete peerConnection[userId]; | ||
} | ||
}; | ||
RTC.prototype.closeAllConnections = function () { | ||
for (var _i = 0, _a = Object.keys(this.peerConnections); _i < _a.length; _i++) { | ||
var type = _a[_i]; | ||
for (var _b = 0, _c = Object.values(this.peerConnections[type]); _b < _c.length; _b++) { | ||
var pc = _c[_b]; | ||
pc.close(); | ||
} | ||
this.peerConnections[type] = {}; | ||
//reduce peer connections map to peer connections array | ||
var allPC = Object.values(this.peerConnections).reduce(function (arr, obj) { return arr.concat(Object.values(obj)); }, []); | ||
for (var _i = 0, allPC_1 = allPC; _i < allPC_1.length; _i++) { | ||
var pc = allPC_1[_i]; | ||
pc.close(); | ||
} | ||
@@ -674,3 +608,37 @@ }; | ||
} | ||
if (this.__debug == 'trace') { | ||
console.groupCollapsed('trace'); | ||
console.trace(); | ||
console.groupEnd(); | ||
} | ||
}; | ||
RTC.prototype.startScreenSharing = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var media; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.setupMedia('displayMedia')]; | ||
case 1: | ||
media = _a.sent(); | ||
this.addConnectionType('displayMedia'); | ||
return [2 /*return*/, media]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RTC.prototype.stopScreenSharing = function () { | ||
this.removeConnectionType('displayMedia'); | ||
}; | ||
RTC.prototype.handlePeerDisconnected = function (pc, mediaType) { | ||
this.destStream[mediaType] = null; | ||
for (var _i = 0, _a = Object.keys(this.peerConnections[mediaType]); _i < _a.length; _i++) { | ||
var peerId = _a[_i]; | ||
if (this.peerConnections[mediaType][peerId] == pc) { | ||
this.log("delete " + mediaType + " connection with " + peerId); | ||
this.peerConnections[mediaType][peerId] = null; | ||
delete this.peerConnections[mediaType][peerId]; | ||
} | ||
} | ||
this.eventEmitter.emit((mediaType == 'userMedia') ? STREAM_EVENTS.REMOTE_USER_MEDIA : STREAM_EVENTS.REMOTE_DISPLAY, null); | ||
}; | ||
return RTC; | ||
@@ -677,0 +645,0 @@ }()); |
@@ -22,2 +22,3 @@ "use strict"; | ||
socket.on('connect', function () { | ||
_this.log('socket connected'); | ||
resolve(); | ||
@@ -24,0 +25,0 @@ }); |
{ | ||
"name": "webrtc-myip", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "WebRTC client", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
750245
16362