相关的类 VideoCaptureModule::DeviceInfo, VideoCaptureDataCallback
VideoCaptureModule类用于操作本地摄像头,包含了枚举,打开,关闭,设置分辨率等等的操作,通过这个类,结合VideoRender 视频渲染类
我们便很容易创建一个带有预览框的摄像头捕获工具,再结合 VideoSendStream 视频流发送类 就可以创建一个整套采集,编码,预览的模型。
- 创建
视频捕获模块通过VideoCaptureFactory这个工厂类的Create函数生产,id代表和流对应的视频设备(webrtc中一个id代表着从采集,编码,传输整个链路),deviceUniqueIdUTF8代表要打开设备的名字,名字可以通过VideoCaptureModule::DeviceInfo来获取 。
class VideoCaptureFactory {
public:
// Create a video capture module object
// id - unique identifier of this video capture module object.
// deviceUniqueIdUTF8 - name of the device.
// Available names can be found by using GetDeviceName
static VideoCaptureModule* Create(const int32_t id,
const char* deviceUniqueIdUTF8);
// Create a video capture module object used for external capture.
// id - unique identifier of this video capture module object
// externalCapture - [out] interface to call when a new frame is captured.
static VideoCaptureModule* Create(const int32_t id,
VideoCaptureExternal*& externalCapture);
static VideoCaptureModule::DeviceInfo* CreateDeviceInfo(
const int32_t id);
private:
~VideoCaptureFactory();
};
- 注册回调
通过注册一个继承自VideoCaptureDataCallback的对象,视频采集的数据回在VideoCaptureDataCallback::OnIncomingCapturedFrame回调中返回
//注册回调
virtual void RegisterCaptureDataCallback(
VideoCaptureDataCallback& dataCallback);
- 打开
// Start capture device
virtual int32_t StartCapture(
const VideoCaptureCapability& capability) = 0;
- 关闭
virtual int32_t StopCapture() = 0;
- 输出
通过第二步注册好的回调类,每当有视频数据的时候都会再OnIncomingCapturedFrame中返回
//回调数据返回
class VideoCaptureDataCallback
{
public:
virtual void OnIncomingCapturedFrame(const int32_t id,
const VideoFrame& videoFrame) = 0;
virtual void OnCaptureDelayChanged(const int32_t id,
const int32_t delay) = 0;
protected:
virtual ~VideoCaptureDataCallback(){}
};
VideoCaptureModule::DeviceInfo视频设备设备信息类,本类为VideoCaptureModule的一个子类,主要提供摄像头枚举,分辨率采集,摄像头方向选抓等等的操作==
- 创建
通过VideoCaptureFactory工厂类的CreateDeviceInfo方法创建设备信息类
class VideoCaptureFactory {
public:
//...
static VideoCaptureModule::DeviceInfo* CreateDeviceInfo(
const int32_t id);
private:
~VideoCaptureFactory();
};
- 操作
// Interface for receiving information about available camera devices.
class DeviceInfo {
public:
virtual uint32_t NumberOfDevices() = 0;
// Returns the available capture devices.
// deviceNumber - Index of capture device.
// deviceNameUTF8 - Friendly name of the capture device.
// deviceUniqueIdUTF8 - Unique name of the capture device if it exist.
// Otherwise same as deviceNameUTF8.
// productUniqueIdUTF8 - Unique product id if it exist.
// Null terminated otherwise.
virtual int32_t GetDeviceName(
uint32_t deviceNumber,
char* deviceNameUTF8,
uint32_t deviceNameLength,
char* deviceUniqueIdUTF8,
uint32_t deviceUniqueIdUTF8Length,
char* productUniqueIdUTF8 = 0,
uint32_t productUniqueIdUTF8Length = 0) = 0;
// Returns the number of capabilities this device.
virtual int32_t NumberOfCapabilities(
const char* deviceUniqueIdUTF8) = 0;
// Gets the capabilities of the named device.
virtual int32_t GetCapability(
const char* deviceUniqueIdUTF8,
const uint32_t deviceCapabilityNumber,
VideoCaptureCapability& capability) = 0;
// Gets clockwise angle the captured frames should be rotated in order
// to be displayed correctly on a normally rotated display.
virtual int32_t GetOrientation(const char* deviceUniqueIdUTF8,
VideoRotation& orientation) = 0;
// Gets the capability that best matches the requested width, height and
// frame rate.
// Returns the deviceCapabilityNumber on success.
virtual int32_t GetBestMatchedCapability(
const char* deviceUniqueIdUTF8,
const VideoCaptureCapability& requested,
VideoCaptureCapability& resulting) = 0;
// Display OS /capture device specific settings dialog
virtual int32_t DisplayCaptureSettingsDialogBox(
const char* deviceUniqueIdUTF8,
const char* dialogTitleUTF8,
void* parentWindow,
uint32_t positionX,
uint32_t positionY) = 0;
virtual ~DeviceInfo() {}
};
特别注意GetBestMatchedCapability这个函数,这个函数比较重要也比较有用,因为当我们要采集想想头图像的时候需要传入一个指定的分辨率,正常地这个分辨率正好是摄像头支持的分辨率就没有问题,但是有一些摄像头可能没有支持这个分辨率,这时候通过这个函数,系统回返回一个最适合的分辨率,这样保证我们程序不会出现问题。常见的用法是在设置分辨率之前再调用一次这个函数。
具体的实现方法可以参考 webrtc/modules/video_capture/device_info_impl.cc
总结
视频采集类的核心就是 VideoCaptureModule及其子类。操作这些类的核心方法通过VideoCaptureFactory这个工厂类提供,因为其中的方法都是静态方法,我们只需要调用webrtc::VideoCaptureFactory::Create()即可。初学者比较难以看懂的主要是webrtc使用的一些设计模式,一旦理解了这些设计模式及这些设计模式要解决的问题。读懂webrtc其实并不难。
欢迎访问本人的小站 xsnip.cn