Exemplo n.º 1
0
        public static int GetMediaFormatPropertyInteger(Uri uri, string key, int defaultValue)
        {
            int value = defaultValue;

            MediaExtractor extractor = new MediaExtractor();

            try
            {
                extractor.SetDataSource(uri.ToString());
            }
            catch (System.Exception e)
            {
                return(value);
            }

            MediaFormat format = GetTrackFormat(extractor, MIME_TYPE_AVC);

            extractor.Release();

            if (format.ContainsKey(key))
            {
                value = format.GetInteger(key);
            }

            return(value);
        }
Exemplo n.º 2
0
        private void FeedClipToEncoder(SamplerClip clip)
        {
            mLastSampleTime = 0;

            MediaCodec decoder = null;

            MediaExtractor extractor = SetupExtractorForClip(clip);

            if (extractor == null)
            {
                return;
            }

            int trackIndex = GetVideoTrackIndex(extractor);

            extractor.SelectTrack(trackIndex);

            MediaFormat clipFormat = extractor.GetTrackFormat(trackIndex);

            if (clip.getStartTime() != -1)
            {
                extractor.SeekTo(clip.getStartTime() * 1000, MediaExtractorSeekTo.PreviousSync);
                clip.setStartTime(extractor.SampleTime / 1000);
            }

            try
            {
                decoder        = MediaCodec.CreateDecoderByType(MediaHelper.MIME_TYPE_AVC);
                mOutputSurface = new OutputSurface();

                decoder.Configure(clipFormat, mOutputSurface.Surface, null, 0);
                decoder.Start();

                ResampleVideo(extractor, decoder, clip);
            }
            catch (System.Exception e)
            {
            }
            finally
            {
                if (mOutputSurface != null)
                {
                    mOutputSurface.Release();
                }
                if (decoder != null)
                {
                    decoder.Stop();
                    decoder.Release();
                }

                if (extractor != null)
                {
                    extractor.Release();
                    extractor = null;
                }
            }
        }
        partial void ReleaseMediaInternal()
        {
            audioMediaDecoder?.Stop();
            audioMediaDecoder?.Release();
            audioMediaDecoder = null;

            audioMediaExtractor?.Release();
            audioMediaExtractor = null;

            InputFileStream?.Dispose();
            InputFileStream = null;

            InputFile?.Dispose();
            InputFile = null;
        }
Exemplo n.º 4
0
        private void Release()
        {
            Scheduler.UnregisterExtractor(this); //to avoid receiving any more event from the scheduler

            MediaDecoder?.Stop();
            MediaDecoder?.Release();
            MediaDecoder = null;

            mediaExtractor?.Release();
            mediaExtractor = null;

            inputFile     = null;
            MediaMetadata = null;
            MediaDuration = TimeSpan.Zero;

            inputFileDescriptor?.Close();
            inputFileDescriptor = null;

            isInitialized = false;
        }
