<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: janaka1984</title>
    <description>The latest articles on DEV Community by janaka1984 (@janaka1984).</description>
    <link>https://dev.to/janaka1984</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F406196%2F9f17eb11-97bb-4152-a24a-7c31b8542a2c.jpeg</url>
      <title>DEV Community: janaka1984</title>
      <link>https://dev.to/janaka1984</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/janaka1984"/>
    <language>en</language>
    <item>
      <title>WebRTC video streaming is not in chrome</title>
      <dc:creator>janaka1984</dc:creator>
      <pubDate>Wed, 03 May 2023 13:31:03 +0000</pubDate>
      <link>https://dev.to/janaka1984/webrtc-video-streaming-is-not-in-chrome-4ieg</link>
      <guid>https://dev.to/janaka1984/webrtc-video-streaming-is-not-in-chrome-4ieg</guid>
      <description>&lt;p&gt;why following code can not stream video on chrome&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; // peer connection
&amp;gt; var pc = null;
&amp;gt; // data channel
&amp;gt; var dc = null, dcInterval = null;
&amp;gt; // Check if the preference is currently disabled
&amp;gt; // if (!navigator.userAgent.includes("Firefox/") || !Services.prefs.getBoolPref("media.devices.insecure.enabled")) {
&amp;gt; //     // If using Firefox or the preference is not enabled, set it to true
&amp;gt; //     Services.prefs.setBoolPref("media.devices.insecure.enabled", true);
&amp;gt; //   }
&amp;gt; // // Check if the preference is currently disabled
&amp;gt; // if (!navigator.userAgent.includes("Firefox/") || !Services.prefs.getBoolPref("media.getusermedia.insecure.enabled")) {
&amp;gt; //     // If using Firefox or the preference is not enabled, set it to true
&amp;gt; //     Services.prefs.setBoolPref("media.getusermedia.insecure.enabled", true);
&amp;gt; //   }
&amp;gt; function createPeerConnection() {
&amp;gt;     // var config = {
&amp;gt;     //     sdpSemantics: 'unified-plan'
&amp;gt;     // };
&amp;gt;     var config = {
&amp;gt;         iceServers: [
&amp;gt;           { urls: "stun:x.x.x.x:443" },
&amp;gt;           { 
&amp;gt;             urls: "turn:x.x.x.x:443",
&amp;gt;             username: "xx",
&amp;gt;             credential: "xx@xx#xx"
&amp;gt;           }
&amp;gt;         ]
&amp;gt;     };
&amp;gt;     pc = new RTCPeerConnection(config);
&amp;gt;     
&amp;gt;     // connect audio / video
&amp;gt;     pc.addEventListener('track', function(evt) {
&amp;gt;         if (evt.track.kind == 'video')
&amp;gt;             document.getElementById('video').srcObject = evt.streams[0];
&amp;gt;     });
&amp;gt;     return pc;
&amp;gt; }
&amp;gt; function negotiate() {
&amp;gt;     return pc.createOffer().then(function(offer) {
&amp;gt;         return pc.setLocalDescription(offer);
&amp;gt;     }).then(function() {
&amp;gt;         // wait for ICE gathering to complete
&amp;gt;         return new Promise(function(resolve) {
&amp;gt;             if (pc.iceGatheringState === 'complete') {
&amp;gt;                 resolve();
&amp;gt;             } else {
&amp;gt;                 function checkState() {
&amp;gt;                     if (pc.iceGatheringState === 'complete') {
&amp;gt;                         pc.removeEventListener('icegatheringstatechange', checkState);
&amp;gt;                         resolve();
&amp;gt;                     }
&amp;gt;                 }
&amp;gt;                 pc.addEventListener('icegatheringstatechange', checkState);
&amp;gt;             }
&amp;gt;         });
&amp;gt;     }).then(function() {
&amp;gt;         var offer = pc.localDescription;
&amp;gt;         var csrftoken = getCookie('csrftoken');
&amp;gt;         var codec;
&amp;gt;         codec = "default"; 
&amp;gt;         if (codec !== 'default') {
&amp;gt;             offer.sdp = sdpFilterCodec('video', codec, offer.sdp);
&amp;gt;         }
&amp;gt;         return fetch('http://127.0.0.1:8081/liveness_detect', {            
&amp;gt;             body: JSON.stringify({
&amp;gt;                 sdp: offer.sdp,
&amp;gt;                 type: offer.type,
&amp;gt;                 user_id: Math.random().toString(36).substring(2,7),
&amp;gt;                 video_transform:"liveness"
&amp;gt;             }),
&amp;gt;             headers: {
&amp;gt;                 'Content-Type': 'application/json',
&amp;gt;                 'X-CSRFToken':  csrftoken,
&amp;gt;                 "Access-Control-Allow-Origin": "http://127.0.0.1:8081",
&amp;gt;             },
&amp;gt;             method: 'POST',
&amp;gt;             mode: "cors"
&amp;gt;         });
&amp;gt;     }).then(function(response) {
&amp;gt;         return response.json();
&amp;gt;     }).then(function(answer) {
&amp;gt;         // return pc.setRemoteDescription(answer);
&amp;gt;         return pc.setRemoteDescription(new RTCSessionDescription(data));
&amp;gt;     }).catch(function(e) {
&amp;gt;         alert(e);
&amp;gt;     });
&amp;gt; }
&amp;gt; function start() {
&amp;gt;     document.getElementById('start').style.display = 'none';
&amp;gt;     pc = createPeerConnection();
&amp;gt;     var time_start = null;
&amp;gt;     function current_stamp() {
&amp;gt;         if (time_start === null) {
&amp;gt;             time_start = new Date().getTime();
&amp;gt;             return 0;
&amp;gt;         } else {
&amp;gt;             return new Date().getTime() - time_start;
&amp;gt;         }
&amp;gt;     }
&amp;gt;     if (true){
&amp;gt;         var parameters = JSON.parse('{"ordered": true}');
&amp;gt;         dc = pc.createDataChannel('chat', parameters);
&amp;gt;         dc.onclose = function() {
&amp;gt;             clearInterval(dcInterval);
&amp;gt;         };
&amp;gt;         dc.onopen = function() {
&amp;gt;             dcInterval = setInterval(function() {
&amp;gt;                 var message = 'ping ' + current_stamp();
&amp;gt;                 dc.send(message);
&amp;gt;             }, 1000);
&amp;gt;         };
&amp;gt;         dc.onmessage = function(evt) {
&amp;gt;             
&amp;gt;             if (evt.data.substring(0, 4) === 'pong') {
&amp;gt;                 var elapsed_ms = current_stamp() - parseInt(evt.data.substring(5), 10);
&amp;gt;                 document.getElementById('msg').textContent = evt.data.split('|')[1].trim();
&amp;gt;                 console.log("******************** datachannel_stop_199",evt.data.split('|')[1].trim())
&amp;gt;                 var is_stop = evt.data.split('|')[2].trim();
&amp;gt;                 if (is_stop == '1'){
&amp;gt;                     stop();
&amp;gt;                     console.log("******************** datachannel_stop",evt.data.split('|')[1].trim())
&amp;gt;                     document.getElementById('start').style.display = 'block';
&amp;gt;                 }
&amp;gt;             }
&amp;gt;         };
&amp;gt;     }
&amp;gt;     var constraints = {
&amp;gt;         audio: false,
&amp;gt;         video: false
&amp;gt;     };
&amp;gt;     if (true){
&amp;gt;         var resolution ="";
&amp;gt;         if (resolution) {
&amp;gt;             resolution = resolution.split('x');
&amp;gt;             constraints.video = {
&amp;gt;                 width: parseInt(resolution[0], 0),
&amp;gt;                 height: parseInt(resolution[1], 0)
&amp;gt;             };
&amp;gt;         } else {
&amp;gt;             // constraints.video = true;
&amp;gt;             constraints.video = {
&amp;gt;                 width: 320, // 23/04/2023 - gagani
&amp;gt;                 height: 240
&amp;gt;             };
&amp;gt;             
&amp;gt;         }
&amp;gt;     }
&amp;gt;     if (constraints.audio || constraints.video) {
&amp;gt;         if (constraints.video) {
&amp;gt;             document.getElementById('media').style.display = 'block';
&amp;gt;         }
&amp;gt;         navigator.mediaDevices.getUserMedia(constraints)
&amp;gt;         .then(function(stream) {
&amp;gt;             stream.getTracks().forEach(function(track) {
&amp;gt;                 pc.addTrack(track, stream);
&amp;gt;             });
&amp;gt;             return negotiate();
&amp;gt;         }, function(err) {
&amp;gt;             alert('Could not acquire media: ' + err);
&amp;gt;         });
&amp;gt;             
&amp;gt;     } else {
&amp;gt;         negotiate();
&amp;gt;     }
&amp;gt;     document.getElementById('stop').style.display = 'inline-block';
&amp;gt; }
&amp;gt; function stop() {
&amp;gt;     document.getElementById('stop').style.display = 'none';
&amp;gt;     console.log("-------------call stop function ----------------------")
&amp;gt;     // close data channel
&amp;gt; //     if (dc) {
&amp;gt; //         dc.close();
&amp;gt; //         dc = null; // 24/04/2023 - gagani
&amp;gt; //     }
&amp;gt; // // 24/04/2023 - gagani
&amp;gt; //     if (pc){
&amp;gt; //         pc.close();
&amp;gt; //         pc = null;
&amp;gt; //     }
&amp;gt;     // close transceivers
&amp;gt;     if (pc.getTransceivers) {
&amp;gt;         pc.getTransceivers().forEach(function(transceiver) {
&amp;gt;             if (transceiver.stop) {
&amp;gt;                 transceiver.stop();
&amp;gt;             }
&amp;gt;         });
&amp;gt;     }
&amp;gt;     // close local audio / video
&amp;gt;     pc.getSenders().forEach(function(sender) {
&amp;gt;         sender.track.stop();
&amp;gt;     });
&amp;gt;     // close peer connection
&amp;gt;     setTimeout(function() {
&amp;gt;         pc.close();
&amp;gt;     }, 500);
&amp;gt; }
&amp;gt; function sdpFilterCodec(kind, codec, realSdp) {
&amp;gt;     var allowed = []
&amp;gt;     var rtxRegex = new RegExp('a=fmtp:(\\d+) apt=(\\d+)\r$');
&amp;gt;     var codecRegex = new RegExp('a=rtpmap:([0-9]+) ' + escapeRegExp(codec))
&amp;gt;     var videoRegex = new RegExp('(m=' + kind + ' .*?)( ([0-9]+))*\\s*$')
&amp;gt;     
&amp;gt;     var lines = realSdp.split('\n');
&amp;gt;     var isKind = false;
&amp;gt;     for (var i = 0; i &amp;lt; lines.length; i++) {
&amp;gt;         if (lines[i].startsWith('m=' + kind + ' ')) {
&amp;gt;             isKind = true;
&amp;gt;         } else if (lines[i].startsWith('m=')) {
&amp;gt;             isKind = false;
&amp;gt;         }
&amp;gt;         if (isKind) {
&amp;gt;             var match = lines[i].match(codecRegex);
&amp;gt;             if (match) {
&amp;gt;                 allowed.push(parseInt(match[1]));
&amp;gt;             }
&amp;gt;             match = lines[i].match(rtxRegex);
&amp;gt;             if (match &amp;amp;&amp;amp; allowed.includes(parseInt(match[2]))) {
&amp;gt;                 allowed.push(parseInt(match[1]));
&amp;gt;             }
&amp;gt;         }
&amp;gt;     }
&amp;gt;     var skipRegex = 'a=(fmtp|rtcp-fb|rtpmap):([0-9]+)';
&amp;gt;     var sdp = '';
&amp;gt;     isKind = false;
&amp;gt;     for (var i = 0; i &amp;lt; lines.length; i++) {
&amp;gt;         if (lines[i].startsWith('m=' + kind + ' ')) {
&amp;gt;             isKind = true;
&amp;gt;         } else if (lines[i].startsWith('m=')) {
&amp;gt;             isKind = false;
&amp;gt;         }
&amp;gt;         if (isKind) {
&amp;gt;             var skipMatch = lines[i].match(skipRegex);
&amp;gt;             if (skipMatch &amp;amp;&amp;amp; !allowed.includes(parseInt(skipMatch[2]))) {
&amp;gt;                 continue;
&amp;gt;             } else if (lines[i].match(videoRegex)) {
&amp;gt;                 sdp += lines[i].replace(videoRegex, '$1 ' + allowed.join(' ')) + '\n';
&amp;gt;             } else {
&amp;gt;                 sdp += lines[i] + '\n';
&amp;gt;             }
&amp;gt;         } else {
&amp;gt;             sdp += lines[i] + '\n';
&amp;gt;         }
&amp;gt;     }
&amp;gt;     return sdp;
&amp;gt; }
&amp;gt; function escapeRegExp(string) {
&amp;gt;     return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&amp;amp;'); // $&amp;amp; means the whole matched string
&amp;gt; }
&amp;gt; function getCookie(name) {
&amp;gt;     let cookieValue = null;
&amp;gt;     if (document.cookie &amp;amp;&amp;amp; document.cookie !== '') {
&amp;gt;         const cookies = document.cookie.split(';');
&amp;gt;         for (let i = 0; i &amp;lt; cookies.length; i++) {
&amp;gt;             const cookie = cookies[i].trim();
&amp;gt;             // Does this cookie string begin with the name we want?
&amp;gt;             if (cookie.substring(0, name.length + 1) === (name + '=')) {
&amp;gt;                 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
&amp;gt;                 break;
&amp;gt;             }
&amp;gt;         }
&amp;gt;     }
&amp;gt;     return cookieValue;
&amp;gt; }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
