-
Notifications
You must be signed in to change notification settings - Fork 364
/
AVDeviceHotplug.mm
55 lines (49 loc) · 1.49 KB
/
AVDeviceHotplug.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "AVDeviceHotplug.h"
#include <AVFoundation/AVFoundation.h>
//#include <QCameraInfo>
//#include <QDebug>
AVDeviceHotplug::AVDeviceHotplug(QObject *parent)
: QObject(parent)
{
}
AVDeviceHotplug::~AVDeviceHotplug()
{
free();
}
AVDeviceHotplug *AVDeviceHotplug::getInstance()
{
static AVDeviceHotplug instance;
return &instance;
}
void AVDeviceHotplug::init()
{
// 注册之前必须先调用AVCaptureDevice
[AVCaptureDevice devices];
// 摄像头可用事件
attachHandle = [[NSNotificationCenter defaultCenter]
addObserverForName:AVCaptureDeviceWasConnectedNotification
object:nil
queue:nil
usingBlock:^(NSNotification *){
//qDebug()<<QCameraInfo::availableCameras().size();
emit deviceAttached();
}];
// 摄像头不可用
detachHandle = [[NSNotificationCenter defaultCenter]
addObserverForName:AVCaptureDeviceWasDisconnectedNotification
object:nil
queue:nil
usingBlock:^(NSNotification *){
//qDebug()<<QCameraInfo::availableCameras().size();
emit deviceDetached();
}];
}
void AVDeviceHotplug::free()
{
if (attachHandle) {
[[NSNotificationCenter defaultCenter] removeObserver:attachHandle];
}
if (detachHandle) {
[[NSNotificationCenter defaultCenter] removeObserver:detachHandle];
}
}