2012-05-31 17:54:23 -07: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/. */
|
2012-08-24 14:20:39 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-03-02 11:14:44 -08:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2012-05-31 17:54:23 -07:00
|
|
|
#include "MediaResource.h"
|
2013-03-19 05:23:54 -07:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2014-07-16 18:32:56 -07:00
|
|
|
#include "AndroidMediaPluginHost.h"
|
2012-05-31 17:54:23 -07:00
|
|
|
#include "nsXPCOMStrings.h"
|
|
|
|
#include "nsISeekableStream.h"
|
2014-07-16 18:32:56 -07:00
|
|
|
#include "AndroidMediaReader.h"
|
2012-11-01 14:13:10 -07:00
|
|
|
#include "nsIGfxInfo.h"
|
2012-12-21 14:32:15 -08:00
|
|
|
#include "gfxCrashReporterUtils.h"
|
2013-03-04 10:34:03 -08:00
|
|
|
#include "prmem.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "prlink.h"
|
2014-07-16 18:32:56 -07:00
|
|
|
#include "AndroidMediaResourceServer.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-05-31 17:54:23 -07:00
|
|
|
|
|
|
|
#include "MPAPI.h"
|
|
|
|
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "nsIPropertyBag2.h"
|
|
|
|
|
2012-11-29 18:37:00 -08:00
|
|
|
#if defined(ANDROID) || defined(MOZ_WIDGET_GONK)
|
|
|
|
#include "android/log.h"
|
2014-07-16 18:32:56 -07:00
|
|
|
#define ALOG(args...) __android_log_print(ANDROID_LOG_INFO, "AndroidMediaPluginHost" , ## args)
|
2012-11-29 18:37:00 -08:00
|
|
|
#else
|
|
|
|
#define ALOG(args...) /* do nothing */
|
|
|
|
#endif
|
|
|
|
|
2012-05-31 17:54:23 -07:00
|
|
|
using namespace MPAPI;
|
2012-11-14 11:45:33 -08:00
|
|
|
|
|
|
|
Decoder::Decoder() :
|
2013-10-23 13:34:10 -07:00
|
|
|
mResource(nullptr), mPrivate(nullptr)
|
2012-11-14 11:45:33 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
2012-05-31 17:54:23 -07:00
|
|
|
|
2013-08-29 21:51:38 -07:00
|
|
|
static char* GetResource(Decoder *aDecoder)
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
2013-08-29 21:51:38 -07:00
|
|
|
return static_cast<char*>(aDecoder->mResource);
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2012-08-24 14:20:39 -07:00
|
|
|
class GetIntPrefEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
GetIntPrefEvent(const char* aPref, int32_t* aResult)
|
|
|
|
: mPref(aPref), mResult(aResult) {}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
return Preferences::GetInt(mPref, mResult);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const char* mPref;
|
|
|
|
int32_t* mResult;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool GetIntPref(const char* aPref, int32_t* aResult)
|
|
|
|
{
|
|
|
|
// GetIntPref() is called on the decoder thread, but the Preferences API
|
|
|
|
// can only be called on the main thread. Post a runnable and wait.
|
2012-09-16 02:19:01 -07:00
|
|
|
NS_ENSURE_TRUE(aPref, false);
|
|
|
|
NS_ENSURE_TRUE(aResult, false);
|
2014-04-03 15:06:26 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event = new GetIntPrefEvent(aPref, aResult);
|
2012-08-24 14:20:39 -07:00
|
|
|
return NS_SUCCEEDED(NS_DispatchToMainThread(event, NS_DISPATCH_SYNC));
|
|
|
|
}
|
|
|
|
|
2014-01-27 20:31:00 -08:00
|
|
|
static bool
|
|
|
|
GetSystemInfoString(const char *aKey, char *aResult, size_t aResultLength)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aKey, false);
|
|
|
|
NS_ENSURE_TRUE(aResult, false);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyBag2> infoService = do_GetService("@mozilla.org/system-info;1");
|
|
|
|
NS_ASSERTION(infoService, "Could not find a system info service");
|
|
|
|
|
|
|
|
nsAutoCString key(aKey);
|
|
|
|
nsAutoCString info;
|
|
|
|
nsresult rv = infoService->GetPropertyAsACString(NS_ConvertUTF8toUTF16(key),
|
|
|
|
info);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
strncpy(aResult, info.get(), aResultLength);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-31 17:54:23 -07:00
|
|
|
static PluginHost sPluginHost = {
|
2013-08-29 21:51:38 -07:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2014-01-27 20:31:00 -08:00
|
|
|
GetIntPref,
|
2014-02-08 17:58:14 -08:00
|
|
|
GetSystemInfoString
|
2012-05-31 17:54:23 -07:00
|
|
|
};
|
|
|
|
|
2012-11-29 18:37:00 -08:00
|
|
|
// Return true if Omx decoding is supported on the device. This checks the
|
|
|
|
// built in whitelist/blacklist and preferences to see if that is overridden.
|
|
|
|
static bool IsOmxSupported()
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
2012-11-01 14:13:10 -07:00
|
|
|
bool forceEnabled =
|
|
|
|
Preferences::GetBool("stagefright.force-enabled", false);
|
|
|
|
bool disabled =
|
|
|
|
Preferences::GetBool("stagefright.disabled", false);
|
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
NS_WARNING("XXX stagefright disabled\n");
|
2012-11-29 18:37:00 -08:00
|
|
|
return false;
|
2012-11-01 14:13:10 -07:00
|
|
|
}
|
|
|
|
|
2012-12-21 14:32:15 -08:00
|
|
|
ScopedGfxFeatureReporter reporter("Stagefright", forceEnabled);
|
|
|
|
|
2012-11-01 14:13:10 -07:00
|
|
|
if (!forceEnabled) {
|
|
|
|
nsCOMPtr<nsIGfxInfo> gfxInfo = do_GetService("@mozilla.org/gfx/info;1");
|
|
|
|
if (gfxInfo) {
|
|
|
|
int32_t status;
|
|
|
|
if (NS_SUCCEEDED(gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_STAGEFRIGHT, &status))) {
|
2014-07-01 14:44:09 -07:00
|
|
|
if (status != nsIGfxInfo::FEATURE_STATUS_OK) {
|
2012-11-01 14:13:10 -07:00
|
|
|
NS_WARNING("XXX stagefright blacklisted\n");
|
2012-11-29 18:37:00 -08:00
|
|
|
return false;
|
2012-11-01 14:13:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-21 14:32:15 -08:00
|
|
|
|
|
|
|
reporter.SetSuccessful();
|
2012-11-29 18:37:00 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the name of the shared library that implements Omx based decoding. This varies
|
|
|
|
// depending on libstagefright version installed on the device and whether it is B2G vs Android.
|
|
|
|
// nullptr is returned if Omx decoding is not supported on the device,
|
|
|
|
static const char* GetOmxLibraryName()
|
|
|
|
{
|
|
|
|
#if defined(ANDROID) && !defined(MOZ_WIDGET_GONK)
|
|
|
|
nsCOMPtr<nsIPropertyBag2> infoService = do_GetService("@mozilla.org/system-info;1");
|
|
|
|
NS_ASSERTION(infoService, "Could not find a system info service");
|
|
|
|
|
|
|
|
int32_t version;
|
|
|
|
nsresult rv = infoService->GetPropertyAsInt32(NS_LITERAL_STRING("version"), &version);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ALOG("Android Version is: %d", version);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString release_version;
|
|
|
|
rv = infoService->GetPropertyAsAString(NS_LITERAL_STRING("release_version"), release_version);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ALOG("Android Release Version is: %s", NS_LossyConvertUTF16toASCII(release_version).get());
|
|
|
|
}
|
2012-11-01 14:13:10 -07:00
|
|
|
|
2012-12-10 12:52:46 -08:00
|
|
|
nsAutoString device;
|
|
|
|
rv = infoService->GetPropertyAsAString(NS_LITERAL_STRING("device"), device);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ALOG("Android Device is: %s", NS_LossyConvertUTF16toASCII(device).get());
|
|
|
|
}
|
|
|
|
|
2013-03-21 19:24:47 -07:00
|
|
|
nsAutoString manufacturer;
|
|
|
|
rv = infoService->GetPropertyAsAString(NS_LITERAL_STRING("manufacturer"), manufacturer);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ALOG("Android Manufacturer is: %s", NS_LossyConvertUTF16toASCII(manufacturer).get());
|
|
|
|
}
|
|
|
|
|
2013-09-02 19:37:51 -07:00
|
|
|
nsAutoString hardware;
|
|
|
|
rv = infoService->GetPropertyAsAString(NS_LITERAL_STRING("hardware"), hardware);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ALOG("Android Hardware is: %s", NS_LossyConvertUTF16toASCII(hardware).get());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!IsOmxSupported())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
#if defined(ANDROID) && !defined(MOZ_WIDGET_GONK)
|
2014-02-18 20:11:42 -08:00
|
|
|
if (version >= 17) {
|
2014-01-08 17:54:57 -08:00
|
|
|
return "libomxpluginkk.so";
|
|
|
|
}
|
|
|
|
else if (version == 13 || version == 12 || version == 11) {
|
2013-03-04 10:34:03 -08:00
|
|
|
return "libomxpluginhc.so";
|
2012-11-29 19:05:37 -08:00
|
|
|
}
|
|
|
|
else if (version == 10 && release_version >= NS_LITERAL_STRING("2.3.6")) {
|
2012-11-29 18:37:00 -08:00
|
|
|
// Gingerbread versions from 2.3.6 and above have a different DataSource
|
|
|
|
// layout to those on 2.3.5 and below.
|
2013-03-04 10:34:03 -08:00
|
|
|
return "libomxplugingb.so";
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
2012-12-12 14:56:01 -08:00
|
|
|
else if (version == 10 && release_version >= NS_LITERAL_STRING("2.3.4") &&
|
|
|
|
device.Find("HTC") == 0) {
|
|
|
|
// HTC devices running Gingerbread 2.3.4+ (HTC Desire HD, HTC Evo Design, etc) seem to
|
|
|
|
// use a newer version of Gingerbread libstagefright than other 2.3.4 devices.
|
2013-03-04 10:34:03 -08:00
|
|
|
return "libomxplugingb.so";
|
2012-12-12 14:56:01 -08:00
|
|
|
}
|
2012-11-29 18:37:00 -08:00
|
|
|
else if (version == 9 || (version == 10 && release_version <= NS_LITERAL_STRING("2.3.5"))) {
|
|
|
|
// Gingerbread versions from 2.3.5 and below have a different DataSource
|
|
|
|
// than 2.3.6 and above.
|
2013-03-04 10:34:03 -08:00
|
|
|
return "libomxplugingb235.so";
|
2012-11-29 18:37:00 -08:00
|
|
|
}
|
2012-12-17 17:44:10 -08:00
|
|
|
else if (version == 8) {
|
|
|
|
// Froyo
|
2013-03-04 10:34:03 -08:00
|
|
|
return "libomxpluginfroyo.so";
|
2012-12-17 17:44:10 -08:00
|
|
|
}
|
|
|
|
else if (version < 8) {
|
|
|
|
// Below Froyo not supported
|
2012-11-29 18:37:00 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-01-08 17:54:57 -08:00
|
|
|
// Ice Cream Sandwich and Jellybean
|
2013-03-04 10:34:03 -08:00
|
|
|
return "libomxplugin.so";
|
2012-11-29 18:37:00 -08:00
|
|
|
|
|
|
|
#elif defined(ANDROID) && defined(MOZ_WIDGET_GONK)
|
|
|
|
return "libomxplugin.so";
|
2012-12-04 14:57:44 -08:00
|
|
|
#else
|
|
|
|
return nullptr;
|
2012-11-29 18:37:00 -08:00
|
|
|
#endif
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
AndroidMediaPluginHost::AndroidMediaPluginHost() {
|
|
|
|
MOZ_COUNT_CTOR(AndroidMediaPluginHost);
|
2012-11-29 18:37:00 -08:00
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
mResourceServer = AndroidMediaResourceServer::Start();
|
2013-08-29 21:51:38 -07:00
|
|
|
|
2012-11-29 18:37:00 -08:00
|
|
|
const char* name = GetOmxLibraryName();
|
|
|
|
ALOG("Loading OMX Plugin: %s", name ? name : "nullptr");
|
|
|
|
if (name) {
|
2013-03-04 10:34:03 -08:00
|
|
|
char *path = PR_GetLibraryFilePathname("libxul.so", (PRFuncPtr) GetOmxLibraryName);
|
2013-10-23 13:34:10 -07:00
|
|
|
PRLibrary *lib = nullptr;
|
2013-03-04 10:34:03 -08:00
|
|
|
if (path) {
|
|
|
|
nsAutoCString libpath(path);
|
|
|
|
PR_Free(path);
|
|
|
|
int32_t slash = libpath.RFindChar('/');
|
|
|
|
if (slash != kNotFound) {
|
|
|
|
libpath.Truncate(slash + 1);
|
|
|
|
libpath.Append(name);
|
|
|
|
lib = PR_LoadLibrary(libpath.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!lib)
|
|
|
|
lib = PR_LoadLibrary(name);
|
|
|
|
|
2012-11-29 18:37:00 -08:00
|
|
|
if (lib) {
|
|
|
|
Manifest *manifest = static_cast<Manifest *>(PR_FindSymbol(lib, "MPAPI_MANIFEST"));
|
|
|
|
if (manifest) {
|
|
|
|
mPlugins.AppendElement(manifest);
|
|
|
|
ALOG("OMX plugin successfully loaded");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
AndroidMediaPluginHost::~AndroidMediaPluginHost() {
|
2013-08-29 21:51:38 -07:00
|
|
|
mResourceServer->Stop();
|
2014-07-16 18:32:56 -07:00
|
|
|
MOZ_COUNT_DTOR(AndroidMediaPluginHost);
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
bool AndroidMediaPluginHost::FindDecoder(const nsACString& aMimeType, const char* const** aCodecs)
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
|
|
|
const char *chars;
|
2012-07-30 07:20:58 -07:00
|
|
|
size_t len = NS_CStringGetData(aMimeType, &chars, nullptr);
|
2012-05-31 17:54:23 -07:00
|
|
|
for (size_t n = 0; n < mPlugins.Length(); ++n) {
|
|
|
|
Manifest *plugin = mPlugins[n];
|
|
|
|
const char* const *codecs;
|
|
|
|
if (plugin->CanDecode(chars, len, &codecs)) {
|
|
|
|
if (aCodecs)
|
|
|
|
*aCodecs = codecs;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
MPAPI::Decoder *AndroidMediaPluginHost::CreateDecoder(MediaResource *aResource, const nsACString& aMimeType)
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
2013-05-02 15:59:18 -07:00
|
|
|
NS_ENSURE_TRUE(aResource, nullptr);
|
2012-05-31 17:54:23 -07:00
|
|
|
|
2013-05-02 15:59:18 -07:00
|
|
|
nsAutoPtr<Decoder> decoder(new Decoder());
|
2012-05-31 17:54:23 -07:00
|
|
|
if (!decoder) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2013-05-02 15:59:18 -07:00
|
|
|
const char *chars;
|
|
|
|
size_t len = NS_CStringGetData(aMimeType, &chars, nullptr);
|
2012-05-31 17:54:23 -07:00
|
|
|
for (size_t n = 0; n < mPlugins.Length(); ++n) {
|
|
|
|
Manifest *plugin = mPlugins[n];
|
|
|
|
const char* const *codecs;
|
|
|
|
if (!plugin->CanDecode(chars, len, &codecs)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-29 21:51:38 -07:00
|
|
|
|
|
|
|
nsCString url;
|
|
|
|
nsresult rv = mResourceServer->AddResource(aResource, url);
|
|
|
|
if (NS_FAILED (rv)) continue;
|
|
|
|
|
|
|
|
decoder->mResource = strdup(url.get());
|
2012-05-31 17:54:23 -07:00
|
|
|
if (plugin->CreateDecoder(&sPluginHost, decoder, chars, len)) {
|
2013-05-02 15:59:18 -07:00
|
|
|
aResource->AddRef();
|
|
|
|
return decoder.forget();
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
void AndroidMediaPluginHost::DestroyDecoder(Decoder *aDecoder)
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
|
|
|
aDecoder->DestroyDecoder(aDecoder);
|
2013-08-29 21:51:38 -07:00
|
|
|
char* resource = GetResource(aDecoder);
|
2013-05-02 15:59:18 -07:00
|
|
|
if (resource) {
|
|
|
|
// resource *shouldn't* be null, but check anyway just in case the plugin
|
|
|
|
// decoder does something stupid.
|
2013-08-29 21:51:38 -07:00
|
|
|
mResourceServer->RemoveResource(nsCString(resource));
|
|
|
|
free(resource);
|
2013-05-02 15:59:18 -07:00
|
|
|
}
|
2012-05-31 17:54:23 -07:00
|
|
|
delete aDecoder;
|
|
|
|
}
|
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
AndroidMediaPluginHost *sAndroidMediaPluginHost = nullptr;
|
|
|
|
AndroidMediaPluginHost *GetAndroidMediaPluginHost()
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
2014-07-16 18:32:56 -07:00
|
|
|
if (!sAndroidMediaPluginHost) {
|
|
|
|
sAndroidMediaPluginHost = new AndroidMediaPluginHost();
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
2014-07-16 18:32:56 -07:00
|
|
|
return sAndroidMediaPluginHost;
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:32:56 -07:00
|
|
|
void AndroidMediaPluginHost::Shutdown()
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
2014-07-16 18:32:56 -07:00
|
|
|
delete sAndroidMediaPluginHost;
|
|
|
|
sAndroidMediaPluginHost = nullptr;
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|