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
diff --git a/media/mtp/MtpServer.cpp b/media/mtp/MtpServer.cpp
index 8677b90..d0ad22c 100644
--- a/media/mtp/MtpServer.cpp
+++ b/media/mtp/MtpServer.cpp
@@ -790,11 +790,34 @@
auto start = std::chrono::steady_clock::now();
const char* filePath = (const char *)pathBuf;
- mtp_file_range mfr;
- mfr.fd = open(filePath, O_RDONLY);
- if (mfr.fd < 0) {
- return MTP_RESPONSE_GENERAL_ERROR;
+ 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 @@
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);