Skip to content

Commit afa8fbc

Browse files
committed
io.appium.settings: recording: Add timer limitor
Signed-off-by: sirmordred <[email protected]>
1 parent 0c1907e commit afa8fbc

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
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,12 +48,14 @@
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_MAX_DURATION;
5152
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_PRIORITY;
5253
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_RESULT_CODE;
5354
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_ROTATION;
5455
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_START;
5556
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_STOP;
5657
import static io.appium.settings.recorder.RecorderConstant.NO_ROTATION_SET;
58+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_MAX_DURATION_DEFAULT_MS;
5759
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_MAX;
5860
import static io.appium.settings.recorder.RecorderConstant.REQUEST_CODE_SCREEN_CAPTURE;
5961

@@ -63,6 +65,7 @@ public class Settings extends Activity {
6365
private String recordingOutputPath = "";
6466
private int recordingRotation = NO_ROTATION_SET;
6567
private int recordingPriority = RECORDING_PRIORITY_MAX;
68+
private int recordingMaxDuration = RECORDING_MAX_DURATION_DEFAULT_MS;
6669

6770
@Override
6871
public void onCreate(Bundle savedInstanceState) {
@@ -159,6 +162,8 @@ so we need to call getExternalFilesDir() method twice
159162

160163
recordingPriority = RecorderUtil.getRecordingPriority(intent);
161164

165+
recordingMaxDuration = RecorderUtil.getRecordingMaxDuration(intent);
166+
162167
// start record
163168
final MediaProjectionManager manager
164169
= (MediaProjectionManager) getSystemService(
@@ -219,6 +224,7 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
219224
intent.putExtra(ACTION_RECORDING_FILENAME, recordingOutputPath);
220225
intent.putExtra(ACTION_RECORDING_ROTATION, recordingRotation);
221226
intent.putExtra(ACTION_RECORDING_PRIORITY, recordingPriority);
227+
intent.putExtra(ACTION_RECORDING_MAX_DURATION, recordingMaxDuration);
222228
intent.putExtras(data);
223229

224230
startService(intent);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class RecorderConstant {
2727
public static final String ACTION_RECORDING_FILENAME = "recording_filename";
2828
public static final String ACTION_RECORDING_ROTATION = "recording_rotation";
2929
public static final String ACTION_RECORDING_PRIORITY = "recording_priority";
30+
public static final String ACTION_RECORDING_MAX_DURATION = "recording_max_duration";
3031
public static final float BITRATE_MULTIPLIER = 0.25f;
3132
public static final int AUDIO_CODEC_SAMPLE_RATE_HZ = 44100;
3233
public static final int AUDIO_CODEC_CHANNEL_COUNT = 1;
@@ -43,4 +44,5 @@ public class RecorderConstant {
4344
public static final int RECORDING_PRIORITY_MIN = 1;
4445
public static final int RECORDING_PRIORITY_NORM = 2;
4546
public static final int RECORDING_PRIORITY_DEFAULT = Thread.MAX_PRIORITY;
47+
public static final int RECORDING_MAX_DURATION_DEFAULT_MS = 15 * 60 * 1000; // 15 Minute, in miliseconds
4648
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
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_MAX_DURATION;
3435
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_PRIORITY;
3536
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_RESULT_CODE;
3637
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_ROTATION;
3738
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_START;
3839
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_STOP;
40+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_MAX_DURATION_DEFAULT_MS;
3941
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_DEFAULT;
4042

4143
public class RecorderService extends Service {
@@ -162,8 +164,12 @@ private void startScreenRecord(MediaProjectionManager mediaProjectionManager,
162164
int recordingPriority = intent.getIntExtra(ACTION_RECORDING_PRIORITY,
163165
RECORDING_PRIORITY_DEFAULT);
164166

167+
int recordingMaxDuration = intent.getIntExtra(ACTION_RECORDING_MAX_DURATION,
168+
RECORDING_MAX_DURATION_DEFAULT_MS);
169+
165170
recorderThread = new RecorderThread(projection, outputFilePath,
166-
rawWidth, rawHeight, recordingRotation, recordingPriority);
171+
rawWidth, rawHeight, recordingRotation, recordingPriority,
172+
recordingMaxDuration);
167173
recorderThread.startRecording();
168174
}
169175

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public class RecorderThread implements Runnable {
5353
private final int videoWidth;
5454
private final int videoHeight;
5555
private final int recordingRotation;
56-
private int recordingPriority = RECORDING_PRIORITY_DEFAULT;
56+
private final int recordingPriority;
57+
private final int recordingMaxDuration;
5758

5859
private boolean muxerStarted = false;
5960
private boolean isStartTimestampInitialized = false;
@@ -86,13 +87,14 @@ public void onStopped() {
8687

8788
public RecorderThread(MediaProjection mediaProjection, String outputFilePath,
8889
int videoWidth, int videoHeight, int recordingRotation,
89-
int recordingPriority) {
90+
int recordingPriority, int recordingMaxDuration) {
9091
this.mediaProjection = mediaProjection;
9192
this.outputFilePath = outputFilePath;
9293
this.videoWidth = videoWidth;
9394
this.videoHeight = videoHeight;
9495
this.recordingRotation = recordingRotation;
9596
this.recordingPriority = recordingPriority;
97+
this.recordingMaxDuration = recordingMaxDuration;
9698
}
9799

98100
public void startRecording() {
@@ -399,6 +401,8 @@ public void run() {
399401
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
400402
lastAudioTimestampUs = NO_TIMESTAMP_SET;
401403

404+
long recordingStartTime = System.currentTimeMillis();
405+
402406
while (!stopped && !hasAsyncError) {
403407
if (!writeAudioBufferToFile(audioEncoder, muxer, bufferInfo)) {
404408
break;
@@ -411,6 +415,11 @@ public void run() {
411415
if (!writeVideoBufferToFile(videoEncoder, muxer, bufferInfo)) {
412416
break;
413417
}
418+
419+
if ((System.currentTimeMillis() - recordingStartTime) < this.recordingMaxDuration) {
420+
Log.v(TAG, "Recording stopped, reached maximum duration");
421+
stopped = true;
422+
}
414423
}
415424
} catch (Exception mainException) {
416425
mainException.printStackTrace();

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import androidx.core.app.ActivityCompat;
3030

3131
import static android.content.Context.WINDOW_SERVICE;
32+
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_MAX_DURATION;
3233
import static io.appium.settings.recorder.RecorderConstant.ACTION_RECORDING_PRIORITY;
3334
import static io.appium.settings.recorder.RecorderConstant.NO_ROTATION_SET;
35+
import static io.appium.settings.recorder.RecorderConstant.RECORDING_MAX_DURATION_DEFAULT_MS;
3436
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_DEFAULT;
3537
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_MAX;
3638
import static io.appium.settings.recorder.RecorderConstant.RECORDING_PRIORITY_MIN;
@@ -141,4 +143,24 @@ public static int getRecordingPriority(Intent intent) {
141143
}
142144
return RECORDING_PRIORITY_DEFAULT;
143145
}
146+
147+
public static int getRecordingMaxDuration(Intent intent) {
148+
if (intent.hasExtra(ACTION_RECORDING_MAX_DURATION)) {
149+
try {
150+
int userRequestedMaxDurationInSecond =
151+
Integer.parseInt(intent.getStringExtra(ACTION_RECORDING_MAX_DURATION));
152+
if (userRequestedMaxDurationInSecond <= 0) {
153+
Log.e(TAG, "Maximum recording duration must be greater than 0 second");
154+
return RECORDING_MAX_DURATION_DEFAULT_MS;
155+
}
156+
// Convert it to millisecond and return
157+
return userRequestedMaxDurationInSecond * 1000;
158+
} catch (NumberFormatException e) {
159+
Log.e(TAG, "Exception while retrieving recording max duration", e);
160+
}
161+
} else {
162+
Log.e(TAG, "Unable to retrieve recording max duration");
163+
}
164+
return RECORDING_MAX_DURATION_DEFAULT_MS;
165+
}
144166
}

0 commit comments

Comments
 (0)