Loading include/media/stagefright/SurfaceMediaSource.h +6 −12 Original line number Original line Diff line number Diff line Loading @@ -79,10 +79,6 @@ public: MediaBuffer **buffer, const ReadOptions *options = NULL); MediaBuffer **buffer, const ReadOptions *options = NULL); virtual sp<MetaData> getFormat(); virtual sp<MetaData> getFormat(); // Pass the metadata over to the buffer, call when you have the lock void passMetadataBufferLocked(MediaBuffer **buffer); bool checkBufferMatchesSlot(int slot, MediaBuffer *buffer); // Get / Set the frame rate used for encoding. Default fps = 30 // Get / Set the frame rate used for encoding. Default fps = 30 status_t setFrameRate(int32_t fps) ; status_t setFrameRate(int32_t fps) ; int32_t getFrameRate( ) const; int32_t getFrameRate( ) const; Loading @@ -105,9 +101,6 @@ public: // when a new frame becomes available. // when a new frame becomes available. void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); // getCurrentBuffer returns the buffer associated with the current image. sp<GraphicBuffer> getCurrentBuffer() const; // dump our state in a String // dump our state in a String void dump(String8& result) const; void dump(String8& result) const; void dump(String8& result, const char* prefix, char* buffer, void dump(String8& result, const char* prefix, char* buffer, Loading Loading @@ -165,11 +158,12 @@ private: // reset mCurrentTexture to INVALID_BUFFER_SLOT. // reset mCurrentTexture to INVALID_BUFFER_SLOT. int mCurrentSlot; int mCurrentSlot; // mCurrentBuf is the graphic buffer of the current slot to be used by // mCurrentBuffers is a list of the graphic buffers that are being used by // buffer consumer. It's possible that this buffer is not associated // buffer consumer (i.e. the video encoder). It's possible that these // with any buffer slot, so we must track it separately in order to // buffers are not associated with any buffer slots, so we must track them // properly use IGraphicBufferAlloc::freeAllGraphicBuffersExcept. // separately. Buffers are added to this list in read, and removed from sp<GraphicBuffer> mCurrentBuf; // this list in signalBufferReturned Vector<sp<GraphicBuffer> > mCurrentBuffers; // mCurrentTimestamp is the timestamp for the current texture. It // mCurrentTimestamp is the timestamp for the current texture. It // gets set to mLastQueuedTimestamp each time updateTexImage is called. // gets set to mLastQueuedTimestamp each time updateTexImage is called. Loading media/libstagefright/SurfaceMediaSource.cpp +52 −48 Original line number Original line Diff line number Diff line Loading @@ -96,11 +96,6 @@ void SurfaceMediaSource::setFrameAvailableListener( mFrameAvailableListener = listener; mFrameAvailableListener = listener; } } sp<GraphicBuffer> SurfaceMediaSource::getCurrentBuffer() const { Mutex::Autolock lock(mMutex); return mCurrentBuf; } void SurfaceMediaSource::dump(String8& result) const void SurfaceMediaSource::dump(String8& result) const { { char buffer[1024]; char buffer[1024]; Loading Loading @@ -185,6 +180,35 @@ sp<MetaData> SurfaceMediaSource::getFormat() return meta; return meta; } } // Pass the data to the MediaBuffer. Pass in only the metadata // The metadata passed consists of two parts: // 1. First, there is an integer indicating that it is a GRAlloc // source (kMetadataBufferTypeGrallocSource) // 2. This is followed by the buffer_handle_t that is a handle to the // GRalloc buffer. The encoder needs to interpret this GRalloc handle // and encode the frames. // -------------------------------------------------------------- // | kMetadataBufferTypeGrallocSource | sizeof(buffer_handle_t) | // -------------------------------------------------------------- // Note: Call only when you have the lock static void passMetadataBuffer(MediaBuffer **buffer, buffer_handle_t bufferHandle) { // MediaBuffer allocates and owns this data MediaBuffer *tempBuffer = new MediaBuffer(4 + sizeof(buffer_handle_t)); char *data = (char *)tempBuffer->data(); if (data == NULL) { ALOGE("Cannot allocate memory for metadata buffer!"); return; } OMX_U32 type = kMetadataBufferTypeGrallocSource; memcpy(data, &type, 4); memcpy(data + 4, &bufferHandle, sizeof(buffer_handle_t)); *buffer = tempBuffer; ALOGV("handle = %p, , offset = %d, length = %d", bufferHandle, (*buffer)->range_length(), (*buffer)->range_offset()); } status_t SurfaceMediaSource::read( MediaBuffer **buffer, status_t SurfaceMediaSource::read( MediaBuffer **buffer, const ReadOptions *options) const ReadOptions *options) { { Loading Loading @@ -251,13 +275,14 @@ status_t SurfaceMediaSource::read( MediaBuffer **buffer, if (item.mGraphicBuffer != NULL) { if (item.mGraphicBuffer != NULL) { mBufferSlot[mCurrentSlot] = item.mGraphicBuffer; mBufferSlot[mCurrentSlot] = item.mGraphicBuffer; } } mCurrentBuf = mBufferSlot[mCurrentSlot]; mCurrentBuffers.push_back(mBufferSlot[mCurrentSlot]); int64_t prevTimeStamp = mCurrentTimestamp; int64_t prevTimeStamp = mCurrentTimestamp; mCurrentTimestamp = item.mTimestamp; mCurrentTimestamp = item.mTimestamp; mNumFramesEncoded++; mNumFramesEncoded++; // Pass the data to the MediaBuffer. Pass in only the metadata // Pass the data to the MediaBuffer. Pass in only the metadata passMetadataBufferLocked(buffer); passMetadataBuffer(buffer, mBufferSlot[mCurrentSlot]->handle); (*buffer)->setObserver(this); (*buffer)->setObserver(this); (*buffer)->add_ref(); (*buffer)->add_ref(); Loading @@ -270,34 +295,12 @@ status_t SurfaceMediaSource::read( MediaBuffer **buffer, return OK; return OK; } } // Pass the data to the MediaBuffer. Pass in only the metadata static buffer_handle_t getMediaBufferHandle(MediaBuffer *buffer) { // The metadata passed consists of two parts: // need to convert to char* for pointer arithmetic and then // 1. First, there is an integer indicating that it is a GRAlloc // copy the byte stream into our handle // source (kMetadataBufferTypeGrallocSource) buffer_handle_t bufferHandle; // 2. This is followed by the buffer_handle_t that is a handle to the memcpy(&bufferHandle, (char*)(buffer->data()) + 4, sizeof(buffer_handle_t)); // GRalloc buffer. The encoder needs to interpret this GRalloc handle return bufferHandle; // and encode the frames. // -------------------------------------------------------------- // | kMetadataBufferTypeGrallocSource | sizeof(buffer_handle_t) | // -------------------------------------------------------------- // Note: Call only when you have the lock void SurfaceMediaSource::passMetadataBufferLocked(MediaBuffer **buffer) { ALOGV("passMetadataBuffer"); // MediaBuffer allocates and owns this data MediaBuffer *tempBuffer = new MediaBuffer(4 + sizeof(buffer_handle_t)); char *data = (char *)tempBuffer->data(); if (data == NULL) { ALOGE("Cannot allocate memory for metadata buffer!"); return; } OMX_U32 type = kMetadataBufferTypeGrallocSource; memcpy(data, &type, 4); memcpy(data + 4, &(mCurrentBuf->handle), sizeof(buffer_handle_t)); *buffer = tempBuffer; ALOGV("handle = %p, , offset = %d, length = %d", mCurrentBuf->handle, (*buffer)->range_length(), (*buffer)->range_offset()); } } void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { Loading @@ -307,16 +310,26 @@ void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { Mutex::Autolock lock(mMutex); Mutex::Autolock lock(mMutex); if (mStopped) { buffer_handle_t bufferHandle = getMediaBufferHandle(buffer); ALOGV("signalBufferReturned: mStopped = true! Nothing to do!"); return; for (size_t i = 0; i < mCurrentBuffers.size(); i++) { if (mCurrentBuffers[i]->handle == bufferHandle) { mCurrentBuffers.removeAt(i); foundBuffer = true; break; } } if (!foundBuffer) { ALOGW("returned buffer was not found in the current buffer list"); } } for (int id = 0; id < BufferQueue::NUM_BUFFER_SLOTS; id++) { for (int id = 0; id < BufferQueue::NUM_BUFFER_SLOTS; id++) { if (mBufferSlot[id] == NULL) { if (mBufferSlot[id] == NULL) { continue; continue; } } if (checkBufferMatchesSlot(id, buffer)) { if (bufferHandle == mBufferSlot[id]->handle) { ALOGV("Slot %d returned, matches handle = %p", id, ALOGV("Slot %d returned, matches handle = %p", id, mBufferSlot[id]->handle); mBufferSlot[id]->handle); Loading @@ -335,15 +348,6 @@ void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { } } } } bool SurfaceMediaSource::checkBufferMatchesSlot(int slot, MediaBuffer *buffer) { ALOGV("Check if Buffer matches slot"); // need to convert to char* for pointer arithmetic and then // copy the byte stream into our handle buffer_handle_t bufferHandle ; memcpy( &bufferHandle, (char *)(buffer->data()) + 4, sizeof(buffer_handle_t)); return mBufferSlot[slot]->handle == bufferHandle; } // Part of the BufferQueue::ConsumerListener // Part of the BufferQueue::ConsumerListener void SurfaceMediaSource::onFrameAvailable() { void SurfaceMediaSource::onFrameAvailable() { ALOGV("onFrameAvailable"); ALOGV("onFrameAvailable"); Loading Loading
include/media/stagefright/SurfaceMediaSource.h +6 −12 Original line number Original line Diff line number Diff line Loading @@ -79,10 +79,6 @@ public: MediaBuffer **buffer, const ReadOptions *options = NULL); MediaBuffer **buffer, const ReadOptions *options = NULL); virtual sp<MetaData> getFormat(); virtual sp<MetaData> getFormat(); // Pass the metadata over to the buffer, call when you have the lock void passMetadataBufferLocked(MediaBuffer **buffer); bool checkBufferMatchesSlot(int slot, MediaBuffer *buffer); // Get / Set the frame rate used for encoding. Default fps = 30 // Get / Set the frame rate used for encoding. Default fps = 30 status_t setFrameRate(int32_t fps) ; status_t setFrameRate(int32_t fps) ; int32_t getFrameRate( ) const; int32_t getFrameRate( ) const; Loading @@ -105,9 +101,6 @@ public: // when a new frame becomes available. // when a new frame becomes available. void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); // getCurrentBuffer returns the buffer associated with the current image. sp<GraphicBuffer> getCurrentBuffer() const; // dump our state in a String // dump our state in a String void dump(String8& result) const; void dump(String8& result) const; void dump(String8& result, const char* prefix, char* buffer, void dump(String8& result, const char* prefix, char* buffer, Loading Loading @@ -165,11 +158,12 @@ private: // reset mCurrentTexture to INVALID_BUFFER_SLOT. // reset mCurrentTexture to INVALID_BUFFER_SLOT. int mCurrentSlot; int mCurrentSlot; // mCurrentBuf is the graphic buffer of the current slot to be used by // mCurrentBuffers is a list of the graphic buffers that are being used by // buffer consumer. It's possible that this buffer is not associated // buffer consumer (i.e. the video encoder). It's possible that these // with any buffer slot, so we must track it separately in order to // buffers are not associated with any buffer slots, so we must track them // properly use IGraphicBufferAlloc::freeAllGraphicBuffersExcept. // separately. Buffers are added to this list in read, and removed from sp<GraphicBuffer> mCurrentBuf; // this list in signalBufferReturned Vector<sp<GraphicBuffer> > mCurrentBuffers; // mCurrentTimestamp is the timestamp for the current texture. It // mCurrentTimestamp is the timestamp for the current texture. It // gets set to mLastQueuedTimestamp each time updateTexImage is called. // gets set to mLastQueuedTimestamp each time updateTexImage is called. Loading
media/libstagefright/SurfaceMediaSource.cpp +52 −48 Original line number Original line Diff line number Diff line Loading @@ -96,11 +96,6 @@ void SurfaceMediaSource::setFrameAvailableListener( mFrameAvailableListener = listener; mFrameAvailableListener = listener; } } sp<GraphicBuffer> SurfaceMediaSource::getCurrentBuffer() const { Mutex::Autolock lock(mMutex); return mCurrentBuf; } void SurfaceMediaSource::dump(String8& result) const void SurfaceMediaSource::dump(String8& result) const { { char buffer[1024]; char buffer[1024]; Loading Loading @@ -185,6 +180,35 @@ sp<MetaData> SurfaceMediaSource::getFormat() return meta; return meta; } } // Pass the data to the MediaBuffer. Pass in only the metadata // The metadata passed consists of two parts: // 1. First, there is an integer indicating that it is a GRAlloc // source (kMetadataBufferTypeGrallocSource) // 2. This is followed by the buffer_handle_t that is a handle to the // GRalloc buffer. The encoder needs to interpret this GRalloc handle // and encode the frames. // -------------------------------------------------------------- // | kMetadataBufferTypeGrallocSource | sizeof(buffer_handle_t) | // -------------------------------------------------------------- // Note: Call only when you have the lock static void passMetadataBuffer(MediaBuffer **buffer, buffer_handle_t bufferHandle) { // MediaBuffer allocates and owns this data MediaBuffer *tempBuffer = new MediaBuffer(4 + sizeof(buffer_handle_t)); char *data = (char *)tempBuffer->data(); if (data == NULL) { ALOGE("Cannot allocate memory for metadata buffer!"); return; } OMX_U32 type = kMetadataBufferTypeGrallocSource; memcpy(data, &type, 4); memcpy(data + 4, &bufferHandle, sizeof(buffer_handle_t)); *buffer = tempBuffer; ALOGV("handle = %p, , offset = %d, length = %d", bufferHandle, (*buffer)->range_length(), (*buffer)->range_offset()); } status_t SurfaceMediaSource::read( MediaBuffer **buffer, status_t SurfaceMediaSource::read( MediaBuffer **buffer, const ReadOptions *options) const ReadOptions *options) { { Loading Loading @@ -251,13 +275,14 @@ status_t SurfaceMediaSource::read( MediaBuffer **buffer, if (item.mGraphicBuffer != NULL) { if (item.mGraphicBuffer != NULL) { mBufferSlot[mCurrentSlot] = item.mGraphicBuffer; mBufferSlot[mCurrentSlot] = item.mGraphicBuffer; } } mCurrentBuf = mBufferSlot[mCurrentSlot]; mCurrentBuffers.push_back(mBufferSlot[mCurrentSlot]); int64_t prevTimeStamp = mCurrentTimestamp; int64_t prevTimeStamp = mCurrentTimestamp; mCurrentTimestamp = item.mTimestamp; mCurrentTimestamp = item.mTimestamp; mNumFramesEncoded++; mNumFramesEncoded++; // Pass the data to the MediaBuffer. Pass in only the metadata // Pass the data to the MediaBuffer. Pass in only the metadata passMetadataBufferLocked(buffer); passMetadataBuffer(buffer, mBufferSlot[mCurrentSlot]->handle); (*buffer)->setObserver(this); (*buffer)->setObserver(this); (*buffer)->add_ref(); (*buffer)->add_ref(); Loading @@ -270,34 +295,12 @@ status_t SurfaceMediaSource::read( MediaBuffer **buffer, return OK; return OK; } } // Pass the data to the MediaBuffer. Pass in only the metadata static buffer_handle_t getMediaBufferHandle(MediaBuffer *buffer) { // The metadata passed consists of two parts: // need to convert to char* for pointer arithmetic and then // 1. First, there is an integer indicating that it is a GRAlloc // copy the byte stream into our handle // source (kMetadataBufferTypeGrallocSource) buffer_handle_t bufferHandle; // 2. This is followed by the buffer_handle_t that is a handle to the memcpy(&bufferHandle, (char*)(buffer->data()) + 4, sizeof(buffer_handle_t)); // GRalloc buffer. The encoder needs to interpret this GRalloc handle return bufferHandle; // and encode the frames. // -------------------------------------------------------------- // | kMetadataBufferTypeGrallocSource | sizeof(buffer_handle_t) | // -------------------------------------------------------------- // Note: Call only when you have the lock void SurfaceMediaSource::passMetadataBufferLocked(MediaBuffer **buffer) { ALOGV("passMetadataBuffer"); // MediaBuffer allocates and owns this data MediaBuffer *tempBuffer = new MediaBuffer(4 + sizeof(buffer_handle_t)); char *data = (char *)tempBuffer->data(); if (data == NULL) { ALOGE("Cannot allocate memory for metadata buffer!"); return; } OMX_U32 type = kMetadataBufferTypeGrallocSource; memcpy(data, &type, 4); memcpy(data + 4, &(mCurrentBuf->handle), sizeof(buffer_handle_t)); *buffer = tempBuffer; ALOGV("handle = %p, , offset = %d, length = %d", mCurrentBuf->handle, (*buffer)->range_length(), (*buffer)->range_offset()); } } void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { Loading @@ -307,16 +310,26 @@ void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { Mutex::Autolock lock(mMutex); Mutex::Autolock lock(mMutex); if (mStopped) { buffer_handle_t bufferHandle = getMediaBufferHandle(buffer); ALOGV("signalBufferReturned: mStopped = true! Nothing to do!"); return; for (size_t i = 0; i < mCurrentBuffers.size(); i++) { if (mCurrentBuffers[i]->handle == bufferHandle) { mCurrentBuffers.removeAt(i); foundBuffer = true; break; } } if (!foundBuffer) { ALOGW("returned buffer was not found in the current buffer list"); } } for (int id = 0; id < BufferQueue::NUM_BUFFER_SLOTS; id++) { for (int id = 0; id < BufferQueue::NUM_BUFFER_SLOTS; id++) { if (mBufferSlot[id] == NULL) { if (mBufferSlot[id] == NULL) { continue; continue; } } if (checkBufferMatchesSlot(id, buffer)) { if (bufferHandle == mBufferSlot[id]->handle) { ALOGV("Slot %d returned, matches handle = %p", id, ALOGV("Slot %d returned, matches handle = %p", id, mBufferSlot[id]->handle); mBufferSlot[id]->handle); Loading @@ -335,15 +348,6 @@ void SurfaceMediaSource::signalBufferReturned(MediaBuffer *buffer) { } } } } bool SurfaceMediaSource::checkBufferMatchesSlot(int slot, MediaBuffer *buffer) { ALOGV("Check if Buffer matches slot"); // need to convert to char* for pointer arithmetic and then // copy the byte stream into our handle buffer_handle_t bufferHandle ; memcpy( &bufferHandle, (char *)(buffer->data()) + 4, sizeof(buffer_handle_t)); return mBufferSlot[slot]->handle == bufferHandle; } // Part of the BufferQueue::ConsumerListener // Part of the BufferQueue::ConsumerListener void SurfaceMediaSource::onFrameAvailable() { void SurfaceMediaSource::onFrameAvailable() { ALOGV("onFrameAvailable"); ALOGV("onFrameAvailable"); Loading