Exemplo n.º 5
0
        /**
         * Tests extraction from an MP4 to a series of PNG files.
         * <p>
         * We scale the video to 640x480 for the PNG just to demonstrate that we can scale the
         * video with the GPU.  If the input video has a different aspect ratio, we could preserve
         * it by adjusting the GL viewport to get letterboxing or pillarboxing, but generally if
         * you're extracting frames you don't want black bars.
         */
        public void extractMpegFrames(int saveWidth, int saveHeight)
        {
            MediaCodec         decoder       = null;
            CodecOutputSurface outputSurface = null;
            MediaExtractor     extractor     = null;

            try
            {
                // must be an absolute path The MediaExtractor error messages aren't very useful.  Check to see if the input file exists so we can throw a better one if it's not there.
                File inputFile = new File(_filesdir, INPUT_FILE);
                if (!inputFile.CanRead())
                {
                    throw new FileNotFoundException("Unable to read " + inputFile);
                }

                extractor = new MediaExtractor();
                extractor.SetDataSource(inputFile.ToString());
                int trackIndex = selectTrack(extractor);
                if (trackIndex < 0)
                {
                    throw new RuntimeException("No video track found in " + inputFile);
                }
                extractor.SelectTrack(trackIndex);

                MediaFormat format = extractor.GetTrackFormat(trackIndex);

                if (VERBOSE)
                {
                    Log.Info(TAG, "Video size is " + format.GetInteger(MediaFormat.KeyWidth) + "x" + format.GetInteger(MediaFormat.KeyHeight));
                }


                // Could use width/height from the MediaFormat to get full-size frames.

                outputSurface = new CodecOutputSurface(saveWidth, saveHeight);

                // Create a MediaCodec decoder, and configure it with the MediaFormat from the
                // extractor.  It's very important to use the format from the extractor because
                // it contains a copy of the CSD-0/CSD-1 codec-specific data chunks.
                String mime = format.GetString(MediaFormat.KeyMime);
                decoder = MediaCodec.CreateDecoderByType(mime);
                decoder.Configure(format, outputSurface.getSurface(), null, 0);
                decoder.Start();

                doExtract(extractor, trackIndex, decoder, outputSurface);
            }
            finally
            {
                // release everything we grabbed
                if (outputSurface != null)
                {
                    outputSurface.release();
                    outputSurface = null;
                }
                if (decoder != null)
                {
                    decoder.Stop();
                    decoder.Release();
                    decoder = null;
                }
                if (extractor != null)
                {
                    extractor.Release();
                    extractor = null;
                }
            }
        }
Exemplo n.º 6
0
        public void PrepareEncoder(string path, File _downloaddir)
        {
            MediaCodec     _Decoder  = null;
            MediaExtractor extractor = null;

            _downloadsfilesdir = _downloaddir;


            try {
                //for (int i = 0; i < extractor.TrackCount; i++)
                //{
                //    MediaFormat Format = extractor.GetTrackFormat(i);
                //    //MediaFormat format = MediaFormat.CreateVideoFormat(MIME_TYPE, 640, 360);
                //    String mime = Format.GetString(MediaFormat.KeyMime);
                //    if (mime.StartsWith("video/"))
                //    {

                //            extractor.SelectTrack(i);
                //            _Decoder = MediaCodec.CreateEncoderByType(mime);

                //            _Decoder.Configure(Format, null, null, 0);
                //            break;

                //    }
                //}

                extractor = new MediaExtractor();
                extractor.SetDataSource(path);
                int trackIndex = selectTrack(extractor);
                //if (trackIndex < 0)
                //{
                //    throw new RuntimeException("No video track found in " + inputFile);
                //}
                extractor.SelectTrack(trackIndex);

                MediaFormat format = extractor.GetTrackFormat(trackIndex);

                _Width  = format.GetInteger(MediaFormat.KeyWidth);
                _Height = format.GetInteger(MediaFormat.KeyHeight);


                // Could use width/height from the MediaFormat to get full-size frames.

                //outputSurface = new CodecOutputSurface(saveWidth, saveHeight);

                // Create a MediaCodec decoder, and configure it with the MediaFormat from the
                // extractor.  It's very important to use the format from the extractor because
                // it contains a copy of the CSD-0/CSD-1 codec-specific data chunks.
                String mime = format.GetString(MediaFormat.KeyMime);
                _Decoder = MediaCodec.CreateDecoderByType(mime);
                _Decoder.Configure(format, null, null, 0);
                _Decoder.Start();



                Decode(_Decoder, extractor);
            }
            catch (Exception e)
            {
                Log.Error(TAG, e.Message, e);
                throw;
            }
            finally
            {
                // release everything we grabbed
                //if (outputSurface != null)
                //{
                //    outputSurface.release();
                //    outputSurface = null;
                //}
                if (_Decoder != null)
                {
                    _Decoder.Stop();
                    _Decoder.Release();
                    _Decoder = null;
                }
                if (extractor != null)
                {
                    extractor.Release();
                    extractor = null;
                }
            }

            _TrackIndex = -1;
            //_MuxerStarted = false;
        }