Example #1
0
        private void ConfigureVideo(VideoMediaFormat format, bool encoder,
                                    MediaCodecTypes supportType)
        {
            int codecType = (int)format.MimeType & CodecTypeMask;

            if (!Enum.IsDefined(typeof(SupportedCodecType), codecType))
            {
                throw new NotSupportedException("The format is not supported." +
                                                $"mime type : { Enum.GetName(typeof(MediaFormatVideoMimeType), format.MimeType) }");
            }

            DoConfigure(codecType, encoder, supportType);

            if (encoder)
            {
                int ret = Interop.MediaCodec.SetVideoEncoderInfo(_handle, format.Size.Width,
                                                                 format.Size.Height, format.FrameRate, format.BitRate / 1000);

                MultimediaDebug.AssertNoError(ret);
            }
            else
            {
                int ret = Interop.MediaCodec.SetVideoDecoderInfo(_handle, format.Size.Width, format.Size.Height);

                MultimediaDebug.AssertNoError(ret);
            }
        }
Example #2
0
        private void RegisterOutputAvailableCallback()
        {
            _outputBufferAvailableCb = (packetHandle, _) =>
            {
                if (_outputAvailable == null)
                {
                    Interop.MediaPacket.Destroy(packetHandle);
                    return;
                }

                OutputAvailableEventArgs args = null;
                try
                {
                    args = new OutputAvailableEventArgs(packetHandle);
                }
                catch (Exception e)
                {
                    Interop.MediaPacket.Destroy(packetHandle);

                    MultimediaLog.Error(typeof(MediaCodec).FullName, "Failed to raise OutputAvailable event", e);
                }

                if (args != null)
                {
                    _outputAvailable?.Invoke(this, args);
                }
            };

            int ret = Interop.MediaCodec.SetOutputBufferAvailableCb(_handle, _outputBufferAvailableCb);

            MultimediaDebug.AssertNoError(ret);
        }
Example #3
0
        private void ConfigureAudio(AudioMediaFormat format, bool encoder,
                                    MediaCodecTypes supportType)
        {
            int codecType = (int)format.MimeType & CodecTypeMask;

            if (!Enum.IsDefined(typeof(SupportedCodecType), codecType))
            {
                throw new NotSupportedException("The format is not supported " +
                                                $"mime type : { Enum.GetName(typeof(MediaFormatAudioMimeType), format.MimeType) }");
            }

            DoConfigure(codecType, encoder, supportType);

            if (encoder)
            {
                int ret = Interop.MediaCodec.SetAudioEncoderInfo(_handle, format.SampleRate,
                                                                 format.Channel, format.Bit, format.BitRate);

                MultimediaDebug.AssertNoError(ret);
            }
            else
            {
                int ret = Interop.MediaCodec.SetAudioDecoderInfo(_handle, format.SampleRate,
                                                                 format.Channel, format.Bit);

                MultimediaDebug.AssertNoError(ret);
            }
        }
Example #4
0
        private static void LoadSupportedCodec()
        {
            var videoCodecList = new List <MediaFormatVideoMimeType>();
            var audioCodecList = new List <MediaFormatAudioMimeType>();

            Interop.MediaCodec.SupportedCodecCallback cb = (codecType, _) =>
            {
                if ((codecType & CodecKindMask) == CodecKindVideo)
                {
                    MediaFormatVideoMimeType mimeType = 0;
                    if (TryGetMimeTypeFromCodecType(codecType, ref mimeType))
                    {
                        videoCodecList.Add(mimeType);
                    }
                }
                else
                {
                    MediaFormatAudioMimeType mimeType = 0;
                    if (TryGetMimeTypeFromCodecType(codecType, ref mimeType))
                    {
                        audioCodecList.Add(mimeType);
                    }
                }

                return(true);
            };

            int ret = Interop.MediaCodec.ForeachSupportedCodec(cb, IntPtr.Zero);

            MultimediaDebug.AssertNoError(ret);

            _supportedVideoCodecs = videoCodecList.AsReadOnly();
            _supportedAudioCodecs = audioCodecList.AsReadOnly();
        }
Example #5
0
        private void RegisterEosReached()
        {
            _eosCb = _ => EosReached?.Invoke(this, EventArgs.Empty);

            int ret = Interop.MediaCodec.SetEosCb(_handle, _eosCb);

            MultimediaDebug.AssertNoError(ret);
        }
Example #6
0
        /// <summary>
        /// Flushes both input and output buffers.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public void FlushBuffers()
        {
            ValidateNotDisposed();

            int ret = Interop.MediaCodec.FlushBuffers(_handle);

            MultimediaDebug.AssertNoError(ret);
        }
Example #7
0
        /// <summary>
        /// Unprepares the MediaCodec.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public void Unprepare()
        {
            ValidateNotDisposed();

            int ret = Interop.MediaCodec.Unprepare(_handle);

            MultimediaDebug.AssertNoError(ret);
        }
Example #8
0
        private MediaCodecTypes GetCodecType(int mimeType, bool isEncoder)
        {
            int codecType = mimeType & CodecTypeMask;
            int value     = 0;

            int ret = Interop.MediaCodec.GetSupportedType(_handle, codecType, isEncoder, out value);

            MultimediaDebug.AssertNoError(ret);

            return((MediaCodecTypes)value);
        }
