Skip to content

Commit

Permalink
feat: support typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
zhensherlock committed Apr 27, 2023
1 parent bc0f886 commit 66bb37d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "0.0.1",
"description": "webrtc streamer helper",
"scripts": {
"build:types": "tsc",
"dev": "concurrently \"npm run src:dev\"",
"src:dev": "rollup -c --watch --bundleConfigAsCjs",
"build": "rollup -c --bundleConfigAsCjs --environment NODE_ENV:production"
"build": "npm run build:types && rollup -c --bundleConfigAsCjs --environment NODE_ENV:production"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +27,9 @@
"browser": "dist/index.browser.js",
"unpkg": "",
"types": "dist/types/main.d.ts",
"files": [
"dist"
],
"devDependencies": {
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-eslint": "^9.0.3",
Expand Down
37 changes: 21 additions & 16 deletions src/core/webRTCStreamer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { WebRTCStreamerOptions } from '../types'
import { MediaConstraints, WebRTCStreamerOptions } from '../types'
import { initialOptions } from '../utils/initialization'

/**
* Interface with WebRTC-streamer API
*/
export default class WebRTCStreamer {
class WebRTCStreamer {
private element?: Element
private options: WebRTCStreamerOptions
private peerConnection: RTCPeerConnection | null = null
private peerConnectionConfig?: RTCConfiguration
private peerConnectionId: number = 0
private mediaConstraints = { offerToReceiveAudio: true, offerToReceiveVideo: true }
private mediaConstraints: MediaConstraints
private iceServers: RTCConfiguration | null = null
private earlyCandidates: RTCIceCandidate[] = []
private srcObject: any
Expand All @@ -25,6 +25,7 @@ export default class WebRTCStreamer {
if (!this.options.url) {
this.options.url = `${window.location.protocol}//${window.location.hostname}:${window.location.port}`
}
this.mediaConstraints = { offerToReceiveAudio: true, offerToReceiveVideo: true }
this.changeElement(this.options.element)
}

Expand All @@ -35,7 +36,7 @@ export default class WebRTCStreamer {
* @param options
* @param localStream
*/
connect (videUrl: string, audioUrl: string, options: string, localStream: any) {
connect (videUrl: string, audioUrl: string, options: string, localStream?: MediaStream): void {
this.disconnect()

if (!this.iceServers) {
Expand All @@ -54,7 +55,7 @@ export default class WebRTCStreamer {
/**
* Disconnect a WebRTC Stream and clear videoElement source
*/
disconnect () {
disconnect (): void {
if (this.srcObject) {
this.srcObject.getTracks().forEach((track: any) => {
track.stop()
Expand All @@ -75,7 +76,7 @@ export default class WebRTCStreamer {
}
}

private changeElement (element: Element | string) {
private changeElement (element: Element | string): void {
if (typeof element === 'string') {
const dom = document.querySelector(element)
dom && (this.element = dom)
Expand All @@ -84,7 +85,7 @@ export default class WebRTCStreamer {
}
}

private handleHttpErrors (resp: Response) {
private handleHttpErrors (resp: Response): Response {
if (!resp.ok) {
throw Error(resp.statusText)
}
Expand All @@ -100,7 +101,7 @@ export default class WebRTCStreamer {
* @param stream
* @private
*/
private onReceiveGetIceServers (iceServers: RTCConfiguration, videoUrl: string, audioUrl: string, options: string, stream: any) {
private onReceiveGetIceServers (iceServers: RTCConfiguration, videoUrl: string, audioUrl: string, options: string, stream?: MediaStream): void {
this.iceServers = iceServers
this.peerConnectionConfig = iceServers || { 'iceServers': [] }
try {
Expand Down Expand Up @@ -154,15 +155,15 @@ export default class WebRTCStreamer {
* @param status
* @private
*/
private onError (status: any) {
private onError (status: any): void {
console.log(`onError: ${status}`)
}

/**
* create RTCPeerConnection
* @private
*/
private createPeerConnection () {
private createPeerConnection (): RTCPeerConnection {
console.log(`createPeerConnection config: ${JSON.stringify(this.peerConnectionConfig)}`)
this.peerConnection = new RTCPeerConnection(this.peerConnectionConfig)
this.peerConnectionId = Math.random()
Expand Down Expand Up @@ -240,7 +241,7 @@ export default class WebRTCStreamer {
* @param dataJson
* @private
*/
private onReceiveCall (dataJson: RTCSessionDescriptionInit) {
private onReceiveCall (dataJson: RTCSessionDescriptionInit): void {
console.log(`offer: ${JSON.stringify(dataJson)}`)
const sessionDescription = new RTCSessionDescription(dataJson)
this.peerConnection?.setRemoteDescription(sessionDescription).then(() => {
Expand All @@ -255,7 +256,7 @@ export default class WebRTCStreamer {
})
}

private addIceCandidate (id: number, candidate: any) {
private addIceCandidate (id: number, candidate: any): void {
fetch(
`${this.options.url}/api/addIceCandidate?peerid=${id}`,
{ method: 'POST', body: JSON.stringify(candidate) }
Expand All @@ -268,7 +269,7 @@ export default class WebRTCStreamer {
}).catch((error) => this.onError(`addIceCandidate ${error}`))
}

private getIceCandidate () {
private getIceCandidate (): void {
fetch(
`${this.options.url}/api/getIceCandidate?peerid=${this.peerConnectionId}`
).then(
Expand All @@ -285,7 +286,7 @@ export default class WebRTCStreamer {
* @param dataJson
* @private
*/
private onReceiveCandidate (dataJson: RTCIceCandidateInit[]) {
private onReceiveCandidate (dataJson: RTCIceCandidateInit[]): void {
console.log(`candidate: ${JSON.stringify(dataJson)}`)
if (dataJson) {
for (let i = 0; i < dataJson.length; i++) {
Expand All @@ -306,7 +307,7 @@ export default class WebRTCStreamer {
* @param event
* @private
*/
private onAddStream (event: any) {
private onAddStream (event: any): void {
console.log(`Remote track added: ${JSON.stringify(event)}`)

this.srcObject = event.stream
Expand All @@ -324,7 +325,7 @@ export default class WebRTCStreamer {
* @param event
* @private
*/
private onIceCandidate (event: RTCPeerConnectionIceEvent) {
private onIceCandidate (event: RTCPeerConnectionIceEvent): void {
if (event.candidate) {
if (this.peerConnection?.currentRemoteDescription) {
this.addIceCandidate(this.peerConnectionId, event.candidate)
Expand All @@ -337,3 +338,7 @@ export default class WebRTCStreamer {
}
}
}

export {
WebRTCStreamer
}
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import WebRTCStreamer from './core/webRTCStreamer'
import {
WebRTCStreamer
} from './core/webRTCStreamer'
import './style'

export {
Expand Down
5 changes: 5 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export interface WebRTCStreamerOptions {
*/
url: string;
}

export type MediaConstraints = {
offerToReceiveAudio: boolean;
offerToReceiveVideo: boolean;
}

0 comments on commit 66bb37d

Please sign in to comment.