forked from louiscenter/hypercast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (85 loc) · 2.75 KB
/
index.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var choo = require('choo')
var app = choo()
app.use(function (state, emitter) {
state.broadcast = {}
state.broadcast.active = false
state.broadcast.key = null
state.broadcast.audioOnly = false
state.settings = {}
state.settings.audioDevice = true
state.settings.videoDevice = true
emitter.on('audioOnlyToggle', function() {
state.broadcast.audioOnly = !state.broadcast.audioOnly
emitter.emit('render')
})
emitter.on('settingsToggle', function() {
state.settings.visible = !state.settings.visible
emitter.emit('render')
})
emitter.on('setMediaDevices', function(devices) {
state.settings.devices = devices
emitter.emit('render')
})
emitter.on('setAudioDevice', function(device) {
state.settings.audioDevice = device
emitter.emit('setStream')
})
emitter.on('setVideoDevice', function(device) {
state.settings.videoDevice = device
emitter.emit('setStream')
})
emitter.on('setViewerTemplate', function(viewerTemplate) {
state.settings.viewerTemplate = viewerTemplate
emitter.emit('render')
})
emitter.on('broadcast:start', function (key) {
state.broadcast.peerCount = 0
state.broadcast.active = true
state.broadcast.key = key
emitter.emit('render')
})
emitter.on('broadcast:peer', function (peerCount) {
state.broadcast.peerCount = peerCount
emitter.emit('render')
})
emitter.on('broadcast:stop', function (key) {
state.broadcast.peerCount = 0
state.broadcast.active = false
state.broadcast.key = null
emitter.emit('render')
})
emitter.on('setStream', function(audioCtx, analyser) {
audioCtx = audioCtx || window.audioCtx
analyser = analyser || window.analyser
navigator.mediaDevices.getUserMedia({
audio: typeof state.settings.videoDevice === 'string' && state.settings.videoDevice.includes('screen')
? false
: state.settings.audioDevice,
video: typeof state.settings.videoDevice === 'string' && state.settings.videoDevice.includes('screen')
? {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: state.settings.videoDevice
}
}
: typeof state.settings.videoDevice === 'boolean'
? state.settings.videoDevice
: {
deviceId: state.settings.videoDevice
}
})
.then(function(stream) {
var elPreview = document.getElementById('preview')
elPreview.srcObject = stream
elPreview.muted = true
if (audioCtx) {
audioCtx.createMediaStreamSource(stream)
.connect(analyser);
}
window.stream = stream
})
emitter.emit('render')
})
})
app.route('/', require('./templates/home'))
document.body.appendChild(app.start())