Example #9
0
        private void RegisterErrorOccurred()
        {
            _errorCb = (errorCode, _) =>
            {
                MediaCodecError error = (Enum.IsDefined(typeof(MediaCodecError), errorCode)) ?
                                        (MediaCodecError)errorCode : MediaCodecError.InternalError;

                ErrorOccurred?.Invoke(this, new MediaCodecErrorOccurredEventArgs(error));
            };
            int ret = Interop.MediaCodec.SetErrorCb(_handle, _errorCb);

            MultimediaDebug.AssertNoError(ret);
        }
Example #10
0
        /// <summary>
        /// Initializes a new instance of the MediaCodec class.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public MediaCodec()
        {
            int ret = Interop.MediaCodec.Create(out _handle);

            if (ret == (int)MediaCodecErrorCode.InvalidOperation)
            {
                throw new InvalidOperationException("Not able to initialize a new media codec.");
            }

            MultimediaDebug.AssertNoError(ret);

            RegisterInputProcessed();
            RegisterErrorOccurred();
        }
Example #11
0
        private void RegisterBufferStatusChanged()
        {
            _bufferStatusCb = (statusCode, _) =>
            {
                Debug.Assert(Enum.IsDefined(typeof(MediaCodecStatus), statusCode),
                             $"{ statusCode } is not defined in MediaCodecStatus!");

                BufferStatusChanged?.Invoke(this,
                                            new BufferStatusChangedEventArgs((MediaCodecStatus)statusCode));
            };

            int ret = Interop.MediaCodec.SetBufferStatusCb(_handle, _bufferStatusCb);

            MultimediaDebug.AssertNoError(ret);
        }
Example #12
0
        private void DoConfigure(int codecType, bool encoder, MediaCodecTypes supportType)
        {
            Debug.Assert(Enum.IsDefined(typeof(SupportedCodecType), codecType));

            int flags = (int)(encoder ? MediaCodecCodingType.Encoder : MediaCodecCodingType.Decoder);

            flags |= (int)supportType;

            int ret = Interop.MediaCodec.Configure(_handle, codecType, flags);

            if (ret == (int)MediaCodecErrorCode.NotSupportedOnDevice)
            {
                throw new NotSupportedException("The format is not supported.");
            }
            MultimediaDebug.AssertNoError(ret);
        }
Example #13
0
        /// <summary>
        /// Prepares the MediaCodec for encoding or decoding.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        ///     The codec is not configured yet.<br/>
        ///     -or-<br/>
        ///     Internal error.
        /// </exception>
        /// <since_tizen> 3 </since_tizen>
        public void Prepare()
        {
            ValidateNotDisposed();

            int ret = Interop.MediaCodec.Prepare(_handle);

            if (ret == (int)MediaCodecErrorCode.NotInitialized)
            {
                throw new InvalidOperationException("The codec is not configured.");
            }
            if (ret != (int)MediaCodecErrorCode.None)
            {
                throw new InvalidOperationException("Operation failed.");
            }

            MultimediaDebug.AssertNoError(ret);
        }
Example #14
0
        /// <summary>
        /// Adds the packet to the internal queue of the codec.
        /// </summary>
        /// <param name="packet">The packet to be encoded or decoded.</param>
        /// <exception cref="ArgumentNullException"><paramref name="packet"/> is null.</exception>
        /// <exception cref="InvalidOperationException">The current codec is not prepared yet.</exception>
        /// <remarks>Any attempts to modify the packet will fail until the <see cref="InputProcessed"/> event for the packet is invoked.</remarks>
        /// <since_tizen> 3 </since_tizen>
        public void ProcessInput(MediaPacket packet)
        {
            ValidateNotDisposed();

            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            MediaPacket.Lock packetLock = MediaPacket.Lock.Get(packet);

            int ret = Interop.MediaCodec.Process(_handle, packetLock.GetHandle(), 0);

            if (ret == (int)MediaCodecErrorCode.InvalidState)
            {
                throw new InvalidOperationException("The codec is in invalid state.");
            }

            MultimediaDebug.AssertNoError(ret);
        }
Example #15
0
        private void RegisterInputProcessed()
        {
            _inputBufferUsedCb = (lockedPacketHandle, _) =>
            {
                MediaPacket packet = null;

                // Lock must be disposed here, note that the packet won't be disposed.
                using (MediaPacket.Lock packetLock =
                           MediaPacket.Lock.FromHandle(lockedPacketHandle))
                {
                    Debug.Assert(packetLock != null);

                    packet = packetLock.MediaPacket;
                }
                Debug.Assert(packet != null);

                InputProcessed?.Invoke(this, new InputProcessedEventArgs(packet));
            };

            int ret = Interop.MediaCodec.SetInputBufferUsedCb(_handle, _inputBufferUsedCb);

            MultimediaDebug.AssertNoError(ret);
        }
Example #16
0
        private void UnregisterOutputAvailableCallback()
        {
            int ret = Interop.MediaCodec.UnsetOutputBufferAvailableCb(_handle);

            MultimediaDebug.AssertNoError(ret);
        }