-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
421 lines (349 loc) · 13.6 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// dear imgui: standalone example application for GLFW + OpenGL 3, using programmable pipeline
// If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <opentok.h>
#include <iostream>
#include <map>
#include <memory>
#include "renderer.h"
#include "session_info.h"
#include "ui_state.h"
using namespace std;
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
/**
* Static vars
*/
map<string, unique_ptr<Renderer>> renderer_map;
UIState ui_state;
static bool publishVideo = true;
static bool publishAudio = true;
static bool subscriberVideo = true;
static bool subscriberAudio = true;
static otc_session* session = nullptr;
static otc_publisher* publisher = nullptr;
static otc_subscriber* subscriber = nullptr;
void hidePublisherButton() {
ui_state.showPublisherButtons = false;
ui_state.isPublishing = false;
}
void hideSubscriberButton() {
ui_state.isSubscribing = false;
ui_state.showSubscriberButtons = false;
}
void setPublisherAudio() {
otc_publisher_set_publish_audio(publisher, publishAudio);
}
void setPublisherVideo() {
otc_publisher_set_publish_video(publisher, publishVideo);
}
void setSubscriberAudio() {
otc_subscriber_set_subscribe_to_audio(subscriber, subscriberAudio);
}
void setSubscriberVideo() {
otc_subscriber_set_subscribe_to_video(subscriber, subscriberVideo);
}
/**
* Subscriber Callbacks
*/
static void on_subscriber_error(otc_subscriber* subscriber,
void *user_data,
const char* error_string,
enum otc_subscriber_error_code error) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
std::cout << "Subscriber error. Error code: " << error_string << std::endl;
ui_state.isSubscribing = false;
}
static void on_subscriber_render_frame(otc_subscriber *subscriber,
void *user_data,
const otc_video_frame *frame) {
otc_stream* stream = otc_subscriber_get_stream(subscriber);
string streamId(otc_stream_get_id(stream));
renderer_map[streamId]->set_frame(frame);
}
static void on_subscriber_reconnected(otc_subscriber * subscriber, void *user_data) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
/**
* Session Callbacks
*/
static void on_session_connected(otc_session *session, void *user_data) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
ui_state.isSessionConnected = true;
ui_state.showPublisherButtons = true;
}
static void on_session_connection_created(otc_session *session,
void *user_data,
const otc_connection *connection) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
static void on_session_connection_dropped(otc_session *session,
void *user_data,
const otc_connection *connection) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
static void on_session_stream_received(otc_session *session,
void *user_data,
const otc_stream *stream) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
otc_subscriber_callbacks subscriber_callbacks = {0};
subscriber_callbacks.on_render_frame = on_subscriber_render_frame;
subscriber_callbacks.on_reconnected = on_subscriber_reconnected;
string streamId(otc_stream_get_id(stream));
// Create an empty placeholder for the renderer
// This callback is not called in the main thread so we cannot
// create the renderer here
// The main loop will create a renderer for the stream
unique_ptr<Renderer> ptr;
renderer_map[streamId] = std::move(ptr);
subscriber = otc_subscriber_new(stream, &subscriber_callbacks);
ui_state.showSubscriberButtons = true;
}
static void on_session_stream_dropped(otc_session *session,
void *user_data,
const otc_stream *stream) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
static void on_session_disconnected(otc_session *session, void *user_data) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
ui_state.isSessionConnected = false;
hidePublisherButton();
hideSubscriberButton();
}
static void on_session_error(otc_session *session,
void *user_data,
const char *error_string,
enum otc_session_error_code error) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
std::cout << "Session error. Error : " << error_string << std::endl;
hidePublisherButton();
hideSubscriberButton();
}
static void on_session_reconnection_started(otc_session *session, void *user_data) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
static void on_session_reconnected(otc_session *session, void *user_data) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
/**
* Publisher Callbacks
*/
static void on_publisher_stream_created(otc_publisher *publisher,
void *user_data,
const otc_stream *stream) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
static void on_publisher_render_frame(otc_publisher *publisher,
void *user_data,
const otc_video_frame *frame) {
if (renderer_map["PUBLISHER"] != nullptr) {
renderer_map["PUBLISHER"]->set_frame(frame);
}
}
static void on_publisher_stream_destroyed(otc_publisher *publisher,
void *user_data,
const otc_stream *stream) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
ui_state.isPublishing = false;
}
static void on_publisher_error(otc_publisher *publisher,
void *user_data,
const char* error_string,
enum otc_publisher_error_code error_code) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
std::cout << "Publisher error. Error code: " << error_string << std::endl;
ui_state.isPublishing = false;
}
/**
* Utility OT functions
*/
static void on_otc_log_message(const char* message) {
std::cout << __FUNCTION__ << ":" << message << std::endl;
}
static void init_ot() {
if (otc_init(nullptr) != OTC_SUCCESS) {
std::cout << "Could not init OpenTok library" << std::endl;
return;
}
otc_log_set_logger_callback(on_otc_log_message);
otc_log_enable(OTC_LOG_LEVEL_INFO);
struct otc_session_callbacks session_callbacks = {0};
session_callbacks.on_connected = on_session_connected;
session_callbacks.on_connection_created = on_session_connection_created;
session_callbacks.on_connection_dropped = on_session_connection_dropped;
session_callbacks.on_stream_received = on_session_stream_received;
session_callbacks.on_stream_dropped = on_session_stream_dropped;
session_callbacks.on_disconnected = on_session_disconnected;
session_callbacks.on_error = on_session_error;
session_callbacks.on_reconnection_started = on_session_reconnection_started;
session_callbacks.on_reconnected = on_session_reconnected;
session = otc_session_new(API_KEY, SESSION_ID, &session_callbacks);
if (session == nullptr) {
cout << "ERROR creatng session" << endl;
}
}
void create_publisher() {
struct otc_publisher_callbacks publisher_callbacks = {0};
publisher_callbacks.on_stream_created = on_publisher_stream_created;
publisher_callbacks.on_render_frame = on_publisher_render_frame;
publisher_callbacks.on_stream_destroyed = on_publisher_stream_destroyed;
publisher_callbacks.on_error = on_publisher_error;
// Create an empty placeholder for the renderer
// This callback is not called in the main thread so we cannot
// create the renderer here
// The main loop will create a renderer for the stream
unique_ptr<Renderer> ptr;
renderer_map["PUBLISHER"] = std::move(ptr);
publisher = otc_publisher_new("name",
nullptr, /* Use WebRTC's video capturer. */
&publisher_callbacks);
if (publisher == nullptr) {
std::cout << "Error building publisher" << std::endl;
}
}
void publish() {
if(publisher != nullptr && session != nullptr) {
std::cout << "Publishing" << endl;
otc_session_publish(session, publisher);
}
}
void unpublish() {
if(publisher != nullptr && session != nullptr) {
std::cout << "Unpublishing" << endl;
otc_session_unpublish(session, publisher);
}
}
int main(int, char**)
{
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
otc_log_set_logger_callback(on_otc_log_message);
otc_log_enable(OTC_LOG_LEVEL_INFO);
// Decide GL+GLSL versions
#if __APPLE__
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// Create window with graphics context
GLFWwindow* window = glfwCreateWindow(1280, 720, "opentok linux sample app", NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Initialize OpenGL loader
bool err = glewInit() != GLEW_OK;
if (err)
{
fprintf(stderr, "Failed to initialize OpenGL loader!\n");
return 1;
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
init_ot();
// Main loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// ImGui::ShowDemoWindow(&show_demo_window);
ImGui::Begin("Control Panel");
if (ImGui::Button(ui_state.connectButtonText().c_str())) {
if (ui_state.isSessionConnected) {
cout << "Disconnecting Session" << endl;
otc_session_disconnect(session);
} else {
cout << "Connecting Session" << endl;
otc_session_connect(session, TOKEN);
create_publisher();
}
}
if (ui_state.showPublisherButtons && ImGui::Button(ui_state.publishButtonText().c_str())) {
if(!ui_state.isPublishing) {
cout << "Creating Publisher" << endl;
publish();
ui_state.isPublishing = true;
} else {
unpublish();
ui_state.isPublishing = false;
}
}
if(ui_state.showPublisherButtons && ImGui::Checkbox("Publisher Audio", &publishAudio)) {
setPublisherAudio();
}
if(ui_state.showPublisherButtons && ImGui::Checkbox("Publisher Video", &publishVideo)) {
setPublisherVideo();
}
if (ui_state.showSubscriberButtons && ImGui::Button(ui_state.subscriberButtonText().c_str())) {
if(!ui_state.isSubscribing) {
cout << "Creating Subscriber" << endl;
otc_session_subscribe(session, subscriber);
ui_state.isSubscribing = true;
} else {
cout << "Unsubscribing" << endl;
otc_session_unsubscribe(session, subscriber);
ui_state.isSubscribing = false;
}
}
if(ui_state.showSubscriberButtons && ImGui::Checkbox("Subscriber Audio", &subscriberAudio)) {
setSubscriberAudio();
}
if(ui_state.showSubscriberButtons && ImGui::Checkbox("Subscriber Video", &subscriberVideo)) {
setSubscriberVideo();
}
ImGui::End();
// Render Pub and Subs
for (auto const& el : renderer_map) {
if (el.second == nullptr) {
unique_ptr<Renderer> ptr(new Renderer(el.first));
renderer_map[el.first] = std::move(ptr);
} else {
el.second->render();
}
}
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}