Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bdca5e4

Browse files
committedFeb 26, 2025·
Implement the delegate
1 parent b397933 commit bdca5e4

File tree

9 files changed

+320
-17
lines changed

9 files changed

+320
-17
lines changed
 

‎examples/camera-app/camera-common/include/camera-app.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class CameraApp
3333
public:
3434
// This class is responsible for initialising all the camera clusters and managing the interactions between them
3535
explicit CameraApp(chip::EndpointId aClustersEndpoint, CameraDeviceInterface * cameraDevice) :
36-
mChimeServer(aClustersEndpoint, cameraDevice->GetChimeDelegate())
36+
mChimeServer(aClustersEndpoint, cameraDevice->GetChimeDelegate()),
37+
mWebRTCTransportProvider(&cameraDevice->GetWebRTCProviderDelegate(), aClustersEndpoint)
3738
{}
3839

3940
// Initialize all the camera device clusters.
@@ -42,6 +43,7 @@ class CameraApp
4243
private:
4344
// SDK cluster servers
4445
chip::app::Clusters::ChimeServer mChimeServer;
46+
chip::app::Clusters::WebRTCTransportProvider::WebRTCTransportProviderServer mWebRTCTransportProvider;
4547
};
4648

4749
void CameraAppInit(CameraDeviceInterface * cameraDevice);

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

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#pragma once
2020
#include <app/clusters/chime-server/chime-server.h>
21+
#include <app/clusters/webrtc-transport-provider-server/webrtc-transport-provider-server.h>
2122

2223
// Camera Device Interface defines all the clusters that need to be implemented for a Camera Device
2324
class CameraDeviceInterface
@@ -27,4 +28,7 @@ class CameraDeviceInterface
2728

2829
// Getter for Chime Delegate
2930
virtual chip::app::Clusters::ChimeDelegate & GetChimeDelegate() = 0;
31+
32+
// Getter for WebRTCProvider Delegate
33+
virtual chip::app::Clusters::WebRTCTransportProvider::Delegate & GetWebRTCProviderDelegate() = 0;
3034
};

‎examples/camera-app/linux/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ executable("chip-camera-app") {
3131
sources = [
3232
"${chip_root}/examples/camera-app/linux/src/camera-device.cpp",
3333
"${chip_root}/examples/camera-app/linux/src/clusters/chime/chime-manager.cpp",
34+
"${chip_root}/examples/camera-app/linux/src/clusters/webrtc_provider/webrtc-provider-manager.cpp",
3435
"include/CHIPProjectAppConfig.h",
3536
"main.cpp",
3637
]
@@ -45,6 +46,7 @@ executable("chip-camera-app") {
4546
include_dirs = [
4647
"include",
4748
"include/clusters/chime",
49+
"include/clusters/webrtc_provider",
4850
"${chip_root}/examples/camera-app/camera-common/include",
4951
]
5052

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

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020
#include "camera-device-interface.h"
2121
#include "chime-manager.h"
22+
#include "webrtc-provider-manager.h"
2223
#include <protocols/interaction_model/StatusCode.h>
2324

2425
namespace Camera {
@@ -30,10 +31,12 @@ class CameraDevice : public CameraDeviceInterface
3031
CameraDevice();
3132

3233
virtual chip::app::Clusters::ChimeDelegate & GetChimeDelegate();
34+
virtual chip::app::Clusters::WebRTCTransportProvider::Delegate & GetWebRTCProviderDelegate();
3335

3436
private:
3537
// Various cluster server delegates
3638
ChimeManager mChimeManager;
39+
WebRTCProviderManager mWebRTCProviderManager;
3740
};
3841

3942
} // namespace Camera
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
*
3+
* Copyright (c) 2025 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
#include <app/clusters/webrtc-transport-provider-server/webrtc-transport-provider-server.h>
21+
22+
namespace Camera {
23+
24+
class WebRTCProviderManager : public chip::app::Clusters::WebRTCTransportProvider::Delegate
25+
{
26+
public:
27+
WebRTCProviderManager() = default;
28+
~WebRTCProviderManager() = default;
29+
30+
CHIP_ERROR HandleSolicitOffer(const SolicitOfferRequestArgs & args,
31+
chip::app::Clusters::WebRTCTransportProvider::Structs::WebRTCSessionStruct::Type & outSession,
32+
bool & outDeferredOffer) override;
33+
34+
CHIP_ERROR
35+
HandleProvideOffer(const ProvideOfferRequestArgs & args,
36+
chip::app::Clusters::WebRTCTransportProvider::Structs::WebRTCSessionStruct::Type & outSession) override;
37+
38+
CHIP_ERROR HandleProvideAnswer(uint16_t sessionId, const std::string & sdpAnswer) override;
39+
40+
CHIP_ERROR HandleProvideICECandidates(uint16_t sessionId, const std::vector<std::string> & candidates) override;
41+
42+
CHIP_ERROR HandleEndSession(uint16_t sessionId,
43+
chip::app::Clusters::WebRTCTransportProvider::WebRTCEndReasonEnum reasonCode) override;
44+
};
45+
46+
} // namespace Camera

‎examples/camera-app/linux/src/camera-device.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ ChimeDelegate & CameraDevice::GetChimeDelegate()
3333
{
3434
return mChimeManager;
3535
}
36+
37+
WebRTCTransportProvider::Delegate & CameraDevice::GetWebRTCProviderDelegate()
38+
{
39+
return mWebRTCProviderManager;
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
/*
2+
*
3+
* Copyright (c) 2025 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "webrtc-provider-manager.h"
20+
#include <iostream>
21+
#include <lib/support/logging/CHIPLogging.h>
22+
23+
using namespace chip;
24+
using namespace chip::app;
25+
using namespace chip::app::Clusters;
26+
using namespace chip::app::Clusters::WebRTCTransportProvider;
27+
using namespace chip::app::Clusters::WebRTCTransportProvider::Structs;
28+
29+
using namespace Camera;
30+
31+
CHIP_ERROR WebRTCProviderManager::HandleSolicitOffer(const SolicitOfferRequestArgs & args, WebRTCSessionStruct::Type & outSession,
32+
bool & outDeferredOffer)
33+
{
34+
// If not able to meet the Resource Management and Stream Priorities conditions or are unable
35+
// to provide another WebRTC session
36+
// if (!CheckResourceManagement(args.streamUsage))
37+
// {
38+
// return CHIP_ERROR_NO_MEMORY;
39+
// }
40+
41+
// TODO: Do any resource checks (camera power, concurrency, etc.) and set outDeferredOffer = true for now
42+
// to indicate you need time to wake hardware or complete resource preparations.
43+
outDeferredOffer = true;
44+
45+
// Initialize a new WebRTC session from the SolicitOfferRequestArgs
46+
outSession.id = args.id;
47+
outSession.peerNodeID = args.peerNodeID;
48+
outSession.peerFabricIndex = args.peerFabricIndex;
49+
outSession.streamUsage = args.streamUsage;
50+
51+
// By spec, MetadataOptions SHALL be set to 0 and reserved for future use
52+
outSession.metadataOptions.ClearAll();
53+
54+
// Resolve or allocate a VIDEO stream
55+
if (args.videoStreamID.HasValue())
56+
{
57+
if (args.videoStreamID.Value().IsNull())
58+
{
59+
// Attempt to find the closest matching existing video stream
60+
// Optional<VideoStreamID> candidateVideoStream = FindClosestMatchingVideoStream(args.streamUsage);
61+
62+
// If no match, try allocating a new stream if resources allow
63+
// if (!candidateVideoStream.HasValue() && CanAllocateNewVideoStream())
64+
// {
65+
// candidateVideoStream = AllocateNewVideoStream(args.streamUsage);
66+
// }
67+
68+
// If still no match, resources are unavailable
69+
// if (!candidateVideoStream.HasValue())
70+
// {
71+
// return CHIP_ERROR_NO_MEMORY; // or another relevant error
72+
// }
73+
74+
// outSession.videoStreamID = candidateVideoStream.Value();
75+
}
76+
else
77+
{
78+
outSession.videoStreamID = args.videoStreamID.Value();
79+
}
80+
}
81+
else
82+
{
83+
outSession.videoStreamID.SetNull();
84+
}
85+
86+
// Resolve or allocate an AUDIO stream
87+
if (args.audioStreamID.HasValue())
88+
{
89+
if (args.audioStreamID.Value().IsNull())
90+
{
91+
// Attempt to find the closest matching existing audio stream
92+
// Optional<AudioStreamID> candidateAudioStream = FindClosestMatchingAudioStream(args.streamUsage);
93+
94+
// If no match, try allocating a new stream if resources allow
95+
// if (!candidateAudioStream.HasValue() && CanAllocateNewAudioStream())
96+
// {
97+
// candidateAudioStream = AllocateNewAudioStream(args.streamUsage);
98+
// }
99+
100+
// If still no match, resources are unavailable
101+
// if (!candidateAudioStream.HasValue())
102+
// {
103+
// return CHIP_ERROR_NO_MEMORY; // or another relevant error
104+
// }
105+
106+
// outSession.audioStreamID = candidateAudioStream.Value();
107+
}
108+
else
109+
{
110+
outSession.audioStreamID = args.audioStreamID.Value();
111+
}
112+
}
113+
else
114+
{
115+
outSession.audioStreamID.SetNull();
116+
}
117+
118+
return CHIP_NO_ERROR;
119+
}
120+
121+
CHIP_ERROR WebRTCProviderManager::HandleProvideOffer(const ProvideOfferRequestArgs & args, WebRTCSessionStruct::Type & outSession)
122+
{
123+
// If not able to meet the Resource Management and Stream Priorities conditions or are unable
124+
// to provide another WebRTC session
125+
// if (!CheckResourceManagement(args.streamUsage))
126+
// {
127+
// return CHIP_ERROR_NO_MEMORY;
128+
// }
129+
130+
// Initialize a new WebRTC session from the SolicitOfferRequestArgs
131+
outSession.id = args.id;
132+
outSession.peerNodeID = args.peerNodeID;
133+
outSession.peerFabricIndex = args.peerFabricIndex;
134+
outSession.streamUsage = args.streamUsage;
135+
136+
// Not storing the actual SDP, but you can do so if needed:
137+
std::string sdpOffer(args.sdp.data(), args.sdp.size());
138+
139+
// By spec, MetadataOptions SHALL be set to 0 and reserved for future use
140+
outSession.metadataOptions.ClearAll();
141+
142+
// Resolve or allocate a VIDEO stream
143+
if (args.videoStreamID.HasValue())
144+
{
145+
if (args.videoStreamID.Value().IsNull())
146+
{
147+
// Automatically select the closest matching video stream for the StreamUsage requested by looking at the and the
148+
// server MAY allocate a new video stream if there are available resources.
149+
150+
// Optional<VideoStreamID> candidateVideoStream = FindClosestMatchingVideoStream(args.streamUsage);
151+
152+
// If no match, try allocating a new stream if resources allow
153+
// if (!candidateVideoStream.HasValue() && CanAllocateNewVideoStream())
154+
// {
155+
// candidateVideoStream = AllocateNewVideoStream(args.streamUsage);
156+
// }
157+
158+
// If still no match, resources are unavailable
159+
// if (!candidateVideoStream.HasValue())
160+
// {
161+
// return CHIP_ERROR_NO_MEMORY; // or another relevant error
162+
// }
163+
164+
// outSession.videoStreamID = candidateVideoStream.Value();
165+
}
166+
else
167+
{
168+
outSession.videoStreamID = args.videoStreamID.Value();
169+
}
170+
}
171+
else
172+
{
173+
outSession.videoStreamID.SetNull();
174+
}
175+
176+
// Resolve or allocate an AUDIO stream
177+
if (args.audioStreamID.HasValue())
178+
{
179+
if (args.audioStreamID.Value().IsNull())
180+
{
181+
// Automatically select the closest matching audio stream for the StreamUsage requested and the server MAY allocate
182+
// a new audio stream if there are available resources.
183+
184+
// Optional<AudioStreamID> candidateAudioStream = FindClosestMatchingAudioStream(args.streamUsage);
185+
186+
// If no match, try allocating a new stream if resources allow
187+
// if (!candidateAudioStream.HasValue() && CanAllocateNewAudioStream())
188+
// {
189+
// candidateAudioStream = AllocateNewAudioStream(args.streamUsage);
190+
// }
191+
192+
// If still no match, resources are unavailable
193+
// if (!candidateAudioStream.HasValue())
194+
// {
195+
// return CHIP_ERROR_NO_MEMORY; // or another relevant error
196+
// }
197+
198+
// outSession.audioStreamID = candidateAudioStream.Value();
199+
}
200+
else
201+
{
202+
outSession.audioStreamID = args.audioStreamID.Value();
203+
}
204+
}
205+
else
206+
{
207+
outSession.audioStreamID.SetNull();
208+
}
209+
210+
return CHIP_NO_ERROR;
211+
}
212+
213+
CHIP_ERROR WebRTCProviderManager::HandleProvideAnswer(uint16_t sessionId, const std::string & sdpAnswer)
214+
{
215+
// The server calls this after it’s validated the session in mCurrentSessions.
216+
// Here you can push the sdpAnswer into your WebRTC library or do any final checks.
217+
218+
// For example:
219+
// MyWebRTCStack::SetRemoteDescription(sessionId, sdpAnswer);
220+
221+
return CHIP_NO_ERROR;
222+
}
223+
224+
CHIP_ERROR WebRTCProviderManager::HandleProvideICECandidates(uint16_t sessionId, const std::vector<std::string> & candidates)
225+
{
226+
// The server calls this when it has already located sessionId in mCurrentSessions.
227+
// Use these new ICE candidates in your actual WebRTC library, if needed.
228+
229+
return CHIP_NO_ERROR;
230+
}
231+
232+
CHIP_ERROR WebRTCProviderManager::HandleEndSession(uint16_t sessionId, WebRTCEndReasonEnum reasonCode)
233+
{
234+
// The server calls this after it finds the session in mCurrentSessions.
235+
// You can do any final hardware cleanup or state updates, but you do NOT remove
236+
// the session from your manager’s store (since there's no manager store).
237+
// The server will remove it from mCurrentSessions.
238+
239+
return CHIP_NO_ERROR;
240+
}

0 commit comments

Comments
 (0)
Please sign in to comment.