Skip to content

Commit

Permalink
chore: merge with arun3528:track-creation
Browse files Browse the repository at this point in the history
  • Loading branch information
haohancom committed Jan 12, 2023
2 parents d7a81be + a2df12a commit 221ec80
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 221 deletions.
96 changes: 65 additions & 31 deletions packages/webrtc-core/src/device/device-management.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as media from '../media';
import { LocalAudioTrack, MicrophoneConstraints } from '../media/local-audio-track';
import { LocalCameraTrack } from '../media/local-camera-track';
import { LocalDisplayTrack } from '../media/local-display-track';
import { LocalMicrophoneTrack } from '../media/local-microphone-track';
import { CameraConstraints } from '../media/local-video-track';

export enum ErrorTypes {
DEVICE_PERMISSION_DENIED = 'DEVICE_PERMISSION_DENIED',
Expand All @@ -10,9 +12,9 @@ export enum ErrorTypes {
}

/**
* Represents a WCME error, which contains error type and error message.
* Represents a webrtc core error, which contains error type and error message.
*/
export class WcmeError {
export class WebrtcError {
type: string;

message: string;
Expand All @@ -29,38 +31,52 @@ export class WcmeError {
}
}

export type AudioDeviceConstraints = {
deviceId?: string;
};

export type VideoDeviceConstraints = {
deviceId?: ConstrainDOMString;
width?: ConstrainULong;
height?: ConstrainULong;
aspectRatio?: ConstrainDouble;
frameRate?: ConstrainDouble;
facingMode?: ConstrainDOMString;
};
/**
* Creates MicrophoneTrack and CameraTrack at the same time.
*
* @param audioConstraints - Audio constraints to create microphone track.
* @param videoConstraints - Video constraints to create camera track.
* @returns MicrophoneTrack and cameraTrack at same time.
*/
export async function createMicrophoneAndCameraTracks(
audioConstraints?: MicrophoneConstraints,
videoConstraints?: CameraConstraints
): Promise<[LocalMicrophoneTrack, LocalCameraTrack]> {
let stream: MediaStream;
try {
stream = await media.getUserMedia({
audio: audioConstraints,
video: videoConstraints,
});
} catch (error) {
throw new WebrtcError(
ErrorTypes.CREATE_CAMERA_TRACK_FAILED,
`Failed to create camera track ${error}`
);
}
return [
new LocalMicrophoneTrack(stream.getAudioTracks()[0]),
new LocalCameraTrack(stream.getVideoTracks()[0]),
];
}

/**
* 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> {
export async function createCameraTrack(constraints?: CameraConstraints): Promise<LocalCameraTrack> {
let stream: MediaStream;
try {
stream = await media.getUserMedia({ video: { ...constraints } });
stream = await media.getUserMedia({ video: constraints });
} catch (error) {
throw new WcmeError(
throw new WebrtcError(
ErrorTypes.CREATE_CAMERA_TRACK_FAILED,
`Failed to create camera track ${error}`
);
}
return new LocalCameraTrack(stream);
return new LocalCameraTrack(stream.getVideoTracks()[0]);
}

/**
Expand All @@ -70,28 +86,43 @@ export async function createCameraTrack(
* @returns A LocalTrack object or an error.
*/
export async function createMicrophoneTrack(
constraints?: AudioDeviceConstraints
constraints?: MicrophoneConstraints
): Promise<LocalMicrophoneTrack> {
let stream: MediaStream;
try {
stream = await media.getUserMedia({ audio: { ...constraints } });
stream = await media.getUserMedia({ audio: constraints });
} catch (error) {
throw new WcmeError(
throw new WebrtcError(
ErrorTypes.CREATE_MICROPHONE_TRACK_FAILED,
`Failed to create microphone track ${error}`
);
}
return new LocalMicrophoneTrack(stream);
// See if we can just pass the track and not streams
return new LocalMicrophoneTrack(stream.getAudioTracks()[0]);
}

/**
* Creates a display video track.
*
* @param constraints - Display constraints for screen sharing.
* @param withAudio
* @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);
export async function createDisplayTrack(
constraints?: CameraConstraints,
withAudio?: boolean
): Promise<{
localDisplayTrack: LocalDisplayTrack;
localAudioTrack?: LocalAudioTrack | undefined;
}> {
const stream = await media.getDisplayMedia({ video: constraints, audio: withAudio });

return {
localDisplayTrack: new LocalDisplayTrack(stream.getVideoTracks()[0]),
localAudioTrack: stream.getAudioTracks()[0]
? new LocalAudioTrack(stream.getAudioTracks()[0])
: undefined,
};
}

/**
Expand All @@ -108,7 +139,10 @@ export async function getDevices(deviceKind?: media.DeviceKind): Promise<MediaDe
media.enumerateDevices
);
} catch (error) {
throw new WcmeError(ErrorTypes.DEVICE_PERMISSION_DENIED, 'Failed to ensure device permissions');
throw new WebrtcError(
ErrorTypes.DEVICE_PERMISSION_DENIED,
'Failed to ensure device permissions'
);
}

return devices.filter((v: MediaDeviceInfo) => (deviceKind ? v.kind === deviceKind : true));
Expand All @@ -119,7 +153,7 @@ export async function getDevices(deviceKind?: media.DeviceKind): Promise<MediaDe
*
* @returns List of microphone devices in an array of MediaDeviceInfo objects.
*/
export async function getAudioInputDevices(): Promise<MediaDeviceInfo[]> {
export async function getMicrophones(): Promise<MediaDeviceInfo[]> {
return getDevices(media.DeviceKind.AudioInput);
}

Expand All @@ -128,7 +162,7 @@ export async function getAudioInputDevices(): Promise<MediaDeviceInfo[]> {
*
* @returns List of speaker devices in an array of MediaDeviceInfo objects.
*/
export async function getAudioOutputDevices(): Promise<MediaDeviceInfo[]> {
export async function getSpeakers(): Promise<MediaDeviceInfo[]> {
return getDevices(media.DeviceKind.AudioOutput);
}

Expand All @@ -137,7 +171,7 @@ export async function getAudioOutputDevices(): Promise<MediaDeviceInfo[]> {
*
* @returns List of camera devices in an array of MediaDeviceInfo objects.
*/
export async function getVideoInputDevices(): Promise<MediaDeviceInfo[]> {
export async function getCameras(): Promise<MediaDeviceInfo[]> {
return getDevices(media.DeviceKind.VideoInput);
}

Expand Down
1 change: 1 addition & 0 deletions packages/webrtc-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './media/local-camera-track';
export * from './media/local-display-track';
export * from './media/local-microphone-track';
export * from './media/local-track';
export * from './media/local-video-track';
export * from './peer-connection';
export * from './peer-connection-utils';
export { media };
28 changes: 28 additions & 0 deletions packages/webrtc-core/src/media/local-audio-track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { LocalTrack } from './local-track';

export type MicrophoneConstraints = Pick<
MediaTrackConstraints,
| 'deviceId'
| 'echoCancellation'
| 'autoGainControl'
| 'noiseSuppression'
| 'sampleRate'
| 'sampleSize'
| 'channelCount'
>;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TrackEffect = any;
/**
* Represents a audio track for a audio source.
*/
export class LocalAudioTrack extends LocalTrack {
/**
* Makes sure to apply the encoderConfig for the audio.
*
* @param encoderConfig - Encoder config for audio.
*/
setEncoderConfig(encoderConfig: MicrophoneConstraints): void {
this.getMediaStreamTrack().applyConstraints(encoderConfig);
}
}
4 changes: 2 additions & 2 deletions packages/webrtc-core/src/media/local-camera-track.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LocalTrack } from './local-track';
import { LocalVideoTrack } from './local-video-track';

/**
* Represents a local track for a camera source.
*/
export class LocalCameraTrack extends LocalTrack {}
export class LocalCameraTrack extends LocalVideoTrack {}
4 changes: 2 additions & 2 deletions packages/webrtc-core/src/media/local-display-track.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LocalTrack } from './local-track';
import { LocalVideoTrack } from './local-video-track';

/**
* Represents a local track for a display source.
*/
export class LocalDisplayTrack extends LocalTrack {}
export class LocalDisplayTrack extends LocalVideoTrack {}
4 changes: 2 additions & 2 deletions packages/webrtc-core/src/media/local-microphone-track.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LocalTrack } from './local-track';
import { LocalAudioTrack } from './local-audio-track';

/**
* Represents a local track for a microphone source.
*/
export class LocalMicrophoneTrack extends LocalTrack {}
export class LocalMicrophoneTrack extends LocalAudioTrack {}
Loading

0 comments on commit 221ec80

Please sign in to comment.