File size: 1,307 Bytes
87337b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use client"

import * as React from "react"
import {
  ICameraVideoTrack,
  ILocalVideoTrack,
  IMicrophoneAudioTrack,
  VideoPlayerConfig,
} from "agora-rtc-sdk-ng"

export interface StreamPlayerProps {
  videoTrack?: ICameraVideoTrack | ILocalVideoTrack
  audioTrack?: IMicrophoneAudioTrack
  style?: React.CSSProperties
  fit?: "cover" | "contain" | "fill"
  onClick?: () => void
  mute?: boolean
}

export const LocalStreamPlayer = React.forwardRef(
  (props: StreamPlayerProps, ref) => {
    const {
      videoTrack,
      audioTrack,
      mute = false,
      style = {},
      fit = "cover",
      onClick = () => {},
    } = props
    const vidDiv = React.useRef(null)

    React.useLayoutEffect(() => {
      const config = { fit } as VideoPlayerConfig
      if (mute) {
        videoTrack?.stop()
      } else {
        if (!videoTrack?.isPlaying) {
          videoTrack?.play(vidDiv.current!, config)
        }
      }

      return () => {
        videoTrack?.stop()
      }
    }, [videoTrack, fit, mute])

    // local audio track need not to be played
    // useLayoutEffect(() => {}, [audioTrack, localAudioMute])

    return (
      <div
        className="relative h-full w-full overflow-hidden"
        style={style}
        ref={vidDiv}
        onClick={onClick}
      />
    )
  },
)