Skip to content

Commit

Permalink
Merge pull request #1 from seanavery/csi-poc
Browse files Browse the repository at this point in the history
CSI Module POC
  • Loading branch information
seanavery authored Jul 7, 2023
2 parents c628f42 + 8eb9991 commit b2bca01
Show file tree
Hide file tree
Showing 18 changed files with 932 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BasedOnStyle: Google
BinPackArguments: false
BinPackParameters: false
ColumnLimit: 100
Cpp11BracedListStyle: true
DerivePointerAlignment: false
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '<.*>'
Priority: 1
SortPriority: 0
- Regex: '^(<|"(boost|google|grpc)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentWidth: 4
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
SortIncludes: true
SpaceBeforeAssignmentOperators: true
Standard: Cpp11
UseTab: Never
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/.git
.DS_Store
build
etc/AppDir
bin
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build Docker Container

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Build Docker container
uses: docker/build-push-action@v4
with:
context: .
file: etc/Dockerfile.jetson
tags: viam-csi-build:0.0.1
build-args: |
TAG=35.3.1
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build
gen
bin
etc/AppDir
etc/appimage-build
*.AppImage*
*.tar*
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "viam-cpp-sdk"]
path = viam-cpp-sdk
url = [email protected]:viamrobotics/viam-cpp-sdk.git
47 changes: 47 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cmake_minimum_required(VERSION 3.25)

project(viam-csi
VERSION 0.0.1
DESCRIPTION "CSI camera modular resource"
)

set(CMAKE_CXX_STANDARD 14) # setting C++ 14 standard
find_package(PkgConfig REQUIRED) # finding pkg-config is a helper tool

# finding cpp sdk
set(CMAKE_PREFIX_PATH "/home/viam/viam-cpp-sdk/build/install" ${CMAKE_PREFIX_PATH})
find_package(viam-cpp-sdk CONFIG REQUIRED)

# using pkg-config to getting Gstreamer
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0 gstreamer-app-1.0 gstreamer-base-1.0)

# including GStreamer header files directory
include_directories(
${GLIB_INCLUDE_DIRS}
${GSTREAMER_INCLUDE_DIRS}
)

# linking GStreamer library directory
link_directories(
${GLIB_LIBRARY_DIRS}
${GSTREAMER_LIBRARY_DIRS}
)

# building target executable
add_executable(${PROJECT_NAME} main.cpp)

# linking Gstreamer library with target executable
target_link_libraries(${PROJECT_NAME}
${GSTREAMER_LIBRARIES}
viam-cpp-sdk::viamsdk
viam_rust_utils
)

# running clang-tidy linter
# find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
# if(CLANG_TIDY_EXE)
# set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
# endif()

# installing executable
install(TARGETS ${PROJECT_NAME} DESTINATION usr/local/bin)
98 changes: 98 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# CMake
BUILD_DIR := ./build
INSTALL_DIR := $(BUILD_DIR)/AppDir
BIN_DIR := ./bin

# Docker
IMAGE_NAME := viam-csi
IMAGE_TAG := 0.0.1
L4T_VERSION := 35.3.1

# Module
# Builds/installs module.
.PHONY: build
build:
rm -rf $(BUILD_DIR) | true && \
mkdir -p build && \
cd build && \
cmake -DCMAKE_INSTALL_PREFIX=$(INSTALL_DIR) .. -G Ninja && \
ninja -j $(shell nproc)

# Creates appimage cmake build.
package:
cd etc && \
appimage-builder --recipe viam-csi-module-aarch64.yml

# Builds docker image with viam-cpp-sdk and viam-csi installed.
image:
rm -rf build | true && \
docker build -t $(IMAGE_NAME):$(IMAGE_TAG) \
--target=cpp-sdk-builder \
--memory=16g \
--build-arg TAG=$(L4T_VERSION) \
-f ./etc/Dockerfile.jetson ./

# Runs docker image with shell.
docker-module:
docker run \
--device /dev/fuse \
--cap-add SYS_ADMIN \
-it $(IMAGE_NAME):$(IMAGE_TAG)

