Skip to content

Commit

Permalink
Updated libcamera (#67)
Browse files Browse the repository at this point in the history
* Pointers are hard

* Two errors:

- Neopixelpath should use _getValue
- Cast output to NdiOutput* before trying to access isPreview and
  isProgram

* Casting issue

* .

* Moved neopixel code

* .

* .

* Added artifacts to build

* More neopixel testing

* Workflow

* Fixed wrong characters

* .

* .

* .

* .

* .

* Well, that's broken then

* Reverted to 3.0.2

* Updated libcamera-apps headers

* Removed argv & argc, fixed raspindi.conf.default

* Updated libcamera
  • Loading branch information
rf152 authored Oct 4, 2023
1 parent 6f55df3 commit 25321ed
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 22 deletions.
21 changes: 20 additions & 1 deletion include/core/libcamera_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string>
#include <thread>
#include <variant>
#include <vector>

#include <libcamera/base/span.h>
#include <libcamera/camera.h>
Expand Down Expand Up @@ -52,7 +53,7 @@ class LibcameraApp
using CameraConfiguration = libcamera::CameraConfiguration;
using FrameBufferAllocator = libcamera::FrameBufferAllocator;
using StreamRole = libcamera::StreamRole;
using StreamRoles = libcamera::StreamRoles;
using StreamRoles = std::vector<libcamera::StreamRole>;
using PixelFormat = libcamera::PixelFormat;
using StreamConfiguration = libcamera::StreamConfiguration;
using BufferMap = Request::BufferMap;
Expand Down Expand Up @@ -95,6 +96,7 @@ class LibcameraApp
Options *GetOptions() const { return options_.get(); }

std::string const &CameraId() const;
std::string CameraModel() const;
void OpenCamera();
void CloseCamera();

Expand All @@ -117,6 +119,12 @@ class LibcameraApp
Stream *LoresStream(StreamInfo *info = nullptr) const;
Stream *GetMainStream() const;

const CameraManager *GetCameraManager() const;
std::vector<std::shared_ptr<libcamera::Camera>> GetCameras()
{
return GetCameras(camera_manager_.get());
}

std::vector<libcamera::Span<uint8_t>> Mmap(FrameBuffer *buffer) const;

void ShowPreview(CompletedRequestPtr &completed_request, Stream *stream);
Expand All @@ -127,6 +135,17 @@ class LibcameraApp
static unsigned int verbosity;
static unsigned int GetVerbosity() { return verbosity; }

static std::vector<std::shared_ptr<libcamera::Camera>> GetCameras(const CameraManager *cm)
{
std::vector<std::shared_ptr<libcamera::Camera>> cameras = cm->cameras();
// Do not show USB webcams as these are not supported in libcamera-apps!
auto rem = std::remove_if(cameras.begin(), cameras.end(),
[](auto &cam) { return cam->id().find("/usb") != std::string::npos; });
cameras.erase(rem, cameras.end());
std::sort(cameras.begin(), cameras.end(), [](auto l, auto r) { return l->id() > r->id(); });
return cameras;
}

protected:
std::unique_ptr<Options> options_;

Expand Down
2 changes: 2 additions & 0 deletions include/core/libcamera_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* libcamera_encoder.cpp - libcamera video encoding class.
*/

#pragma once

#include "core/libcamera_app.hpp"
#include "core/stream_info.hpp"
#include "core/video_options.hpp"
Expand Down
7 changes: 7 additions & 0 deletions include/core/video_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ struct VideoOptions : public Options
("frames", value<unsigned int>(&frames)->default_value(0),
"Run for the exact number of frames specified. This will override any timeout set.")
#if LIBAV_PRESENT
("libav-video-codec", value<std::string>(&libav_video_codec)->default_value("h264_v4l2m2m"),
"Sets the libav video codec to use. "
"To list available codecs, run the \"ffmpeg -codecs\" command.")
("libav-format", value<std::string>(&libav_format)->default_value(""),
"Sets the libav encoder output format to use. "
"Leave blank to try and deduce this from the filename.\n"
Expand All @@ -76,6 +79,8 @@ struct VideoOptions : public Options
"\"pactl list | grep -A2 'Source #' | grep 'Name: '\"\n"
"or for alsa, use the following command:\n"
"\"arecord -L\"")
("audio-channels", value<uint32_t>(&audio_channels)->default_value(0),
"Number of channels to use for recording audio. Set to 0 to use default value.")
("audio-bitrate", value<uint32_t>(&audio_bitrate)->default_value(32768),
"Set the audio bitrate for encoding, in bits/second.")
("audio-samplerate", value<uint32_t>(&audio_samplerate)->default_value(0),
Expand All @@ -94,11 +99,13 @@ struct VideoOptions : public Options
unsigned int intra;
bool inline_headers;
std::string codec;
std::string libav_video_codec;
std::string libav_format;
bool libav_audio;
std::string audio_codec;
std::string audio_device;
std::string audio_source;
uint32_t audio_channels;
uint32_t audio_bitrate;
uint32_t audio_samplerate;
int32_t av_sync;
Expand Down
2 changes: 1 addition & 1 deletion include/encoder/encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef std::function<void(void *, size_t, int64_t, bool)> OutputReadyCallback;
class Encoder
{
public:
static Encoder *Create(VideoOptions const *options, StreamInfo const &info);
static Encoder *Create(VideoOptions *options, StreamInfo const &info);

Encoder(VideoOptions const *options) : options_(options) {}
virtual ~Encoder() {}
Expand Down
7 changes: 7 additions & 0 deletions include/encoder/libav_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
#include <thread>
Expand All @@ -21,6 +22,7 @@ extern "C"
#include "libavutil/audio_fifo.h"
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_drm.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.h"
#include "libavutil/version.h"
#include "libswresample/swresample.h"
Expand Down Expand Up @@ -48,6 +50,8 @@ class LibAvEncoder : public Encoder
void videoThread();
void audioThread();

static void releaseBuffer(void *opaque, uint8_t *data);

std::atomic<bool> output_ready_;
bool abort_video_;
bool abort_audio_;
Expand All @@ -67,4 +71,7 @@ class LibAvEncoder : public Encoder
AVStream *stream_[3];
AVFormatContext *in_fmt_ctx_;
AVFormatContext *out_fmt_ctx_;

std::mutex drm_queue_lock_;
std::queue<std::unique_ptr<AVDRMFrameDescriptor>> drm_frame_queue_;
};
4 changes: 2 additions & 2 deletions include/image/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct StillOptions;

// In jpeg.cpp:
void jpeg_save(std::vector<libcamera::Span<uint8_t>> const &mem, StreamInfo const &info,
libcamera::ControlList const &metadata, std::string const &filename, std::string const &cam_name,
libcamera::ControlList const &metadata, std::string const &filename, std::string const &cam_model,
StillOptions const *options);

// In yuv.cpp:
Expand All @@ -28,7 +28,7 @@ void yuv_save(std::vector<libcamera::Span<uint8_t>> const &mem, StreamInfo const

// In dng.cpp:
void dng_save(std::vector<libcamera::Span<uint8_t>> const &mem, StreamInfo const &info,
libcamera::ControlList const &metadata, std::string const &filename, std::string const &cam_name,
libcamera::ControlList const &metadata, std::string const &filename, std::string const &cam_model,
StillOptions const *options);

// In png.cpp:
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ target_link_libraries(raspindi PRIVATE
# ndi
# dl
config++
encoders
outputs
# encoders
# outputs
ndioutput
camera_app
boost_program_options
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@ int main(int argc, char *argv[])
return -1;
}
return 0;
}
}
27 changes: 12 additions & 15 deletions src/ndi_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,29 @@ void NdiOutput::outputBuffer(void *mem, size_t size, int64_t timestamp_us, uint3
{
this->NDI_video_frame.p_data = (uint8_t*)mem;
NDIlib_send_send_video_v2(this->pNDI_send, &this->NDI_video_frame);
NDIlib_tally_t* NDI_tally;
NDIlib_send_get_tally(this->pNDI_send, NDI_tally, 0);
NDIlib_tally_t NDI_tally;
NDIlib_send_get_tally(this->pNDI_send, &NDI_tally, 0);
this->program = NDI_tally.on_program;
this->preview = NDI_tally.on_preview;

char pixelStatus;
std::ofstream neopixel;

std::cout << "PGM: " << NDI_tally->on_program << " PVW: " << NDI_tally->on_preview << std::endl;

if(NDI_tally->on_program)
if(this->isProgram())
{
neopixel.open(neopixelpath);
neopixel << "L";
neopixel.close();
pixelStatus = 'L';
}
else if (NDI_tally->on_preview)
else if (this->isPreview())
{
neopixel.open(neopixelpath);
neopixel << "P";
neopixel.close();
pixelStatus = 'P';
}
else
{
neopixel.open(neopixelpath);
neopixel << "N";
neopixel.close();
pixelStatus = 'N';
}
neopixel.open(neopixelpath);
neopixel << pixelStatus;
neopixel.close();
}


Expand Down

0 comments on commit 25321ed

Please sign in to comment.