Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit afeec0a
Showing
7 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* text=auto | ||
*.swift text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.build | ||
/Packages | ||
/*.xcodeproj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// swift-tools-version:3.1 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "IsCameraOn" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Foundation | ||
import AVFoundation | ||
import CoreMediaIO | ||
|
||
private func getCameraProp() -> CMIOObjectID? { | ||
var opa = CMIOObjectPropertyAddress( | ||
mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyDevices), | ||
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal), | ||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster) | ||
) | ||
|
||
var (dataSize, dataUsed) = (UInt32(0), UInt32(0)) | ||
var result = CMIOObjectGetPropertyDataSize(CMIOObjectID(kCMIOObjectSystemObject), &opa, 0, nil, &dataSize) | ||
var devices: UnsafeMutableRawPointer? = nil | ||
|
||
repeat { | ||
if devices != nil { | ||
free(devices) | ||
devices = nil | ||
} | ||
devices = malloc(Int(dataSize)) | ||
result = CMIOObjectGetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &opa, 0, nil, dataSize, &dataUsed, devices); | ||
} while result == OSStatus(kCMIOHardwareBadPropertySizeError) | ||
|
||
var cameraId: CMIOObjectID? = nil | ||
|
||
if let devices = devices { | ||
for offset in stride(from: 0, to: dataSize, by: MemoryLayout<CMIOObjectID>.size) { | ||
let current = devices.advanced(by: Int(offset)).assumingMemoryBound(to: CMIOObjectID.self) | ||
cameraId = current.pointee | ||
} | ||
} | ||
|
||
free(devices) | ||
|
||
return cameraId | ||
} | ||
|
||
public func isCameraOn() -> Bool { | ||
guard let camera = getCameraProp() else { | ||
return false | ||
} | ||
|
||
var opa = CMIOObjectPropertyAddress( | ||
mSelector: CMIOObjectPropertySelector(kCMIODevicePropertyDeviceIsRunningSomewhere), | ||
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeWildcard), | ||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementWildcard) | ||
) | ||
|
||
var isUsed: Bool? = nil | ||
|
||
var (dataSize, dataUsed) = (UInt32(0), UInt32(0)) | ||
var result = CMIOObjectGetPropertyDataSize(camera, &opa, 0, nil, &dataSize) | ||
if result == OSStatus(kCMIOHardwareNoError) { | ||
if let data = malloc(Int(dataSize)) { | ||
result = CMIOObjectGetPropertyData(camera, &opa, 0, nil, dataSize, &dataUsed, data) | ||
let on = data.assumingMemoryBound(to: UInt8.self) | ||
isUsed = on.pointee != 0 | ||
} | ||
} | ||
|
||
return isUsed ?? false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# is-camera-on | ||
|
||
> Check if the built-in Mac camera is on | ||
The camera is commonly known as FaceTime HD or iSight. | ||
|
||
This module can be useful to check if the camera is already in use or notify you if it's turned on when you didn't intend it to be. | ||
|
||
|
||
## Requirements | ||
|
||
- Xcode 8+ | ||
- macOS 10.11+ | ||
|
||
|
||
## Install | ||
|
||
With [SPM](https://swift.org/package-manager/): | ||
|
||
```swift | ||
.Package(url: "https://github.com/sindresorhus/is-camera-on", majorVersion: 1) | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
```swift | ||
import IsCameraOn | ||
|
||
print(isCameraOn()) | ||
//=> true | ||
``` | ||
|
||
|
||
## Related | ||
|
||
- [node-is-camera-on](https://github.com/sindresorhus/node-is-camera-on) - Node.js wrapper for this module | ||
- [is-camera-on-cli](https://github.com/sindresorhus/is-camera-on-cli) - CLI for this module | ||
|
||
|
||
## License | ||
|
||
MIT © [Sindre Sorhus](https://sindresorhus.com) |