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

Commit f0fbd643 authored by Kevin Rocard's avatar Kevin Rocard
Browse files

Limit transcoding size workaround to windows

Test: Connect android to windows through usbc.
      Enable mtp, then enable the transcoding option.
      Copy H265 video from the camera folder to windows' desktop.
      Transcoded H264 video plays correctly in WMPcorrectly.

Bug: 184117074
Bug: 190422448
Change-Id: I17264f0ee3e742315569d4c50817b1da7f3f2eb4
parent 1cfc0c74
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ public class MtpDatabase implements AutoCloseable {
    private int mDeviceType;
    private String mHostType;
    private boolean mSkipThumbForHost = false;
    private volatile boolean mHostIsWindows = false;

    private MtpServer mServer;
    private MtpStorageManager mManager;
@@ -358,7 +359,7 @@ public class MtpDatabase implements AutoCloseable {
    }

    public void addStorage(StorageVolume storage) {
        MtpStorage mtpStorage = mManager.addMtpStorage(storage);
        MtpStorage mtpStorage = mManager.addMtpStorage(storage, () -> mHostIsWindows);
        mStorageMap.put(storage.getPath(), mtpStorage);
        if (mServer != null) {
            mServer.addStorage(mtpStorage);
@@ -413,6 +414,7 @@ public class MtpDatabase implements AutoCloseable {
        }
        mHostType = "";
        mSkipThumbForHost = false;
        mHostIsWindows = false;
    }

    @VisibleForNative
@@ -736,10 +738,12 @@ public class MtpDatabase implements AutoCloseable {
                        : MtpConstants.RESPONSE_GENERAL_ERROR);
            case MtpConstants.DEVICE_PROPERTY_SESSION_INITIATOR_VERSION_INFO:
                mHostType = stringValue;
                if (stringValue.startsWith("Android/")) {
                Log.d(TAG, "setDeviceProperty." + Integer.toHexString(property)
                        + "=" + stringValue);
                if (stringValue.startsWith("Android/")) {
                    mSkipThumbForHost = true;
                } else if (stringValue.startsWith("Windows/")) {
                    mHostIsWindows = true;
                }
                return MtpConstants.RESPONSE_OK;
        }
+14 −1
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package android.mtp;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.storage.StorageVolume;

import java.util.function.Supplier;

/**
 * This class represents a storage unit on an MTP device.
 * Used only for MTP support in USB responder mode.
@@ -33,14 +35,16 @@ public class MtpStorage {
    private final boolean mRemovable;
    private final long mMaxFileSize;
    private final String mVolumeName;
    private final Supplier<Boolean> mIsHostWindows;

    public MtpStorage(StorageVolume volume, int storageId) {
    public MtpStorage(StorageVolume volume, int storageId, Supplier<Boolean> isHostWindows) {
        mStorageId = storageId;
        mPath = volume.getPath();
        mDescription = volume.getDescription(null);
        mRemovable = volume.isRemovable();
        mMaxFileSize = volume.getMaxFileSize();
        mVolumeName = volume.getMediaStoreVolumeName();
        mIsHostWindows = isHostWindows;
    }

    /**
@@ -93,4 +97,13 @@ public class MtpStorage {
    public String getVolumeName() {
        return mVolumeName;
    }

    /**
     * Returns true if the mtp host of this storage is Windows.
     *
     * @return is host Windows
     */
    public boolean isHostWindows() {
        return mIsHostWindows.get();
    }
}
+7 −4
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

/**
 * MtpStorageManager provides functionality for listing, tracking, and notifying MtpServer of
@@ -208,7 +209,7 @@ public class MtpStorageManager {

        private long maybeApplyTranscodeLengthWorkaround(long length) {
            // Windows truncates transferred files to the size advertised in the object property.
            if (isTranscodeMtpEnabled() && isFileTranscodeSupported()) {
            if (mStorage.isHostWindows() && isTranscodeMtpEnabled() && isFileTranscodeSupported()) {
                // If the file supports transcoding, we double the returned size to accommodate
                // the increase in size from transcoding to AVC. This is the same heuristic
                // applied in the FUSE daemon (MediaProvider).
@@ -455,10 +456,12 @@ public class MtpStorageManager {
     * @param volume Storage to add.
     * @return the associated MtpStorage
     */
    public synchronized MtpStorage addMtpStorage(StorageVolume volume) {
    public synchronized MtpStorage addMtpStorage(StorageVolume volume,
                                                 Supplier<Boolean> isHostWindows) {
        int storageId = ((getNextStorageId() & 0x0000FFFF) << 16) + 1;
        MtpStorage storage = new MtpStorage(volume, storageId);
        MtpObject root = new MtpObject(storage.getPath(), storageId, storage, null, true);
        MtpStorage storage = new MtpStorage(volume, storageId, isHostWindows);
        MtpObject root = new MtpObject(storage.getPath(), storageId, storage, /* parent= */ null,
                                       /* isDir= */ true);
        mRoots.put(storageId, root);
        return storage;
    }
+4 −3
Original line number Diff line number Diff line
@@ -159,10 +159,11 @@ public class MtpStorageManagerTest {
                Log.d(TAG, "sendObjectInfoChanged: " + id);
                objectsInfoChanged.add(id);
            }
        }, null);
        }, /* subdirectories= */ null);

        mainMtpStorage = manager.addMtpStorage(mainStorage);
        secondaryMtpStorage = manager.addMtpStorage(secondaryStorage);
        mainMtpStorage = manager.addMtpStorage(mainStorage, /* isHostWindows= */ () -> false);
        secondaryMtpStorage = manager.addMtpStorage(secondaryStorage,
                                                    /* isHostWindows= */ () -> false);
    }

    @After