forked from opentok/opentok-linux-sdk-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
169 lines (136 loc) · 5.92 KB
/
Copy pathmain.cpp
File metadata and controls
169 lines (136 loc) · 5.92 KB
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
#include <opentok.h>
#include <atomic>
#include <cstdlib>
#include <iostream>
#include "config.h"
#include "renderer.h"
static std::atomic<bool> g_is_connected(false);
static otc_publisher *g_publisher = nullptr;
static std::atomic<bool> g_is_publishing(false);
static void on_session_connected(otc_session *session, void *user_data) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
g_is_connected = true;
if ((session != nullptr) && (g_publisher != nullptr)) {
if (otc_session_publish(session, g_publisher) == OTC_SUCCESS) {
g_is_publishing = true;
return;
}
std::cout << "Could not publish successfully" << std::endl;
}
}
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;
}
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;
}
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;
}
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) {
RendererManager *render_manager = static_cast<RendererManager*>(user_data);
if (render_manager == nullptr) {
return;
}
render_manager->addFrame(publisher, frame);
}
static void on_publisher_stream_destroyed(otc_publisher *publisher,
void *user_data,
const otc_stream *stream) {
std::cout << __FUNCTION__ << " callback function" << std::endl;
}
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;
}
static void on_otc_log_message(const char* message) {
std::cout << __FUNCTION__ << ":" << message << std::endl;
}
int main(int argc, char** argv) {
if (otc_init(nullptr) != OTC_SUCCESS) {
std::cout << "Could not init OpenTok library" << std::endl;
return EXIT_FAILURE;
}
#ifdef CONSOLE_LOGGING
otc_log_set_logger_callback(on_otc_log_message);
otc_log_enable(OTC_LOG_LEVEL_ALL);
#endif
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;
otc_session *session = nullptr;
session = otc_session_new(API_KEY, SESSION_ID, &session_callbacks);
if (session == nullptr) {
std::cout << "Could not create OpenTok session successfully" << std::endl;
return EXIT_FAILURE;
}
RendererManager renderer_manager;
struct otc_publisher_callbacks publisher_callbacks = {0};
publisher_callbacks.user_data = &renderer_manager;
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;
g_publisher = otc_publisher_new("opentok-linux-sdk-samples",
nullptr, /* Use WebRTC's video capturer. */
&publisher_callbacks);
if (g_publisher == nullptr) {
std::cout << "Could not create OpenTok publisher successfully" << std::endl;
otc_session_delete(session);
return EXIT_FAILURE;
}
renderer_manager.createRenderer(g_publisher);
otc_session_connect(session, TOKEN);
renderer_manager.runEventLoop();
renderer_manager.destroyRenderer(g_publisher);
if ((session != nullptr) && (g_publisher != nullptr) && g_is_publishing.load()) {
otc_session_unpublish(session, g_publisher);
}
if (g_publisher != nullptr) {
otc_publisher_delete(g_publisher);
}
if ((session != nullptr) && g_is_connected.load()) {
otc_session_disconnect(session);
}
if (session != nullptr) {
otc_session_delete(session);
}
otc_destroy();
return EXIT_SUCCESS;
}