Exemple #1
0
        public static string LongName(this MediaFormat value)
        {
            FieldInfo         field = value.GetType().GetField(value.ToString());
            LongNameAttribute attribute
                = Attribute.GetCustomAttribute(field, typeof(LongNameAttribute))
                  as LongNameAttribute;

            return(attribute == null?value.ToString() : attribute.LongName);
        }
Exemple #2
0
        public static string Command(this MediaFormat value)
        {
            FieldInfo        field = value.GetType().GetField(value.ToString());
            CommandAttribute attribute
                = Attribute.GetCustomAttribute(field, typeof(CommandAttribute))
                  as CommandAttribute;

            return(attribute == null?value.ToString() : attribute.Command);
        }
        public int Read(byte[] buffer, int offset, int length)
        {
            int min = 0;

            try
            {
                if (mBuffer == null)
                {
                    while (!Thread.Interrupted() && !mClosed)
                    {
                        mIndex = mMediaCodec.DequeueOutputBuffer(mBufferInfo, 500000);
                        if (mIndex >= 0)
                        {
                            //Log.d(TAG,"Index: "+mIndex+" Time: "+mBufferInfo.presentationTimeUs+" size: "+mBufferInfo.size);
                            mBuffer = mBuffers[mIndex];
                            mBuffer.Position(0);
                            break;
                        }
                        else if (mIndex == (int)MediaCodec.InfoOutputBuffersChanged)
                        {
                            mBuffers = mMediaCodec.GetOutputBuffers();
                        }
                        else if (mIndex == (int)MediaCodec.InfoOutputFormatChanged)
                        {
                            mMediaFormat = mMediaCodec.GetOutputFormat(mIndex);
                            Log.i(TAG, mMediaFormat.ToString());
                        }
                        else if (mIndex == (int)MediaCodec.InfoTryAgainLater)
                        {
                            Log.v(TAG, "No buffer available...");
                            //return 0;
                        }
                        else
                        {
                            Log.e(TAG, "Message: " + mIndex);
                            //return 0;
                        }
                    }
                }

                if (mClosed)
                {
                    throw new IOException("This InputStream was closed");
                }

                min = length < mBufferInfo.Size - mBuffer.Position() ? length : mBufferInfo.Size - mBuffer.Position();
                mBuffer.Get(buffer, offset, min);
                if (mBuffer.Position() >= mBufferInfo.Size)
                {
                    mMediaCodec.ReleaseOutputBuffer(mIndex, false);
                    mBuffer = null;
                }
            }
            catch (RuntimeException e)
            {
                e.PrintStackTrace();
            }

            return(min);
        }
Exemple #4
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"a={MediaAttributeFieldName}:{MediaFormat.ToString()} {MIMEType}/{SampleRate}").AppendLine();
            return(sb.ToString());
        }
