aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/invalid/lena/scrcpy/AudioSink.java
blob: f14f85cc8597c46bfee00a68484f00724916d79f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package invalid.lena.scrcpy;

import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.HandlerThread;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

// Audio output: AudioTrack writing 48 kHz stereo 16-bit PCM. The
// upstream feed is either raw PCM (passthrough) or Opus packets
// (decoded through MediaCodec into PCM first).
public final class AudioSink implements AudioFrames {

    private static final int SAMPLE_RATE = 48_000;
    private static final int CHANNEL_OUT = AudioFormat.CHANNEL_OUT_STEREO;
    private static final int ENCODING    = AudioFormat.ENCODING_PCM_16BIT;
    private static final int MAX_PENDING_OPUS = 16;
    private static final int MAX_PENDING_PCM = 16;
    private static final int MAX_PACKET_BYTES = 256 * 1024;

    // Defaults documented at <https://developer.android.com/reference/android/media/MediaCodec#CSD>.
    private static final long DEFAULT_PRE_ROLL_NS = 80_000_000L;

    private AudioTrack    track;
    private volatile MediaCodec opusCodec;
    private HandlerThread opusThread;
    private Handler       opusHandler;
    private Thread        opusOutputThread;
    private volatile boolean opusConfigured;
    private int           fourcc;

    private final Object lock = new Object();
    private final AtomicBoolean failed = new AtomicBoolean();
    private final AtomicBoolean playbackReported = new AtomicBoolean();
    private final Deque<Integer> freeOpusInputs = new ArrayDeque<>();
    private final Deque<Packet> pendingOpus = new ArrayDeque<>(MAX_PENDING_OPUS);
    private final ArrayBlockingQueue<byte[]> pendingPcm =
            new ArrayBlockingQueue<>(MAX_PENDING_PCM);

    private static final class Packet {
        final byte[] data;
        final long ptsUs;

        Packet(byte[] data, long ptsUs) {
            this.data = data;
            this.ptsUs = ptsUs;
        }
    }

