forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 938034 - Add GonkCameraImage format. r=roc
- Loading branch information
Alfredo Yang
committed
Dec 18, 2014
1 parent
ae83997
commit 01779d0
Showing
7 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim: set ts=2 et sw=2 tw=80: */ | ||
/* 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 "GonkCameraImage.h" | ||
#include "stagefright/MediaBuffer.h" | ||
|
||
namespace mozilla { | ||
|
||
GonkCameraImage::GonkCameraImage() | ||
: GrallocImage() | ||
, mMonitor("GonkCameraImage.Monitor") | ||
, mMediaBuffer(nullptr) | ||
, mThread(nullptr) | ||
{ | ||
mFormat = ImageFormat::GONK_CAMERA_IMAGE; | ||
} | ||
|
||
GonkCameraImage::~GonkCameraImage() | ||
{ | ||
ReentrantMonitorAutoEnter mon(mMonitor); | ||
// mMediaBuffer must be cleared before destructor. | ||
MOZ_ASSERT(mMediaBuffer == nullptr); | ||
} | ||
|
||
nsresult | ||
GonkCameraImage::GetBuffer(android::MediaBuffer** aBuffer) | ||
{ | ||
ReentrantMonitorAutoEnter mon(mMonitor); | ||
|
||
if (!mMediaBuffer) { | ||
return NS_ERROR_FAILURE; | ||
} | ||
|
||
MOZ_ASSERT(NS_GetCurrentThread() == mThread); | ||
|
||
*aBuffer = mMediaBuffer; | ||
mMediaBuffer->add_ref(); | ||
|
||
return NS_OK; | ||
} | ||
|
||
bool | ||
GonkCameraImage::HasMediaBuffer() | ||
{ | ||
ReentrantMonitorAutoEnter mon(mMonitor); | ||
return mMediaBuffer != nullptr; | ||
} | ||
|
||
nsresult | ||
GonkCameraImage::SetBuffer(android::MediaBuffer* aBuffer) | ||
{ | ||
ReentrantMonitorAutoEnter mon(mMonitor); | ||
MOZ_ASSERT(!mMediaBuffer); | ||
|
||
mMediaBuffer = aBuffer; | ||
mMediaBuffer->add_ref(); | ||
mThread = NS_GetCurrentThread(); | ||
|
||
return NS_OK; | ||
} | ||
|
||
nsresult | ||
GonkCameraImage::ClearBuffer() | ||
{ | ||
ReentrantMonitorAutoEnter mon(mMonitor); | ||
|
||
if (mMediaBuffer) { | ||
MOZ_ASSERT(NS_GetCurrentThread() == mThread); | ||
mMediaBuffer->release(); | ||
mMediaBuffer = nullptr; | ||
mThread = nullptr; | ||
} | ||
return NS_OK; | ||
} | ||
|
||
} // namespace mozilla | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- | ||
* 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 GONKCAMERAIMAGE_H | ||
#define GONKCAMERAIMAGE_H | ||
|
||
#include "mozilla/ReentrantMonitor.h" | ||
#include "ImageLayers.h" | ||
#include "ImageContainer.h" | ||
#include "GrallocImages.h" | ||
|
||
namespace android { | ||
class MOZ_EXPORT MediaBuffer; | ||
} | ||
|
||
namespace mozilla { | ||
|
||
/** | ||
* GonkCameraImage has two parts. One is preview image which will be saved in | ||
* GrallocImage, another kind is the MediaBuffer keeps in mMediaBuffer | ||
* which is from gonk camera recording callback. The data in MediaBuffer is Gonk | ||
* shared memory based on android binder (IMemory), the actual format in IMemory | ||
* is platform dependent. | ||
* This instance is created in MediaEngine when the preview image arrives. | ||
* The MediaBuffer is attached to the current created GonkCameraImage via SetBuffer(). | ||
* After sending this image to MediaStreamGraph by AppendToTrack(), ClearBuffer() | ||
* must be called to clear MediaBuffer to avoid MediaBuffer be kept in MSG thread. | ||
* The reason to keep MediaBuffer be accessed from MSG thread is MediaBuffer is | ||
* limited resource and it could cause frame rate jitter if MediaBuffer stay too | ||
* long in other threads. | ||
* So there will be 3 threads to accessed this class. First is camera preview | ||
* thread which creates an instance of this class and initialize the preview | ||
* image in the base class GrallocImage. Second is the camera recording | ||
* thread which attaches MediaBuffer and sends this image to MediaStreamDirectListener. | ||
* Third is the MSG thread via NotifyPull, the image should have preview image | ||
* only in NotifyPull. | ||
* | ||
* Note: SetBuffer() and GetBuffer() should be called from the same thread. It | ||
* is forbidden to call GetBuffer() from other threads. | ||
*/ | ||
class GonkCameraImage : public layers::GrallocImage | ||
{ | ||
public: | ||
GonkCameraImage(); | ||
|
||
// The returned aBuffer has called aBuffer->add_ref() already, so it is caller's | ||
// duty to release aBuffer. It should be called from the same thread which | ||
// called SetBuffer(). | ||
nsresult GetBuffer(android::MediaBuffer** aBuffer); | ||
|
||
// Set MediaBuffer to image. It is caller's responsibility to call ClearBuffer() | ||
// after the MediaBuffer is sent via MediaStreamGraph. | ||
nsresult SetBuffer(android::MediaBuffer* aBuffer); | ||
|
||
// It should be called from the same thread which called SetBuffer(). | ||
nsresult ClearBuffer(); | ||
|
||
bool HasMediaBuffer(); | ||
|
||
protected: | ||
virtual ~GonkCameraImage(); | ||
|
||
// mMonitor protects mMediaBuffer and mThread. | ||
ReentrantMonitor mMonitor; | ||
android::MediaBuffer* mMediaBuffer; | ||
// Check if current thread is the same one which called SetBuffer(). | ||
// It doesn't need to hold reference count. | ||
DebugOnly<nsIThread*> mThread; | ||
}; | ||
|
||
} // namespace mozilla | ||
|
||
#endif /* GONKCAMERAIMAGE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters