Fix(android): correct lock/input state and recover screen capture
db148c4 asked KeyguardManager on every heartbeat; some TVs answer "locked"
while playing video, so hosts showed "Locked: HH:MM:SS" forever and the
foreground app never reached the server. Lock state is event-driven again:
SCREEN_OFF records the start, USER_PRESENT clears it, and on SCREEN_ON the
state clears when the device is TV-like or has no keyguard (devices without
a lock screen never send USER_PRESENT); a phone that reconnects while
locked is re-calibrated the same way. Keyguard is consulted only at those
event points, never per heartbeat.
Input strategy and TV heuristics are separate immutable values: isTV (D-pad
focus navigation vs touch injection) only looks at leanback, isTvLike (lock
and encoder heuristics) also accepts television/uiMode. They were once
merged and the TV lost remote control, then an assignment was dropped and
it silently defaulted to false; immutable lazy values make both impossible.
The resolved mode is logged as "input=dpad|touch tvLike=..." and mirrored
into the native ring when available.
Screen capture: on TVs the default encoder configuration is tried first.
After a sleep the existing VirtualDisplay stops composing while the
projection stays alive, so a 5s zero-frame stall rebuilds the encoder and
VirtualDisplay on the same projection (reusing the session's encoded size),
requests a fresh IDR and reports failure only after 13s; if the projection
is gone the native size is cleared so the next request re-authorizes. A
repeated onStartCommand no longer calls nativeInit again (the -1 answer
used to stopSelf and drop the host). Kotlin capture logs mirror into the
native ring and fall back to logcat.
Debug APK builds; device verification pending.
Co-Authored-By: deepseek-flash
This commit was merged in pull request #8.
This commit is contained in:
1 parent
be19744898
commit
340fb20518
6 files changed
+392
-74
No files matched your search
@@ -22,6 +22,11 @@
|
||||
#define LOGE_SH(...) android_log_tag(ANDROID_LOG_ERROR, "YAMA_SCR", __VA_ARGS__)
|
||||
|
||||
extern uint64_t g_myClientID;
|
||||
// 定义在 main.cpp:把消息发进主控消息栏(TOKEN_CLIENT_MSG)。
|
||||
// 远程桌面编码器零输出时,主控端停在「请等待」,必须主动回一条说明。
|
||||
extern void SendClientMessageToMain(const char* title, const char* text);
|
||||
// 定义在 main.cpp:编码器零输出时让 Kotlin 用同一个 MediaProjection 重建采集管线。
|
||||
extern void NotifyEncoderStalled();
|
||||
// 定义在 main.cpp,供子连接的 OnReceive 调用
|
||||
extern void DispatchControlEvent(uint32_t msgVal, uint64_t wParam, int32_t ptX, int32_t ptY);
|
||||
extern void DispatchClipboardText(const uint8_t* data, size_t size);
|
||||
@@ -279,6 +284,7 @@ private:
|
||||
// 干等。改成带超时的等待,把「没有帧」和「帧被当 P 帧丢弃」区分开。
|
||||
const auto startTick = std::chrono::steady_clock::now();
|
||||
bool noFrameWarned = false;
|
||||
bool stallNotified = false;
|
||||
while (m_running) {
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
m_cond.wait_for(lk, std::chrono::seconds(1),
|
||||
@@ -289,12 +295,37 @@ private:
|
||||
// 只在「一帧都没发出去过」时告警。发过首帧之后队列空是正常的:电视
|
||||
// 停在 launcher 这类静止画面时 SurfaceFlinger 不产生脏区,编码器就没
|
||||
// 输入,5 秒不打新帧属于预期行为,不能报成编码器故障。
|
||||
const auto elapsed = std::chrono::steady_clock::now() - startTick;
|
||||
if (!noFrameWarned && m_started && !m_firstSent &&
|
||||
std::chrono::steady_clock::now() - startTick > std::chrono::seconds(5)) {
|
||||
elapsed > std::chrono::seconds(5)) {
|
||||
noFrameWarned = true;
|
||||
// 发消息/重建都是慢操作,不能拿着 m_mutex:生产者(编码回调)推帧要拿它。
|
||||
lk.unlock();
|
||||
LOGE_SH("SendLoop: no frame from MediaCodec after 5s — encoder is not producing "
|
||||
"(MediaProjection stopped, VirtualDisplay null, or unsupported %dx%d)",
|
||||
m_width, m_height);
|
||||
// 电视睡眠后常见:投影还在,但旧的 VirtualDisplay 收不到合成帧。先用同一个
|
||||
// MediaProjection 重建采集管线(不需要重新授权);重建结果由 Kotlin 侧记录。
|
||||
NotifyEncoderStalled();
|
||||
} else if (noFrameWarned && !stallNotified && m_started && !m_firstSent &&
|
||||
elapsed > std::chrono::seconds(13)) {
|
||||
stallNotified = true;
|
||||
// 自愈也没救回来:主控端停在「请等待……」,明确回一条消息说明原因。
|
||||
// 多个观看者各自 13 秒都会走到这里,进程内 60 秒只发一条,避免刷屏。
|
||||
lk.unlock();
|
||||
static std::atomic<long long> lastNotifyMs{0};
|
||||
const long long nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||
long long prevMs = lastNotifyMs.load();
|
||||
// 多个观看者各自 13 秒都会走到这里;进程内 60 秒只发一条,避免刷屏。
|
||||
// prevMs == 0 表示本次开机还没发过(steady_clock 的 0 点是开机)。
|
||||
if ((prevMs == 0 || nowMs - prevMs > 60 * 1000) &&
|
||||
lastNotifyMs.compare_exchange_strong(prevMs, nowMs)) {
|
||||
SendClientMessageToMain(
|
||||
"Remote desktop",
|
||||
"The device screen encoder produced no frame (hardware encoder may reject "
|
||||
"this size or config). Remote desktop cannot start.");
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user