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
About me • Masashi Ganeko / @massie_g – working for 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
Topic of today • iOS Browser does not support WebRTC 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
Reduce data size: Video/Image • While using TCP, delay will 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
μ-law algorithm • Irreversible audio compress algorithm • Reduce bits 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
G.722 • Sampling rate:16kHz, Bitrate:48, 56, 64kbps • ADPCM (adapRve 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
Use lamejs for MP3 Compression • To reduce audio size, use MP3 compression • lamejs – h[ps://github.com/zhuker/lamejs • Not streaming, short length MP3 files • Input: Array of 16bit integer – Specify channels(Mono/Stereo), Sampling rate for input data • Output: Array of 8bit integer – Specify bitrate (kbps) for output – Min limit: bitrate >= 32kbps(22kHz input), bitrate >= about 36kbps (24kHz Input) 17
Sample from GitHub var channels = 1, sampleRate = 44100, 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
Tips to use lamejs • Reduce overhead of MP3 data – 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
Inside of MCU Server:Video 23 RTCPeerConnecRon A MediaStream
element element drawImage()
element drawImage() Worker WebSocket (socket.io) DataURL Blob JPEB toBlob() Blob JPEB from iOS to iOS setInterval() Keep drawing with requestAnimaRonFrame()
Inside of MCU Server: Recording 25 MediaStream (Video A+B+C+D) 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
Conclusion • iOS Browser does not support WebRTC yet… So 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