Example #1
0
        /// <summary>
        ///     Initializes the DmoStream. Important: This has to be called before using the DmoStream.
        /// </summary>
        protected void Initialize()
        {
            _inputFormat  = GetInputFormat();
            _outputFormat = GetOutputFormat();

            _ratio = _outputFormat.BytesPerSecond / (double)_inputFormat.BytesPerSecond;

            _mediaObject = CreateMediaObject(_inputFormat, _outputFormat);

            //Setup Mediatype
            if (_mediaObject.SupportsInputFormat(InputIndex, _inputFormat) == false)
            {
                throw new NotSupportedException("Inputformat is not supported.");
            }
            _mediaObject.SetInputType(InputIndex, _inputFormat);

            if (_mediaObject.SupportsOutputFormat(OutputIndex, _outputFormat) == false)
            {
                throw new NotSupportedException("Outputformat is not supported.");
            }
            _mediaObject.SetOutputType(OutputIndex, _outputFormat);

            //Create Mediabuffers
            _inputDataBuffer  = new MediaBuffer(_inputFormat.BytesPerSecond / 2);
            _outputDataBuffer = new DmoOutputDataBuffer(_outputFormat.BytesPerSecond / 2);

            _isInitialized = true;
        }
Example #2
0
        public virtual int Read(byte[] buffer, int offset, int count)
        {
            if (!_isInitialized)
            {
                throw new InvalidOperationException("DmoStream is not initialized.");
            }

            int read = 0;

            while (read < count)
            {
                if (_disposed)
                {
                    break;
                }

                //check for overflows
                if (_outputDataBufferOverflows != 0)
                {
                    int overflowsRead = _outputDataBuffer.Read(buffer, offset + read, count - read,
                                                               _outputDataBufferOffset);
                    read += overflowsRead;

                    _outputDataBufferOverflows -= overflowsRead;
                    _outputDataBufferOffset    += overflowsRead;

                    continue;
                }

                bool isIncomplete = (_outputDataBuffer.Status & OutputDataBufferFlags.Incomplete) ==
                                    OutputDataBufferFlags.Incomplete;
                bool isReadyForInput = _mediaObject.IsReadyForInput(InputIndex);

                //Process data if
                //  the MediaObject is ready for input
                //  there is no data to process left
                if (isReadyForInput && isIncomplete == false)
                {
                    var bytesToRead = (int)OutputToInput(Math.Max(0, count - read));
                    int bytesRead   = GetInputData(ref _inputBuffer, bytesToRead);
                    if (bytesRead == 0)
                    {
                        break;
                    }

                    if (_disposed)
                    {
                        break;
                    }

                    if (_inputDataBuffer.MaxLength < bytesRead)
                    {
                        _inputDataBuffer.Dispose();
                        _inputDataBuffer = new MediaBuffer(bytesRead);
                    }
                    _inputDataBuffer.Write(_inputBuffer, 0, bytesRead);

                    _mediaObject.ProcessInput(0, _inputDataBuffer);
                }

                //If there is no data left
                //   -> reset the outputDataBuffer
                if (isIncomplete == false)
                {
                    _outputDataBuffer.Reset();
                }

                //Process output data
                _mediaObject.ProcessOutput(ProcessOutputFlags.None, _outputDataBuffer);

                _outputDataBufferOffset    = 0;
                _outputDataBufferOverflows = _outputDataBuffer.Length;

                if (_outputDataBuffer.Length <= 0)
                {
                    Debug.WriteLine("No data in output buffer.");
                    continue; //todo:
                }

                //if there is less data available than requested (count) -> outputDataRead will be the actual number of bytes read.
                int outputDataRead = _outputDataBuffer.Read(buffer, offset + read, count - read);
                read += outputDataRead;

                _outputDataBufferOverflows -= outputDataRead;
                _outputDataBufferOffset    += outputDataRead;
            }

            return(read);
        }