Building WebRTC - iOS Safari Gateway on PC Web Browser, using webaudio, canvas, websocket.
This is a presentation for WebRTC Meetup Tokyo 14 at Mar. 23, 2017
a research team of INFOCOM CORPORATION • WriRng a series of WebRTC on HTML5Experts.jp (HTML5 tech site) – h[ps://html5experts.jp/series/webrtc2016/ • English PresentaRon for WebRTC (2013-2017) – h[ps://speakerdeck.com/mganeko • Japanese PresentaRon for WebRTC (2013-2017) – h[p://www.slideshare.net/mganeko 2
yet – Webkit is on the way, but Safari may take much longer • It is possible to communicate today, with other features – Canvas, Web Audio, WebSocket • Thanks for preceding trials: – Audio: "Using WebAudio for pseudo WebRTC" @leader22 • h[p://leader22.github.io/slides/webaudio_tokyo-1/ – Video: Snapshoto from "Introducing WebRTC SFU Sora" @voluntas • h[ps://gist.github.com/voluntas/0d6621d15947a24e710b0610093a5d20 3
occur on high traffic condiRon – It is not preferable for real-Rme video/audio communicaRon – Too keep enough margin, data should be reduced • Image 240x180 / sec – RGB RAW Image … 240x180x24bit = 1 Mbit, 129 kb – PNG … about 760 kbit, 96 kb – JPEG … about 110 kbit, 14 kb – → reduce frequency to once per 2 sec. … 56 kbit/sec 8
of each sample • Used in G.711 for VoIP – G.711: Sampling rate 8kHz → bitrate 64kbps • Wikipedia – h[ps://en.wikipedia.org/wiki/G.711 – PCMU … Use μ-law in North America and Japan – PCMA … Use A-law in Europe • 22kHz μ-law is enough for WiFi, but not enough for mobile • Ex) Trying primiRve audio compression with WebAudio (Japanese lang.) – h[p://qiita.com/massie_g/items/49183a03b015b9ea27eb 12
differenRal pulse code modulaRon) • Compression algorithm – Calculate diff of previous sample • Should be small value (than sample) – Use logarithm for difference • Fine for small value • Rough for big value 16 Linear PCM ADPCM ケータイWatch ケータイ用語の基礎知識より h[p://k-tai.watch.impress.co.jp/cda/arRcle/keyword/2936.html
kbps = 128; // mono, 44.1kHz, 128kbps var mp3encoder = new lamejs.Mp3Encoder(channels, sampleRate, kbps); var sampleBlockSize = 1152; // be[er to use 576 * n var samples = new Int16Array(44100); // 1sec sample var mp3Data = []; for (var i = 0; i < samples.length; i += sampleBlockSize) { sampleChunk = samples.subarray(i, i + sampleBlockSize); var mp3buf = mp3encoder.encodeBuffer(sampleChunk); if (mp3buf.length > 0) { mp3Data.push(mp3buf); } } var mp3buf = mp3encoder.flush(); //finish wriRng mp3 if (mp3buf.length > 0) { mp3Data.push(new Int8Array(mp3buf)); } 18
– Header + Tag = 132byte – Longer block of audio to convert – Max limit of block for WebAudio ScriptProcessor : 16384 sample for each event • à 370 ms (44.1kHz), 340 ms (48kHz) 19 Header Compressed data MP3 tag 4byte 128byte 1500 - 2000 byte • Reduce sample before compress: 44.1kHz→22.05kHz, 48kHz→24kHzに • Consider Min limit for output bitreate • bitrate >= 32kbps(22kHz input), bitrate >= about 36kbps (24kHz Input) • Use WebWorker for compress … compression Rme is 50ms - 100ms / block
<canvas> element drawImage() <img> element drawImage() Worker WebSocket (socket.io) DataURL Blob JPEB toBlob() Blob JPEB from iOS to iOS setInterval() Keep drawing with requestAnimaRonFrame()
element Same as sending to remote peers Web Audio API MediaStream (Audio A+B+C) Mix all audio for recording videoTracks[] MediaStreamTrack MediaStreamTrack aidioTracks[] MediaStream new MediaStream() MediaStream.addTrack() MediaStream.addTrack() MediaRecorder WebM
What? – The GOAL is not WebRTC, but "CommunicaRon" with iOS user • Try everything what you have now! – Most advantage or "Web"RTC is flexibility to combine with other Web features, such as: – Canvas, WebAudio, WebSocket • It was interesRng to follow history of audio codecs 26