Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ interface Transport { void write(byte[] packet) throws IOException; }
private final Transport transport;
private final Consumer<String> log, fail;
private final ArrayBlockingQueue<Chunk> queue = new ArrayBlockingQueue<>(8);
// Capture must not acquire the lifecycle monitor: stopped() can join its thread.
private final Object pcmLock = new Object();
private volatile Lease active;
private volatile boolean closed;
private Thread worker;
private final class Lease implements Capture {
volatile boolean enabled = true;
long samples, packets, nonSilent, lastLog;
public void pcm(byte[] bytes) {
synchronized (CarLifeMicrophone.this) {
if (closed || !enabled || active != this) return;
if (bytes.length == 0 || bytes.length > 3200 || bytes.length % 2 != 0)
throw new IllegalArgumentException("Invalid microphone PCM frame");
if (!queue.offer(new Chunk(this, bytes.clone()))) {
if (!closed && enabled) fail.accept("MICROPHONE_QUEUE_OVERFLOW");
}
boolean overflow;
synchronized (pcmLock) {
if (closed || !enabled || active != this) return;
if (bytes.length == 0 || bytes.length > 3200 || bytes.length % 2 != 0)
throw new IllegalArgumentException("Invalid microphone PCM frame");
overflow = !queue.offer(new Chunk(this, bytes.clone()));
}
if (overflow && !closed && enabled) fail.accept("MICROPHONE_QUEUE_OVERFLOW");
}
}
private static final class Chunk {
Expand Down Expand Up @@ -77,16 +79,21 @@ private void run() {
catch (Exception error) { if (!closed) fail.accept("MICROPHONE_FAILED " + error.getClass().getSimpleName()); }
}
private void end() {
Lease old = active; active = null;
Lease old;
synchronized (pcmLock) {
old = active; active = null;
if (old != null) old.enabled = false;
queue.clear();
}
if (old != null) {
old.enabled = false; queue.clear();
listener.stopped(old); log.accept("MICROPHONE_STOP");
}
}
@Override public synchronized void close() {
if (closed) return;
closed = true; end(); queue.clear();
if (worker != null) worker.interrupt();
closed = true;
try { end(); }
finally { if (worker != null) worker.interrupt(); }
}
/** Caller closes the socket before joining to unblock a stalled transport write. */
void awaitStopped() throws InterruptedException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void log(String message) {
listener.log(message);
}
public void media(CarLifeMedia.Snapshot state) { if (!stopped.get()) listener.media(state); }
public void goToDesktop() { if (!stopped.get()) listener.goToDesktop(); }
public void configured(int width, int height) { listener.configured(width, height); }
public void failed(String reason) { failure.compareAndSet(null, reason); }
public void video(byte[] bytes, long ptsUs) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
/** One attempt over TCP or USB; version fallback belongs to CarLifeRecovery. */
public final class CarLifeSession {
public static final int MAX_VIDEO_BYTES = Wire.MAX_VIDEO;
private static final int GO_TO_DESKTOP = 0x10021;
public interface Listener {
default void configured(int width, int height) {}
default void failed(String message) {}
default void media(CarLifeMedia.Snapshot state) {}
default void goToDesktop() {}
void log(String message);
// Session-relative microseconds from complete-frame arrival, not the coarse wire timestamp.
void video(byte[] annexB, long ptsUs) throws Exception;
Expand Down Expand Up @@ -157,6 +159,10 @@ private void message(Wire.Packet p) throws Exception {
if (microphone != null && matched && !closing.get() && (xiaomi == null || xiaomi.running()))
microphone.command(p.type, xiaomi == null ? p.body : xiaomi.decrypt(p.body));
if (seen.add(p.type) || (xiaomi != null && !xiaomi.running())) listener.log("RX_COMMAND id=" + p.type + " size=" + p.body.length);
if (p.type == GO_TO_DESKTOP) {
if (streaming && !closing.get()) listener.goToDesktop();
return;
}
if (xiaomi != null) {
java.util.List<XiaomiHandshake.Action> actions;
try { actions = xiaomi.accept(p.type, p.body, requestedSize); }
Expand Down Expand Up @@ -358,7 +364,6 @@ private void fail(String message) {
}
public CompletableFuture<Void> stop() {
if (!closing.compareAndSet(false, true)) return closed;
if (microphone != null) microphone.close();
CountDownLatch released = new CountDownLatch(1);
try {
control.execute(() -> {
Expand All @@ -370,6 +375,11 @@ public CompletableFuture<Void> stop() {
} catch (RejectedExecutionException ignored) { released.countDown(); }
new Thread(() -> {
try {
// A capture failure can call stop() from the capture thread itself.
// Join it here, and keep cleaning up even if its stop callback fails.
Exception microphoneFailure = null;
if (microphone != null) try { microphone.close(); }
catch (Exception error) { microphoneFailure = error; }
// Give the control owner a bounded chance to lift a held pointer.
// Closing the transport also unblocks a stalled writer or reader.
released.await(250, TimeUnit.MILLISECONDS);
Expand All @@ -382,6 +392,11 @@ public CompletableFuture<Void> stop() {
boolean clean = readers.awaitTermination(3, TimeUnit.SECONDS) && controlStopped;
if (clean && xiaomi != null) xiaomi.close();
listener.log(clean ? "STOPPED_CLEAN" : "STOP_TIMEOUT");
if (microphoneFailure != null) {
if (transportFailure != null) microphoneFailure.addSuppressed(transportFailure);
if (!clean) microphoneFailure.addSuppressed(new IOException("workers did not stop"));
throw microphoneFailure;
}
if (transportFailure != null) throw transportFailure;
if (!clean) throw new IOException("workers did not stop");
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ internal class CarLifeProjectionSession(private val context: Context, override v
override fun media(value: moe.feng.wheelplay.carlife.CarLifeMedia.Snapshot) {
main.post { if (!stopping.get()) media.update(value) }
}
override fun goToDesktop() {
main.post {
if (inputReady && projectionSessions.snapshot() === this@CarLifeProjectionSession) {
WebSession.requestSettings()
}
}
}
override fun waiting(reason: String) {
ready = false; touch.release()
main.post { if (!stopping.get()) { media.reset(); statusText = reason; projectionSessions.changed(this@CarLifeProjectionSession) } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@
import static org.junit.Assert.*;

public class CarLifeMicrophoneTest {
@Test public void stopAndCloseAllowInFlightCaptureToExit() throws Exception {
for (boolean close : new boolean[] { false, true }) {
CountDownLatch stopping = new CountDownLatch(1);
BlockingQueue<byte[]> packets = new LinkedBlockingQueue<>();
Thread[] captureThread = new Thread[1];
CarLifeMicrophone mic = new CarLifeMicrophone(new CarLifeMicrophone.Listener() {
public void started(CarLifeMicrophone.Capture capture) {
captureThread[0] = new Thread(() -> {
try {
stopping.await();
// Simulate PCM already past the audio source's active check.
capture.pcm(new byte[] { 1, 0 });
} catch (InterruptedException error) { Thread.currentThread().interrupt(); }
});
captureThread[0].start();
}
public void stopped(CarLifeMicrophone.Capture capture) {
stopping.countDown();
try { captureThread[0].join(1000); }
catch (InterruptedException error) { throw new AssertionError(error); }
assertFalse("Capture must exit while stopped() waits", captureThread[0].isAlive());
}
}, packets::add, s -> {}, s -> { throw new AssertionError(s); });
try {
mic.command(65570, Wire.EMPTY);
if (close) mic.close(); else mic.command(65571, Wire.EMPTY);
assertTrue("Revoked capture must not send PCM", packets.isEmpty());
} finally {
stopping.countDown(); mic.close(); mic.awaitStopped();
captureThread[0].join(1000);
}
}
}
@Test public void duplicateStartAndStopCloseCaptureExactlyOnce() throws Exception {
List<CarLifeMicrophone.Capture> starts=new ArrayList<>(),stops=new ArrayList<>();
BlockingQueue<byte[]> packets=new LinkedBlockingQueue<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,74 @@
import static org.junit.Assert.*;

public class CarLifeSessionTest {
@Test public void captureThreadCanRequestSessionStopWithoutWaitingForItself() throws Exception {
CountDownLatch requestStop = new CountDownLatch(1);
CarLifeSession[] owner = new CarLifeSession[1];
Thread capture = new Thread(() -> {
try { requestStop.await(); owner[0].stop(); }
catch (InterruptedException error) { Thread.currentThread().interrupt(); }
});
owner[0] = new CarLifeSession(new CarLifeSession.Listener() {
public void log(String message) {}
public void video(byte[] data, long time) {}
public void ended() {}
}, null, null, VideoSize.DEFAULT, CarLifeProtocolMode.LEGACY_3_0,
new CarLifeMicrophone.Listener() {
public void started(CarLifeMicrophone.Capture lease) { capture.start(); }
public void stopped(CarLifeMicrophone.Capture lease) {
try { capture.join(1000); }
catch (InterruptedException error) { throw new IllegalStateException(error); }
if (capture.isAlive()) throw new IllegalStateException("capture did not stop");
}
});
java.lang.reflect.Field field = CarLifeSession.class.getDeclaredField("microphone");
field.setAccessible(true);
try {
((CarLifeMicrophone) field.get(owner[0])).command(65570, Wire.EMPTY);
requestStop.countDown();
capture.join(2000);
assertFalse(capture.isAlive());
owner[0].stop().get(5, TimeUnit.SECONDS);
} finally {
requestStop.countDown();
owner[0].stop().get(5, TimeUnit.SECONDS);
capture.join(2000);
}
}
@Test public void microphoneStopFailureStillClosesTransportAndExecutors() throws Exception {
AtomicInteger transportCloses = new AtomicInteger(), ends = new AtomicInteger();
IllegalStateException failure = new IllegalStateException("capture cleanup failed");
CarLifeSession session = new CarLifeSession(new CarLifeSession.Listener() {
public void log(String message) {}
public void video(byte[] data, long time) {}
public void ended() { ends.incrementAndGet(); }
}, null, null, VideoSize.DEFAULT, CarLifeProtocolMode.LEGACY_3_0,
new CarLifeMicrophone.Listener() {
public void started(CarLifeMicrophone.Capture capture) {}
public void stopped(CarLifeMicrophone.Capture capture) { throw failure; }
}, new CarLifeTransport() {
public void connect() {}
public java.io.InputStream input(int port) { return new java.io.ByteArrayInputStream(Wire.EMPTY); }
public void write(int port, byte[] packet) {}
public void close() { transportCloses.incrementAndGet(); }
});
java.lang.reflect.Field micField = CarLifeSession.class.getDeclaredField("microphone");
micField.setAccessible(true);
CarLifeMicrophone mic = (CarLifeMicrophone) micField.get(session);
mic.command(65570, Wire.EMPTY);
CompletableFuture<Void> stopped = session.stop();
ExecutionException error = assertThrows(ExecutionException.class, () -> stopped.get(5, TimeUnit.SECONDS));
assertSame(failure, error.getCause());
assertSame(stopped, session.stop());
assertEquals(1, transportCloses.get());
assertEquals(1, ends.get());
for (String name : new String[] { "control", "readers" }) {
java.lang.reflect.Field field = CarLifeSession.class.getDeclaredField(name);
field.setAccessible(true);
assertTrue(((ExecutorService) field.get(session)).isTerminated());
}
mic.awaitStopped();
}
private CarLifeSession session(AtomicInteger ends, boolean throwOnEnd) {
return new CarLifeSession(new CarLifeSession.Listener() {
@Override public void log(String message) {}
Expand Down Expand Up @@ -60,6 +128,30 @@ public void ended() {}
assertEquals(1, ends.get());
} finally { callers.shutdownNow(); }
}
@Test public void desktopCommandOnlyNotifiesWhileStreaming() throws Exception {
java.lang.reflect.Method message = CarLifeSession.class.getDeclaredMethod("message", Wire.Packet.class);
message.setAccessible(true);
java.lang.reflect.Field streaming = CarLifeSession.class.getDeclaredField("streaming");
streaming.setAccessible(true);
Wire.Packet desktop = new Wire.Packet(0x10021, 0, Wire.EMPTY);
for (CarLifeProtocolMode mode : new CarLifeProtocolMode[] {
CarLifeProtocolMode.LEGACY_3_0, CarLifeProtocolMode.XIAOMI_4_1 }) {
AtomicInteger desktopRequests = new AtomicInteger();
CarLifeSession session = new CarLifeSession(new CarLifeSession.Listener() {
public void log(String message) {}
public void video(byte[] bytes, long ptsUs) {}
public void ended() {}
public void goToDesktop() { desktopRequests.incrementAndGet(); }
}, null, null, VideoSize.DEFAULT, mode);
try {
message.invoke(session, desktop);
assertEquals(0, desktopRequests.get());
streaming.setBoolean(session, true);
message.invoke(session, desktop);
assertEquals(1, desktopRequests.get());
} finally { session.stop().get(2, TimeUnit.SECONDS); }
}
}
@Test public void cleanupFailureCompletesFutureRatherThanHanging() throws Exception {
CarLifeSession session = session(new AtomicInteger(), true);
ExecutionException error = assertThrows(ExecutionException.class,
Expand Down
Loading