-
Notifications
You must be signed in to change notification settings - Fork 45
/
gstcefsrc.cc
1360 lines (1132 loc) · 40.5 KB
/
gstcefsrc.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdio>
#include <glib.h>
#include <sstream>
#include <string>
#ifdef __APPLE__
#include <memory>
#include <string>
#include <vector>
#include <mach-o/dyld.h>
#include <dispatch/dispatch.h>
#include <CoreFoundation/CoreFoundation.h>
#endif
#include <include/base/cef_bind.h>
#include <include/base/cef_callback_helpers.h>
#include <include/wrapper/cef_closure_task.h>
#include <include/wrapper/cef_message_router.h>
#include "gstcefsrc.h"
#include "gstcefaudiometa.h"
#ifdef __APPLE__
#include "gstcefloader.h"
#include "gstcefnsapplication.h"
#endif
#define GST_ELEMENT_PROGRESS(el, type, code, text) \
G_STMT_START { \
gchar *__txt = _gst_element_error_printf text; \
gst_element_post_message (GST_ELEMENT_CAST (el), \
gst_message_new_progress (GST_OBJECT_CAST (el), \
GST_PROGRESS_TYPE_ ##type, code, __txt)); \
g_free (__txt); \
} G_STMT_END
GST_DEBUG_CATEGORY_STATIC (cef_src_debug);
#define GST_CAT_DEFAULT cef_src_debug
GST_DEBUG_CATEGORY_STATIC (cef_console_debug);
#define DEFAULT_WIDTH 1920
#define DEFAULT_HEIGHT 1080
#define DEFAULT_FPS_N 30
#define DEFAULT_FPS_D 1
#define DEFAULT_URL "https://www.google.com"
#define DEFAULT_GPU FALSE
#define DEFAULT_CHROMIUM_DEBUG_PORT -1
#define DEFAULT_LOG_SEVERITY LOGSEVERITY_INFO
#if !defined(__APPLE__) && defined(GST_CEF_USE_SANDBOX)
#define DEFAULT_SANDBOX TRUE
#else
#define DEFAULT_SANDBOX FALSE
#endif
#define DEFAULT_LISTEN_FOR_JS_SIGNALS FALSE
using CefStatus = enum : guint8 {
// CEF was either unloaded successfully or not yet loaded.
CEF_STATUS_NOT_LOADED = 0U,
// Blocks other elements from initializing CEF is it's already in progress.
CEF_STATUS_INITIALIZING = 1U << 1U,
// CEF's initialization process has completed successfully.
CEF_STATUS_INITIALIZED = 1U << 2U,
// No CEF elements will be allowed to complete initialization.
CEF_STATUS_FAILURE = 1U << 3U,
};
static CefStatus cef_status = CEF_STATUS_NOT_LOADED;
static const guint8 CEF_STATUS_MASK_INITIALIZED = CEF_STATUS_FAILURE | CEF_STATUS_INITIALIZED;
static const guint8 CEF_STATUS_MASK_TRANSITIONING = CEF_STATUS_INITIALIZING;
static GMutex init_lock;
static GCond init_cond;
#ifdef __APPLE__
// On every timeout, the CEF event handler will be run in the context
// of the main thread's Cocoa event loop.
static CFRunLoopTimerRef workTimer_ = nullptr;
#else
static GThread *thread = nullptr;
#endif
#define GST_TYPE_CEF_LOG_SEVERITY_MODE \
(gst_cef_log_severity_mode_get_type ())
static const GEnumValue log_severity_values[] = {
{LOGSEVERITY_DEBUG, "debug / verbose cef log severity", "debug"},
{LOGSEVERITY_INFO, "info cef log severity", "info"},
{LOGSEVERITY_WARNING, "warning cef log severity", "warning"},
{LOGSEVERITY_ERROR, "error cef log severity", "error"},
{LOGSEVERITY_FATAL, "fatal cef log severity", "fatal"},
{LOGSEVERITY_DISABLE, "disable cef log severity", "disable"},
{0, NULL, NULL},
};
static GType
gst_cef_log_severity_mode_get_type (void)
{
static GType type = 0;
if (!type) {
type = g_enum_register_static ("GstCefLogSeverityMode", log_severity_values);
}
return type;
}
static gint gst_cef_log_severity_from_str (const gchar *str)
{
for (guint i = 0; i < sizeof(log_severity_values) / sizeof(GEnumValue); i++) {
const gchar *nick = log_severity_values[i].value_nick;
if (!nick) break;
if (g_str_equal(str, nick)) {
return log_severity_values[i].value;
}
}
return -1;
}
enum
{
PROP_0,
PROP_URL,
PROP_GPU,
PROP_CHROMIUM_DEBUG_PORT,
PROP_CHROME_EXTRA_FLAGS,
PROP_SANDBOX,
PROP_LISTEN_FOR_JS_SIGNAL,
PROP_JS_FLAGS,
PROP_LOG_SEVERITY,
PROP_CEF_CACHE_LOCATION,
};
#define gst_cef_src_parent_class parent_class
G_DEFINE_TYPE (GstCefSrc, gst_cef_src, GST_TYPE_PUSH_SRC);
#define CEF_VIDEO_CAPS "video/x-raw, format=BGRA, width=[1, 2147483647], height=[1, 2147483647], framerate=[1/1, 60/1], pixel-aspect-ratio=1/1"
#define CEF_AUDIO_CAPS "audio/x-raw, format=F32LE, rate=[1, 2147483647], channels=[1, 2147483647], layout=interleaved"
static GstStaticPadTemplate gst_cef_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (CEF_VIDEO_CAPS)
);
gchar* get_plugin_base_path () {
GstPlugin *plugin = gst_registry_find_plugin(gst_registry_get(), "cef");
gchar* base_path = g_path_get_dirname(gst_plugin_get_filename(plugin));
gst_object_unref(plugin);
return base_path;
}
/** Cef Client */
/** Handlers */
// see https://bitbucket.org/chromiumembedded/cef-project/src/master/examples/message_router
// and https://bitbucket.org/chromiumembedded/cef/src/master/include/wrapper/cef_message_router.h
// for details of the message passing infrastructure in CEF
// Handle messages in the browser process.
class MessageHandler : public CefMessageRouterBrowserSide::Handler {
public:
explicit MessageHandler(GstCefSrc* src)
: src(src) {}
// Called due to gstSendMsg execution in ready_test.html.
bool OnQuery(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int64_t query_id,
const CefString& request,
bool persistent,
CefRefPtr<Callback> callback) override
{
if (!src) return false;
// TODO: do we want to make the incoming payload json??
bool success = false;
if (request == "ready") {
g_mutex_lock (&src->state_lock);
if (src->state == CEF_SRC_WAITING_FOR_READY) {
src->state = CEF_SRC_READY;
g_cond_broadcast (&src->state_cond);
success = true;
} else {
std::ostringstream error_msg;
error_msg << "error: (" << request << ") - " <<
"js ready signal sent with invalid cef state: " << cef_status;
GST_WARNING_OBJECT(src, "%s", error_msg.str().c_str());
success = false;
}
g_mutex_unlock (&src->state_lock);
} else if (request == "eos") {
if (src) {
gst_element_send_event(GST_ELEMENT(src), gst_event_new_eos());
success = true;
}
}
// send json response back to js
std::ostringstream response;
response <<
"{ " <<
"\"success\": \"" << (success ? "true" : "false") << "\", " <<
"\"cmd\": \"" << request << "\"" <<
" }";
if (success) {
GST_INFO_OBJECT(
src, "js signal processed successfully: %s", request.ToString().c_str()
);
callback->Success(response.str());
} else {
GST_WARNING_OBJECT(
src, "js signal processing error: %s", request.ToString().c_str()
);
callback->Failure(0, response.str());
}
return true;
}
private:
GstCefSrc* src;
DISALLOW_COPY_AND_ASSIGN(MessageHandler);
};
class RenderHandler : public CefRenderHandler
{
public:
RenderHandler(GstCefSrc *src) :
src (src)
{
}
~RenderHandler()
{
}
void GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) override
{
GST_LOG_OBJECT(src, "getting view rect");
GST_OBJECT_LOCK (src);
rect = CefRect(0, 0, src->vinfo.width ? src->vinfo.width : DEFAULT_WIDTH, src->vinfo.height ? src->vinfo.height : DEFAULT_HEIGHT);
GST_OBJECT_UNLOCK (src);
}
void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void * buffer, int w, int h) override
{
GstBuffer *new_buffer;
GST_LOG_OBJECT (src, "painting, width / height: %d %d", w, h);
new_buffer = gst_buffer_new_allocate (NULL, src->vinfo.width * src->vinfo.height * 4, NULL);
gst_buffer_fill (new_buffer, 0, buffer, w * h * 4);
GST_OBJECT_LOCK (src);
gst_buffer_replace (&(src->current_buffer), new_buffer);
gst_buffer_unref (new_buffer);
GST_OBJECT_UNLOCK (src);
GST_LOG_OBJECT (src, "done painting");
}
private:
GstCefSrc *src;
IMPLEMENT_REFCOUNTING(RenderHandler);
};
class AudioHandler : public CefAudioHandler
{
public:
AudioHandler(GstCefSrc *src) :
src (src)
{
}
~AudioHandler()
{
}
void OnAudioStreamStarted(CefRefPtr<CefBrowser> browser,
const CefAudioParameters& params,
int channels) override
{
GstStructure *s = gst_structure_new ("cef-audio-stream-start",
"channels", G_TYPE_INT, channels,
"rate", G_TYPE_INT, params.sample_rate,
nullptr);
GstEvent *event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
mRate = params.sample_rate;
mChannels = channels;
GST_OBJECT_LOCK (src);
src->audio_events = g_list_append (src->audio_events, event);
GST_OBJECT_UNLOCK (src);
}
void OnAudioStreamPacket(CefRefPtr<CefBrowser> browser,
const float** data,
int frames,
int64_t pts) override
{
GstBuffer *buf;
GstMapInfo info;
gint i, j;
GST_LOG_OBJECT (src, "Handling audio stream packet with %d frames", frames);
buf = gst_buffer_new_allocate (NULL, mChannels * frames * 4, NULL);
gst_buffer_map (buf, &info, GST_MAP_WRITE);
for (i = 0; i < mChannels; i++) {
gfloat *cdata = (gfloat *) data[i];
for (j = 0; j < frames; j++) {
memcpy (info.data + j * 4 * mChannels + i * 4, &cdata[j], 4);
}
}
gst_buffer_unmap (buf, &info);
GST_OBJECT_LOCK (src);
GST_BUFFER_DURATION (buf) = gst_util_uint64_scale (frames, GST_SECOND, mRate);
if (!src->audio_buffers) {
src->audio_buffers = gst_buffer_list_new();
}
gst_buffer_list_add (src->audio_buffers, buf);
GST_OBJECT_UNLOCK (src);
GST_LOG_OBJECT (src, "Handled audio stream packet");
}
void OnAudioStreamStopped(CefRefPtr<CefBrowser> browser) override
{
}
void OnAudioStreamError(CefRefPtr<CefBrowser> browser,
const CefString& message) override {
GST_WARNING_OBJECT (src, "Audio stream error: %s", message.ToString().c_str());
}
private:
GstCefSrc *src;
gint mRate;
gint mChannels;
IMPLEMENT_REFCOUNTING(AudioHandler);
};
class DisplayHandler : public CefDisplayHandler {
public:
DisplayHandler(GstCefSrc *src) : src(src) {}
~DisplayHandler() = default;
virtual bool OnConsoleMessage(CefRefPtr<CefBrowser>, cef_log_severity_t level, const CefString &message, const CefString &source, int line) override {
GstDebugLevel gst_level = GST_LEVEL_NONE;
switch (level) {
case LOGSEVERITY_DEFAULT:
case LOGSEVERITY_INFO:
gst_level = GST_LEVEL_INFO;
break;
case LOGSEVERITY_DEBUG:
gst_level = GST_LEVEL_DEBUG;
break;
case LOGSEVERITY_WARNING:
gst_level = GST_LEVEL_WARNING;
break;
case LOGSEVERITY_ERROR:
case LOGSEVERITY_FATAL:
gst_level = GST_LEVEL_ERROR;
break;
case LOGSEVERITY_DISABLE:
gst_level = GST_LEVEL_NONE;
break;
};
GST_CAT_LEVEL_LOG (cef_console_debug, gst_level, src, "%s:%d %s", source.ToString().c_str(), line,
message.ToString().c_str());
return false;
}
private:
GstCefSrc *src;
IMPLEMENT_REFCOUNTING(DisplayHandler);
};
class BrowserClient :
public CefClient,
public CefLifeSpanHandler,
public CefRequestHandler
{
public:
BrowserClient(GstCefSrc *src) : src(src)
{
this->render_handler = new RenderHandler(src);
this->audio_handler = new AudioHandler(src);
this->display_handler = new DisplayHandler(src);
}
// CefClient Methods:
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override
{
return this;
}
virtual CefRefPtr<CefRenderHandler> GetRenderHandler() override
{
return render_handler;
}
virtual CefRefPtr<CefAudioHandler> GetAudioHandler() override
{
return audio_handler;
}
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() override
{
return this;
}
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() override
{
return display_handler;
}
bool OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message
) override
{
CEF_REQUIRE_UI_THREAD();
return browser_msg_router_
? browser_msg_router_->OnProcessMessageReceived(
browser,
frame,
source_process,
message
)
: false;
}
// CefLifeSpanHandler Methods:
void OnAfterCreated(CefRefPtr<CefBrowser> browser) override
{
CEF_REQUIRE_UI_THREAD();
if (src->listen_for_js_signals && !browser_msg_router_) {
// Create the browser-side router for query handling.
CefMessageRouterConfig config;
config.js_query_function = "gstSendMsg";
config.js_cancel_function = "gstCancelMsg";
browser_msg_router_ = CefMessageRouterBrowserSide::Create(config);
// Register handlers with the router.
browser_msg_handler_.reset(new MessageHandler(src));
browser_msg_router_->AddHandler(browser_msg_handler_.get(), false);
}
}
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override
{
src->browser = nullptr;
g_mutex_lock (&src->state_lock);
src->state = CEF_SRC_CLOSED;
g_cond_signal (&src->state_cond);
g_mutex_unlock(&src->state_lock);
}
// CefRequestHandler methods:
bool OnBeforeBrowse(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
bool user_gesture,
bool is_redirect
) override
{
CEF_REQUIRE_UI_THREAD();
if (browser_msg_router_) browser_msg_router_->OnBeforeBrowse(browser, frame);
return false;
}
virtual void OnRenderProcessTerminated(
CefRefPtr<CefBrowser> browser,
TerminationStatus status,
int error_code,
const CefString& error_string) override
{
CEF_REQUIRE_UI_THREAD();
GST_WARNING_OBJECT (src, "Render subprocess terminated, reloading URL!");
if (browser_msg_router_) browser_msg_router_->OnRenderProcessTerminated(browser);
browser->Reload();
}
// Custom methods:
void MakeBrowser(int)
{
CefWindowInfo window_info;
CefRefPtr<CefBrowser> browser;
CefBrowserSettings browser_settings;
window_info.SetAsWindowless(0);
browser = CefBrowserHost::CreateBrowserSync(
window_info,
this,
std::string(src->url),
browser_settings,
nullptr,
nullptr
);
browser->GetHost()->SetAudioMuted(true);
src->browser = browser;
g_mutex_lock (&src->state_lock);
src->state = src->listen_for_js_signals ? CEF_SRC_WAITING_FOR_READY : CEF_SRC_OPEN;
g_cond_signal (&src->state_cond);
g_mutex_unlock(&src->state_lock);
}
private:
// Handles the browser side of query routing.
CefRefPtr<CefMessageRouterBrowserSide> browser_msg_router_;
std::unique_ptr<CefMessageRouterBrowserSide::Handler> browser_msg_handler_;
CefRefPtr<CefRenderHandler> render_handler;
CefRefPtr<CefAudioHandler> audio_handler;
CefRefPtr<CefDisplayHandler> display_handler;
public:
GstCefSrc *src;
IMPLEMENT_REFCOUNTING(BrowserClient);
};
/** Browser App methods */
BrowserApp::BrowserApp(GstCefSrc *src) : src(src)
{
}
CefRefPtr<CefBrowserProcessHandler> BrowserApp::GetBrowserProcessHandler()
{
return this;
}
#ifdef __APPLE__
void BrowserApp::OnScheduleMessagePumpWork(int64_t delay_ms)
{
static const int64_t kMaxTimerDelay = 1000.0 / 60.0;
if (workTimer_ != nullptr) {
CFRunLoopTimerInvalidate(workTimer_);
workTimer_ = nullptr;
}
if (delay_ms <= 0) {
// Execute the work immediately.
gst_cef_loop();
// Schedule more work later.
OnScheduleMessagePumpWork(kMaxTimerDelay);
} else {
int64_t timer_delay_ms = delay_ms;
// Never wait longer than the maximum allowed time.
if (timer_delay_ms > kMaxTimerDelay) timer_delay_ms = kMaxTimerDelay;
workTimer_ = gst_cef_domessagework((double)timer_delay_ms * (1.0 / 1000.0));
CFRunLoopAddTimer(CFRunLoopGetMain(), workTimer_, kCFRunLoopDefaultMode);
}
}
#endif
void BrowserApp::OnBeforeCommandLineProcessing(const CefString &process_type,
CefRefPtr<CefCommandLine> command_line)
{
command_line->AppendSwitchWithValue("autoplay-policy", "no-user-gesture-required");
command_line->AppendSwitch("enable-media-stream");
command_line->AppendSwitch("disable-dev-shm-usage"); /* https://github.com/GoogleChrome/puppeteer/issues/1834 */
command_line->AppendSwitch("enable-begin-frame-scheduling"); /* https://bitbucket.org/chromiumembedded/cef/issues/1368 */
bool gpu = src->gpu || (!!g_getenv ("GST_CEF_GPU_ENABLED"));
#ifdef __APPLE__
command_line->AppendSwitch("off-screen-rendering-enabled");
if (gpu) {
GST_WARNING_OBJECT(src, "GPU rendering is known not to work on macOS. Disabling it now. See https://github.com/chromiumembedded/cef/issues/3322 and https://magpcss.org/ceforum/viewtopic.php?f=6&t=19397");
gpu = FALSE;
}
#endif
if (!gpu) {
// Optimize for no gpu usage
command_line->AppendSwitch("disable-gpu");
command_line->AppendSwitch("disable-gpu-compositing");
}
if (src->chromium_debug_port >= 0) {
command_line->AppendSwitchWithValue("remote-debugging-port", g_strdup_printf ("%i", src->chromium_debug_port));
}
const gchar *extra_flags = src->chrome_extra_flags;
if (!extra_flags) {
extra_flags = g_getenv ("GST_CEF_CHROME_EXTRA_FLAGS");
}
if (extra_flags) {
gchar **flags_list = g_strsplit (extra_flags, ",", -1);
guint i;
for (i = 0; i < g_strv_length (flags_list); i++) {
gchar **switch_value = g_strsplit ((const gchar *) flags_list[i], "=", -1);
if (g_strv_length (switch_value) > 1) {
GST_INFO_OBJECT (src, "Adding switch with value %s=%s", switch_value[0], switch_value[1]);
command_line->AppendSwitchWithValue (switch_value[0], switch_value[1]);
} else {
GST_INFO_OBJECT (src, "Adding flag %s", flags_list[i]);
command_line->AppendSwitch (flags_list[i]);
}
g_strfreev (switch_value);
}
g_strfreev (flags_list);
}
}
/** cefsrc (Gstreamer) methods */
static GstFlowReturn gst_cef_src_create(GstPushSrc *push_src, GstBuffer **buf)
{
GstCefSrc *src = GST_CEF_SRC (push_src);
GList *tmp;
GST_OBJECT_LOCK (src);
if (src->audio_events) {
for (tmp = src->audio_events; tmp; tmp = tmp->next) {
gst_pad_push_event (GST_BASE_SRC_PAD (src), (GstEvent *) tmp->data);
}
g_list_free (src->audio_events);
src->audio_events = NULL;
}
g_assert (src->current_buffer);
*buf = gst_buffer_copy (src->current_buffer);
if (src->audio_buffers) {
gst_buffer_add_cef_audio_meta (*buf, src->audio_buffers);
src->audio_buffers = NULL;
}
GST_BUFFER_PTS (*buf) = gst_util_uint64_scale (src->n_frames, src->vinfo.fps_d * GST_SECOND, src->vinfo.fps_n);
GST_BUFFER_DURATION (*buf) = gst_util_uint64_scale (GST_SECOND, src->vinfo.fps_d, src->vinfo.fps_n);
src->n_frames++;
GST_OBJECT_UNLOCK (src);
return GST_FLOW_OK;
}
/* Once we have started a first cefsrc for this process, we start
* a UI thread and never shut it down. We could probably refine this
* to stop and restart the thread as needed, but this updated approach
* now no longer requires a main loop to be running, doesn't crash
* when one is running either with CEF 86+, and allows for multiple
* concurrent cefsrc instances.
*/
static gpointer
init_cef (GstCefSrc *src)
{
#ifdef G_OS_WIN32
HINSTANCE hInstance = GetModuleHandle(NULL);
CefMainArgs args(hInstance);
#else
CefMainArgs args(0, NULL);
#endif
CefSettings settings;
CefRefPtr<BrowserApp> app;
CefWindowInfo window_info;
CefBrowserSettings browserSettings;
// pull in parameters from gst properties (deprecated) or environment
cef_log_severity_t log_severity = src->log_severity;
const gchar* log_severity_env = g_getenv ("GST_CEF_LOG_SEVERITY");
if (log_severity_env) {
gint severity = gst_cef_log_severity_from_str(log_severity_env);
if (severity >= 0) {
log_severity = (cef_log_severity_t) severity;
}
}
const gchar *js_flags = src->js_flags;
if (!js_flags) {
js_flags = g_getenv ("GST_CEF_JS_FLAGS");
}
const gchar *cef_cache_location = src->cef_cache_location;
if (!cef_cache_location) {
cef_cache_location = g_getenv ("GST_CEF_CACHE_LOCATION");
}
bool sandbox = src->gpu || (!!g_getenv ("GST_CEF_SANDBOX"));
settings.no_sandbox = !sandbox;
settings.windowless_rendering_enabled = true;
settings.log_severity = log_severity;
settings.multi_threaded_message_loop = false;
#ifdef __APPLE__
settings.external_message_pump = true;
#endif
GST_INFO_OBJECT(src, "Initializing CEF");
gchar* base_path = get_plugin_base_path();
// If not absolute path append to current_dir
if (!g_path_is_absolute(base_path)) {
gchar* current_dir = g_get_current_dir();
gchar* old_base_path = base_path;
base_path = g_build_filename(current_dir, base_path, nullptr);
g_free(current_dir);
g_free(old_base_path);
}
#ifdef __APPLE__
gchar* browser_subprocess_path = g_build_filename(base_path, "gstcefsubprocess.app/Contents/MacOS/gstcefsubprocess", nullptr);
#else
gchar* browser_subprocess_path = g_build_filename(base_path, "gstcefsubprocess", nullptr);
#endif
if (const gchar *custom_subprocess_path = g_getenv ("GST_CEF_SUBPROCESS_PATH")) {
g_setenv ("CEF_SUBPROCESS_PATH", browser_subprocess_path, TRUE);
g_free (browser_subprocess_path);
browser_subprocess_path = g_strdup (custom_subprocess_path);
}
GST_DEBUG_OBJECT(src, "CEF subprocess: %s", browser_subprocess_path);
CefString(&settings.browser_subprocess_path).FromASCII(browser_subprocess_path);
g_free(browser_subprocess_path);
#ifdef __APPLE__
const std::string framework_folder = []() {
std::string framework = gst_cef_get_framework_path(false);
const auto split = framework.find_last_of('/');
return framework.substr(0, split);
}();
GST_DEBUG_OBJECT(src, "CEF framework_dir_path: %s", framework_folder.c_str());
CefString(&settings.framework_dir_path).FromString(framework_folder);
const std::string main_bundle_folder = [&](){
uint32_t size = 0;
_NSGetExecutablePath(nullptr, &size);
std::vector<char> host(size);
_NSGetExecutablePath(host.data(), &size);
auto host2 = std::unique_ptr<char>(realpath(host.data(), nullptr));
GST_DEBUG_OBJECT(src, "Main executable path: %s", host2.get());
assert(size != 0);
std::string host3(host2.get());
const auto split = host3.find("Contents/MacOS");
assert(split != std::string::npos);
return host3.substr(0, split);
}();
CefString(&settings.main_bundle_path).FromString(main_bundle_folder);
#endif
gchar *locales_dir_path = g_build_filename(base_path, "locales", nullptr);
CefString(&settings.locales_dir_path).FromASCII(locales_dir_path);
if (js_flags != NULL) {
CefString(&settings.javascript_flags).FromASCII(js_flags);
}
if (cef_cache_location != NULL) {
CefString(&settings.cache_path).FromASCII(cef_cache_location);
}
g_free(base_path);
g_free(locales_dir_path);
app = new BrowserApp(src);
if (!CefInitialize(args, settings, app, nullptr)) {
GST_ERROR ("Failed to initialize CEF");
/* unblock start () */
g_mutex_lock (&init_lock);
cef_status = CEF_STATUS_FAILURE;
g_cond_broadcast (&init_cond);
g_mutex_unlock (&init_lock);
goto done;
}
g_mutex_lock (&init_lock);
cef_status = CEF_STATUS_INITIALIZED;
g_cond_broadcast (&init_cond);
g_mutex_unlock (&init_lock);
#ifndef __APPLE__
CefRunMessageLoop();
#endif
done:
return NULL;
}
static GstStateChangeReturn
gst_cef_src_change_state(GstElement *src, GstStateChange transition)
{
GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
switch(transition)
{
case GST_STATE_CHANGE_NULL_TO_READY:
{
g_mutex_lock (&init_lock);
// Wait till a previous CEF is dismantled or completes initialization
while (cef_status & CEF_STATUS_MASK_TRANSITIONING)
g_cond_wait (&init_cond, &init_lock);
if (cef_status == CEF_STATUS_FAILURE) {
// BAIL OUT, CEF is not loaded.
result = GST_STATE_CHANGE_FAILURE;
} else if (cef_status == CEF_STATUS_NOT_LOADED) {
cef_status = CEF_STATUS_INITIALIZING;
/* Initialize Chromium Embedded Framework */
#ifdef __APPLE__
/* in the main thread as per Cocoa */
if (pthread_main_np()) {
g_mutex_unlock (&init_lock);
init_cef ((GstCefSrc*) src);
g_mutex_lock (&init_lock);
} else {
dispatch_async_f(dispatch_get_main_queue(), (GstCefSrc*)src, (dispatch_function_t)&init_cef);
while (cef_status == CEF_STATUS_INITIALIZING)
g_cond_wait (&init_cond, &init_lock);
}
#else
/* in a separate UI thread */
thread = g_thread_new("cef-ui-thread", (GThreadFunc) init_cef, (GstCefSrc*)src);
while (cef_status == CEF_STATUS_INITIALIZING)
g_cond_wait (&init_cond, &init_lock);
#endif
if (cef_status & ~CEF_STATUS_MASK_INITIALIZED) {
// BAIL OUT, CEF is not loaded.
result = GST_STATE_CHANGE_FAILURE;
#ifndef __APPLE__
g_thread_join(thread);
thread = nullptr;
#endif
}
}
g_mutex_unlock(&init_lock);
break;
}
default:
break;
}
if (result == GST_STATE_CHANGE_FAILURE) return result;
result = GST_ELEMENT_CLASS(parent_class)->change_state(src, transition);
return result;
}
static gboolean
gst_cef_src_start(GstBaseSrc *base_src)
{
gboolean ret = FALSE;
GstCefSrc *src = GST_CEF_SRC (base_src);
GST_ELEMENT_PROGRESS(src, START, "open", ("Creating CEF browser client"));
CefRefPtr<BrowserClient> browserClient = new BrowserClient(src);
/* Make sure CEF is initialized before posting a task */
g_mutex_lock (&init_lock);
while (cef_status & ~CEF_STATUS_MASK_INITIALIZED)
g_cond_wait (&init_cond, &init_lock);
g_mutex_unlock (&init_lock);
if (cef_status == CEF_STATUS_FAILURE) {
GST_ELEMENT_PROGRESS(src, ERROR, "open", ("CEF in failed state"));
goto done;
}
GST_OBJECT_LOCK (src);
src->n_frames = 0;
GST_OBJECT_UNLOCK (src);
GST_ELEMENT_PROGRESS(src, CONTINUE, "open", ("Creating CEF browser ..."));
#ifdef __APPLE__
if (pthread_main_np()) {
/* in the main thread as per Cocoa */
browserClient->MakeBrowser(0);
} else {
#endif
CefPostTask(TID_UI, base::BindOnce(&BrowserClient::MakeBrowser, browserClient.get(), 0));
/* And wait for this src's browser to have been created */
GST_ELEMENT_PROGRESS(src, CONTINUE, "open", ("Waiting for CEF browser initialization..."));
g_mutex_lock(&src->state_lock);
while (!CefSrcStateIsOpen(src->state))
g_cond_wait (&src->state_cond, &src->state_lock);
g_mutex_unlock (&src->state_lock);
#ifdef __APPLE__
}
#endif
if (src->listen_for_js_signals) {
g_mutex_lock (&src->state_lock);
while (src->state == CEF_SRC_WAITING_FOR_READY)
g_cond_wait (&src->state_cond, &src->state_lock);
g_mutex_unlock (&src->state_lock);
}
ret = src->browser != NULL;
if (ret) {
GST_ELEMENT_PROGRESS(
src, COMPLETE, "open",
("CEF browser created")
);
} else {
GST_ELEMENT_PROGRESS(
src, ERROR, "open",
("CEF browser failed to create")
);
}
done:
return ret;
}
static void
gst_cef_src_close_browser(GstCefSrc *src)
{
src->browser->GetHost()->CloseBrowser(true);
}
static gboolean
gst_cef_src_stop (GstBaseSrc *base_src)
{
GstCefSrc *src = GST_CEF_SRC (base_src);
GST_INFO_OBJECT (src, "Stopping");
if (src->browser) {
gst_cef_src_close_browser(src);
#ifdef __APPLE__
if (!pthread_main_np()) {
#endif
/* And wait for this src's browser to have been closed */
g_mutex_lock(&src->state_lock);
while (CefSrcStateIsOpen(src->state))
g_cond_wait (&src->state_cond, &src->state_lock);
g_mutex_unlock (&src->state_lock);
#ifdef __APPLE__
}
#endif
}
gst_buffer_replace (&src->current_buffer, NULL);
return TRUE;
}
static void
gst_cef_src_get_times (GstBaseSrc * base_src, GstBuffer * buffer,
GstClockTime * start, GstClockTime * end)
{
GstClockTime timestamp = GST_BUFFER_PTS (buffer);
GstClockTime duration = GST_BUFFER_DURATION (buffer);
*end = timestamp + duration;
*start = timestamp;