Skip to content

Commit d229c00

Browse files
committed
io.appium.settings: recording: Make audio encoder thread's priority dynamic
Signed-off-by: sirmordred <[email protected]>
1 parent cd56a26 commit d229c00

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

app/src/main/java/io/appium/settings/Settings.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,21 @@
4848

4949
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_BASE;
5050
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_FILENAME;
51+
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_PRIORITY;
5152
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_RESULT_CODE;
5253
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_ROTATION;
5354
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_START;
5455
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_STOP;
5556
import static io.appium.settings.recorder.RecorderConstant.NO_ROTATION_SET;
57+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_MAX;
5658
import static io.appium.settings.recorder.RecorderConstant.REQUEST_CODE_SCREEN_CAPTURE;
5759

5860
public class Settings extends Activity {
5961
private static final String TAG = "APPIUM SETTINGS";
6062

6163
private String recordingOutputPath = "";
6264
private int recordingRotation = NO_ROTATION_SET;
65+
private int recordingPriority = RECORDING_PRIORITY_MAX;
6366

6467
@Override
6568
public void onCreate(Bundle savedInstanceState) {
@@ -154,6 +157,8 @@ so we need to call getExternalFilesDir() method twice
154157

155158
recordingRotation = RecorderUtil.getDeviceRotation(getApplicationContext());
156159

160+
recordingPriority = RecorderUtil.getRecordingPriority(intent);
161+
157162
// start record
158163
final MediaProjectionManager manager
159164
= (MediaProjectionManager) getSystemService(
@@ -213,6 +218,7 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
213218
intent.putExtra(ACTION_RECORDING_RESULT_CODE, resultCode);
214219
intent.putExtra(ACTION_RECORDING_FILENAME, recordingOutputPath);
215220
intent.putExtra(ACTION_RECORDING_ROTATION, recordingRotation);
221+
intent.putExtra(ACTION_RECORDING_PRIORITY, recordingPriority);
216222
intent.putExtras(data);
217223

218224
startService(intent);

app/src/main/java/io/appium/settings/recorder/RecorderConstant.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class RecorderConstant {
2626
public static final String ACTION_RECORDING_RESULT_CODE = "result_code";
2727
public static final String ACTION_RECORDING_FILENAME = "recording_filename";
2828
public static final String ACTION_RECORDING_ROTATION = "recording_rotation";
29+
public static final String ACTION_RECORDING_PRIORITY = "recording_priority";
2930
public static final float BITRATE_MULTIPLIER = 0.25f;
3031
public static final int AUDIO_CODEC_SAMPLE_RATE_HZ = 44100;
3132
public static final int AUDIO_CODEC_CHANNEL_COUNT = 1;
@@ -38,4 +39,8 @@ public class RecorderConstant {
3839
public static final long NO_TIMESTAMP_SET = -1;
3940
public static final int NO_ROTATION_SET = -1;
4041
public static final int NO_TRACK_INDEX_SET = -1;
42+
public static final int RECORDING_PRIORITY_MAX = 0;
43+
public static final int RECORDING_PRIORITY_MIN = 1;
44+
public static final int RECORDING_PRIORITY_NORM = 2;
45+
public static final int RECORDING_PRIORITY_DEFAULT = Thread.MAX_PRIORITY;
4146
}

app/src/main/java/io/appium/settings/recorder/RecorderService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
import io.appium.settings.helpers.NotificationHelpers;
3232

3333
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_FILENAME;
34+
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_PRIORITY;
3435
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_RESULT_CODE;
3536
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_ROTATION;
3637
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_START;
3738
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_STOP;
39+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_DEFAULT;
3840

3941
public class RecorderService extends Service {
4042
private static final String TAG = "RecorderService";
@@ -157,8 +159,11 @@ private void startScreenRecord(MediaProjectionManager mediaProjectionManager,
157159
Log.v(TAG, String.format("Starting recording with width/height(not clamped):(%d,%d)",
158160
rawWidth, rawHeight));
159161

162+
int recordingPriority = intent.getIntExtra(ACTION_RECORDING_PRIORITY,
163+
RECORDING_PRIORITY_DEFAULT);
164+
160165
recorderThread = new RecorderThread(projection, outputFilePath,
161-
rawWidth, rawHeight, recordingRotation);
166+
rawWidth, rawHeight, recordingRotation, recordingPriority);
162167
recorderThread.startRecording();
163168
}
164169

app/src/main/java/io/appium/settings/recorder/RecorderThread.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static io.appium.settings.recorder.RecorderConstant.NANOSECOND_TO_MICROSECOND;
4343
import static io.appium.settings.recorder.RecorderConstant.NO_TIMESTAMP_SET;
4444
import static io.appium.settings.recorder.RecorderConstant.NO_TRACK_INDEX_SET;
45+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_DEFAULT;
4546

4647
public class RecorderThread implements Runnable {
4748

@@ -52,6 +53,7 @@ public class RecorderThread implements Runnable {
5253
private final int videoWidth;
5354
private final int videoHeight;
5455
private final int recordingRotation;
56+
private int recordingPriority = RECORDING_PRIORITY_DEFAULT;
5557

5658
private boolean muxerStarted = false;
5759
private boolean isStartTimestampInitialized = false;
@@ -83,12 +85,14 @@ public void onStopped() {
8385
};
8486

8587
public RecorderThread(MediaProjection mediaProjection, String outputFilePath,
86-
int videoWidth, int videoHeight, int recordingRotation) {
88+
int videoWidth, int videoHeight, int recordingRotation,
89+
int recordingPriority) {
8790
this.mediaProjection = mediaProjection;
8891
this.outputFilePath = outputFilePath;
8992
this.videoWidth = videoWidth;
9093
this.videoHeight = videoHeight;
9194
this.recordingRotation = recordingRotation;
95+
this.recordingPriority = recordingPriority;
9296
}
9397

9498
public void startRecording() {
@@ -170,11 +174,12 @@ private AudioRecord initAudioRecord(MediaProjection mediaProjection, int sampleR
170174
}
171175

172176
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
173-
private Thread initAudioRecordThread(MediaCodec audioEncoder, final AudioRecord audioRecord) {
177+
private Thread initAudioRecordThread(MediaCodec audioEncoder, final AudioRecord audioRecord,
178+
int priority) {
174179
return new Thread(new Runnable() {
175180
@Override
176181
public void run() {
177-
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
182+
Thread.currentThread().setPriority(priority);
178183
try {
179184
audioRecord.startRecording();
180185
} catch (Exception e) {
@@ -387,7 +392,8 @@ public void run() {
387392
// note: this method must be run before muxer.start()
388393
muxer.setOrientationHint(recordingRotation);
389394

390-
audioRecordThread = initAudioRecordThread(audioEncoder, audioRecord);
395+
audioRecordThread = initAudioRecordThread(audioEncoder, audioRecord,
396+
this.recordingPriority);
391397
audioRecordThread.start();
392398

393399
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

app/src/main/java/io/appium/settings/recorder/RecorderUtil.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,27 @@
1818

1919
import android.Manifest;
2020
import android.content.Context;
21+
import android.content.Intent;
2122
import android.content.pm.PackageManager;
2223
import android.os.Build;
24+
import android.util.Log;
2325
import android.view.Display;
2426
import android.view.Surface;
2527
import android.view.WindowManager;
2628

2729
import androidx.core.app.ActivityCompat;
2830

2931
import static android.content.Context.WINDOW_SERVICE;
32+
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_PRIORITY;
3033
import static io.appium.settings.recorder.RecorderConstant.NO_ROTATION_SET;
34+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_DEFAULT;
35+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_MAX;
36+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_MIN;
37+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_NORM;
3138

3239
public class RecorderUtil {
40+
private static final String TAG = "RecorderUtil";
41+
3342
public static boolean isLowerThanQ() {
3443
return Build.VERSION.SDK_INT < Build.VERSION_CODES.Q;
3544
}
@@ -108,4 +117,28 @@ public static int getDeviceRotation(Context context) {
108117
}
109118
return NO_ROTATION_SET;
110119
}
120+
121+
public static int getRecordingPriority(Intent intent) {
122+
if (intent.hasExtra(ACTION_RECORDING_PRIORITY)) {
123+
try {
124+
int userRequestedRecordingPriority =
125+
Integer.parseInt(intent.getStringExtra(ACTION_RECORDING_PRIORITY));
126+
switch(userRequestedRecordingPriority) {
127+
case RECORDING_PRIORITY_MAX:
128+
return RECORDING_PRIORITY_DEFAULT;
129+
case RECORDING_PRIORITY_MIN:
130+
return Thread.MIN_PRIORITY;
131+
case RECORDING_PRIORITY_NORM:
132+
return Thread.NORM_PRIORITY;
133+
default:
134+
break;
135+
}
136+
} catch (NumberFormatException e) {
137+
Log.e(TAG, "Exception while retrieving recording priority", e);
138+
}
139+
} else {
140+
Log.e(TAG, "Unable to retrieve recording priority");
141+
}
142+
return RECORDING_PRIORITY_DEFAULT;
143+
}
111144
}

0 commit comments

Comments
 (0)