-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributor.html
144 lines (122 loc) · 4.6 KB
/
distributor.html
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<body>
<video autoplay width="640" height="480"></video>
<canvas style="display:none;" id="snapshot" width="320" height="240"></canvas>
<div id="wrapper">
<button id="start">start streaming</button>
<button id="stop">stop streaming</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"></script>
<script src="/static/recorder.js"></script>
<script>
$(function() {
// id of this user!!!
var id = 100
var frameRate = 10;
var video = document.querySelector('video');
video.volume = 0;
var canvas = document.querySelector('#snapshot');
var ctx = canvas.getContext('2d');
var videoStream = null;
var socket = io('http://45.55.10.134/');
var videoStreamId = null;
var audioStreamId = null;
var d = null;
var hasGetUserMedia = function() {
return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
var sendAudioToSocket = function(recorder, startedAt) {
recorder.exportWAV(function(blob) {
if (!videoStream) return false;
var audioBlob = new Blob([blob], {type: 'audio/wav'});
var reader = new FileReader;
reader.onload = function() {
var blobUrl = reader.result;
socket.emit('stream', {ts: startedAt, audio: blobUrl});
};
reader.readAsDataURL(audioBlob);
});
}
var sendVideoToSocket = function() {
if (!videoStream) return false;
ctx.drawImage(video, 0, 0, 320, 240);
socket.emit('stream', {ts: new Number(new Date()), video: canvas.toDataURL('image/jpeg', 0.4)});
}
var startAudioStreaming = function() {
if (!videoStream) return false;
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext();
return setInterval(() => {
var audioInput = null,
realAudioInput = null,
inputPoint = null,
audioRecorder = null;
inputPoint = audioContext.createGain();
// Create an AudioNode from the stream.
realAudioInput = audioContext.createMediaStreamSource(videoStream);
audioInput = realAudioInput;
audioInput.connect(inputPoint);
var splitter = audioContext.createChannelSplitter(2);
var merger = audioContext.createChannelMerger(2);
analyserNode = audioContext.createAnalyser();
analyserNode.fftSize = 32;
inputPoint.connect(analyserNode);
audioRecorder = new Recorder(inputPoint);
audioRecorder.record();
var startedAt = new Number(new Date())
setTimeout(() => {
sendAudioToSocket(audioRecorder, startedAt);
}, 2200);
}, 2000)
}
var startVideoStreaming = function() {
return setInterval(() => {
sendVideoToSocket();
}, 1000 / frameRate);
}
var startStreaming = function() {
video.src = window.URL.createObjectURL(videoStream);
socket.emit('start distribution', id);
videoStreamId = startVideoStreaming();
// audioStreamId = startAudioStreaming();
}
var stopStreaming = function() {
clearInterval(videoStreamId);
clearInterval(audioStreamId);
socket.emit('stop distribution', id);
if(_.isFunction(videoStream.stop)) {
videoStream.stop()
}
videoStream = null;
video.src = '';
}
var startDistribution = function() {
if (!hasGetUserMedia()) {
alert("未対応ブラウザです。");
return false;
}
if (videoStream) return false;
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
navigator.getUserMedia({video: true, audio: false}, function(stream) {
videoStream = stream;
startStreaming();
}, function(e) {
console.log('エラー!', e);
});
}
$("#start").click(function() {
startDistribution();
});
$("#stop").click(function() {
stopStreaming();
});
$("video").click(function() {
snapshot();
});
})
</script>
</body>