Bug 1086693 - Part 6: Use generated bindings for AndroidMediaCodec and AndroidSurfaceTexture r=blassey,gcp

This commit is contained in:
James Willcox 2014-11-13 12:47:24 -06:00
parent 2bbaa38108
commit a24b125d4b
13 changed files with 181 additions and 2511 deletions

View File

@ -3,13 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AndroidDecoderModule.h"
#include "PlatformDecoderModule.h"
#include "GeneratedJNIWrappers.h"
#include "GeneratedSDKWrappers.h"
#include "AndroidBridge.h"
#include "MediaTaskQueue.h"
#include "SharedThreadPool.h"
#include "TexturePoolOGL.h"
#include "GLImages.h"
#include "MediaData.h"
@ -26,7 +20,7 @@
using namespace mozilla;
using namespace mozilla::gl;
using namespace mozilla::widget::android;
using namespace mozilla::widget::android::sdk;
namespace mozilla {
@ -36,10 +30,7 @@ static MediaCodec* CreateDecoder(JNIEnv* aEnv, const char* aMimeType)
return nullptr;
}
nsAutoString mimeType;
mimeType.AssignASCII(aMimeType);
jobject decoder = MediaCodec::CreateDecoderByType(mimeType);
jobject decoder = MediaCodec::CreateDecoderByType(nsCString(aMimeType));
return new MediaCodec(decoder, aEnv);
}
@ -103,7 +94,7 @@ public:
protected:
layers::ImageContainer* mImageContainer;
const mp4_demuxer::VideoDecoderConfig& mConfig;
nsRefPtr<AndroidSurfaceTexture> mSurfaceTexture;
RefPtr<AndroidSurfaceTexture> mSurfaceTexture;
};
class AudioDataDecoder : public MediaCodecDataDecoder {
@ -115,11 +106,11 @@ public:
virtual nsresult Input(mp4_demuxer::MP4Sample* aSample) MOZ_OVERRIDE {
if (!strcmp(mMimeType, "audio/mp4a-latm")) {
uint32_t numChannels = mFormat->GetInteger(NS_LITERAL_STRING("channel-count"));
uint32_t sampleRate = mFormat->GetInteger(NS_LITERAL_STRING("sample-rate"));
uint32_t numChannels = mFormat->GetInteger(NS_LITERAL_CSTRING("channel-count"));
uint32_t sampleRate = mFormat->GetInteger(NS_LITERAL_CSTRING("sample-rate"));
uint8_t frequencyIndex =
mp4_demuxer::Adts::GetFrequencyIndex(sampleRate);
uint32_t aacProfile = mFormat->GetInteger(NS_LITERAL_STRING("aac-profile"));
uint32_t aacProfile = mFormat->GetInteger(NS_LITERAL_CSTRING("aac-profile"));
bool rv = mp4_demuxer::Adts::ConvertSample(numChannels,
frequencyIndex,
aacProfile,
@ -135,8 +126,8 @@ public:
nsresult Output(BufferInfo* aInfo, void* aBuffer, MediaFormat* aFormat, Microseconds aDuration) {
// The output on Android is always 16-bit signed
uint32_t numChannels = aFormat->GetInteger(NS_LITERAL_STRING("channel-count"));
uint32_t sampleRate = aFormat->GetInteger(NS_LITERAL_STRING("sample-rate"));
uint32_t numChannels = aFormat->GetInteger(NS_LITERAL_CSTRING("channel-count"));
uint32_t sampleRate = aFormat->GetInteger(NS_LITERAL_CSTRING("sample-rate"));
uint32_t numFrames = (aInfo->getSize() / numChannels) / 2;
AudioDataValue* audio = new AudioDataValue[aInfo->getSize()];
@ -169,10 +160,7 @@ AndroidDecoderModule::CreateH264Decoder(
MediaTaskQueue* aVideoTaskQueue,
MediaDataDecoderCallback* aCallback)
{
nsAutoString mimeType;
mimeType.AssignASCII(aConfig.mime_type);
jobject jFormat = MediaFormat::CreateVideoFormat(mimeType,
jobject jFormat = MediaFormat::CreateVideoFormat(nsCString(aConfig.mime_type),
aConfig.display_width,
aConfig.display_height);
@ -199,10 +187,7 @@ AndroidDecoderModule::CreateAudioDecoder(const mp4_demuxer::AudioDecoderConfig&
{
MOZ_ASSERT(aConfig.bits_per_sample == 16, "We only handle 16-bit audio!");
nsAutoString mimeType;
mimeType.AssignASCII(aConfig.mime_type);
jobject jFormat = MediaFormat::CreateAudioFormat(mimeType,
jobject jFormat = MediaFormat::CreateAudioFormat(nsCString(aConfig.mime_type),
aConfig.samples_per_second,
aConfig.channel_count);
@ -216,21 +201,21 @@ AndroidDecoderModule::CreateAudioDecoder(const mp4_demuxer::AudioDecoderConfig&
JNIEnv* env = GetJNIForThread();
if (!format->GetByteBuffer(NS_LITERAL_STRING("csd-0"))) {
if (!format->GetByteBuffer(NS_LITERAL_CSTRING("csd-0"))) {
uint8_t* csd0 = new uint8_t[2];
csd0[0] = aConfig.audio_specific_config[0];
csd0[1] = aConfig.audio_specific_config[1];
jobject buffer = env->NewDirectByteBuffer(csd0, 2);
format->SetByteBuffer(NS_LITERAL_STRING("csd-0"), buffer);
format->SetByteBuffer(NS_LITERAL_CSTRING("csd-0"), buffer);
env->DeleteLocalRef(buffer);
}
if (mimeType.EqualsLiteral("audio/mp4a-latm")) {
format->SetInteger(NS_LITERAL_STRING("is-adts"), 1);
format->SetInteger(NS_LITERAL_STRING("aac-profile"), aConfig.aac_profile);
if (strcmp(aConfig.mime_type, "audio/mp4a-latm") == 0) {
format->SetInteger(NS_LITERAL_CSTRING("is-adts"), 1);
format->SetInteger(NS_LITERAL_CSTRING("aac-profile"), aConfig.aac_profile);
}
nsRefPtr<MediaDataDecoder> decoder =
@ -294,15 +279,26 @@ nsresult MediaCodecDataDecoder::InitDecoder(jobject aSurface)
return NS_ERROR_FAILURE;
}
if (!mDecoder->Configure(mFormat->wrappedObject(), aSurface, nullptr, 0)) {
mCallback->Error();
return NS_ERROR_FAILURE;
nsresult res;
mDecoder->Configure(mFormat->wrappedObject(), aSurface, nullptr, 0, &res);
if (NS_FAILED(res)) {
return res;
}
mDecoder->Start();
mDecoder->Start(&res);
if (NS_FAILED(res)) {
return res;
}
ResetInputBuffers();
ResetOutputBuffers();
res = ResetInputBuffers();
if (NS_FAILED(res)) {
return res;
}
res = ResetOutputBuffers();
if (NS_FAILED(res)) {
return res;
}
NS_NewNamedThread("MC Decoder", getter_AddRefs(mThread),
NS_NewRunnableMethod(this, &MediaCodecDataDecoder::DecoderLoop));
@ -321,6 +317,7 @@ void MediaCodecDataDecoder::DecoderLoop()
mp4_demuxer::MP4Sample* sample = nullptr;
nsAutoPtr<MediaFormat> outputFormat;
nsresult res;
for (;;) {
{
@ -354,7 +351,13 @@ void MediaCodecDataDecoder::DecoderLoop()
if (sample) {
// We have a sample, try to feed it to the decoder
int inputIndex = mDecoder->DequeueInputBuffer(DECODER_TIMEOUT);
int inputIndex = mDecoder->DequeueInputBuffer(DECODER_TIMEOUT, &res);
if (NS_FAILED(res)) {
printf_stderr("exiting decoder loop due to exception while dequeuing input\n");
mCallback->Error();
break;
}
if (inputIndex >= 0) {
jobject buffer = env->GetObjectArrayElement(mInputBuffers, inputIndex);
void* directBuffer = env->GetDirectBufferAddress(buffer);
@ -369,7 +372,13 @@ void MediaCodecDataDecoder::DecoderLoop()
PodCopy((uint8_t*)directBuffer, sample->data, sample->size);
mDecoder->QueueInputBuffer(inputIndex, 0, sample->size, sample->composition_timestamp, 0);
mDecoder->QueueInputBuffer(inputIndex, 0, sample->size, sample->composition_timestamp, 0, &res);
if (NS_FAILED(res)) {
printf_stderr("exiting decoder loop due to exception while queuing input\n");
mCallback->Error();
break;
}
mDurations.push(sample->duration);
delete sample;
@ -383,12 +392,23 @@ void MediaCodecDataDecoder::DecoderLoop()
if (!outputDone) {
BufferInfo bufferInfo;
int outputStatus = mDecoder->DequeueOutputBuffer(bufferInfo.wrappedObject(), DECODER_TIMEOUT);
int outputStatus = mDecoder->DequeueOutputBuffer(bufferInfo.wrappedObject(), DECODER_TIMEOUT, &res);
if (NS_FAILED(res)) {
printf_stderr("exiting decoder loop due to exception while dequeuing output\n");
mCallback->Error();
break;
}
if (outputStatus == MediaCodec::getINFO_TRY_AGAIN_LATER()) {
// We might want to call mCallback->InputExhausted() here, but there seems to be
// some possible bad interactions here with the threading
} else if (outputStatus == MediaCodec::getINFO_OUTPUT_BUFFERS_CHANGED()) {
ResetOutputBuffers();
res = ResetOutputBuffers();
if (NS_FAILED(res)) {
printf_stderr("exiting decoder loop due to exception while restting output buffers\n");
mCallback->Error();
break;
}
} else if (outputStatus == MediaCodec::getINFO_OUTPUT_FORMAT_CHANGED()) {
outputFormat = new MediaFormat(mDecoder->GetOutputFormat(), GetJNIForThread());
} else if (outputStatus < 0) {
@ -454,7 +474,7 @@ nsresult MediaCodecDataDecoder::Input(mp4_demuxer::MP4Sample* aSample) {
return NS_OK;
}
void MediaCodecDataDecoder::ResetInputBuffers()
nsresult MediaCodecDataDecoder::ResetInputBuffers()
{
JNIEnv* env = GetJNIForThread();
@ -462,10 +482,16 @@ void MediaCodecDataDecoder::ResetInputBuffers()
env->DeleteGlobalRef(mInputBuffers);
}
mInputBuffers = (jobjectArray) env->NewGlobalRef(mDecoder->GetInputBuffers());
nsresult res;
mInputBuffers = (jobjectArray) env->NewGlobalRef(mDecoder->GetInputBuffers(&res));
if (NS_FAILED(res)) {
return res;
}
return NS_OK;
}
void MediaCodecDataDecoder::ResetOutputBuffers()
nsresult MediaCodecDataDecoder::ResetOutputBuffers()
{
JNIEnv* env = GetJNIForThread();
@ -473,7 +499,13 @@ void MediaCodecDataDecoder::ResetOutputBuffers()
env->DeleteGlobalRef(mOutputBuffers);
}
mOutputBuffers = (jobjectArray) env->NewGlobalRef(mDecoder->GetOutputBuffers());
nsresult res;
mOutputBuffers = (jobjectArray) env->NewGlobalRef(mDecoder->GetOutputBuffers(&res));
if (NS_FAILED(res)) {
return res;
}
return NS_OK;
}
nsresult MediaCodecDataDecoder::Flush() {
@ -515,6 +547,7 @@ nsresult MediaCodecDataDecoder::Shutdown() {
mDecoder->Stop();
mDecoder->Release();
return NS_OK;
}

View File

@ -6,10 +6,9 @@
#define AndroidDecoderModule_h_
#include "PlatformDecoderModule.h"
#include "AndroidJavaWrappers.h"
#include "AndroidSurfaceTexture.h"
#include "GeneratedSDKWrappers.h"
#include "MediaCodec.h"
#include "mozilla/Monitor.h"
#include <queue>
@ -20,11 +19,13 @@ typedef std::queue<mp4_demuxer::MP4Sample*> SampleQueue;
namespace widget {
namespace android {
namespace sdk {
class MediaCodec;
class MediaFormat;
class ByteBuffer;
}
}
}
class MediaCodecDataDecoder;
@ -56,7 +57,7 @@ public:
MediaCodecDataDecoder(MediaData::Type aType,
const char* aMimeType,
mozilla::widget::android::MediaFormat* aFormat,
mozilla::widget::android::sdk::MediaFormat* aFormat,
MediaDataDecoderCallback* aCallback);
virtual ~MediaCodecDataDecoder();
@ -73,11 +74,11 @@ protected:
MediaData::Type mType;
nsAutoPtr<char> mMimeType;
nsAutoPtr<mozilla::widget::android::MediaFormat> mFormat;
nsAutoPtr<mozilla::widget::android::sdk::MediaFormat> mFormat;
MediaDataDecoderCallback* mCallback;
nsAutoPtr<mozilla::widget::android::MediaCodec> mDecoder;
nsAutoPtr<mozilla::widget::android::sdk::MediaCodec> mDecoder;
jobjectArray mInputBuffers;
jobjectArray mOutputBuffers;
@ -94,11 +95,11 @@ protected:
virtual nsresult InitDecoder(jobject aSurface = nullptr);
virtual nsresult Output(mozilla::widget::android::BufferInfo* aInfo, void* aBuffer, mozilla::widget::android::MediaFormat* aFormat, Microseconds aDuration) { return NS_OK; }
virtual nsresult PostOutput(mozilla::widget::android::BufferInfo* aInfo, mozilla::widget::android::MediaFormat* aFormat, Microseconds aDuration) { return NS_OK; }
virtual nsresult Output(mozilla::widget::android::sdk::BufferInfo* aInfo, void* aBuffer, mozilla::widget::android::sdk::MediaFormat* aFormat, Microseconds aDuration) { return NS_OK; }
virtual nsresult PostOutput(mozilla::widget::android::sdk::BufferInfo* aInfo, mozilla::widget::android::sdk::MediaFormat* aFormat, Microseconds aDuration) { return NS_OK; }
void ResetInputBuffers();
void ResetOutputBuffers();
nsresult ResetInputBuffers();
nsresult ResetOutputBuffers();
void DecoderLoop();
virtual void ClearQueue();

View File

@ -952,7 +952,7 @@ void nsNPAPIPluginInstance::ReleaseContentTexture(nsNPAPIPluginInstance::Texture
mContentTexture->Release(aTextureInfo);
}
AndroidSurfaceTexture* nsNPAPIPluginInstance::CreateSurfaceTexture()
TemporaryRef<AndroidSurfaceTexture> nsNPAPIPluginInstance::CreateSurfaceTexture()
{
if (!EnsureGLContext())
return nullptr;
@ -961,14 +961,15 @@ AndroidSurfaceTexture* nsNPAPIPluginInstance::CreateSurfaceTexture()
if (!texture)
return nullptr;
AndroidSurfaceTexture* surface = AndroidSurfaceTexture::Create(TexturePoolOGL::GetGLContext(),
texture);
if (!surface)
RefPtr<AndroidSurfaceTexture> surface = AndroidSurfaceTexture::Create(TexturePoolOGL::GetGLContext(),
texture);
if (!surface) {
return nullptr;
}
nsCOMPtr<nsIRunnable> frameCallback = NS_NewRunnableMethod(this, &nsNPAPIPluginInstance::OnSurfaceTextureFrameAvailable);
surface->SetFrameAvailableCallback(frameCallback);
return surface;
return surface.forget();
}
void nsNPAPIPluginInstance::OnSurfaceTextureFrameAvailable()
@ -1009,9 +1010,10 @@ nsNPAPIPluginInstance::AsSurfaceTexture()
void* nsNPAPIPluginInstance::AcquireVideoWindow()
{
AndroidSurfaceTexture* surface = CreateSurfaceTexture();
if (!surface)
RefPtr<AndroidSurfaceTexture> surface = CreateSurfaceTexture();
if (!surface) {
return nullptr;
}
VideoInfo* info = new VideoInfo(surface);

View File

@ -207,7 +207,7 @@ public:
mSurfaceTexture = nullptr;
}
nsRefPtr<mozilla::gl::AndroidSurfaceTexture> mSurfaceTexture;
mozilla::RefPtr<mozilla::gl::AndroidSurfaceTexture> mSurfaceTexture;
gfxRect mDimensions;
};
@ -328,8 +328,8 @@ protected:
bool mFullScreen;
bool mInverted;
nsRefPtr<SharedPluginTexture> mContentTexture;
nsRefPtr<mozilla::gl::AndroidSurfaceTexture> mContentSurface;
mozilla::RefPtr<SharedPluginTexture> mContentTexture;
mozilla::RefPtr<mozilla::gl::AndroidSurfaceTexture> mContentSurface;
#endif
enum {
@ -378,7 +378,7 @@ private:
#ifdef MOZ_WIDGET_ANDROID
void EnsureSharedTexture();
mozilla::gl::AndroidSurfaceTexture* CreateSurfaceTexture();
mozilla::TemporaryRef<mozilla::gl::AndroidSurfaceTexture> CreateSurfaceTexture();
std::map<void*, VideoInfo*> mVideos;
bool mOnScreen;

View File

@ -15,10 +15,12 @@
#include "nsThreadUtils.h"
#include "mozilla/gfx/Matrix.h"
#include "GeneratedJNIWrappers.h"
#include "SurfaceTexture.h"
#include "GLContext.h"
using namespace mozilla;
using namespace mozilla::widget::android;
using namespace mozilla::widget::android::sdk;
namespace mozilla {
namespace gl {
@ -39,192 +41,26 @@ IsSTSupported()
return AndroidBridge::Bridge()->GetAPIVersion() >= 14; /* ICS */
}
static class JNIFunctions {
public:
JNIFunctions() : mInitialized(false)
{
}
bool EnsureInitialized()
{
if (mInitialized) {
return true;
}
if (!IsSTSupported()) {
return false;
}
JNIEnv* env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env);
jSurfaceTextureClass = (jclass)env->NewGlobalRef(env->FindClass("android/graphics/SurfaceTexture"));
jSurfaceTexture_Ctor = env->GetMethodID(jSurfaceTextureClass, "<init>", "(I)V");
jSurfaceTexture_updateTexImage = env->GetMethodID(jSurfaceTextureClass, "updateTexImage", "()V");
jSurfaceTexture_getTransformMatrix = env->GetMethodID(jSurfaceTextureClass, "getTransformMatrix", "([F)V");
jSurfaceTexture_setDefaultBufferSize = env->GetMethodID(jSurfaceTextureClass, "setDefaultBufferSize", "(II)V");
if (IsDetachSupported()) {
jSurfaceTexture_attachToGLContext = env->GetMethodID(jSurfaceTextureClass, "attachToGLContext", "(I)V");
jSurfaceTexture_detachFromGLContext = env->GetMethodID(jSurfaceTextureClass, "detachFromGLContext", "()V");
} else {
jSurfaceTexture_attachToGLContext = jSurfaceTexture_detachFromGLContext = 0;
}
jSurfaceClass = (jclass)env->NewGlobalRef(env->FindClass("android/view/Surface"));
jSurface_Ctor = env->GetMethodID(jSurfaceClass, "<init>", "(Landroid/graphics/SurfaceTexture;)V");
mInitialized = true;
return true;
}
jobject CreateSurfaceTexture(GLuint aTexture)
{
if (!EnsureInitialized())
return nullptr;
JNIEnv* env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env);
return env->NewGlobalRef(env->NewObject(jSurfaceTextureClass, jSurfaceTexture_Ctor, (int) aTexture));
}
jobject CreateSurface(jobject aSurfaceTexture)
{
if (!EnsureInitialized())
return nullptr;
JNIEnv* env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env);
return env->NewGlobalRef(env->NewObject(jSurfaceClass, jSurface_Ctor, aSurfaceTexture));
}
void ReleaseSurfaceTexture(jobject aSurfaceTexture)
{
JNIEnv* env = GetJNIForThread();
env->DeleteGlobalRef(aSurfaceTexture);
}
void UpdateTexImage(jobject aSurfaceTexture)
{
JNIEnv* env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env);
env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_updateTexImage);
}
bool GetTransformMatrix(jobject aSurfaceTexture, gfx::Matrix4x4& aMatrix)
{
JNIEnv* env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env);
jfloatArray jarray = env->NewFloatArray(16);
env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_getTransformMatrix, jarray);
jfloat* array = env->GetFloatArrayElements(jarray, nullptr);
aMatrix._11 = array[0];
aMatrix._12 = array[1];
aMatrix._13 = array[2];
aMatrix._14 = array[3];
aMatrix._21 = array[4];
aMatrix._22 = array[5];
aMatrix._23 = array[6];
aMatrix._24 = array[7];
aMatrix._31 = array[8];
aMatrix._32 = array[9];
aMatrix._33 = array[10];
aMatrix._34 = array[11];
aMatrix._41 = array[12];
aMatrix._42 = array[13];
aMatrix._43 = array[14];
aMatrix._44 = array[15];
env->ReleaseFloatArrayElements(jarray, array, 0);
return false;
}
void SetDefaultBufferSize(jobject aSurfaceTexture, int32_t width, int32_t height)
{
JNIEnv* env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env);
env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_setDefaultBufferSize, width, height);
}
void AttachToGLContext(jobject aSurfaceTexture, int32_t texName)
{
MOZ_ASSERT(jSurfaceTexture_attachToGLContext);
JNIEnv* env = GetJNIForThread();
env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_attachToGLContext, texName);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
}
void DetachFromGLContext(jobject aSurfaceTexture)
{
MOZ_ASSERT(jSurfaceTexture_detachFromGLContext);
JNIEnv* env = GetJNIForThread();
env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_detachFromGLContext);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
}
private:
bool mInitialized;
jclass jSurfaceTextureClass;
jmethodID jSurfaceTexture_Ctor;
jmethodID jSurfaceTexture_updateTexImage;
jmethodID jSurfaceTexture_getTransformMatrix;
jmethodID jSurfaceTexture_setDefaultBufferSize;
jmethodID jSurfaceTexture_attachToGLContext;
jmethodID jSurfaceTexture_detachFromGLContext;
jclass jSurfaceClass;
jmethodID jSurface_Ctor;
} sJNIFunctions;
AndroidSurfaceTexture*
TemporaryRef<AndroidSurfaceTexture>
AndroidSurfaceTexture::Create()
{
return Create(nullptr, 0);
}
AndroidSurfaceTexture*
TemporaryRef<AndroidSurfaceTexture>
AndroidSurfaceTexture::Create(GLContext* aContext, GLuint aTexture)
{
if (!IsSTSupported()) {
return nullptr;
}
AndroidSurfaceTexture* st = new AndroidSurfaceTexture();
RefPtr<AndroidSurfaceTexture> st = new AndroidSurfaceTexture();
if (!st->Init(aContext, aTexture)) {
printf_stderr("Failed to initialize AndroidSurfaceTexture");
delete st;
st = nullptr;
}
return st;
return st.forget();
}
AndroidSurfaceTexture*
@ -239,30 +75,25 @@ AndroidSurfaceTexture::Find(int id)
return it->second;
}
bool
AndroidSurfaceTexture::Check()
{
return sJNIFunctions.EnsureInitialized();
}
bool
nsresult
AndroidSurfaceTexture::Attach(GLContext* aContext, PRIntervalTime aTimeout)
{
MonitorAutoLock lock(mMonitor);
if (mAttachedContext == aContext) {
NS_WARNING("Tried to attach same GLContext to AndroidSurfaceTexture");
return true;
return NS_OK;
}
if (!IsDetachSupported()) {
return false;
return NS_ERROR_NOT_AVAILABLE;
}
while (mAttachedContext) {
// Wait until it's detached (or we time out)
if (NS_FAILED(lock.Wait(aTimeout))) {
return false;
return NS_ERROR_NOT_AVAILABLE;
}
}
@ -272,29 +103,29 @@ AndroidSurfaceTexture::Attach(GLContext* aContext, PRIntervalTime aTimeout)
mAttachedContext->MakeCurrent();
aContext->fGenTextures(1, &mTexture);
sJNIFunctions.AttachToGLContext(mSurfaceTexture, mTexture);
return true;
nsresult res;
mSurfaceTexture->AttachToGLContext(mTexture, &res);
return res;
}
bool
nsresult
AndroidSurfaceTexture::Detach()
{
MonitorAutoLock lock(mMonitor);
if (!IsDetachSupported() ||
!mAttachedContext || !mAttachedContext->IsOwningThreadCurrent()) {
return false;
return NS_ERROR_FAILURE;
}
mAttachedContext->MakeCurrent();
// This call takes care of deleting the texture
sJNIFunctions.DetachFromGLContext(mSurfaceTexture);
mSurfaceTexture->DetachFromGLContext();
mTexture = 0;
mAttachedContext = nullptr;
lock.NotifyAll();
return true;
return NS_OK;
}
bool
@ -305,28 +136,26 @@ AndroidSurfaceTexture::Init(GLContext* aContext, GLuint aTexture)
return false;
}
if (!sJNIFunctions.EnsureInitialized())
return false;
JNIEnv* env = GetJNIForThread();
mSurfaceTexture = sJNIFunctions.CreateSurfaceTexture(aTexture);
if (!mSurfaceTexture) {
nsresult res;
mSurfaceTexture = new SurfaceTexture(aTexture, &res);
if (NS_FAILED(res)) {
return false;
}
if (!aTexture) {
sJNIFunctions.DetachFromGLContext(mSurfaceTexture);
mSurfaceTexture->DetachFromGLContext();
}
mAttachedContext = aContext;
mSurface = sJNIFunctions.CreateSurface(mSurfaceTexture);
if (!mSurface) {
mSurface = new Surface(mSurfaceTexture->wrappedObject(), &res);
if (NS_FAILED(res)) {
return false;
}
mNativeWindow = AndroidNativeWindow::CreateFromSurface(env, mSurface);
mNativeWindow = AndroidNativeWindow::CreateFromSurface(GetJNIForThread(),
mSurface->wrappedObject());
MOZ_ASSERT(mNativeWindow, "Failed to create native window from surface");
mID = ++sNextID;
sInstances.insert(std::pair<int, AndroidSurfaceTexture*>(mID, this));
@ -349,40 +178,60 @@ AndroidSurfaceTexture::~AndroidSurfaceTexture()
mFrameAvailableCallback = nullptr;
JNIEnv* env = GetJNIForThread();
if (mSurfaceTexture) {
GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture);
env->DeleteGlobalRef(mSurfaceTexture);
GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture->wrappedObject());
mSurfaceTexture = nullptr;
}
if (mSurface) {
env->DeleteGlobalRef(mSurface);
mSurface = nullptr;
}
}
void
AndroidSurfaceTexture::UpdateTexImage()
{
sJNIFunctions.UpdateTexImage(mSurfaceTexture);
mSurfaceTexture->UpdateTexImage();
}
bool
void
AndroidSurfaceTexture::GetTransformMatrix(gfx::Matrix4x4& aMatrix)
{
return sJNIFunctions.GetTransformMatrix(mSurfaceTexture, aMatrix);
JNIEnv* env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env);
jfloatArray jarray = env->NewFloatArray(16);
mSurfaceTexture->GetTransformMatrix(jarray);
jfloat* array = env->GetFloatArrayElements(jarray, nullptr);
aMatrix._11 = array[0];
aMatrix._12 = array[1];
aMatrix._13 = array[2];
aMatrix._14 = array[3];
aMatrix._21 = array[4];
aMatrix._22 = array[5];
aMatrix._23 = array[6];
aMatrix._24 = array[7];
aMatrix._31 = array[8];
aMatrix._32 = array[9];
aMatrix._33 = array[10];
aMatrix._34 = array[11];
aMatrix._41 = array[12];
aMatrix._42 = array[13];
aMatrix._43 = array[14];
aMatrix._44 = array[15];
env->ReleaseFloatArrayElements(jarray, array, 0);
}
void
AndroidSurfaceTexture::SetFrameAvailableCallback(nsIRunnable* aRunnable)
{
if (aRunnable) {
GeckoAppShell::RegisterSurfaceTextureFrameListener(mSurfaceTexture, mID);
GeckoAppShell::RegisterSurfaceTextureFrameListener(mSurfaceTexture->wrappedObject(), mID);
} else {
GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture);
GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture->wrappedObject());
}
mFrameAvailableCallback = aRunnable;
@ -391,7 +240,7 @@ AndroidSurfaceTexture::SetFrameAvailableCallback(nsIRunnable* aRunnable)
void
AndroidSurfaceTexture::SetDefaultSize(mozilla::gfx::IntSize size)
{
sJNIFunctions.SetDefaultBufferSize(mSurfaceTexture, size.width, size.height);
mSurfaceTexture->SetDefaultBufferSize(size.width, size.height);
}
void

View File

@ -15,6 +15,7 @@
#include "mozilla/gfx/2D.h"
#include "mozilla/Monitor.h"
#include "SurfaceTexture.h"
#include "AndroidNativeWindow.h"
class gfxASurface;
@ -42,30 +43,26 @@ public:
// The SurfaceTexture is created in an attached state. This method requires
// Android Ice Cream Sandwich.
static AndroidSurfaceTexture* Create(GLContext* aGLContext, GLuint aTexture);
static TemporaryRef<AndroidSurfaceTexture> Create(GLContext* aGLContext, GLuint aTexture);
// Here the SurfaceTexture will be created in a detached state. You must call
// Attach() with the GLContext you wish to composite with. It must be done
// on the thread where that GLContext is current. This method requires
// Android Jelly Bean.
static AndroidSurfaceTexture* Create();
static TemporaryRef<AndroidSurfaceTexture> Create();
static AndroidSurfaceTexture* Find(int id);
// Returns with reasonable certainty whether or not we'll
// be able to create and use a SurfaceTexture
static bool Check();
// If we are on Jelly Bean, the SurfaceTexture can be detached and reattached
// to allow consumption from different GLContexts. It is recommended to only
// attach while you are consuming in order to allow this.
//
// Only one GLContext may be attached at any given time. If another is already
// attached, we try to wait for it to become detached.
bool Attach(GLContext* aContext, PRIntervalTime aTiemout = PR_INTERVAL_NO_TIMEOUT);
nsresult Attach(GLContext* aContext, PRIntervalTime aTiemout = PR_INTERVAL_NO_TIMEOUT);
// This is a noop on ICS, and will always fail
bool Detach();
nsresult Detach();
GLContext* GetAttachedContext() { return mAttachedContext; }
@ -76,7 +73,7 @@ public:
// This attaches the updated data to the TEXTURE_EXTERNAL target
void UpdateTexImage();
bool GetTransformMatrix(mozilla::gfx::Matrix4x4& aMatrix);
void GetTransformMatrix(mozilla::gfx::Matrix4x4& aMatrix);
int ID() { return mID; }
void SetDefaultSize(mozilla::gfx::IntSize size);
@ -90,7 +87,7 @@ public:
void NotifyFrameAvailable();
GLuint Texture() { return mTexture; }
jobject JavaSurface() { return mSurface; }
jobject JavaSurface() { return mSurface->wrappedObject(); }
private:
AndroidSurfaceTexture();
~AndroidSurfaceTexture();
@ -98,8 +95,8 @@ private:
bool Init(GLContext* aContext, GLuint aTexture);
GLuint mTexture;
jobject mSurfaceTexture;
jobject mSurface;
nsAutoPtr<mozilla::widget::android::sdk::SurfaceTexture> mSurfaceTexture;
nsAutoPtr<mozilla::widget::android::sdk::Surface> mSurface;
Monitor mMonitor;
GLContext* mAttachedContext;

View File

@ -728,7 +728,7 @@ GLBlitHelper::BlitSurfaceTextureImage(layers::SurfaceTextureImage* stImage)
ScopedBindTextureUnit boundTU(mGL, LOCAL_GL_TEXTURE0);
mGL->fClear(LOCAL_GL_COLOR_BUFFER_BIT);
if (!surfaceTexture->Attach(mGL)) {
if (NS_FAILED(surfaceTexture->Attach(mGL))) {
return false;
}

View File

@ -534,9 +534,7 @@ SurfaceTextureHost::Lock()
mSize);
}
mSurfTex->Attach(gl());
return true;
return NS_SUCCEEDED(mSurfTex->Attach(gl()));
}
void

View File

@ -41,7 +41,9 @@
#include "nsContentUtils.h"
#include "nsIScriptError.h"
#include "nsIHttpChannel.h"
#include "GeneratedSDKWrappers.h"
#include "MediaCodec.h"
#include "SurfaceTexture.h"
using namespace mozilla;
using namespace mozilla::widget::android;
@ -222,8 +224,11 @@ AndroidBridge::Init(JNIEnv *jEnv)
InitAndroidJavaWrappers(jEnv);
if (mAPIVersion >= 16 /* Jelly Bean */) {
// We only use this for MediaCodec right now
InitSDKStubs(jEnv);
sdk::InitMediaCodecStubs(jEnv);
}
if (mAPIVersion >= 14 /* ICS */) {
sdk::InitSurfaceTextureStubs(jEnv);
}
// jEnv should NOT be cached here by anything -- the jEnv here

View File

@ -11,7 +11,6 @@
#include "mozilla/BasicEvents.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/TouchEvents.h"
#include "GeneratedSDKWrappers.h"
using namespace mozilla;
using namespace mozilla::dom;

File diff suppressed because it is too large Load Diff

View File

@ -1,335 +0,0 @@
// GENERATED CODE
// NOTE: This code has been doctored. The JarClassProcessor is still a work in progress,
// and so additions and deletions have been made to make this file valid.
// Generated by the Java program at /build/jarClassProcessors at compile time from
// a given set of jars and a set of requested methods. To update, change the annotations
// on the corresponding Java methods and rerun the build. Manually updating this file
// will cause your build to fail.
#ifndef GeneratedSDKWrappers_h__
#define GeneratedSDKWrappers_h__
#include "nsXPCOMStrings.h"
#include "AndroidJavaWrappers.h"
namespace mozilla {
namespace widget {
namespace android {
#define MEDIACODEC_EXCEPTION_INDEX -255
void InitSDKStubs(JNIEnv *jEnv);
class MediaCodec : public AutoGlobalWrappedJavaObject {
public:
static void InitStubs(JNIEnv *jEnv);
static MediaCodec* Wrap(jobject obj);
MediaCodec(jobject obj, JNIEnv* env) : AutoGlobalWrappedJavaObject(obj, env) {};
bool Configure(jobject a0, jobject a1, jobject a2, int32_t a3);
static jobject CreateByCodecName(const nsAString& a0);
static jobject CreateDecoderByType(const nsAString& a0);
static jobject CreateEncoderByType(const nsAString& a0);
int32_t DequeueInputBuffer(int64_t a0);
int32_t DequeueOutputBuffer(jobject a0, int64_t a1);
void Finalize();
void Flush();
jobjectArray GetInputBuffers();
jobjectArray GetOutputBuffers();
jobject GetOutputFormat();
void QueueInputBuffer(int32_t a0, int32_t a1, int32_t a2, int64_t a3, int32_t a4);
void QueueSecureInputBuffer(int32_t a0, int32_t a1, jobject a2, int64_t a3, int32_t a4);
void Release();
void ReleaseOutputBuffer(int32_t a0, bool a1);
void SetVideoScalingMode(int32_t a0);
bool Start();
void Stop();
static int32_t getBUFFER_FLAG_CODEC_CONFIG();
static int32_t getBUFFER_FLAG_END_OF_STREAM();
static int32_t getBUFFER_FLAG_SYNC_FRAME();
static int32_t getCONFIGURE_FLAG_ENCODE();
static int32_t getCRYPTO_MODE_AES_CTR();
static int32_t getCRYPTO_MODE_UNENCRYPTED();
static int32_t getINFO_OUTPUT_BUFFERS_CHANGED();
static int32_t getINFO_OUTPUT_FORMAT_CHANGED();
static int32_t getINFO_TRY_AGAIN_LATER();
static int32_t getVIDEO_SCALING_MODE_SCALE_TO_FIT();
static int32_t getVIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING();
MediaCodec() : AutoGlobalWrappedJavaObject() {};
protected:
static jclass mMediaCodecClass;
static jmethodID jConfigure;
static jmethodID jCreateByCodecName;
static jmethodID jCreateDecoderByType;
static jmethodID jCreateEncoderByType;
static jmethodID jDequeueInputBuffer;
static jmethodID jDequeueOutputBuffer;
static jmethodID jFinalize;
static jmethodID jFlush;
static jmethodID jGetInputBuffers;
static jmethodID jGetOutputBuffers;
static jmethodID jGetOutputFormat;
static jmethodID jQueueInputBuffer;
static jmethodID jQueueSecureInputBuffer;
static jmethodID jRelease;
static jmethodID jReleaseOutputBuffer;
static jmethodID jSetVideoScalingMode;
static jmethodID jStart;
static jmethodID jStop;
static jfieldID jBUFFER_FLAG_CODEC_CONFIG;
static jfieldID jBUFFER_FLAG_END_OF_STREAM;
static jfieldID jBUFFER_FLAG_SYNC_FRAME;
static jfieldID jCONFIGURE_FLAG_ENCODE;
static jfieldID jCRYPTO_MODE_AES_CTR;
static jfieldID jCRYPTO_MODE_UNENCRYPTED;
static jfieldID jINFO_OUTPUT_BUFFERS_CHANGED;
static jfieldID jINFO_OUTPUT_FORMAT_CHANGED;
static jfieldID jINFO_TRY_AGAIN_LATER;
static jfieldID jVIDEO_SCALING_MODE_SCALE_TO_FIT;
static jfieldID jVIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING;
};
class MediaFormat : public AutoGlobalWrappedJavaObject {
public:
static void InitStubs(JNIEnv *jEnv);
static MediaFormat* Wrap(jobject obj);
MediaFormat(jobject obj, JNIEnv* env) : AutoGlobalWrappedJavaObject(obj, env) {};
MediaFormat();
bool ContainsKey(const nsAString& a0);
static jobject CreateAudioFormat(const nsAString& a0, int32_t a1, int32_t a2);
static jobject CreateVideoFormat(const nsAString& a0, int32_t a1, int32_t a2);
jobject GetByteBuffer(const nsAString& a0);
jfloat GetFloat(const nsAString& a0);
int32_t GetInteger(const nsAString& a0);
int64_t GetLong(const nsAString& a0);
jstring GetString(const nsAString& a0);
void SetByteBuffer(const nsAString& a0, jobject a1);
void SetFloat(const nsAString& a0, jfloat a1);
void SetInteger(const nsAString& a0, int32_t a1);
void SetLong(const nsAString& a0, int64_t a1);
void SetString(const nsAString& a0, const nsAString& a1);
jstring ToString();
static jstring getKEY_AAC_PROFILE();
static jstring getKEY_BIT_RATE();
static jstring getKEY_CHANNEL_COUNT();
static jstring getKEY_CHANNEL_MASK();
static jstring getKEY_COLOR_FORMAT();
static jstring getKEY_DURATION();
static jstring getKEY_FLAC_COMPRESSION_LEVEL();
static jstring getKEY_FRAME_RATE();
static jstring getKEY_HEIGHT();
static jstring getKEY_IS_ADTS();
static jstring getKEY_I_FRAME_INTERVAL();
static jstring getKEY_MAX_INPUT_SIZE();
static jstring getKEY_MIME();
static jstring getKEY_SAMPLE_RATE();
static jstring getKEY_WIDTH();
protected:
static jclass mMediaFormatClass;
static jmethodID jMediaFormat;
static jmethodID jContainsKey;
static jmethodID jCreateAudioFormat;
static jmethodID jCreateVideoFormat;
static jmethodID jGetByteBuffer;
static jmethodID jGetFloat;
static jmethodID jGetInteger;
static jmethodID jGetLong;
static jmethodID jGetString;
static jmethodID jSetByteBuffer;
static jmethodID jSetFloat;
static jmethodID jSetInteger;
static jmethodID jSetLong;
static jmethodID jSetString;
static jmethodID jToString;
static jfieldID jKEY_AAC_PROFILE;
static jfieldID jKEY_BIT_RATE;
static jfieldID jKEY_CHANNEL_COUNT;
static jfieldID jKEY_CHANNEL_MASK;
static jfieldID jKEY_COLOR_FORMAT;
static jfieldID jKEY_DURATION;
static jfieldID jKEY_FLAC_COMPRESSION_LEVEL;
static jfieldID jKEY_FRAME_RATE;
static jfieldID jKEY_HEIGHT;
static jfieldID jKEY_IS_ADTS;
static jfieldID jKEY_I_FRAME_INTERVAL;
static jfieldID jKEY_MAX_INPUT_SIZE;
static jfieldID jKEY_MIME;
static jfieldID jKEY_SAMPLE_RATE;
static jfieldID jKEY_WIDTH;
};
class ByteBuffer : public AutoGlobalWrappedJavaObject {
public:
static void InitStubs(JNIEnv *jEnv);
static ByteBuffer* Wrap(jobject obj);
ByteBuffer(jobject obj, JNIEnv* env) : AutoGlobalWrappedJavaObject(obj, env) {};
int8_t _get(int32_t a0);
void _put(int32_t a0, int8_t a1);
static jobject Allocate(int32_t a0);
static jobject AllocateDirect(int32_t a0);
jobject Array();
jbyteArray Array1();
int32_t ArrayOffset();
jstring AsCharBuffer();
jobject AsDoubleBuffer();
jobject AsFloatBuffer();
jobject AsIntBuffer();
jobject AsLongBuffer();
jobject AsReadOnlyBuffer();
jobject AsShortBuffer();
jobject Compact();
int32_t CompareTo(jobject a0);
int32_t CompareTo1(jobject a0);
jobject Duplicate();
bool Equals(jobject a0);
int8_t Get();
int8_t Get1(int32_t a0);
jobject Get1(jbyteArray a0);
jobject Get1(jbyteArray a0, int32_t a1, int32_t a2);
uint16_t GetChar();
uint16_t GetChar1(int32_t a0);
jdouble GetDouble();
jdouble GetDouble1(int32_t a0);
jfloat GetFloat();
jfloat GetFloat1(int32_t a0);
int32_t GetInt();
int32_t GetInt1(int32_t a0);
int64_t GetLong();
int64_t GetLong1(int32_t a0);
int16_t GetShort();
int16_t GetShort1(int32_t a0);
bool HasArray();
int32_t HashCode();
bool IsDirect();
jobject Order();
jobject Order1(jobject a0);
jobject Put(int8_t a0);
jobject Put1(int32_t a0, int8_t a1);
jobject Put1(jobject a0);
jobject Put1(jbyteArray a0);
jobject Put1(jbyteArray a0, int32_t a1, int32_t a2);
jobject PutChar(uint16_t a0);
jobject PutChar1(int32_t a0, uint16_t a1);
jobject PutDouble(jdouble a0);
jobject PutDouble1(int32_t a0, jdouble a1);
jobject PutFloat(jfloat a0);
jobject PutFloat1(int32_t a0, jfloat a1);
jobject PutInt(int32_t a0);
jobject PutInt1(int32_t a0, int32_t a1);
jobject PutLong(int32_t a0, int64_t a1);
jobject PutLong1(int64_t a0);
jobject PutShort(int32_t a0, int16_t a1);
jobject PutShort1(int16_t a0);
jobject Slice();
jstring ToString();
static jobject Wrap1(jbyteArray a0);
static jobject Wrap2(jbyteArray a0, int32_t a1, int32_t a2);
bool getBigEndian();
void setBigEndian(bool a0);
jbyteArray getHb();
bool getIsReadOnly();
void setIsReadOnly(bool a0);
bool getNativeByteOrder();
void setNativeByteOrder(bool a0);
int32_t getOffset();
ByteBuffer() : AutoGlobalWrappedJavaObject() {};
protected:
static jclass mByteBufferClass;
static jmethodID j_get;
static jmethodID j_put;
static jmethodID jAllocate;
static jmethodID jAllocateDirect;
static jmethodID jArray;
static jmethodID jArray1;
static jmethodID jArrayOffset;
static jmethodID jAsCharBuffer;
static jmethodID jAsDoubleBuffer;
static jmethodID jAsFloatBuffer;
static jmethodID jAsIntBuffer;
static jmethodID jAsLongBuffer;
static jmethodID jAsReadOnlyBuffer;
static jmethodID jAsShortBuffer;
static jmethodID jCompact;
static jmethodID jCompareTo;
static jmethodID jCompareTo1;
static jmethodID jDuplicate;
static jmethodID jEquals;
static jmethodID jGet;
static jmethodID jGet1;
static jmethodID jGet10;
static jmethodID jGet11;
static jmethodID jGetChar;
static jmethodID jGetChar1;
static jmethodID jGetDouble;
static jmethodID jGetDouble1;
static jmethodID jGetFloat;
static jmethodID jGetFloat1;
static jmethodID jGetInt;
static jmethodID jGetInt1;
static jmethodID jGetLong;
static jmethodID jGetLong1;
static jmethodID jGetShort;
static jmethodID jGetShort1;
static jmethodID jHasArray;
static jmethodID jHashCode;
static jmethodID jIsDirect;
static jmethodID jOrder;
static jmethodID jOrder1;
static jmethodID jPut;
static jmethodID jPut1;
static jmethodID jPut12;
static jmethodID jPut13;
static jmethodID jPut14;
static jmethodID jPutChar;
static jmethodID jPutChar1;
static jmethodID jPutDouble;
static jmethodID jPutDouble1;
static jmethodID jPutFloat;
static jmethodID jPutFloat1;
static jmethodID jPutInt;
static jmethodID jPutInt1;
static jmethodID jPutLong;
static jmethodID jPutLong1;
static jmethodID jPutShort;
static jmethodID jPutShort1;
static jmethodID jSlice;
static jmethodID jToString;
static jmethodID jWrap;
static jmethodID jWrap1;
static jfieldID jBigEndian;
static jfieldID jHb;
static jfieldID jIsReadOnly;
static jfieldID jNativeByteOrder;
static jfieldID jOffset;
};
class BufferInfo : public AutoGlobalWrappedJavaObject {
public:
static void InitStubs(JNIEnv *jEnv);
static BufferInfo* Wrap(jobject obj);
BufferInfo(jobject obj, JNIEnv* env) : AutoGlobalWrappedJavaObject(obj, env) {};
BufferInfo();
void Set(int32_t a0, int32_t a1, int64_t a2, int32_t a3);
int32_t getFlags();
void setFlags(int32_t a0);
int32_t getOffset();
void setOffset(int32_t a0);
int64_t getPresentationTimeUs();
void setPresentationTimeUs(int64_t a0);
int32_t getSize();
void setSize(int32_t a0);
protected:
static jclass mBufferInfoClass;
static jmethodID jBufferInfo;
static jmethodID jSet;
static jfieldID jFlags;
static jfieldID jOffset;
static jfieldID jPresentationTimeUs;
static jfieldID jSize;
};
} /* android */
} /* widget */
} /* mozilla */
#endif

View File

@ -19,7 +19,6 @@ EXPORTS += [
'AndroidJavaWrappers.h',
'AndroidJNIWrapper.h',
'GeneratedJNIWrappers.h',
'GeneratedSDKWrappers.h',
]
SOURCES += [
@ -31,7 +30,6 @@ SOURCES += [
'AndroidJNIWrapper.cpp',
'APZCCallbackHandler.cpp',
'GeneratedJNIWrappers.cpp',
'GeneratedSDKWrappers.cpp',
'GfxInfo.cpp',
'NativeJSContainer.cpp',
'nsAndroidProtocolHandler.cpp',