emulatorCameraStarved function

Future<bool> emulatorCameraStarved(
  1. String adbPath,
  2. String serial, {
  3. Duration within = const Duration(seconds: 20),
})

True when the emulator behind serial keeps logging that its camera got no frame: at least three times in within, the last in the past 6 s. macOS has not let it use the Mac's camera; one or two at camera start are a warm-up.

Implementation

Future<bool> emulatorCameraStarved(String adbPath, String serial,
    {Duration within = const Duration(seconds: 20)}) async {
  try {
    final now = await runBounded(adbPath, ['-s', serial, 'shell', 'date', '+%s'],
        timeout: const Duration(seconds: 5), phase: 'adb date');
    final deviceNow = int.tryParse('${now.stdout}'.trim());
    if (deviceNow == null) return false;
    final log = await runBounded(adbPath, ['-s', serial, 'logcat', '-d', '-v', 'epoch', '-t', '400'],
        timeout: const Duration(seconds: 5), phase: 'adb logcat');
    final ages = [
      for (final line in '${log.stdout}'.split('\n'))
        if (line.contains('Unable to obtain video frame'))
          if (double.tryParse(line.trim().split(RegExp(r'\s+')).first) case final at?) deviceNow - at,
    ].where((age) => age <= within.inSeconds).toList();
    return ages.length >= 3 && ages.reduce((a, b) => a < b ? a : b) <= 6;
  } on Object {
    return false;
  }
}