Add video file playback support for Windows and macOS #40
+1,926
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Extends
ccap::Providerto open video files with the same API used for camera capture, matching OpenCV'scv::VideoCapturepattern.API Additions
PropertyName::Duration,CurrentTime,PlaybackSpeed,FrameCount,CurrentFrameIndexfor file playback controlErrorCode::FileOpenFailed,UnsupportedVideoFormat,SeekFailedProvider::isFileMode()/ccap_provider_is_file_mode()to detect modePath Detection
looksLikeFilePath()checks for path separators or video extensions (.mp4,.mov,.avi, etc.)Platform Implementations
AVAssetReaderwith NV12/BGRA32 output, frame rate control, seek supportIMFSourceReaderwith NV12/RGB32 output, frame rate control, seek supportUsage
Examples
examples/desktop/5-play_video.cpp(C++)examples/desktop/5-play_video_c.c(C)Original prompt
📋 任务:为 CameraCapture 添加视频文件播放支持
目标
扩展
ccap::Provider类,使其能够像 OpenCVcv::VideoCapture一样,用统一的接口既打开摄像头,也打开视频文件。支持平台:Windows 和 macOS(不实现 Linux)
核心设计
统一接口(与 OpenCV cv::VideoCapture 相同)
路径识别
/或\→ 文件.mp4.mov.avi等结尾 → 文件主要改动
1. API 扩展(参考 cv::VideoCapture 设计)
在
include/ccap_def.h的PropertyName枚举中添加文件播放属性:在
include/ccap_core.h的Provider类中仅添加一个判断方法:使用方式(与 OpenCV 一致):
对应的 C 接口仅需在
include/ccap_c.h添加:2. 内部架构
修改
src/ccap_imp.h:实现方式:
open()方法中内部判断是设备还是文件(根据路径特征)open()接口,完全透明3. macOS 实现
新增文件:
src/ccap_file_reader_apple.h- 文件读取器接口src/ccap_file_reader_apple.mm- 使用 AVAssetReader 实现修改文件:
src/ccap_imp_apple.h- 添加CcapFileReaderApple* m_fileReader成员src/ccap_imp_apple.mm- 在open()方法中判断并处理文件/设备实现逻辑:
技术栈:
AVAsset→AVAssetReader→CMSampleBuffer→ccap::VideoFrame4. Windows 实现
新增文件:
src/ccap_file_reader_windows.h- 文件读取器接口src/ccap_file_reader_windows.cpp- 使用 Media Foundation 实现修改文件:
src/ccap_imp_windows.h- 添加WindowsFileReader* m_fileReader成员src/ccap_imp_windows.cpp- 在open()方法中判断并处理文件/设备实现逻辑:
技术栈:
IMFSourceReader→IMFSample→IMFMediaBuffer→ccap::VideoFrame5. 帧时序控制
文件播放需要控制帧率(不同于摄像头的实时采集):
grab()方法中实现时序控制6. 示例和测试
新增示例:
examples/desktop/5-play_video.cpp- C++ 示例examples/desktop/5-play_video_c.c- C 示例新增测试:
tests/test_file_playback.cpp- 基础播放测试tests/test_file_seek.cpp- Seek 功能测试tests/test_unified_api.cpp- 统一接口测试7. 错误处理
在
include/ccap_def.h的ErrorCode枚举中添加:关键实现要求
reportError()和全局ErrorCallback成功标准
✅
provider.open("video.mp4")成功打开文件✅
provider.grab()返回视频帧,API 与摄像头完全一致✅ 使用 `...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.