2013-05-16 17:24:13 -07:00
|
|
|
#include <stagefright/foundation/ABase.h>
|
|
|
|
#include <stagefright/foundation/AHandlerReflector.h>
|
|
|
|
#include <stagefright/foundation/ALooper.h>
|
|
|
|
#include <stagefright/MediaSource.h>
|
2012-09-26 21:33:43 -07:00
|
|
|
#include <stagefright/DataSource.h>
|
2013-03-08 11:43:32 -08:00
|
|
|
#include <stagefright/MediaSource.h>
|
2012-09-26 21:33:43 -07:00
|
|
|
#include <utils/RefBase.h>
|
|
|
|
|
|
|
|
#include "GonkNativeWindow.h"
|
2013-03-08 11:43:33 -08:00
|
|
|
#include "GonkNativeWindowClient.h"
|
2013-07-24 19:13:35 -07:00
|
|
|
#include "GrallocImages.h"
|
2012-09-26 21:33:43 -07:00
|
|
|
#include "MPAPI.h"
|
|
|
|
#include "MediaResource.h"
|
2012-11-19 07:11:21 -08:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2013-06-10 05:22:05 -07:00
|
|
|
#include "OMXCodecProxy.h"
|
2012-09-26 21:33:43 -07:00
|
|
|
|
2013-02-15 18:27:18 -08:00
|
|
|
namespace android {
|
|
|
|
class OmxDecoder;
|
|
|
|
};
|
|
|
|
|
2012-09-26 21:33:43 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
class VideoGraphicBuffer : public GraphicBufferLocked {
|
|
|
|
// XXX change this to an actual smart pointer at some point
|
|
|
|
android::MediaBuffer *mMediaBuffer;
|
2013-02-15 18:27:18 -08:00
|
|
|
android::wp<android::OmxDecoder> mOmxDecoder;
|
2012-09-26 21:33:43 -07:00
|
|
|
public:
|
2013-02-15 18:27:18 -08:00
|
|
|
VideoGraphicBuffer(const android::wp<android::OmxDecoder> aOmxDecoder,
|
|
|
|
android::MediaBuffer *aBuffer,
|
2013-05-27 07:12:13 -07:00
|
|
|
SurfaceDescriptor& aDescriptor);
|
2012-09-26 21:33:43 -07:00
|
|
|
~VideoGraphicBuffer();
|
|
|
|
void Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// MediaStreamSource is a DataSource that reads from a MPAPI media stream.
|
|
|
|
class MediaStreamSource : public DataSource {
|
|
|
|
typedef mozilla::MediaResource MediaResource;
|
2012-11-19 07:11:21 -08:00
|
|
|
typedef mozilla::AbstractMediaDecoder AbstractMediaDecoder;
|
2012-09-26 21:33:43 -07:00
|
|
|
|
2013-06-03 07:41:54 -07:00
|
|
|
Mutex mLock;
|
2013-05-02 15:59:18 -07:00
|
|
|
nsRefPtr<MediaResource> mResource;
|
2012-11-19 07:11:21 -08:00
|
|
|
AbstractMediaDecoder *mDecoder;
|
2012-09-26 21:33:43 -07:00
|
|
|
public:
|
2013-05-02 15:59:18 -07:00
|
|
|
MediaStreamSource(MediaResource* aResource,
|
2012-11-19 07:11:21 -08:00
|
|
|
AbstractMediaDecoder *aDecoder);
|
2012-09-26 21:33:43 -07:00
|
|
|
|
|
|
|
virtual status_t initCheck() const;
|
|
|
|
virtual ssize_t readAt(off64_t offset, void *data, size_t size);
|
|
|
|
virtual ssize_t readAt(off_t offset, void *data, size_t size) {
|
|
|
|
return readAt(static_cast<off64_t>(offset), data, size);
|
|
|
|
}
|
|
|
|
virtual status_t getSize(off_t *size) {
|
|
|
|
off64_t size64;
|
|
|
|
status_t status = getSize(&size64);
|
|
|
|
*size = size64;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
virtual status_t getSize(off64_t *size);
|
|
|
|
virtual uint32_t flags() {
|
|
|
|
return kWantsPrefetching;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MediaStreamSource();
|
|
|
|
|
|
|
|
private:
|
|
|
|
MediaStreamSource(const MediaStreamSource &);
|
|
|
|
MediaStreamSource &operator=(const MediaStreamSource &);
|
|
|
|
};
|
|
|
|
|
2013-06-10 05:22:05 -07:00
|
|
|
class OmxDecoder : public OMXCodecProxy::EventListener {
|
2012-09-26 21:33:43 -07:00
|
|
|
typedef MPAPI::AudioFrame AudioFrame;
|
|
|
|
typedef MPAPI::VideoFrame VideoFrame;
|
|
|
|
typedef mozilla::MediaResource MediaResource;
|
2012-11-19 07:11:21 -08:00
|
|
|
typedef mozilla::AbstractMediaDecoder AbstractMediaDecoder;
|
2012-09-26 21:33:43 -07:00
|
|
|
|
|
|
|
enum {
|
2012-12-10 23:48:58 -08:00
|
|
|
kPreferSoftwareCodecs = 1,
|
|
|
|
kSoftwareCodecsOnly = 8,
|
|
|
|
kHardwareCodecsOnly = 16,
|
2012-09-26 21:33:43 -07:00
|
|
|
};
|
|
|
|
|
2013-05-16 17:24:13 -07:00
|
|
|
enum {
|
2013-06-26 06:36:22 -07:00
|
|
|
kNotifyPostReleaseVideoBuffer = 'noti',
|
|
|
|
kNotifyStatusChanged = 'stat'
|
2013-05-16 17:24:13 -07:00
|
|
|
};
|
|
|
|
|
2012-11-19 07:11:21 -08:00
|
|
|
AbstractMediaDecoder *mDecoder;
|
2013-05-02 15:59:18 -07:00
|
|
|
nsRefPtr<MediaResource> mResource;
|
2012-09-26 21:33:43 -07:00
|
|
|
sp<GonkNativeWindow> mNativeWindow;
|
2013-03-08 11:43:33 -08:00
|
|
|
sp<GonkNativeWindowClient> mNativeWindowClient;
|
2012-09-26 21:33:43 -07:00
|
|
|
sp<MediaSource> mVideoTrack;
|
2013-06-10 05:22:05 -07:00
|
|
|
sp<OMXCodecProxy> mVideoSource;
|
2012-09-26 21:33:43 -07:00
|
|
|
sp<MediaSource> mAudioTrack;
|
|
|
|
sp<MediaSource> mAudioSource;
|
|
|
|
int32_t mVideoWidth;
|
|
|
|
int32_t mVideoHeight;
|
|
|
|
int32_t mVideoColorFormat;
|
|
|
|
int32_t mVideoStride;
|
|
|
|
int32_t mVideoSliceHeight;
|
|
|
|
int32_t mVideoRotation;
|
|
|
|
int32_t mAudioChannels;
|
|
|
|
int32_t mAudioSampleRate;
|
|
|
|
int64_t mDurationUs;
|
|
|
|
VideoFrame mVideoFrame;
|
|
|
|
AudioFrame mAudioFrame;
|
|
|
|
|
|
|
|
// Lifetime of these should be handled by OMXCodec, as long as we release
|
|
|
|
// them after use: see ReleaseVideoBuffer(), ReleaseAudioBuffer()
|
|
|
|
MediaBuffer *mVideoBuffer;
|
|
|
|
MediaBuffer *mAudioBuffer;
|
|
|
|
|
2013-02-15 18:27:18 -08:00
|
|
|
// Hold video's MediaBuffers that are released during video seeking.
|
|
|
|
// The holded MediaBuffers are released soon after seek completion.
|
|
|
|
// OMXCodec does not accept MediaBuffer during seeking. If MediaBuffer is
|
|
|
|
// returned to OMXCodec during seeking, OMXCodec calls assert.
|
|
|
|
Vector<MediaBuffer *> mPendingVideoBuffers;
|
2013-05-16 17:24:13 -07:00
|
|
|
// The lock protects mPendingVideoBuffers.
|
|
|
|
Mutex mPendingVideoBuffersLock;
|
|
|
|
|
2013-02-15 18:27:18 -08:00
|
|
|
// Show if OMXCodec is seeking.
|
|
|
|
bool mIsVideoSeeking;
|
|
|
|
// The lock protects video MediaBuffer release()'s pending operations called
|
|
|
|
// from multiple threads. The pending operations happen only during video
|
|
|
|
// seeking. Holding mSeekLock long time could affect to video rendering.
|
|
|
|
// Holding time should be minimum.
|
|
|
|
Mutex mSeekLock;
|
|
|
|
|
2013-05-16 17:24:13 -07:00
|
|
|
// ALooper is a message loop used in stagefright.
|
|
|
|
// It creates a thread for messages and handles messages in the thread.
|
|
|
|
// ALooper is a clone of Looper in android Java.
|
|
|
|
// http://developer.android.com/reference/android/os/Looper.html
|
|
|
|
sp<ALooper> mLooper;
|
|
|
|
// deliver a message to a wrapped object(OmxDecoder).
|
|
|
|
// AHandlerReflector is similar to Handler in android Java.
|
|
|
|
// http://developer.android.com/reference/android/os/Handler.html
|
|
|
|
sp<AHandlerReflector<OmxDecoder> > mReflector;
|
|
|
|
|
2012-09-26 21:33:43 -07:00
|
|
|
// 'true' if a read from the audio stream was done while reading the metadata
|
|
|
|
bool mAudioMetadataRead;
|
|
|
|
|
|
|
|
void ReleaseVideoBuffer();
|
|
|
|
void ReleaseAudioBuffer();
|
2013-02-15 18:27:18 -08:00
|
|
|
// Call with mSeekLock held.
|
|
|
|
void ReleaseAllPendingVideoBuffersLocked();
|
2012-09-26 21:33:43 -07:00
|
|
|
|
|
|
|
void PlanarYUV420Frame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame);
|
|
|
|
void CbYCrYFrame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame);
|
|
|
|
void SemiPlanarYUV420Frame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame);
|
|
|
|
void SemiPlanarYVU420Frame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame);
|
|
|
|
bool ToVideoFrame(VideoFrame *aFrame, int64_t aTimeUs, void *aData, size_t aSize, bool aKeyFrame);
|
|
|
|
bool ToAudioFrame(AudioFrame *aFrame, int64_t aTimeUs, void *aData, size_t aDataOffset, size_t aSize,
|
|
|
|
int32_t aAudioChannels, int32_t aAudioSampleRate);
|
2013-04-10 17:58:25 -07:00
|
|
|
|
|
|
|
//True if decoder is in a paused state
|
2013-06-30 21:02:03 -07:00
|
|
|
bool mAudioPaused;
|
|
|
|
bool mVideoPaused;
|
2013-04-10 17:58:25 -07:00
|
|
|
|
2012-09-26 21:33:43 -07:00
|
|
|
public:
|
2012-11-19 07:11:21 -08:00
|
|
|
OmxDecoder(MediaResource *aResource, AbstractMediaDecoder *aDecoder);
|
2012-09-26 21:33:43 -07:00
|
|
|
~OmxDecoder();
|
|
|
|
|
2013-06-10 05:22:05 -07:00
|
|
|
// MediaResourceManagerClient::EventListener
|
|
|
|
virtual void statusChanged();
|
|
|
|
|
2012-09-26 21:33:43 -07:00
|
|
|
bool Init();
|
2013-06-10 05:22:05 -07:00
|
|
|
bool TryLoad();
|
|
|
|
bool IsDormantNeeded();
|
|
|
|
bool IsWaitingMediaResources();
|
|
|
|
bool AllocateMediaResources();
|
|
|
|
void ReleaseMediaResources();
|
2012-09-26 21:33:43 -07:00
|
|
|
bool SetVideoFormat();
|
|
|
|
bool SetAudioFormat();
|
|
|
|
|
|
|
|
void GetDuration(int64_t *durationUs) {
|
|
|
|
*durationUs = mDurationUs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetVideoParameters(int32_t *width, int32_t *height) {
|
|
|
|
*width = mVideoWidth;
|
|
|
|
*height = mVideoHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAudioParameters(int32_t *numChannels, int32_t *sampleRate) {
|
|
|
|
*numChannels = mAudioChannels;
|
|
|
|
*sampleRate = mAudioSampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasVideo() {
|
|
|
|
return mVideoSource != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasAudio() {
|
|
|
|
return mAudioSource != nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-08 10:00:26 -07:00
|
|
|
bool ReadVideo(VideoFrame *aFrame, int64_t aSeekTimeUs,
|
2012-09-26 21:33:43 -07:00
|
|
|
bool aKeyframeSkip = false,
|
|
|
|
bool aDoSeek = false);
|
|
|
|
bool ReadAudio(AudioFrame *aFrame, int64_t aSeekTimeUs);
|
|
|
|
|
|
|
|
MediaResource *GetResource() {
|
|
|
|
return mResource;
|
|
|
|
}
|
2013-02-15 18:27:18 -08:00
|
|
|
|
2013-04-10 17:58:25 -07:00
|
|
|
//Change decoder into a playing state
|
|
|
|
nsresult Play();
|
|
|
|
|
|
|
|
//Change decoder into a paused state
|
|
|
|
void Pause();
|
2013-05-16 17:24:13 -07:00
|
|
|
|
|
|
|
// Post kNotifyPostReleaseVideoBuffer message to OmxDecoder via ALooper.
|
|
|
|
void PostReleaseVideoBuffer(MediaBuffer *aBuffer);
|
|
|
|
// Receive a message from AHandlerReflector.
|
|
|
|
// Called on ALooper thread.
|
|
|
|
void onMessageReceived(const sp<AMessage> &msg);
|
|
|
|
|
2012-09-26 21:33:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|