forked from obsproject/obs-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-client.cpp
399 lines (339 loc) · 9.78 KB
/
browser-client.cpp
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
/******************************************************************************
Copyright (C) 2014 by John R. Bradley <[email protected]>
Copyright (C) 2018 by Hugh Bailey ("Jim") <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "browser-client.hpp"
#include "obs-browser-source.hpp"
#include "base64/base64.hpp"
#include "json11/json11.hpp"
#include <obs-frontend-api.h>
#include <obs.hpp>
#include <util/platform.h>
using namespace json11;
BrowserClient::~BrowserClient()
{
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED && USE_TEXTURE_COPY
if (sharing_available) {
obs_enter_graphics();
gs_texture_destroy(texture);
obs_leave_graphics();
}
#endif
}
CefRefPtr<CefLoadHandler> BrowserClient::GetLoadHandler()
{
return this;
}
CefRefPtr<CefRenderHandler> BrowserClient::GetRenderHandler()
{
return this;
}
CefRefPtr<CefDisplayHandler> BrowserClient::GetDisplayHandler()
{
return this;
}
CefRefPtr<CefLifeSpanHandler> BrowserClient::GetLifeSpanHandler()
{
return this;
}
CefRefPtr<CefContextMenuHandler> BrowserClient::GetContextMenuHandler()
{
return this;
}
#if CHROME_VERSION_BUILD >= 3683
CefRefPtr<CefAudioHandler> BrowserClient::GetAudioHandler()
{
return this;
}
#endif
bool BrowserClient::OnBeforePopup(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
const CefString &, const CefString &,
WindowOpenDisposition, bool,
const CefPopupFeatures &, CefWindowInfo &,
CefRefPtr<CefClient> &, CefBrowserSettings &,
#if CHROME_VERSION_BUILD >= 3770
CefRefPtr<CefDictionaryValue> &,
#endif
bool *)
{
/* block popups */
return true;
}
void BrowserClient::OnBeforeContextMenu(CefRefPtr<CefBrowser>,
CefRefPtr<CefFrame>,
CefRefPtr<CefContextMenuParams>,
CefRefPtr<CefMenuModel> model)
{
/* remove all context menu contributions */
model->Clear();
}
bool BrowserClient::OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
#if CHROME_VERSION_BUILD >= 3770
CefRefPtr<CefFrame>,
#endif
CefProcessId, CefRefPtr<CefProcessMessage> message)
{
const std::string &name = message->GetName();
Json json;
if (!bs) {
return false;
}
if (name == "getCurrentScene") {
OBSSource current_scene = obs_frontend_get_current_scene();
obs_source_release(current_scene);
if (!current_scene)
return false;
const char *name = obs_source_get_name(current_scene);
if (!name)
return false;
json = Json::object{
{"name", name},
{"width", (int)obs_source_get_width(current_scene)},
{"height", (int)obs_source_get_height(current_scene)}};
} else if (name == "getStatus") {
json = Json::object{
{"recording", obs_frontend_recording_active()},
{"streaming", obs_frontend_streaming_active()},
{"replaybuffer", obs_frontend_replay_buffer_active()}};
} else {
return false;
}
CefRefPtr<CefProcessMessage> msg =
CefProcessMessage::Create("executeCallback");
CefRefPtr<CefListValue> args = msg->GetArgumentList();
args->SetInt(0, message->GetArgumentList()->GetInt(0));
args->SetString(1, json.dump());
SendBrowserProcessMessage(browser, PID_RENDERER, msg);
return true;
}
#if CHROME_VERSION_BUILD >= 3578
void BrowserClient::GetViewRect(
#else
bool BrowserClient::GetViewRect(
#endif
CefRefPtr<CefBrowser>, CefRect &rect)
{
if (!bs) {
#if CHROME_VERSION_BUILD >= 3578
rect.Set(0, 0, 16, 16);
return;
#else
return false;
#endif
}
rect.Set(0, 0, bs->width, bs->height);
#if CHROME_VERSION_BUILD >= 3578
return;
#else
return true;
#endif
}
void BrowserClient::OnPaint(CefRefPtr<CefBrowser>, PaintElementType type,
const RectList &, const void *buffer, int width,
int height)
{
if (type != PET_VIEW) {
return;
}
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
if (sharing_available) {
return;
}
#endif
if (!bs) {
return;
}
if (bs->width != width || bs->height != height) {
obs_enter_graphics();
bs->DestroyTextures();
obs_leave_graphics();
}
if (!bs->texture && width && height) {
obs_enter_graphics();
bs->texture = gs_texture_create(width, height, GS_BGRA, 1,
(const uint8_t **)&buffer,
GS_DYNAMIC);
bs->width = width;
bs->height = height;
obs_leave_graphics();
} else {
obs_enter_graphics();
gs_texture_set_image(bs->texture, (const uint8_t *)buffer,
width * 4, false);
obs_leave_graphics();
}
}
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
void BrowserClient::OnAcceleratedPaint(CefRefPtr<CefBrowser>, PaintElementType,
const RectList &, void *shared_handle)
{
if (!bs) {
return;
}
if (shared_handle != last_handle) {
obs_enter_graphics();
#if USE_TEXTURE_COPY
gs_texture_destroy(texture);
texture = nullptr;
#endif
gs_texture_destroy(bs->texture);
bs->texture = nullptr;
#if USE_TEXTURE_COPY
texture = gs_texture_open_shared(
(uint32_t)(uintptr_t)shared_handle);
uint32_t cx = gs_texture_get_width(texture);
uint32_t cy = gs_texture_get_height(texture);
gs_color_format format = gs_texture_get_color_format(texture);
bs->texture = gs_texture_create(cx, cy, format, 1, nullptr, 0);
#else
bs->texture = gs_texture_open_shared(
(uint32_t)(uintptr_t)shared_handle);
#endif
obs_leave_graphics();
last_handle = shared_handle;
}
#if USE_TEXTURE_COPY
if (texture && bs->texture) {
obs_enter_graphics();
gs_copy_texture(bs->texture, texture);
obs_leave_graphics();
}
#endif
}
#endif
#if CHROME_VERSION_BUILD >= 3683
static speaker_layout GetSpeakerLayout(CefAudioHandler::ChannelLayout cefLayout)
{
switch (cefLayout) {
case CEF_CHANNEL_LAYOUT_MONO:
return SPEAKERS_MONO; /**< Channels: MONO */
case CEF_CHANNEL_LAYOUT_STEREO:
return SPEAKERS_STEREO; /**< Channels: FL, FR */
case CEF_CHANNEL_LAYOUT_2POINT1:
return SPEAKERS_2POINT1; /**< Channels: FL, FR, LFE */
case CEF_CHANNEL_LAYOUT_2_2:
case CEF_CHANNEL_LAYOUT_QUAD:
case CEF_CHANNEL_LAYOUT_4_0:
return SPEAKERS_4POINT0; /**< Channels: FL, FR, FC, RC */
case CEF_CHANNEL_LAYOUT_4_1:
return SPEAKERS_4POINT1; /**< Channels: FL, FR, FC, LFE, RC */
case CEF_CHANNEL_LAYOUT_5_1:
case CEF_CHANNEL_LAYOUT_5_1_BACK:
return SPEAKERS_5POINT1; /**< Channels: FL, FR, FC, LFE, RL, RR */
case CEF_CHANNEL_LAYOUT_7_1:
case CEF_CHANNEL_LAYOUT_7_1_WIDE_BACK:
case CEF_CHANNEL_LAYOUT_7_1_WIDE:
return SPEAKERS_7POINT1; /**< Channels: FL, FR, FC, LFE, RL, RR, SL, SR */
}
return SPEAKERS_UNKNOWN;
}
void BrowserClient::OnAudioStreamStarted(CefRefPtr<CefBrowser> browser, int id,
int, ChannelLayout channel_layout,
int sample_rate, int)
{
if (!bs) {
return;
}
AudioStream &stream = bs->audio_streams[id];
if (!stream.source) {
stream.source = obs_source_create_private("audio_line", nullptr,
nullptr);
obs_source_release(stream.source);
obs_source_add_active_child(bs->source, stream.source);
std::lock_guard<std::mutex> lock(bs->audio_sources_mutex);
bs->audio_sources.push_back(stream.source);
}
stream.speakers = GetSpeakerLayout(channel_layout);
stream.channels = get_audio_channels(stream.speakers);
stream.sample_rate = sample_rate;
}
void BrowserClient::OnAudioStreamPacket(CefRefPtr<CefBrowser> browser, int id,
const float **data, int frames,
int64_t pts)
{
if (!bs) {
return;
}
AudioStream &stream = bs->audio_streams[id];
struct obs_source_audio audio = {};
const uint8_t **pcm = (const uint8_t **)data;
for (int i = 0; i < stream.channels; i++)
audio.data[i] = pcm[i];
audio.samples_per_sec = stream.sample_rate;
audio.frames = frames;
audio.format = AUDIO_FORMAT_FLOAT_PLANAR;
audio.speakers = stream.speakers;
audio.timestamp = (uint64_t)pts * 1000000LLU;
obs_source_output_audio(stream.source, &audio);
}
void BrowserClient::OnAudioStreamStopped(CefRefPtr<CefBrowser> browser, int id)
{
if (!bs) {
return;
}
auto pair = bs->audio_streams.find(id);
if (pair == bs->audio_streams.end()) {
return;
}
AudioStream &stream = pair->second;
{
std::lock_guard<std::mutex> lock(bs->audio_sources_mutex);
for (size_t i = 0; i < bs->audio_sources.size(); i++) {
obs_source_t *source = bs->audio_sources[i];
if (source == stream.source) {
bs->audio_sources.erase(
bs->audio_sources.begin() + i);
break;
}
}
}
bs->audio_streams.erase(pair);
}
#endif
void BrowserClient::OnLoadEnd(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame> frame,
int)
{
if (!bs) {
return;
}
if (frame->IsMain()) {
std::string base64EncodedCSS = base64_encode(bs->css);
std::string href;
href += "data:text/css;charset=utf-8;base64,";
href += base64EncodedCSS;
std::string script;
script += "var link = document.createElement('link');";
script += "link.setAttribute('rel', 'stylesheet');";
script += "link.setAttribute('type', 'text/css');";
script += "link.setAttribute('href', '" + href + "');";
script +=
"document.getElementsByTagName('head')[0].appendChild(link);";
frame->ExecuteJavaScript(script, href, 0);
}
}
bool BrowserClient::OnConsoleMessage(CefRefPtr<CefBrowser>,
#if CHROME_VERSION_BUILD >= 3282
cef_log_severity_t level,
#endif
const CefString &message,
const CefString &source, int line)
{
#if CHROME_VERSION_BUILD >= 3282
if (level < LOGSEVERITY_ERROR)
return false;
#endif
blog(LOG_INFO, "obs-browser: %s (source: %s:%d)",
message.ToString().c_str(), source.ToString().c_str(), line);
return false;
}