Getting Started

Introduction to the Native Client technology, implemented in Samsung Smart TV.

This article will introduce you to the Native Client. Native Client, or simply NaCl, is an awesome technology which enables running native compiled code in a web browser. NaCl is portable across many architectures and operating systems. It focuses greatly on safety of the host system by using various sandboxing techniques which ensure that malicious code will be detected and terminated in time. It offers speed comparable to running standalone native code and when comparing to JavaScript it can be even 24 times faster.


Up until recently applications for SmartTVs had to be written in JavaScript programming language. More precisely those applications were HTML webpages that contained scripts written in JavaScript. In the nature of JavaScript lays an ease of use and inter-compatibility, but that comes at a certain price. Being interpreted by the web browser JavaScript applications are much slower than the native applications written in compiled languages. JavaScript also restricts developers in terms of more advanced programming techniques that are sometimes necessary for implementation of some features. These problems are solved by a new technology called Native Client introduced by Google, and adopted by Samsung in their line of SmartTV and Tizen TV.

About NaCl

NaCl is an advanced technology that provides the web page with a way to load and safely execute native binaries written in C or C++ programming language as if they were browser plug-ins. As every TizenTV application (called "widget") is essentially a webpage, TizenTV is able to run NaCl binaries in the same way that Google Chrome web browser does it. During the application execution, both the JavaScript module and the C++ module may communicate either in one direction via input events or with each other via special messaging system. Furthermore, Native Client provides a ported OpenGL ES 2.0 library and thus can be used to create advanced 3D applications that run inside the browser or on TizenTV. NaCl also provides support for other resources not available in JavaScript that can enchance your application. Currently NaCl is supported on Samsung SmartTV (starting from 2013 platform) and TizenTV platforms and also on Google Chrome browser.

The NaCl unique build system lets the developer target all three current major system architectures (x86_32, x86_64 and ARM) and is future compatible with platforms that are yet to come. This way when the support for a new platform is added, the developer can translate (no need to recompile) the application for the new architecture without worrying about compatibility issues. However C/C++ native code compiled by the NaCl toolchain has some restrictions in order to ensure the safety of executed code, e.g.:

  • you cannot open system resources directly (fopen(..), etc.),
  • main() is replaced by a set of functions/classes that represent the NaCl plugin logic,
  • system dependent libraries are not available (uinstd.h, windows.h, etc.).

NaCl Features

Some of NaCl features are:

  • Much greater speed than JavaScript - even 24 times faster execution than JavaScript (benchmark based in encryption algorithm).
  • Near native code performance - about 85% of native performance on ARM platform.
  • Multithreading - it's finally possible to take advantage of multicore architecture of the most SmartTVs and TizenTVs.
  • Input event handling - NaCl plugin can register for mouse and keyboard events inside of it.
  • Access to local and remote resources via specially prepared interfaces, e.g. TCP or UDP sockets, file IO, etc.
    • Many C/C++ libraries were ported to NaCl, including:
    • OpenGL ES 2.0 - for amazing 3D graphics.
    • OpenSSL - for secure encryption of data.
    • OpenCV - for image/video recognition and analysis.
    • Boost - for access to collection of general purpose tools.
  • Support for C++ 11/C++ 14
  • Security - NaCl module is sandboxed inside the web browser engine which ensures safety of application runtime.
  • Code obfuscation via compilation - after application deployment it's not possible to retrieve the native code from it.
  • Portability across many systems and architectures - porting NaCl application between different runtime environments is extremely easy and doesn't involve code modifications.
  • Intercompatibility and interoperability - once compiled, NaCl application can be run both on TizenTV and on Google Chrome (with exception to applications that require TV specific features).

NaCl Benefits

NaCl can be used for many purposes and in many ways. It may be used for standalone CPU-heavy web applications like 3D games, easy porting of existing C++ applications to the web, or embedding a NaCl plugin on a webpage to support it's heavy computational tasks or extend it's functionality. Especially on SmartTV and TizenTV NaCl can be used to enchance TV experience as it can be used to implement fast and highly responsive applications with great connectivity possibilities.

Constraints of JavaScript Solved by NaCl

  • Lack of multithreading - NaCl allows a developer to use C++11 multithreading capabilities. This greatly improves performance and allows to take advantage of multi-core CPU architectures that are more and more common in SmartTVs.
  • Low performance - JavaScript is a language interpreted by the browser, hence its low performance. Native Client addresses this issue by introducing compiled languages, which are run natively on the host CPU, to the web.
  • Lack TCP and UDP sockets - JavaScript doesn't allow a developer to use standard sockets. In NaCl it's possible to create TCP and UDP connections via specially prepared implementations called interfaces. NaCl also provides support for websockets.
  • Access to source code after application release - native code compilation process lets you obfuscate application code and, as a result, make it virtually impossible to steal the code of the application from the released binary.

