Do not remove tracks when sharing desktop and camera
This commit is contained in:
parent
8c0377bdaf
commit
6effc10d9e
@ -4,13 +4,16 @@ import Peer from 'simple-peer'
|
||||
import { PeerAction } from '../actions/PeerActions'
|
||||
import * as constants from '../constants'
|
||||
import { MediaStreamAction } from '../actions/MediaActions'
|
||||
import { RemoveStreamAction } from '../actions/StreamActions'
|
||||
import { RemoveStreamAction, StreamType } from '../actions/StreamActions'
|
||||
|
||||
export type PeersState = Record<string, Peer.Instance>
|
||||
|
||||
const defaultState: PeersState = {}
|
||||
|
||||
let localStreams: Record<string, MediaStream> = {}
|
||||
let localStreams: Record<StreamType, MediaStream | undefined> = {
|
||||
camera: undefined,
|
||||
desktop: undefined,
|
||||
}
|
||||
|
||||
function handleRemoveStream(
|
||||
state: PeersState,
|
||||
@ -29,6 +32,33 @@ function handleRemoveStream(
|
||||
return state
|
||||
}
|
||||
|
||||
function handleMediaStream(
|
||||
state: PeersState,
|
||||
action: MediaStreamAction,
|
||||
): PeersState {
|
||||
if (action.status !== 'resolved') {
|
||||
return state
|
||||
}
|
||||
const streamType = action.payload.type
|
||||
if (
|
||||
action.payload.userId === constants.ME &&
|
||||
streamType
|
||||
) {
|
||||
forEach(state, peer => {
|
||||
const localStream = localStreams[streamType]
|
||||
localStream && localStream.getTracks().forEach(track => {
|
||||
peer.removeTrack(track, localStream)
|
||||
})
|
||||
const stream = action.payload.stream
|
||||
stream.getTracks().forEach(track => {
|
||||
peer.addTrack(track, stream)
|
||||
})
|
||||
})
|
||||
localStreams[streamType] = action.payload.stream
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
export default function peers(
|
||||
state = defaultState,
|
||||
action: PeerAction | MediaStreamAction | RemoveStreamAction,
|
||||
@ -42,26 +72,16 @@ export default function peers(
|
||||
case constants.PEER_REMOVE:
|
||||
return omit(state, [action.payload.userId])
|
||||
case constants.PEERS_DESTROY:
|
||||
localStreams = {}
|
||||
localStreams = {
|
||||
camera: undefined,
|
||||
desktop: undefined,
|
||||
}
|
||||
forEach(state, peer => peer.destroy())
|
||||
return defaultState
|
||||
case constants.STREAM_REMOVE:
|
||||
return handleRemoveStream(state, action)
|
||||
case constants.MEDIA_STREAM:
|
||||
if (action.status === 'resolved') {
|
||||
forEach(state, peer => {
|
||||
const localStream = localStreams[action.payload.userId]
|
||||
localStream && localStream.getTracks().forEach(track => {
|
||||
peer.removeTrack(track, localStream)
|
||||
})
|
||||
const stream = action.payload.stream
|
||||
stream.getTracks().forEach(track => {
|
||||
peer.addTrack(track, stream)
|
||||
})
|
||||
})
|
||||
localStreams[action.payload.userId] = action.payload.stream
|
||||
}
|
||||
return state
|
||||
return handleMediaStream(state, action)
|
||||
default:
|
||||
return state
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user