Exemple #5
0
        override public void OnOutputBufferAvailable(MediaCodec mc, int outputBufferId, BufferInfo info)
        {
            ByteBuffer  outputBuffer = mDecoder.GetOutputBuffer(outputBufferId);
            MediaFormat bufferFormat = mDecoder.GetOutputFormat(outputBufferId); // option A

            Console.WriteLine("decoded buffer format:" + bufferFormat.ToString());

            // bufferFormat is equivalent to mOutputFormat
            // outputBuffer is ready to be processed or rendered.

            Console.WriteLine("OnOutputBufferAvailable: outputBufferId = " + outputBufferId.ToString());
            byte[] decoded_data = new byte[info.Size];
            outputBuffer.Position(info.Offset);
            outputBuffer.Get(decoded_data, 0, info.Size);
            mDecoder.ReleaseOutputBuffer(outputBufferId, false);
            Console.WriteLine("call OnDecodeFrame from decoder!");

            Console.WriteLine("bufferFormat.getInteger(MediaFormat.KeyWidth)=" + bufferFormat.GetInteger(MediaFormat.KeyWidth).ToString() + " bufferFormat.getInteger(MediaFormat.KeyHeight)=" + bufferFormat.GetInteger(MediaFormat.KeyHeight).ToString());
            mCallbackObj.OnDecodeFrame(decoded_data, bufferFormat.GetInteger(MediaFormat.KeyWidth), bufferFormat.GetInteger(MediaFormat.KeyHeight), bufferFormat.GetInteger(MediaFormat.KeyColorFormat));
        }
        private bool ExtractSomeAudioData(out bool endOfFile)
        {
            endOfFile = extractionOutputDone;
            if (endOfFile)
            {
                return(false);
            }

            var hasExtractedData = false;

            int TimeoutUs = 20000;

            MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

            if (!extractionInputDone)
            {
                int inputBufIndex = audioMediaDecoder.DequeueInputBuffer(TimeoutUs);
                if (inputBufIndex >= 0)
                {
                    Java.Nio.ByteBuffer inputBuffer = audioMediaDecoder.GetInputBuffer(inputBufIndex);

                    //Read the sample data into the ByteBuffer.  This neither respects nor updates inputBuf's position, limit, etc.
                    int chunkSize = audioMediaExtractor.ReadSampleData(inputBuffer, 0);
                    if (chunkSize < 0)
                    {
                        //End of stream: send empty frame with EOS flag set
                        audioMediaDecoder.QueueInputBuffer(inputBufIndex, 0, 0, 0L, MediaCodecBufferFlags.EndOfStream);
                        extractionInputDone = true;
                        //Logger.Verbose("sent input EOS");
                    }
                    else
                    {
                        if (audioMediaExtractor.SampleTrackIndex != trackIndexAudio)
                        {
                            Logger.Warning(string.Format("got audio sample from track {0}, expected {1}", audioMediaExtractor.SampleTrackIndex, trackIndexAudio));
                        }

                        var presentationTimeMicroSeconds = audioMediaExtractor.SampleTime;
                        audioMediaDecoder.QueueInputBuffer(inputBufIndex, 0, chunkSize, presentationTimeMicroSeconds, 0);

                        audioMediaExtractor.Advance();
                    }
                }
                else
                {
                    //do nothing: the input buffer queue is full (we need to output them first)
                    //continue;
                }
            }

            int decoderStatus = audioMediaDecoder.DequeueOutputBuffer(info, TimeoutUs);

            switch (decoderStatus)
            {
            case (int)MediaCodecInfoState.TryAgainLater:
            {
                Logger.Verbose("no output from decoder available");
                break;
            }

            case (int)MediaCodecInfoState.OutputFormatChanged:
            {
                MediaFormat newFormat    = audioMediaDecoder.OutputFormat;
                string      newFormatStr = newFormat.ToString();
                Logger.Verbose("audio decoder output format changed: " + newFormatStr);
                break;
            }

            case (int)MediaCodecInfoState.OutputBuffersChanged:
            {
                //deprecated: we just ignore it
                break;
            }

            default:
            {
                if (decoderStatus < 0)
                {
                    throw new InvalidOperationException(string.Format("unexpected result from audio decoder.DequeueOutputBuffer: {0}", decoderStatus));
                }

                if ((info.Flags & MediaCodecBufferFlags.EndOfStream) != 0)
                {
                    Logger.Verbose("audio: output EOS");
                    extractionOutputDone = true;
                }

                if (info.Size > 0)
                {
                    hasExtractedData = true;
                    var buffer           = audioMediaDecoder.GetOutputBuffer(decoderStatus);
                    var presentationTime = TimeSpanExtensions.FromMicroSeconds(info.PresentationTimeUs);

                    if (StorageBuffer.CountDataBytes + info.Size <= StorageBuffer.Data.Length)
                    {
                        buffer.Get(StorageBuffer.Data, StorageBuffer.CountDataBytes, info.Size); // Read the buffer all at once
                        buffer.Clear();                                                          // MUST DO!!! OTHERWISE THE NEXT TIME YOU GET THIS SAME BUFFER BAD THINGS WILL HAPPEN
                        buffer.Position(0);

                        if (StorageBuffer.CountDataBytes == 0)
                        {
                            StorageBuffer.PresentationTime = presentationTime;
                        }

                        StorageBuffer.CountDataBytes += info.Size;
                    }
                    else
                    {
                        Logger.Error("The storage buffer has reached full capacity. Current data will be dropped");
                    }
                }

                audioMediaDecoder.ReleaseOutputBuffer(decoderStatus, false);
                break;
            }
            }

            endOfFile = extractionOutputDone;
            return(hasExtractedData);
        }
Exemple #7
0
        public static bool IsAudioFormat(this MediaFormat value)
        {
            FieldInfo field = value.GetType().GetField(value.ToString());

            return(Attribute.GetCustomAttribute(field, typeof(AudioFormatAttribute)) != null);
        }