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
24 #include "producer/html_producer.h"
25 #include "producer/html_cg_proxy.h"
27 #include <common/executor.h>
28 #include <common/future.h>
30 #include <core/producer/cg_proxy.h>
32 #include <boost/thread.hpp>
33 #include <boost/asio.hpp>
34 #include <boost/date_time/posix_time/posix_time.hpp>
35 #include <boost/foreach.hpp>
36 #include <boost/timer.hpp>
37 #include <boost/range/algorithm/remove_if.hpp>
38 #include <boost/thread/future.hpp>
39 #include <boost/lexical_cast.hpp>
40 #include <boost/log/trivial.hpp>
45 #pragma warning(disable: 4458)
49 #pragma comment(lib, "libcef.lib")
50 #pragma comment(lib, "libcef_dll_wrapper.lib")
52 namespace caspar { namespace html {
54 std::unique_ptr<executor> g_cef_executor;
57 const CefRefPtr<CefBrowser>& browser,
58 boost::log::trivial::severity_level level,
59 const std::string& message)
63 auto msg = CefProcessMessage::Create(LOG_MESSAGE_NAME);
64 msg->GetArgumentList()->SetInt(0, level);
65 msg->GetArgumentList()->SetString(1, message);
66 browser->SendProcessMessage(PID_BROWSER, msg);
70 class remove_handler : public CefV8Handler
72 CefRefPtr<CefBrowser> browser_;
74 remove_handler(CefRefPtr<CefBrowser> browser)
80 const CefString& name,
81 CefRefPtr<CefV8Value> object,
82 const CefV8ValueList& arguments,
83 CefRefPtr<CefV8Value>& retval,
84 CefString& exception) override
86 if (!CefCurrentlyOn(TID_RENDERER))
89 browser_->SendProcessMessage(
91 CefProcessMessage::Create(REMOVE_MESSAGE_NAME));
96 IMPLEMENT_REFCOUNTING(remove_handler);
99 class renderer_application : public CefApp, CefRenderProcessHandler
101 std::vector<CefRefPtr<CefV8Context>> contexts_;
103 CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() override
108 void OnContextCreated(
109 CefRefPtr<CefBrowser> browser,
110 CefRefPtr<CefFrame> frame,
111 CefRefPtr<CefV8Context> context) override
113 caspar_log(browser, boost::log::trivial::trace,
115 + boost::lexical_cast<std::string>(frame->GetIdentifier())
117 contexts_.push_back(context);
119 auto window = context->GetGlobal();
123 CefV8Value::CreateFunction(
125 new remove_handler(browser)),
126 V8_PROPERTY_ATTRIBUTE_NONE);
128 CefRefPtr<CefV8Value> ret;
129 CefRefPtr<CefV8Exception> exception;
130 bool injected = context->Eval(R"(
131 var requestedAnimationFrames = {};
132 var currentAnimationFrameId = 0;
134 window.requestAnimationFrame = function(callback) {
135 requestedAnimationFrames[++currentAnimationFrameId] = callback;
136 return currentAnimationFrameId;
139 window.cancelAnimationFrame = function(animationFrameId) {
140 delete requestedAnimationFrames[animationFrameId];
143 function tickAnimations() {
144 var requestedFrames = requestedAnimationFrames;
145 var timestamp = performance.now();
146 requestedAnimationFrames = {};
148 for (var animationFrameId in requestedFrames)
149 if (requestedFrames.hasOwnProperty(animationFrameId))
150 requestedFrames[animationFrameId](timestamp);
155 caspar_log(browser, boost::log::trivial::error, "Could not inject javascript animation code.");
158 void OnContextReleased(
159 CefRefPtr<CefBrowser> browser,
160 CefRefPtr<CefFrame> frame,
161 CefRefPtr<CefV8Context> context)
163 auto removed = boost::remove_if(
164 contexts_, [&](const CefRefPtr<CefV8Context>& c)
166 return c->IsSame(context);
169 if (removed != contexts_.end())
170 caspar_log(browser, boost::log::trivial::trace,
172 + boost::lexical_cast<std::string>(frame->GetIdentifier())
175 caspar_log(browser, boost::log::trivial::warning,
177 + boost::lexical_cast<std::string>(frame->GetIdentifier())
178 + " released, but not found");
181 void OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) override
186 bool OnProcessMessageReceived(
187 CefRefPtr<CefBrowser> browser,
188 CefProcessId source_process,
189 CefRefPtr<CefProcessMessage> message) override
191 if (message->GetName().ToString() == TICK_MESSAGE_NAME)
193 for (auto& context : contexts_)
195 CefRefPtr<CefV8Value> ret;
196 CefRefPtr<CefV8Exception> exception;
197 context->Eval("tickAnimations()", ret, exception);
208 IMPLEMENT_REFCOUNTING(renderer_application);
211 bool intercept_command_line(int argc, char** argv)
214 CefMainArgs main_args;
216 CefMainArgs main_args(argc, argv);
219 if (CefExecuteProcess(main_args, CefRefPtr<CefApp>(new renderer_application), nullptr) >= 0)
225 void init(core::module_dependencies dependencies)
227 dependencies.producer_registry->register_producer_factory(L"HTML Producer", html::create_producer, html::describe_producer);
229 CefMainArgs main_args;
230 g_cef_executor.reset(new executor(L"cef"));
231 g_cef_executor->invoke([&]
233 CefSettings settings;
234 settings.no_sandbox = true;
235 //settings.windowless_rendering_enabled = true;
236 CefInitialize(main_args, settings, nullptr, nullptr);
238 g_cef_executor->begin_invoke([&]
242 dependencies.cg_registry->register_cg_producer(
245 [](const std::wstring& filename)
249 [](const spl::shared_ptr<core::frame_producer>& producer)
251 return spl::make_shared<html_cg_proxy>(producer);
253 [](const core::frame_producer_dependencies& dependencies, const std::wstring& filename)
255 return html::create_producer(dependencies, { filename });
265 CefQuitMessageLoop();
267 g_cef_executor->begin_invoke([&]
271 g_cef_executor.reset();
274 class cef_task : public CefTask
277 std::promise<void> promise_;
278 std::function<void ()> function_;
280 cef_task(const std::function<void ()>& function)
281 : function_(function)
285 void Execute() override
287 CASPAR_LOG(trace) << "[cef_task] executing task";
292 promise_.set_value();
293 CASPAR_LOG(trace) << "[cef_task] task succeeded";
297 promise_.set_exception(std::current_exception());
298 CASPAR_LOG(warning) << "[cef_task] task failed";
302 std::future<void> future()
304 return promise_.get_future();
307 IMPLEMENT_REFCOUNTING(cef_task);
310 void invoke(const std::function<void()>& func)
312 begin_invoke(func).get();
315 std::future<void> begin_invoke(const std::function<void()>& func)
317 CefRefPtr<cef_task> task = new cef_task(func);
319 if (CefCurrentlyOn(TID_UI))
323 return task->future();
326 if (CefPostTask(TID_UI, task.get()))
327 return task->future();
329 CASPAR_THROW_EXCEPTION(caspar_exception()
330 << msg_info("[cef_executor] Could not post task"));