2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Diagnostics;
21 namespace Tizen.Multimedia.MediaCodec
24 /// Provides the means to encode and decode video and audio data.
26 public class MediaCodec : IDisposable
28 private const int CodecTypeMask = 0xFFFF;
29 private const int CodecKindMask = 0x3000;
30 // private const int CodecKindAudio = 0x1000; // Not used
31 private const int CodecKindVideo = 0x2000;
33 private IntPtr _handle;
36 /// Initialize a new instance of the MediaCodec class.
40 int ret = Interop.MediaCodec.Create(out _handle);
42 if (ret == (int)MediaCodecErrorCode.InvalidOperation)
44 throw new InvalidOperationException("Not able to initialize a new media codec.");
47 MultimediaDebug.AssertNoError(ret);
49 RegisterInputProcessed();
50 RegisterErrorOccurred();
53 #region IDisposable-support
54 private bool _isDisposed = false;
56 protected virtual void Dispose(bool disposing)
60 if (_handle != IntPtr.Zero)
62 Interop.MediaCodec.Destroy(_handle);
63 _handle = IntPtr.Zero;
79 GC.SuppressFinalize(this);
84 /// Validates if the object already has been disposed of.
86 /// <exception cref="ObjectDisposedException">The current object has been disposed of.</exception>
87 private void ValidateNotDisposed()
91 throw new ObjectDisposedException(nameof(MediaCodec));
95 private static IEnumerable<MediaFormatVideoMimeType> _supportedVideoCodecs;
98 /// Gets the audio codec list that the current device supports.
100 public static IEnumerable<MediaFormatVideoMimeType> SupportedVideoCodecs
104 if (_supportedVideoCodecs == null)
106 LoadSupportedCodec();
109 return _supportedVideoCodecs;
113 private static IEnumerable<MediaFormatAudioMimeType> _supportedAudioCodecs;
117 /// Gets the audio codec list that the current device supports.
119 public static IEnumerable<MediaFormatAudioMimeType> SupportedAudioCodecs
123 if (_supportedAudioCodecs == null)
125 LoadSupportedCodec();
128 return _supportedAudioCodecs;
132 private static bool TryGetMimeTypeFromCodecType<T>(int codecType, ref T result)
139 foreach (T value in Enum.GetValues(typeof(T)))
141 if ((Convert.ToInt32(value) & CodecTypeMask) == codecType)
148 Debug.Fail($"Unknown codec : { codecType }.");
152 private static void LoadSupportedCodec()
154 var videoCodecList = new List<MediaFormatVideoMimeType>();
155 var audioCodecList = new List<MediaFormatAudioMimeType>();
157 Interop.MediaCodec.SupportedCodecCallback cb = (codecType, _) =>
159 if ((codecType & CodecKindMask) == CodecKindVideo)
161 MediaFormatVideoMimeType mimeType = 0;
162 if (TryGetMimeTypeFromCodecType(codecType, ref mimeType))
164 videoCodecList.Add(mimeType);
169 MediaFormatAudioMimeType mimeType = 0;
170 if (TryGetMimeTypeFromCodecType(codecType, ref mimeType))
172 audioCodecList.Add(mimeType);
179 int ret = Interop.MediaCodec.ForeachSupportedCodec(cb, IntPtr.Zero);
181 MultimediaDebug.AssertNoError(ret);
183 _supportedVideoCodecs = videoCodecList.AsReadOnly();
184 _supportedAudioCodecs = audioCodecList.AsReadOnly();
188 /// Prepares the MediaCodec for encoding or decoding.
190 /// <exception cref="InvalidOperationException">
191 /// The codec is not configured, yet.\n
195 public void Prepare()
197 ValidateNotDisposed();
199 int ret = Interop.MediaCodec.Prepare(_handle);
201 if (ret == (int)MediaCodecErrorCode.NotInitialized)
203 throw new InvalidOperationException("The codec is not configured.");
205 if (ret != (int)MediaCodecErrorCode.None)
207 throw new InvalidOperationException("Operation failed.");
210 MultimediaDebug.AssertNoError(ret);
214 /// Unprepares the MediaCodec.
216 public void Unprepare()
218 ValidateNotDisposed();
220 int ret = Interop.MediaCodec.Unprepare(_handle);
222 MultimediaDebug.AssertNoError(ret);
226 /// Configures the MediaCodec.
228 /// <param name="format">The <see cref="MediaFormat"/> for properties of media data to decode or encode.</param>
229 /// <param name="encoder">The value indicating whether the codec works as a encoder or a decoder.</param>
230 /// <param name="codecType">The value indicating whether the codec uses hardware acceleration.</param>
231 /// <exception cref="ArgumentNullException">format is null</exception>
232 /// <exception cref="ArgumentException">
233 /// <paramref name="codecType"/> is invalid.\n
235 /// <paramref name="format"/> is neither audio type nor video type.
237 /// <exception cref="NotSupportedException">the mime type of the format is not supported.</exception>
238 /// <see cref="SupportedAudioCodecs"/>
239 /// <see cref="SupportedVideoCodecs"/>
240 public void Configure(MediaFormat format, bool encoder, MediaCodecTypes codecType)
242 ValidateNotDisposed();
246 throw new ArgumentNullException(nameof(format));
249 if (codecType != MediaCodecTypes.Hardware && codecType != MediaCodecTypes.Software)
251 throw new ArgumentException("codecType is invalid.");
254 if (format.Type == MediaFormatType.Audio)
256 ConfigureAudio((AudioMediaFormat)format, encoder, codecType);
258 else if (format.Type == MediaFormatType.Video)
260 ConfigureVideo((VideoMediaFormat)format, encoder, codecType);
264 throw new ArgumentException("Only video and audio formats are allowed.");
268 private void ConfigureAudio(AudioMediaFormat format, bool encoder,
269 MediaCodecTypes supportType)
271 int codecType = (int)format.MimeType & CodecTypeMask;
273 if (!Enum.IsDefined(typeof(SupportedCodecType), codecType))
275 throw new NotSupportedException("The format is not supported " +
276 $"mime type : { Enum.GetName(typeof(MediaFormatAudioMimeType), format.MimeType) }");
279 DoConfigure(codecType, encoder, supportType);
283 int ret = Interop.MediaCodec.SetAudioEncoderInfo(_handle, format.SampleRate,
284 format.Channel, format.Bit, format.BitRate);
286 MultimediaDebug.AssertNoError(ret);
290 int ret = Interop.MediaCodec.SetAudioDecoderInfo(_handle, format.SampleRate,
291 format.Channel, format.Bit);
293 MultimediaDebug.AssertNoError(ret);
297 private void ConfigureVideo(VideoMediaFormat format, bool encoder,
298 MediaCodecTypes supportType)
300 int codecType = (int)format.MimeType & CodecTypeMask;
302 if (!Enum.IsDefined(typeof(SupportedCodecType), codecType))
304 throw new NotSupportedException("The format is not supported." +
305 $"mime type : { Enum.GetName(typeof(MediaFormatVideoMimeType), format.MimeType) }");
308 DoConfigure(codecType, encoder, supportType);
312 int ret = Interop.MediaCodec.SetVideoEncoderInfo(_handle, format.Size.Width,
313 format.Size.Height, format.FrameRate, format.BitRate / 1000);
315 MultimediaDebug.AssertNoError(ret);
319 int ret = Interop.MediaCodec.SetVideoDecoderInfo(_handle, format.Size.Width, format.Size.Height);
321 MultimediaDebug.AssertNoError(ret);
325 private void DoConfigure(int codecType, bool encoder, MediaCodecTypes supportType)
327 Debug.Assert(Enum.IsDefined(typeof(SupportedCodecType), codecType));
329 int flags = (int)(encoder ? MediaCodecCodingType.Encoder : MediaCodecCodingType.Decoder);
331 flags |= (int)supportType;
333 int ret = Interop.MediaCodec.Configure(_handle, codecType, flags);
335 if (ret == (int)MediaCodecErrorCode.NotSupportedOnDevice)
337 throw new NotSupportedException("The format is not supported.");
339 MultimediaDebug.AssertNoError(ret);
343 /// Adds the packet to the internal queue of the codec.
345 /// <param name="packet">The packet to be encoded or decoded.</param>
346 /// <exception cref="ArgumentNullException">packet is null.</exception>
347 /// <exception cref="InvalidOperationException">the current codec is not prepared, yet.</exception>
348 /// <remarks>Any attempts to modify the packet will be failed until the <see cref="InputProcessed"/> event for the packet is invoked.</remarks>
349 public void ProcessInput(MediaPacket packet)
351 ValidateNotDisposed();
355 throw new ArgumentNullException(nameof(packet));
358 MediaPacket.Lock packetLock = MediaPacket.Lock.Get(packet);
360 int ret = Interop.MediaCodec.Process(_handle, packetLock.GetHandle(), 0);
362 if (ret == (int)MediaCodecErrorCode.InvalidState)
364 throw new InvalidOperationException("The codec is in invalid state.");
367 MultimediaDebug.AssertNoError(ret);
371 /// Flushes both input and output buffers.
373 public void FlushBuffers()
375 ValidateNotDisposed();
377 int ret = Interop.MediaCodec.FlushBuffers(_handle);
379 MultimediaDebug.AssertNoError(ret);
383 /// Retrieves supported codec types for the specified params.
385 /// <param name="encoder">The value indicating encoder or decoder.</param>
386 /// <param name="type">The mime type to query.</param>
387 /// <returns>The values indicating which codec types are supported on the current device.</returns>
388 /// <exception cref="ArgumentException">type is invalid.</exception>
389 public MediaCodecTypes GetCodecType(bool encoder, MediaFormatVideoMimeType type)
391 ValidateNotDisposed();
393 if (CheckMimeType(typeof(MediaFormatVideoMimeType), (int)type) == false)
398 return GetCodecType((int)type, encoder);
402 /// Retrieves supported codec types for the specified params.
404 /// <param name="encoder">The value indicating encoder or decoder.</param>
405 /// <param name="type">The mime type to query.</param>
406 /// <returns>The values indicating which codec types are supported on the current device.</returns>
407 /// <exception cref="ArgumentException">type is invalid.</exception>
408 public MediaCodecTypes GetCodecType(bool encoder, MediaFormatAudioMimeType type)
410 ValidateNotDisposed();
412 if (CheckMimeType(typeof(MediaFormatAudioMimeType), (int)type) == false)
417 return GetCodecType((int)type, encoder);
420 private MediaCodecTypes GetCodecType(int mimeType, bool isEncoder)
422 int codecType = mimeType & CodecTypeMask;
425 int ret = Interop.MediaCodec.GetSupportedType(_handle, codecType, isEncoder, out value);
427 MultimediaDebug.AssertNoError(ret);
429 return (MediaCodecTypes)value;
432 private bool CheckMimeType(Type type, int value)
434 int codecType = value & CodecTypeMask;
436 if (!Enum.IsDefined(type, value))
438 throw new ArgumentException($"The mime type value is invalid : { value }.");
441 return Enum.IsDefined(typeof(SupportedCodecType), codecType);
444 #region OutputAvailable event
445 private EventHandler<OutputAvailableEventArgs> _outputAvailable;
446 private Interop.MediaCodec.OutputBufferAvailableCallback _outputBufferAvailableCb;
447 private object _outputAvailableLock = new object();
450 /// Occurs when an output buffer is available.
452 /// <remarks>The output packet needs to be disposed after it is used to clean up unmanaged resources.</remarks>
453 public event EventHandler<OutputAvailableEventArgs> OutputAvailable
457 ValidateNotDisposed();
459 lock (_outputAvailableLock)
461 if (_outputAvailable == null)
463 RegisterOutputAvailableCallback();
465 _outputAvailable += value;
470 ValidateNotDisposed();
472 lock (_outputAvailableLock)
474 _outputAvailable -= value;
475 if (_outputAvailable == null)
477 // We can remove handler first, because we know the method that unregisters callback does not throw.
478 UnregisterOutputAvailableCallback();
484 private void RegisterOutputAvailableCallback()
486 _outputBufferAvailableCb = (packetHandle, _) =>
488 if (_outputAvailable == null)
490 Interop.MediaPacket.Destroy(packetHandle);
494 OutputAvailableEventArgs args = null;
497 args = new OutputAvailableEventArgs(packetHandle);
501 Interop.MediaPacket.Destroy(packetHandle);
503 MultimediaLog.Error(typeof(MediaCodec).FullName, "Failed to raise OutputAvailable event", e);
508 _outputAvailable?.Invoke(this, args);
512 int ret = Interop.MediaCodec.SetOutputBufferAvailableCb(_handle, _outputBufferAvailableCb);
514 MultimediaDebug.AssertNoError(ret);
517 private void UnregisterOutputAvailableCallback()
519 int ret = Interop.MediaCodec.UnsetOutputBufferAvailableCb(_handle);
521 MultimediaDebug.AssertNoError(ret);
525 #region InputProcessed event
526 private Interop.MediaCodec.InputBufferUsedCallback _inputBufferUsedCb;
529 /// Occurs when an input packet is processed.
531 /// <see cref="ProcessInput(MediaPacket)"/>
532 public event EventHandler<InputProcessedEventArgs> InputProcessed;
534 private void RegisterInputProcessed()
536 _inputBufferUsedCb = (lockedPacketHandle, _) =>
538 MediaPacket packet = null;
540 // Lock must be disposed here, note that the packet won't be disposed.
541 using (MediaPacket.Lock packetLock =
542 MediaPacket.Lock.FromHandle(lockedPacketHandle))
544 Debug.Assert(packetLock != null);
546 packet = packetLock.MediaPacket;
548 Debug.Assert(packet != null);
550 InputProcessed?.Invoke(this, new InputProcessedEventArgs(packet));
553 int ret = Interop.MediaCodec.SetInputBufferUsedCb(_handle, _inputBufferUsedCb);
555 MultimediaDebug.AssertNoError(ret);
559 #region ErrorOccurred event
560 private Interop.MediaCodec.ErrorCallback _errorCb;
563 /// Occurs whenever an error is produced in the codec.
565 public event EventHandler<MediaCodecErrorOccurredEventArgs> ErrorOccurred;
567 private void RegisterErrorOccurred()
569 _errorCb = (errorCode, _) =>
571 MediaCodecError error = (Enum.IsDefined(typeof(MediaCodecError), errorCode)) ?
572 (MediaCodecError)errorCode : MediaCodecError.InternalError;
574 ErrorOccurred?.Invoke(this, new MediaCodecErrorOccurredEventArgs(error));
576 int ret = Interop.MediaCodec.SetErrorCb(_handle, _errorCb);
578 MultimediaDebug.AssertNoError(ret);
582 #region EosReached event
583 private Interop.MediaCodec.EosCallback _eosCb;
586 /// Occurs when the codec processes all input data.
588 public event EventHandler<EventArgs> EosReached;
590 private void RegisterEosReached()
592 _eosCb = _ => EosReached?.Invoke(this, EventArgs.Empty);
594 int ret = Interop.MediaCodec.SetEosCb(_handle, _eosCb);
596 MultimediaDebug.AssertNoError(ret);
601 #region BufferStatusChanged event
602 private Interop.MediaCodec.BufferStatusCallback _bufferStatusCb;
605 /// Occurs when the codec needs more data or has enough data.
607 public event EventHandler<BufferStatusChangedEventArgs> BufferStatusChanged;
609 private void RegisterBufferStatusChanged()
611 _bufferStatusCb = (statusCode, _) =>
613 Debug.Assert(Enum.IsDefined(typeof(MediaCodecStatus), statusCode),
614 $"{ statusCode } is not defined in MediaCodecStatus!");
616 BufferStatusChanged?.Invoke(this,
617 new BufferStatusChangedEventArgs((MediaCodecStatus)statusCode));
620 int ret = Interop.MediaCodec.SetBufferStatusCb(_handle, _bufferStatusCb);
622 MultimediaDebug.AssertNoError(ret);