-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
321 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
prefer-workspace-packages=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { hasValue } from '@casual-simulation/aux-common'; | ||
import { appManager } from './AppManager'; | ||
import { ParsedCasualOSUrl } from './UrlUtils'; | ||
|
||
/** | ||
* Gets the media for the given CasualOS URL. | ||
* Returns null if the media could not be found. | ||
* @param url The URL that should be used to get the media. | ||
*/ | ||
export async function getMediaForCasualOSUrl( | ||
url: ParsedCasualOSUrl | null | ||
): Promise<MediaProvider> { | ||
if (url && url.type === 'camera-feed') { | ||
try { | ||
const media = await window.navigator.mediaDevices.getUserMedia({ | ||
audio: false, | ||
video: { | ||
// Use the user specified one if specified. | ||
// Otherwise default to environment. | ||
facingMode: hasValue(url.camera) | ||
? { | ||
exact: | ||
url.camera === 'front' | ||
? 'user' | ||
: 'environment', | ||
} | ||
: { ideal: 'environment' }, | ||
}, | ||
}); | ||
|
||
return media; | ||
} catch (err) { | ||
console.warn( | ||
'[Game] Unable to get camera feed for background.', | ||
err | ||
); | ||
return; | ||
} | ||
} else if (url && url.type === 'video-element') { | ||
for (let sim of appManager.simulationManager.simulations.values()) { | ||
let stream = sim.livekit.getMediaByAddress(url.address); | ||
if (stream) { | ||
return stream; | ||
} | ||
} | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { parseCasualOSUrl } from './UrlUtils'; | ||
|
||
describe('parseCasualOSUrl()', () => { | ||
it('should return an object describing the CasualOS URL', () => { | ||
expect(parseCasualOSUrl('casualos://camera-feed')).toEqual({ | ||
type: 'camera-feed', | ||
}); | ||
|
||
expect(parseCasualOSUrl('casualos://camera-feed/front')).toEqual({ | ||
type: 'camera-feed', | ||
camera: 'front', | ||
}); | ||
|
||
expect(parseCasualOSUrl('casualos://camera-feed/rear')).toEqual({ | ||
type: 'camera-feed', | ||
camera: 'rear', | ||
}); | ||
|
||
expect(parseCasualOSUrl('casualos://camera-feed/other')).toEqual({ | ||
type: 'camera-feed', | ||
}); | ||
|
||
expect( | ||
parseCasualOSUrl('casualos://video-element/uuid-123-abc') | ||
).toEqual({ | ||
type: 'video-element', | ||
address: 'casualos://video-element/uuid-123-abc', | ||
}); | ||
}); | ||
|
||
// See https://bugs.chromium.org/p/chromium/issues/detail?id=869291 | ||
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1374505 | ||
it('should support Chrome and Firefox URL results', () => { | ||
// How Chrome/Firefox parse casualos://camera-feed | ||
expect( | ||
parseCasualOSUrl({ | ||
protocol: 'casualos:', | ||
hostname: '', | ||
host: '', | ||
pathname: '//camera-feed', | ||
}) | ||
).toEqual({ | ||
type: 'camera-feed', | ||
}); | ||
|
||
// How Chrome/Firefox parse casualos://camera-feed/front | ||
expect( | ||
parseCasualOSUrl({ | ||
protocol: 'casualos:', | ||
hostname: '', | ||
host: '', | ||
pathname: '//camera-feed/front', | ||
}) | ||
).toEqual({ | ||
type: 'camera-feed', | ||
camera: 'front', | ||
}); | ||
|
||
// How Chrome/Firefox parse casualos://camera-feed/rear | ||
expect( | ||
parseCasualOSUrl({ | ||
protocol: 'casualos:', | ||
hostname: '', | ||
host: '', | ||
pathname: '//camera-feed/rear', | ||
}) | ||
).toEqual({ | ||
type: 'camera-feed', | ||
camera: 'rear', | ||
}); | ||
|
||
// How Chrome/Firefox parse casualos://video-element/uuid-123-abc | ||
expect( | ||
parseCasualOSUrl({ | ||
protocol: 'casualos:', | ||
hostname: '', | ||
host: '', | ||
pathname: '//video-element/uuid-123-abc', | ||
href: 'casualos://video-element/uuid-123-abc', | ||
}) | ||
).toEqual({ | ||
type: 'video-element', | ||
address: 'casualos://video-element/uuid-123-abc', | ||
}); | ||
}); | ||
|
||
it('should return null if given a non CasualOS URL', () => { | ||
expect(parseCasualOSUrl('http://example.com')).toBe(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { CameraType } from '@casual-simulation/aux-common'; | ||
|
||
export type ParsedCasualOSUrl = CasualOSCameraFeedUrl | CasualOSVideoElementUrl; | ||
|
||
export interface CasualOSCameraFeedUrl { | ||
type: 'camera-feed'; | ||
camera?: CameraType; | ||
} | ||
|
||
export interface CasualOSVideoElementUrl { | ||
type: 'video-element'; | ||
address: string; | ||
} | ||
|
||
/** | ||
* Parses special CasualOS URLs into an object that indicates what it should be used for. | ||
* Currently only supports "casualos://camera-feed/{rear|front}". | ||
* Returns null if the URL has no special CasualOS meaning. | ||
* @param url The URL that should be parsed. | ||
* @returns | ||
*/ | ||
export function parseCasualOSUrl( | ||
url: string | Partial<URL> | ||
): ParsedCasualOSUrl { | ||
try { | ||
const uri = typeof url === 'object' ? url : new URL(url); | ||
if (uri.protocol !== 'casualos:') { | ||
return null; | ||
} | ||
|
||
if (uri.hostname === 'camera-feed') { | ||
let camera: CameraType; | ||
if (uri.pathname === '/front') { | ||
camera = 'front'; | ||
} else if (uri.pathname === '/rear') { | ||
camera = 'rear'; | ||
} | ||
|
||
let result: ParsedCasualOSUrl = { | ||
type: 'camera-feed', | ||
}; | ||
|
||
if (camera) { | ||
result.camera = camera; | ||
} | ||
|
||
return result; | ||
} else if (uri.hostname === 'video-element') { | ||
let result: ParsedCasualOSUrl = { | ||
type: 'video-element', | ||
address: uri.href, | ||
}; | ||
|
||
return result; | ||
} else if (uri.hostname === '') { | ||
// Chrome/Firefox | ||
// See https://bugs.chromium.org/p/chromium/issues/detail?id=869291 and https://bugzilla.mozilla.org/show_bug.cgi?id=1374505 | ||
if (uri.pathname.startsWith('//camera-feed')) { | ||
let path = uri.pathname.slice('//camera-feed'.length); | ||
let camera: CameraType; | ||
if (path === '/front') { | ||
camera = 'front'; | ||
} else if (path === '/rear') { | ||
camera = 'rear'; | ||
} | ||
|
||
let result: ParsedCasualOSUrl = { | ||
type: 'camera-feed', | ||
}; | ||
|
||
if (camera) { | ||
result.camera = camera; | ||
} | ||
|
||
return result; | ||
} else if (uri.pathname.startsWith('//video-element')) { | ||
let result: ParsedCasualOSUrl = { | ||
type: 'video-element', | ||
address: uri.href, | ||
}; | ||
|
||
return result; | ||
} | ||
} | ||
|
||
return null; | ||
} catch { | ||
return null; | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.