如何优化我的截屏实用程序?
我正在用 C++ 开发一个截屏实用程序。
它主要捕获桌面帧并创建 AVI 文件。算法如下:
- 创建线程:
this->m_hThread=CreateThread(NULL,0,thScreenCapture,this,0,NULL);
在
thScreenCapture中捕捉桌面 每秒 n 次(例如 5 fps)。
obj->Capture();
在Capture()中,将位图数据附加到avi文件中。
this->appendBitmapToAvi(this->avifile, bmp);
此实用程序还可以录制声音。因此,在 thScreenCapture 方法中,声音数据也被附加到 avi 文件中。
问题是,当每秒捕获超过 6 帧(这可能会根据硬件配置而变化)时,帧和声音之间会出现延迟。
我正在寻找优化算法的解决方案。解决方案可能是在内存中缓冲帧,而不是即时将所有帧附加到 avi 文件中。但这使得代码变得更加复杂,因为我必须处理在不同线程中捕获的声音数据。
您建议如何在不失去同步的情况下增加该实用程序支持的 fps 值?
I'm developing a screencasting utility in C++.
It basically captures desktop frames and creates an AVI file. The algorithm is as follows:
- Create a thread:
this->m_hThread=CreateThread(NULL,0,thScreenCapture,this,0,NULL);
Capture desktop in
thScreenCapture
n times per second (like 5 fps).obj->Capture();
In Capture(), append the bitmap data to the avi file.
this->appendBitmapToAvi(this->avifile, bmp);
This utility also records sound. So, in method thScreenCapture, sound data is also being appended to the avi file.
The problem is that a lag occurs between frames and sound when more than 6 frames (this can change depending on hardware configuration) are captured per second.
I'm seeking for a solution to optimize the algorithm. A solution may be buffering frames in memory, not appending all of them to the avi file on-the-fly. But this makes the code more complex because I have to deal with the sound data that's being captured in a different thread.
What do you suggest to increase the fps value that this utility supports without losing synchronization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AVI 文件是你自己写的吗?这是一项崇高的努力,但有 API 可以帮助完成这项任务。
如果您在 Windows 平台上工作,我建议考虑使用 DirectShow 或 < a href="http://en.wikipedia.org/wiki/Media_Foundation" rel="nofollow noreferrer">媒体基础 API,用于将音频和视频复用到磁盘。 DirectShow 是 Windows 平台上用于 A/V 捕获、流式传输和混合的 API。
CodeProject 上的这篇文章 讨论了音频和渲染技术。视频同步问题以及 DirectShow 用于克服此困难的机制。
本质上,采用参考时钟并对帧加时间戳。
有一个非常活跃的 DirectShow 社区,对于新人来说这是非常有用的资源各位。 TMH 网站非常值得一看 - 他是 MS MVP,也是社区的活跃成员。
我希望这有帮助!
Are you writing the AVI file yourself? A noble effort, but there are APIs to help with this task.
If you're working on the windows platform, I'd suggest considering using the DirectShow or Media Foundation APIs to mux the audio and video to disk. DirectShow is the API for A/V capture, streaming and muxing on the windows platform.
This article on CodeProject talks about audio & video sync issues and the mechanism that DirectShow uses to overcome this difficulty.
Essentially, a reference clock is employed and the frames are timestamped.
There's a very active DirectShow community that is an extremely useful resource for new folks. TMH's website is well worth checking out - he's an MS MVP and is an active member of the community.
I hope this helps!
您可以查看其他截屏软件的来源,例如CamStudio,看看他们是如何做到的。
如果您的程序是磁盘绑定的(我怀疑是这样),那么通过使用压缩可能会改善情况(这就是 Camtasia Studio 等大牌程序的运行方式)
You could take a look at the source for other screencasting software, such as CamStudio, to see how they are doing it.
If your program is disk bound (and I suspect it is), then things might improve by using compression (this is how the big name programs, such as Camtasia Studio, operate)
使用循环双缓冲区或三缓冲区来存储每帧的位图和声音,并使用单独的线程将位图和声音添加到 avi。因此,数据收集位于一个线程中,数据位于循环(线程安全)缓冲区中,数据存储位于另一线程中。
Use a circular double or triple buffer to store the bitmap and sound each frame and use a separate thread to add the bitmap and sound to the avi. So data-collection is in one thread, data is in a circular (thread-safe) buffer and the data-storage is in another thread.
您的目标操作系统是什么?如果您使用的是 Windows XP,我会在 http://tmhare 上查看一些 DirectShow 代码.mvps.org/downloads.htm,特别是过滤器图形库。
What OS are you targeting? If tyou are working on Windows XP I would take a look at some of the DirectShow code at http://tmhare.mvps.org/downloads.htm, specifically Filter Graph Library.