private void Init()
        {
            if (sps.Length == 14)
                decoderWidth = 1280;
            else
                decoderWidth = 960;

            MediaFormat videoFormat = MediaFormat.CreateVideoFormat("video/avc", decoderWidth, decoderHeight);
            videoFormat.SetByteBuffer("csd-0", ByteBuffer.Wrap(sps));
            videoFormat.SetByteBuffer("csd-1", ByteBuffer.Wrap(pps));

            string str = videoFormat.GetString("mime");
            try
            {
                var cdx = MediaCodec.CreateDecoderByType(str);
                cdx.Configure(videoFormat, Holder.Surface, (MediaCrypto)null, 0);
                cdx.SetVideoScalingMode(VideoScalingMode.ScaleToFit);
                cdx.Start();

                codec = cdx;
            }
            catch (Exception ex)
            {
//handle
            }

            bConfigured = true;

            //set surface aspect ratio
            MainActivity.getActivity().RunOnUiThread(() =>
            {
                float videoProportion = (float)decoderWidth / (float)decoderHeight;

                var size = new Android.Graphics.Point();
                MainActivity.getActivity().WindowManager.DefaultDisplay.GetSize(size);
                int screenWidth = size.X;
                int screenHeight = size.Y;
                float screenProportion = (float)screenWidth / (float)screenHeight;

                var lp = this.LayoutParameters;
                if (videoProportion > screenProportion)
                {
                    lp.Width = screenWidth;
                    lp.Height = (int)((float)screenWidth / videoProportion);
                }
                else
                {
                    lp.Width = (int)(videoProportion * (float)screenHeight);
                    lp.Height = screenHeight;
                }

                this.LayoutParameters = lp;
            });

        }
Exemplo n.º 2
0
        bool SetCodecSpecificData(byte[] sps, byte[] pps)
        {
            if (!Initialized)
            {
                return(false);
            }

            _mediaFormat.SetByteBuffer("csd-0", Java.Nio.ByteBuffer.Wrap(sps));
            _mediaFormat.SetByteBuffer("csd-1", Java.Nio.ByteBuffer.Wrap(pps));
            return(true);
        }
Exemplo n.º 3
0
            private static void Init()
            {
                MediaFormat videoFormat = MediaFormat.CreateVideoFormat("video/avc", width, height);

                videoFormat.SetByteBuffer("csd-0", ByteBuffer.Wrap(sps));
                videoFormat.SetByteBuffer("csd-1", ByteBuffer.Wrap(pps));

                string str = videoFormat.GetString("mime");

                try
                {
                    var cdx = MediaCodec.CreateDecoderByType(str);
                    cdx.Configure(videoFormat, picSurface, (MediaCrypto)null, 0);
                    cdx.SetVideoScalingMode(VideoScalingMode.ScaleToFit);
                    cdx.Start();

                    picCodec = cdx;
                    //codec = picCodec;
                }
                catch (Exception ex)
                {
                }


                videoFormat = MediaFormat.CreateVideoFormat("video/avc", 1280, 720);
                videoFormat.SetByteBuffer("csd-0", ByteBuffer.Wrap(vidSps));
                videoFormat.SetByteBuffer("csd-1", ByteBuffer.Wrap(pps));

                try
                {
                    var cdx = MediaCodec.CreateDecoderByType(videoFormat.GetString("mime"));
                    cdx.Configure(videoFormat, vidSurface, (MediaCrypto)null, 0);
                    cdx.SetVideoScalingMode(VideoScalingMode.ScaleToFit);
                    cdx.Start();

                    vidCodec = cdx;
                }
                catch (Exception ex)
                {
                }

                bConfigured = true;
            }
