-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1160 from League-of-Foundry-Developers/0.8.x/av
Update AV related classes
- Loading branch information
Showing
12 changed files
with
271 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import './easyRTCClient'; | ||
import './simplePeerAVClient'; |
138 changes: 138 additions & 0 deletions
138
src/foundry/foundry.js/avClients/simplePeerAVClient.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* An implementation of the AVClient which uses the simple-peer library and the Foundry socket server for signaling. | ||
* Credit to bekit#4213 for identifying simple-peer as a viable technology and providing a POC implementation. | ||
*/ | ||
declare class SimplePeerAVClient extends AVClient { | ||
/** | ||
* The local Stream which captures input video and audio | ||
* @defaultValue `null` | ||
*/ | ||
localStream: MediaStream | null; | ||
|
||
/** | ||
* A mapping of connected peers | ||
*/ | ||
peers: Map<string, SimplePeer.Instance>; | ||
|
||
/** | ||
* A mapping of connected remote streams | ||
*/ | ||
remoteStreams: Map<string, MediaStream>; | ||
|
||
/** | ||
* Has the client been successfully initialized? | ||
* @defaultValue `false` | ||
* @internal | ||
*/ | ||
_initialized: boolean; | ||
|
||
/** | ||
* Is outbound broadcast of local audio enabled? | ||
* @defaultValue `false` | ||
*/ | ||
audioBroadcastEnabled: boolean; | ||
|
||
/** @override */ | ||
connect(): Promise<boolean>; | ||
|
||
/** @override */ | ||
disconnect(): Promise<boolean>; | ||
|
||
/** @override */ | ||
initialize(): Promise<void>; | ||
|
||
/** @override */ | ||
getConnectedUsers(): string[]; | ||
|
||
/** @override */ | ||
getMediaStreamForUser(userId: string): MediaStream | null | undefined; | ||
|
||
/** @override */ | ||
isAudioEnabled(): boolean; | ||
|
||
/** @override */ | ||
isVideoEnabled(): boolean; | ||
|
||
/** @override */ | ||
toggleAudio(enable: boolean): void; | ||
|
||
/** @override */ | ||
toggleBroadcast(broadcast: boolean): void; | ||
|
||
/** @override */ | ||
toggleVideo(enable: boolean): void; | ||
|
||
/** @override */ | ||
setUserVideo(userId: string, videoElement: HTMLVideoElement): Promise<void>; | ||
|
||
/** | ||
* Initialize a local media stream for the current user | ||
*/ | ||
initializeLocalStream(): Promise<MediaStream | null>; | ||
|
||
/** | ||
* Listen for Audio/Video updates on the av socket to broker connections between peers | ||
*/ | ||
activateSocketListeners(): void; | ||
|
||
/** | ||
* Initialize a stream connection with a new peer | ||
* @param userId - The Foundry user ID for which the peer stream should be established | ||
* @returns A Promise which resolves once the peer stream is initialized | ||
*/ | ||
initializePeerStream(userId: string): Promise<SimplePeer.Instance>; | ||
|
||
/** | ||
* Receive a request to establish a peer signal with some other User id | ||
* @param userId - The Foundry user ID who is requesting to establish a connection | ||
* @param data - The connection details provided by SimplePeer | ||
*/ | ||
receiveSignal(userId: string, data: SimplePeer.SignalData): void; | ||
|
||
/** | ||
* Connect to a peer directly, either as the initiator or as the receiver | ||
* @param userId - The Foundry user ID with whom we are connecting | ||
* @param isInitiator - Is the current user initiating the connection, or responding to it? | ||
* (default: `false`) | ||
* @returns The constructed and configured SimplePeer instance | ||
*/ | ||
connectPeer(userId: string, isInitiator?: boolean): SimplePeer.Instance; | ||
|
||
/** | ||
* Create the SimplePeer instance for the desired peer connection. | ||
* Modules may implement more advanced connection strategies by overriding this method. | ||
* @param userId - The Foundry user ID with whom we are connecting | ||
* @param isInitiator - Is the current user initiating the connection, or responding to it? | ||
* @internal | ||
*/ | ||
_createPeerConnection(userId: string, isInitiator: boolean): SimplePeer.Instance; | ||
|
||
/** | ||
* Setup the custom TURN relay to be used in subsequent calls if there is one configured. | ||
* TURN credentials are mandatory in WebRTC. | ||
* @param options - The SimplePeer configuration object. | ||
* @internal | ||
*/ | ||
_setupCustomTURN(options: SimplePeer.Options): void; | ||
|
||
/** | ||
* Disconnect from a peer by stopping current stream tracks and destroying the SimplePeer instance | ||
* @param userId - The Foundry user ID from whom we are disconnecting | ||
* @returns A Promise which resolves once the disconnection is complete | ||
*/ | ||
disconnectPeer(userId: string): Promise<void>; | ||
|
||
/** | ||
* Disconnect from all current peer streams | ||
* @returns A Promise which resolves once all peers have been disconnected | ||
*/ | ||
disconnectAll(): Promise<Array<void>>; | ||
|
||
/** @override */ | ||
onSettingsChanged(changed: DeepPartial<AVSettings.Settings>): Promise<void>; | ||
|
||
/** | ||
* Replace the local stream for each connected peer with a re-generated MediaStream | ||
*/ | ||
updateLocalStream(): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
import * as SimplePeer from 'simple-peer'; | ||
export = SimplePeer; | ||
export as namespace SimplePeer; | ||
import * as _SimplePeer from 'simple-peer'; | ||
|
||
declare global { | ||
namespace SimplePeer { | ||
type Options = _SimplePeer.Options; | ||
type SimplePeer = _SimplePeer.SimplePeer; | ||
type TypedArray = _SimplePeer.TypedArray; | ||
type SimplePeerData = _SimplePeer.SimplePeerData; | ||
type SignalData = _SimplePeer.SignalData; | ||
type Instance = _SimplePeer.Instance; | ||
} | ||
const SimplePeer: SimplePeer.SimplePeer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { expectType } from 'tsd'; | ||
|
||
interface CustomVoiceModes { | ||
SOME_CUSTOM_MODE: 'custom'; | ||
} | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace AVSettings { | ||
interface Overrides { | ||
VoiceModes: CustomVoiceModes; | ||
} | ||
} | ||
} | ||
|
||
AVSettings.VOICE_MODES = { | ||
SOME_CUSTOM_MODE: 'custom' | ||
}; | ||
|
||
expectType<CustomVoiceModes>(AVSettings.VOICE_MODES); | ||
|
||
const avMaster = new AVMaster(); | ||
expectType<'custom'>(avMaster.mode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { expectType } from 'tsd'; | ||
|
||
declare class CustomAVCLient extends AVClient { | ||
initialize(): Promise<void>; | ||
connect(): Promise<boolean>; | ||
disconnect(): Promise<boolean>; | ||
getConnectedUsers(): string[]; | ||
getMediaStreamForUser(userId: string): MediaStream | null; | ||
isAudioEnabled(): boolean; | ||
isVideoEnabled(): boolean; | ||
toggleAudio(enable: boolean): void; | ||
toggleBroadcast(broadcast: boolean): void; | ||
toggleVideo(enable: boolean): void; | ||
setUserVideo(userId: string, videoElement: HTMLVideoElement): Promise<void>; | ||
|
||
customProperty: string; | ||
} | ||
|
||
declare global { | ||
interface WebRTCConfig { | ||
clientClass: typeof CustomAVCLient; | ||
} | ||
} | ||
|
||
CONFIG.WebRTC.clientClass = CustomAVCLient; | ||
|
||
const avMaster = new AVMaster(); | ||
|
||
expectType<string>(avMaster.client.customProperty); | ||
if (game instanceof Game) { | ||
expectType<string | undefined>(game?.webrtc?.client.customProperty); | ||
} |