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 { PeerAction } from '../actions/PeerActions'
|
||||||
import * as constants from '../constants'
|
import * as constants from '../constants'
|
||||||
import { MediaStreamAction } from '../actions/MediaActions'
|
import { MediaStreamAction } from '../actions/MediaActions'
|
||||||
import { RemoveStreamAction } from '../actions/StreamActions'
|
import { RemoveStreamAction, StreamType } from '../actions/StreamActions'
|
||||||
|
|
||||||
export type PeersState = Record<string, Peer.Instance>
|
export type PeersState = Record<string, Peer.Instance>
|
||||||
|
|
||||||
const defaultState: PeersState = {}
|
const defaultState: PeersState = {}
|
||||||
|
|
||||||
let localStreams: Record<string, MediaStream> = {}
|
let localStreams: Record<StreamType, MediaStream | undefined> = {
|
||||||
|
camera: undefined,
|
||||||
|
desktop: undefined,
|
||||||
|
}
|
||||||
|
|
||||||
function handleRemoveStream(
|
function handleRemoveStream(
|
||||||
state: PeersState,
|
state: PeersState,
|
||||||
@ -29,6 +32,33 @@ function handleRemoveStream(
|
|||||||
return state
|
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(
|
export default function peers(
|
||||||
state = defaultState,
|
state = defaultState,
|
||||||
action: PeerAction | MediaStreamAction | RemoveStreamAction,
|
action: PeerAction | MediaStreamAction | RemoveStreamAction,
|
||||||
@ -42,26 +72,16 @@ export default function peers(
|
|||||||
case constants.PEER_REMOVE:
|
case constants.PEER_REMOVE:
|
||||||
return omit(state, [action.payload.userId])
|
return omit(state, [action.payload.userId])
|
||||||
case constants.PEERS_DESTROY:
|
case constants.PEERS_DESTROY:
|
||||||
localStreams = {}
|
localStreams = {
|
||||||
|
camera: undefined,
|
||||||
|
desktop: undefined,
|
||||||
|
}
|
||||||
forEach(state, peer => peer.destroy())
|
forEach(state, peer => peer.destroy())
|
||||||
return defaultState
|
return defaultState
|
||||||
case constants.STREAM_REMOVE:
|
case constants.STREAM_REMOVE:
|
||||||
return handleRemoveStream(state, action)
|
return handleRemoveStream(state, action)
|
||||||
case constants.MEDIA_STREAM:
|
case constants.MEDIA_STREAM:
|
||||||
if (action.status === 'resolved') {
|
return handleMediaStream(state, action)
|
||||||
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
|
|
||||||
default:
|
default:
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user