-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbroadcast.js
68 lines (58 loc) · 1.94 KB
/
broadcast.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
var html = require('choo/html')
var signalhub = require('signalhubws')
var swarm = require('webrtc-swarm')
var getUserMedia = require('getusermedia')
var recorder = require('media-recorder-stream')
var hypercore = require('hypercore')
var ram = require('random-access-memory')
var pump = require('pump')
var cluster = require('webm-cluster-stream')
var mimeType = require('./lib/getMimeType')(window.MediaRecorder.isTypeSupported)
var config = require('./config')
module.exports = broadcast
function broadcast (state, emit) {
return html`
<div>
<div style="margin-bottom: 12px">
<video id="preview" controls autoplay></video>
</div>
<div style="margin-bottom: 12px">
<button onclick=${startBroadcasting}>
Start broadcast
</button>
</div>
<div>
Key: ${state.broadcast.key ? state.broadcast.key : 'waiting...'}
</div>
</div>
`
function startBroadcasting () {
getUserMedia(function (err, stream) {
if (err) return console.log('😵 getUserMedia error', err)
var elPreview = document.getElementById('preview')
elPreview.muted = true
elPreview.srcObject = stream
var mediaRecorder = recorder(stream, {
mimeType: mimeType,
videoBitsPerSecond: 200000,
audioBitsPerSecond: 32000
})
var feed = hypercore(ram)
feed.on('ready', function () {
var key = feed.key.toString('hex')
var discoveryKey = feed.discoveryKey.toString('hex')
emit('broadcast:key:set', key)
var hub = signalhub(discoveryKey, config.signalhub)
var sw = swarm(hub)
sw.on('peer', function (peer, id) {
pump(peer, feed.replicate({ live: true, encrypt: false }), peer)
})
})
var mediaStream = pump(mediaRecorder, cluster())
mediaStream.on('data', function (data) {
console.log('⚡️ appending to broadcast:', data)
feed.append(data)
})
})
}
}