-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrating the wcme track to webrtc core
- Loading branch information
Showing
26 changed files
with
4,971 additions
and
4,146 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
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 +1 @@ | ||
engine-strict = true | ||
registry = https://registry.npmjs.org |
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,113 @@ | ||
import * as media from '../media'; | ||
import { LocalCameraTrack } from '../media/local-camera-track'; | ||
import { LocalDisplayTrack } from '../media/local-display-track'; | ||
import { LocalMicrophoneTrack } from '../media/local-microphone-track'; | ||
import { createBrowserMock } from '../mocks/create-browser-mock'; | ||
import MediaStreamStub from '../mocks/media-stream-stub'; | ||
import { mocked } from '../mocks/mock'; | ||
import { createCameraTrack, createDisplayTrack, createMicrophoneTrack } from './device-management'; | ||
|
||
jest.mock('../mocks/media-stream-stub'); | ||
|
||
describe('Device Management', () => { | ||
// const mockedMedia = createBrowserMock(media); | ||
createBrowserMock(MediaStreamStub, 'MediaStream'); | ||
|
||
const mockStream = mocked(new MediaStream()); | ||
const track = new MediaStreamTrack(); | ||
mockStream.getTracks.mockReturnValue([track]); | ||
|
||
describe('createMicrophoneTrack', () => { | ||
jest | ||
.spyOn(media, 'getUserMedia') | ||
.mockImplementation() | ||
.mockReturnValue(Promise.resolve(mockStream as unknown as MediaStream)); | ||
|
||
it('should call getUserMedia', async () => { | ||
expect.assertions(1); | ||
|
||
await createMicrophoneTrack({ deviceId: 'test-device-id' }); | ||
expect(media.getUserMedia).toHaveBeenCalledWith({ | ||
audio: { | ||
deviceId: 'test-device-id', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return a LocalMicrophoneTrack instance', async () => { | ||
expect.assertions(1); | ||
|
||
const localMicrophoneTrack = await createMicrophoneTrack({ deviceId: 'test-device-id' }); | ||
expect(localMicrophoneTrack).toBeInstanceOf(LocalMicrophoneTrack); | ||
}); | ||
}); | ||
|
||
describe('createCameraTrack', () => { | ||
jest | ||
.spyOn(media, 'getUserMedia') | ||
.mockImplementation() | ||
.mockReturnValue(Promise.resolve(mockStream as unknown as MediaStream)); | ||
|
||
it('should call getUserMedia', async () => { | ||
expect.assertions(1); | ||
|
||
await createCameraTrack({ deviceId: 'test-device-id' }); | ||
expect(media.getUserMedia).toHaveBeenCalledWith({ | ||
video: { | ||
deviceId: 'test-device-id', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should call getUserMedia with constraints', async () => { | ||
expect.assertions(1); | ||
|
||
await createCameraTrack({ | ||
deviceId: 'test-device-id', | ||
aspectRatio: 1.777, | ||
width: 1920, | ||
height: 1080, | ||
frameRate: 30, | ||
facingMode: { exact: 'user' }, | ||
}); | ||
expect(media.getUserMedia).toHaveBeenCalledWith({ | ||
video: { | ||
deviceId: 'test-device-id', | ||
aspectRatio: 1.777, | ||
width: 1920, | ||
height: 1080, | ||
frameRate: 30, | ||
facingMode: { exact: 'user' }, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return a LocalCameraTrack instance', async () => { | ||
expect.assertions(1); | ||
|
||
const localCameraTrack = await createCameraTrack({ deviceId: 'test-device-id' }); | ||
expect(localCameraTrack).toBeInstanceOf(LocalCameraTrack); | ||
}); | ||
}); | ||
|
||
describe('createDisplayTrack', () => { | ||
jest | ||
.spyOn(media, 'getDisplayMedia') | ||
.mockImplementation() | ||
.mockReturnValue(Promise.resolve(mockStream as unknown as MediaStream)); | ||
|
||
it('should call getDisplayMedia', async () => { | ||
expect.assertions(1); | ||
|
||
await createDisplayTrack(); | ||
expect(media.getDisplayMedia).toHaveBeenCalledWith({ video: true }); | ||
}); | ||
|
||
it('should return a LocalDisplayTrack instance', async () => { | ||
expect.assertions(1); | ||
|
||
const localDisplayTrack = await createDisplayTrack(); | ||
expect(localDisplayTrack).toBeInstanceOf(LocalDisplayTrack); | ||
}); | ||
}); | ||
}); |
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,147 @@ | ||
import * as media from '../media'; | ||
import { LocalCameraTrack } from '../media/local-camera-track'; | ||
import { LocalDisplayTrack } from '../media/local-display-track'; | ||
import { LocalMicrophoneTrack } from '../media/local-microphone-track'; | ||
|
||
export enum ErrorTypes { | ||
DEVICE_PERMISSION_DENIED = 'DEVICE_PERMISSION_DENIED', | ||
CREATE_CAMERA_TRACK_FAILED = 'CREATE_CAMERA_TRACK_FAILED', | ||
CREATE_MICROPHONE_TRACK_FAILED = 'CREATE_MICROPHONE_TRACK_FAILED', | ||
} | ||
|
||
/** | ||
* Represents a WCME error, which contains error type and error message. | ||
*/ | ||
export class WcmeError { | ||
type: string; | ||
|
||
message: string; | ||
|
||
/** | ||
* Creates new error. | ||
* | ||
* @param type - Error type. | ||
* @param message - Error message. | ||
*/ | ||
constructor(type: ErrorTypes, message = '') { | ||
this.type = type; | ||
this.message = message; | ||
} | ||
} | ||
|
||
export type AudioDeviceConstraints = { | ||
deviceId?: string; | ||
}; | ||
|
||
export type VideoDeviceConstraints = { | ||
deviceId?: ConstrainDOMString; | ||
width?: ConstrainULong; | ||
height?: ConstrainULong; | ||
aspectRatio?: ConstrainDouble; | ||
frameRate?: ConstrainDouble; | ||
facingMode?: ConstrainDOMString; | ||
}; | ||
|
||
/** | ||
* Creates a camera video track. | ||
* | ||
* @param constraints - Video device constraints. | ||
* @returns A LocalTrack object or an error. | ||
*/ | ||
export async function createCameraTrack( | ||
constraints?: VideoDeviceConstraints | ||
): Promise<LocalCameraTrack> { | ||
let stream: MediaStream; | ||
try { | ||
stream = await media.getUserMedia({ video: { ...constraints } }); | ||
} catch (error) { | ||
throw new WcmeError( | ||
ErrorTypes.CREATE_CAMERA_TRACK_FAILED, | ||
`Failed to create camera track ${error}` | ||
); | ||
} | ||
return new LocalCameraTrack(stream); | ||
} | ||
|
||
/** | ||
* Creates a microphone audio track. | ||
* | ||
* @param constraints - Audio device constraints. | ||
* @returns A LocalTrack object or an error. | ||
*/ | ||
export async function createMicrophoneTrack( | ||
constraints?: AudioDeviceConstraints | ||
): Promise<LocalMicrophoneTrack> { | ||
let stream: MediaStream; | ||
try { | ||
stream = await media.getUserMedia({ audio: { ...constraints } }); | ||
} catch (error) { | ||
throw new WcmeError( | ||
ErrorTypes.CREATE_MICROPHONE_TRACK_FAILED, | ||
`Failed to create microphone track ${error}` | ||
); | ||
} | ||
return new LocalMicrophoneTrack(stream); | ||
} | ||
|
||
/** | ||
* Creates a display video track. | ||
* | ||
* @returns A Promise that resolves to a LocalDisplayTrack. | ||
*/ | ||
export async function createDisplayTrack(): Promise<LocalDisplayTrack> { | ||
const stream = await media.getDisplayMedia({ video: true }); | ||
return new LocalDisplayTrack(stream); | ||
} | ||
|
||
/** | ||
* Enumerates the media input and output devices available. | ||
* | ||
* @param deviceKind - Optional filter to return a specific device kind. | ||
* @returns List of media devices in an array of MediaDeviceInfo objects. | ||
*/ | ||
export async function getDevices(deviceKind?: media.DeviceKind): Promise<MediaDeviceInfo[]> { | ||
let devices: MediaDeviceInfo[]; | ||
try { | ||
devices = await media.ensureDevicePermissions( | ||
[media.DeviceKind.AudioInput, media.DeviceKind.VideoInput], | ||
media.enumerateDevices | ||
); | ||
} catch (error) { | ||
throw new WcmeError(ErrorTypes.DEVICE_PERMISSION_DENIED, 'Failed to ensure device permissions'); | ||
} | ||
|
||
return devices.filter((v: MediaDeviceInfo) => (deviceKind ? v.kind === deviceKind : true)); | ||
} | ||
|
||
/** | ||
* Helper function to get a list of microphone devices. | ||
* | ||
* @returns List of microphone devices in an array of MediaDeviceInfo objects. | ||
*/ | ||
export async function getAudioInputDevices(): Promise<MediaDeviceInfo[]> { | ||
return getDevices(media.DeviceKind.AudioInput); | ||
} | ||
|
||
/** | ||
* Helper function to get a list of speaker devices. | ||
* | ||
* @returns List of speaker devices in an array of MediaDeviceInfo objects. | ||
*/ | ||
export async function getAudioOutputDevices(): Promise<MediaDeviceInfo[]> { | ||
return getDevices(media.DeviceKind.AudioOutput); | ||
} | ||
|
||
/** | ||
* Helper function to get a list of camera devices. | ||
* | ||
* @returns List of camera devices in an array of MediaDeviceInfo objects. | ||
*/ | ||
export async function getVideoInputDevices(): Promise<MediaDeviceInfo[]> { | ||
return getDevices(media.DeviceKind.VideoInput); | ||
} | ||
|
||
/** | ||
* Export the setOnDeviceChangeHandler method directly from the core lib. | ||
*/ | ||
export const { setOnDeviceChangeHandler } = media; |
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 +1,9 @@ | ||
export { EventEmitter } from 'events'; | ||
import { EventEmitter as EE } from 'events'; | ||
import TypedEmitter, { EventMap } from 'typed-emitter'; | ||
|
||
/** | ||
* Typed event emitter class. | ||
*/ | ||
export default class EventEmitter<T extends EventMap> extends (EE as { | ||
new <TT extends EventMap>(): TypedEmitter<TT>; | ||
})<T> {} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { LocalTrack } from './local-track'; | ||
|
||
/** | ||
* Represents a local track for a camera source. | ||
*/ | ||
export class LocalCameraTrack extends LocalTrack {} |
Oops, something went wrong.