michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "GStreamerFormatHelper.h" michael@0: #include "nsCharSeparatedTokenizer.h" michael@0: #include "nsString.h" michael@0: #include "GStreamerLoader.h" michael@0: michael@0: #define ENTRY_FORMAT(entry) entry[0] michael@0: #define ENTRY_CAPS(entry) entry[1] michael@0: michael@0: namespace mozilla { michael@0: michael@0: GStreamerFormatHelper* GStreamerFormatHelper::gInstance = nullptr; michael@0: bool GStreamerFormatHelper::sLoadOK = false; michael@0: michael@0: GStreamerFormatHelper* GStreamerFormatHelper::Instance() { michael@0: if (!gInstance) { michael@0: if ((sLoadOK = load_gstreamer())) { michael@0: gst_init(nullptr, nullptr); michael@0: } michael@0: michael@0: gInstance = new GStreamerFormatHelper(); michael@0: } michael@0: michael@0: return gInstance; michael@0: } michael@0: michael@0: void GStreamerFormatHelper::Shutdown() { michael@0: delete gInstance; michael@0: gInstance = nullptr; michael@0: } michael@0: michael@0: static char const *const sContainers[6][2] = { michael@0: {"video/mp4", "video/quicktime"}, michael@0: {"video/quicktime", "video/quicktime"}, michael@0: {"audio/mp4", "audio/x-m4a"}, michael@0: {"audio/x-m4a", "audio/x-m4a"}, michael@0: {"audio/mpeg", "audio/mpeg, mpegversion=(int)1"}, michael@0: {"audio/mp3", "audio/mpeg, mpegversion=(int)1"}, michael@0: }; michael@0: michael@0: static char const *const sCodecs[9][2] = { michael@0: {"avc1.42E01E", "video/x-h264"}, michael@0: {"avc1.42001E", "video/x-h264"}, michael@0: {"avc1.58A01E", "video/x-h264"}, michael@0: {"avc1.4D401E", "video/x-h264"}, michael@0: {"avc1.64001E", "video/x-h264"}, michael@0: {"avc1.64001F", "video/x-h264"}, michael@0: {"mp4v.20.3", "video/3gpp"}, michael@0: {"mp4a.40.2", "audio/mpeg, mpegversion=(int)4"}, michael@0: {"mp3", "audio/mpeg, mpegversion=(int)1"}, michael@0: }; michael@0: michael@0: static char const * const sDefaultCodecCaps[][2] = { michael@0: {"video/mp4", "video/x-h264"}, michael@0: {"video/quicktime", "video/x-h264"}, michael@0: {"audio/mp4", "audio/mpeg, mpegversion=(int)4"}, michael@0: {"audio/x-m4a", "audio/mpeg, mpegversion=(int)4"}, michael@0: {"audio/mp3", "audio/mpeg, layer=(int)3"}, michael@0: {"audio/mpeg", "audio/mpeg, layer=(int)3"} michael@0: }; michael@0: michael@0: GStreamerFormatHelper::GStreamerFormatHelper() michael@0: : mFactories(nullptr), michael@0: mCookie(static_cast(-1)) michael@0: { michael@0: if (!sLoadOK) { michael@0: return; michael@0: } michael@0: michael@0: mSupportedContainerCaps = gst_caps_new_empty(); michael@0: for (unsigned int i = 0; i < G_N_ELEMENTS(sContainers); i++) { michael@0: const char* capsString = sContainers[i][1]; michael@0: GstCaps* caps = gst_caps_from_string(capsString); michael@0: gst_caps_append(mSupportedContainerCaps, caps); michael@0: } michael@0: michael@0: mSupportedCodecCaps = gst_caps_new_empty(); michael@0: for (unsigned int i = 0; i < G_N_ELEMENTS(sCodecs); i++) { michael@0: const char* capsString = sCodecs[i][1]; michael@0: GstCaps* caps = gst_caps_from_string(capsString); michael@0: gst_caps_append(mSupportedCodecCaps, caps); michael@0: } michael@0: } michael@0: michael@0: GStreamerFormatHelper::~GStreamerFormatHelper() { michael@0: if (!sLoadOK) { michael@0: return; michael@0: } michael@0: michael@0: gst_caps_unref(mSupportedContainerCaps); michael@0: gst_caps_unref(mSupportedCodecCaps); michael@0: michael@0: if (mFactories) michael@0: g_list_free(mFactories); michael@0: } michael@0: michael@0: static GstCaps * michael@0: GetContainerCapsFromMIMEType(const char *aType) { michael@0: /* convert aMIMEType to gst container caps */ michael@0: const char* capsString = nullptr; michael@0: for (uint32_t i = 0; i < G_N_ELEMENTS(sContainers); i++) { michael@0: if (!strcmp(ENTRY_FORMAT(sContainers[i]), aType)) { michael@0: capsString = ENTRY_CAPS(sContainers[i]); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (!capsString) { michael@0: /* we couldn't find any matching caps */ michael@0: return nullptr; michael@0: } michael@0: michael@0: return gst_caps_from_string(capsString); michael@0: } michael@0: michael@0: static GstCaps * michael@0: GetDefaultCapsFromMIMEType(const char *aType) { michael@0: GstCaps *caps = GetContainerCapsFromMIMEType(aType); michael@0: michael@0: for (uint32_t i = 0; i < G_N_ELEMENTS(sDefaultCodecCaps); i++) { michael@0: if (!strcmp(sDefaultCodecCaps[i][0], aType)) { michael@0: GstCaps *tmp = gst_caps_from_string(sDefaultCodecCaps[i][1]); michael@0: michael@0: gst_caps_append(caps, tmp); michael@0: return caps; michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: bool GStreamerFormatHelper::CanHandleMediaType(const nsACString& aMIMEType, michael@0: const nsAString* aCodecs) { michael@0: if (!sLoadOK) { michael@0: return false; michael@0: } michael@0: michael@0: const char *type; michael@0: NS_CStringGetData(aMIMEType, &type, nullptr); michael@0: michael@0: GstCaps *caps; michael@0: if (aCodecs && !aCodecs->IsEmpty()) { michael@0: caps = ConvertFormatsToCaps(type, aCodecs); michael@0: } else { michael@0: // Get a minimal set of codec caps for this MIME type we should support so michael@0: // that we don't overreport MIME types we are able to play. michael@0: caps = GetDefaultCapsFromMIMEType(type); michael@0: } michael@0: michael@0: if (!caps) { michael@0: return false; michael@0: } michael@0: michael@0: bool ret = HaveElementsToProcessCaps(caps); michael@0: gst_caps_unref(caps); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: GstCaps* GStreamerFormatHelper::ConvertFormatsToCaps(const char* aMIMEType, michael@0: const nsAString* aCodecs) { michael@0: NS_ASSERTION(sLoadOK, "GStreamer library not linked"); michael@0: michael@0: unsigned int i; michael@0: michael@0: GstCaps *caps = GetContainerCapsFromMIMEType(aMIMEType); michael@0: if (!caps) { michael@0: return nullptr; michael@0: } michael@0: michael@0: /* container only */ michael@0: if (!aCodecs) { michael@0: return caps; michael@0: } michael@0: michael@0: nsCharSeparatedTokenizer tokenizer(*aCodecs, ','); michael@0: while (tokenizer.hasMoreTokens()) { michael@0: const nsSubstring& codec = tokenizer.nextToken(); michael@0: const char *capsString = nullptr; michael@0: michael@0: for (i = 0; i < G_N_ELEMENTS(sCodecs); i++) { michael@0: if (codec.EqualsASCII(ENTRY_FORMAT(sCodecs[i]))) { michael@0: capsString = ENTRY_CAPS(sCodecs[i]); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (!capsString) { michael@0: gst_caps_unref(caps); michael@0: return nullptr; michael@0: } michael@0: michael@0: GstCaps* tmp = gst_caps_from_string(capsString); michael@0: /* appends and frees tmp */ michael@0: gst_caps_append(caps, tmp); michael@0: } michael@0: michael@0: return caps; michael@0: } michael@0: michael@0: static gboolean FactoryFilter(GstPluginFeature *aFeature, gpointer) michael@0: { michael@0: if (!GST_IS_ELEMENT_FACTORY(aFeature)) { michael@0: return FALSE; michael@0: } michael@0: michael@0: // TODO _get_klass doesn't exist in 1.0 michael@0: const gchar *className = michael@0: gst_element_factory_get_klass(GST_ELEMENT_FACTORY_CAST(aFeature)); michael@0: michael@0: if (!strstr(className, "Decoder") && !strstr(className, "Demux")) { michael@0: return FALSE; michael@0: } michael@0: michael@0: return gst_plugin_feature_get_rank(aFeature) >= GST_RANK_MARGINAL; michael@0: } michael@0: michael@0: /** michael@0: * Returns true if any |aFactory| caps intersect with |aCaps| michael@0: */ michael@0: static bool SupportsCaps(GstElementFactory *aFactory, GstCaps *aCaps) michael@0: { michael@0: for (const GList *iter = gst_element_factory_get_static_pad_templates(aFactory); iter; iter = iter->next) { michael@0: GstStaticPadTemplate *templ = static_cast(iter->data); michael@0: michael@0: if (templ->direction == GST_PAD_SRC) { michael@0: continue; michael@0: } michael@0: michael@0: GstCaps *caps = gst_static_caps_get(&templ->static_caps); michael@0: if (!caps) { michael@0: continue; michael@0: } michael@0: michael@0: if (gst_caps_can_intersect(gst_static_caps_get(&templ->static_caps), aCaps)) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool GStreamerFormatHelper::HaveElementsToProcessCaps(GstCaps* aCaps) { michael@0: NS_ASSERTION(sLoadOK, "GStreamer library not linked"); michael@0: michael@0: GList* factories = GetFactories(); michael@0: michael@0: /* here aCaps contains [containerCaps, [codecCaps1, [codecCaps2, ...]]] so process michael@0: * caps structures individually as we want one element for _each_ michael@0: * structure */ michael@0: for (unsigned int i = 0; i < gst_caps_get_size(aCaps); i++) { michael@0: GstStructure* s = gst_caps_get_structure(aCaps, i); michael@0: GstCaps* caps = gst_caps_new_full(gst_structure_copy(s), nullptr); michael@0: michael@0: bool found = false; michael@0: for (GList *elem = factories; elem; elem = elem->next) { michael@0: if (SupportsCaps(GST_ELEMENT_FACTORY_CAST(elem->data), caps)) { michael@0: found = true; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (!found) { michael@0: return false; michael@0: } michael@0: michael@0: gst_caps_unref(caps); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool GStreamerFormatHelper::CanHandleContainerCaps(GstCaps* aCaps) michael@0: { michael@0: NS_ASSERTION(sLoadOK, "GStreamer library not linked"); michael@0: michael@0: return gst_caps_can_intersect(aCaps, mSupportedContainerCaps); michael@0: } michael@0: michael@0: bool GStreamerFormatHelper::CanHandleCodecCaps(GstCaps* aCaps) michael@0: { michael@0: NS_ASSERTION(sLoadOK, "GStreamer library not linked"); michael@0: michael@0: return gst_caps_can_intersect(aCaps, mSupportedCodecCaps); michael@0: } michael@0: michael@0: GList* GStreamerFormatHelper::GetFactories() { michael@0: NS_ASSERTION(sLoadOK, "GStreamer library not linked"); michael@0: michael@0: #if GST_VERSION_MAJOR >= 1 michael@0: uint32_t cookie = gst_registry_get_feature_list_cookie(gst_registry_get()); michael@0: #else michael@0: uint32_t cookie = gst_default_registry_get_feature_list_cookie(); michael@0: #endif michael@0: if (cookie != mCookie) { michael@0: g_list_free(mFactories); michael@0: #if GST_VERSION_MAJOR >= 1 michael@0: mFactories = michael@0: gst_registry_feature_filter(gst_registry_get(), michael@0: (GstPluginFeatureFilter)FactoryFilter, michael@0: false, nullptr); michael@0: #else michael@0: mFactories = michael@0: gst_default_registry_feature_filter((GstPluginFeatureFilter)FactoryFilter, michael@0: false, nullptr); michael@0: #endif michael@0: mCookie = cookie; michael@0: } michael@0: michael@0: return mFactories; michael@0: } michael@0: michael@0: } // namespace mozilla