From c9da3f7848f5a06cd43460687fb5121d3a929fca Mon Sep 17 00:00:00 2001 From: Robert Wu Date: Wed, 1 Feb 2023 21:47:38 +0000 Subject: [PATCH] USB MIDI: Fix incorrect timestamps Timestamp is calculated before waiting for USB data. When USB data comes in, the timestamp from when it starts to wait is used. This makes the timestamp very inaccurate. The fix is to move "timestamp = System.nanoTime()" after getting the USB response. Bug: 267519645 Test: Connect phone with MIDI Scope app to USB MIDI 2.0 keyboard Test: Play notes on the keyboard and verify timestamps look reasonable Change-Id: Ic3bccd89b65787badf3139964983572f83d99eaa --- .../usb/java/com/android/server/usb/UsbDirectMidiDevice.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/usb/java/com/android/server/usb/UsbDirectMidiDevice.java b/services/usb/java/com/android/server/usb/UsbDirectMidiDevice.java index 394d6e774aa13..0dcf8ce961925 100644 --- a/services/usb/java/com/android/server/usb/UsbDirectMidiDevice.java +++ b/services/usb/java/com/android/server/usb/UsbDirectMidiDevice.java @@ -379,8 +379,6 @@ public final class UsbDirectMidiDevice implements Closeable { Log.w(TAG, "input thread interrupted"); break; } - // Record time of event immediately after waking. - long timestamp = System.nanoTime(); final ByteBuffer byteBuffer = ByteBuffer.wrap(inputBuffer); if (!request.queue(byteBuffer)) { Log.w(TAG, "Cannot queue request"); @@ -396,6 +394,9 @@ public final class UsbDirectMidiDevice implements Closeable { continue; } + // Record time of event after receiving response. + long timestamp = System.nanoTime(); + int bytesRead = byteBuffer.position(); if (bytesRead > 0) {