diff --git a/dom/media/DecoderTraits.cpp b/dom/media/DecoderTraits.cpp index d5edf6c633..093f2b7a14 100644 --- a/dom/media/DecoderTraits.cpp +++ b/dom/media/DecoderTraits.cpp @@ -75,6 +75,9 @@ #endif #include "MediaFormatReader.h" +#include "MP3Decoder.h" +#include "MP3Demuxer.h" + namespace mozilla { @@ -363,6 +366,13 @@ IsMP4SupportedType(const nsACString& aType, } #endif +static bool +IsMP3SupportedType(const nsACString& aType, + const nsAString& aCodecs = EmptyString()) +{ + return aType.EqualsASCII("audio/mpeg") && MP3Decoder::IsEnabled(); +} + #ifdef MOZ_APPLEMEDIA static const char * const gAppleMP3Types[] = { "audio/mp3", @@ -447,6 +457,10 @@ DecoderTraits::CanHandleMediaType(const char* aMIMEType, return aHaveRequestedCodecs ? CANPLAY_YES : CANPLAY_MAYBE; } #endif +if (IsMP3SupportedType(nsDependentCString(aMIMEType), + aRequestedCodecs)) { + return aHaveRequestedCodecs ? CANPLAY_YES : CANPLAY_MAYBE; + } #ifdef MOZ_GSTREAMER if (GStreamerDecoder::CanHandleMediaType(nsDependentCString(aMIMEType), aHaveRequestedCodecs ? &aRequestedCodecs : nullptr)) { @@ -539,6 +553,10 @@ InstantiateDecoder(const nsACString& aType, MediaDecoderOwner* aOwner) return decoder.forget(); } #endif +if (IsMP3SupportedType(aType)) { + decoder = new MP3Decoder(); + return decoder.forget(); + } #ifdef MOZ_GSTREAMER if (IsGStreamerSupportedType(aType)) { decoder = new GStreamerDecoder(); @@ -665,6 +683,9 @@ MediaDecoderReader* DecoderTraits::CreateReader(const nsACString& aType, Abstrac static_cast(new MP4Reader(aDecoder)); } else #endif +if (IsMP3SupportedType(aType)) { + decoderReader = new MediaFormatReader(aDecoder, new mp3::MP3Demuxer(aDecoder->GetResource())); + } else #ifdef MOZ_GSTREAMER if (IsGStreamerSupportedType(aType)) { decoderReader = new GStreamerReader(aDecoder); @@ -759,6 +780,7 @@ bool DecoderTraits::IsSupportedInVideoDocument(const nsACString& aType) #ifdef MOZ_FMP4 IsMP4SupportedType(aType) || #endif + IsMP3SupportedType(aType) || #ifdef MOZ_WMF IsWMFSupportedType(aType) || #endif diff --git a/dom/media/MP3Decoder.cpp b/dom/media/MP3Decoder.cpp new file mode 100644 index 0000000000..7dfa86ac14 --- /dev/null +++ b/dom/media/MP3Decoder.cpp @@ -0,0 +1,45 @@ + +/* -*- 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 "MP3Decoder.h" +#include "MediaDecoderStateMachine.h" +#include "MediaFormatReader.h" +#include "MP3Demuxer.h" +#include "mozilla/Preferences.h" + +#ifdef MOZ_WIDGET_ANDROID +#include "AndroidBridge.h" +#endif + +namespace mozilla { + +MediaDecoder* +MP3Decoder::Clone() { + if (!IsEnabled()) { + return nullptr; + } + return new MP3Decoder(); +} + +MediaDecoderStateMachine* +MP3Decoder::CreateStateMachine() { + nsRefPtr reader = + new MediaFormatReader(this, new mp3::MP3Demuxer(GetResource())); + return new MediaDecoderStateMachine(this, reader); +} + +bool +MP3Decoder::IsEnabled() { +#ifdef MOZ_WIDGET_ANDROID + // We need android.media.MediaCodec which exists in API level 16 and higher. + return AndroidBridge::Bridge()->GetAPIVersion() >= 16; +#else + return Preferences::GetBool("media.mp3.enabled"); +#endif +} + +} // namespace mozilla diff --git a/dom/media/MP3Decoder.h b/dom/media/MP3Decoder.h new file mode 100644 index 0000000000..b41e264d43 --- /dev/null +++ b/dom/media/MP3Decoder.h @@ -0,0 +1,26 @@ +/* -*- 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/. */ +#ifndef MP3Decoder_h_ +#define MP3Decoder_h_ + +#include "MediaDecoder.h" + +namespace mozilla { + +class MP3Decoder : public MediaDecoder { +public: + // MediaDecoder interface. + MediaDecoder* Clone() override; + MediaDecoderStateMachine* CreateStateMachine() override; + + // Returns true if the MP3 backend is preffed on, and we're running on a + // platform that is likely to have decoders for the format. + static bool IsEnabled(); +}; + +} // namespace mozilla + +#endif diff --git a/dom/media/moz.build b/dom/media/moz.build index 57d3ab6e56..af148e6bcf 100644 --- a/dom/media/moz.build +++ b/dom/media/moz.build @@ -116,6 +116,7 @@ EXPORTS += [ 'MediaTimer.h', 'MediaTrack.h', 'MediaTrackList.h', + 'MP3Decoder.h', 'MP3Demuxer.h', 'MP3FrameParser.h', 'nsIDocumentActivity.h', @@ -198,6 +199,7 @@ UNIFIED_SOURCES += [ 'MediaTimer.cpp', 'MediaTrack.cpp', 'MediaTrackList.cpp', + 'MP3Decoder.cpp', 'MP3Demuxer.cpp', 'MP3FrameParser.cpp', 'RTCIdentityProviderRegistrar.cpp',