Skip to content

Commit 69684c7

Browse files
committed
Improvement
1 parent b545250 commit 69684c7

File tree

2 files changed

+125
-60
lines changed

2 files changed

+125
-60
lines changed

src/app/clusters/webrtc-transport-provider-server/webrtc-transport-provider-server.cpp

+101-51
Original file line numberDiff line numberDiff line change
@@ -181,70 +181,104 @@ void WebRTCTransportProviderServer::RemoveSession(uint16_t sessionId)
181181
// Command Handlers
182182
void WebRTCTransportProviderServer::HandleSolicitOffer(HandlerContext & ctx, const Commands::SolicitOffer::DecodableType & req)
183183
{
184-
CHIP_ERROR err = CHIP_NO_ERROR;
185-
Status status = Status::Success;
186-
bool deferOffer = false;
184+
// Extract parameters from the command
185+
StreamUsageEnum streamUsage = req.streamUsage;
186+
auto videoStreamID = req.videoStreamID;
187+
auto audioStreamID = req.audioStreamID;
188+
auto iceServers = req.iceServers;
189+
auto iceTransportPolicy = req.iceTransportPolicy;
190+
auto metadataOptions = req.metadataOptions;
191+
192+
// Convert ICE Servers from DecodableList to vector
193+
Optional<std::vector<Structs::ICEServerStruct::Type>> iceServersList;
194+
if (iceServers.HasValue())
195+
{
196+
std::vector<Structs::ICEServerStruct::Type> list;
197+
auto iter = iceServers.Value().begin();
198+
while (iter.Next())
199+
{
200+
list.push_back(iter.GetValue());
201+
}
202+
iceServersList.SetValue(list);
203+
}
187204

188-
if (mDelegate == nullptr)
205+
// Convert ICETransportPolicy to string
206+
Optional<std::string> iceTransportPolicyStr;
207+
if (iceTransportPolicy.HasValue())
189208
{
190-
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::Failure);
191-
return;
209+
iceTransportPolicyStr.SetValue(std::string(iceTransportPolicy.Value().data(), iceTransportPolicy.Value().size()));
192210
}
193211

194-
auto nodeId = GetNodeIdFromCtx(ctx.mCommandHandler);
195-
auto fabricIdx = ctx.mCommandHandler.GetAccessingFabricIndex();
212+
NodeId nodeId = GetNodeIdFromCtx(ctx.mCommandHandler);
213+
FabricIndex fabricIdx = ctx.mCommandHandler.GetAccessingFabricIndex();
196214

197-
// Create a new session entry or fetch one, as needed
215+
// Delegate processing
198216
Structs::WebRTCSessionStruct::Type session;
199-
err = mDelegate->HandleSolicitOffer(req.streamUsage, nodeId, fabricIdx, session, deferOffer);
200-
if (err != CHIP_NO_ERROR)
201-
{
202-
status = StatusIB(err).mStatus;
203-
}
217+
bool deferredOffer = false;
218+
CHIP_ERROR err = mDelegate->HandleSolicitOffer(streamUsage, videoStreamID, audioStreamID, iceServersList,
219+
iceTransportPolicyStr, metadataOptions, nodeId, fabricIdx,
220+
session, deferredOffer);
221+
Status status = StatusIB(err).mStatus;
204222

205-
// Build the response command
223+
// Build response with actual session data
206224
Commands::SolicitOfferResponse::Type resp;
207-
resp.webRTCSessionID = static_cast<uint16_t>(session.sessionId);
208-
resp.deferredOffer = deferOffer;
209-
210-
// The cluster spec says video/audio stream IDs might be null if not known yet
211-
// or if no video/audio is requested. For illustration, we set them to NullOptional:
212-
resp.videoStreamID.SetNull();
213-
resp.audioStreamID.SetNull();
225+
resp.webRTCSessionID = session.id;
226+
resp.deferredOffer = deferredOffer;
227+
resp.videoStreamID = session.videoStreamID;
228+
resp.audioStreamID = session.audioStreamID;
214229

215230
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, resp, status);
216231
}
217232

