ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
ice_selected_pair.cpp
Go to the documentation of this file.
1
13#include <rtc/rtc.hpp>
14#include <string>
15#include <cstring>
16
17#include <ascii-chat/network/webrtc/ice.h>
18#include <ascii-chat/network/webrtc/webrtc.h>
19
20// Windows.h defines these as macros which conflict with our error codes
21#ifdef _WIN32
22#undef ERROR_INVALID_STATE
23#undef ERROR_INVALID_PARAM
24#endif
25#include <ascii-chat/common/error_codes.h>
26
33static asciichat_error_t parse_datachannel_candidate(const std::string &candidate_str, ice_candidate_t *candidate) {
34 if (!candidate) {
35 return ERROR_INVALID_PARAM;
36 }
37
38 // Remove "candidate:" prefix if present
39 std::string line = candidate_str;
40 const char *prefix = "candidate:";
41 size_t prefix_len = strlen(prefix);
42 if (line.compare(0, prefix_len, prefix) == 0) {
43 line = line.substr(prefix_len);
44 }
45
46 // Use the existing ice_parse_candidate function
47 return ice_parse_candidate(line.c_str(), candidate);
48}
49
50extern "C" {
51
58asciichat_error_t ice_get_selected_pair_impl(webrtc_peer_connection_t *pc, ice_candidate_t *local_candidate,
59 ice_candidate_t *remote_candidate) {
60 if (!pc) {
61 return ERROR_INVALID_PARAM;
62 }
63
64 try {
65 // Get the libdatachannel peer connection ID using helper function
66 int rtc_id = webrtc_get_rtc_id(pc);
67 if (rtc_id < 0) {
68 return ERROR_INVALID_PARAM;
69 }
70
71 // Query selected candidate pair from libdatachannel
72 // Note: This requires libdatachannel to have the selected candidate pair available
73 char local_buf[512];
74 char remote_buf[512];
75
76 // Try to get the selected local candidate
77 if (rtcGetSelectedCandidatePair(rtc_id, local_buf, sizeof(local_buf), remote_buf, sizeof(remote_buf)) < 0) {
78 // No pair selected yet, or error accessing it
79 return ERROR_INVALID_STATE;
80 }
81
82 // Parse local candidate if requested
83 if (local_candidate) {
84 std::string local_str(local_buf);
85 asciichat_error_t err = parse_datachannel_candidate(local_str, local_candidate);
86 if (err != ASCIICHAT_OK) {
87 return err;
88 }
89 }
90
91 // Parse remote candidate if requested
92 if (remote_candidate) {
93 std::string remote_str(remote_buf);
94 asciichat_error_t err = parse_datachannel_candidate(remote_str, remote_candidate);
95 if (err != ASCIICHAT_OK) {
96 return err;
97 }
98 }
99
100 return ASCIICHAT_OK;
101
102 } catch (const std::exception &e) {
103 return ERROR_NETWORK;
104 } catch (...) {
105 return ERROR_NETWORK;
106 }
107}
108
109} // extern "C"
asciichat_error_t ice_parse_candidate(const char *line, ice_candidate_t *candidate)
Definition ice.c:319
asciichat_error_t ice_get_selected_pair_impl(webrtc_peer_connection_t *pc, ice_candidate_t *local_candidate, ice_candidate_t *remote_candidate)
Get selected ICE candidate pair (C++ implementation)
int webrtc_get_rtc_id(webrtc_peer_connection_t *pc)
Get the internal libdatachannel peer connection ID.