1 #include <OpenteraWebrtcNativeClient/DataChannelClient.h>
5 using namespace opentera;
8 int main(
int argc,
char* argv[])
10 vector<IceServer> iceServers;
13 cout <<
"IceServer::fetchFromServer failed" << endl;
17 auto signalingServerConfiguration =
21 DataChannelClient client(signalingServerConfiguration, webrtcConfiguration, dataChannelConfiguration);
23 client.setOnSignalingConnectionOpened(
27 cout <<
"OnSignalingConnectionOpened" << endl;
29 client.setOnSignalingConnectionClosed(
33 cout <<
"OnSignalingConnectionClosed" << endl;
35 client.setOnSignalingConnectionError(
36 [](
const string& error)
39 cout <<
"OnSignalingConnectionError:" << endl <<
"\t" << error;
42 client.setOnRoomClientsChanged(
43 [](
const vector<RoomClient>& roomClients)
46 cout <<
"OnRoomClientsChanged:" << endl;
47 for (
const auto& c : roomClients)
49 cout <<
"\tid=" << c.id() <<
", name=" << c.name() <<
", isConnected=" << c.isConnected() << endl;
53 client.setOnClientConnected(
57 cout <<
"OnClientConnected:" << endl;
58 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
60 client.setOnClientDisconnected(
64 cout <<
"OnClientDisconnected:" << endl;
65 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
67 client.setOnClientConnectionFailed(
71 cout <<
"OnClientConnectionFailed:" << endl;
72 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
76 [](
const string& error)
79 cout <<
"error:" << endl;
80 cout <<
"\t" << error << endl;
84 [](
const string& message)
87 cout <<
"log:" << endl;
88 cout <<
"\t" << message << endl;
91 client.setOnDataChannelOpened(
95 cout <<
"OnDataChannelOpened:" << endl;
96 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
98 client.setOnDataChannelClosed(
102 cout <<
"OnDataChannelClosed:" << endl;
103 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
105 client.setOnDataChannelError(
106 [](
const Client& client,
const string& error)
109 cout <<
"OnDataChannelError:" << endl;
110 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
111 cout <<
"\t" << error << endl;
113 client.setOnDataChannelMessageString(
114 [](
const Client& client,
const string& message)
117 cout <<
"setOnDataChannelMessageString:" << endl;
118 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
119 cout <<
"\t" << message << endl;
Represents a peer client.
Definition: Client.h:14
const std::string & id() const
Returns the client id.
Definition: Client.h:43
const std::string & name() const
Returns the client name.
Definition: Client.h:49
Represents a client for data channel communication.
Definition: DataChannelClient.h:16
static DataChannelConfiguration create()
Creates a data channel configuration with default values.
Definition: DataChannelConfiguration.h:62
static bool fetchFromServer(const std::string &url, const std::string &password, std::vector< IceServer > &iceServers, bool verifyCertificate=true)
Fetches the ice servers from the signaling server.
Definition: IceServer.cpp:61
static SignalingServerConfiguration create(std::string url, std::string clientName, std::string room)
Creates an signaling server configuration with the specified values.
Definition: SignalingServerConfiguration.h:64
static WebrtcConfiguration create()
Creates a WebRTC peer connection configuration with default values.
Definition: WebrtcConfiguration.h:39
1 #include <OpenteraWebrtcNativeClient/StreamClient.h>
3 #include <opencv2/core.hpp>
4 #include <opencv2/videoio.hpp>
5 #include <opencv2/highgui.hpp>
13 using namespace opentera;
16 class CvVideoCaptureVideoSource :
public VideoSource
18 atomic_bool m_stopped;
23 CvVideoCaptureVideoSource(
string path)
26 m_thread(&CvVideoCaptureVideoSource::run, this),
31 ~CvVideoCaptureVideoSource()
override
33 m_stopped.store(
true);
43 while (!m_stopped.load())
48 cerr <<
"Invalid video file" << endl;
52 auto frameDuration = chrono::microseconds(
static_cast<int>(1e6 / cap.get(cv::CAP_PROP_FPS)));
53 auto frameTime = chrono::steady_clock::now();
54 while (!m_stopped.load())
63 chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now().time_since_epoch()).count();
64 sendFrame(bgrImg, timestampUs);
66 frameTime += frameDuration;
67 this_thread::sleep_until(frameTime);
73 constexpr uint32_t SoundCardTotalDelayMs = 0;
74 constexpr
int BitsPerSample = 16;
75 constexpr
int SampleRate = 48000;
76 constexpr
size_t NumberOfChannels = 1;
77 constexpr chrono::milliseconds SinAudioSourceFrameDuration = 10ms;
78 constexpr chrono::milliseconds SinAudioSourceSleepBuffer = 2ms;
79 constexpr int16_t SinAudioSourceAmplitude = 15000;
83 atomic_bool m_stopped;
94 m_thread(&SinAudioSource::run, this)
98 ~SinAudioSource()
override
100 m_stopped.store(
true);
107 vector<int16_t> data(SinAudioSourceFrameDuration.count() * SampleRate / 1000, 0);
109 for (
size_t i = 0; i < data.size(); i++)
111 data[i] =
static_cast<int16_t
>(SinAudioSourceAmplitude * sin(t));
112 t += 2 * M_PI /
static_cast<double>(data.size());
115 while (!m_stopped.load())
117 sendFrame(data.data(), data.size() *
sizeof(int16_t) / bytesPerFrame());
119 auto start = chrono::steady_clock::now();
120 this_thread::sleep_for(SinAudioSourceFrameDuration - SinAudioSourceSleepBuffer);
121 while ((chrono::steady_clock::now() - start) < SinAudioSourceFrameDuration);
126 int main(
int argc,
char* argv[])
130 cout <<
"Usage: CppStreamClient video_path" << endl;
134 vector<IceServer> iceServers;
137 cout <<
"IceServer::fetchFromServer failed" << endl;
141 auto signalingServerConfiguration =
145 auto videoSource = make_shared<CvVideoCaptureVideoSource>(argv[1]);
146 auto audioSource = make_shared<SinAudioSource>();
148 client(signalingServerConfiguration, webrtcConfiguration, videoStreamConfiguration, videoSource, audioSource);
150 client.setOnSignalingConnectionOpened(
154 cout <<
"OnSignalingConnectionOpened" << endl;
156 client.setOnSignalingConnectionClosed(
160 cout <<
"OnSignalingConnectionClosed" << endl;
162 client.setOnSignalingConnectionError(
163 [](
const string& error)
166 cout <<
"OnSignalingConnectionError:" << endl <<
"\t" << error;
169 client.setOnRoomClientsChanged(
170 [](
const vector<RoomClient>& roomClients)
173 cout <<
"OnRoomClientsChanged:" << endl;
174 for (
const auto& c : roomClients)
176 cout <<
"\tid=" << c.id() <<
", name=" << c.name() <<
", isConnected=" << c.isConnected() << endl;
180 client.setOnClientConnected(
184 cout <<
"OnClientConnected:" << endl;
185 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
186 cv::namedWindow(client.
id(), cv::WINDOW_AUTOSIZE);
188 client.setOnClientDisconnected(
192 cout <<
"OnClientDisconnected:" << endl;
193 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
194 cv::destroyWindow(client.
id());
196 client.setOnClientConnectionFailed(
200 cout <<
"OnClientConnectionFailed:" << endl;
201 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
205 [](
const string& error)
208 cout <<
"error:" << endl;
209 cout <<
"\t" << error << endl;
213 [](
const string& message)
216 cout <<
"log:" << endl;
217 cout <<
"\t" << message << endl;
220 client.setOnAddRemoteStream(
224 cout <<
"OnAddRemoteStream:" << endl;
225 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
227 client.setOnRemoveRemoteStream(
231 cout <<
"OnRemoveRemoteStream:" << endl;
232 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
234 client.setOnVideoFrameReceived(
235 [](
const Client& client,
const cv::Mat& bgrImg, uint64_t timestampUs)
238 cout <<
"OnVideoFrameReceived:" << endl;
239 cv::imshow(client.
id(), bgrImg);
242 client.setOnAudioFrameReceived(
244 const void* audioData,
247 size_t numberOfChannels,
248 size_t numberOfFrames)
251 cout <<
"OnAudioFrameReceived:" << endl;
252 cout <<
"\tid=" << client.
id() <<
", name=" << client.
name() << endl;
253 cout <<
"\tbitsPerSample=" << bitsPerSample <<
", sampleRate = " << sampleRate;
254 cout <<
", numberOfChannels = " << numberOfChannels <<
", numberOfFrames=" << numberOfFrames << endl;
256 client.setOnMixedAudioFrameReceived(
257 [](
const void* audioData,
int bitsPerSample,
int sampleRate,
size_t numberOfChannels,
size_t numberOfFrames)
260 cout <<
"OnMixedAudioFrameReceived:" << endl;
261 cout <<
"\tbitsPerSample=" << bitsPerSample <<
", sampleRate=" << sampleRate;
262 cout <<
", numberOfChannels=" << numberOfChannels <<
", numberOfFrames=" << numberOfFrames << endl;
Represents a configuration of an audio source that can be added to a WebRTC call.
Definition: AudioSourceConfiguration.h:13
Represents an audio source that can be added to a WebRTC call.
Definition: AudioSource.h:25
A signaling client to join a WebRTC room and stream a video source.
Definition: StreamClient.h:18
Represents a configuration of a video source that can be added to a WebRTC call.
Definition: VideoSourceConfiguration.h:10
Represents a video source that can be added to a WebRTC call.
Definition: VideoSource.h:21
static VideoStreamConfiguration create()
Creates a stream configuration with default values.
Definition: VideoStreamConfiguration.h:60