Exemplo n.º 4
0
            static public void config(Surface surface, int width, int height, byte[] sps, byte[] pps)
            {
                if (sps == null || pps == null)//not ready.
                {
                    return;
                }

                if (bConfigured)
                {
                    return;
                }

                if (codec != null)
                {
                    stop();
                }

                Decoder.width  = width;
                Decoder.height = height;
                Decoder.sps    = sps;
                Decoder.pps    = pps;
                MediaFormat videoFormat = MediaFormat.CreateVideoFormat("video/avc", width, height);

                videoFormat.SetByteBuffer("csd-0", ByteBuffer.Wrap(sps));
                videoFormat.SetByteBuffer("csd-1", ByteBuffer.Wrap(pps));
                videoFormat.SetInteger("color-format", 19);

                string str = videoFormat.GetString("mime");

                try
                {
                    codec = MediaCodec.CreateDecoderByType(str);
                    codec.Configure(videoFormat, surface, (MediaCrypto)null, 0);
                    codec.SetVideoScalingMode(VideoScalingMode.ScaleToFit);
                    codec.Start();
                    bConfigured = true;
                }
                catch (Exception ex)
                {
                    var errstr = ex.Message.ToString();
                }
            }
        /**
         * Instantiates and starts the decoder.
         * @throws IOException The decoder cannot be configured
         */
        private void configureDecoder()
        {
            byte[] prefix = new byte[] { 0x00, 0x00, 0x00, 0x01 };

            ByteBuffer csd0 = ByteBuffer.Allocate(4 + mSPS.Length + 4 + mPPS.Length);

            csd0.Put(new byte[] { 0x00, 0x00, 0x00, 0x01 });
            csd0.Put(mSPS);
            csd0.Put(new byte[] { 0x00, 0x00, 0x00, 0x01 });
            csd0.Put(mPPS);

            mDecoder = MediaCodec.CreateByCodecName(mDecoderName);
            MediaFormat mediaFormat = MediaFormat.CreateVideoFormat(MIME_TYPE, mWidth, mHeight);

            mediaFormat.SetByteBuffer("csd-0", csd0);
            mediaFormat.SetInteger(MediaFormat.KeyColorFormat, mDecoderColorFormat);
            mDecoder.Configure(mediaFormat, null, null, 0);
            mDecoder.Start();

            ByteBuffer[] decInputBuffers = mDecoder.GetInputBuffers();

            int decInputIndex = mDecoder.DequeueInputBuffer(1000000 / FRAMERATE);

            if (decInputIndex >= 0)
            {
                decInputBuffers[decInputIndex].Clear();
                decInputBuffers[decInputIndex].Put(prefix);
                decInputBuffers[decInputIndex].Put(mSPS);
                mDecoder.QueueInputBuffer(decInputIndex, 0, decInputBuffers[decInputIndex].Position(), timestamp(), 0);
            }
            else
            {
                if (VERBOSE)
                {
                    Log.e(TAG, "No buffer available !");
                }
            }

            decInputIndex = mDecoder.DequeueInputBuffer(1000000 / FRAMERATE);
            if (decInputIndex >= 0)
            {
                decInputBuffers[decInputIndex].Clear();
                decInputBuffers[decInputIndex].Put(prefix);
                decInputBuffers[decInputIndex].Put(mPPS);
                mDecoder.QueueInputBuffer(decInputIndex, 0, decInputBuffers[decInputIndex].Position(), timestamp(), 0);
            }
            else
            {
                if (VERBOSE)
                {
                    Log.Error(TAG, "No buffer available !");
                }
            }
        }
Exemplo n.º 6
0
        public void SetupAudio(int sampleRate, int channels, byte[] esdsData)
        {
            _audioTrack = new AudioTrack(
                new AudioAttributes.Builder()
                .SetUsage(AudioUsageKind.Media)
                .SetContentType(AudioContentType.Music)
                .SetFlags(AudioFlags.LowLatency)
                .Build(),
                new Android.Media.AudioFormat.Builder()
                .SetEncoding(Encoding.Pcm16bit)
                .SetSampleRate(44100)
                .SetChannelMask(ChannelOut.Stereo)
                .Build(),
                4096,
                AudioTrackMode.Stream,
                AudioManager.AudioSessionIdGenerate);

            MediaFormat audioFormat = MediaFormat.CreateAudioFormat(
                mime: MediaFormat.MimetypeAudioAac,
                sampleRate: sampleRate,
                channelCount: channels);

            audioFormat.SetInteger(MediaFormat.KeyIsAdts, 0);
            audioFormat.SetInteger(MediaFormat.KeyAacProfile, (int)MediaCodecProfileType.Aacobjectlc);

            _audioCodec = MediaCodec.CreateDecoderByType(
                MediaFormat.MimetypeAudioAac);

            // TODO: Remove hardcoding
            byte profile     = (byte)MediaCodecProfileType.Aacobjectlc;
            byte sampleIndex = AacAdtsAssembler.GetSamplingFrequencyIndex(sampleRate);

            byte[] csd0 = new byte[2];
            csd0[0]  = (byte)(((byte)profile << 3) | (sampleIndex >> 1));
            csd0[1]  = (byte)((byte)((sampleIndex << 7) & 0x80) | (channels << 3));
            esdsData = csd0;

            audioFormat.SetByteBuffer("csd-0", Java.Nio.ByteBuffer.Wrap(esdsData));


            _audioCodec.SetCallback(this);
            _audioCodec.Configure(
                format: audioFormat,
                surface: null,
                crypto: null,
                flags: MediaCodecConfigFlags.None);

            _audioCodec.Start();
            _audioTrack.Play();
        }