[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [plasma-browser-integration] extension: Wait for duration to be known before registering player
From:       Kai Uwe Broulik <null () kde ! org>
Date:       2018-09-24 11:34:55
Message-ID: E1g4P8h-0006Oc-K1 () code ! kde ! org
[Download RAW message or body]

Git commit bc4dad725bbbec6356f9e4b7435e24f1e429e545 by Kai Uwe Broulik.
Committed on 24/09/2018 at 11:34.
Pushed by broulik into branch 'master'.

Wait for duration to be known before registering player

A player might emit a play signal before its duration is known.
We want to avoid controlling short sounds (e.g. chat notifications) but I found the \
duration often is NaN on invocation when the sound is loaded for the first time.
Defer activating the player until we get a durationchange event for that player

CHANGELOG: Improved algorithm to ignore short sounds, such as chat notifications

Differential Revision: https://phabricator.kde.org/D15724

M  +22   -2    extension/content-script.js

https://commits.kde.org/plasma-browser-integration/bc4dad725bbbec6356f9e4b7435e24f1e429e545


diff --git a/extension/content-script.js b/extension/content-script.js
index 403ece9..3bcd84e 100644
--- a/extension/content-script.js
+++ b/extension/content-script.js
@@ -102,6 +102,9 @@ var mediaSessionsTransferDivId \
='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/  var mediaSessionsClassName = "f" + \
mediaSessionsTransferDivId.replace(/-/g, "");  
 var activePlayer;
+// When a player has no duration yet, we'll wait for it becoming known
+// to determine whether to ignore it (short sound) or make it active
+var pendingActivePlayer;
 var playerMetadata = {};
 var playerCallbacks = [];
 
@@ -219,9 +222,19 @@ function playerPaused(player) {
 }
 
 function setPlayerActive(player) {
+    if (isNaN(player.duration)) {
+        // Ignore this player for now until we know a duration
+        // In durationchange event handler we'll check for this and end up here \
again +        pendingActivePlayer = player;
+        return;
+    }
+
+    pendingActivePlayer = undefined;
+
     // Ignore short sounds, they are most likely a chat notification sound
-    // but still allow when undetermined (e.g. video stream)
-    if (!isNaN(player.duration) && player.duration > 0 && player.duration < 5) {
+    // A stream has a duration of Infinity
+    // Note that "NaN" is also not finite but we already returned earlier for that
+    if (isFinite(player.duration) && player.duration > 0 && player.duration < 5) {
         return;
     }
 
@@ -243,6 +256,7 @@ function setPlayerActive(player) {
 
 function sendPlayerGone() {
     activePlayer = undefined;
+    pendingActivePlayer = undefined;
     playerMetadata = {};
     playerCallbacks = [];
     sendMessage("mpris", "gone");
@@ -308,6 +322,12 @@ function registerPlayer(player) {
 
     // TODO use player.seekable for determining whether we can seek?
     player.addEventListener("durationchange", function () {
+        // Deferred active due to unknown duration
+        if (pendingActivePlayer == player) {
+            setPlayerActive(pendingActivePlayer);
+            return;
+        }
+
         sendPlayerInfo(player, "duration", {
             duration: player.duration
         });


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic