spice-streaming-agent启动分析笔记
·
1、解析参数,加载插件,开始命令读取和发送帧数据,都是在主线程中
int main(int argc, char* argv[])
{
const char *stream_port_name = "/dev/virtio-ports/org.spice-space.stream.0";
int opt;
const char *log_filename = NULL;
bool log_binary = false;
bool log_frames = false;
const char *pluginsdir = PLUGINSDIR;
enum {
OPT_first = UCHAR_MAX,
OPT_PLUGINS_DIR,
OPT_LOG_BINARY,
OPT_LOG_CATEGORIES,
};
static const struct option long_options[] = {
{ "plugins-dir", required_argument, NULL, OPT_PLUGINS_DIR},
{ "log-binary", no_argument, NULL, OPT_LOG_BINARY},
{ "log-categories", required_argument, NULL, OPT_LOG_CATEGORIES},
{ "help", no_argument, NULL, 'h'},
{ 0, 0, 0, 0}
};
std::vector<std::string> old_args(argv, argv+argc);
openlog("spice-streaming-agent", isatty(fileno(stderr)) ? (LOG_PERROR|LOG_PID) : LOG_PID, LOG_USER);
setlogmask(LOG_UPTO(LOG_NOTICE));
std::vector<ConcreteConfigureOption> options;
while ((opt = getopt_long(argc, argv, "hp:c:l:d", long_options, NULL)) != -1) {
switch (opt) {
case 0:
/* Handle long options if needed */
break;
case OPT_PLUGINS_DIR:
pluginsdir = optarg;
break;
case 'p':
stream_port_name = optarg;
break;
case 'c': {
char *p = strchr(optarg, '=');
if (p == NULL) {
syslog(LOG_ERR, "Invalid '-c' argument value: %s", optarg);
usage(argv[0]);
}
*p++ = '\0';
options.push_back(ConcreteConfigureOption(optarg, p));
break;
}
case OPT_LOG_BINARY:
log_binary = true;
break;
case OPT_LOG_CATEGORIES:
for (const char *tok = strtok(optarg, ":"); tok; tok = strtok(nullptr, ":")) {
std::string cat = tok;
if (cat == "frames") {
log_frames = true;
}
// ignore not existing, compatibility for future
}
break;
case 'l':
log_filename = optarg;
break;
case 'd':
setlogmask(LOG_UPTO(LOG_DEBUG));
break;
case 'h':
usage(argv[0]);
break;
}
}
register_interrupts();
try {
FrameLog frame_log(log_filename, log_binary, log_frames);
ConcreteAgent agent(options, &frame_log);
// 注册mjpeg插件,内部自带插件
MjpegPlugin::Register(&agent);
// 加载插件目录
agent.LoadPlugins(pluginsdir);
for (const std::string& arg: old_args) {
frame_log.log_stat("Args: %s", arg.c_str());
}
old_args.clear();
// 串口通信句柄
StreamPort stream_port(stream_port_name);
// 光标更新线程
CursorUpdater running_cursor_thread(&stream_port); // can live only while stream_port is alive !
// 抓取图像
do_capture(stream_port, frame_log, agent);
}
catch (std::exception &err) {
syslog(LOG_ERR, "%s", err.what());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
2、抓图函数
static void
do_capture(StreamPort &stream_port, FrameLog &frame_log, ConcreteAgent &agent)
{
unsigned int frame_count = 0;
// 是否退出
while (!quit_requested) {
// 是否开启了流,否则读取指令
while (!quit_requested && !streaming_requested) {
read_command(stream_port, true);
}
// 退出
if (quit_requested) {
return;
}
syslog(LOG_INFO, "streaming starts now");
uint64_t time_last = 0;
// 通过具体的agent获取具体抓屏插件的抓屏函数
std::unique_ptr<FrameCapture> capture(agent.GetBestFrameCapture(client_codecs));
if (!capture) {
throw std::runtime_error("cannot find a suitable capture system");
}
// 显示信息
std::vector<DeviceDisplayInfo> display_info;
try {
display_info = capture->get_device_display_info();
} catch (const Error &e) {
syslog(LOG_ERR, "Error while getting device display info: %s", e.what());
}
syslog(LOG_DEBUG, "Got device info of %zu devices from the plugin", display_info.size());
for (const auto &info : display_info) {
syslog(LOG_DEBUG, " stream id %u: device address: %s, device display id: %u",
info.stream_id,
info.device_address.c_str(),
info.device_display_id);
}
// 获取到了显示信息发送,否则提示
if (display_info.size() > 0) {
if (display_info.size() > 1) {
syslog(LOG_WARNING, "Warning: the Frame Capture plugin returned device display "
"info for more than one display device, but we currently only support "
"a single device. Sending information for first device to the server.");
}
stream_port.send<DeviceDisplayInfoMessage>(display_info[0]);
} else {
syslog(LOG_ERR, "Empty device display info from the plugin");
}
// 是否接收到了流请求
while (!quit_requested && streaming_requested) {
// 每100帧输出日志
if (++frame_count % 100 == 0) {
syslog(LOG_DEBUG, "SENT %d frames", frame_count);
}
// 获取时间
uint64_t time_before = FrameLog::get_time();
frame_log.log_stat("Capturing frame...");
// 抓图
FrameInfo frame = capture->CaptureFrame();
frame_log.log_stat("Captured frame");
// 记录时间
uint64_t time_after = FrameLog::get_time();
syslog(LOG_DEBUG,
"got a frame -- size is %zu (%" PRIu64 " ms) "
"(%" PRIu64 " ms from last frame)(%" PRIu64 " us)\n",
frame.buffer_size, (time_after - time_before)/1000,
(time_after - time_last)/1000,
(time_before - time_last));
time_last = time_after;
// 流开始发送流编码,宽高给客户端
if (frame.stream_start) {
unsigned width, height;
unsigned char codec;
width = frame.size.width;
height = frame.size.height;
codec = capture->VideoCodecType();
syslog(LOG_DEBUG, "wXh %uX%u codec=%u", width, height, codec);
frame_log.log_stat("Started new stream wXh %uX%u codec=%u", width, height, codec);
stream_port.send<FormatMessage>(width, height, codec);
}
// 发送帧数据
frame_log.log_stat("Frame of %zu bytes", frame.buffer_size);
frame_log.log_frame(frame.buffer, frame.buffer_size);
try {
stream_port.send<FrameMessage>(frame.buffer, frame.buffer_size);
} catch (const WriteError& e) {
utils::syslog(e);
break;
}
frame_log.log_stat("Sent frame");
// 读取命令
read_command(stream_port, false);
}
}
}
更多推荐



所有评论(0)