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>
41 #include <boost/property_tree/ptree.hpp>
46 #pragma warning(disable: 4458)
48 #include <cef_version.h>
51 #pragma comment(lib, "libcef.lib")
52 #pragma comment(lib, "libcef_dll_wrapper.lib")
54 namespace caspar { namespace html {
56 std::unique_ptr<executor> g_cef_executor;
59 const CefRefPtr<CefBrowser>& browser,
60 boost::log::trivial::severity_level level,
61 const std::string& message)
65 auto msg = CefProcessMessage::Create(LOG_MESSAGE_NAME);
66 msg->GetArgumentList()->SetInt(0, level);
67 msg->GetArgumentList()->SetString(1, message);
68 browser->SendProcessMessage(PID_BROWSER, msg);
72 class remove_handler : public CefV8Handler
74 CefRefPtr<CefBrowser> browser_;
76 remove_handler(CefRefPtr<CefBrowser> browser)
82 const CefString& name,
83 CefRefPtr<CefV8Value> object,
84 const CefV8ValueList& arguments,
85 CefRefPtr<CefV8Value>& retval,
86 CefString& exception) override
88 if (!CefCurrentlyOn(TID_RENDERER))
91 browser_->SendProcessMessage(
93 CefProcessMessage::Create(REMOVE_MESSAGE_NAME));
98 IMPLEMENT_REFCOUNTING(remove_handler);
101 class renderer_application : public CefApp, CefRenderProcessHandler
103 std::vector<CefRefPtr<CefV8Context>> contexts_;
105 CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() override
110 void OnContextCreated(
111 CefRefPtr<CefBrowser> browser,
112 CefRefPtr<CefFrame> frame,
113 CefRefPtr<CefV8Context> context) override
115 caspar_log(browser, boost::log::trivial::trace,
117 + boost::lexical_cast<std::string>(frame->GetIdentifier())
119 contexts_.push_back(context);
121 auto window = context->GetGlobal();
125 CefV8Value::CreateFunction(
127 new remove_handler(browser)),
128 V8_PROPERTY_ATTRIBUTE_NONE);
130 CefRefPtr<CefV8Value> ret;
131 CefRefPtr<CefV8Exception> exception;
132 bool injected = context->Eval(R"(
133 var requestedAnimationFrames = {};
134 var currentAnimationFrameId = 0;
136 window.requestAnimationFrame = function(callback) {
137 requestedAnimationFrames[++currentAnimationFrameId] = callback;
138 return currentAnimationFrameId;
141 window.cancelAnimationFrame = function(animationFrameId) {
142 delete requestedAnimationFrames[animationFrameId];
145 function tickAnimations() {
146 var requestedFrames = requestedAnimationFrames;
147 var timestamp = performance.now();
148 requestedAnimationFrames = {};
150 for (var animationFrameId in requestedFrames)
151 if (requestedFrames.hasOwnProperty(animationFrameId))
152 requestedFrames[animationFrameId](timestamp);
157 caspar_log(browser, boost::log::trivial::error, "Could not inject javascript animation code.");
160 void OnContextReleased(
161 CefRefPtr<CefBrowser> browser,
162 CefRefPtr<CefFrame> frame,
163 CefRefPtr<CefV8Context> context)
165 auto removed = boost::remove_if(
166 contexts_, [&](const CefRefPtr<CefV8Context>& c)
168 return c->IsSame(context);
171 if (removed != contexts_.end())
172 caspar_log(browser, boost::log::trivial::trace,
174 + boost::lexical_cast<std::string>(frame->GetIdentifier())
177 caspar_log(browser, boost::log::trivial::warning,
179 + boost::lexical_cast<std::string>(frame->GetIdentifier())
180 + " released, but not found");
183 void OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) override
188 bool OnProcessMessageReceived(
189 CefRefPtr<CefBrowser> browser,
190 CefProcessId source_process,
191 CefRefPtr<CefProcessMessage> message) override
193 if (message->GetName().ToString() == TICK_MESSAGE_NAME)
195 for (auto& context : contexts_)
197 CefRefPtr<CefV8Value> ret;
198 CefRefPtr<CefV8Exception> exception;
199 context->Eval("tickAnimations()", ret, exception);
210 IMPLEMENT_REFCOUNTING(renderer_application);
213 bool intercept_command_line(int argc, char** argv)
216 CefMainArgs main_args;
218 CefMainArgs main_args(argc, argv);
221 if (CefExecuteProcess(main_args, CefRefPtr<CefApp>(new renderer_application), nullptr) >= 0)
227 void init(core::module_dependencies dependencies)
229 dependencies.producer_registry->register_producer_factory(L"HTML Producer", html::create_producer, html::describe_producer);
231 CefMainArgs main_args;
232 g_cef_executor.reset(new executor(L"cef"));
233 g_cef_executor->invoke([&]
235 CefSettings settings;
236 settings.no_sandbox = true;
237 //settings.windowless_rendering_enabled = true;
238 CefInitialize(main_args, settings, nullptr, nullptr);
240 g_cef_executor->begin_invoke([&]
244 dependencies.cg_registry->register_cg_producer(
247 [](const std::wstring& filename)
251 [](const spl::shared_ptr<core::frame_producer>& producer)
253 return spl::make_shared<html_cg_proxy>(producer);
255 [](const core::frame_producer_dependencies& dependencies, const std::wstring& filename)
257 return html::create_producer(dependencies, { filename });
262 auto cef_version_major = boost::lexical_cast<std::wstring>(cef_version_info(0));
263 auto cef_revision = boost::lexical_cast<std::wstring>(cef_version_info(1));
264 auto chrome_major = boost::lexical_cast<std::wstring>(cef_version_info(2));
265 auto chrome_minor = boost::lexical_cast<std::wstring>(cef_version_info(3));
266 auto chrome_build = boost::lexical_cast<std::wstring>(cef_version_info(4));
267 auto chrome_patch = boost::lexical_cast<std::wstring>(cef_version_info(5));
269 dependencies.system_info_provider_repo->register_version_provider(L"cef", [=]
271 return cef_version_major + L"." + chrome_build + L"." + cef_revision;
273 dependencies.system_info_provider_repo->register_system_info_provider([=](boost::property_tree::wptree& info)
275 info.add(L"system.cef.version", cef_version_major + L"." + chrome_build + L"." + cef_revision);
276 info.add(L"system.cef.chromeversion", chrome_major + L"." + chrome_minor + L"." + chrome_build + L"." + chrome_patch);
284 CefQuitMessageLoop();
286 g_cef_executor->begin_invoke([&]
290 g_cef_executor.reset();
293 class cef_task : public CefTask
296 std::promise<void> promise_;
297 std::function<void ()> function_;
299 cef_task(const std::function<void ()>& function)
300 : function_(function)
304 void Execute() override
306 CASPAR_LOG_CALL(trace) << "[cef_task] executing task";
311 promise_.set_value();
312 CASPAR_LOG_CALL(trace) << "[cef_task] task succeeded";
316 promise_.set_exception(std::current_exception());
317 CASPAR_LOG(warning) << "[cef_task] task failed";
321 std::future<void> future()
323 return promise_.get_future();
326 IMPLEMENT_REFCOUNTING(cef_task);
329 void invoke(const std::function<void()>& func)
331 begin_invoke(func).get();
334 std::future<void> begin_invoke(const std::function<void()>& func)
336 CefRefPtr<cef_task> task = new cef_task(func);
338 if (CefCurrentlyOn(TID_UI))
342 return task->future();
345 if (CefPostTask(TID_UI, task.get()))
346 return task->future();
348 CASPAR_THROW_EXCEPTION(caspar_exception()
349 << msg_info("[cef_executor] Could not post task"));