Media Conversions
You can perform various media conversions through codecs. You can encode and decode video and audio data.
The main features of the Tizen.Multimedia.MediaCodec namespace include the following:
-
Preparing media codecs
Configure the audio and video codecs and set them as encoders or decoders.
-
Filling media packets
Fill the media packet with data by reading data chunks from the input file and writing them to the media packet.
-
Running the media codec
Run the media codec loop and retrieve the output packet.
Prepare media codecs
To prepare the media codecs, proceed as follows:
-
Create an instance of the Tizen.Multimedia.MediaCodec.MediaCodec class:
C#Copyvar mediaCodec = new MediaCodec();
-
Configure the audio and video encoder and decoder, using the
Configure()
method of theTizen.Multimedia.MediaCodec.MediaCodec
class:-
To set a codec as the encoder, pass
true
as the second parameter of theConfigure()
method:C#Copy/// Configure an audio encoder var audioFormat = new AudioMediaFormat(MediaFormatAudioMimeType.Aac, 2, 48000, 16, 128); mediaCodec.Configure(audioFormat, true, MediaCodecTypes.Software);
-
To set a codec as the decoder, pass
false
as the second parameter of theConfigure()
method:C#Copy/// Configure a video decoder var videoFormat = new VideoMediaFormat(MediaFormatVideoMimeType.H264SP, 640, 480); mediaCodec.Configure(videoFormat, false, MediaCodecTypes.Software);
-
-
To receive notifications whenever the input or output buffers are changed:
-
To receive notifications when the input buffer is processed, register an event handler for the
InputProcessed
event of theTizen.Multimedia.MediaCodec.MediaCodec
class.The event handler receives the currently-processing input packet:
C#CopymediaCodec.InputProcessed += OnInputProcessed; void OnInputProcessed(object sender, InputProcessedEventArgs e) { Tizen.Log.Info(LogTag, $"The packet format is {e.Packet.Format} and buffer written length is {e.Packet.BufferWrittenLength}"); }
-
To receive notifications when the output buffers are dequeued, register an event handler for the
OutputAvailable
event of theTizen.Multimedia.MediaCodec.MediaCodec
class.The event handler receives the result packet:
C#CopymediaCodec.OutputAvailable += OnOutputAvailable; void OnOutputAvailable(object sender, OutputAvailableEventArgs e) { Tizen.Log.Info(LogTag, $"The packet format is {e.Packet.Format} and buffer written length is {e.Packet.BufferWrittenLength}"); e.Packet.Dispose(); }
-
Fill media packets
To create a media packet and fill it with data, proceed as follows:
-
Create an instance of the Tizen.Multimedia.MediaPacket class:
C#Copyvar format = new AudioMediaFormat(MediaFormatAudioMimeType.Pcm, 2, 48000, 16, 128000); var packet = MediaPacket.Create(format);
-
Read the data from the input file and fill the packet:
C#Copyusing (var fs = File.OpenRead(filePath) { int readSize = 1024 * 2 * 2 * 2; byte[] arr = new byte[readSize]; fs.Read(arr, 0, readSize); packet.Buffer.CopyFrom(arr, 0, readSize); packet.BufferWrittenLength = readSize; }
Run media codecs
After preparing the media codec and filling the media packet with data, run the media codec in the following loop:
- When an input buffer is ready, read a chunk of input and copy it into the buffer to be encoded or decoded.
- When an output buffer is ready, copy the encoded or decoded output from the buffer.
To run the media codec loop, proceed as follows:
-
Prepare the media codec using the
Prepare()
method of the Tizen.Multimedia.MediaCodec.MediaCodec class:C#CopymediaCodec.Prepare();
-
Set the media packet buffer flag using the
BufferFlags
property of the Tizen.Multimedia.MediaPacket class:C#Copy/// If the MediaPacket contains codec-specific data, such as SPS or PPS for H.264, set the CodecConfig flag packet.BufferFlags |= MediaPacketBufferFlags.CodecConfig; /// If the MediaPacket contains the end of stream, set the EOS flag packet.BufferFlags |= MediaPacketBufferFlags.EndOfStream;
-
To encode or decode packets, start the media codec loop and call the
ProcessInput()
method:C#CopymediaCodec.ProcessInput(packet);
-
To receive the output packet whenever it is available, register an event handler for the
OutputAvailable
event of theTizen.Multimedia.MediaCodec.MediaCodec
class.Within the event handler, check whether the output media packet contains key frame or codec data:
C#Copy/// Register an event handler to receive output packet mediaCodec.OutputAvailable += OnOutputAvailable; /// Define the event handler void OnOutputAvailable(object sender, OutputAvailableEventArgs e) { if (e.Packet.BufferFlags.HasFlag(MediaPacketBufferFlags.SyncFrame)) { Tizen.Log.Info(LogTag, "The packet contains key frame"); } if (e.Packet.BufferFlags.HasFlag(MediaPacketBufferFlags.CodecConfig)) { Tizen.Log.Info(LogTag, "The packet contains codec frame"); } e.Packet.Dispose(); }
-
After the loop is over and you have finished working with the media codec, reset the codec using the
Unprepare()
method:C#CopymediaCodec.Unprepare();
Related information
- Dependencies
- Tizen 4.0 and Higher