API Usage

This topic describes how to implement a NaCl multimedia player for Samsung Smart TV.

Related Info

The NaCl (Native Client) Player API allows you to implement various multimedia playback modes in your application, depending on the source of the multimedia data. You can play content from a multimedia resource URL, or from elementary stream packets demuxed by the application. Both modes can handle DRM-protected content, as well as subtitles.


To enable additional functionality for NaCl Player media playback:

  • To access content on the Internet, the application has to request permission by adding the following privilege to the "config.xml" file:

    <tizen:privilege name='http://tizen.org/privilege/internet'></tizen:privilege> 
  • To play PlayReady-protected content, the application has to request permission by adding the following privilege to the "config.xml" file:

    <tizen:privilege name='http://developer.samsung.com/privilege/drmplay'></tizen:privilege>

Preparing Managing Class

All examples below will be written as parts of implementation of some managing class, which is declared to have members:

// An instance of player
Samsung::NaClPlayer::MediaPlayer player_;

// An instance of class derived from Samsung::NaClPlayer::XxxListener,
// where Xxx is MediaEvents, Buffering, DRM or Subtitle
MyXxxListener xxx_listener_;

// Use either this or ESDataSource
Samsung::NaClPlayer::URLDataSource url_data_source_;
// Use either this or URLDataSource
Samsung::NaClPlayer::ESDataSource es_data_source_;

// Following members are needed only with ESDataSource:
// An instance of audio elementary stream
Samsung::NaClPlayer::AudioElementaryStream audio_stream_;
// An instance of video elementary stream
Samsung::NaClPlayer::VideoElementaryStream video_stream_;
// An instance of class derived from Samsung::NaClPlayer::ElementaryStreamListener
MyESListener audio_listener_;
// An instance of class derived from Samsung::NaClPlayer::ElementaryStreamListener
MyESListener video_listener_;

Initializing the Media Player

To implement playback using the NaCl Player API, you must initialize the following classes:

  1. The MediaPlayer class is a representation of the platform player, which allows you to control it.
    Creating the MediaPlayer object is parameter-free:

    // Create the player object 
    player_ = Samsung::NaClPlayer::MediaPlayer(); 
  2. The MediaDataSource class, specialized as either a URLDataSource class or an ESDataSource class, is an abstract representation of a data source. Each specialization has a different initialization procedure. For more information, see Configuring Media Data Sources.

  3. The initialized MediaDataSource object must be attached to the MediaPlayer object:

    // Application-defined function to initialize the MediaDataSource-derived object
    // or InitializeDataSource(url_data_source_);
    // Attach the initialized data source
    // or player_.AttachDataSource(url_data_source_);

Registering Listeners

To receive notifications about NaCl Player events, you must implement listeners. The NaCl Player listener classes do not provide event handling by default; event handlers must be implemented by the application.

