Skip to content

Commit

Permalink
feat(soudcheck): Allow toggle for devices, add local storage support
Browse files Browse the repository at this point in the history
  • Loading branch information
Prash committed May 10, 2018
1 parent 163d413 commit 4f61690
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/client/config/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import stunServers from './stun'
const config = {
iceServers: stunServers.map(url => ({ urls: url }))
iceServers: stunServers.map(url => ({ urls: url })),
localStorage: {
gumConstraints: 'gum',
auth: 'auth',
code: 'code'
}
}

export default config
4 changes: 3 additions & 1 deletion src/client/redux/ducks/soundcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ const soundcheckReducer = (state = initialState, action) => {
devices: keyBy(action.devices, device => `${device.kind}-${device.deviceId}`),
defaultAudioInputId: findDefaultDevice('audioinput', action.devices),
defaultVideoInputId: findDefaultDevice('videoinput', action.devices),
defaultAudioOutputId: findDefaultDevice('audiooutput', action.devices)
defaultAudioOutputId: findDefaultDevice('audiooutput', action.devices),
audioEnabled: action.audioEnabled,
videoEnabled: action.videoEnabled
}
case UPDATE_SOUNDCHECK_SUCCESS:
return {
Expand Down
10 changes: 9 additions & 1 deletion src/client/redux/sagas/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import { call, put, fork, takeLatest } from 'redux-saga/effects'
import { INITIALIZE, INITIALIZE_SUCCESS, INITIALIZE_FAILED, SHUTDOWN } from '../ducks/room'
import { SOCKET_INITIALIZE, SOCKET_DESTROY } from '../ducks/socket'
import { getUserMedia } from '../../utils/navigator'
import { getLocalStorage } from '../../utils/window'
import config from '../../config'

function* initialize (action) {
try {
const stream = yield call(getUserMedia)
const constraints = getLocalStorage(config.localStorage.gumConstraints)
let stream
if (constraints) {
stream = yield call(getUserMedia, constraints)
} else {
stream = yield call(getUserMedia)
}
yield put({
type: INITIALIZE_SUCCESS,
roomId: action.roomId,
Expand Down
5 changes: 3 additions & 2 deletions src/client/redux/sagas/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AUTHENTICATE_FAILED
} from '../ducks/session'
import axios from 'axios'
import config from '../../config'
import { goToUrl, setLocalStorage } from '../../utils/window'

function* authenticate (action) {
Expand All @@ -14,8 +15,8 @@ function* authenticate (action) {
}))
const auth = response.data.auth
const code = response.data.code || action.code
setLocalStorage('auth', auth)
setLocalStorage('invite-code', code)
setLocalStorage(config.localStorage.auth, auth)
setLocalStorage(config.localStorage.code, code)
yield put({
type: AUTHENTICATE_SUCCESS,
code,
Expand Down
19 changes: 16 additions & 3 deletions src/client/redux/sagas/soundcheck.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { put, fork, takeLatest, call, select } from 'redux-saga/effects'
import {
INITIALIZE_SOUNDCHECK,
INITIALIZE_SOUNDCHECK_SUCCESS,
INITIALIZE_SOUNDCHECK,
INITIALIZE_SOUNDCHECK_SUCCESS,
INITIALIZE_SOUNDCHECK_FAILED,
UPDATE_SOUNDCHECK,
UPDATE_SOUNDCHECK_FAILED,
UPDATE_SOUNDCHECK_SUCCESS
} from '../ducks/soundcheck'
import { INITIALIZE_SUCCESS } from '../ducks/room'
import { getDevices, getUserMedia } from '../../utils/navigator'
import { setLocalStorage, getLocalStorage } from '../../utils/window'
import config from '../../config'

function* soundcheckInitialize () {
try {
const devices = yield getDevices()
const constraints = getLocalStorage(config.localStorage.gumConstraints)
let audioEnabled = true
let videoEnabled = true
if (constraints) {
audioEnabled = constraints.audio && constraints.audio !== false
videoEnabled = constraints.video && constraints.video !== false
}
yield put({
type: INITIALIZE_SOUNDCHECK_SUCCESS,
devices
devices,
audioEnabled,
videoEnabled
})
} catch (err) {
console.log('Error initializing soundcheck')
Expand Down Expand Up @@ -71,11 +82,13 @@ function* soundcheckUpdate ({ audioInput, audioOutput, videoInput, audioEnabled,
console.log('scheck-updatesaga: constraints', constraints)
const stream = yield call(getUserMedia, constraints)
console.log('scheck-updatasaga: stream', stream, roomId, stream.getVideoTracks())
setLocalStorage(config.localStorage.gumConstraints, constraints)
yield put({
type: INITIALIZE_SUCCESS,
roomId,
stream
})
// @todo: TODO: Pass stream updates to all users
} catch (err) {
console.log('Error initializing soundcheck')
console.error(err)
Expand Down
3 changes: 2 additions & 1 deletion src/client/redux/selectors/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSelector } from 'reselect'
import { isTokenValid } from '../../utils/jwt'
import { getLocalStorage } from '../../utils/window'
import config from '../../config'

const authSelector = state => state.session && state.session.auth

Expand All @@ -9,7 +10,7 @@ export const getAuth = createSelector(
auth => {
if (auth) return auth
try {
return JSON.parse(getLocalStorage('auth') || null)
return getLocalStorage(config.localStorage.auth)
} catch (error) {
console.error(error)
}
Expand Down
31 changes: 29 additions & 2 deletions src/client/ui-components/soundcheck/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ class Soundcheck extends Component {
}
}

componentWillReceiveProps (nextProps) {
if (this.props !== nextProps) {
if (this.state.audioEnabled !== nextProps.audioEnabled) {
this.setState({
audioEnabled: nextProps.audioEnabled
})
}
if (this.state.videoEnabled !== nextProps.videoEnabled) {
this.setState({
videoEnabled: nextProps.videoEnabled
})
}
}
}

onDeviceChanged (stateProperty, event) {
event.preventDefault()
this.setState({
Expand All @@ -47,8 +62,8 @@ class Soundcheck extends Component {
return (
<FormGroup>
<FormControl
componentClass="select"
placeholder="Select source"
componentClass='select'
placeholder='Select source'
selected={selectedKey}
onChange={onChange}
>
Expand All @@ -71,6 +86,18 @@ class Soundcheck extends Component {
if (!this.props.devices) return null
return (
<div>
<Row>
<Col md={5}>
Media Type
</Col>
<Col md={5}>
Device
</Col>
<Col md={2}>
On / Off
</Col>
</Row>
<br />
<Row>
<Col md={5}>
Video Input Source
Expand Down
2 changes: 1 addition & 1 deletion src/client/ui-components/soundcheck/soundcheck-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SoundcheckWrapper extends Component {
onHide={this.props.onClose}
>
<Modal.Header closeButton>
Soundcheck
Audio / Video Settings
</Modal.Header>
<Modal.Body>
<Soundcheck
Expand Down
5 changes: 1 addition & 4 deletions src/client/ui-components/video-container/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class VideoContainer extends Component {
this.playStream = this.playStream.bind(this)
}

componentWillReceiveProps (nextProps) {
// console.log('new', nextProps)
}

playStream (event) {
event.preventDefault()
event.target.play()
Expand Down Expand Up @@ -49,6 +45,7 @@ class VideoContainer extends Component {
}}
disableMute={this.props.users[userId].disableMute}
muted={this.props.users[userId].muted}
height={750}
/>
</Col>
))}
Expand Down
2 changes: 2 additions & 0 deletions src/client/ui-components/video/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Button, ButtonGroup } from 'react-bootstrap'
import { getRandomAvatarUrl } from '../../utils/room'
import './index.css'

class VideoPlayer extends Component {
Expand Down Expand Up @@ -44,6 +45,7 @@ class VideoPlayer extends Component {
</span>
}
<video
poster={getRandomAvatarUrl()}
className='video'
ref={el => { this.videoRef = el }}
muted={this.props.muted}
Expand Down
17 changes: 17 additions & 0 deletions src/client/utils/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ import { toLower } from 'lodash'

export const urlSafe = (text) => slug(text)
export const generateName = (addRandomId = true) => urlSafe(toLower(`${faker.hacker.adjective()}-${faker.random.word()}${addRandomId ? `-${cuid.slug()}` : ''}`))

/**
* Get random avatar url to show in video posters
* @param {*} key unique id
* @param {*} features { face: { eyes: [], nose, mouth }, color: HEXWITHOUTHASH}
*/
export const getRandomAvatarUrl = (
key,
features
) => {
const AVATAR_URL_PREFIX = `https://api.adorable.io`
// const ALT_URL_PREFIX = `https://randomuser.me/api/portraits/lego` // lego/[1-9].jpg
if (!features) {
return `${AVATAR_URL_PREFIX}/avatars/285/${key || cuid.slug()}.png`
}
return `${AVATAR_URL_PREFIX}/avatars/face/${features.eyes}/${features.nose}/${features.mouth}/${features.color || 'eeee'}`
}
2 changes: 1 addition & 1 deletion src/client/utils/window.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const getLocalStorage = (key) => window.localStorage.getItem(key)
export const getLocalStorage = (key) => JSON.parse(window.localStorage.getItem(key) || null)
export const setLocalStorage = (key, val) => window.localStorage.setItem(key, JSON.stringify(val))
export const existsInLocalStorage = (key) => window.localStorage.getItem(key) !== undefined
export const clearLocalStorage = (key) => window.localStorage.clear()
Expand Down

0 comments on commit 4f61690

Please sign in to comment.