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"
|
2012-05-31 17:54:23 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "nsTimeRanges.h"
|
|
|
|
#include "MediaResource.h"
|
|
|
|
#include "nsHTMLMediaElement.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "MediaPluginHost.h"
|
2012-05-31 17:54:23 -07:00
|
|
|
#include "nsXPCOMStrings.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "pratom.h"
|
2012-11-14 11:46:40 -08:00
|
|
|
#include "MediaPluginReader.h"
|
2012-11-01 14:13:10 -07:00
|
|
|
#include "nsIGfxInfo.h"
|
2012-05-31 17:54:23 -07:00
|
|
|
|
|
|
|
#include "MPAPI.h"
|
|
|
|
|
|
|
|
using namespace MPAPI;
|
2012-11-14 11:45:33 -08:00
|
|
|
|
|
|
|
Decoder::Decoder() :
|
|
|
|
mResource(NULL), mPrivate(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
2012-05-31 17:54:23 -07:00
|
|
|
|
|
|
|
static MediaResource *GetResource(Decoder *aDecoder)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<MediaResource *>(aDecoder->mResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool Read(Decoder *aDecoder, char *aBuffer, int64_t aOffset, uint32_t aCount, uint32_t* aBytes)
|
|
|
|
{
|
|
|
|
MediaResource *resource = GetResource(aDecoder);
|
|
|
|
if (aOffset != resource->Tell()) {
|
|
|
|
nsresult rv = resource->Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsresult rv = resource->Read(aBuffer, aCount, aBytes);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t GetLength(Decoder *aDecoder)
|
|
|
|
{
|
|
|
|
return GetResource(aDecoder)->GetLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetMetaDataReadMode(Decoder *aDecoder)
|
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
GetResource(aDecoder)->SetReadMode(MediaCacheStream::MODE_METADATA);
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetPlaybackReadMode(Decoder *aDecoder)
|
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
GetResource(aDecoder)->SetReadMode(MediaCacheStream::MODE_PLAYBACK);
|
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);
|
2012-08-24 14:20:39 -07:00
|
|
|
nsCOMPtr<GetIntPrefEvent> event = new GetIntPrefEvent(aPref, aResult);
|
|
|
|
return NS_SUCCEEDED(NS_DispatchToMainThread(event, NS_DISPATCH_SYNC));
|
|
|
|
}
|
|
|
|
|
2012-05-31 17:54:23 -07:00
|
|
|
static PluginHost sPluginHost = {
|
|
|
|
Read,
|
|
|
|
GetLength,
|
|
|
|
SetMetaDataReadMode,
|
2012-08-24 14:20:39 -07:00
|
|
|
SetPlaybackReadMode,
|
|
|
|
GetIntPref
|
2012-05-31 17:54:23 -07:00
|
|
|
};
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void MediaPluginHost::TryLoad(const char *name)
|
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");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))) {
|
|
|
|
if (status != nsIGfxInfo::FEATURE_NO_INFO) {
|
|
|
|
NS_WARNING("XXX stagefright blacklisted\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 17:54:23 -07:00
|
|
|
PRLibrary *lib = PR_LoadLibrary(name);
|
|
|
|
if (lib) {
|
|
|
|
Manifest *manifest = static_cast<Manifest *>(PR_FindSymbol(lib, "MPAPI_MANIFEST"));
|
|
|
|
if (manifest)
|
|
|
|
mPlugins.AppendElement(manifest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaPluginHost::MediaPluginHost() {
|
|
|
|
MOZ_COUNT_CTOR(MediaPluginHost);
|
2012-08-10 01:15:20 -07:00
|
|
|
#if defined(ANDROID) && !defined(MOZ_WIDGET_GONK)
|
|
|
|
TryLoad("lib/libomxplugin.so");
|
|
|
|
#elif defined(ANDROID) && defined(MOZ_WIDGET_GONK)
|
2012-05-31 17:54:23 -07:00
|
|
|
TryLoad("libomxplugin.so");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaPluginHost::~MediaPluginHost() {
|
|
|
|
MOZ_COUNT_DTOR(MediaPluginHost);
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
bool MediaPluginHost::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;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
MPAPI::Decoder *MediaPluginHost::CreateDecoder(MediaResource *aResource, const nsACString& aMimeType)
|
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
|
|
|
|
|
|
|
Decoder *decoder = new Decoder();
|
|
|
|
if (!decoder) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
decoder->mResource = aResource;
|
|
|
|
|
|
|
|
for (size_t n = 0; n < mPlugins.Length(); ++n) {
|
|
|
|
Manifest *plugin = mPlugins[n];
|
|
|
|
const char* const *codecs;
|
|
|
|
if (!plugin->CanDecode(chars, len, &codecs)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (plugin->CreateDecoder(&sPluginHost, decoder, chars, len)) {
|
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void MediaPluginHost::DestroyDecoder(Decoder *aDecoder)
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
|
|
|
aDecoder->DestroyDecoder(aDecoder);
|
|
|
|
delete aDecoder;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaPluginHost *sMediaPluginHost = nullptr;
|
|
|
|
MediaPluginHost *GetMediaPluginHost()
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
|
|
|
if (!sMediaPluginHost) {
|
2012-11-14 11:46:40 -08:00
|
|
|
sMediaPluginHost = new MediaPluginHost();
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
return sMediaPluginHost;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
void MediaPluginHost::Shutdown()
|
2012-05-31 17:54:23 -07:00
|
|
|
{
|
|
|
|
if (sMediaPluginHost) {
|
|
|
|
delete sMediaPluginHost;
|
2012-07-30 07:20:58 -07:00
|
|
|
sMediaPluginHost = nullptr;
|
2012-05-31 17:54:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|