|
| 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