218-
void WebRTCProviderServer::HandleSolicitOffer(HandlerContext & ctx, const Commands::SolicitOffer::DecodableType & req)
233+
void WebRTCTransportProviderServer::HandleSolicitOffer(HandlerContext & ctx, const Commands::SolicitOffer::DecodableType & req)
219234
{
220-
// Example logic: we create a brand new session with an ID
221-
static uint16_t sNextSessionId = 1; // simplistic ID generator for illustration
222-
uint16_t newSessionId = sNextSessionId++;
223-
224-
// Fill out the struct from the spec
225-
WebRTCSessionStruct::Type newSession;
226-
newSession.id = newSessionId;
227-
newSession.peerNodeID = ctx.mCommandHandler.GetSubjectDescriptor().subject;
228-
newSession.peerFabricIndex = ctx.mCommandHandler.GetAccessingFabricIndex();
229-
newSession.streamUsage = req.streamUsage;
230-
newSession.videoStreamID.SetNull();
231-
newSession.audioStreamID.SetNull();
232-
newSession.metadataOptions.SetRaw(0);
233-
234-
// Store it
235-
AddOrUpdateSession(newSession);
236-
237-
// Build the response
238-
Commands::SolicitOfferResponse::Type response;
239-
response.webRTCSessionID = newSessionId;
240-
// For demonstration, we say "deferredOffer" is always false
241-
response.deferredOffer = false;
242-
// Echo the video/audio stream IDs we set
243-
response.videoStreamID = newSession.videoStreamID;
244-
response.audioStreamID = newSession.audioStreamID;
245-
246-
// Return the response
247-
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
235+
// Extract parameters from the command
236+
uint8_t streamUsage = req.streamUsage;
237+
auto videoStreamID = req.videoStreamID;
238+
auto audioStreamID = req.audioStreamID;
239+
auto iceServers = req.iceServers;
240+
auto iceTransportPolicy = req.iceTransportPolicy;
241+
auto metadataOptions = req.metadataOptions;
242+
243+
// Convert ICE Servers from DecodableList to vector
244+
Optional<std::vector<Structs::ICEServerStruct::Type>> iceServersList;
245+
if (iceServers.HasValue())
246+
{
247+
std::vector<Structs::ICEServerStruct::Type> list;
248+
auto iter = iceServers.Value().begin();
249+
while (iter.Next())
250+
{
251+
list.push_back(iter.GetValue());
252+
}
253+
iceServersList.SetValue(list);
254+
}
255+
256+
// Convert ICETransportPolicy to string
257+
Optional<std::string> iceTransportPolicyStr;
258+
if (iceTransportPolicy.HasValue())
259+
{
260+
iceTransportPolicyStr.SetValue(std::string(iceTransportPolicy.Value().data(), iceTransportPolicy.Value().size()));
261+
}
262+
263+
NodeId nodeId = GetNodeIdFromCtx(ctx.mCommandHandler);
264+
FabricIndex fabricIdx = ctx.mCommandHandler.GetAccessingFabricIndex();
265+
266+
// Delegate processing
267+
Structs::WebRTCSessionStruct::Type session;
268+
bool deferredOffer = false;
269+
CHIP_ERROR err = mDelegate->HandleSolicitOffer(streamUsage, videoStreamID, audioStreamID, iceServersList,
270+
iceTransportPolicyStr, metadataOptions, nodeId, fabricIdx,
271+
session, deferredOffer);
272+
Status status = StatusIB(err).mStatus;
273+
274+
// Build response with actual session data
275+
Commands::SolicitOfferResponse::Type resp;
276+
resp.webRTCSessionID = session.id;
277+
resp.deferredOffer = deferredOffer;
278+
resp.videoStreamID = session.videoStreamID;
279+
resp.audioStreamID = session.audioStreamID;
280+
281+
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, resp, status);
248282
}
249283

250284
void WebRTCTransportProviderServer::HandleProvideOffer(HandlerContext & ctx, const Commands::ProvideOffer::DecodableType & req)
@@ -367,6 +401,22 @@ void WebRTCTransportProviderServer::HandleProvideICECandidates(HandlerContext &
367401
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
368402
}
369403

