This topic covers how to initialize the Tizen WASM Player.
To make Media Player work with the WASM player, the following prerequisites and setup operations need to be met:
In the WASM Player, the ElementaryMediaStreamSource class acts as a source object for HTMLMediaElement. To set up ElementaryMediaStreamSource and HTMLMediaElement to work together:
ElementaryMediaStreamSource
HTMLMediaElement
Create the HTMLMediaElement object:
auto html_media_element = std::make_unique<samsung::html::HTMLMediaElement>(element_id);
Note The HTMLMediaElement class connects to either a <video> or an <audio> element in a HTML document hosting the WebAssembly module. The HTML element is chosen by the element_id specified during construction.
The HTMLMediaElement class connects to either a <video> or an <audio> element in a HTML document hosting the WebAssembly module. The HTML element is chosen by the element_id specified during construction.
<video>
<audio>
element_id
Create an object implementing the HTMLMediaElementListener interface and associate it with the media element. This step is optional, but the events generated by the media element are essential for creating a functional media player.
HTMLMediaElementListener
Define a custom implementation of HTMLMediaElementListener:
class MyMediaElementListener : public samsung::html::HTMLMediaElementListener { // Override virtual methods that the application uses };
Instantiate the custom listener and associate it with the HTMLMediaElement object:
auto my_media_element_listener = std::make_unique<MyMediaElementListener>(); html_media_element->SetListener(my_media_element_listener.get());
Note The HTMLMediaElement::SetListener() method does not take ownership of the listener. Make sure that the listener outlives the media element object.
The HTMLMediaElement::SetListener() method does not take ownership of the listener. Make sure that the listener outlives the media element object.
HTMLMediaElement::SetListener()
Create the ElementaryMediaStreamSource object:
using LatencyMode = samsung::wasm::ElementaryMediaStreamSource::LatencyMode; using RenderingMode = samsung::wasm::ElementaryMediaStreamSource::RenderingMode; auto elementary_media_stream_source = std::make_unique<samsung::wasm::ElementaryMediaStreamSource>(LatencyMode::kNormal, RenderingMode::kMediaElement);
Note Choose LatencyMode::kNormal to set up the WASM Player to work in normal latency mode, LatencyMode::kLow for low latency mode and LatencyMode::kUltraLow for ultra low latency mode. Choose RenderingMode::kMediaElement to set up the WASM Player to work in Media Element rendering mode or RenderingMode::kVideoTexture for Video Texture rendering mode.
Choose LatencyMode::kNormal to set up the WASM Player to work in normal latency mode, LatencyMode::kLow for low latency mode and LatencyMode::kUltraLow for ultra low latency mode. Choose RenderingMode::kMediaElement to set up the WASM Player to work in Media Element rendering mode or RenderingMode::kVideoTexture for Video Texture rendering mode.
LatencyMode::kNormal
LatencyMode::kLow
LatencyMode::kUltraLow
RenderingMode::kMediaElement
RenderingMode::kVideoTexture
Create an object implementing the ElementaryMediaStreamSourceListener interface and associate it with the source. This step is optional, but the events generated by the source are essential for creating a functional media player.
ElementaryMediaStreamSourceListener
Define a custom implementation of ElementaryMediaStreamSourceListener:
class MySourceListener : public samsung::wasm::ElementaryMediaStreamSourceListener { // Override virtual methods that the application uses };
Instantiate the custom listener and associate it with the ElementaryMediaStreamSource object:
auto my_source_listener = std::make_unique<MySourceListener>(); elementary_media_stream_source->SetListener(my_source_listener.get());
Note The ElementaryMediaStreamSource::SetListener() method does not take ownership of the listener. Make sure it outlives the source object.
The ElementaryMediaStreamSource::SetListener() method does not take ownership of the listener. Make sure it outlives the source object.
ElementaryMediaStreamSource::SetListener()
Attach ElementaryMediaStreamSource to HTMLMediaElement:
html_media_element->SetSrc(elementary_media_stream_source.get());
Note When created, ElementaryMediaStreamSource is set to the ReadyState::kDetached state, preventing any operations on the source. Attaching it to HTMLMediaElement changes the source's state to ReadyState::kClosed, allowing configuration of media tracks.
When created, ElementaryMediaStreamSource is set to the ReadyState::kDetached state, preventing any operations on the source. Attaching it to HTMLMediaElement changes the source's state to ReadyState::kClosed, allowing configuration of media tracks.
ReadyState::kDetached
ReadyState::kClosed
Once ElementaryMediaStreamSource is created and attached to HTMLMediaElement, the application must configure the tracks that take part in the media playback. Depending on its needs, the application can add up to one video track and up to one audio track to the source, as described below.
To prepare an ElementaryVideoStreamTrackConfig structure, fill it with an initial configuration of a video track:
ElementaryVideoStreamTrackConfig
samsung::wasm::ElementaryVideoStreamTrackConfig conf; conf.mimeType = "video/mp4; codecs=\"hev1.1.6.L93.B0\""; // h265 conf.extradata = { /* codec extradata */ }; conf.width = 1920; conf.height = 1080; conf.framerateNum = 60; conf.framerateDen = 1; conf.decodingMode = samsung::wasm::DecodingMode::kHardware;
Note The configuration provided when a video track is added is an initial configuration of the track. If the configuration changes during the stream playback (for example, due to an adaptive streaming), the existing track can be reconfigured.
The configuration provided when a video track is added is an initial configuration of the track. If the configuration changes during the stream playback (for example, due to an adaptive streaming), the existing track can be reconfigured.
Note For information on supported codecs and configurations, see Samsung Smart TV Media Specifications.
For information on supported codecs and configurations, see Samsung Smart TV Media Specifications.
To prepare an ElementaryAudioStreamTrackConfig structure, fill it with a configuration of an audio track:
ElementaryAudioStreamTrackConfig
samsung::wasm::ElementaryAudioStreamTrackConfig conf; conf.mimeType = "audio/mp4; codecs=\"mp4a.40.2\""; // AAC conf.extradata = { /* codec extradata */ }; conf.sampleFormat = samsung::wasm::SampleFormat::kPlanarF32; conf.channelLayout = samsung::wasm::ChannelLayout::kStereo; conf.samplesPerSecond = 48000; conf.decodingMode = samsung::wasm::DecodingMode::kHardware;
Note The configuration provided when an audio track is added is the final configuration of the track. Changing configuration during the stream runtime is not possible.
The configuration provided when an audio track is added is the final configuration of the track. Changing configuration during the stream runtime is not possible.
As soon as a track's configuration is ready, the track can be added to the source. This procedure is the same for both audio and video tracks.
Add a track to the source:
auto track = elementary_media_stream_source->AddTrack(conf);
Create an object implementing the ElementaryMediaTrackListener interface and associate it with the track. This step is optional, but the events generated by the track are essential for implementing certain media player functionalities.
ElementaryMediaTrackListener
class MyTrackListener : public samsung::wasm::ElementaryMediaTrackListener { // Override virtual methods that the application uses };
ElementaryMediaTrack
auto my_track_listener = std::make_unique<MyTrackListener>(); track->SetListener(my_track_listener.get());
Note The ElementaryMediaTrack::SetListener() method does not take ownership of the listener. Make sure that the listener outlives the track object.
The ElementaryMediaTrack::SetListener() method does not take ownership of the listener. Make sure that the listener outlives the track object.
ElementaryMediaTrack::SetListener()
Duration of media should be set on the ElementaryMediaStreamSource instance during initialization:
auto content_duration = samsung::wasm::Seconds{42}; elementary_media_stream_source->SetDuration(content_duration);
Once set, the duration is not fixed. It can be changed during runtime if the playback scenario requires it.
Note Media duration cannot be set in the low latency modes, where duration is automatically set to infinite.
Media duration cannot be set in the low latency modes, where duration is automatically set to infinite.
When the app has configured the track layout, ElementaryMediaStreamSource can be initialized by putting it in ReadyState::kOpen and thus enabling playback.
ReadyState::kOpen
Request ElementaryMediaStreamSource to open:
using OperationResult = samsung::wasm::OperationResult; elementary_media_stream_source->Open([](OperationResult result) { if (result != OperationResult::kSuccess) { // handle errors } });
Running the Open() operation on the source immediately puts it in the ReadyState::kOpenPending state. That means the source will enter the ReadyState::kOpen state as soon as possible, however this is not necessarily immediately after the Open() operation finishes.
Open()
ReadyState::kOpenPending