2012-12-18 00:49:58 -08:00
|
|
|
/* -*- 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 "WMF.h"
|
|
|
|
#include "WMFDecoder.h"
|
|
|
|
#include "WMFReader.h"
|
|
|
|
#include "WMFUtils.h"
|
|
|
|
#include "MediaDecoderStateMachine.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2013-11-21 19:35:42 -08:00
|
|
|
#include "mozilla/WindowsVersion.h"
|
2013-11-17 14:29:55 -08:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2013-03-26 07:40:01 -07:00
|
|
|
|
2013-08-12 21:49:25 -07:00
|
|
|
#ifdef MOZ_DIRECTSHOW
|
|
|
|
#include "DirectShowDecoder.h"
|
|
|
|
#endif
|
|
|
|
|
2012-12-18 00:49:58 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
MediaDecoderStateMachine* WMFDecoder::CreateStateMachine()
|
|
|
|
{
|
|
|
|
return new MediaDecoderStateMachine(this, new WMFReader(this));
|
|
|
|
}
|
|
|
|
|
2013-04-18 19:54:26 -07:00
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
WMFDecoder::IsMP3Supported()
|
|
|
|
{
|
2013-04-30 03:14:18 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");
|
2013-11-21 19:35:42 -08:00
|
|
|
if (!MediaDecoder::IsWMFEnabled()) {
|
2013-04-18 19:54:26 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-03 14:39:42 -07:00
|
|
|
// MP3 works fine in WMF on Windows Vista and Windows 8.
|
2013-11-21 19:35:42 -08:00
|
|
|
if (!IsWin7OrLater()) {
|
2013-04-18 19:54:26 -07:00
|
|
|
return true;
|
|
|
|
}
|
2014-04-03 14:39:42 -07:00
|
|
|
// MP3 support is disabled if we're on Windows 7 and no service packs are
|
|
|
|
// installed, since it's crashy. Win7 with service packs is not so crashy,
|
|
|
|
// so we enable it there. Note we prefer DirectShow for MP3 playback, but
|
|
|
|
// we still support MP3 in MP4 via WMF, or MP3 in WMF if DirectShow is
|
|
|
|
// disabled.
|
2013-11-21 19:35:42 -08:00
|
|
|
return IsWin7SP1OrLater();
|
2013-04-18 19:54:26 -07:00
|
|
|
}
|
|
|
|
|
2013-11-17 14:29:55 -08:00
|
|
|
static bool
|
|
|
|
IsSupportedH264Codec(const nsAString& aCodec)
|
|
|
|
{
|
|
|
|
// According to the WMF documentation:
|
|
|
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd797815%28v=vs.85%29.aspx
|
|
|
|
// "The Media Foundation H.264 video decoder is a Media Foundation Transform
|
|
|
|
// that supports decoding of Baseline, Main, and High profiles, up to level
|
|
|
|
// 5.1.". We also report that we can play Extended profile, as there are
|
|
|
|
// bitstreams that are Extended compliant that are also Baseline compliant.
|
|
|
|
|
|
|
|
// H.264 codecs parameters have a type defined as avc1.PPCCLL, where
|
|
|
|
// PP = profile_idc, CC = constraint_set flags, LL = level_idc.
|
|
|
|
// We ignore the constraint_set flags, as it's not clear from the WMF
|
|
|
|
// documentation what constraints the WMF H.264 decoder supports.
|
|
|
|
// See http://blog.pearce.org.nz/2013/11/what-does-h264avc1-codecs-parameters.html
|
|
|
|
// for more details.
|
|
|
|
if (aCodec.Length() != strlen("avc1.PPCCLL")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the codec starts with "avc1.".
|
|
|
|
const nsAString& sample = Substring(aCodec, 0, 5);
|
|
|
|
if (!sample.EqualsASCII("avc1.")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the profile_idc and level_idc. Note: the constraint_set flags
|
|
|
|
// are ignored, it's not clear from the WMF documentation if they make a
|
|
|
|
// difference.
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
const int32_t profile = PromiseFlatString(Substring(aCodec, 5, 2)).ToInteger(&rv, 16);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
const int32_t level = PromiseFlatString(Substring(aCodec, 9, 2)).ToInteger(&rv, 16);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
return level >= eAVEncH264VLevel1 &&
|
|
|
|
level <= eAVEncH264VLevel5_1 &&
|
|
|
|
(profile == eAVEncH264VProfile_Base ||
|
|
|
|
profile == eAVEncH264VProfile_Main ||
|
|
|
|
profile == eAVEncH264VProfile_Extended ||
|
|
|
|
profile == eAVEncH264VProfile_High);
|
|
|
|
}
|
|
|
|
|
2012-12-18 00:49:58 -08:00
|
|
|
bool
|
2013-11-17 14:29:55 -08:00
|
|
|
WMFDecoder::CanPlayType(const nsACString& aType,
|
|
|
|
const nsAString& aCodecs)
|
2012-12-18 00:49:58 -08:00
|
|
|
{
|
|
|
|
if (!MediaDecoder::IsWMFEnabled() ||
|
2013-11-17 14:29:55 -08:00
|
|
|
NS_FAILED(LoadDLLs())) {
|
2012-12-18 00:49:58 -08:00
|
|
|
return false;
|
2013-11-17 14:29:55 -08:00
|
|
|
}
|
2012-12-18 00:49:58 -08:00
|
|
|
|
2013-02-26 12:27:43 -08:00
|
|
|
// Assume that if LoadDLLs() didn't fail, we can playback the types that
|
2013-04-18 19:54:26 -07:00
|
|
|
// we know should be supported by Windows Media Foundation.
|
|
|
|
if ((aType.EqualsASCII("audio/mpeg") || aType.EqualsASCII("audio/mp3")) &&
|
|
|
|
IsMP3Supported()) {
|
|
|
|
// Note: We block MP3 playback on Window 7 SP0 since it seems to crash
|
|
|
|
// in some circumstances.
|
2013-11-17 14:29:55 -08:00
|
|
|
return !aCodecs.Length() || aCodecs.EqualsASCII("mp3");
|
2012-12-18 00:49:58 -08:00
|
|
|
}
|
|
|
|
|
2014-04-03 14:39:42 -07:00
|
|
|
// AAC-LC or MP3 in M4A.
|
2013-11-17 14:29:55 -08:00
|
|
|
if (aType.EqualsASCII("audio/mp4") || aType.EqualsASCII("audio/x-m4a")) {
|
2014-04-03 14:39:42 -07:00
|
|
|
return !aCodecs.Length() ||
|
|
|
|
aCodecs.EqualsASCII("mp4a.40.2") ||
|
|
|
|
aCodecs.EqualsASCII("mp3");
|
2012-12-18 00:49:58 -08:00
|
|
|
}
|
|
|
|
|
2013-11-17 14:29:55 -08:00
|
|
|
if (!aType.EqualsASCII("video/mp4")) {
|
|
|
|
return false;
|
2012-12-18 00:49:58 -08:00
|
|
|
}
|
|
|
|
|
2013-11-17 14:29:55 -08:00
|
|
|
// H.264 + AAC in MP4. Verify that all the codecs specifed are ones that
|
|
|
|
// we expect that we can play.
|
|
|
|
nsCharSeparatedTokenizer tokenizer(aCodecs, ',');
|
|
|
|
bool expectMoreTokens = false;
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
const nsSubstring& token = tokenizer.nextToken();
|
|
|
|
expectMoreTokens = tokenizer.separatorAfterCurrentToken();
|
|
|
|
if (token.EqualsASCII("mp4a.40.2") || // AAC-LC
|
2014-04-03 14:39:42 -07:00
|
|
|
token.EqualsASCII("mp3") ||
|
2013-11-17 14:29:55 -08:00
|
|
|
IsSupportedH264Codec(token)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (expectMoreTokens) {
|
|
|
|
// Last codec name was empty
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-12-18 00:49:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WMFDecoder::LoadDLLs()
|
|
|
|
{
|
|
|
|
return SUCCEEDED(wmf::LoadDLLs()) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WMFDecoder::UnloadDLLs()
|
|
|
|
{
|
|
|
|
wmf::UnloadDLLs();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
WMFDecoder::IsEnabled()
|
|
|
|
{
|
2013-03-26 07:40:01 -07:00
|
|
|
// We only use WMF on Windows Vista and up
|
2013-11-21 19:35:42 -08:00
|
|
|
return IsVistaOrLater() &&
|
2012-12-18 00:49:58 -08:00
|
|
|
Preferences::GetBool("media.windows-media-foundation.enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|