ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
ice_selected_pair.cpp
Go to the documentation of this file.
1
12
13/* Storage capacities in elements; text buffers include the terminating NUL. */
14#define NETWORK_WEBRTC_ICE_SELECTED_PAIR_LOCAL_BUFFER_SIZE 512
15#define NETWORK_WEBRTC_ICE_SELECTED_PAIR_REMOTE_BUFFER_SIZE 512
16
17#include <rtc/rtc.hpp>
18#include <string>
19#include <cstring>
20
23
24// Windows.h defines these as macros which conflict with our error codes
25#ifdef _WIN32
26#undef ERROR_INVALID_STATE
27#undef ERROR_INVALID_PARAM
28#endif
30
37static asciichat_error_t parse_datachannel_candidate(const std::string &candidate_str, ice_candidate_t *candidate) {
38 if (!candidate) {
40 }
41
42 // Remove "candidate:" prefix if present
43 std::string line = candidate_str;
44 const char *prefix = "candidate:";
45 size_t prefix_len = strlen(prefix);
46 if (line.compare(0, prefix_len, prefix) == 0) {
47 line = line.substr(prefix_len);
48 }
49
50 // Use the existing ice_parse_candidate function
51 return ice_parse_candidate(line.c_str(), candidate);
52}
53
54extern "C" {
55
63 if (!pc) {
65 }
66
67 try {
68 // Get the libdatachannel peer connection ID using helper function
69 int rtc_id = webrtc_get_rtc_id(pc);
70 if (rtc_id < 0) {
72 }
73
74 // Query selected candidate pair from libdatachannel
75 // Note: This requires libdatachannel to have the selected candidate pair available
78
79 // Try to get the selected local candidate
80 if (rtcGetSelectedCandidatePair(rtc_id, local_buf, sizeof(local_buf), remote_buf, sizeof(remote_buf)) < 0) {
81 // No pair selected yet, or error accessing it
83 }
84
85 // Parse local candidate if requested
86 if (local_candidate) {
87 std::string local_str(local_buf);
88 asciichat_error_t err = parse_datachannel_candidate(local_str, local_candidate);
89 if (err != ASCIICHAT_OK) {
90 return err;
91 }
92 }
93
94 // Parse remote candidate if requested
95 if (remote_candidate) {
96 std::string remote_str(remote_buf);
97 asciichat_error_t err = parse_datachannel_candidate(remote_str, remote_candidate);
98 if (err != ASCIICHAT_OK) {
99 return err;
100 }
101 }
102
103 return ASCIICHAT_OK;
104
105 } catch (const std::exception &) {
106 return ERROR_NETWORK;
107 } catch (...) {
108 return ERROR_NETWORK;
109 }
110}
111
112} // extern "C"
Error and exit codes - unified status values (0-255).
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK
Definition error_codes.h:77
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
int webrtc_get_rtc_id(webrtc_peer_connection_t *pc)
Get the internal libdatachannel peer connection ID.
struct webrtc_peer_connection webrtc_peer_connection_t
asciichat_error_t ice_parse_candidate(const char *line, ice_candidate_t *candidate)
Parse ICE candidate from attribute string.
Definition ice.c:318
ICE (Interactive Connectivity Establishment) for WebRTC.
#define NETWORK_WEBRTC_ICE_SELECTED_PAIR_REMOTE_BUFFER_SIZE
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).
#define NETWORK_WEBRTC_ICE_SELECTED_PAIR_LOCAL_BUFFER_SIZE
Single ICE candidate for connectivity.
Definition ice.h:118