Bug 889699 - Make gstreamer backend return more accurate results for canPlayType() r=alessandro.d
☠☠ backed out by 4b4e29861bef ☠ ☠
authorEdwin Flores <eflores@mozilla.com>
Fri, 19 Jul 2013 15:31:11 +1200
changeset 139227 40f9e902464aedf57948308b9aa286b33cbf5af7
parent 139226 dabdd7ceb43eb52cae4aef7a030d65e7b48959e5
child 139228 df425bca56655b64513f2144e9a23f3ef50f36a9
push id1890
push userryanvm@gmail.com
push dateFri, 19 Jul 2013 17:44:21 +0000
treeherderfx-team@20848adc9980 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersalessandro
bugs889699
milestone25.0a1
Bug 889699 - Make gstreamer backend return more accurate results for canPlayType() r=alessandro.d
content/media/gstreamer/GStreamerFormatHelper.cpp
--- a/content/media/gstreamer/GStreamerFormatHelper.cpp
+++ b/content/media/gstreamer/GStreamerFormatHelper.cpp
@@ -1,17 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "GStreamerFormatHelper.h"
 #include "nsCharSeparatedTokenizer.h"
-#include "nsXPCOMStrings.h"
+#include "nsString.h"
 #include "GStreamerLoader.h"
 
 #define ENTRY_FORMAT(entry) entry[0]
 #define ENTRY_CAPS(entry) entry[1]
 
 namespace mozilla {
 
 GStreamerFormatHelper* GStreamerFormatHelper::gInstance = nullptr;
@@ -31,55 +31,63 @@ GStreamerFormatHelper* GStreamerFormatHe
 
 void GStreamerFormatHelper::Shutdown() {
   if (gInstance) {
     delete gInstance;
     gInstance = nullptr;
   }
 }
 
-char const *const GStreamerFormatHelper::mContainers[6][2] = {
+static char const *const sContainers[6][2] = {
   {"video/mp4", "video/quicktime"},
   {"video/quicktime", "video/quicktime"},
   {"audio/mp4", "audio/x-m4a"},
   {"audio/x-m4a", "audio/x-m4a"},
   {"audio/mpeg", "audio/mpeg, mpegversion=(int)1"},
   {"audio/mp3", "audio/mpeg, mpegversion=(int)1"},
 };
 
-char const *const GStreamerFormatHelper::mCodecs[9][2] = {
+static char const *const sCodecs[9][2] = {
   {"avc1.42E01E", "video/x-h264"},
   {"avc1.42001E", "video/x-h264"},
   {"avc1.58A01E", "video/x-h264"},
   {"avc1.4D401E", "video/x-h264"},
   {"avc1.64001E", "video/x-h264"},
   {"avc1.64001F", "video/x-h264"},
   {"mp4v.20.3", "video/3gpp"},
   {"mp4a.40.2", "audio/mpeg, mpegversion=(int)4"},
   {"mp3", "audio/mpeg, mpegversion=(int)1"},
 };
 
+static char const * const sDefaultCodecCaps[][2] = {
+  {"video/mp4", "video/x-h264"},
+  {"video/quicktime", "video/x-h264"},
+  {"audio/mp4", "audio/mpeg, mpegversion=(int)4"},
+  {"audio/x-m4a", "audio/mpeg, mpegversion=(int)4"},
+  {"audio/mp3", "audio/mpeg, layer=(int)3"}
+};
+
 GStreamerFormatHelper::GStreamerFormatHelper()
   : mFactories(nullptr),
     mCookie(static_cast<uint32_t>(-1))
 {
   if (!sLoadOK) {
     return;
   }
 
   mSupportedContainerCaps = gst_caps_new_empty();
-  for (unsigned int i = 0; i < G_N_ELEMENTS(mContainers); i++) {
-    const char* capsString = mContainers[i][1];
+  for (unsigned int i = 0; i < G_N_ELEMENTS(sContainers); i++) {
+    const char* capsString = sContainers[i][1];
     GstCaps* caps = gst_caps_from_string(capsString);
     gst_caps_append(mSupportedContainerCaps, caps);
   }
 
   mSupportedCodecCaps = gst_caps_new_empty();
-  for (unsigned int i = 0; i < G_N_ELEMENTS(mCodecs); i++) {
-    const char* capsString = mCodecs[i][1];
+  for (unsigned int i = 0; i < G_N_ELEMENTS(sCodecs); i++) {
+    const char* capsString = sCodecs[i][1];
     GstCaps* caps = gst_caps_from_string(capsString);
     gst_caps_append(mSupportedCodecCaps, caps);
   }
 }
 
 GStreamerFormatHelper::~GStreamerFormatHelper() {
   if (!sLoadOK) {
     return;
@@ -87,70 +95,99 @@ GStreamerFormatHelper::~GStreamerFormatH
 
   gst_caps_unref(mSupportedContainerCaps);
   gst_caps_unref(mSupportedCodecCaps);
 
   if (mFactories)
     g_list_free(mFactories);
 }
 
+static GstCaps *
+GetContainerCapsFromMIMEType(const char *aType) {
+  /* convert aMIMEType to gst container caps */
+  const char* capsString = nullptr;
+  for (uint32_t i = 0; i < G_N_ELEMENTS(sContainers); i++) {
+    if (!strcmp(ENTRY_FORMAT(sContainers[i]), aType)) {
+      capsString = ENTRY_CAPS(sContainers[i]);
+      break;
+    }
+  }
+
+  if (!capsString) {
+    /* we couldn't find any matching caps */
+    return nullptr;
+  }
+
+  return gst_caps_from_string(capsString);
+}
+
+static GstCaps *
+GetDefaultCapsFromMIMEType(const char *aType) {
+  GstCaps *caps = GetContainerCapsFromMIMEType(aType);
+
+  for (uint32_t i = 0; i < G_N_ELEMENTS(sDefaultCodecCaps); i++) {
+    if (!strcmp(sDefaultCodecCaps[i][0], aType)) {
+      GstCaps *tmp = gst_caps_from_string(sDefaultCodecCaps[i][1]);
+
+      gst_caps_append(caps, tmp);
+      return caps;
+    }
+  }
+
+  return nullptr;
+}
+
 bool GStreamerFormatHelper::CanHandleMediaType(const nsACString& aMIMEType,
                                                const nsAString* aCodecs) {
   if (!sLoadOK) {
     return false;
   }
 
   const char *type;
   NS_CStringGetData(aMIMEType, &type, NULL);
 
-  GstCaps* caps = ConvertFormatsToCaps(type, aCodecs);
+  GstCaps *caps;
+  if (aCodecs && !aCodecs->IsEmpty()) {
+    caps = ConvertFormatsToCaps(type, aCodecs);
+  } else {
+    // Get a minimal set of codec caps for this MIME type we should support so
+    // that we don't overreport MIME types we are able to play.
+    caps = GetDefaultCapsFromMIMEType(type);
+  }
+
   if (!caps) {
     return false;
   }
 
   bool ret = HaveElementsToProcessCaps(caps);
   gst_caps_unref(caps);
 
   return ret;
 }
 
 GstCaps* GStreamerFormatHelper::ConvertFormatsToCaps(const char* aMIMEType,
                                                      const nsAString* aCodecs) {
   NS_ASSERTION(sLoadOK, "GStreamer library not linked");
 
   unsigned int i;
 
-  /* convert aMIMEType to gst container caps */
-  const char* capsString = nullptr;
-  for (i = 0; i < G_N_ELEMENTS(mContainers); i++) {
-    if (!strcmp(ENTRY_FORMAT(mContainers[i]), aMIMEType)) {
-      capsString = ENTRY_CAPS(mContainers[i]);
-      break;
-    }
-  }
-
-  if (!capsString) {
-    /* we couldn't find any matching caps */
+  GstCaps *caps = GetContainerCapsFromMIMEType(aMIMEType);
+  /* container only */
+  if (!caps) {
     return nullptr;
   }
 
-  GstCaps* caps = gst_caps_from_string(capsString);
-  /* container only */
-  if (!aCodecs) {
-    return caps;
-  }
-
   nsCharSeparatedTokenizer tokenizer(*aCodecs, ',');
   while (tokenizer.hasMoreTokens()) {
     const nsSubstring& codec = tokenizer.nextToken();
-    capsString = nullptr;
+    const char *capsString = nullptr;
 
-    for (i = 0; i < G_N_ELEMENTS(mCodecs); i++) {
-      if (codec.EqualsASCII(ENTRY_FORMAT(mCodecs[i]))) {
-        capsString = ENTRY_CAPS(mCodecs[i]);
+    for (i = 0; i < G_N_ELEMENTS(sCodecs); i++) {
+      if (codec.EqualsASCII(ENTRY_FORMAT(sCodecs[i]))) {
+        capsString = ENTRY_CAPS(sCodecs[i]);
         break;
       }
     }
 
     if (!capsString) {
       gst_caps_unref(caps);
       return nullptr;
     }