[Multimedia] Add guide for the MediaCodec 64/153664/2
authorcoderhyme <jhyo.kim@samsung.com>
Fri, 29 Sep 2017 05:33:54 +0000 (14:33 +0900)
committerEditor Lionbridge <TizenEditor.SEL@lionbridge.com>
Fri, 29 Sep 2017 12:50:54 +0000 (15:50 +0300)
PS2: Reviewed

Change-Id: Iba75c45ebdcdf539e80f7a184c855a23b3fa3589
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
org.tizen.guides/html/dotnet/media/mediacodec.htm [new file with mode: 0644]

diff --git a/org.tizen.guides/html/dotnet/media/mediacodec.htm b/org.tizen.guides/html/dotnet/media/mediacodec.htm
new file mode 100644 (file)
index 0000000..393469a
--- /dev/null
@@ -0,0 +1,226 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+       <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
+       <meta http-equiv="X-UA-Compatible" content="IE=9" />
+       <link rel="stylesheet" type="text/css" href="../../css/styles.css" />
+       <link rel="stylesheet" type="text/css" href="../../css/snippet.css" />
+       <script type="text/javascript" src="../../scripts/snippet.js"></script>
+       <script type="text/javascript" src="../../scripts/jquery.util.js" charset="utf-8"></script>
+       <script type="text/javascript" src="../../scripts/common.js" charset="utf-8"></script>
+       <script type="text/javascript" src="../../scripts/core.js" charset="utf-8"></script>
+       <script type="text/javascript" src="../../scripts/search.js" charset="utf-8"></script>
+
+  <title>Media Conversions</title>
+
+</head>
+
+<body onload="prettyPrint()" style="overflow: auto;">
+
+<div id="toc-navigation">
+       <div id="toc_border"><div id="toc">
+               <p class="toc-title">Dependencies</p>
+               <ul class="toc">
+                       <li>Tizen 4.0 and Higher</li>
+               </ul>
+
+               <p class="toc-title">Content</p>
+                       <ul class="toc">
+                       <li><a href="#PrepareCodec">Preparing Media Codecs</a></li>
+                       <li><a href="#FillPacket">Filling Media Packets</a></li>
+                       <li><a href="#RunCodec">Running Media Codecs</a></li>
+                       </ul>
+
+               <p class="toc-title">Related Info</p>
+               <ul class="toc">
+                       <li><a href="https://developer.tizen.org/dev-guide/csapi/namespaceTizen_1_1Multimedia_1_1MediaCodec.html">Tizen.Multimedia.MediaCodec namespace</a></li>
+               </ul>
+       </div></div>
+</div>
+
+<div id="container"><div id="contents"><div class="content">
+
+<h1>Media Conversions</h1>
+<p>You can perform various media conversions through codecs. You can encode and decode video and audio data.</p>
+<p>The main features of the Tizen.Multimedia.MediaCodec namespace include:</p>
+  <ul>
+       <li>Preparing media codecs
+       <p><a href="#PrepareCodec">Configure the audio and video codecs</a> and set them as encoders or decoders.</p></li>
+    <li>Filling media packets
+    <p><a href="#FillPacket">Fill the media packet with data</a> by reading data chunks from the input file and writing them to the media packet.</p></li>
+       <li>Running the media codec
+       <p><a href="#RunCodec">Run the media codec loop</a> and retrieve the output packet.</p></li>
+  </ul>
+
+<h2 id="PrepareCodec">Preparing Media Codecs</h2>
+<p>To prepare the media codecs:</p>
+<ol>
+<li>
+<p>Create an instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Multimedia_1_1MediaCodec_1_1MediaCodec.html">Tizen.Multimedia.MediaCodec.MediaCodec</a> class:</p>
+<pre class="prettyprint">
+var mediaCodec = new MediaCodec();
+</pre>
+</li>
+<li>
+<p>Configure the audio and video encoder and decoder, using the <code>Configure()</code> method of the <code>Tizen.Multimedia.MediaCodec.MediaCodec</code> class:</p>
+<ul>
+<li>To set a codec as the encoder, pass <code>true</code> as the second parameter of the <code>Configure()</code> method:
+<pre class="prettyprint">
+/// Configure an audio encoder
+var audioFormat = new AudioMediaFormat(MediaFormatAudioMimeType.Aac, 2, 48000, 16, 128);
+mediaCodec.Configure(audioFormat, true, MediaCodecTypes.Software);
+</pre>
+</li>
+<li>To set a codec as the decoder, pass <code>false</code> as the second parameter of the <code>Configure()</code> method:
+<pre class="prettyprint">
+/// Configure a video decoder
+var videoFormat = new VideoMediaFormat(MediaFormatVideoMimeType.H264SP, 640, 480);
+mediaCodec.Configure(videoFormat, false, MediaCodecTypes.Software);
+</pre>
+</li>
+</ul>
+</li>
+<li>
+<p>To receive notifications whenever the input or output buffers are changed:</p>
+<ol type="a">
+<li>
+<p>To receive notifications when the input buffer is processed, register an event handler for the <code>InputProcessed</code> event of the <code>Tizen.Multimedia.MediaCodec.MediaCodec</code> class.</p>
+<p>The event handler receives the currently-processing input packet:</p>
+<pre class="prettyprint">
+mediaCodec.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}");
+}
+</pre>
+</li>
+<li>
+<p>To receive notifications when the output buffers are dequeued, register an event handler for the <code>OutputAvailable</code> event of the <code>Tizen.Multimedia.MediaCodec.MediaCodec</code> class.</p>
+<p>The event handler receives the result packet:</p>
+<pre class="prettyprint">
+mediaCodec.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();
+}
+</pre>
+</li>
+</ol>
+</li>
+</ol>
+
+<h2 id="FillPacket">Filling Media Packets</h2>
+<p>To create a media packet and fill it with data:</p>
+<ol>
+<li>Create an instance of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Multimedia_1_1MediaPacket.html">Tizen.Multimedia.MediaPacket</a> class:
+<pre class="prettyprint">
+var format = new AudioMediaFormat(MediaFormatAudioMimeType.Pcm, 2, 48000, 16, 128000);
+var packet = MediaPacket.Create(format);
+</pre>
+</li>
+<li>
+Read the data from the input file and fill the packet:
+<pre class="prettyprint">
+using (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;
+}
+</pre>
+</li>
+</ol>
+
+<h2 id="RunCodec">Running Media Codecs</h2>
+<p>After <a href="#PrepareCodec">preparing the media codec</a> and <a href="#FillPacket">filling the media packet with data</a>, run the media codec in the following loop:</p>
+<ol>
+<li>When an input buffer is ready, read a chunk of input and copy it into the buffer to be encoded or decoded.</li>
+<li>When an output buffer is ready, copy the encoded or decoded output from the buffer.</li>
+</ol>
+<p>To run the media codec loop:</p>
+<ol>
+<li>
+<p>Prepare the media codec using the <code>Prepare()</code> method of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Multimedia_1_1MediaCodec_1_1MediaCodec.html">Tizen.Multimedia.MediaCodec.MediaCodec</a> class:</p>
+<pre class="prettyprint">
+mediaCodec.Prepare();
+</pre>
+</li>
+<li>
+<p>Set the media packet buffer flag using the <code>BufferFlags</code> property of the <a href="https://developer.tizen.org/dev-guide/csapi/classTizen_1_1Multimedia_1_1MediaPacket.html">Tizen.Multimedia.MediaPacket</a> class:</p>
+<pre class="prettyprint">
+/// 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;
+</pre>
+</li>
+<li>
+<p>To encode or decode packets, start the media codec loop and call the <code>ProcessInput()</code> method:</p>
+<pre class="prettyprint">
+mediaCodec.ProcessInput(packet);
+</pre>
+</li>
+<li>
+<p>To receive the output packet whenever it is available, register an event handler for the <code>OutputAvailable</code> event of the <code>Tizen.Multimedia.MediaCodec.MediaCodec</code> class.</p>
+<p>Within the event handler, check whether the output media packet contains key frame or codec data:</p>
+<pre class="prettyprint">
+/// 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();
+}
+</pre>
+</li>
+<li>
+<p>After the loop is over and you have finished working with the media codec, reset the codec using the <code>Unprepare()</code> method:</p>
+<pre class="prettyprint">
+mediaCodec.Unprepare();
+</pre>
+</li>
+</ol>
+
+<script type="text/javascript" src="../../scripts/jquery.zclip.min.js"></script>
+<script type="text/javascript" src="../../scripts/showhide.js"></script>
+</div></div></div>
+
+
+<a class="top sms" href="#"><img src="../../images/btn_top.gif" alt="Go to top" /></a>
+
+<div id="footer">
+<p class="footer">Except as noted, this content - excluding the Code Examples - is licensed under <a href="http://creativecommons.org/licenses/by/3.0/legalcode" target="_blank">Creative Commons Attribution 3.0</a> and all of the Code Examples contained herein are licensed under <a href="https://www.tizen.org/bsd-3-clause-license" target="_blank">BSD-3-Clause</a>.<br/>For details, see the <a href="https://www.tizen.org/content-license" target="_blank">Content License</a>.</p>
+</div>
+
+<script type="text/javascript">
+var _gaq = _gaq || [];
+_gaq.push(['_setAccount', 'UA-25976949-1']);
+_gaq.push(['_trackPageview']);
+(function() {
+var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+})();
+</script>
+
+</body>
+</html>