Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6bc185ff authored by Linus Nilsson's avatar Linus Nilsson
Browse files

Transcoder: Request background priority mode from muxer and codecs.

Request MediaCodec and MediaMuxer to run their internal threads
on background priority.

Bug: 183751395
Test: build_and_run_all_unit_tests.sh and manually inspecting format
Change-Id: I7982874d7155c95f38a9bfed9ed9c3a87acf445f
parent f5df0cc1
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <android-base/logging.h>
#include <media/MediaSampleWriter.h>
#include <media/NdkCommon.h>
#include <media/NdkMediaMuxer.h>
#include <sys/prctl.h>
#include <utils/AndroidThreads.h>
@@ -126,7 +127,15 @@ MediaSampleWriter::MediaSampleConsumerFunction MediaSampleWriter::addTrack(
        LOG(ERROR) << "Muxer needs to be initialized when adding tracks.";
        return nullptr;
    }
    ssize_t trackIndexOrError = mMuxer->addTrack(trackFormat.get());

    AMediaFormat* trackFormatCopy = AMediaFormat_new();
    AMediaFormat_copy(trackFormatCopy, trackFormat.get());
    // Request muxer to use background priorities by default.
    AMediaFormatUtils::SetDefaultFormatValueInt32(TBD_AMEDIACODEC_PARAMETER_KEY_BACKGROUND_MODE,
                                                  trackFormatCopy, 1 /* true */);

    ssize_t trackIndexOrError = mMuxer->addTrack(trackFormatCopy);
    AMediaFormat_delete(trackFormatCopy);
    if (trackIndexOrError < 0) {
        LOG(ERROR) << "Failed to add media track to muxer: " << trackIndexOrError;
        return nullptr;
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ const char* TBD_AMEDIACODEC_PARAMETER_KEY_MAX_B_FRAMES = "max-bframes";

/* TODO(lnilsson): Finalize value or adopt AMediaFormat key once available. */
const char* TBD_AMEDIACODEC_PARAMETER_KEY_COLOR_TRANSFER_REQUEST = "color-transfer-request";
const char* TBD_AMEDIACODEC_PARAMETER_KEY_BACKGROUND_MODE = "android._background-mode";

namespace AMediaFormatUtils {

+5 −0
Original line number Diff line number Diff line
@@ -254,6 +254,10 @@ media_status_t VideoTrackTranscoder::configureDestinationFormat(
    // MediaSampleWriter track format, and MediaSampleWriter will call AMediaMuxer_setOrientationHint.
    AMediaFormat_setInt32(encoderFormat, AMEDIAFORMAT_KEY_ROTATION, 0);

    // Request encoder to use background priorities by default.
    SetDefaultFormatValueInt32(TBD_AMEDIACODEC_PARAMETER_KEY_BACKGROUND_MODE, encoderFormat,
                               1 /* true */);

    mDestinationFormat = std::shared_ptr<AMediaFormat>(encoderFormat, &AMediaFormat_delete);

    // Create and configure the encoder.
@@ -335,6 +339,7 @@ media_status_t VideoTrackTranscoder::configureDestinationFormat(
    static const std::vector<EntryCopier> kEncoderEntriesToCopy{
            ENTRY_COPIER2(AMEDIAFORMAT_KEY_OPERATING_RATE, Float, Int32),
            ENTRY_COPIER(AMEDIAFORMAT_KEY_PRIORITY, Int32),
            ENTRY_COPIER(TBD_AMEDIACODEC_PARAMETER_KEY_BACKGROUND_MODE, Int32),
    };
    CopyFormatEntries(mDestinationFormat.get(), decoderFormat.get(), kEncoderEntriesToCopy);

+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ extern const char* TBD_AMEDIACODEC_PARAMETER_KEY_REQUEST_SYNC_FRAME;
extern const char* TBD_AMEDIACODEC_PARAMETER_KEY_VIDEO_BITRATE;
extern const char* TBD_AMEDIACODEC_PARAMETER_KEY_MAX_B_FRAMES;
extern const char* TBD_AMEDIACODEC_PARAMETER_KEY_COLOR_TRANSFER_REQUEST;
extern const char* TBD_AMEDIACODEC_PARAMETER_KEY_BACKGROUND_MODE;
static constexpr int TBD_AMEDIACODEC_BUFFER_FLAG_KEY_FRAME = 0x1;

static constexpr int kBitrateModeConstant = 2;
+3 −2
Original line number Diff line number Diff line
@@ -117,8 +117,9 @@ bool operator==(const AMediaCodecBufferInfo& lhs, const AMediaCodecBufferInfo& r
}

bool operator==(const TestMuxer::Event& lhs, const TestMuxer::Event& rhs) {
    return lhs.type == rhs.type && lhs.format == rhs.format && lhs.trackIndex == rhs.trackIndex &&
           lhs.data == rhs.data && lhs.info == rhs.info;
    // Don't test format pointer equality since the writer can make a copy.
    return lhs.type == rhs.type /*&& lhs.format == rhs.format*/ &&
           lhs.trackIndex == rhs.trackIndex && lhs.data == rhs.data && lhs.info == rhs.info;
}

/** Represents a media source file. */