コード例 #1
0
        public override void OnOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info)
        {
            Java.Nio.ByteBuffer decodedSample = codec.GetOutputBuffer(index);

            // Just release outputBuffer, callback will handle rendering
            codec.ReleaseOutputBuffer(index, true);
        }
コード例 #2
0
    public override void OnOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info)
    {
        if (!running)
        {
            return;
        }
        try
        {
            var ob = audioEncoder.GetOutputBuffer(index);

            ob.Position(info.Offset);
            ob.Limit(info.Offset + info.Size);

            byte[] buffer = new byte[info.Size];
            ob.Get(buffer, 0, info.Size);
            audioEncoder.ReleaseOutputBuffer(index, false);

            lock (outputBuffers)
            {
                outputBuffers.Add(buffer);
            }
        }
        catch (Exception e)
        {
            Logging.error("Exception occured while recording audio stream: " + e);
        }
    }
コード例 #3
0
    public override void OnOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info)
    {
        if (!running)
        {
            return;
        }
        try
        {
            var ob = audioDecoder.GetOutputBuffer(index);

            ob.Position(info.Offset);
            ob.Limit(info.Offset + info.Size);

            byte[] decoded_data = new byte[info.Size];
            ob.Get(decoded_data);

            if (audioPlayer.Write(decoded_data, 0, decoded_data.Length) == 0)
            {
                // TODO drop frames
            }

            audioDecoder.ReleaseOutputBuffer(index, false);
        }
        catch (Exception e)
        {
            Logging.error("Exception occured while playing audio stream: " + e);
        }
    }
コード例 #4
0
ファイル: HwDecoder.cs プロジェクト: tomaszlt/Spixi
        public override void OnOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info)
        {
            if (!running)
            {
                return;
            }
            try
            {
                var ob = audioDecoder.GetOutputBuffer(index);

                ob.Position(info.Offset);
                ob.Limit(info.Offset + info.Size);

                byte[] decoded_data = new byte[info.Size];
                ob.Get(decoded_data);

                audioDecoder.ReleaseOutputBuffer(index, false);

                decodedDataCallback.onDecodedData(decoded_data);
            }
            catch (Exception e)
            {
                Logging.error("Exception occured while playing audio stream: " + e);
            }
        }
コード例 #5
0
        public override void OnOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info)
        {
            Java.Nio.ByteBuffer decodedSample = codec.GetOutputBuffer(index);

            _audioTrack.Write(decodedSample, 4096, WriteMode.NonBlocking);

            codec.ReleaseOutputBuffer(index, true);
        }
コード例 #6
0
ファイル: VideoStreamer.cs プロジェクト: RedMindZ/RedOrc
            public override void OnOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info)
            {
                ByteBuffer outputBuffer = codec.GetOutputBuffer(index);

                byte[] outputArray = new byte[outputBuffer.Remaining()];
                outputBuffer.Get(outputArray);

                DatagramPacket packet = new DatagramPacket(outputArray, outputArray.Length, InetAddress.GetByAddress(new byte[] { 192, 168, 0, 31 }), 9482);

                _udpSocket.Send(packet);

                codec.ReleaseOutputBuffer(index, false);
            }
コード例 #7
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));
        }
コード例 #8
0
        private Thread GetEncoderThread()
        {
            var encoderThread = new Thread(() =>
            {
                MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
                try
                {
                    while (!_disposed)
                    {
                        // Try to get an available pcm audio frame
                        int outIndex = _mediaCodec.DequeueOutputBuffer(info, 50000);
                        if (outIndex >= 0)
                        {
                            int lastIndex = outIndex;

                            // Get the last available output buffer
                            while ((outIndex = this._mediaCodec.DequeueOutputBuffer(info, 0)) >= 0)
                            {
                                this._mediaCodec.ReleaseOutputBuffer(lastIndex, false);

                                lastIndex = outIndex;
                            }

                            ByteBuffer outputBuffer = _mediaCodec.GetOutputBuffer(lastIndex);
                            _audioTrack.Write(outputBuffer, outputBuffer.Limit(), WriteMode.NonBlocking);
                            _mediaCodec.ReleaseOutputBuffer(lastIndex, false);
                        }
                    }
                }
                catch (ThreadInterruptedException)
                {
                    // Ignore Thread got interrupted from outside
                }
            });

            encoderThread.Daemon   = true;
            encoderThread.Priority = Thread.MaxPriority;
            return(encoderThread);
        }
コード例 #9
0
        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);
        }