trancoding: lazy-start transcoding service

bug: 186387244
test: atest MediaTranscodeManagerTest; unit tests
Change-Id: I354f32d062b9a46ddfc9f86d5f79c778809799a4
This commit is contained in:
Chong Zhang
2021-04-26 14:25:07 -07:00
parent c9fcf22f2b
commit 0621358a52
2 changed files with 28 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
@@ -36,6 +37,7 @@ import android.util.Log;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.annotation.MinSdk;
import com.android.modules.utils.build.SdkLevel;
import java.io.FileNotFoundException;
import java.lang.annotation.Retention;
@@ -119,6 +121,7 @@ public final class MediaTranscodeManager {
private final String mPackageName;
private final int mPid;
private final int mUid;
private final boolean mIsLowRamDevice;
private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
private final HashMap<Integer, TranscodingSession> mPendingTranscodingSessions = new HashMap();
private final Object mLock = new Object();
@@ -199,7 +202,16 @@ public final class MediaTranscodeManager {
}
}
private static IMediaTranscodingService getService(boolean retry) {
private IMediaTranscodingService getService(boolean retry) {
// Do not try to get the service on pre-S. The service is lazy-start and getting the
// service could block.
if (!SdkLevel.isAtLeastS()) {
return null;
}
// Do not try to get the service on AndroidGo (low-ram) devices.
if (mIsLowRamDevice) {
return null;
}
int retryCount = !retry ? 1 : CONNECT_SERVICE_RETRY_COUNT;
Log.i(TAG, "get service with retry " + retryCount);
for (int count = 1; count <= retryCount; count++) {
@@ -417,6 +429,7 @@ public final class MediaTranscodeManager {
mPackageName = mContext.getPackageName();
mUid = Os.getuid();
mPid = Os.getpid();
mIsLowRamDevice = mContext.getSystemService(ActivityManager.class).isLowRamDevice();
IMediaTranscodingService service = getService(false /*retry*/);
if (service != null) {
mTranscodingClient = registerClient(service);

View File

@@ -45,12 +45,21 @@ public class MediaServiceManager {
*/
public static final class ServiceRegisterer {
private final String mServiceName;
private final boolean mLazyStart;
/**
* @hide
*/
public ServiceRegisterer(String serviceName, boolean lazyStart) {
mServiceName = serviceName;
mLazyStart = lazyStart;
}
/**
* @hide
*/
public ServiceRegisterer(String serviceName) {
mServiceName = serviceName;
this(serviceName, false /*lazyStart*/);
}
/**
@@ -61,6 +70,9 @@ public class MediaServiceManager {
*/
@Nullable
public IBinder get() {
if (mLazyStart) {
return ServiceManager.waitForService(mServiceName);
}
return ServiceManager.getService(mServiceName);
}
}
@@ -78,7 +90,7 @@ public class MediaServiceManager {
*/
@NonNull
public ServiceRegisterer getMediaTranscodingServiceRegisterer() {
return new ServiceRegisterer(MEDIA_TRANSCODING_SERVICE);
return new ServiceRegisterer(MEDIA_TRANSCODING_SERVICE, true /*lazyStart*/);
}
/**