Skip to content

Commit febe766

Browse files
committed
Camera App changes
- Define Camera HAL interface inside camera-device-interface.h. - Move stream handling logic from AVStream Delegate to inside CameraDevice. - Add initial Gstreamer support inside CameraDevice for stream start/stop.
1 parent 625d78a commit febe766

File tree

6 files changed

+826
-37
lines changed

6 files changed

+826
-37
lines changed

examples/camera-app/camera-common/include/camera-device-interface.h

+88-1
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,102 @@
2020
#include <app/clusters/chime-server/chime-server.h>
2121
#include <app/clusters/camera-av-stream-management-server/camera-av-stream-management-server.h>
2222

23+
using chip::app::Clusters::CameraAvStreamManagement::VideoStreamStruct;
24+
using chip::app::Clusters::CameraAvStreamManagement::AudioStreamStruct;
25+
using chip::app::Clusters::CameraAvStreamManagement::SnapshotStreamStruct;
26+
using chip::app::Clusters::CameraAvStreamManagement::VideoResolutionStruct;
27+
using chip::app::Clusters::CameraAvStreamManagement::VideoSensorParamsStruct;
28+
using chip::app::Clusters::CameraAvStreamManagement::ImageSnapshot;
29+
30+
// Enumeration for common camera errors
31+
enum class CameraError
32+
{
33+
SUCCESS,
34+
ERROR_INIT_FAILED,
35+
ERROR_VIDEO_STREAM_ALLOC_FAILED,
36+
ERROR_AUDIO_STREAM_ALLOC_FAILED,
37+
ERROR_SNAPSHOT_STREAM_ALLOC_FAILED,
38+
ERROR_VIDEO_STREAM_START_FAILED,
39+
ERROR_VIDEO_STREAM_STOP_FAILED,
40+
ERROR_AUDIO_STREAM_START_FAILED,
41+
ERROR_AUDIO_STREAM_STOP_FAILED,
42+
ERROR_SNAPSHOT_STREAM_START_FAILED,
43+
ERROR_SNAPSHOT_STREAM_STOP_FAILED,
44+
ERROR_CAPTURE_SNAPSHOT_FAILED,
45+
ERROR_CONFIG_FAILED,
46+
ERROR_NOT_IMPLEMENTED, // For features not supported on a platform
47+
};
48+
2349
// Camera Device Interface defines all the clusters that need to be implemented for a Camera Device
2450
class CameraDeviceInterface
2551
{
2652
public:
27-
virtual ~CameraDeviceInterface(){};
53+
virtual ~CameraDeviceInterface() = default;
2854

2955
// Getter for Chime Delegate
3056
virtual chip::app::Clusters::ChimeDelegate & GetChimeDelegate() = 0;
3157

3258
// Getter for CameraAVStreamManagement Delegate
3359
virtual chip::app::Clusters::CameraAvStreamManagement::CameraAVStreamMgmtDelegate & GetCameraAVStreamMgmtDelegate() = 0;
60+
61+
// Class defining the Camera HAL interface
62+
class CameraHALInterface {
63+
public:
64+
// Virtual destructor
65+
virtual ~CameraHALInterface() = default;
66+
67+
// Initialize the camera hardware
68+
virtual CameraError InitializeCameraDevice() = 0;
69+
70+
virtual CameraError InitializeStreams() = 0;
71+
72+
// Configure camera settings (e.g., exposure, focus)
73+
// virtual CameraError Configure(const std::string & setting, const std::string & value) = 0;
74+
75+
virtual CameraError VideoStreamAllocate(const VideoStreamStruct & videoStreamParams, uint16_t & outStreamID) = 0;
76+
virtual CameraError AudioStreamAllocate(const AudioStreamStruct & videoStreamParams, uint16_t & outStreamID) = 0;
77+
virtual CameraError SnapshotStreamAllocate(const SnapshotStreamStruct & videoStreamParams, uint16_t & outStreamID) = 0;
78+
79+
virtual CameraError VideoStreamDeallocate(uint16_t streamID) = 0;
80+
virtual CameraError AudioStreamDeallocate(uint16_t streamID) = 0;
81+
virtual CameraError SnapshotStreamDeallocate(uint16_t streamID) = 0;
82+
83+
// Capture a snapshot image
84+
virtual CameraError CaptureSnapshot(uint16_t streamID, const VideoResolutionStruct & resolution,
85+
ImageSnapshot & outImageSnapshot) = 0;
86+
// Start video stream
87+
virtual CameraError StartVideoStream(uint16_t streamID) = 0;
88+
89+
// Stop video stream
90+
virtual CameraError StopVideoStream(uint16_t streamID) = 0;
91+
92+
// Start audio stream
93+
virtual CameraError StartAudioStream(uint16_t streamID) = 0;
94+
95+
// Stop audio stream
96+
virtual CameraError StopAudioStream(uint16_t streamID) = 0;
97+
98+
// Start snapshot stream
99+
virtual CameraError StartSnapshotStream(uint16_t streamID) = 0;
100+
101+
// Stop snapshot stream
102+
virtual CameraError StopSnapshotStream(uint16_t streamID) = 0;
103+
104+
virtual VideoSensorParamsStruct & GetVideoSensorParams() = 0;
105+
106+
virtual bool GetNightVisionCapable() = 0;
107+
108+
virtual VideoResolutionStruct & GetMinViewport() = 0;
109+
110+
virtual uint8_t GetMaxConcurrentVideoEncoders() = 0;
111+
112+
virtual uint32_t GetMaxEncodedPixelRate() = 0;
113+
114+
virtual uint16_t GetFrameRate() = 0;
115+
116+
virtual void SetHDRMode(bool hdrMode) = 0;
117+
118+
};
119+
120+
virtual CameraHALInterface & GetCameraHALInterface() = 0;
34121
};

