simon | daae910 | 2022-12-02 16:51:31 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import { forwardRef, useEffect, useRef, VideoHTMLAttributes } from 'react'; |
| 19 | |
| 20 | import { VideoElementWithSinkId } from '../utils/utils'; |
| 21 | |
| 22 | export type VideoStreamProps = Partial<VideoHTMLAttributes<VideoElementWithSinkId>> & { |
| 23 | stream: MediaStream | undefined; |
| 24 | audioOutDeviceId?: string; |
| 25 | }; |
| 26 | |
| 27 | const VideoStream = forwardRef<VideoElementWithSinkId, VideoStreamProps>( |
| 28 | ({ stream, audioOutDeviceId, ...props }, ref) => { |
| 29 | const videoRef = useRef<VideoElementWithSinkId | null>(null); |
| 30 | |
| 31 | useEffect(() => { |
| 32 | if (!ref) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | if (typeof ref === 'function') { |
| 37 | ref(videoRef.current); |
| 38 | } else { |
| 39 | ref.current = videoRef.current; |
| 40 | } |
| 41 | }, [ref]); |
| 42 | |
| 43 | useEffect(() => { |
| 44 | if (stream && videoRef.current) { |
| 45 | videoRef.current.srcObject = stream; |
| 46 | } |
| 47 | }, [stream, videoRef]); |
| 48 | |
| 49 | useEffect(() => { |
| 50 | if (!audioOutDeviceId) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (videoRef.current?.setSinkId) { |
| 55 | // This only work on chrome and other browsers that support `setSinkId` |
| 56 | // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility |
| 57 | videoRef.current.setSinkId(audioOutDeviceId); |
| 58 | } |
| 59 | }, [audioOutDeviceId, videoRef]); |
| 60 | |
| 61 | return <video ref={videoRef} autoPlay {...props} />; |
| 62 | } |
| 63 | ); |
| 64 | export default VideoStream; |