Browse Source

Add VideoSource.video

customisations
alemart 8 months ago
parent
commit
f7dc9e7f5c
4 changed files with 27 additions and 11 deletions
  1. 1
    1
      docs/api/camera-source.md
  2. 11
    1
      docs/api/video-source.md
  3. 7
    9
      src/sources/camera-source.ts
  4. 8
    0
      src/sources/video-source.ts

+ 1
- 1
docs/api/camera-source.md View File

@@ -1,6 +1,6 @@
1 1
 # CameraSource
2 2
 
3
-A [source of data](source.md) linked to a webcam.
3
+A [source of data](source.md) linked to a webcam. This class extends [VideoSource](video-source.md).
4 4
 
5 5
 ## Instantiation
6 6
 

+ 11
- 1
docs/api/video-source.md View File

@@ -16,4 +16,14 @@ Create a new source of data linked to the provided `video`.
16 16
 
17 17
 **Returns**
18 18
 
19
-A new source of data.
19
+A new source of data.
20
+
21
+## Properties
22
+
23
+### video
24
+
25
+`source.video: HTMLVideoElement, read-only`
26
+
27
+The underlying `<video>` element.
28
+
29
+*Since:* 0.4.1

+ 7
- 9
src/sources/camera-source.ts View File

@@ -58,9 +58,6 @@ const DEFAULT_CAMERA_OPTIONS: Readonly<Required<CameraSourceOptions>> = {
58 58
  */
59 59
 export class CameraSource extends VideoSource
60 60
 {
61
-    /** Video element */
62
-    private _cameraVideo: HTMLVideoElement;
63
-
64 61
     /** Options of the constructor */
65 62
     private _options: Required<CameraSourceOptions>;
66 63
 
@@ -75,7 +72,6 @@ export class CameraSource extends VideoSource
75 72
         const video = document.createElement('video');
76 73
 
77 74
         super(video);
78
-        this._cameraVideo = video;
79 75
         this._options = Object.assign({}, DEFAULT_CAMERA_OPTIONS, options);
80 76
     }
81 77
 
@@ -108,6 +104,7 @@ export class CameraSource extends VideoSource
108 104
             video: {
109 105
                 width: size.width,
110 106
                 height: size.height,
107
+                aspectRatio: options.aspectRatio,
111 108
                 ...options.constraints,
112 109
             }
113 110
         };
@@ -115,7 +112,7 @@ export class CameraSource extends VideoSource
115 112
         // load camera stream
116 113
         return new Speedy.Promise<HTMLVideoElement>((resolve, reject) => {
117 114
             navigator.mediaDevices.getUserMedia(constraints).then(stream => {
118
-                const video = this._cameraVideo;
115
+                const video = this.video;
119 116
                 video.onloadedmetadata = () => {
120 117
                     const promise = video.play();
121 118
                     const success = 'Access to the webcam has been granted.';
@@ -148,7 +145,7 @@ export class CameraSource extends VideoSource
148 145
                     error
149 146
                 ));
150 147
             });
151
-        }).then(_ => super._init()); // this will call VideoSource._handleBrowserQuirks()
148
+        }).then(_ => super._init()); // this will handle browser quirks
152 149
     }
153 150
 
154 151
     /**
@@ -158,13 +155,14 @@ export class CameraSource extends VideoSource
158 155
      */
159 156
     _release(): SpeedyPromise<void>
160 157
     {
161
-        const stream = this._cameraVideo.srcObject as MediaStream;
158
+        const video = this.video;
159
+        const stream = video.srcObject as MediaStream;
162 160
         const tracks = stream.getTracks();
163 161
 
164 162
         // stop camera feed
165 163
         tracks.forEach(track => track.stop());
166
-        this._cameraVideo.onloadedmetadata = null;
167
-        this._cameraVideo.srcObject = null;
164
+        video.onloadedmetadata = null;
165
+        video.srcObject = null;
168 166
 
169 167
         // release the media
170 168
         return super._release();

+ 8
- 0
src/sources/video-source.ts View File

@@ -59,6 +59,14 @@ export class VideoSource implements Source
59 59
     }
60 60
 
61 61
     /**
62
+     * The underlying <video> element
63
+     */
64
+    get video(): HTMLVideoElement
65
+    {
66
+        return this._video;
67
+    }
68
+
69
+    /**
62 70
      * A type-identifier of the source of data
63 71
      * @internal
64 72
      */

Loading…
Cancel
Save