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

Commit e3b5ba0d authored by Zim's avatar Zim Committed by Manish Singh
Browse files

Support transcode via MTP

If the persist.sys.fuse.transcode_mtp flag is disabled, we open files
via MediaProvider ContentResolver, this allows the MediaProvider
explictly disable transcode for the MTP process.

We are trying to optimize for the current default transcode option - B
which is to transcode by default. For this case we keep the original
path.

Note that this optimization also implies that if the user changes the
default option to A (not transcode) then no transcoding will happen even
if the user has enabled transcoding for MTP in the USB preferences.

Test: Manual
Bug: 158466651
Change-Id: I6ee8c0e74952fb4ddb94f4f2f37d108b8245dc3f
parent 6ab4df23
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -91,6 +91,8 @@ public:
                                            int64_t& outFileLength,
                                            MtpObjectFormat& outFormat) = 0;

    virtual int                     openFilePath(const char* path, bool transcode) = 0;

    virtual MtpResponseCode         beginDeleteObject(MtpObjectHandle handle) = 0;
    virtual void                    endDeleteObject(MtpObjectHandle handle, bool succeeded) = 0;

+27 −7
Original line number Diff line number Diff line
@@ -791,10 +791,33 @@ MtpResponseCode MtpServer::doGetObject() {

    const char* filePath = (const char *)pathBuf;
    mtp_file_range mfr;
    struct stat sstat;
    uint64_t finalsize;
    bool transcode = android::base::GetBoolProperty("sys.fuse.transcode_mtp", true);
    if (!transcode) {
        ALOGD("Mtp transcode disabled");
        mfr.fd = mDatabase->openFilePath(filePath, false);
        // Doing this here because we want to update fileLength only for this case and leave the
        // regular path as unchanged as possible.
        if (mfr.fd >= 0) {
            fstat(mfr.fd, &sstat);
            finalsize = sstat.st_size;
            fileLength = finalsize;
        } else {
            ALOGW("Mtp open with no transcoding failed for %s. Falling back to the original",
                    filePath);
        }
    }

    if (transcode || mfr.fd < 0) {
        mfr.fd = open(filePath, O_RDONLY);
        if (mfr.fd < 0) {
            return MTP_RESPONSE_GENERAL_ERROR;
        }
        fstat(mfr.fd, &sstat);
        finalsize = sstat.st_size;
    }

    mfr.offset = 0;
    mfr.length = fileLength;
    mfr.command = mRequest.getOperationCode();
@@ -815,9 +838,6 @@ MtpResponseCode MtpServer::doGetObject() {

    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> diff = end - start;
    struct stat sstat;
    fstat(mfr.fd, &sstat);
    uint64_t finalsize = sstat.st_size;
    ALOGV("Sent a file over MTP. Time: %f s, Size: %" PRIu64 ", Rate: %f bytes/s",
            diff.count(), finalsize, ((double) finalsize) / diff.count());
    closeObjFd(mfr.fd, filePath);
+5 −0
Original line number Diff line number Diff line
@@ -252,6 +252,11 @@ MtpResponseCode MtpMockDatabase::getObjectFilePath(MtpObjectHandle handle,
    return MTP_RESPONSE_OK;
}

int MtpMockDatabase::openFilePath(const char* path, bool transcode) {
    ALOGD("MockDatabase %s: filePath=%s transcode=%d\n", __func__, path, transcode);
    return 0;
}

MtpResponseCode MtpMockDatabase::beginDeleteObject(MtpObjectHandle handle) {
    ALOGD("MockDatabase %s: ohandle=%u\n", __func__, handle);
    return MTP_RESPONSE_OK;
+2 −0
Original line number Diff line number Diff line
@@ -90,6 +90,8 @@ public:
    MtpResponseCode getObjectFilePath(MtpObjectHandle handle, MtpStringBuffer& outFilePath,
                                      int64_t& outFileLength, MtpObjectFormat& outFormat);

    int openFilePath(const char* path, bool transcode);

    MtpResponseCode beginDeleteObject(MtpObjectHandle handle);
    void endDeleteObject(MtpObjectHandle handle, bool succeeded);