404+
void WebRTCTransportProviderServer::HandleProvideICECandidates(HandlerContext & ctx,
405+
const Commands::ProvideICECandidates::DecodableType & req)
406+
{
407+
std::vector<std::string> candidates;
408+
for (auto & candidate : req.iCECandidates)
409+
{
410+
candidates.emplace_back(candidate.data(), candidate.size());
411+
}
412+
413+
NodeId nodeId = GetNodeIdFromCtx(ctx.mCommandHandler);
414+
FabricIndex fabricIdx = ctx.mCommandHandler.GetAccessingFabricIndex();
415+
416+
CHIP_ERROR err = mDelegate->HandleProvideICECandidates(req.webRTCSessionID, nodeId, fabricIdx, candidates);
417+
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, StatusIB(err).mStatus);
418+
}
419+
370420
void WebRTCTransportProviderServer::HandleEndSession(HandlerContext & ctx, const Commands::EndSession::DecodableType & req)
371421
{
372422
// Remove from our vector

src/app/clusters/webrtc-transport-provider-server/webrtc-transport-provider-server.h

+24-9
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,37 @@ class Delegate
4141
/**
4242
* @brief Called when the server receives the SolicitOffer command.
4343
* The delegate should decide how to set up a new session, or return an
44-
* existing one if that is allowed by your design.
44+
* existing one.
4545
*
46-
* @param[in] usage Stream usage from the spec (e.g. camera streaming).
46+
* @param[in] usage Stream usage for this session.
4747
* @param[in] peerNodeId The Node ID of the controller requesting.
4848
* @param[in] peerFabricIndex The FabricIndex of the controller requesting.
4949
* @param[out] outSession A new or existing session to store internally.
5050
* @param[out] outDeferredOffer If the device is in standby or otherwise delayed.
5151
* @return CHIP_NO_ERROR on success; appropriate error on failure.
5252
*/
53-
virtual CHIP_ERROR HandleSolicitOffer(uint8_t usage, NodeId peerNodeId, FabricIndex peerFabricIndex,
54-
Structs::WebRTCSessionStruct::Type & outSession, bool & outDeferredOffer) = 0;
53+
virtual CHIP_ERROR HandleSolicitOffer(
54+
StreamUsageEnum streamUsage,
55+
const Optional<DataModel::Nullable<uint16_t>> & videoStreamId,
56+
const Optional<DataModel::Nullable<uint16_t>> & audioStreamId,
57+
const Optional<std::vector<Structs::ICEServerStruct::Type>> & iceServers,
58+
const Optional<std::string> & iceTransportPolicy,
59+
const BitMask<WebRTCMetadataOptionsBitmap> & metadataOptions,
60+
NodeId peerNodeId, FabricIndex peerFabricIndex,
61+
Structs::WebRTCSessionStruct::Type & outSession, bool & outDeferredOffer) = 0;
5562

5663
/**
5764
* @brief Called when the server receives ProvideOffer to create or re-offer an SDP.
5865
*/
59-
virtual CHIP_ERROR HandleProvideOffer(uint16_t existingSessionId, bool isNewSession, uint8_t usage, NodeId peerNodeId,
60-
FabricIndex peerFabricIdx, Structs::WebRTCSessionStruct::Type & outSession) = 0;
66+
virtual CHIP_ERROR HandleProvideOffer(
67+
const Optional<uint16_t> & existingSessionId, const chip::CharSpan & sdp, uint8_t streamUsage,
68+
const Optional<DataModel::Nullable<uint16_t>> & videoStreamId,
69+
const Optional<DataModel::Nullable<uint16_t>> & audioStreamId,
70+
const Optional<std::vector<Structs::ICEServerStruct::Type>> & iceServers,
71+
const Optional<std::string> & iceTransportPolicy,
72+
const BitMask<WebRTCMetadataOptionsBitmap> & metadataOptions,
73+
NodeId peerNodeId, FabricIndex peerFabricIndex,
74+
Structs::WebRTCSessionStruct::Type & outSession) = 0;
6175

6276
/**
6377
* @brief Called when the server receives ProvideAnswer.
@@ -67,14 +81,15 @@ class Delegate
6781

6882
/**
6983
* @brief Called when the server receives ProvideICECandidates.
70-
*/
84+
*/
7185
virtual CHIP_ERROR HandleProvideICECandidates(uint16_t sessionId, NodeId peerNodeId, FabricIndex peerFabricIdx,
7286
const std::vector<std::string> & candidates) = 0;
7387

7488
/**
7589
* @brief Called when the server or client ends a session via EndSession.
76-
*/
77-
virtual CHIP_ERROR HandleEndSession(uint16_t sessionId, NodeId peerNodeId, FabricIndex peerFabricIdx, uint8_t reasonCode) = 0;
90+
*/
91+
virtual CHIP_ERROR HandleEndSession(uint16_t sessionId, NodeId peerNodeId, FabricIndex peerFabricIdx,
92+
uint8_t reasonCode) = 0;
7893
};
7994

8095
class WebRTCTransportProviderServer : public AttributeAccessInterface, public CommandHandlerInterface

0 commit comments

Comments
 (0)