Bladeren bron

Session: make non-existent primary sources acceptable

customisations
alemart 10 maanden geleden
bovenliggende
commit
666086e08f
1 gewijzigde bestanden met toevoegingen van 14 en 17 verwijderingen
  1. 14
    17
      src/core/session.ts

+ 14
- 17
src/core/session.ts Bestand weergeven

@@ -110,7 +110,7 @@ export class Session extends AREventTarget<SessionEventType>
110 110
     private readonly _viewport: Viewport;
111 111
 
112 112
     /** Primary source of data */
113
-    private readonly _primarySource: VideoSource | CanvasSource;
113
+    private readonly _primarySource: Nullable<VideoSource | CanvasSource>;
114 114
 
115 115
     /** Time Manager */
116 116
     private _time: TimeManager;
@@ -171,7 +171,10 @@ export class Session extends AREventTarget<SessionEventType>
171 171
 
172 172
         // setup the viewport
173 173
         this._viewport = viewport;
174
-        this._viewport._init(() => this._media.size, mode);
174
+        if(this._primarySource !== null)
175
+            this._viewport._init(() => this._primarySource!._data.size, mode);
176
+        else
177
+            this._viewport._init(() => Utils.resolution('sm', window.innerWidth / window.innerHeight), mode);
175 178
 
176 179
         // setup the main loop
177 180
         this._setupUpdateLoop();
@@ -486,9 +489,9 @@ export class Session extends AREventTarget<SessionEventType>
486 489
     /**
487 490
      * Find the primary source of data (generally a camera stream)
488 491
      * @param sources
489
-     * @returns the primary source
492
+     * @returns the primary source, or null if there isn't any
490 493
      */
491
-    private _findPrimarySource(sources: Source[]): VideoSource | CanvasSource
494
+    private _findPrimarySource(sources: Source[]): Nullable<VideoSource | CanvasSource>
492 495
     {
493 496
         // prefer video sources
494 497
         for(let i = 0; i < sources.length; i++) {
@@ -500,16 +503,9 @@ export class Session extends AREventTarget<SessionEventType>
500 503
                 return sources[i] as CanvasSource;
501 504
         }
502 505
 
503
-        // this shouldn't happen
504
-        throw new IllegalOperationError(`No primary source of data was found!`);
505
-    }
506
-
507
-    /**
508
-     * The media of the primary source of data
509
-     */
510
-    private get _media(): SpeedyMedia
511
-    {
512
-        return this._primarySource._data;
506
+        // emit warning
507
+        Utils.warning(`No primary source of data was found!`);
508
+        return null;
513 509
     }
514 510
 
515 511
     /**
@@ -533,14 +529,15 @@ export class Session extends AREventTarget<SessionEventType>
533 529
      */
534 530
     private _renderUserMedia(): void
535 531
     {
532
+        const media = this._primarySource?._data;
536 533
         const canvas = this._viewport._backgroundCanvas;
537 534
         const ctx = canvas.getContext('2d', { alpha: false });
538
-        
539
-        if(ctx && this._media.type != 'data') {
535
+
536
+        if(ctx && media && media.type != 'data') {
540 537
             ctx.imageSmoothingEnabled = false;
541 538
 
542 539
             // draw user media
543
-            const image = this._media.source as CanvasImageSource;
540
+            const image = media.source as CanvasImageSource;
544 541
             ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
545 542
 
546 543
             // render output image(s)

Laden…
Annuleren
Opslaan