2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "html_producer.h"
24 #include <core/video_format.h>
26 #include <core/monitor/monitor.h>
27 #include <core/frame/draw_frame.h>
28 #include <core/frame/frame_factory.h>
29 #include <core/producer/frame_producer.h>
30 #include <core/interaction/interaction_event.h>
31 #include <core/frame/frame.h>
32 #include <core/frame/pixel_format.h>
33 #include <core/frame/audio_channel_layout.h>
34 #include <core/frame/geometry.h>
35 #include <core/help/help_repository.h>
36 #include <core/help/help_sink.h>
38 #include <common/assert.h>
39 #include <common/env.h>
40 #include <common/executor.h>
41 #include <common/lock.h>
42 #include <common/future.h>
43 #include <common/diagnostics/graph.h>
44 #include <common/prec_timer.h>
45 #include <common/linq.h>
46 #include <common/os/filesystem.h>
47 #include <common/memcpy.h>
49 #include <boost/algorithm/string/predicate.hpp>
50 #include <boost/filesystem.hpp>
51 #include <boost/timer.hpp>
52 #include <boost/log/trivial.hpp>
53 #include <boost/property_tree/ptree.hpp>
55 #include <tbb/atomic.h>
56 #include <tbb/concurrent_queue.h>
59 #pragma warning(disable: 4458)
62 #include <cef_client.h>
63 #include <cef_render_handler.h>
72 #pragma comment (lib, "libcef.lib")
73 #pragma comment (lib, "libcef_dll_wrapper.lib")
75 namespace caspar { namespace html {
79 , public CefRenderHandler
80 , public CefLifeSpanHandler
81 , public CefLoadHandler
84 spl::shared_ptr<diagnostics::graph> graph_;
85 boost::timer tick_timer_;
86 boost::timer frame_timer_;
87 boost::timer paint_timer_;
89 spl::shared_ptr<core::frame_factory> frame_factory_;
90 core::video_format_desc format_desc_;
91 tbb::concurrent_queue<std::wstring> javascript_before_load_;
92 tbb::atomic<bool> loaded_;
93 tbb::atomic<bool> removed_;
94 std::queue<core::draw_frame> frames_;
95 mutable boost::mutex frames_mutex_;
97 core::draw_frame last_frame_;
98 core::draw_frame last_progressive_frame_;
99 mutable boost::mutex last_frame_mutex_;
101 CefRefPtr<CefBrowser> browser_;
108 spl::shared_ptr<core::frame_factory> frame_factory,
109 const core::video_format_desc& format_desc,
110 const std::wstring& url)
112 , frame_factory_(std::move(frame_factory))
113 , format_desc_(format_desc)
114 , last_frame_(core::draw_frame::empty())
115 , last_progressive_frame_(core::draw_frame::empty())
116 , executor_(L"html_producer")
118 graph_->set_color("browser-tick-time", diagnostics::color(0.1f, 1.0f, 0.1f));
119 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
120 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
121 graph_->set_text(print());
122 diagnostics::register_graph(graph_);
126 executor_.begin_invoke([&]{ update(); });
129 core::draw_frame receive()
131 auto frame = last_frame();
132 executor_.begin_invoke([&]{ update(); });
136 core::draw_frame last_frame() const
138 return lock(last_frame_mutex_, [&]
144 void execute_javascript(const std::wstring& javascript)
148 javascript_before_load_.push(javascript);
152 execute_queued_javascript();
153 do_execute_javascript(javascript);
157 CefRefPtr<CefBrowserHost> get_browser_host()
159 return browser_->GetHost();
166 if (browser_ != nullptr)
168 browser_->GetHost()->CloseBrowser(true);
179 bool is_removed() const
186 bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect)
188 CASPAR_ASSERT(CefCurrentlyOn(TID_UI));
190 rect = CefRect(0, 0, format_desc_.square_width, format_desc_.square_height);
195 CefRefPtr<CefBrowser> browser,
196 PaintElementType type,
197 const RectList &dirtyRects,
202 graph_->set_value("browser-tick-time", paint_timer_.elapsed()
204 * format_desc_.field_count
206 paint_timer_.restart();
207 CASPAR_ASSERT(CefCurrentlyOn(TID_UI));
209 core::pixel_format_desc pixel_desc;
210 pixel_desc.format = core::pixel_format::bgra;
211 pixel_desc.planes.push_back(
212 core::pixel_format_desc::plane(width, height, 4));
213 auto frame = frame_factory_->create_frame(this, pixel_desc, core::audio_channel_layout::invalid());
214 fast_memcpy(frame.image_data().begin(), buffer, width * height * 4);
216 lock(frames_mutex_, [&]
218 frames_.push(core::draw_frame(std::move(frame)));
220 size_t max_in_queue = format_desc_.field_count + 1;
222 while (frames_.size() > max_in_queue)
225 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
230 void OnAfterCreated(CefRefPtr<CefBrowser> browser) override
232 CASPAR_ASSERT(CefCurrentlyOn(TID_UI));
237 void OnBeforeClose(CefRefPtr<CefBrowser> browser) override
239 CASPAR_ASSERT(CefCurrentlyOn(TID_UI));
244 bool DoClose(CefRefPtr<CefBrowser> browser) override
246 CASPAR_ASSERT(CefCurrentlyOn(TID_UI));
251 CefRefPtr<CefRenderHandler> GetRenderHandler() override
256 CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override
261 CefRefPtr<CefLoadHandler> GetLoadHandler() override {
266 CefRefPtr<CefBrowser> browser,
267 CefRefPtr<CefFrame> frame,
268 int httpStatusCode) override
271 execute_queued_javascript();
274 bool OnProcessMessageReceived(
275 CefRefPtr<CefBrowser> browser,
276 CefProcessId source_process,
277 CefRefPtr<CefProcessMessage> message) override
279 auto name = message->GetName().ToString();
281 if (name == REMOVE_MESSAGE_NAME)
287 else if (name == LOG_MESSAGE_NAME)
289 auto args = message->GetArgumentList();
291 static_cast<boost::log::trivial::severity_level>(args->GetInt(0));
292 auto msg = args->GetString(1).ToWString();
294 BOOST_LOG_STREAM_WITH_PARAMS(
296 (boost::log::keywords::severity = severity))
297 << print() << L" [renderer_process] " << msg;
303 void invoke_requested_animation_frames()
306 browser_->SendProcessMessage(
307 CefProcessId::PID_RENDERER,
308 CefProcessMessage::Create(TICK_MESSAGE_NAME));
310 graph_->set_value("tick-time", tick_timer_.elapsed()
312 * format_desc_.field_count
314 tick_timer_.restart();
317 bool try_pop(core::draw_frame& result)
319 return lock(frames_mutex_, [&]() -> bool
321 if (!frames_.empty())
323 result = std::move(frames_.front());
333 core::draw_frame pop()
335 core::draw_frame frame;
339 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(u8(print()) + " No frame in buffer"));
347 invoke_requested_animation_frames();
352 auto num_frames = lock(frames_mutex_, [&]
354 return frames_.size();
357 if (num_frames >= format_desc_.field_count)
359 if (format_desc_.field_mode != core::field_mode::progressive)
363 executor_.yield(caspar::task_priority::lowest_priority);
364 timer.tick(1.0 / (format_desc_.fps * format_desc_.field_count));
365 invoke_requested_animation_frames();
369 lock(last_frame_mutex_, [&]
371 last_progressive_frame_ = frame2;
372 last_frame_ = core::draw_frame::interlace(frame1, frame2, format_desc_.field_mode);
379 lock(last_frame_mutex_, [&]
385 else if (num_frames == 1) // Interlaced but only one frame
386 { // available. Probably the last frame
387 // of some animation sequence.
390 lock(last_frame_mutex_, [&]
392 last_progressive_frame_ = frame;
396 timer.tick(1.0 / (format_desc_.fps * format_desc_.field_count));
397 invoke_requested_animation_frames();
401 if (format_desc_.field_mode != core::field_mode::progressive)
402 lock(last_frame_mutex_, [&]
404 last_frame_ = last_progressive_frame_;
409 void do_execute_javascript(const std::wstring& javascript)
411 html::begin_invoke([=]
413 if (browser_ != nullptr)
414 browser_->GetMainFrame()->ExecuteJavaScript(u8(javascript).c_str(), browser_->GetMainFrame()->GetURL(), 0);
418 void execute_queued_javascript()
420 std::wstring javascript;
422 while (javascript_before_load_.try_pop(javascript))
423 do_execute_javascript(javascript);
426 std::wstring print() const
428 return L"html[" + url_ + L"]";
431 IMPLEMENT_REFCOUNTING(html_client);
435 : public core::frame_producer_base
437 core::monitor::subject monitor_subject_;
438 const std::wstring url_;
439 core::constraints constraints_;
441 CefRefPtr<html_client> client_;
445 const spl::shared_ptr<core::frame_factory>& frame_factory,
446 const core::video_format_desc& format_desc,
447 const std::wstring& url)
450 constraints_.width.set(format_desc.square_width);
451 constraints_.height.set(format_desc.square_height);
455 client_ = new html_client(frame_factory, format_desc, url_);
457 CefWindowInfo window_info;
459 window_info.SetTransparentPainting(true);
460 window_info.SetAsOffScreen(nullptr);
461 //window_info.SetAsWindowless(nullptr, true);
463 CefBrowserSettings browser_settings;
464 browser_settings.web_security = cef_state_t::STATE_DISABLED;
465 CefBrowserHost::CreateBrowser(window_info, client_.get(), url, browser_settings, nullptr);
477 std::wstring name() const override
482 void on_interaction(const core::interaction_event::ptr& event) override
484 if (core::is<core::mouse_move_event>(event))
486 auto move = core::as<core::mouse_move_event>(event);
487 int x = static_cast<int>(move->x * constraints_.width.get());
488 int y = static_cast<int>(move->y * constraints_.height.get());
493 client_->get_browser_host()->SendMouseMoveEvent(e, false);
495 else if (core::is<core::mouse_button_event>(event))
497 auto button = core::as<core::mouse_button_event>(event);
498 int x = static_cast<int>(button->x * constraints_.width.get());
499 int y = static_cast<int>(button->y * constraints_.height.get());
504 client_->get_browser_host()->SendMouseClickEvent(
506 static_cast<CefBrowserHost::MouseButtonType>(button->button),
510 else if (core::is<core::mouse_wheel_event>(event))
512 auto wheel = core::as<core::mouse_wheel_event>(event);
513 int x = static_cast<int>(wheel->x * constraints_.width.get());
514 int y = static_cast<int>(wheel->y * constraints_.height.get());
519 static const int WHEEL_TICKS_AMPLIFICATION = 40;
520 client_->get_browser_host()->SendMouseWheelEvent(
523 wheel->ticks_delta * WHEEL_TICKS_AMPLIFICATION); // delta_y
527 bool collides(double x, double y) const override
532 core::draw_frame receive_impl() override
536 if (client_->is_removed())
539 return core::draw_frame::empty();
542 return client_->receive();
545 return core::draw_frame::empty();
548 std::future<std::wstring> call(const std::vector<std::wstring>& params) override
551 return make_ready_future(std::wstring(L""));
553 auto javascript = params.at(0);
555 client_->execute_javascript(javascript);
557 return make_ready_future(std::wstring(L""));
560 std::wstring print() const override
562 return L"html[" + url_ + L"]";
565 boost::property_tree::wptree info() const override
567 boost::property_tree::wptree info;
568 info.add(L"type", L"html-producer");
572 core::constraints& pixel_constraints() override
577 core::monitor::subject& monitor_output()
579 return monitor_subject_;
583 void describe_producer(core::help_sink& sink, const core::help_repository& repo)
585 sink.short_description(L"Renders a web page in real time.");
586 sink.syntax(L"{[html_filename:string]},{[HTML] [url:string]}");
587 sink.para()->text(L"Embeds an actual web browser and renders the content in realtime.");
589 ->text(L"HTML content can either be stored locally under the ")->code(L"templates")
590 ->text(L" folder or fetched directly via an URL. If a .html file is found with the name ")
591 ->code(L"html_filename")->text(L" under the ")->code(L"templates")->text(L" folder it will be rendered. If the ")
592 ->code(L"[HTML] url")->text(L" syntax is used instead, the URL will be loaded.");
593 sink.para()->text(L"Examples:");
594 sink.example(L">> PLAY 1-10 [HTML] http://www.casparcg.com");
595 sink.example(L">> PLAY 1-10 folder/html_file");
598 spl::shared_ptr<core::frame_producer> create_producer(
599 const core::frame_producer_dependencies& dependencies,
600 const std::vector<std::wstring>& params)
602 const auto filename = env::template_folder() + params.at(0) + L".html";
603 const auto found_filename = find_case_insensitive(filename);
604 const auto html_prefix = boost::iequals(params.at(0), L"[HTML]");
606 if (!found_filename && !html_prefix)
607 return core::frame_producer::empty();
609 const auto url = found_filename
610 ? L"file://" + *found_filename
613 if (!html_prefix || boost::algorithm::ends_with(url, "_A") || boost::algorithm::ends_with(url, "_ALPHA"))
614 return core::frame_producer::empty();
616 return core::create_destroy_proxy(spl::make_shared<html_producer>(
617 dependencies.frame_factory,
618 dependencies.format_desc,