examples/camera-app/camera-common/src/camera-app.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,31 @@ CameraApp::CameraApp(chip::EndpointId aClustersEndpoint, CameraDeviceInterface *
3131
{
3232
mEndpoint = aClustersEndpoint;
3333
mCameraDevice = aCameraDevice;
34+
35+
// Instantiate Chime Server
3436
mChimeServerPtr = std::make_unique<ChimeServer>(mEndpoint, mCameraDevice->GetChimeDelegate());
3537

38+
// Fetch all initialization parameters for CameraAVStreamMgmt Server
3639
BitFlags<Feature> features;
3740
features.Set(Feature::kSnapshot);
3841
BitFlags<OptionalAttribute> optionalAttrs;
3942
optionalAttrs.Set(chip::app::Clusters::CameraAvStreamManagement::OptionalAttribute::kNightVision);
4043
optionalAttrs.Set(chip::app::Clusters::CameraAvStreamManagement::OptionalAttribute::kNightVisionIllum);
41-
uint32_t maxConcurrentVideoEncoders = 1;
42-
uint32_t maxEncodedPixelRate = 10000;
43-
VideoSensorParamsStruct sensorParams = { 4608, 2592, 120, chip::Optional<uint16_t>(30) }; // Typical numbers for Pi camera.
44-
bool nightVisionCapable = false;
45-
VideoResolutionStruct minViewport = { 854, 480 }; // Assuming 480p resolution.
44+
uint32_t maxConcurrentVideoEncoders = mCameraDevice->GetCameraHALInterface().GetMaxConcurrentVideoEncoders();
45+
uint32_t maxEncodedPixelRate = mCameraDevice->GetCameraHALInterface().GetMaxEncodedPixelRate();
46+
VideoSensorParamsStruct sensorParams = mCameraDevice->GetCameraHALInterface().GetVideoSensorParams();
47+
bool nightVisionCapable = mCameraDevice->GetCameraHALInterface().GetNightVisionCapable();
48+
VideoResolutionStruct minViewport = mCameraDevice->GetCameraHALInterface().GetMinViewport();
4649
std::vector<RateDistortionTradeOffStruct> rateDistortionTradeOffPoints = {};
47-
uint32_t maxContentBufferSize = 1024;
50+
uint32_t maxContentBufferSize = 1024;
4851
AudioCapabilitiesStruct micCapabilities{};
4952
AudioCapabilitiesStruct spkrCapabilities{};
5053
TwoWayTalkSupportTypeEnum twowayTalkSupport = TwoWayTalkSupportTypeEnum::kNotSupported;
5154
std::vector<SnapshotParamsStruct> supportedSnapshotParams = {};
5255
uint32_t maxNetworkBandwidth = 64;
5356
std::vector<StreamUsageEnum> supportedStreamUsages = { StreamUsageEnum::kLiveView, StreamUsageEnum::kRecording };
5457

58+
// Instantiate the CameraAVStreamMgmt Server
5559
mAVStreamMgmtServerPtr = std::make_unique<CameraAVStreamMgmtServer>(
5660
mCameraDevice->GetCameraAVStreamMgmtDelegate(), mEndpoint, features, optionalAttrs, maxConcurrentVideoEncoders, maxEncodedPixelRate,
5761
sensorParams, nightVisionCapable, minViewport, rateDistortionTradeOffPoints, maxContentBufferSize, micCapabilities,

examples/camera-app/linux/BUILD.gn

+16
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,27 @@ executable("chip-camera-app") {
6363
"include/clusters/camera-avstream-mgmt",
6464
"include/camera-hal",
6565
"${chip_root}/examples/camera-app/camera-common/include",
66+
67+
#TODO: Adjust paths
68+
"/usr/include/gstreamer-1.0",
69+
"/usr/include/glib-2.0",
70+
"/usr/lib/x86_64-linux-gnu/glib-2.0/include",
71+
"/usr/include/gobject-2.0",
72+
"/usr/include/gst-app-1.0",
6673
]
6774

6875
cflags = [ "-Wconversion" ]
6976

7077
output_dir = root_out_dir
78+
79+
# Manually specify GStreamer libraries
80+
libs = [
81+
"gstreamer-1.0",
82+
"glib-2.0",
83+
"gobject-2.0",
84+
"gstapp-1.0",
85+
]
86+
7187
}
7288

7389
group("linux") {

examples/camera-app/linux/include/camera-device.h

+105-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,53 @@
2020
#include "camera-device-interface.h"
2121
#include "chime-manager.h"
2222
#include "camera-av-stream-manager.h"
23-
#include "camera-hal.h"
2423
#include <protocols/interaction_model/StatusCode.h>
2524

25+
#include <gst/gst.h>
26+
#define VIDEO_STREAM_GST_DEST_PORT 5000
27+
#define AUDIO_STREAM_GST_DEST_PORT 5001
28+
2629
namespace Camera {
2730

28-
class CameraDevice : public CameraDeviceInterface
29-
//public CameraHAL::CameraHALInterface
31+
constexpr uint8_t kMaxVideoStreams = 10; // Maximum number of pre-allocated streams
32+
constexpr uint16_t kInvalidStreamID = 65500;
33+
34+
struct VideoStream
35+
{
36+
uint16_t id; // Stream ID
37+
bool isAllocated; // Flag to indicate if the stream is allocated
38+
chip::app::Clusters::CameraAvStreamManagement::VideoCodecEnum codec; // Codec information (e.g., "H.264", "HEVC")
39+
VideoResolutionStruct videoRes;
40+
uint16_t frameRate; // frame rate
41+
GstElement * videoPipeline;
42+
};
43+
44+
struct AudioStream
45+
{
46+
uint16_t id; // Stream ID
47+
bool isAllocated; // Flag to indicate if the stream is allocated
48+
chip::app::Clusters::CameraAvStreamManagement::AudioCodecEnum codec; // Codec information (e.g., "OPUS", "AACLC")
49+
uint8_t channelCount; // channel count
50+
GstElement * audioPipeline;
51+
};
52+
53+
struct SnapshotStream
54+
{
55+
uint16_t id; // Stream ID
56+
bool isAllocated; // Flag to indicate if the stream is allocated
57+
chip::app::Clusters::CameraAvStreamManagement::ImageCodecEnum codec; // Codec information (e.g., "JPEG")
58+
VideoResolutionStruct videoRes;
59+
uint8_t quality; // Quality
60+
GstElement * snapshotPipeline;
61+
};
62+
63+
class CameraDevice : public CameraDeviceInterface,
64+
public CameraDeviceInterface::CameraHALInterface
3065
{
3166
public:
3267

68+
~CameraDevice();
69+
3370
static CameraDevice & GetInstance()
3471
{
3572
static CameraDevice sCameraDevice;
@@ -39,9 +76,74 @@ class CameraDevice : public CameraDeviceInterface
3976
chip::app::Clusters::ChimeDelegate & GetChimeDelegate();
4077
chip::app::Clusters::CameraAvStreamManagement::CameraAVStreamMgmtDelegate & GetCameraAVStreamMgmtDelegate();
4178

79+
CameraDeviceInterface::CameraHALInterface & GetCameraHALInterface() { return *this; }
80+
81+
// HAL interface impl
82+
CameraError InitializeCameraDevice();
83+
84+
CameraError InitializeStreams();
85+
86+
CameraError VideoStreamAllocate(const VideoStreamStruct & allocateArgs, uint16_t & outStreamID);
87+
88+
CameraError VideoStreamDeallocate(const uint16_t streamID);
89+
90+
CameraError AudioStreamAllocate(const AudioStreamStruct & allocateArgs, uint16_t & outStreamID);
91+
92+
CameraError AudioStreamDeallocate(const uint16_t streamID);
93+
94+
CameraError SnapshotStreamAllocate(const SnapshotStreamStruct & allocateArgs, uint16_t & outStreamID);
95+
96+
CameraError SnapshotStreamDeallocate(const uint16_t streamID);
97+
98+
CameraError CaptureSnapshot(const uint16_t streamID, const VideoResolutionStruct & resolution,
99+
ImageSnapshot & outImageSnapshot);
100+
101+
CameraError StartVideoStream(uint16_t streamID);
102+
103+
// Stop video stream
104+
CameraError StopVideoStream(uint16_t streamID);
105+
106+
// Start audio stream
107+
CameraError StartAudioStream(uint16_t streamID);
108+
109+
// Stop audio stream
110+
CameraError StopAudioStream(uint16_t streamID);
111+
112+
// Start snapshot stream
113+
CameraError StartSnapshotStream(uint16_t streamID);
114+
115+
// Stop snapshot stream
116+
CameraError StopSnapshotStream(uint16_t streamID);
117+
118+
VideoSensorParamsStruct & GetVideoSensorParams();
119+
120+
bool GetNightVisionCapable();
121+
122+
VideoResolutionStruct & GetMinViewport();
123+
124+
uint8_t GetMaxConcurrentVideoEncoders();
125+
126+
uint32_t GetMaxEncodedPixelRate();
127+
128+
uint16_t GetFrameRate();
129+
130+
void SetHDRMode(bool hdrMode);
131+
42132
private:
43133
CameraDevice();
44134

135+
int videoDeviceFd = -1;
136+
std::vector<VideoStream> videoStreams; // Vector to hold available video streams
137+
std::vector<AudioStream> audioStreams; // Vector to hold available audio streams
138+
std::vector<SnapshotStream> snapshotStreams; // Vector to hold available snapshot streams
139+
140+
void InitializeVideoStreams();
141+
void InitializeAudioStreams();
142+
void InitializeSnapshotStreams();
143+
144+
GstElement * CreatePipeline(const std::string & pipelineString, CameraError& error);
145+
CameraError SetV4l2Control(int controlId, int value);
146+
45147
// Various cluster server delegates
46148
ChimeManager mChimeManager;
47149

0 commit comments

Comments
 (0)