From c6e3ce19252b4d25136cb84d2e3e054f1a073a40 Mon Sep 17 00:00:00 2001
From: Agate <me@agate.blue>
Date: Fri, 31 Jul 2020 11:06:29 +0200
Subject: [PATCH] Fix #1180: Fix broken media support detection

---
 changes/changelog.d/1180.bugfix       |  1 +
 front/src/EmbedFrame.vue              |  8 +++++++-
 front/src/components/audio/Player.vue | 10 ++++++++--
 front/src/utils.js                    |  1 -
 4 files changed, 16 insertions(+), 4 deletions(-)
 create mode 100644 changes/changelog.d/1180.bugfix

diff --git a/changes/changelog.d/1180.bugfix b/changes/changelog.d/1180.bugfix
new file mode 100644
index 0000000000..262d1037ef
--- /dev/null
+++ b/changes/changelog.d/1180.bugfix
@@ -0,0 +1 @@
+Fix broken media support detection (#1180)
\ No newline at end of file
diff --git a/front/src/EmbedFrame.vue b/front/src/EmbedFrame.vue
index 8dcccf3e5f..67dc023036 100644
--- a/front/src/EmbedFrame.vue
+++ b/front/src/EmbedFrame.vue
@@ -342,13 +342,19 @@ export default {
     },
     getSources (uploads) {
       let self = this;
-      let sources = uploads.map(u => {
+      let a = document.createElement('audio')
+      let allowed = ['probably', 'maybe']
+      let sources = uploads.filter(u => {
+        let canPlay = a.canPlayType(u.mimetype)
+        return allowed.indexOf(canPlay) > -1
+      }).map(u => {
         return {
           type: u.mimetype,
           src: self.fullUrl(u.listen_url),
           duration: u.duration
         }
       })
+      a.remove()
       if (sources.length > 0) {
         // We always add a transcoded MP3 src at the end
         // because transcoding is expensive, but we want browsers that do
diff --git a/front/src/components/audio/Player.vue b/front/src/components/audio/Player.vue
index b4cf6a3851..c67e6e5548 100644
--- a/front/src/components/audio/Player.vue
+++ b/front/src/components/audio/Player.vue
@@ -408,12 +408,18 @@ export default {
       return sound
     },
     getSrcs: function (trackData) {
-      let sources = trackData.uploads.map(u => {
+      let a = document.createElement('audio')
+      let allowed = ['probably', 'maybe']
+      let sources = trackData.uploads.filter(u => {
+        let canPlay = a.canPlayType(u.mimetype)
+        return allowed.indexOf(canPlay) > -1
+      }).map(u => {
         return {
           type: u.extension,
           url: this.$store.getters['instance/absoluteUrl'](u.listen_url),
         }
       })
+      a.remove()
       // We always add a transcoded MP3 src at the end
       // because transcoding is expensive, but we want browsers that do
       // not support other codecs to be able to play it :)
@@ -721,7 +727,7 @@ export default {
         }, 500);
         // If the session is playing as a PWA, populate the notification
         // with details from the track
-        if ('mediaSession' in navigator) {
+        if (this.currentTrack && 'mediaSession' in navigator) {
           let metadata = {
             title: this.currentTrack.title,
             artist: this.currentTrack.artist.name,
diff --git a/front/src/utils.js b/front/src/utils.js
index 380f13e957..fb7117a20a 100644
--- a/front/src/utils.js
+++ b/front/src/utils.js
@@ -47,7 +47,6 @@ export function setCsrf(xhr) {
 }
 
 export function checkRedirectToLogin (store, router) {
-  console.log('HELLO', store.state.auth.authenticated, router.currentRoute.fullPath)
   if (!store.state.auth.authenticated) {
     router.push({name: 'login', query: {next: router.currentRoute.fullPath}})
   }
-- 
GitLab