Skip to content

Commit b93a8b9

Browse files
committed
Quit on audio configuration failure
When audio capture fails on the device, scrcpy continue mirroring the video stream. This allows to enable audio by default only when supported. However, if an audio configuration occurs (for example the user explicitly selected an unknown audio encoder), this must be treated as an error and scrcpy must exit. PR #3757 <Genymobile/scrcpy#3757>
1 parent afbeeeb commit b93a8b9

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

app/src/demuxer.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ run_demuxer(void *data) {
195195
goto end;
196196
}
197197

198+
if (raw_codec_id == 1) {
199+
LOGE("Demuxer '%s': stream configuration error on the device",
200+
demuxer->name);
201+
goto end;
202+
}
203+
198204
enum AVCodecID codec_id = sc_demuxer_to_avcodec_id(raw_codec_id);
199205
if (codec_id == AV_CODEC_ID_NONE) {
200206
LOGE("Demuxer '%s': stream disabled due to unsupported codec",

app/src/scrcpy.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,19 @@ static void
231231
sc_audio_demuxer_on_ended(struct sc_demuxer *demuxer, bool eos,
232232
void *userdata) {
233233
(void) demuxer;
234-
(void) eos;
235234
(void) userdata;
236235

237-
// Contrary to the video demuxer, keep mirroring if only the audio fails
236+
// Contrary to the video demuxer, keep mirroring if only the audio fails.
237+
// 'eos' is true on end-of-stream, including when audio capture is not
238+
// possible on the device (so that scrcpy continue to mirror video without
239+
// failing).
240+
// However, if an audio configuration failure occurs (for example the user
241+
// explicitly selected an unknown audio encoder), 'eos' is false and scrcpy
242+
// must exit.
243+
244+
if (!eos) {
245+
PUSH_EVENT(SC_EVENT_DEMUXER_ERROR);
246+
}
238247
}
239248

240249
static void

server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private synchronized void waitEnded() {
222222
public void encode() throws IOException, ConfigurationException {
223223
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
224224
Ln.w("Audio disabled: it is not supported before Android 11");
225-
streamer.writeDisableStream();
225+
streamer.writeDisableStream(false);
226226
return;
227227
}
228228

@@ -279,9 +279,13 @@ public void encode() throws IOException, ConfigurationException {
279279
outputThread.start();
280280

281281
waitEnded();
282+
} catch (ConfigurationException e) {
283+
// Notify the error to make scrcpy exit
284+
streamer.writeDisableStream(true);
285+
throw e;
282286
} catch (Throwable e) {
283287
// Notify the client that the audio could not be captured
284-
streamer.writeDisableStream();
288+
streamer.writeDisableStream(false);
285289
throw e;
286290
} finally {
287291
// Cleanup everything (either at the end or on error at any step of the initialization)

server/src/main/java/com/genymobile/scrcpy/Streamer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ public void writeHeader() throws IOException {
4040
}
4141
}
4242

43-
public void writeDisableStream() throws IOException {
44-
// Writing 0 (32-bit) as codec-id means that the device disables the stream (because it could not capture)
45-
byte[] zeros = new byte[4];
46-
IO.writeFully(fd, zeros, 0, zeros.length);
43+
public void writeDisableStream(boolean error) throws IOException {
44+
// Writing a specific code as codec-id means that the device disables the stream
45+
// code 0: it explicitly disables the stream (because it could not capture audio), scrcpy should continue mirroring video only
46+
// code 1: a configuration error occurred, scrcpy must be stopped
47+
byte[] code = new byte[4];
48+
if (error) {
49+
code[3] = 1;
50+
}
51+
IO.writeFully(fd, code, 0, code.length);
4752
}
4853

4954
public void writePacket(ByteBuffer buffer, long pts, boolean config, boolean keyFrame) throws IOException {

0 commit comments

Comments
 (0)