Possible Use Scenarios of NaCl

All the below NaCl use cases are impossible or highly limited when using only JavaScript to create a SmartTV or TizenTV application:

  • 3D games - creating games in NaCl is made easy thanks to OpenGL library and the Audio interface.
  • Graphics modeling applications - OpenGL ES 2.0 offers great graphics performance and flexibility inside a web page.
  • Emulators - great computational power that is required by emulators can be harnessed by the NaCl technology in a web page.
    Sound mixing - the NaCl Audio interface provides access to sample-level audio control.
  • TV applications - NaCl can be used for creating STB-like experience on the TV itself.
  • Custom Video/Audio streaming services - using advanced video, audio and networking NaCl capabilities it's possible to create VOD and music streaming applications for the SmartTV.
  • UDP multicast servers - the NaCl implementation of UDP sockets enables multicast streaming of data.
  • Face recognition - NaCl provides an interface for video capture from the SmartTV's webcam. This video can then processed by the OpenCV library which provides an API for face recognition.
  • Interactive simulations - combination of input events handling and 3D graphics provides necessary tools for developing interactive simulations in NaCl.

Technical Overview

To better understand how the NaCl technology actually works we need to delve deeper into the browser technology. TizenTVs use a web browser engine called Webkit. This engine is used both for displaying web pages via web browser included in the OS and running all of the SmartTV applications. Although applications written in JavaScript and HTML constitute the majority of the SmartTV apps, since the introduction of NaCl it can be observed that the number of applications using NaCl is steadily rising. With that rise in numbers we can also see that applications for SmartTV are getting more advanced.

The NaCl technology relies on another technology called Pepper Plugin API (or PPAPI), which is a technology that enables running web browser plugins in Webkit and Blink (web engine of Google Chrome) and substitutes old and unsafe NPAPI (Netscape Plugin API). A NaCl module is essentially a Pepper plugin which is embeded on a webpage, loaded from the host and executed inside the web engine sandbox. To reach outside of that sandbox the application uses PPAPI which provides a way to exchange information with JavaScript on the loaded web page including: messaging, events handling, viewport drawing, etc.

In order to run a NaCl application on the TizenTV (or any other supported web browser) you have to compile the native code first. Compilation of the NaCl application is similar to the compilation of normal C/C++ code. NaCl toolchain provides a set of tools specially prepared for NaCl compilation like: GCC, Clang, ar, make, etc. Compilation of NaCl application can take two different courses. One way to compile NaCl app is the direct compilation to .nexe using the provided GCC, which is almost identical to normal C/C++ compilation. The other way is using an LLVM compiler - Clang. Clang compiles the source code to a .pexe file which is an intermediate form of bytecode called Portable NativeClient or PNaCl for short. These .pexe files are then translated to the desired architecture's bytecode format resulting in .nexe file for every coresponding architecture. Google Chrome supports just-in-time translation mechanism which allows the .pexe files to be loaded directly by the web browser. On TizenTV the process of translation is performed by the developer after the compilation to .pexe. Advantages of the second approach include:

  • Compile once, translate for multiple architectures.
  • Compatibility of .pexe files with future architectures.
  • Advanced features of the Clang compiler like address, memory and thread sanitizers.

With the NaCl module compiled everything is ready to go. Loading of the NaCl application is done in several steps:

  1. The index.html file is loaded, parsed and the webpage is displayed.
  2. For each embed element of type application/x-nacl the coresponding manifest file is loaded and parsed.
  3. The .nexe file coresponding to the system architecture is loaded from the path specified in the manifest file.
  4. The nexe file is statically analyzed for security violations and if none are found it's executed in the browser sandbox. When the NaCl instance is successfully created the module loaded event is sent to JavaScript.
  5. The NaCl plugin is displayed on the webpage in the place declared for the embed element object.

Development in NaCl

This covers the basic knowledge about the NativeClient technology. If you want to start creating apps in NaCl for TizenTV it's recommended to read How to create a sample NaCl application and Hello World in C++ articles. Also please consider using the specially prepared IDE for NaCl TizenTV developement which makes the process of compilation, testing and debugging the application very easy.

This is an example game written in NaCl that takes advantage of OpenGL ES 2.0. It's called Futuracer and can be downloaded on supported SmartTV platforms from the SmartHub.