11import { Shutter } from "./Shutter" ;
22import { Recorder } from "./Recorder" ;
3- import { CaptureSource , FallbackMediaRecorderConfig } from "../types" ;
3+ import { CaptureSource } from "./CaptureSource" ;
4+ import { FallbackMediaRecorderConfig } from "../types" ;
5+ import { toTrackConstraints , createVideoElement } from "../util" ;
46
57export class CaptureStream {
68 private mediaStream : MediaStream ;
79 private previewStream : MediaStream ;
810
9- private videoSource : CaptureSource | undefined ;
10- private audioSource : CaptureSource | undefined ;
11+ private videoSource :
12+ | CaptureSource
13+ | MediaTrackConstraints
14+ | "front"
15+ | "back"
16+ | undefined ;
17+ private audioSource : CaptureSource | MediaTrackConstraints | undefined ;
1118 private previewVideoSource = this . videoSource ;
1219 private previewAudioSource = this . audioSource ;
1320
@@ -19,17 +26,17 @@ export class CaptureStream {
1926 /**
2027 * CaptureStream provides access to streaming related functions
2128 * @param {Object } opts
22- * @param {CaptureSource } [opts.video] - Video source to create CaptureStream from
23- * @param {CaptureSource } [opts.audio] - Audio source to create CaptureStream from
29+ * @param {CaptureSource | MediaTrackConstraints | "front" | "back" } [opts.video] - Video source to create CaptureStream from
30+ * @param {CaptureSource | MediaTrackConstraints } [opts.audio] - Audio source to create CaptureStream from
2431 * @param {Partial<FallbackMediaRecorderConfig> } [opts.fallbackConfig] - Optional config for FallbackMediaRecorder
2532 */
2633 constructor ( {
2734 video,
2835 audio,
2936 fallbackConfig
3037 } : {
31- video ?: CaptureSource ;
32- audio ?: CaptureSource ;
38+ video ?: CaptureSource | MediaTrackConstraints | "front" | "back" ;
39+ audio ?: CaptureSource | MediaTrackConstraints ;
3340 fallbackConfig ?: Partial < FallbackMediaRecorderConfig > ;
3441 } ) {
3542 this . previewVideoSource = this . videoSource = video ;
@@ -50,19 +57,17 @@ export class CaptureStream {
5057 private generateConstraints ( {
5158 source
5259 } : { source ?: "original" | "preview" } = { } ) : MediaStreamConstraints {
53- const video = ( source === "preview"
54- ? this . previewVideoSource
55- : this . videoSource ) ! . device ! . deviceId ;
56- const audio = ( source === "preview"
57- ? this . previewAudioSource
58- : this . audioSource ) ! . device ! . deviceId ;
59- if ( ! video && ! audio ) {
60+ const videoSource =
61+ source === "preview" ? this . previewVideoSource : this . videoSource ;
62+ const audioSource =
63+ source === "preview" ? this . previewAudioSource : this . audioSource ;
64+ if ( videoSource && ! audioSource ) {
6065 throw new Error ( "No compatible media sources" ) ;
6166 }
6267
6368 return {
64- video : { deviceId : video ? { exact : video } : undefined } ,
65- audio : { deviceId : audio ? { exact : audio } : undefined }
69+ video : toTrackConstraints ( videoSource ) ,
70+ audio : toTrackConstraints ( audioSource )
6671 } ;
6772 }
6873
@@ -98,15 +103,8 @@ export class CaptureStream {
98103 * @returns {Promise<Shutter> } Newly created shutter instance
99104 */
100105 private async initalizeShutter ( ) : Promise < Shutter > {
101- const original = document . createElement ( "video" ) ;
102- const preview = document . createElement ( "video" ) ;
103-
104- original . srcObject = this . mediaStream ;
105- original . play ( ) ;
106- original . muted = true ;
107- preview . srcObject = this . previewStream ;
108- preview . play ( ) ;
109- preview . muted = true ;
106+ const original = createVideoElement ( this . mediaStream ) ;
107+ const preview = createVideoElement ( this . previewStream ) ;
110108
111109 this . shutter = new Shutter ( { original, preview } ) ;
112110 return this . shutter ;
@@ -181,8 +179,8 @@ export class CaptureStream {
181179 /**
182180 * Re-sets the audio/video source for the specified stream
183181 * @param {Object } [opts={}]
184- * @param {CaptureSource } [opts.video] - New video source
185- * @param {CaptureSource } [opts.audio] - New audio source
182+ * @param {CaptureSource | MediaTrackConstraints | "front" | "back" } [opts.video] - New video source
183+ * @param {CaptureSource | MediaTrackConstraints } [opts.audio] - New audio source
186184 * @param {("original" | "preview") } [opts.source=original] - Which stream the to set the new sources
187185 * @returns {Promise<MediaStream> } The new stream with updated source
188186 */
0 commit comments