Skip to content

Commit

Permalink
[droidmedia] Reuse AsyncDecodingSource for encoding. JB#57149
Browse files Browse the repository at this point in the history
  • Loading branch information
d-grigorev committed Feb 8, 2022
1 parent 7451931 commit d8c3e4b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
28 changes: 17 additions & 11 deletions AsyncDecodingSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

//#define LOG_NDEBUG 0
#include "AsyncDecodingSource.h"
#include <utils/Log.h>
#include <gui/Surface.h>
Expand Down Expand Up @@ -43,34 +44,39 @@ using namespace android;
typedef ABuffer MediaCodecBuffer;
#endif

//#define LOG_NDEBUG 0
#define LOG_TAG "AsyncDecodingSource"



//static
sp<AsyncDecodingSource> AsyncDecodingSource::Create(
const sp<MediaSource> &source, uint32_t flags, const sp<ANativeWindow> &nativeWindow,
const sp<MediaSource> &source, const sp<AMessage> &srcFormat,
bool isEncoder, uint32_t flags, const sp<ANativeWindow> &nativeWindow,
const sp<ALooper> &looper, const char *desiredCodec) {
sp<Surface> surface = static_cast<Surface*>(nativeWindow.get());
const char *mime = nullptr;
sp<MetaData> meta = source->getFormat();
CHECK(meta->findCString(kKeyMIMEType, &mime));

sp<AMessage> format = new AMessage;
if (convertMetaDataToMessage(meta, &format) != OK) {
if (srcFormat.get()) {
format = srcFormat;
} else if (convertMetaDataToMessage(meta, &format) != OK) {
ALOGE("Cannot convertMetaDataToMessage()");
return nullptr;
}

if (!isEncoder) {
#if ANDROID_MAJOR > 6
format->setInt32("android._num-input-buffers", 12);
format->setInt32("android._num-input-buffers", 12);
#else
format->setInt32("inputbuffercnt", 12);
format->setInt32("inputbuffercnt", 12);
#endif
}

Vector<AString> matchingCodecs;
MediaCodecList::findMatchingCodecs(
mime, false /* encoder */, flags, &matchingCodecs);
mime, isEncoder, flags, &matchingCodecs);

for (const AString &componentName : matchingCodecs) {
if (desiredCodec != nullptr && componentName.compare(desiredCodec)) {
Expand All @@ -83,7 +89,7 @@ sp<AsyncDecodingSource> AsyncDecodingSource::Create(

if (res->mCodec != NULL) {
ALOGI("Successfully allocated codec '%s'", componentName.c_str());
if (res->configure(format, surface, 0)) {
if (res->configure(format, surface, isEncoder ? MediaCodec::CONFIGURE_FLAG_ENCODE : 0)) {
if (surface != nullptr) {
#if ANDROID_MAJOR > 7
nativeWindowConnect(nativeWindow.get(), "AsyncDecodingSource");
Expand Down Expand Up @@ -400,13 +406,13 @@ void AsyncDecodingSource::onMessageReceived(const sp<AMessage> &msg) {
mCodec->releaseOutputBuffer(index);
me->mAvailable.signal();
}
} else if (cbID == MediaCodec::CB_ERROR) {
} else if (cbID == MediaCodec::CB_ERROR) {
status_t err;
CHECK(msg->findInt32("err", &err));
ALOGE("Decoder (%s) reported error : 0x%d",
mComponentName.c_str(), err);
mState = ERROR;
} else {
ALOGE("Decoder (%s) unhandled callback id : 0x%d", mComponentName.c_str(), cbID);
}
} else {
ALOGE("Decoder (%s) unhandled callback id : 0x%d", mComponentName.c_str(), cbID);
}
}
5 changes: 3 additions & 2 deletions AsyncDecodingSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ class Surface;
class AsyncDecodingSource : public MediaSource {
public:
static sp<AsyncDecodingSource> Create(const sp<MediaSource> &source,
uint32_t flags, const sp<ANativeWindow> &nativeWindow,
const sp<ALooper> &looper, const char *desiredCodec = NULL);
const sp<AMessage> &format, bool isEncoder, uint32_t flags,
const sp<ANativeWindow> &nativeWindow, const sp<ALooper> &looper,
const char *desiredCodec = NULL);

bool configure(const sp<AMessage> format,
const sp<Surface> surface,
Expand Down
8 changes: 6 additions & 2 deletions droidmediacodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ class DroidMediaCodecBuilder {
}
//TODO: time-scale

#if ANDROID_MAJOR >= 6
#if ANDROID_MAJOR > 6
return android::AsyncDecodingSource::Create(src, format, true /* isEncoder */,
flags(), window, looper);
#elif ANDROID_MAJOR == 6
return android::MediaCodecSource::Create(looper, format, src, NULL, flags());
#else
return android::MediaCodecSource::Create(looper, format, src, flags());
Expand Down Expand Up @@ -525,7 +528,8 @@ class DroidMediaCodecBuilder {
src,
NULL, flags(), window);
#else
return android::AsyncDecodingSource::Create(src, flags(), window, looper);
return android::AsyncDecodingSource::Create(src, nullptr, false /* isEncoder */,
flags(), window, looper);
#endif
}

Expand Down

0 comments on commit d8c3e4b

Please sign in to comment.