{"id":153,"date":"2016-10-19T14:36:41","date_gmt":"2016-10-19T14:36:41","guid":{"rendered":"https:\/\/testmozilla.wpengine.com\/webrtc\/?p=153"},"modified":"2018-03-23T21:14:47","modified_gmt":"2018-03-23T21:14:47","slug":"warm-up-with-replacetrack","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/","title":{"rendered":"Warm-up with dummy tracks and replaceTrack"},"content":{"rendered":"<p>If you&#8217;ve looked at the <a href=\"http:\/\/w3c.github.io\/webrtc-pc\/\">WebRTC spec<\/a> over the last year, you&#8217;ll find a gap between what it says and what browsers have implemented so far. The big difference is the spec talks about <a href=\"http:\/\/w3c.github.io\/webrtc-pc\/#rtcpeerconnection-interface-extensions\">senders and receivers of tracks<\/a>, whereas most browsers still operate with streams.<\/p>\n<p>Firefox is the only browser so far to have pivoted to tracks, the stuff streams are made of. This opens up exciting possibilities like <code>sender.replaceTrack()<\/code>. It lets you switch out an already-sending track without needing to renegotiate, and still see the change reflected remotely. We&#8217;re not fully done with senders and receivers, and we have more to do (like transports), but replaceTrack works.<\/p>\n<p>The spec, however, goes even further, defining transceivers, a pairing of a sender and a receiver. Even Firefox hasn&#8217;t implemented that yet. The spec <a href=\"http:\/\/w3c.github.io\/webrtc-pc\/#simple-peer-to-peer-example-with-warm-up\">shows an example of connection warm-up<\/a> using transceivers to set up a connection before media is ready. This means you can send media the instant it becomes available. Quite neat, and a benefit of the spec moving to objects that can exist before media is added.<\/p>\n<p>But it turns out we don&#8217;t have to wait for transceivers to do warm-up, which is what we&#8217;re going to show here!<!--more--><\/p>\n<p>You can already read my <a href=\"..\/rtcsimpleconnection\/\">other blog on how to renegotiate quickly over datachannels<\/a>, but that won&#8217;t cut it here. For parity with the spec&#8217;s warm-up example, we must be ready to send media without any additional negotiation at all.<\/p>\n<p>We&#8217;re going to (surprise!) use replaceTrack for this: Basically, we&#8217;ll set up and negotiate a connection using dummy tracks, and replace them with real media later once available. To do this, we first need dummy tracks.<\/p>\n<h3>Dummy tracks<\/h3>\n<p>For all the bells and whistles of WebRTC, you&#8217;d think creating dummy audio and video tracks would be basic. Well, it&#8217;s not, but it&#8217;s doable. Without further ado, here&#8217;s what we have to do:<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/jsfiddle.net\/jib1\/72da8gr3\/embedded\/js,result,html\/\" allow=\"camera; microphone\" width=\"100%\" height=\"390px\" frameborder=\"0\"><\/iframe><\/p>\n<p>Click the &#8220;Result&#8221; tab above to see it working in both Firefox and Chrome.<\/p>\n<p>Tada! Silly, right? Quite the effort for a black rectangle. What a dumb blog this is so far. The crucial part here though is that we now have a stream we didn&#8217;t have to ask permission for from the user. This will help later to connect immediately with usable media ports, on all browsers, even less permissive ones like Firefox, and we avoid asking users for permission twice.<\/p>\n<h3>Connect with dummies, then replaceTrack<\/h3>\n<p>Now that we have our dummies, we can connect with them, which hooks up all the ports needed for media. We have a button to add and switch in real media later. Switch to &#8220;Result&#8221; to see it in action:<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/jsfiddle.net\/jib1\/06a9nu2L\/embedded\/js,result,html\/\" allow=\"camera; microphone\" width=\"100%\" height=\"500px\" frameborder=\"0\"><\/iframe><\/p>\n<p>And there you have it. If you run this in Firefox, you&#8217;ll see a connection being established immediately, with two video elements showing black: the original one on the left, and the &#8220;remote&#8221; one coming out the other end of the peer connection on the right (which may be blank in Chrome), and silence.<\/p>\n<p>Once you click the &#8220;Add Media!&#8221; button, video from your camera should appear in both places, and sound should echo out of the remote end, without any additional negotiation having happened.<\/p>\n<p>If you ran this in Chrome, you might be surprised to see it working there as well! This may be thanks to a <a href=\"https:\/\/jan-ivar.github.io\/replacetrack-polyfill\/replacetrack-polyfill.js\">polyfill<\/a> I wrote special for this example, which means there&#8217;s an extra renegotiation for you, sorry! However, I wrote it such that once Chrome implements <code>replaceTrack<\/code>, you should be able to run the example without renegotiation. If you&#8217;re using Firefox, rest assured that the polyfill is unused.<\/p>\n<p>Once we&#8217;ve implemented transceivers, we&#8217;ll no longer need to do warm-up this way. Still, this is just one of the many uses of replaceTrack. Hopefully you&#8217;ve seen how versatile replaceTrack can be, and you&#8217;ve gotten some insight into the kinds of things the spec envisions you doing with tracks in WebRTC.<\/p>\n","protected":false},"excerpt":{"rendered":"If you&#8217;ve looked at the WebRTC spec over the last year, you&#8217;ll find a gap between what it says and what browsers have implemented so far. The big difference is the spec talks about senders and receivers of tracks, whereas most browsers still operate with streams. Firefox is the only browser so far to have [&hellip;]","protected":false},"author":1399,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[297123,297656,298274],"coauthors":[301098],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Warm-up with dummy tracks and replaceTrack - Advancing WebRTC<\/title>\n<meta name=\"description\" content=\"The WebRTC spec has pivoted to using senders and receivers of tracks. Firefox shows off what can be done with replaceTrack.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Warm-up with dummy tracks and replaceTrack - Advancing WebRTC\" \/>\n<meta property=\"og:description\" content=\"The WebRTC spec has pivoted to using senders and receivers of tracks. Firefox shows off what can be done with replaceTrack.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/\" \/>\n<meta property=\"og:site_name\" content=\"Advancing WebRTC\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-19T14:36:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-03-23T21:14:47+00:00\" \/>\n<meta name=\"author\" content=\"Jan-Ivar Bruaroey\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jan-Ivar Bruaroey\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/\",\"url\":\"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/\",\"name\":\"Warm-up with dummy tracks and replaceTrack - Advancing WebRTC\",\"isPartOf\":{\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/#website\"},\"datePublished\":\"2016-10-19T14:36:41+00:00\",\"dateModified\":\"2018-03-23T21:14:47+00:00\",\"author\":{\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/#\/schema\/person\/f2eb9712b8d85b70aebe1faf24e731fd\"},\"description\":\"The WebRTC spec has pivoted to using senders and receivers of tracks. Firefox shows off what can be done with replaceTrack.\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.mozilla.org\/webrtc\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Warm-up with dummy tracks and replaceTrack\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/#website\",\"url\":\"https:\/\/blog.mozilla.org\/webrtc\/\",\"name\":\"Advancing WebRTC\",\"description\":\"Committed to moving Firefox and WebRTC forward\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.mozilla.org\/webrtc\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/#\/schema\/person\/f2eb9712b8d85b70aebe1faf24e731fd\",\"name\":\"Jan-Ivar Bruaroey\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.mozilla.org\/webrtc\/#\/schema\/person\/image\/5f3d49a61b032619d0d33c4cc7c7433f\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/16d7e05dc9f8a855a02e0796b00aad3f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/16d7e05dc9f8a855a02e0796b00aad3f?s=96&d=mm&r=g\",\"caption\":\"Jan-Ivar Bruaroey\"},\"url\":\"https:\/\/blog.mozilla.org\/webrtc\/author\/jbruaroeymozilla-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Warm-up with dummy tracks and replaceTrack - Advancing WebRTC","description":"The WebRTC spec has pivoted to using senders and receivers of tracks. Firefox shows off what can be done with replaceTrack.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/","og_locale":"en_US","og_type":"article","og_title":"Warm-up with dummy tracks and replaceTrack - Advancing WebRTC","og_description":"The WebRTC spec has pivoted to using senders and receivers of tracks. Firefox shows off what can be done with replaceTrack.","og_url":"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/","og_site_name":"Advancing WebRTC","article_published_time":"2016-10-19T14:36:41+00:00","article_modified_time":"2018-03-23T21:14:47+00:00","author":"Jan-Ivar Bruaroey","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jan-Ivar Bruaroey","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/","url":"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/","name":"Warm-up with dummy tracks and replaceTrack - Advancing WebRTC","isPartOf":{"@id":"https:\/\/blog.mozilla.org\/webrtc\/#website"},"datePublished":"2016-10-19T14:36:41+00:00","dateModified":"2018-03-23T21:14:47+00:00","author":{"@id":"https:\/\/blog.mozilla.org\/webrtc\/#\/schema\/person\/f2eb9712b8d85b70aebe1faf24e731fd"},"description":"The WebRTC spec has pivoted to using senders and receivers of tracks. Firefox shows off what can be done with replaceTrack.","breadcrumb":{"@id":"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.mozilla.org\/webrtc\/warm-up-with-replacetrack\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.mozilla.org\/webrtc\/"},{"@type":"ListItem","position":2,"name":"Warm-up with dummy tracks and replaceTrack"}]},{"@type":"WebSite","@id":"https:\/\/blog.mozilla.org\/webrtc\/#website","url":"https:\/\/blog.mozilla.org\/webrtc\/","name":"Advancing WebRTC","description":"Committed to moving Firefox and WebRTC forward","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.mozilla.org\/webrtc\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.mozilla.org\/webrtc\/#\/schema\/person\/f2eb9712b8d85b70aebe1faf24e731fd","name":"Jan-Ivar Bruaroey","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.mozilla.org\/webrtc\/#\/schema\/person\/image\/5f3d49a61b032619d0d33c4cc7c7433f","url":"https:\/\/secure.gravatar.com\/avatar\/16d7e05dc9f8a855a02e0796b00aad3f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/16d7e05dc9f8a855a02e0796b00aad3f?s=96&d=mm&r=g","caption":"Jan-Ivar Bruaroey"},"url":"https:\/\/blog.mozilla.org\/webrtc\/author\/jbruaroeymozilla-com\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/posts\/153"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/users\/1399"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/tags?post=153"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webrtc\/wp-json\/wp\/v2\/coauthors?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}