Fix(android): keep encoder operating rate at 30 to restore screen streaming
3ab5154 tied KEY_OPERATING_RATE to the tuned fps, so any profile below 30 (Good=20, Medium=15, Low=12, Minimal=8) pushed it under 30. Some hardware encoders, notably TV SoCs, then drop into a low-power mode that accepts configure/createInputSurface/start without error but never emits a single buffer. The controller receives BITMAPINFO and waits forever on the first decoded frame.
Confirmed on a Changhong Android TV: with operating rate 20 the log stopped right after SendBitmapInfo with no H.264 output at all; clamping to maxOf(fps, 30) produced a first IDR within the same second. KEY_OPERATING_RATE is a resource-planning hint, not a frame limiter - the actual rate is driven by KEY_FRAME_RATE and scene content.
Harden the capture lifecycle while here: fail loudly when createVirtualDisplay returns null instead of advertising a dead surface; downgrade the foreground type back to dataSync before stopping that projection, otherwise Android 14+ tears down a service still declaring mediaProjection and the controller loses the connection entirely; release the previous capture when MainActivity is reopened, including the projection-only state left behind when encoder creation throws before mediaCodec is assigned; ignore stale MediaProjection onStop callbacks that would otherwise tear down a fresh encoder; zero the native screen size on teardown; and make SendLoop's wait timeout so a silent zero-output encoder is reported instead of hanging.
This commit is contained in:
1 parent
db4543ad54
commit
7faca2c01b
2 files changed
+96
-4
No files matched your search
@@ -5,6 +5,7 @@
|
||||
#include "common/logger.h"
|
||||
#include "client/IOCPClient.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
@@ -113,7 +114,12 @@ public:
|
||||
memcpy(&buf[off + 16], &ss, sizeof(ss));
|
||||
|
||||
m_client->Send2Server((char*)buf.data(), total);
|
||||
LOGI_SH("SendBitmapInfo %dx%d clientID=%" PRIu64, m_width, m_height, clientID);
|
||||
// 档位和帧率一并打出来。Kotlin 侧真正喂给 MediaCodec 的 bitRateKbps / fps /
|
||||
// KEY_OPERATING_RATE 只进 logcat,主控端取不到;这里按 ScreenTuning 的规则
|
||||
// (fps = maxFps>0 ? maxFps : 档位表 maxFPS,再 clamp 到 1..60)就能反推,
|
||||
// 是判断「编码器为什么一帧不出」唯一能从服务端看到的线索。
|
||||
LOGI_SH("SendBitmapInfo %dx%d clientID=%" PRIu64 " maxFps=%d level=%d",
|
||||
m_width, m_height, clientID, ss.MaxFPS, ss.QualityLevel);
|
||||
|
||||
// H.264 是推送模式,不需要等 COMMAND_NEXT 才开始发帧。
|
||||
// 服务端在 auth 通过瞬间发 COMMAND_NEXT,此时 setManagerCallBack 尚未注册,
|
||||
@@ -267,11 +273,32 @@ private:
|
||||
std::thread m_sendThread;
|
||||
|
||||
void SendLoop() {
|
||||
// "一帧都没进来"是 Android 侧「请等待」最常见的成因:MediaProjection 被系统停掉、
|
||||
// createVirtualDisplay 返回 null、硬件编码器不接受当前尺寸,都会让 MediaCodec
|
||||
// 零输出。原先这里无限期阻塞在 wait 上,队列一直空就什么日志都不打,服务端只能
|
||||
// 干等。改成带超时的等待,把「没有帧」和「帧被当 P 帧丢弃」区分开。
|
||||
const auto startTick = std::chrono::steady_clock::now();
|
||||
bool noFrameWarned = false;
|
||||
while (m_running) {
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
m_cond.wait(lk, [&]{ return (!m_queue.empty() && m_started) || !m_running; });
|
||||
m_cond.wait_for(lk, std::chrono::seconds(1),
|
||||
[&]{ return (!m_queue.empty() && m_started) || !m_running; });
|
||||
if (!m_running) break;
|
||||
|
||||
if (m_queue.empty() || !m_started) {
|
||||
// 只在「一帧都没发出去过」时告警。发过首帧之后队列空是正常的:电视
|
||||
// 停在 launcher 这类静止画面时 SurfaceFlinger 不产生脏区,编码器就没
|
||||
// 输入,5 秒不打新帧属于预期行为,不能报成编码器故障。
|
||||
if (!noFrameWarned && m_started && !m_firstSent &&
|
||||
std::chrono::steady_clock::now() - startTick > std::chrono::seconds(5)) {
|
||||
noFrameWarned = true;
|
||||
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);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
Frame f = std::move(m_queue.front());
|
||||
m_queue.pop();
|
||||
lk.unlock();
|
||||
|
||||
Reference in new issue
Block a user