This topic introduces the Native Client technology, which allows you to run native C and C++ code on Samsung Smart TVs.

Native Client (NaCl) is a technology developed by Google that allows you to load and safely execute native binaries, written in C or C++, as if they were Web browser plug-ins. NaCl is supported on the Google Chrome™ browser, but Samsung has customized the NaCl technology and has supported it on Smart TVs since 2013. Since most Smart TV applications are essentially Web pages, it means NaCl binaries can also be run on Smart TVs.

The following figure shows Futuracer, a game that uses NaCl to take advantage of OpenGL® ES graphics features.

Figure 1. Futuracer game

Figure 1. Futuracer game

The following topics introduce you to creating NaCl applications for Smart TVs:

NaCl Features

When you use NaCl, you can take advantage of the following features:

  • Greater execution speed
    NaCl code runs up to 24 times faster than JavaScript (benchmark based on an encryption algorithm).
  • Near-native code performance
    In Web applications, you can use compiled code run natively on the host CPU. On the ARM platform, NaCl applications can achieve about 85% of native performance.
  • Multithreading
    You can use the multithreading features introduced in C++ 11 and take advantage of the multi-core architecture of most Smart TVs.
  • Input event handling
    The NaCl plugin can register mouse and keyboard events.
  • Access to local and remote resources using special interfaces, such as TCP/UDP sockets, WebSockets, and file I/O.
  • Support for C++ 11 and C++ 14
  • Access to libraries ported from C/C++, including:
    • OpenGL® ES 2.0: 3D graphics
    • OpenSSL: secure data encryption
    • OpenCV: image/video recognition and analysis
    • Boost: general-purpose tools
  • Security
    The NaCl plugin is sandboxed inside the Web browser engine to ensure that malicious code is detected and terminated. To ensure the safety of executed code, C/C++ native code compiled using the NaCl toolchain has some restrictions:
    • System resources cannot be opened directly using functions, such as the fopen() function.
    • The main() function is replaced by a set of functions and classes that represent the NaCl plugin logic.
    • System-dependent libraries, such as "uinstd.h" and "windows.h", are not available.
  • Code obfuscation
    After compiling the application, the native code cannot be retrieved from the application package.
  • Portability across systems and architectures
    NaCl applications can be easily ported between various runtime environments. The NaCl build system allows you to target the 3 major system architectures: x86-32, x86-64, and ARM. When support for new platforms is added, you can simply translate the application to the new architecture without extensive code modification, recompiling, or worrying about compatibility.
  • Intercompatibility and interoperability with the Google Chrome™ Web browser
    Excluding TV-specific features, NaCl applications can run on Samsung Smart TVs and on the Google Chrome™ Web browser.

NaCl Use Cases

NaCl can be used in many ways. It can be used to easily port existing C++ applications to the Web, to support heavy computational tasks and extend Web page functionality, and to create standalone, CPU-intensive Web applications, such as 3D games. You can enhance the Smart TV experience by using NaCl to implement fast, responsive applications with great connectivity possibilities.

The following Smart TV use cases are made possible using NaCl:

  • Creating 3D games using the OpenGL® library and the audio interface.
  • Creating and displaying graphics models on a Web page using OpenGL® ES 2.0.
  • Harnessing the computational power of the device to create embedded emulators.
  • Performing sound mixing using the audio interface to access sample-level audio control.
  • Creating a set-top box (STB) experience on the TV itself.
  • Creating custom media streaming applications using advanced video, audio, and networking features.
  • Implementing multicast data streaming using UDP sockets.
  • Using the OpenCV face recognition features with video captured from the Smart TV webcam.
  • Creating interactive simulations using 3D graphics and input event handling.

Technical Overview

Samsung Smart TVs use a Web engine to render Web pages in the browser and to run TV applications.

The NaCl technology uses the Pepper Plugin API (or PPAPI), which replaces the Netscape Plugin API and enables running Web browser plugins on the Webkit and Blink Web engines. A NaCl module is essentially a Pepper plugin embedded on a Web page, loaded from the host, and executed inside the Web engine sandbox. The PPAPI allows you to exchange information with JavaScript in the Web page, enabling features, such as messaging, event handling, and viewport drawing. The JavaScript component and NaCl module communicate using input events or through a messaging system.

Figure 2. Interaction between JavaScript and NaCl components

Figure 2. Interaction between JavaScript and NaCl components

Native code must be compiled before it can be run as a NaCl module. The NaCl toolchain includes compilation tools, such as GCC, Clang, ar, and make, adapted for working with NaCl code.

You can compile a NaCl module in 2 ways:

  • Compile directly to a ".nexe" file using GCC
    This process is similar to compiling normal C or C++ code.
  • Compile using Clang, a LLVM compiler
    Clang compiles the source code to a ".pexe" file, an intermediate bytecode format called Portable Native Client (PNaCl). Google Chrome™ can load the ".pexe" file directly because it supports just-in-time translation, but for Samsung TVs, you must create a ".nexe" file for each architecture you want the module to support.
    Using the Clang compiler, you can compile once and simply translate for multiple architectures, including future architectures. You can also use advanced Clang features, such as address, memory, and thread sanitizers.

Figure 3. Compilation methods

Figure 3. Compilation methods

To load a compiled NaCl module in an application:

  1. The "index.html" file is loaded and parsed.
  2. The Web page is displayed.
  3. For each embed element with the type "application/x-nacl":
    1. The corresponding manifest file is loaded and parsed.
    2. The ".nexe" file coresponding to the system architecture is loaded from the path specified in the manifest file.
    3. The ".nexe" file is statically analyzed for security violations. If no security violations are found, the module is executed in the browser sandbox.
    4. When the NaCl instance is successfully created, the module loaded event is sent to the JavaScript component.
    5. The NaCl plugin is displayed on the Web page in the embed element.
      Figure 4. Loading a NaCl module

      Figure 4. Loading a NaCl module