    private volatile boolean released;
    @Override
    public void start(int fourcc) {
        this.fourcc = fourcc;

        int minBuf = AudioTrack.getMinBufferSize(SAMPLE_RATE, CHANNEL_OUT, ENCODING);
        if (minBuf <= 0) throw new IllegalStateException("AudioTrack.getMinBufferSize=" + minBuf);
        int bufSize = Math.max(minBuf, 32 * 1024);

        AudioAttributes attrs = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build();
        AudioFormat fmt = new AudioFormat.Builder()
                .setSampleRate(SAMPLE_RATE)
                .setChannelMask(CHANNEL_OUT)
                .setEncoding(ENCODING)
                .build();

        track = new AudioTrack(
                attrs, fmt, bufSize, AudioTrack.MODE_STREAM, AudioManager.AUDIO_SESSION_ID_GENERATE);
        if (track.getState() != AudioTrack.STATE_INITIALIZED) {
            track.release();
            track = null;
            throw new IllegalStateException("audio sink: AudioTrack failed to initialize");
        }
        track.play();
        if (track.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) {
            track.release();
            track = null;
            throw new IllegalStateException("audio sink: AudioTrack failed to start");
        }
        Log.i("audio sink: AudioTrack started sr=%d ch=2 buf=%d", SAMPLE_RATE, bufSize);

        if (fourcc == Wire.CODEC_OPUS) {
            opusThread = new HandlerThread("audio-mc");
            opusThread.start();
            opusHandler = new Handler(opusThread.getLooper());
            // Codec is created here, but only configured once the first
            // FLAG_CONFIG frame arrives with the OpusHead bytes.
            try {
                opusCodec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_AUDIO_OPUS);
            } catch (IOException e) {
                throw new IllegalStateException("audio sink: no opus decoder", e);
            }
            opusOutputThread = new Thread(this::playOpus, "audio-out");
            opusOutputThread.start();
            opusCodec.setCallback(new MediaCodec.Callback() {
                @Override public void onInputBufferAvailable(MediaCodec mc, int idx) {
                    synchronized (lock) {
                        if (released || failed.get() || mc != opusCodec) return;
                        Packet packet = pendingOpus.pollFirst();
                        if (packet == null) {
                            freeOpusInputs.offerLast(idx);
                        } else {
                            lock.notifyAll();
                            submitOpus(mc, idx, packet);
                        }
                    }
                }
                @Override public void onOutputBufferAvailable(MediaCodec mc, int idx,
                                                              MediaCodec.BufferInfo info) {
                    byte[] pcm = null;
                    try {
                        ByteBuffer out = mc.getOutputBuffer(idx);
                        if (out != null && info.size > 0 && !released && !failed.get()) {
                            pcm = new byte[info.size];
                            out.position(info.offset);
                            out.limit(info.offset + info.size);
                            out.get(pcm);
                        }
                    } catch (IllegalStateException e) {
                        reportFatal(e, "audio sink: opus output");
                    } finally {
                        try { mc.releaseOutputBuffer(idx, false); }
                        catch (IllegalStateException ignored) {}
                    }
                    if (pcm != null) queuePcm(pcm);
                }
                @Override public void onError(MediaCodec mc, MediaCodec.CodecException e) {
                    reportFatal(e, "audio sink: opus codec error");
                }
                @Override public void onOutputFormatChanged(MediaCodec mc, MediaFormat f) {
                    Log.i("audio sink: opus output format %s", f);
                }
            }, opusHandler);
        }
    }

    @Override
    public void feed(byte[] data, int off, int len, long ptsUs, boolean isConfig) {
        if (data == null || off < 0 || len < 0 || off > data.length - len) {
            throw new IndexOutOfBoundsException("invalid audio frame range");
        }
        if (len > MAX_PACKET_BYTES) {
            reportFatal(null, "audio sink: packet too large (" + len + " bytes)");
            return;
        }
        if (released || failed.get() || len == 0) return;
        if (fourcc == Wire.CODEC_OPUS) {
            feedOpus(data, off, len, ptsUs, isConfig);
        } else {
            // Raw PCM: passthrough.
            writePcm(data, off, len);
        }
    }

    private void feedOpus(byte[] data, int off, int len, long ptsUs, boolean isConfig) {
        if (isConfig) {
            if (opusConfigured) return; // already configured
            // OpusHead is 19 bytes; pre_skip lives at bytes [10..11]. Reject
            // anything shorter before indexing into it.
            if (len < 19) {
                reportFatal(null, "audio sink: opus head too short (" + len + " bytes)");
                return;
            }
            try {
                byte[] head = new byte[len];
                System.arraycopy(data, off, head, 0, len);
                int preSkipSamples = ((head[10] & 0xff) | ((head[11] & 0xff) << 8));
                long preSkipNs = preSkipSamples * 1_000_000_000L / SAMPLE_RATE;

                MediaFormat fmt = MediaFormat.createAudioFormat(
                        MediaFormat.MIMETYPE_AUDIO_OPUS, SAMPLE_RATE, 2);
                fmt.setByteBuffer("csd-0", ByteBuffer.wrap(head));
                fmt.setByteBuffer("csd-1", longLeBytes(preSkipNs));
                fmt.setByteBuffer("csd-2", longLeBytes(DEFAULT_PRE_ROLL_NS));
                opusCodec.configure(fmt, null, null, 0);
                opusCodec.start();
                opusConfigured = true;
                Log.i("audio sink: opus configured, pre_skip=%d ns", preSkipNs);
            } catch (Exception e) {
                reportFatal(e, "audio sink: opus configure");
            }
            return;
        }
        if (!opusConfigured) return;
        byte[] packet = new byte[len];
        System.arraycopy(data, off, packet, 0, len);
        Packet p = new Packet(packet, ptsUs);
        synchronized (lock) {
            if (released || opusCodec == null) return;
            // Apply socket backpressure during decoder bursts. Dropping a
            // compressed packet corrupts playback; growing the queue only
            // postpones the same failure.
            while (!released && !failed.get() && freeOpusInputs.isEmpty()
                    && pendingOpus.size() == MAX_PENDING_OPUS) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
            if (released || failed.get() || opusCodec == null) return;
            Integer idx = freeOpusInputs.pollFirst();
            if (idx != null) {
                submitOpus(opusCodec, idx, p);
                return;
            }
            pendingOpus.offerLast(p);
        }
    }

    // Must be called with lock held. Async MediaCodec input indices belong
    // to the codec instance that delivered them.
    private void submitOpus(MediaCodec codec, int idx, Packet packet) {
        try {
            ByteBuffer in = codec.getInputBuffer(idx);
            if (in == null || packet.data.length > in.capacity()) {
                reportFatal(null,
                        "audio sink: opus packet exceeds codec input ("
                                + packet.data.length + " bytes)");
                return;
            }
            in.clear();
            in.put(packet.data);
            codec.queueInputBuffer(idx, 0, packet.data.length, packet.ptsUs, 0);
        } catch (IllegalStateException e) {
            reportFatal(e, "audio sink: opus queueInputBuffer");
        }
    }

    // Encode a long little-endian for MediaFormat csd-1 / csd-2.
    private static ByteBuffer longLeBytes(long v) {
        ByteBuffer b = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(v);
        b.flip();
        return b;
    }

    private void queuePcm(byte[] pcm) {
        try {
            while (!released && !failed.get()
                    && !pendingPcm.offer(pcm, 100, TimeUnit.MILLISECONDS)) {
                // Wait for the blocking AudioTrack writer to make room.
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    private void playOpus() {
        try {
            while (!released && !failed.get()) {
                byte[] pcm = pendingPcm.take();
                writePcm(pcm, 0, pcm.length);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (RuntimeException e) {
            reportFatal(e, "audio sink: AudioTrack write");
        }
    }

    private void writePcm(byte[] data, int off, int len) {
        AudioTrack t = track;
        if (released || failed.get() || t == null || len <= 0) return;
        int end = off + len;
        while (!released && !failed.get() && off < end) {
            int written = t.write(data, off, end - off, AudioTrack.WRITE_BLOCKING);
            if (written <= 0) {
                if (!released) {
                    reportFatal(null, "audio sink: AudioTrack write rc=" + written);
                }
                return;
            }
            off += written;
            if (playbackReported.compareAndSet(false, true)) {
                Log.i("audio sink: playback started");
            }
        }
    }

    private void reportFatal(Exception error, String message) {
        if (!failed.compareAndSet(false, true)) return;
        if (error == null) Log.e("%s", message);
        else Log.e(error, "%s", message);
        synchronized (lock) {
            pendingOpus.clear();
            lock.notifyAll();
        }
        pendingPcm.clear();
        Thread t = opusOutputThread;
        if (t != null) t.interrupt();
    }

    @Override
    public void release() {
        MediaCodec c;
        synchronized (lock) {
            released = true;
            c = opusCodec;
            opusCodec = null;
            freeOpusInputs.clear();
            pendingOpus.clear();
            lock.notifyAll();
        }
        AudioTrack t = track;
        track = null;
        // Stop playback first so the blocking writer can return before
        // AudioTrack is released.
        if (t != null) {
            try { t.pause(); t.flush(); t.stop(); } catch (Exception ignored) {}
        }

        HandlerThread ht = opusThread;
        opusThread = null;
        opusHandler = null;
        Thread out = opusOutputThread;
        opusOutputThread = null;
        if (out != null) out.interrupt();
        if (c != null) {
            try { c.stop(); } catch (Exception ignored) {}
            try { c.release(); } catch (Exception ignored) {}
        }
        if (ht != null) ht.quitSafely();
        if (out != null && out != Thread.currentThread()) {
            try { out.join(1_000); }
            catch (InterruptedException e) { Thread.currentThread().interrupt(); }
            if (out.isAlive()) Log.e("audio sink: output thread did not stop");
        }
        pendingPcm.clear();
        if (t != null) {
            try { t.release(); } catch (Exception ignored) {}
        }
    }
}