# Copies binary and AppImage from container to host.
bin-module:
rm -rf bin | true && \
mkdir -p bin && \
docker stop viam-csi-bin | true && \
docker rm viam-csi-bin | true && \
docker run -d -it --name viam-csi-bin $(IMAGE_NAME):$(IMAGE_TAG) && \
docker cp viam-csi-bin:/root/opt/src/viam-csi/etc/viam-csi-0.0.1-aarch64.AppImage ./bin && \
docker cp viam-csi-bin:/root/opt/src/viam-csi/build/viam-csi ./bin && \
docker stop viam-csi-bin

# SDK
.PHONY: build-sdk
build-sdk:
cd viam-cpp-sdk && \
mkdir -p build && \
cd build && \
cmake -DVIAMCPPSDK_USE_DYNAMIC_PROTOS=ON -DVIAMCPPSDK_OFFLINE_PROTO_GENERATION=ON .. -G Ninja && \
ninja -j $(shell nproc) && \
sudo ninja install -j $(shell nproc)) && \
cp -r ./install/* /usr/local/

docker-sdk:
docker build -t viam-cpp-sdk -f ./viam-cpp-sdk/etc/docker/Dockerfile.ubuntu.focal ./ && \
docker run -it viam-cpp-sdk /bin/bash

# Tests
# Tests out package in a fresh container.
docker-tests:
docker build \
-t viam-csi-tests \
--build-arg TAG=$(L4T_VERSION) \
-f ./etc/Dockerfile.test.jetson ./ && \
docker run \
--device /dev/fuse \
--cap-add SYS_ADMIN \
-it \
viam-csi-tests \
/bin/bash

docker-ci:
docker buildx build \
-f etc/Dockerfile.jetson \
--platform linux/arm64 \
-t viam-csi-ci:latest \
--build-arg TAG=35.3.1 \
./

# Utils
# Installs waveshare camera overrides.
waveshare:
mkdir -p gen && \
wget https://www.waveshare.com/w/upload/e/eb/Camera_overrides.tar.gz -O gen/Camera_overrides.tar.gz && \
tar -xvf gen/Camera_overrides.tar.gz -C gen && \
sudo cp gen/camera_overrides.isp /var/nvidia/nvcam/settings/ && \
sudo chmod 664 /var/nvidia/nvcam/settings/camera_overrides.isp && \
sudo chown root:root /var/nvidia/nvcam/settings/camera_overrides.isp
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# viam-csi
> csi camera viam module
### why?

Want to use CSI cameras with viam? This is the module for you. viam-csi is a [Viam Module](https://docs.viam.com/extend/modular-resources/) that includes a simple wrapper around [GStreamer](https://gstreamer.freedesktop.org/documentation/?gi-language=c) and an interface that satisfies a [Viam Camera Component](https://docs.viam.com/components/camera/webcam/). This means you can utilize the hardware accelerated GST plugins on your Jetson or Pi with the Viam ecosystem.

### usage

1. Download appimage from releases page.
```bash
sudo curl -o /usr/local/bin/viam-csi https://github.com/seanvery/viam-csi/archive/refs/tags/v0.0.01-viam-csi
sudo chmod a+rx /usr/local/bin/viam-csi
```

2. Run RDK with example config file [etc/app-config.json](https://github.com/seanavery/viam-csi/blob/master/etc/app-config.json).

### support
- [x] jetson
- [ ] pi
23 changes: 23 additions & 0 deletions constraints.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

// Module
#define DEFAULT_SOCKET_PATH "/tmp/viam.csi.sock"
#define RESOURCE_TYPE "CSICamera"
#define API_NAMESPACE "viam"
#define API_TYPE "camera"
#define API_SUBTYPE "csi"

// Camera
#define FAKE_CAMERA false
#define DEFAULT_INPUT_SOURCE "nvarguscamerasrc"
#define DEFAULT_INPUT_SENSOR "0"
#define DEFAULT_INPUT_FORMAT "video/x-raw(memory:NVMM)"
#define DEFAULT_INPUT_WIDTH 1920
#define DEFAULT_INPUT_HEIGHT 1080
#define DEFAULT_INPUT_FRAMERATE 30
#define DEFAULT_INPUT_FLIP_METHOD "0"
#define DEFAULT_OUTPUT_FORMAT "video/x-raw"
#define DEFAULT_OUTPUT_WIDTH 960
#define DEFAULT_OUTPUT_HEIGHT 540
#define DEFAULT_OUTPUT_ENCODER "nvjpegenc"
#define DEFAULT_OUTPUT_MIMETYPE "image/jpeg"
Loading

0 comments on commit b2bca01

Please sign in to comment.