Beispiel #1
0
        private void SetupFormat()
        {
            if (ffmpeg.avformat_find_stream_info(_formatContext, null) != 0)
            {
                throw new ApplicationException(@"Could not find stream info");
            }


            _formatContext->flags |= ffmpeg.AVFMT_FLAG_DISCARD_CORRUPT;
            _formatContext->flags |= ffmpeg.AVFMT_FLAG_NOBUFFER;

            for (int i = 0; i < _formatContext->nb_streams; i++)
            {
                if (_formatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
                {
                    // get the pointer to the codec context for the video stream
                    _codecContext = _formatContext->streams[i]->codec;
                    _codecContext->workaround_bugs = 1;
                    _videoStream = _formatContext->streams[i];
                    break;
                }
            }

            if (_videoStream != null)
            {
                _codecContext->flags2 |= ffmpeg.CODEC_FLAG2_FAST | ffmpeg.CODEC_FLAG2_CHUNKS |
                                         ffmpeg.CODEC_FLAG_LOW_DELAY;

                var codec = ffmpeg.avcodec_find_decoder(_codecContext->codec_id);
                if (codec == null)
                {
                    throw new ApplicationException("Cannot find a codec to decode the video stream.");
                }

                if ((codec->capabilities & ffmpeg.AV_CODEC_CAP_TRUNCATED) == ffmpeg.AV_CODEC_CAP_TRUNCATED)
                {
                    _codecContext->flags |= ffmpeg.AV_CODEC_FLAG_TRUNCATED;
                }

                if (ffmpeg.avcodec_open2(_codecContext, codec, null) < 0)
                {
                    throw new ApplicationException("Cannot open the video codec.");
                }

                _lastVideoFrame = DateTime.UtcNow;

                AVHWAccel *hwaccel = null;

                while (true)
                {
                    hwaccel = ffmpeg.av_hwaccel_next(hwaccel);
                    if (hwaccel == null)
                    {
                        break;
                    }
                    if (hwaccel->id == _codecContext->codec_id && hwaccel->pix_fmt == _codecContext->pix_fmt)
                    {
                        Logger.LogMessage("USing HW decoder");
                        _codecContext->hwaccel = hwaccel;
                        break;
                    }
                }
            }

            _lastPacket = DateTime.UtcNow;

            for (int i = 0; i < _formatContext->nb_streams; i++)
            {
                if (_formatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
                {
                    _audioCodecContext = _formatContext->streams[i]->codec;
                    _audioStream       = _formatContext->streams[i];
                    break;
                }
            }

            if (_audioStream != null)
            {
                AVCodec *audiocodec = ffmpeg.avcodec_find_decoder(_audioCodecContext->codec_id);
                if (audiocodec != null)
                {
                    _audioCodecContext->refcounted_frames = 0;

                    if (ffmpeg.avcodec_open2(_audioCodecContext, audiocodec, null) == 0)
                    {
                        _audioCodecContext->request_sample_fmt = AVSampleFormat.AV_SAMPLE_FMT_S16;

                        int chans = 1;
                        if (_audioCodecContext->channels > 1) //downmix
                        {
                            chans = 2;
                        }

                        _swrContext = ffmpeg.swr_alloc_set_opts(null,
                                                                ffmpeg.av_get_default_channel_layout(chans),
                                                                AVSampleFormat.AV_SAMPLE_FMT_S16,
                                                                _audioCodecContext->sample_rate,
                                                                ffmpeg.av_get_default_channel_layout(_audioCodecContext->channels),
                                                                _audioCodecContext->sample_fmt,
                                                                _audioCodecContext->sample_rate,
                                                                0,
                                                                null);
                        ffmpeg.swr_init(_swrContext);

                        RecordingFormat = new WaveFormat(_audioCodecContext->sample_rate, 16, _audioCodecContext->channels);
                    }
                }
            }

            if (_videoStream == null && _audioStream == null)
            {
                throw new ApplicationException("Cannot find any streams.");
            }

            _lastPacket = DateTime.UtcNow;

            _thread = new Thread(ReadFrames)
            {
                Name = Source, IsBackground = true
            };
            _thread.Start();
        }
Beispiel #2
0
        private void SetupFormat()
        {
            if (ffmpeg.avformat_find_stream_info(_formatContext, null) != 0)
            {
                throw new ApplicationException("Could not find stream info");
            }


            //_formatContext->flags |= ffmpeg.AVFMT_FLAG_DISCARD_CORRUPT;
            //_formatContext->flags |= ffmpeg.AVFMT_FLAG_NOBUFFER;


            for (var i = 0; i < _formatContext->nb_streams; i++)
            {
                if (_formatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
                {
                    // get the pointer to the codec context for the video stream
                    _videoCodecContext = _formatContext->streams[i]->codec;
                    _videoStream       = _formatContext->streams[i];
                    break;
                }
            }

            if (_videoStream != null)
            {
                var codec = ffmpeg.avcodec_find_decoder(_videoCodecContext->codec_id);
                if (codec == null)
                {
                    throw new ApplicationException("Cannot find a codec to decode the video stream.");
                }
                _videoCodecContext->refcounted_frames = 1;



                _lastVideoFrame = DateTime.UtcNow;

                AVHWAccel *hwaccel = null;

                while (true)
                {
                    hwaccel = ffmpeg.av_hwaccel_next(hwaccel);
                    if (hwaccel == null)
                    {
                        break;
                    }
                    if (hwaccel->id == _videoCodecContext->codec_id && hwaccel->pix_fmt == _videoCodecContext->pix_fmt)
                    {
                        Logger.LogMessage("Using HW decoder");
                        _videoCodecContext->hwaccel = hwaccel;
                        break;
                    }
                }

                //_videoCodecContext->idct_algo = ffmpeg.FF_IDCT_AUTO;
                //_videoCodecContext->skip_frame = AVDiscard.AVDISCARD_DEFAULT;
                //_videoCodecContext->skip_idct = AVDiscard.AVDISCARD_DEFAULT;
                //_videoCodecContext->skip_loop_filter = AVDiscard.AVDISCARD_DEFAULT;
                //_videoCodecContext->error_concealment = 3;

                _videoCodecContext->workaround_bugs = 1;
                _videoCodecContext->flags2         |= ffmpeg.CODEC_FLAG2_FAST | ffmpeg.CODEC_FLAG_LOW_DELAY;// | ffmpeg.CODEC_FLAG2_CHUNKS;


                if ((codec->capabilities & ffmpeg.CODEC_CAP_DR1) != 0)
                {
                    _videoCodecContext->flags |= ffmpeg.CODEC_FLAG_EMU_EDGE;
                }
                if ((codec->capabilities & ffmpeg.AV_CODEC_CAP_TRUNCATED) == ffmpeg.AV_CODEC_CAP_TRUNCATED)
                {
                    _videoCodecContext->flags |= ffmpeg.AV_CODEC_FLAG_TRUNCATED;
                }

                Throw("OPEN2", ffmpeg.avcodec_open2(_videoCodecContext, codec, null));

                _videoFrame = ffmpeg.av_frame_alloc();
            }

            _lastPacket = DateTime.UtcNow;

            for (var i = 0; i < _formatContext->nb_streams; i++)
            {
                if (_formatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
                {
                    _audioCodecContext = _formatContext->streams[i]->codec;
                    _audioStream       = _formatContext->streams[i];
                    break;
                }
            }

            if (_audioStream != null)
            {
                var audiocodec = ffmpeg.avcodec_find_decoder(_audioCodecContext->codec_id);
                if (audiocodec != null)
                {
                    _audioCodecContext->refcounted_frames = 1;

                    Throw("OPEN2 audio", ffmpeg.avcodec_open2(_audioCodecContext, audiocodec, null));

                    var outlayout = ffmpeg.av_get_default_channel_layout(OutFormat.Channels);
                    _audioCodecContext->request_sample_fmt     = AVSampleFormat.AV_SAMPLE_FMT_S16;
                    _audioCodecContext->request_channel_layout = (ulong)outlayout;


                    //var chans = 1;
                    //if (_audioCodecContext->channels > 1) //downmix
                    //    chans = 2;

                    _swrContext = ffmpeg.swr_alloc_set_opts(null,
                                                            outlayout,
                                                            AVSampleFormat.AV_SAMPLE_FMT_S16,
                                                            OutFormat.SampleRate,
                                                            ffmpeg.av_get_default_channel_layout(_audioCodecContext->channels),
                                                            _audioCodecContext->sample_fmt,
                                                            _audioCodecContext->sample_rate,
                                                            0,
                                                            null);

                    Throw("SWR_INIT", ffmpeg.swr_init(_swrContext));
                    _audioFrame = ffmpeg.av_frame_alloc();
                }
            }

            if (_videoStream == null && _audioStream == null)
            {
                throw new ApplicationException("Cannot find any streams.");
            }

            _lastPacket = DateTime.UtcNow;
            if (_abort)
            {
                throw new Exception("Connect aborted");
            }

            _thread = new Thread(ReadFrames)
            {
                Name = Source, IsBackground = false
            };
            _thread.Start();
        }