20
20
#include < AppMain.h>
21
21
#include < platform/CHIPDeviceConfig.h>
22
22
23
+ #include < rtc/rtc.hpp>
24
+ #include < thread>
25
+
23
26
using namespace chip ;
24
27
using namespace chip ::app;
25
28
using namespace chip ::app::Clusters;
26
29
using namespace Camera ;
30
+ using namespace std ::chrono_literals;
27
31
28
32
CameraDevice cameraDevice;
29
33
34
+ void RunCommandThread ()
35
+ {
36
+ std::this_thread::sleep_for (1s);
37
+
38
+ rtc::InitLogger (rtc::LogLevel::Warning);
39
+
40
+ rtc::Configuration config;
41
+ // config.iceServers.emplace_back("stun.l.google.com:19302");
42
+
43
+ auto pc = std::make_shared<rtc::PeerConnection>(config);
44
+
45
+ pc->onLocalDescription ([](rtc::Description description) {
46
+ std::cout << " Local Description (Paste this to the other peer):" << std::endl;
47
+ std::cout << std::string (description) << std::endl;
48
+ });
49
+
50
+ pc->onLocalCandidate ([](rtc::Candidate candidate) {
51
+ std::cout << " Local Candidate (Paste this to the other peer after the local description):" << std::endl;
52
+ std::cout << std::string (candidate) << std::endl << std::endl;
53
+ });
54
+
55
+ pc->onStateChange ([](rtc::PeerConnection::State state) { std::cout << " [State: " << state << " ]" << std::endl; });
56
+ pc->onGatheringStateChange (
57
+ [](rtc::PeerConnection::GatheringState state) { std::cout << " [Gathering State: " << state << " ]" << std::endl; });
58
+
59
+ std::shared_ptr<rtc::DataChannel> dc;
60
+ pc->onDataChannel ([&](std::shared_ptr<rtc::DataChannel> _dc) {
61
+ std::cout << " [Got a DataChannel with label: " << _dc->label () << " ]" << std::endl;
62
+ dc = _dc;
63
+
64
+ dc->onClosed ([&]() { std::cout << " [DataChannel closed: " << dc->label () << " ]" << std::endl; });
65
+
66
+ dc->onMessage ([](auto data) {
67
+ if (std::holds_alternative<std::string>(data))
68
+ {
69
+ std::cout << " [Received message: " << std::get<std::string>(data) << " ]" << std::endl;
70
+ }
71
+ });
72
+ });
73
+
74
+ bool exit = false ;
75
+ while (!exit )
76
+ {
77
+ std::cout << std::endl
78
+ << " **********************************************************************************"
79
+ " *****"
80
+ << std::endl
81
+ << " * 0: Exit /"
82
+ << " 1: Enter remote description /"
83
+ << " 2: Enter remote candidate /"
84
+ << " 3: Send message /"
85
+ << " 4: Print Connection Info *" << std::endl
86
+ << " [Command]: " ;
87
+
88
+ int command = -1 ;
89
+ std::cin >> command;
90
+ std::cin.ignore ();
91
+
92
+ switch (command)
93
+ {
94
+ case 0 : {
95
+ exit = true ;
96
+ break ;
97
+ }
98
+ case 1 : {
99
+ // Parse Description
100
+ std::cout << " [Description]: " ;
101
+ std::string sdp, line;
102
+ while (getline (std::cin, line) && !line.empty ())
103
+ {
104
+ sdp += line;
105
+ sdp += " \r\n " ;
106
+ }
107
+ pc->setRemoteDescription (sdp);
108
+ std::this_thread::sleep_for (1s);
109
+ break ;
110
+ }
111
+ case 2 : {
112
+ // Parse Candidate
113
+ std::cout << " [Candidate]: " ;
114
+ std::string candidate;
115
+ getline (std::cin, candidate);
116
+ pc->addRemoteCandidate (candidate);
117
+ std::this_thread::sleep_for (2s);
118
+ break ;
119
+ }
120
+ case 3 : {
121
+ // Send Message
122
+ if (!dc || !dc->isOpen ())
123
+ {
124
+ std::cout << " ** Channel is not Open ** " ;
125
+ break ;
126
+ }
127
+ std::cout << " [Message]: " ;
128
+ std::string message;
129
+ getline (std::cin, message);
130
+ dc->send (message);
131
+ break ;
132
+ }
133
+ case 4 : {
134
+ // Connection Info
135
+ if (!dc || !dc->isOpen ())
136
+ {
137
+ std::cout << " ** Channel is not Open ** " ;
138
+ break ;
139
+ }
140
+ rtc::Candidate local, remote;
141
+ std::optional<std::chrono::milliseconds> rtt = pc->rtt ();
142
+ if (pc->getSelectedCandidatePair (&local, &remote))
143
+ {
144
+ std::cout << " Local: " << local << std::endl;
145
+ std::cout << " Remote: " << remote << std::endl;
146
+ std::cout << " Bytes Sent:" << pc->bytesSent () << " / Bytes Received:" << pc->bytesReceived ()
147
+ << " / Round-Trip Time:" ;
148
+ if (rtt.has_value ())
149
+ std::cout << rtt.value ().count ();
150
+ else
151
+ std::cout << " null" ;
152
+ std::cout << " ms" ;
153
+ }
154
+ else
155
+ {
156
+ std::cout << " Could not get Candidate Pair Info" << std::endl;
157
+ }
158
+ break ;
159
+ }
160
+ default : {
161
+ std::cout << " ** Invalid Command ** " << std::endl;
162
+ break ;
163
+ }
164
+ }
165
+ }
166
+
167
+ if (dc)
168
+ dc->close ();
169
+
170
+ if (pc)
171
+ pc->close ();
172
+ }
173
+
30
174
void ApplicationInit ()
31
175
{
32
176
ChipLogProgress (Zcl, " Matter Camera Linux App: ApplicationInit()" );
@@ -42,6 +186,9 @@ int main(int argc, char * argv[])
42
186
{
43
187
VerifyOrDie (ChipLinuxAppInit (argc, argv) == 0 );
44
188
189
+ std::thread runCommands (RunCommandThread);
190
+ runCommands.detach ();
191
+
45
192
ChipLinuxAppMainLoop ();
46
193
47
194
return 0 ;
0 commit comments