To receive notifications from the NaCl Player:

  1. Override the appropriate listener class, and handle the virtual function associated with the event.

    • To receive playback notifications from the NaCl Player, override the MediaEventsListener class:

      class MyMediaEventsListener : public Samsung::NaClPlayer::MediaEventsListener {
          // During playback, provides the current playback time
          void OnTimeUpdate(Samsung::NaClPlayer::TimeTicks time) override {
            // Handle the event...
          // Triggers when playback has finished
          void OnEnded() override {
            // Handle the event...
          // Triggers when a player error occurs
          void OnError(Samsung::NaClPlayer::MediaPlayerError) override {
            // Handle the event...
    • To receive buffering notifications from the NaCl Player, override the BufferingListener class:

      class MyBufferingListener : public Samsung::NaClPlayer::BufferingListener {
          // Triggers when the NaCl player starts buffering data
          void OnBufferingStart() override {
            // Handle the event...
          // Provides the buffering progress
          void OnBufferingProgress(uint32_t percent) override {
            // Handle the event...
          // Triggers when the NaCl Player finishes buffering data
          void OnBufferingComplete() override {
            // Handle the event...

    The meaning of the buffering events depends on the data source.

    Event Elementary stream source URL source

    This event is fired when a data source is attached to the MediaPlayer object.
    The NaCl Player data buffer is ready to be filled with elementary stream packets.

    This event is fired when a data source is attached to the MediaPlayer object and during seek.
    The NaCl Player has started buffering.


    - Sends information about the buffering completion percentage.

    The NaCl Player data buffer has received enough packets to start playback. The NaCl Player has finished buffering. Playback can be started, or resumed after seeking.

    Table 1. Buffering events

    • To receive DRM-related notifications from the NaCl Player, override the DRMListener class:

      class MyDRMListener : public Samsung::NaClPlayer::DRMListener {
          DRMListener(const Samsung::NaClPlayer::MediaPlayer* player) 
            : player_(player) {
        // Notifies when the DRM initialization data is loaded
        void OnInitdataLoaded(
            Samsung::NaClPlayer::DRMType drm_type, uint32_t init_data_size,
            const void* init_data) override {
          // Notifies that a license is required to play protected content
          // The request parameter contains challenge data for obtaining the license
          void OnLicenseRequest(uint32_t request_size, const void* request) override {
            // Connect to a license server and download the license
            std::string license;
            // Fill the license...
            // Install the obtained license
                DRMType_/* type */, DRMOperation_InstallLicense,
                license.size(), license.data());
          Samsung::NaClPlayer::MediaPlayer* player_;

      When protected content needs to be decrypted, and there is no installed license that can decrypt it, the OnLicenseRequest() event is triggered. Generate the license request and install the license with the MediaPlayer::SetDRMSpecificData() function. If multiple tracks are protected by the same license, install it only once.

    • To receive subtitle change notifications from the NaCl Player, override the SubtitleListener class. The listener only notifies about subtitle changes in the currently-selected text track.

      class SubtitleListener : public Samsung::NaClPlayer::SubtitleListener {
          // Provide the subtitle text and duration
          void OnShowSubtitle(
            Samsung::NaClPlayer::TimeDelta duration, const char* text) override {
              // Show the subtitle...
  2. Register each listener in the MediaPlayer object, using the corresponding SetXxxListener() function:

    // Create a listener and register it in the player 
    xxx_listener_ = MyXxxListener(); // or MyXxxListener(&player) if needed 
  3. To unsubscribe from events, deregister the applicable listener:


Configuring Media Data Sources

Media data sources are configured in different ways, depending on whether the source is provided as a URL or an elementary stream.

URL Data Sources

The URLDataSource class allows you to play media from a specific URL source.

To create a URLDataSource object, call its constructor and attach it to the MediaPlayer object:

// Create a media data source for the URL and attach it to the player
url_data_source_ = Samsung::NaClPlayer::URLDataSource(media_content_url);

Elementary Stream Data Sources

The ESDataSource class allows you to play media using demuxed elementary stream packets.

To create and configure a ESDataSource object:

  1. Instantiate the ESDataSource class:

    // Create the media data source
    es_data_source_ = Samsung::NaClPlayer::ESDataSource();
  2. Implement event handling using the ElementaryStreamListener class:

    class MyESListener : public Samsung::NaClPlayer::ElementaryStreamListener {
      // Triggers when NaCl Player needs ES packets
      void OnNeedData(int32_t bytes) {
        // Send one or more packets to the NaCl Player
      // Triggers when the NaCl Player ES packet buffer is full
      void OnEnoughData() {
        // Stop sending packets
      // Triggers when the NaCl Player performs seek operations
      void OnSeekData(Samsung::NaClPlayer::TimeTicks new_position) {
        // Adjust the time position from which packets are sent
  3. Add elementary streams to the ESDataSource object and associate them with the listener:

    // Add VideoElementaryStream
    video_listener_ = MyESListener();
    video_stream_ = Samsung::NaClPlayer::VideoElementaryStream();
    es_data_source_.AddStream(video_stream_, &video_listener_);
    // Add AudioElementaryStream
    audio_listener_ = MyESListener();
    audio_stream_ = Samsung::NaClPlayer::AudioElementaryStream();
    es_data_source_->AddStream(audio_stream_, &audio_listener_);
  4. Configure each elementary stream:

    • To configure an AudioElementaryStream object:

      1. Define the parameters in the AudioConfig struct:

        AudioConfig audio_config;
        // Fill audio_config with the appropriate information
      2. Confirm and save the configuration using the ElementaryStream::InitializeDone() function:

    • To configure a VideoElementaryStream object:

      1. Define the parameters in the VideoConfig struct:

        VideoConfig video_config;
        // Fill video_config with the appropriate information
      2. Confirm and save the configuration using the ElementaryStream::InitializeDone() function:

  5. When the elementary streams have been configured, attach the ESDataSource object to the MediaPlayer object:


    The application can receive elementary stream events and start appending elementary stream packets.

  6. Start appending packets to the buffer (as part of main playback loop if you implement push mode or as a response to ElementaryStreamListener::OnNeedData() event if you implement pull mode):


    When the MediaPlayer object sends the BufferingListener::OnBufferingComplete() event, the application can begin playback.

Playing DRM-protected Content

The NaCl Player can handle multimedia content protected by DRM technology. The logic for handling DRM-protected content varies depending on the data source.

DRM for URL Data Sources

In the URL data source scenario, the DRM is configured automatically by the platform, based on the DRM configuration stored in a file at a specific URL (for example, a DASH manifest file).

DRM for Elementary Stream Data Sources

In the elementary stream source scenario, the DRM engine must be configured by the application during stream configuration. When the DRM license is requested, the license information must be retrieved and installed.

In this paragraph we will go into details of implementing MyDRMListener, which was already mentioned earlier.

  1. Define the DRM listener.
    The DRMListener class listens for the OnLicenseRequest() event and reacts to it by installing the license required to play the DRM-protected content:

    class MyDRMListener : public Samsung::NaClPlayer::DRMListener {
        MyDRMListener(const Samsung::NaClPlayer::MediaPlayer* player)
          : player_(player) {
        // Notify that DRM protection data has been loaded
        void OnInitdataLoaded(
          Samsung::NaClPlayer::DRMType drm_type, uint32_t init_data_size,
          const void* init_data) override;
        // Signal that a license is required to play the current content
        void OnLicenseRequest(uint32_t request_size, const void* request) override;
        // OnLicenseRequest calls this function when it downloads a license 
        void InstallLicense(std:: string license);
        Samsung::NaClPlayer::MediaPlayer* player_;
  2. Register the listener in the MediaPlayer object:

    // Create the listener and register it in the player
    drm_listener_ = MyDRMListener(&player_);

    For more information, see Registering Listeners.

  3. Configure the elementary streams.
    The DRM information for each elementary stream must be configured at the same time as the stream's other parameters, using the ElementaryStream::SetDRMInitData() function:

    // ...
    // Add a video elementary stream as in an earlier example
    es_data_source_.AddStream(video_stream_, &video_listener_);
    // Configure the video stream as we did before
    // and add DRM init data before calling ElementaryStream::InitializeDone()
    video_stream_.SetDRMInitData(encryption_type, init_data_size, init_data_ptr); 
    // Repeat the above for an audio elementary stream...
    // ...
  4. Request and install the DRM license.
    If the license required to play the current DRM-protected content is not available, the OnLicenseRequest() event is triggered. The DRMListener() event handler must download the license information from the license server and pass it to the NaCl Player.

    // Download a license asynchronously and call the license_downloaded_cb() functor
    void DownloadLicense(
      std::string license_server_url, uint32_t request_size, const void* request,
      std::function<std::string> license_downloaded_cb);
    void MyDRMListener::OnLicenseRequest(
      uint32_t request_size, const void* request) {
      // Download a valid license from the license server
      // For application responsiveness, download the license asynchronously
      DownloadLicense("license server URL", request_size, request, std::bind(
        &MyDRMListener::InstallLicense, this, std::placeholders::_1));
    void MyDRMListener::InstallLicense(std::string license) {
      int32_t result = player_.SetDRMSpecificData(
        Samsung::NaClPlayer::DRMType_/* type */,
        license.size(), license.data());
      if (result < Samsung::NaClPlayer::ErrorCodes::Success) {
        // Handle the error...
  5. Interact with the DRM system using the MediaPlayer::SetDRMSpecificData()function.

Supported Player Modes

Tizen Version default D2TV
2.4 ?


3.0 ?


4.0 ?


5.0 ?