Skip to content

Commit

Permalink
Support macOS camera access
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Dec 24, 2024
1 parent 9610fda commit e0a9cad
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 11 deletions.
32 changes: 29 additions & 3 deletions litecam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ elseif (UNIX AND NOT APPLE)
src/CameraPreviewLinux.cpp
)
elseif (APPLE)
# Ensure that Objective-C++ source files are compiled as Objective-C++
set(LIBRARY_SOURCES
src/CameraMacOS.mm
src/CameraPreviewMacOS.mm
)
set_source_files_properties(src/CameraMacOS.mm src/CameraPreviewMacOS.mm PROPERTIES COMPILE_FLAGS "-x objective-c++")

# Set main.cpp to be treated as Objective-C++
set_source_files_properties(src/main.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
endif()

# Define source files for the executable
Expand All @@ -50,9 +55,19 @@ if (UNIX AND NOT APPLE)
target_link_libraries(litecam PRIVATE ${X11_LIBRARIES} pthread)
endif()
elseif (APPLE)
find_library(COCOA_LIBRARY Cocoa)
find_library(AVFOUNDATION_LIBRARY AVFoundation)
target_link_libraries(litecam PRIVATE ${AVFOUNDATION_LIBRARY})
find_library(COCOA_LIBRARY Cocoa REQUIRED)
find_library(AVFOUNDATION_LIBRARY AVFoundation REQUIRED)
find_library(COREMEDIA_LIBRARY CoreMedia REQUIRED)
find_library(COREVIDEO_LIBRARY CoreVideo REQUIRED)
find_library(OBJC_LIBRARY objc REQUIRED) # Add the Objective-C runtime library

target_link_libraries(litecam PRIVATE
${COCOA_LIBRARY}
${AVFOUNDATION_LIBRARY}
${COREMEDIA_LIBRARY}
${COREVIDEO_LIBRARY}
${OBJC_LIBRARY} # Link the Objective-C runtime
)
elseif (WIN32)
target_link_libraries(litecam PRIVATE ole32 uuid mfplat mf mfreadwrite mfuuid)
endif()
Expand All @@ -65,3 +80,14 @@ target_link_libraries(camera_capture PRIVATE litecam)

# Include the shared library's headers in the executable
target_include_directories(camera_capture PRIVATE ${INCLUDE_DIR})

# For macOS, link against the Cocoa framework
if (APPLE)
target_link_libraries(camera_capture PRIVATE
${COCOA_LIBRARY}
${AVFOUNDATION_LIBRARY}
${COREMEDIA_LIBRARY}
${COREVIDEO_LIBRARY}
${OBJC_LIBRARY} # Link the Objective-C runtime
)
endif()
7 changes: 4 additions & 3 deletions litecam/dist/include/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ struct Buffer
void *start;
size_t length;
};

#elif __APPLE__
#include <AVFoundation/AVFoundation.h>
#endif

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -163,6 +160,9 @@ class CAMERA_API Camera
#elif __linux__
Camera() : fd(-1), frameWidth(640), frameHeight(480), buffers(nullptr), bufferCount(0) {}
~Camera() { Release(); }
#elif __APPLE__
Camera() noexcept; // Add noexcept to match the implementation
~Camera();
#endif

bool Open(int cameraIndex);
Expand Down Expand Up @@ -197,6 +197,7 @@ class CAMERA_API Camera

#ifdef __APPLE__
void *captureSession; // AVFoundation session object
void *videoOutput;
#endif
};

Expand Down
7 changes: 4 additions & 3 deletions litecam/include/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ struct Buffer
void *start;
size_t length;
};

#elif __APPLE__
#include <AVFoundation/AVFoundation.h>
#endif

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -163,6 +160,9 @@ class CAMERA_API Camera
#elif __linux__
Camera() : fd(-1), frameWidth(640), frameHeight(480), buffers(nullptr), bufferCount(0) {}
~Camera() { Release(); }
#elif __APPLE__
Camera() noexcept; // Add noexcept to match the implementation
~Camera();
#endif

bool Open(int cameraIndex);
Expand Down Expand Up @@ -197,6 +197,7 @@ class CAMERA_API Camera

#ifdef __APPLE__
void *captureSession; // AVFoundation session object
void *videoOutput;
#endif
};

Expand Down
Loading

0 comments on commit e0a9cad

Please sign in to comment.