Skip to content

Commit

Permalink
impl navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Jul 3, 2024
1 parent eb5e058 commit 925bafb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/webrtc/src/media/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ export class MediaStream {
id!: string;
tracks: MediaStreamTrack[] = [];

constructor(props: Partial<MediaStream> & Pick<MediaStream, "id">) {
Object.assign(this, props);
constructor(props: Partial<MediaStream> | MediaStreamTrack[]) {
if (Array.isArray(props)) {
this.tracks = props;
} else {
Object.assign(this, props);
}
this.id ??= v4();
}

addTrack(track: MediaStreamTrack) {
Expand All @@ -83,4 +88,12 @@ export class MediaStream {
getTracks() {
return this.tracks;
}

getAudioTracks() {
return this.tracks.filter((track) => track.kind === "audio");
}

getVideoTracks() {
return this.tracks.filter((track) => track.kind === "video");
}
}
1 change: 1 addition & 0 deletions packages/webrtc/src/nonstandard/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./recorder";
export * from "./userMedia";
export * from "./navigator";
93 changes: 93 additions & 0 deletions packages/webrtc/src/nonstandard/navigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { MediaStream, MediaStreamTrack } from "../media/track";

export class Navigator {
readonly mediaDevices: MediaDevices;

constructor(props: ConstructorParameters<typeof MediaDevices>[0] = {}) {
this.mediaDevices = new MediaDevices(props);
}
}

class MediaDevices extends EventTarget {
video?: MediaStreamTrack;
audio?: MediaStreamTrack;

constructor(props: { video?: MediaStreamTrack; audio?: MediaStreamTrack }) {
super();
this.video = props.video;
this.audio = props.audio;
}

readonly getUserMedia = async (
constraints: MediaStreamConstraints,
): Promise<MediaStream> => {
if (constraints.video && constraints.audio) {
return new MediaStream([this.video!, this.audio!]);
} else if (constraints.audio) {
return new MediaStream([this.audio!]);
} else if (constraints.video) {
return new MediaStream([this.video!]);
}

throw new Error("Not implemented");
};

readonly getDisplayMedia = this.getUserMedia;
}

interface MediaStreamConstraints {
audio?: boolean | MediaTrackConstraints;
peerIdentity?: string;
preferCurrentTab?: boolean;
video?: boolean | MediaTrackConstraints;
}

interface MediaTrackConstraints extends MediaTrackConstraintSet {
advanced?: MediaTrackConstraintSet[];
}

interface MediaTrackConstraintSet {
aspectRatio?: ConstrainDouble;
autoGainControl?: ConstrainBoolean;
channelCount?: ConstrainULong;
deviceId?: ConstrainDOMString;
displaySurface?: ConstrainDOMString;
echoCancellation?: ConstrainBoolean;
facingMode?: ConstrainDOMString;
frameRate?: ConstrainDouble;
groupId?: ConstrainDOMString;
height?: ConstrainULong;
noiseSuppression?: ConstrainBoolean;
sampleRate?: ConstrainULong;
sampleSize?: ConstrainULong;
width?: ConstrainULong;
}

type ConstrainDOMString = string | string[] | ConstrainDOMStringParameters;
interface ConstrainDOMStringParameters {
exact?: string | string[];
ideal?: string | string[];
}
type ConstrainBoolean = boolean | ConstrainBooleanParameters;
interface ConstrainBooleanParameters {
exact?: boolean;
ideal?: boolean;
}
type ConstrainULong = number | ConstrainULongRange;
interface ConstrainULongRange extends ULongRange {
exact?: number;
ideal?: number;
}
interface ULongRange {
max?: number;
min?: number;
}
type ConstrainDouble = number | ConstrainDoubleRange;
interface ConstrainDoubleRange extends DoubleRange {
exact?: number;
ideal?: number;
}
interface DoubleRange {
max?: number;
min?: number;
}

0 comments on commit 925bafb

Please sign in to comment.