Recherche avancée

Médias (1)

Mot : - Tags -/ogg

Autres articles (21)

  • Multilang : améliorer l’interface pour les blocs multilingues

    18 février 2011, par

    Multilang est un plugin supplémentaire qui n’est pas activé par défaut lors de l’initialisation de MediaSPIP.
    Après son activation, une préconfiguration est mise en place automatiquement par MediaSPIP init permettant à la nouvelle fonctionnalité d’être automatiquement opérationnelle. Il n’est donc pas obligatoire de passer par une étape de configuration pour cela.

  • List of compatible distributions

    26 avril 2011, par

    The table below is the list of Linux distributions compatible with the automated installation script of MediaSPIP. Distribution nameVersion nameVersion number Debian Squeeze 6.x.x Debian Weezy 7.x.x Debian Jessie 8.x.x Ubuntu The Precise Pangolin 12.04 LTS Ubuntu The Trusty Tahr 14.04
    If you want to help us improve this list, you can provide us access to a machine whose distribution is not mentioned above or send the necessary fixes to add (...)

  • Selection of projects using MediaSPIP

    2 mai 2011, par

    The examples below are representative elements of MediaSPIP specific uses for specific projects.
    MediaSPIP farm @ Infini
    The non profit organizationInfini develops hospitality activities, internet access point, training, realizing innovative projects in the field of information and communication technologies and Communication, and hosting of websites. It plays a unique and prominent role in the Brest (France) area, at the national level, among the half-dozen such association. Its members (...)

Sur d’autres sites (6796)

  • record mediasoup RTP stream using FFmpeg for Firefox

    30 juillet 2024, par Hadi Aghandeh

    I am trying to record WebRTC stream using mediasoup. I could record successfully on chrome and safari 13/14/15. However on Firefox the does not work.

    


    Client side code is a vue js component which gets rtp-compabilities using socket.io and create producers after the server creates the transports. This works good on chrome and safari.

    


    const { connect , createLocalTracks } = require('twilio-video');
const SocketClient = require("socket.io-client");
const SocketPromise = require("socket.io-promise").default;
const MediasoupClient = require("mediasoup-client");

export default {
    data() {
        return {
            errors: [],
            isReady: false,
            isRecording: false,
            loading: false,
            sapio: {
                token: null,
                connectionId: 0
            },
            server: {
                host: 'https://rtc.test',
                ws: '/server',
                socket: null,
            },
            peer: {},
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        async init() {
            await this.startCamera();

            if (this.takeId) {
                await this.recordBySapioServer();
            }
        },
        startCamera() {
            return new Promise( (resolve, reject) => {
                if (window.videoMediaStreamObject) {
                    this.setVideoElementStream(window.videoMediaStreamObject);
                    resolve();
                } else {
                    // Get user media as required
                    try {
                        this.localeStream = navigator.mediaDevices.getUserMedia({
                            audio: true,
                            video: true,
                        }).then((stream) => {
                            this.setVideoElementStream(stream);
                            resolve();
                        })
                    } catch (err) {
                        console.error(err);
                        reject();
                    }
                }
            })
        },
        setVideoElementStream(stream) {
            this.localStream = stream;
            this.$refs.video.srcObject = stream;
            this.$refs.video.muted = true;
            this.$refs.video.play().then((video) => {
                this.isStreaming = true;
                this.height = this.$refs.video.videoHeight;
                this.width = this.$refs.video.videoWidth;
            });
        },
        // first thing we need is connecting to websocket
        connectToSocket() {
            const serverUrl = this.server.host;
            console.log("Connect with sapio rtc server:", serverUrl);

            const socket = SocketClient(serverUrl, {
                path:  this.server.ws,
                transports: ["websocket"],
            });
            this.socket = socket;

            socket.on("connect", () => {
                console.log("WebSocket connected");
                // we ask for rtp-capabilities from server to send to us
                socket.emit('send-rtp-capabilities');
            });

            socket.on("error", (err) => {
                this.loading = true;
                console.error("WebSocket error:", err);
            });

            socket.on("router-rtp-capabilities", async (msg) => {
                const { routerRtpCapabilities, sessionId, externalId } = msg;
                console.log('[rtpCapabilities:%o]', routerRtpCapabilities);
                this.routerRtpCapabilities = routerRtpCapabilities;

                try {
                    const device = new MediasoupClient.Device();
                    // Load the mediasoup device with the router rtp capabilities gotten from the server
                    await device.load({ routerRtpCapabilities });

                    this.peer.sessionId = sessionId;
                    this.peer.externalId = externalId;
                    this.peer.device = device;

                    this.createTransport();
                } catch (error) {
                    console.error('failed to init device [error:%o]', error);
                    socket.disconnect();
                }
            });

            socket.on("create-transport", async (msg) => {
                console.log('handleCreateTransportRequest() [data:%o]', msg);

                try {
                    // Create the local mediasoup send transport
                    this.peer.sendTransport = await this.peer.device.createSendTransport(msg);
                    console.log('send transport created [id:%s]', this.peer.sendTransport.id);

                    // Set the transport listeners and get the users media stream
                    this.handleSendTransportListeners();
                    this.setTracks();
                    this.loading = false;
                } catch (error) {
                    console.error('failed to create transport [error:%o]', error);
                    socket.disconnect();
                }
            });

            socket.on("connect-transport", async (msg) => {
                console.log('handleTransportConnectRequest()');
                try {
                    const action = this.connectTransport;

                    if (!action) {
                        throw new Error('transport-connect action was not found');
                    }

                    await action(msg);
                } catch (error) {
                    console.error('ailed [error:%o]', error);
                }
            });

            socket.on("produce", async (msg) => {
                console.log('handleProduceRequest()');
                try {
                    if (!this.produce) {
                        throw new Error('produce action was not found');
                    }
                    await this.produce(msg);
                } catch (error) {
                    console.error('failed [error:%o]', error);
                }
            });

            socket.on("recording", async (msg) => {
                this.isRecording = true;
            });

            socket.on("recording-error", async (msg) => {
                this.isRecording = false;
                console.error(msg);
            });

            socket.on("recording-closed", async (msg) => {
                this.isRecording = false;
                console.warn(msg)
            });

        },
        createTransport() {
            console.log('createTransport()');

            if (!this.peer || !this.peer.device.loaded) {
                throw new Error('Peer or device is not initialized');
            }

            // First we must create the mediasoup transport on the server side
            this.socket.emit('create-transport',{
                sessionId: this.peer.sessionId
            });
        },
        handleSendTransportListeners() {
            this.peer.sendTransport.on('connect', this.handleTransportConnectEvent);
            this.peer.sendTransport.on('produce', this.handleTransportProduceEvent);
            this.peer.sendTransport.on('connectionstatechange', connectionState => {
                console.log('send transport connection state change [state:%s]', connectionState);
            });
        },
        handleTransportConnectEvent({ dtlsParameters }, callback, errback) {
            console.log('handleTransportConnectEvent()');
            try {
                this.connectTransport = (msg) => {
                    console.log('connect-transport action');
                    callback();
                    this.connectTransport = null;
                };

                this.socket.emit('connect-transport',{
                    sessionId: this.peer.sessionId,
                    transportId: this.peer.sendTransport.id,
                    dtlsParameters
                });

            } catch (error) {
                console.error('handleTransportConnectEvent() failed [error:%o]', error);
                errback(error);
            }
        },
        handleTransportProduceEvent({ kind, rtpParameters }, callback, errback)  {
            console.log('handleTransportProduceEvent()');
            try {
                this.produce = jsonMessage => {
                    console.log('handleTransportProduceEvent callback [data:%o]', jsonMessage);
                    callback({ id: jsonMessage.id });
                    this.produce = null;
                };

                this.socket.emit('produce', {
                    sessionId: this.peer.sessionId,
                    transportId: this.peer.sendTransport.id,
                    kind,
                    rtpParameters
                });
            } catch (error) {
                console.error('handleTransportProduceEvent() failed [error:%o]', error);
                errback(error);
            }
        },
        async recordBySapioServer() {
            this.loading = true;
            this.connectToSocket();
        },
        async setTracks() {
            // Start mediasoup-client's WebRTC producers
            const audioTrack = this.localStream.getAudioTracks()[0];
            this.peer.audioProducer = await this.peer.sendTransport.produce({
                track: audioTrack,
                codecOptions :
                    {
                        opusStereo : 1,
                        opusDtx    : 1
                    }
            });


            let encodings;
            let codec;
            const codecOptions = {videoGoogleStartBitrate : 1000};

            codec = this.peer.device.rtpCapabilities.codecs.find((c) => c.kind.toLowerCase() === 'video');
            if (codec.mimeType.toLowerCase() === 'video/vp9') {
                encodings = { scalabilityMode: 'S3T3_KEY' };
            } else {
                encodings = [
                    { scaleResolutionDownBy: 4, maxBitrate: 500000 },
                    { scaleResolutionDownBy: 2, maxBitrate: 1000000 },
                    { scaleResolutionDownBy: 1, maxBitrate: 5000000 }
                ];
            }
            const videoTrack = this.localStream.getVideoTracks()[0];
            this.peer.videoProducer =await this.peer.sendTransport.produce({
                track: videoTrack,
                encodings,
                codecOptions,
                codec
            });

        },
        startRecording() {
            this.Q.answer.recordingId = this.peer.externalId;
            this.socket.emit("start-record", {
                sessionId: this.peer.sessionId
            });
        },
        stopRecording() {
            this.socket.emit("stop-record" , {
                sessionId: this.peer.sessionId
            });
        },
    },

}





    


    console.log of my ffmpeg process :

    


    // sdp string
[sdpString:v=0
  o=- 0 0 IN IP4 127.0.0.1
  s=FFmpeg
  c=IN IP4 127.0.0.1
  t=0 0
  m=video 25549 RTP/AVP 101 
  a=rtpmap:101 VP8/90000
  a=sendonly
  m=audio 26934 RTP/AVP 100 
  a=rtpmap:100 opus/48000/2
  a=sendonly
  ]

// ffmpeg args
commandArgs:[
  '-loglevel',
  'debug',
  '-protocol_whitelist',
  'pipe,udp,rtp',
  '-fflags',
  '+genpts',
  '-f',
  'sdp',
  '-i',
  'pipe:0',
  '-map',
  '0:v:0',
  '-c:v',
  'copy',
  '-map',
  '0:a:0',
  '-strict',
  '-2',
  '-c:a',
  'copy',
  '-f',
  'webm',
  '-flags',
  '+global_header',
  '-y',
  'storage/recordings/26e63cb3-4f81-499e-941a-c0bb7f7f52ce.webm',
  [length]: 26
]
// ffmpeg log
ffmpeg::process::data [data:'ffmpeg version n4.4']
ffmpeg::process::data [data:' Copyright (c) 2000-2021 the FFmpeg developers']
ffmpeg::process::data [data:'\n']
ffmpeg::process::data [data:'  built with gcc 11.1.0 (GCC)\n']
ffmpeg::process::data [data:'  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-amf --enable-avisynth --enable-cuda-llvm --enable-lto --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmfx --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librav1e --enable-librsvg --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-nvdec --enable-nvenc --enable-shared --enable-version3\n']
ffmpeg::process::data [data:'  libavutil      56. 70.100 / 56. 70.100\n' +
  '  libavcodec     58.134.100 / 58.134.100\n' +
  '  libavformat    58. 76.100 / 58. 76.100\n' +
  '  libavdevice    58. 13.100 / 58. 13.100\n' +
  '  libavfilter     7.110.100 /  7.110.100\n' +
  '  libswscale      5.  9.100 /  5.  9.100\n' +
  '  libswresample   3.  9.100 /  3.  9.100\n' +
  '  libpostproc    55.  9.100 / 55.  9.100\n' +
  'Splitting the commandline.\n' +
  "Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.\n" +
  "Reading option '-protocol_whitelist' ..."]
ffmpeg::process::data [data:" matched as AVOption 'protocol_whitelist' with argument 'pipe,udp,rtp'.\n" +
  "Reading option '-fflags' ..."]
ffmpeg::process::data [data:" matched as AVOption 'fflags' with argument '+genpts'.\n" +
  "Reading option '-f' ... matched as option 'f' (force format) with argument 'sdp'.\n" +
  "Reading option '-i' ... matched as input url with argument 'pipe:0'.\n" +
  "Reading option '-map' ... matched as option 'map' (set input stream mapping) with argument '0:v:0'.\n" +
  "Reading option '-c:v' ... matched as option 'c' (codec name) with argument 'copy'.\n" +
  "Reading option '-map' ... matched as option 'map' (set input stream mapping) with argument '0:a:0'.\n" +
  "Reading option '-strict' ...Routing option strict to both codec and muxer layer\n" +
  " matched as AVOption 'strict' with argument '-2'.\n" +
  "Reading option '-c:a' ... matched as option 'c' (codec name) with argument 'copy'.\n" +
  "Reading option '-f' ... matched as option 'f' (force format) with argument 'webm'.\n" +
  "Reading option '-flags' ... matched as AVOption 'flags' with argument '+global_header'.\n" +
  "Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.\n" +
  "Reading option 'storage/recordings/26e63cb3-4f81-499e-941a-c0bb7f7f52ce.webm' ... matched as output url.\n" +
  'Finished splitting the commandline.\n' +
  'Parsing a group of options: global .\n' +
  'Applying option loglevel (set logging level) with argument debug.\n' +
  'Applying option y (overwrite output files) with argument 1.\n' +
  'Successfully parsed a group of options.\n' +
  'Parsing a group of options: input url pipe:0.\n' +
  'Applying option f (force format) with argument sdp.\n' +
  'Successfully parsed a group of options.\n' +
  'Opening an input file: pipe:0.\n' +
  "[sdp @ 0x55604dc58400] Opening 'pipe:0' for reading\n" +
  '[sdp @ 0x55604dc58400] video codec set to: vp8\n' +
  '[sdp @ 0x55604dc58400] audio codec set to: opus\n' +
  '[sdp @ 0x55604dc58400] audio samplerate set to: 48000\n' +
  '[sdp @ 0x55604dc58400] audio channels set to: 2\n' +
  '[udp @ 0x55604dc6c500] end receive buffer size reported is 425984\n' +
  '[udp @ 0x55604dc6c7c0] end receive buffer size reported is 425984\n' +
  '[sdp @ 0x55604dc58400] setting jitter buffer size to 500\n' +
  '[udp @ 0x55604dc6d900] end receive buffer size reported is 425984\n' +
  '[udp @ 0x55604dc6d2c0] end receive buffer size reported is 425984\n' +
  '[sdp @ 0x55604dc58400] setting jitter buffer size to 500\n']
ffmpeg::process::data [data:'[sdp @ 0x55604dc58400] Before avformat_find_stream_info() pos: 210 bytes read:210 seeks:0 nb_streams:2\n']
  **mediasoup:Consumer resume() +1s**
  **mediasoup:Channel request() [method:consumer.resume, id:12] +1s**
  **mediasoup:Channel request succeeded [method:consumer.resume, id:12] +0ms**
  **mediasoup:Consumer resume() +1ms**
  **mediasoup:Channel request() [method:consumer.resume, id:13] +0ms**
  **mediasoup:Channel request succeeded [method:consumer.resume, id:13] +0ms**
ffmpeg::process::data [data:'[sdp @ 0x55604dc58400] Could not find codec parameters for stream 0 (Video: vp8, 1 reference frame, yuv420p): unspecified size\n' +
  "Consider increasing the value for the 'analyzeduration' (0) and 'probesize' (5000000) options\n"]
ffmpeg::process::data [data:'[sdp @ 0x55604dc58400] After avformat_find_stream_info() pos: 210 bytes read:210 seeks:0 frames:0\n' +
  "Input #0, sdp, from 'pipe:0':\n" +
  '  Metadata:\n' +
  '    title           : FFmpeg\n' +
  '  Duration: N/A, bitrate: N/A\n' +
  '  Stream #0:0, 0, 1/90000: Video: vp8, 1 reference frame, yuv420p, 90k tbr, 90k tbn, 90k tbc\n' +
  '  Stream #0:1, 0, 1/48000: Audio: opus, 48000 Hz, stereo, fltp\n' +
  'Successfully opened the file.\n' +
  'Parsing a group of options: output url storage/recordings/26e63cb3-4f81-499e-941a-c0bb7f7f52ce.webm.\n' +
  'Applying option map (set input stream mapping) with argument 0:v:0.\n' +
  'Applying option c:v (codec name) with argument copy.\n' +
  'Applying option map (set input stream mapping) with argument 0:a:0.\n' +
  'Applying option c:a (codec name) with argument copy.\n' +
  'Applying option f (force format) with argument webm.\n' +
  'Successfully parsed a group of options.\n' +
  'Opening an output file: storage/recordings/26e63cb3-4f81-499e-941a-c0bb7f7f52ce.webm.\n' +
  "[file @ 0x55604dce5bc0] Setting default whitelist 'file,crypto,data'\n"]
ffmpeg::process::data [data:'Successfully opened the file.\n' +
  '[webm @ 0x55604dce0fc0] dimensions not set\n' +
  'Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument\n' +
  'Error initializing output stream 0:1 -- \n' +
  'Stream mapping:\n' +
  '  Stream #0:0 -> #0:0 (copy)\n' +
  '  Stream #0:1 -> #0:1 (copy)\n' +
  '    Last message repeated 1 times\n' +
  '[AVIOContext @ 0x55604dc6dcc0] Statistics: 0 seeks, 0 writeouts\n' +
  '[AVIOContext @ 0x55604dc69380] Statistics: 210 bytes read, 0 seeks\n']
ffmpeg::process::close



    


    FFmpeg says dimensions not  set and Could not write header for output file when I use Firefox. This might be enough for understanding the problem, but if you need more information you can read how server side is performing.
Server-Side in summary can be something like this :
lets say we initialized worker and router at run time using following functions.

    


        // Start the mediasoup workers
module.exports.initializeWorkers = async () => {
  const { logLevel, logTags, rtcMinPort, rtcMaxPort } = config.worker;

  console.log('initializeWorkers() creating %d mediasoup workers', config.numWorkers);

  for (let i = 0; i < config.numWorkers; ++i) {
    const worker = await mediasoup.createWorker({
      logLevel, logTags, rtcMinPort, rtcMaxPort
    });

    worker.once('died', () => {
      console.error('worker::died worker has died exiting in 2 seconds... [pid:%d]', worker.pid);
      setTimeout(() => process.exit(1), 2000);
    });

    workers.push(worker);
  }
};


    


    module.exports.createRouter = async () => {
  const worker = getNextWorker();

  console.log('createRouter() creating new router [worker.pid:%d]', worker.pid);

  console.log(`config.router.mediaCodecs:${JSON.stringify(config.router.mediaCodecs)}`)

  return await worker.createRouter({ mediaCodecs: config.router.mediaCodecs });
};


    


    We pass router.rtpCompatibilities to the client. clients get the rtpCompatibilities and create a device and loads it. after that a transport must be created at server side.

    


        const handleCreateTransportRequest = async (jsonMessage) => {

  const transport = await createTransport('webRtc', router);

  var peer;
  try {peer = peers.get(jsonMessage.sessionId);}
  catch{console.log('peer not found')}
  
  peer.addTransport(transport);

  peer.socket.emit('create-transport',{
    id: transport.id,
    iceParameters: transport.iceParameters,
    iceCandidates: transport.iceCandidates,
    dtlsParameters: transport.dtlsParameters
  });
};


    


    Then after the client side also created the transport we listen to connect event an at the time of event, we request the server to create connection.

    


    const handleTransportConnectRequest = async (jsonMessage) => {
  var peer;
  try {peer = peers.get(jsonMessage.sessionId);}
  catch{console.log('peer not found')}

  if (!peer) {
    throw new Error(`Peer with id ${jsonMessage.sessionId} was not found`);
  }

  const transport = peer.getTransport(jsonMessage.transportId);

  if (!transport) {
    throw new Error(`Transport with id ${jsonMessage.transportId} was not found`);
  }

  await transport.connect({ dtlsParameters: jsonMessage.dtlsParameters });
  console.log('handleTransportConnectRequest() transport connected');
  peer.socket.emit('connect-transport');
};


    


    Similar thing happen on produce event.

    


    const handleProduceRequest = async (jsonMessage) => {
  console.log('handleProduceRequest [data:%o]', jsonMessage);

  var peer;
  try {peer = peers.get(jsonMessage.sessionId);}
  catch{console.log('peer not found')}

  if (!peer) {
    throw new Error(`Peer with id ${jsonMessage.sessionId} was not found`);
  }

  const transport = peer.getTransport(jsonMessage.transportId);

  if (!transport) {
    throw new Error(`Transport with id ${jsonMessage.transportId} was not found`);
  }

  const producer = await transport.produce({
    kind: jsonMessage.kind,
    rtpParameters: jsonMessage.rtpParameters
  });

  peer.addProducer(producer);

  console.log('handleProducerRequest() new producer added [id:%s, kind:%s]', producer.id, producer.kind);

  peer.socket.emit('produce',{
    id: producer.id,
    kind: producer.kind
  });
};


    


    For Recording, first I create plain transports for audio and video producers.

    


    const rtpTransport = router.createPlainTransport(config.plainRtpTransport);


    


    then rtp transport must be connected to ports :

    


      await rtpTransport.connect({
    ip: '127.0.0.1',
    port: remoteRtpPort,
    rtcpPort: remoteRtcpPort
  });


    


    Then the consumer must also be created.

    


      const rtpConsumer = await rtpTransport.consume({
    producerId: producer.id,
    rtpCapabilities,
    paused: true
  });


    


    After that we can start recording using following code :

    


     this._rtpParameters = args;
    this._process = undefined;
    this._observer = new EventEmitter();
    this._peer = args.peer;

    this._sdpString = createSdpText(this._rtpParameters);
    this._sdpStream = convertStringToStream(this._sdpString);
    // create dir
    const dir = process.env.REOCRDING_PATH ?? 'storage/recordings';
    if (!fs.existsSync(dir)) shelljs.mkdir('-p', dir);
  
    this._extension = 'webm';
    // create file path
    this._path = `${dir}/${args.peer.sessionId}.${this._extension}`
    let loop = 0;
    while(fs.existsSync(this._path)) {
      this._path = `${dir}/${args.peer.sessionId}-${++loop}.${this._extension}`
    }

this._recordingnModel = await Recording.findOne({sessionIds: { $in: [this._peer.sessionId] }})
    this._recordingnModel.files.push(this._path);
    this._recordingnModel.save();

let proc  = ffmpeg(this._sdpStream)
    .inputOptions([
      '-protocol_whitelist','pipe,udp,rtp',
      '-f','sdp',
    ])
    .format(this._extension)
    .output(this._path)
    .size('720x?')
    .on('start', ()=>{
      this._peer.socket.emit('recording');
    })
    .on('end', ()=>{
      let path = this._path.replace('storage/recordings/', '');
      this._peer.socket.emit('recording-closed', {
        url: `${process.env.APP_URL}/recording/file/${path}`
      });
    });

    proc.run();
    this._process =  proc;
  }



    


  • audio do not stop recording after pause ffmpeg c++

    15 septembre 2021, par C1ngh10

    I am developing an application that record the screen and the audio from microphone. I implemented the pause function stopping video and audio thread on a condition variable, resuming them with a notify on the same condition variable. This is done in captureAudio(), in the main while. In this way works on macOS and linux, where I use avfoudation and alsa respectively, but on windows, with dshow, keep recording audio during the pause, when the thread is waiting on the condition variable. Does anybody know how can I fix this behaviour ?

    


    #include "ScreenRecorder.h"

using namespace std;

ScreenRecorder::ScreenRecorder() : pauseCapture(false), stopCapture(false), started(false), activeMenu(true) {
    avcodec_register_all();
    avdevice_register_all();

    width = 1920;
    height = 1200;
}

ScreenRecorder::~ScreenRecorder() {

    if (started) {
        value = av_write_trailer(outAVFormatContext);
        if (value < 0) {
            cerr << "Error in writing av trailer" << endl;
            exit(-1);
        }

        avformat_close_input(&inAudioFormatContext);
        if(inAudioFormatContext == nullptr){
            cout << "inAudioFormatContext close successfully" << endl;
        }
        else{
            cerr << "Error: unable to close the inAudioFormatContext" << endl;
            exit(-1);
            //throw "Error: unable to close the file";
        }
        avformat_free_context(inAudioFormatContext);
        if(inAudioFormatContext == nullptr){
            cout << "AudioFormat freed successfully" << endl;
        }
        else{
            cerr << "Error: unable to free AudioFormatContext" << endl;
            exit(-1);
        }
        
        avformat_close_input(&pAVFormatContext);
        if (pAVFormatContext == nullptr) {
            cout << "File close successfully" << endl;
        }
        else {
            cerr << "Error: unable to close the file" << endl;
            exit(-1);
            //throw "Error: unable to close the file";
        }

        avformat_free_context(pAVFormatContext);
        if (pAVFormatContext == nullptr) {
            cout << "VideoFormat freed successfully" << endl;
        }
        else {
            cerr << "Error: unable to free VideoFormatContext" << endl;
            exit(-1);
        }
    }
}

/*==================================== VIDEO ==============================*/

int ScreenRecorder::openVideoDevice() throw() {
    value = 0;
    options = nullptr;
    pAVFormatContext = nullptr;

    pAVFormatContext = avformat_alloc_context();

    string dimension = to_string(width) + "x" + to_string(height);
    av_dict_set(&options, "video_size", dimension.c_str(), 0);   //option to set the dimension of the screen section to record

#ifdef _WIN32
    pAVInputFormat = av_find_input_format("gdigrab");
    if (avformat_open_input(&pAVFormatContext, "desktop", pAVInputFormat, &options) != 0) {
        cerr << "Couldn't open input stream" << endl;
        exit(-1);
    }

#elif defined linux
   
    int offset_x = 0, offset_y = 0;
    string url = ":0.0+" + to_string(offset_x) + "," + to_string(offset_y);  //custom string to set the start point of the screen section
    pAVInputFormat = av_find_input_format("x11grab");
    value = avformat_open_input(&pAVFormatContext, url.c_str(), pAVInputFormat, &options);

    if (value != 0) {
        cerr << "Error in opening input device (video)" << endl;
        exit(-1);
    }
#else

    value = av_dict_set(&options, "pixel_format", "0rgb", 0);
    if (value < 0) {
        cerr << "Error in setting pixel format" << endl;
        exit(-1);
    }

    value = av_dict_set(&options, "video_device_index", "1", 0);

    if (value < 0) {
        cerr << "Error in setting video device index" << endl;
        exit(-1);
    }

    pAVInputFormat = av_find_input_format("avfoundation");

    if (avformat_open_input(&pAVFormatContext, "Capture screen 0:none", pAVInputFormat, &options) != 0) {  //TODO trovare un modo per selezionare sempre lo schermo (forse "Capture screen 0")
        cerr << "Error in opening input device" << endl;
        exit(-1);
    }



#endif
    //set frame per second

    value = av_dict_set(&options, "framerate", "30", 0);
    if (value < 0) {
        cerr << "Error in setting dictionary value (setting framerate)" << endl;
        exit(-1);
    }

    value = av_dict_set(&options, "preset", "medium", 0);
    if (value < 0) {
        cerr << "Error in setting dictionary value (setting preset value)" << endl;
        exit(-1);
    }
    /*
    value = av_dict_set(&options, "vsync", "1", 0);
    if(value < 0){
        cerr << "Error in setting dictionary value (setting vsync value)" << endl;
        exit(-1);
    }
    */

    value = av_dict_set(&options, "probesize", "60M", 0);
    if (value < 0) {
        cerr << "Error in setting probesize value" << endl;
        exit(-1);
    }

    //get video stream infos from context
    value = avformat_find_stream_info(pAVFormatContext, nullptr);
    if (value < 0) {
        cerr << "Error in retrieving the stream info" << endl;
        exit(-1);
    }

    VideoStreamIndx = -1;
    for (int i = 0; i < pAVFormatContext->nb_streams; i++) {
        if (pAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            VideoStreamIndx = i;
            break;
        }
    }
    if (VideoStreamIndx == -1) {
        cerr << "Error: unable to find video stream index" << endl;
        exit(-2);
    }

    pAVCodecContext = pAVFormatContext->streams[VideoStreamIndx]->codec;
    pAVCodec = avcodec_find_decoder(pAVCodecContext->codec_id/*params->codec_id*/);
    if (pAVCodec == nullptr) {
        cerr << "Error: unable to find decoder video" << endl;
        exit(-1);
    }

    cout << "Insert height and width [h w]: ";   //custom screen dimension to record
    cin >> h >> w;*/


    return 0;
}

/*==========================================  AUDIO  ============================*/

int ScreenRecorder::openAudioDevice() {
    audioOptions = nullptr;
    inAudioFormatContext = nullptr;

    inAudioFormatContext = avformat_alloc_context();
    value = av_dict_set(&audioOptions, "sample_rate", "44100", 0);
    if (value < 0) {
        cerr << "Error: cannot set audio sample rate" << endl;
        exit(-1);
    }
    value = av_dict_set(&audioOptions, "async", "1", 0);
    if (value < 0) {
        cerr << "Error: cannot set audio sample rate" << endl;
        exit(-1);
    }

#if defined linux
    audioInputFormat = av_find_input_format("alsa");
    value = avformat_open_input(&inAudioFormatContext, "hw:0", audioInputFormat, &audioOptions);
    if (value != 0) {
        cerr << "Error in opening input device (audio)" << endl;
        exit(-1);
    }
#endif

#if defined _WIN32
    audioInputFormat = av_find_input_format("dshow");
    value = avformat_open_input(&inAudioFormatContext, "audio=Microfono (Realtek(R) Audio)", audioInputFormat, &audioOptions);
    if (value != 0) {
        cerr << "Error in opening input device (audio)" << endl;
        exit(-1);
    }
#endif

    value = avformat_find_stream_info(inAudioFormatContext, nullptr);
    if (value != 0) {
        cerr << "Error: cannot find the audio stream information" << endl;
        exit(-1);
    }

    audioStreamIndx = -1;
    for (int i = 0; i < inAudioFormatContext->nb_streams; i++) {
        if (inAudioFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
            audioStreamIndx = i;
            break;
        }
    }
    if (audioStreamIndx == -1) {
        cerr << "Error: unable to find audio stream index" << endl;
        exit(-2);
    }
}

int ScreenRecorder::initOutputFile() {
    value = 0;

    outAVFormatContext = nullptr;
    outputAVFormat = av_guess_format(nullptr, "output.mp4", nullptr);
    if (outputAVFormat == nullptr) {
        cerr << "Error in guessing the video format, try with correct format" << endl;
        exit(-5);
    }
    avformat_alloc_output_context2(&outAVFormatContext, outputAVFormat, outputAVFormat->name, "..\\media\\output.mp4");
    if (outAVFormatContext == nullptr) {
        cerr << "Error in allocating outAVFormatContext" << endl;
        exit(-4);
    }

    /*===========================================================================*/
    this->generateVideoStream();
    this->generateAudioStream();

    //create an empty video file
    if (!(outAVFormatContext->flags & AVFMT_NOFILE)) {
        if (avio_open2(&outAVFormatContext->pb, "..\\media\\output.mp4", AVIO_FLAG_WRITE, nullptr, nullptr) < 0) {
            cerr << "Error in creating the video file" << endl;
            exit(-10);
        }
    }

    if (outAVFormatContext->nb_streams == 0) {
        cerr << "Output file does not contain any stream" << endl;
        exit(-11);
    }
    value = avformat_write_header(outAVFormatContext, &options);
    if (value < 0) {
        cerr << "Error in writing the header context" << endl;
        exit(-12);
    }
    return 0;
}

/*===================================  VIDEO  ==================================*/

void ScreenRecorder::generateVideoStream() {
    //Generate video stream
    videoSt = avformat_new_stream(outAVFormatContext, nullptr);
    if (videoSt == nullptr) {
        cerr << "Error in creating AVFormatStream" << endl;
        exit(-6);
    }

    outVideoCodec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);  //AV_CODEC_ID_MPEG4
    if (outVideoCodec == nullptr) {
        cerr << "Error in finding the AVCodec, try again with the correct codec" << endl;
        exit(-8);
    }
avcodec_alloc_context3(outAVCodec)
    outVideoCodecContext = avcodec_alloc_context3(outVideoCodec);
    if (outVideoCodecContext == nullptr) {
        cerr << "Error in allocating the codec context" << endl;
        exit(-7);
    }

    //set properties of the video file (stream)
    outVideoCodecContext = videoSt->codec;
    outVideoCodecContext->codec_id = AV_CODEC_ID_MPEG4;
    outVideoCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
    outVideoCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
    outVideoCodecContext->bit_rate = 10000000;
    outVideoCodecContext->width = width;
    outVideoCodecContext->height = height;
    outVideoCodecContext->gop_size = 10;
    outVideoCodecContext->global_quality = 500;
    outVideoCodecContext->max_b_frames = 2;
    outVideoCodecContext->time_base.num = 1;
    outVideoCodecContext->time_base.den = 30;
    outVideoCodecContext->bit_rate_tolerance = 400000;

    if (outVideoCodecContext->codec_id == AV_CODEC_ID_H264) {
        av_opt_set(outVideoCodecContext->priv_data, "preset", "slow", 0);
    }

    if (outAVFormatContext->oformat->flags & AVFMT_GLOBALHEADER) {
        outVideoCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }

    value = avcodec_open2(outVideoCodecContext, outVideoCodec, nullptr);
    if (value < 0) {
        cerr << "Error in opening the AVCodec" << endl;
        exit(-9);
    }

    outVideoStreamIndex = -1;
    for (int i = 0; i < outAVFormatContext->nb_streams; i++) {
        if (outAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN) {
            outVideoStreamIndex = i;
        }
    }
    if (outVideoStreamIndex < 0) {
        cerr << "Error: cannot find a free stream index for video output" << endl;
        exit(-1);
    }
    avcodec_parameters_from_context(outAVFormatContext->streams[outVideoStreamIndex]->codecpar, outVideoCodecContext);
}

/*===============================  AUDIO  ==================================*/

void ScreenRecorder::generateAudioStream() {
    AVCodecParameters* params = inAudioFormatContext->streams[audioStreamIndx]->codecpar;
    inAudioCodec = avcodec_find_decoder(params->codec_id);
    if (inAudioCodec == nullptr) {
        cerr << "Error: cannot find the audio decoder" << endl;
        exit(-1);
    }

    inAudioCodecContext = avcodec_alloc_context3(inAudioCodec);
    if (avcodec_parameters_to_context(inAudioCodecContext, params) < 0) {
        cout << "Cannot create codec context for audio input" << endl;
    }

    value = avcodec_open2(inAudioCodecContext, inAudioCodec, nullptr);
    if (value < 0) {
        cerr << "Error: cannot open the input audio codec" << endl;
        exit(-1);
    }

    //Generate audio stream
    outAudioCodecContext = nullptr;
    outAudioCodec = nullptr;
    int i;

    AVStream* audio_st = avformat_new_stream(outAVFormatContext, nullptr);
    if (audio_st == nullptr) {
        cerr << "Error: cannot create audio stream" << endl;
        exit(1);
    }

    outAudioCodec = avcodec_find_encoder(AV_CODEC_ID_AAC);
    if (outAudioCodec == nullptr) {
        cerr << "Error: cannot find requested encoder" << endl;
        exit(1);
    }

    outAudioCodecContext = avcodec_alloc_context3(outAudioCodec);
    if (outAudioCodecContext == nullptr) {
        cerr << "Error: cannot create related VideoCodecContext" << endl;
        exit(1);
    }

    if ((outAudioCodec)->supported_samplerates) {
        outAudioCodecContext->sample_rate = (outAudioCodec)->supported_samplerates[0];
        for (i = 0; (outAudioCodec)->supported_samplerates[i]; i++) {
            if ((outAudioCodec)->supported_samplerates[i] == inAudioCodecContext->sample_rate)
                outAudioCodecContext->sample_rate = inAudioCodecContext->sample_rate;
        }
    }
    outAudioCodecContext->codec_id = AV_CODEC_ID_AAC;
    outAudioCodecContext->sample_fmt = (outAudioCodec)->sample_fmts ? (outAudioCodec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
    outAudioCodecContext->channels = inAudioCodecContext->channels;
    outAudioCodecContext->channel_layout = av_get_default_channel_layout(outAudioCodecContext->channels);
    outAudioCodecContext->bit_rate = 96000;
    outAudioCodecContext->time_base = { 1, inAudioCodecContext->sample_rate };

    outAudioCodecContext->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;

    if ((outAVFormatContext)->oformat->flags & AVFMT_GLOBALHEADER) {
        outAudioCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }

    if (avcodec_open2(outAudioCodecContext, outAudioCodec, nullptr) < 0) {
        cerr << "error in opening the avcodec" << endl;
        exit(1);
    }

    //find a free stream index
    outAudioStreamIndex = -1;
    for (i = 0; i < outAVFormatContext->nb_streams; i++) {
        if (outAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN) {
            outAudioStreamIndex = i;
        }
    }
    if (outAudioStreamIndex < 0) {
        cerr << "Error: cannot find a free stream for audio on the output" << endl;
        exit(1);
    }

    avcodec_parameters_from_context(outAVFormatContext->streams[outAudioStreamIndex]->codecpar, outAudioCodecContext);
}

int ScreenRecorder::init_fifo()
{
    /* Create the FIFO buffer based on the specified output sample format. */
    if (!(fifo = av_audio_fifo_alloc(outAudioCodecContext->sample_fmt,
        outAudioCodecContext->channels, 1))) {
        fprintf(stderr, "Could not allocate FIFO\n");
        return AVERROR(ENOMEM);
    }
    return 0;
}

int ScreenRecorder::add_samples_to_fifo(uint8_t** converted_input_samples, const int frame_size) {
    int error;
    /* Make the FIFO as large as it needs to be to hold both,
     * the old and the new samples. */
    if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
        fprintf(stderr, "Could not reallocate FIFO\n");
        return error;
    }
    /* Store the new samples in the FIFO buffer. */
    if (av_audio_fifo_write(fifo, (void**)converted_input_samples, frame_size) < frame_size) {
        fprintf(stderr, "Could not write data to FIFO\n");
        return AVERROR_EXIT;
    }
    return 0;
}

int ScreenRecorder::initConvertedSamples(uint8_t*** converted_input_samples,
    AVCodecContext* output_codec_context,
    int frame_size) {
    int error;
    /* Allocate as many pointers as there are audio channels.
     * Each pointer will later point to the audio samples of the corresponding
     * channels (although it may be NULL for interleaved formats).
     */
    if (!(*converted_input_samples = (uint8_t**)calloc(output_codec_context->channels,
        sizeof(**converted_input_samples)))) {
        fprintf(stderr, "Could not allocate converted input sample pointers\n");
        return AVERROR(ENOMEM);
    }
    /* Allocate memory for the samples of all channels in one consecutive
     * block for convenience. */
    if (av_samples_alloc(*converted_input_samples, nullptr,
        output_codec_context->channels,
        frame_size,
        output_codec_context->sample_fmt, 0) < 0) {

        exit(1);
    }
    return 0;
}

static int64_t pts = 0;
void ScreenRecorder::captureAudio() {
    int ret;
    AVPacket* inPacket, * outPacket;
    AVFrame* rawFrame, * scaledFrame;
    uint8_t** resampledData;

    init_fifo();

    //allocate space for a packet
    inPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
    if (!inPacket) {
        cerr << "Cannot allocate an AVPacket for encoded video" << endl;
        exit(1);
    }
    av_init_packet(inPacket);

    //allocate space for a packet
    rawFrame = av_frame_alloc();
    if (!rawFrame) {
        cerr << "Cannot allocate an AVPacket for encoded video" << endl;
        exit(1);
    }

    scaledFrame = av_frame_alloc();
    if (!scaledFrame) {
        cerr << "Cannot allocate an AVPacket for encoded video" << endl;
        exit(1);
    }

    outPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
    if (!outPacket) {
        cerr << "Cannot allocate an AVPacket for encoded video" << endl;
        exit(1);
    }

    //init the resampler
    SwrContext* resampleContext = nullptr;
    resampleContext = swr_alloc_set_opts(resampleContext,
        av_get_default_channel_layout(outAudioCodecContext->channels),
        outAudioCodecContext->sample_fmt,
        outAudioCodecContext->sample_rate,
        av_get_default_channel_layout(inAudioCodecContext->channels),
        inAudioCodecContext->sample_fmt,
        inAudioCodecContext->sample_rate,
        0,
        nullptr);
    if (!resampleContext) {
        cerr << "Cannot allocate the resample context" << endl;
        exit(1);
    }
    if ((swr_init(resampleContext)) < 0) {
        fprintf(stderr, "Could not open resample context\n");
        swr_free(&resampleContext);
        exit(1);
    }

    while (true) {
        if (pauseCapture) {
            cout << "Pause audio" << endl;
        }
        cv.wait(ul, [this]() { return !pauseCapture; });

        if (stopCapture) {
            break;
        }

        ul.unlock();

        if (av_read_frame(inAudioFormatContext, inPacket) >= 0 && inPacket->stream_index == audioStreamIndx) {
            //decode audio routing
            av_packet_rescale_ts(outPacket, inAudioFormatContext->streams[audioStreamIndx]->time_base, inAudioCodecContext->time_base);
            if ((ret = avcodec_send_packet(inAudioCodecContext, inPacket)) < 0) {
                cout << "Cannot decode current audio packet " << ret << endl;
                continue;
            }
            
            while (ret >= 0) {
                ret = avcodec_receive_frame(inAudioCodecContext, rawFrame);
                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                    break;
                else if (ret < 0) {
                    cerr << "Error during decoding" << endl;
                    exit(1);
                }
                if (outAVFormatContext->streams[outAudioStreamIndex]->start_time <= 0) {
                    outAVFormatContext->streams[outAudioStreamIndex]->start_time = rawFrame->pts;
                }
                initConvertedSamples(&resampledData, outAudioCodecContext, rawFrame->nb_samples);

                swr_convert(resampleContext,
                    resampledData, rawFrame->nb_samples,
                    (const uint8_t**)rawFrame->extended_data, rawFrame->nb_samp

                add_samples_to_fifo(resampledData, rawFrame->nb_samples);

                //raw frame ready
                av_init_packet(outPacket);
                outPacket->data = nullptr;
                outPacket->size = 0;

                const int frame_size = FFMAX(av_audio_fifo_size(fifo), outAudioCodecContext->frame_size);

                scaledFrame = av_frame_alloc();
                if (!scaledFrame) {
                    cerr << "Cannot allocate an AVPacket for encoded video" << endl;
                    exit(1);
                }

                scaledFrame->nb_samples = outAudioCodecContext->frame_size;
                scaledFrame->channel_layout = outAudioCodecContext->channel_layout;
                scaledFrame->format = outAudioCodecContext->sample_fmt;
                scaledFrame->sample_rate = outAudioCodecContext->sample_rate;
                av_frame_get_buffer(scaledFrame, 0);

                while (av_audio_fifo_size(fifo) >= outAudioCodecContext->frame_size) {

                    ret = av_audio_fifo_read(fifo, (void**)(scaledFrame->data), outAudioCodecContext->frame_size);
                    scaledFrame->pts = pts;
                    pts += scaledFrame->nb_samples;
                    if (avcodec_send_frame(outAudioCodecContext, scaledFrame) < 0) {
                        cout << "Cannot encode current audio packet " << endl;
                        exit(1);
                    }
                    while (ret >= 0) {
                        ret = avcodec_receive_packet(outAudioCodecContext, outPacket);
                        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                            break;
                        else if (ret < 0) {
                            cerr << "Error during encoding" << endl;
                            exit(1);
                        }
                        av_packet_rescale_ts(outPacket, outAudioCodecContext->time_base, outAVFormatContext->streams[outAudioStreamIndex]->time_base);

                        outPacket->stream_index = outAudioStreamIndex;

                        write_lock.lock();
                        
                        if (av_write_frame(outAVFormatContext, outPacket) != 0)
                        {
                            cerr << "Error in writing audio frame" << endl;
                        }
                        write_lock.unlock();
                        av_packet_unref(outPacket);
                    }
                    ret = 0;
                }
                av_frame_free(&scaledFrame);
                av_packet_unref(outPacket);
            }
        }
    }
}

int ScreenRecorder::captureVideoFrames() {
    int64_t pts = 0;
    int flag;
    int frameFinished = 0;
    bool endPause = false;
    int numPause = 0;

    ofstream outFile{ "..\\media\\log.txt", ios::out };

    int frameIndex = 0;
    value = 0;

    pAVPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
    if (pAVPacket == nullptr) {
        cerr << "Error in allocating AVPacket" << endl;
        exit(-1);
    }

    pAVFrame = av_frame_alloc();
    if (pAVFrame == nullptr) {
        cerr << "Error: unable to alloc the AVFrame resources" << endl;
        exit(-1);
    }

    outFrame = av_frame_alloc();
    if (outFrame == nullptr) {
        cerr << "Error: unable to alloc the AVFrame resources for out frame" << endl;
        exit(-1);
    }

    int videoOutBuffSize;
    int nBytes = av_image_get_buffer_size(outVideoCodecContext->pix_fmt, outVideoCodecContext->width, outVideoCodecContext->height, 32);
    uint8_t* videoOutBuff = (uint8_t*)av_malloc(nBytes);

    if (videoOutBuff == nullptr) {
        cerr << "Error: unable to allocate memory" << endl;
        exit(-1);
    }

    value = av_image_fill_arrays(outFrame->data, outFrame->linesize, videoOutBuff, AV_PIX_FMT_YUV420P, outVideoCodecContext->width, outVideoCodecContext->height, 1);
    if (value < 0) {
        cerr << "Error in filling image array" << endl;
    }

    SwsContext* swsCtx_;
    if (avcodec_open2(pAVCodecContext, pAVCodec, nullptr) < 0) {
        cerr << "Could not open codec" << endl;
        exit(-1);
    }
    swsCtx_ = sws_getContext(pAVCodecContext->width, pAVCodecContext->height, pAVCodecContext->pix_fmt, outVideoCodecContext->width, outVideoCodecContext->height, outVideoCodecContext->pix_fmt, SWS_BICUBIC,
        nullptr, nullptr, nullptr);

    AVPacket outPacket;
    int gotPicture;

    time_t startTime;
    time(&startTime);

    while (true) {

        if (pauseCapture) {
            cout << "Pause" << endl;
            outFile << "///////////////////   Pause  ///////////////////" << endl;
            cout << "outVideoCodecContext->time_base: " << outVideoCodecContext->time_base.num << ", " << outVideoCodecContext->time_base.den << endl;
        }
        cv.wait(ul, [this]() { return !pauseCapture; });   //pause capture (not busy waiting)
        if (endPause) {
            endPause = false;
        }

        if (stopCapture)  //check if the capture has to stop
            break;
        ul.unlock();

        if (av_read_frame(pAVFormatContext, pAVPacket) >= 0 && pAVPacket->stream_index == VideoStreamIndx) {
            av_packet_rescale_ts(pAVPacket, pAVFormatContext->streams[VideoStreamIndx]->time_base, pAVCodecContext->time_base);
            value = avcodec_decode_video2(pAVCodecContext, pAVFrame, &frameFinished, pAVPacket);
            if (value < 0) {
                cout << "Unable to decode video" << endl;
            }

            if (frameFinished) { //frame successfully decoded
                //sws_scale(swsCtx_, pAVFrame->data, pAVFrame->linesize, 0, pAVCodecContext->height, outFrame->data, outFrame->linesize);
                av_init_packet(&outPacket);
                outPacket.data = nullptr;
                outPacket.size = 0;

                if (outAVFormatContext->streams[outVideoStreamIndex]->start_time <= 0) {
                    outAVFormatContext->streams[outVideoStreamIndex]->start_time = pAVFrame->pts;
                }

                //disable warning on the console
                outFrame->width = outVideoCodecContext->width;
                outFrame->height = outVideoCodecContext->height;
                outFrame->format = outVideoCodecContext->pix_fmt;

                sws_scale(swsCtx_, pAVFrame->data, pAVFrame->linesize, 0, pAVCodecContext->height, outFrame->data, outFrame->linesize);

                avcodec_encode_video2(outVideoCodecContext, &outPacket, outFrame, &gotPicture);

                if (gotPicture) {
                    if (outPacket.pts != AV_NOPTS_VALUE) {
                        outPacket.pts = av_rescale_q(outPacket.pts, videoSt->codec->time_base, videoSt->time_base);
                    }
                    if (outPacket.dts != AV_NOPTS_VALUE) {
                        outPacket.dts = av_rescale_q(outPacket.dts, videoSt->codec->time_base, videoSt->time_base);
                    }

                    //cout << "Write frame " << j++ << " (size = " << outPacket.size / 1000 << ")" << endl;
                    //cout << "(size = " << outPacket.size << ")" << endl;

                    //av_packet_rescale_ts(&outPacket, outVideoCodecContext->time_base, outAVFormatContext->streams[outVideoStreamIndex]->time_base);
                    //outPacket.stream_index = outVideoStreamIndex;

                    outFile << "outPacket->duration: " << outPacket.duration << ", " << "pAVPacket->duration: " << pAVPacket->duration << endl;
                    outFile << "outPacket->pts: " << outPacket.pts << ", " << "pAVPacket->pts: " << pAVPacket->pts << endl;
                    outFile << "outPacket.dts: " << outPacket.dts << ", " << "pAVPacket->dts: " << pAVPacket->dts << endl;

                    time_t timer;
                    double seconds;

                    mu.lock();
                    if (!activeMenu) {
                        time(&timer);
                        seconds = difftime(timer, startTime);
                        int h = (int)(seconds / 3600);
                        int m = (int)(seconds / 60) % 60;
                        int s = (int)(seconds) % 60;

                        std::cout << std::flush << "\r" << std::setw(2) << std::setfill('0') << h << ':'
                            << std::setw(2) << std::setfill('0') << m << ':'
                            << std::setw(2) << std::setfill('0') << s << std::flush;
                    }
                    mu.unlock();

                    write_lock.lock();
                    if (av_write_frame(outAVFormatContext, &outPacket) != 0) {
                        cerr << "Error in writing video frame" << endl;
                    }
                    write_lock.unlock();
                    av_packet_unref(&outPacket);
                }

                av_packet_unref(&outPacket);
                av_free_packet(pAVPacket);  //avoid memory saturation
            }
        }
    }

    outFile.close();

    av_free(videoOutBuff);

    return 0;
}


    


  • Segmentation fault on debian 9 when decoding audio with ffmpeg and libopus

    27 août 2021, par Ramil Dautov

    I wrote the program that takes as an input some .opus file, decodes it using libavcodec and libopus and then plays it using SDL2. Program works on Windows 10 and Ubuntu 18.04, however it crashes with the segmentation fault on Debian 9.

    


    I've tried to update libavcodec and libopus libraries, tried to compile using clang and gcc - nothing helped.

    


    Address sanitizer shows that stack overflow happens :

    


    ASAN:DEADLYSIGNAL
=================================================================
==12167==ERROR: AddressSanitizer: stack-overflow on address 0x2b3e74c81ff8 (pc 0x2b3e7a098803 bp 0x2b3e74c82690 sp 0x2b3e74c81eb0 T2)
    #0 0x2b3e7a098802 in quant_all_bands celt/bands.c:1403
    #1 0x2b3e7a0a2a37 in celt_decode_with_ec celt/celt_decoder.c:1083
    #2 0x2b3e7a0c8afb in opus_decode_frame src/opus_decoder.c:518
    #3 0x2b3e7a0c9e40 in opus_decode_native src/opus_decoder.c:721
    #4 0x2b3e7a0d33f3 in opus_multistream_decode_native src/opus_multistream_decoder.c:253
    #5 0x2b3e7a0d37a8 in opus_multistream_decode src/opus_multistream_decoder.c:398
    #6 0x2b3e760ad83c  (/usr/lib/x86_64-linux-gnu/libavcodec.so.57+0x43583c)
    #7 0x2b3e75e4ca27  (/usr/lib/x86_64-linux-gnu/libavcodec.so.57+0x1d4a27)
    #8 0x2b3e75e4f62a in avcodec_send_packet (/usr/lib/x86_64-linux-gnu/libavcodec.so.57+0x1d762a)
    #9 0x2b3e75e4f9e6  (/usr/lib/x86_64-linux-gnu/libavcodec.so.57+0x1d79e6)
    #10 0x55ef09511882 in decode(AVCodecContext*, AVPacket*, unsigned char*, int) /home/ram/my/player3/speaker.cpp:296
    #11 0x55ef09511626 in fillBuffer(AVCodecContext*, unsigned char*, int) /home/ram/my/player3/speaker.cpp:251
    #12 0x55ef09511294 in process(AVCodecContext*, unsigned char*, int) /home/ram/my/player3/speaker.cpp:194
    #13 0x55ef095105b9 in audio_callback(void*, unsigned char*, int) /home/ram/my/player3/speaker.cpp:69
    #14 0x2b3e7815cc31  (/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0+0x1fc31)
    #15 0x2b3e781bcf8b  (/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0+0x7ff8b)
    #16 0x2b3e7820c6c8  (/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0+0xcf6c8)
    #17 0x2b3e77be74a3 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x74a3)
    #18 0x2b3e7940ed0e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0xe8d0e)

SUMMARY: AddressSanitizer: stack-overflow celt/bands.c:1403 in quant_all_bands
Thread T2 (SDLAudioDev2) created by T0 here:
    #0 0x2b3e74d0df59 in __interceptor_pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.3+0x30f59)
    #1 0x2b3e7820c732  (/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0+0xcf732)



    


    I also tried to increase stack size using ulimit -s unlimited and tried to increase stack size for the thread that starts decoding, didn't work.

    


    In main.cpp file I have this :

    


    #include <iostream>&#xA;#include <memory>&#xA;#include <mutex>&#xA;#include "speaker.h"&#xA;#include "SDL2/SDL.h"&#xA;&#xA;extern "C"{&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libswresample></libswresample>swresample.h>&#xA;}&#xA;&#xA;static int decode_audio_file(const char* path) {&#xA;    &#xA;    av_register_all();&#xA;&#xA;    // get format from audio file&#xA;    AVFormatContext* format = avformat_alloc_context();&#xA;    if (avformat_open_input(&amp;format, path, NULL, NULL) != 0) {&#xA;        std::cout &lt;&lt; "Could not open file" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;    if (avformat_find_stream_info(format, NULL) &lt; 0) {&#xA;        std::cout &lt;&lt; "Could not retrieve stream info from file" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Find the index of the first audio stream&#xA;    int stream_index =- 1;&#xA;    for (int i=0; i&lt; format->nb_streams; i&#x2B;&#x2B;) {&#xA;        if (format->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;            stream_index = i;&#xA;            break;&#xA;        }&#xA;    }&#xA;    if (stream_index == -1) {&#xA;        std::cout &lt;&lt; "Could not retrieve audio stream from file" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;    AVStream* stream = format->streams[stream_index];&#xA;&#xA;    // Initialize speaker&#xA;    init_Speaker("OPUS",&#xA;                  48000,&#xA;                  2,&#xA;                  15,&#xA;                  3,&#xA;                  av_get_channel_layout("stereo"),&#xA;                  av_get_sample_fmt("s16"));&#xA;&#xA;    // prepare to read data&#xA;    AVPacket* packet;&#xA;    packet = av_packet_alloc();&#xA;    av_init_packet(packet);&#xA;&#xA;    // iterate through frames&#xA;    while (av_read_frame(format, packet) >= 0) {&#xA;        play(packet->data, packet->size, std::chrono::microseconds{packet->pts},&#xA;                std::chrono::microseconds{packet->dts});&#xA;        av_packet_unref(packet);&#xA;    }&#xA;&#xA;    // clean up&#xA;    avformat_free_context(format);&#xA;    close_Speaker();&#xA;&#xA;    // success&#xA;    return 0;&#xA;}&#xA;&#xA;int main(int argc, char const *argv[]) {&#xA;    // check parameters&#xA;    if (argc &lt; 2) {&#xA;        std::cout &lt;&lt; "Please provide the path to an audio file as first command-line argument.\n";&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Init Audio&#xA;    SDL_Init(SDL_INIT_AUDIO);&#xA;&#xA;    // decode data&#xA;    if (decode_audio_file(argv[1]) != 0) {&#xA;        return -1;&#xA;    }&#xA;&#xA;    std::cout &lt;&lt; "Finish" &lt;&lt; std::endl;&#xA;    return 0;&#xA;}&#xA;&#xA;</mutex></memory></iostream>

    &#xA;

    In speaker.cpp :

    &#xA;

    #include "speaker.h"&#xA;#include "pthread.h"&#xA;#include "avcodec.h"&#xA;#include "common.h"&#xA;#include <iostream>&#xA;&#xA;extern "C"&#xA;{&#xA;#include <libswresample></libswresample>swresample.h>&#xA;#include <libavutil></libavutil>hwcontext.h>&#xA;}&#xA;&#xA;using std::chrono::microseconds;&#xA;&#xA;SDL_AudioDeviceID m_id;&#xA;&#xA;AVCodecParserContext *parser = nullptr;&#xA;&#xA;//&#xA;constexpr static auto buffer_size{1024}; // 2048&#xA;constexpr static auto buffer_max_size{AVCODEC_MAX_AUDIO_FRAME_SIZE * 4};&#xA;uint32_t m_samplerate;&#xA;uint32_t m_queue_limit;&#xA;uint32_t m_queue_dropfactor;&#xA;int64_t m_channel_layout;&#xA;AVSampleFormat m_device_format;&#xA;AVCodecID audio_codec_id{AV_CODEC_ID_NONE};&#xA;AVCodecContext* adecoder;&#xA;&#xA;player::PacketQueue queue(0, true); &#xA;&#xA;&#xA;static uint8_t* buf = nullptr;&#xA;uint32_t absize{};&#xA;uint32_t abpos{};&#xA;int32_t max_decoder_size{};&#xA;// need a converter?&#xA;uint8_t* convbuf{};&#xA;SwrContext* swrctx{};&#xA;int32_t sframes{};&#xA;AVCodec* codec{};&#xA;&#xA;uint8_t * audio_buffer_init() {&#xA;    if(buf == nullptr) {&#xA;        buf = (uint8_t*) malloc(buffer_max_size);&#xA;        if(buf == nullptr) {&#xA;            return nullptr;&#xA;        }&#xA;    }&#xA;    return buf;&#xA;}&#xA;&#xA;void audio_callback(void* userdata, uint8_t* stream, int len) {&#xA;    AVCodecContext* decoder = (AVCodecContext*)userdata;&#xA;    process(decoder, stream, len);&#xA;};&#xA;&#xA;void init_Speaker(const std::string&amp; codecName,&#xA;                 int32_t samplerate,&#xA;                 uint8_t channels,&#xA;                 uint32_t queue_limit,&#xA;                 uint32_t queue_dropfactor,&#xA;                 int64_t channel_layout,&#xA;                 AVSampleFormat format)&#xA;{&#xA;    m_samplerate = samplerate;&#xA;    m_queue_limit = queue_limit;&#xA;    m_queue_dropfactor = queue_dropfactor;&#xA;    m_device_format = format;&#xA;    m_channel_layout = channel_layout;&#xA;&#xA;&#xA;    SDL_SetHint(SDL_HINT_THREAD_STACK_SIZE, "8388608");&#xA;&#xA;    if(codecName.empty())&#xA;        throw std::runtime_error("audio decoder: no codec specified.");&#xA;&#xA;    auto names = player::lookup_ffmpeg_decoders(codecName);&#xA;    if(names == nullptr)&#xA;        throw std::runtime_error("audio decoder: cannot find decoder names for {}"&#x2B;codecName);&#xA;&#xA;    audio_codec_id = player::lookup_codec_id(codecName);&#xA;    codec      = player::avcodec_find_decoder(names, AV_CODEC_ID_NONE);&#xA;    if(codec == nullptr)&#xA;        throw std::runtime_error("audio decoder: cannot find the decoder for {}"&#x2B;codecName);&#xA;&#xA;    adecoder = avcodec_alloc_context3(codec);&#xA;    if(adecoder == nullptr)&#xA;        throw std::runtime_error("audio decoder: cannot allocate context");&#xA;&#xA;    adecoder->channels   = channels;&#xA;    adecoder->sample_rate = samplerate;&#xA;    if(adecoder->channels == 1)&#xA;    {&#xA;        adecoder->channel_layout = AV_CH_LAYOUT_MONO;&#xA;    }&#xA;    else if(adecoder->channels == 2)&#xA;    {&#xA;        adecoder->channel_layout = AV_CH_LAYOUT_STEREO;&#xA;    }&#xA;    else&#xA;        throw std::runtime_error("audio decoder: unsupported number of channels ({})"&#x2B; adecoder->channels);&#xA;    &#xA;&#xA;    if(avcodec_open2(adecoder, codec, nullptr) != 0)&#xA;        throw std::runtime_error("audio decoder: cannot open decoder");&#xA;&#xA;    parser = av_parser_init(codec->id);&#xA;&#xA;    SDL_AudioSpec wanted, spec;&#xA;    wanted.freq     = samplerate;&#xA;    wanted.format   = AUDIO_S16SYS;&#xA;    wanted.channels = channels;&#xA;    wanted.silence  = 0;&#xA;    wanted.samples  = buffer_size;&#xA;    wanted.userdata = adecoder;&#xA;    wanted.callback = audio_callback;&#xA;    &#xA;&#xA;    m_id = SDL_OpenAudioDevice(nullptr, 0, &amp;wanted, &amp;spec, 0);&#xA;    if(m_id == 0)&#xA;        throw std::runtime_error(SDL_GetError());&#xA;&#xA;    SDL_PauseAudioDevice(m_id, 0);&#xA;}&#xA;&#xA;void close_Speaker()&#xA;{&#xA;    SDL_CloseAudioDevice(m_id);&#xA;    &#xA;    if(adecoder != nullptr)&#xA;        player::avcodec_close(adecoder);&#xA;&#xA;}&#xA;&#xA;void play(uint8_t* buffer, size_t bufsize, microseconds pts, microseconds dts)&#xA;{&#xA;    if(!buffer || !bufsize) {&#xA;        return;&#xA;    }&#xA;    &#xA;    AVPacket* avpkt;&#xA;    avpkt = av_packet_alloc();&#xA;    av_init_packet(avpkt);&#xA;    uint8_t bf[bufsize &#x2B; 64];&#xA;    memcpy(bf, buffer, bufsize);&#xA;&#xA;    av_parser_parse2(parser, adecoder, &amp;avpkt->data, &amp;avpkt->size,&#xA;                           bf, bufsize,&#xA;                           pts.count(), dts.count(), 0);&#xA;&#xA;    queue.put(av_packet_clone(avpkt));&#xA;    queue.drop(m_queue_limit, m_queue_dropfactor); &#xA;&#xA;}&#xA;&#xA;//&#xA;&#xA;void process(AVCodecContext* decoder, uint8_t* stream, int ssize)&#xA;{&#xA;    auto filled = fillBuffer(decoder, stream, ssize);&#xA;&#xA;    auto unfilled{(ssize - filled) / 4};&#xA;    auto dummy{sframes};&#xA;&#xA;    sframes = unfilled == 0 ? 0 : sframes &#x2B; unfilled;&#xA;    memset(stream &#x2B; filled, 0, unfilled * 4);&#xA;    &#xA;    if(sframes != dummy)&#xA;        queue.add_silence((int64_t)sframes * 1000000 / m_samplerate);&#xA;}&#xA;&#xA;int fillBuffer(AVCodecContext* decoder, uint8_t* stream, int ssize)&#xA;{&#xA;    int filled{};&#xA;    AVPacket avpkt;&#xA;    audio_buffer_init();&#xA;    while(filled &lt; ssize)&#xA;    {&#xA;        int dsize{}, delta{};&#xA;&#xA;        // buffer has enough data&#xA;        if(absize - abpos >= static_cast<unsigned int="int">(ssize - filled))&#xA;        {&#xA;            delta = ssize - filled;&#xA;            std::copy(buf &#x2B; abpos, buf &#x2B; abpos &#x2B; delta, stream);&#xA;            abpos &#x2B;= delta;&#xA;            filled &#x2B;= delta;&#xA;            return ssize;&#xA;        }&#xA;        else if(absize - abpos > 0)&#xA;        {&#xA;            delta = absize - abpos;&#xA;            std::copy(buf &#x2B; abpos, buf &#x2B; abpos &#x2B; delta, stream);&#xA;            stream &#x2B;= delta;&#xA;            filled &#x2B;= delta;&#xA;            abpos = absize = 0;&#xA;        }&#xA;        // move data to head, leave more ab buffers&#xA;        if(abpos != 0)&#xA;        {&#xA;            std::copy(buf &#x2B; abpos, buf &#x2B; abpos &#x2B; absize - abpos, buf);&#xA;            absize -= abpos;&#xA;            abpos = 0;&#xA;        }&#xA;        // decode more packets&#xA;        if(!queue.get(&amp;avpkt, false))&#xA;            break;&#xA;        if((dsize = decode(decoder, &amp;avpkt, buf &#x2B; absize, buffer_max_size - absize)) &lt; 0)&#xA;            break;&#xA;        absize &#x2B;= dsize;&#xA;    }&#xA;&#xA;    return filled;&#xA;}&#xA;&#xA;int decode(AVCodecContext* decoder, AVPacket* pkt, uint8_t* dstbuf, int dstlen)&#xA;{&#xA;    const uint8_t* srcplanes[SWR_CH_MAX];&#xA;    uint8_t* dstplanes[SWR_CH_MAX];&#xA;    int filled{};&#xA;&#xA;    AVFrame* aframe = av_frame_alloc();&#xA;&#xA;    auto saveptr = pkt->data;&#xA;&#xA;    while(pkt->size > 0)&#xA;    {&#xA;        int len{}, got_frame{};&#xA;        unsigned char* srcbuf{};&#xA;        int datalen{};&#xA;&#xA;        if((len = avcodec_decode_audio4(decoder, aframe, &amp;got_frame, pkt)) &lt; 0)&#xA;        {&#xA;            return -1;&#xA;        }&#xA;        if(got_frame == 0)&#xA;        {&#xA;            pkt->size -= len;&#xA;            pkt->data &#x2B;= len;&#xA;            continue;&#xA;        }&#xA;&#xA;        if(aframe->format == m_device_format)&#xA;        {&#xA;            datalen = av_samples_get_buffer_size(nullptr,&#xA;                                                 aframe->channels /*rtspconf->audio_channels*/,&#xA;                                                 aframe->nb_samples,&#xA;                                                 (AVSampleFormat)aframe->format,&#xA;                                                 1 /*no-alignment*/);&#xA;            srcbuf  = aframe->data[0];&#xA;        }&#xA;        else&#xA;        {&#xA;            // need conversion!&#xA;            if(swrctx == nullptr)&#xA;            {&#xA;                if((swrctx = swr_alloc_set_opts(nullptr,&#xA;                                                m_channel_layout,&#xA;                                                m_device_format,&#xA;                                                m_samplerate,&#xA;                                                aframe->channel_layout,&#xA;                                                (AVSampleFormat)aframe->format,&#xA;                                                aframe->sample_rate,&#xA;                                                0,&#xA;                                                nullptr)) == nullptr)&#xA;                {&#xA;                    return -1;&#xA;                }&#xA;                auto err = swr_init(swrctx);&#xA;                if(err &lt; 0)&#xA;                {&#xA;                    char msg[1024];&#xA;                    av_strerror(err, msg, 1024);&#xA;                    return -1;&#xA;                }&#xA;                max_decoder_size = av_samples_get_buffer_size(nullptr,&#xA;                                                              2, &#xA;                                                              m_samplerate,&#xA;                                                              m_device_format,&#xA;                                                              1 /*no-alignment*/);&#xA;                if((convbuf = (unsigned char*)::malloc(max_decoder_size)) == nullptr)&#xA;                {&#xA;                    return -1;&#xA;                }&#xA;            }&#xA;            datalen = av_samples_get_buffer_size(nullptr,&#xA;                                                 2,&#xA;                                                 aframe->nb_samples,&#xA;                                                 m_device_format,&#xA;                                                 1 /*no-alignment*/);&#xA;            if(datalen > max_decoder_size)&#xA;            {&#xA;                return -1;&#xA;            }&#xA;            srcplanes[0] = aframe->data[0];&#xA;            if(av_sample_fmt_is_planar((AVSampleFormat)aframe->format) != 0)&#xA;            {&#xA;                // planar&#xA;                int i;&#xA;                for(i = 1; i &lt; aframe->channels; i&#x2B;&#x2B;)&#xA;                {&#xA;                    srcplanes[i] = aframe->data[i];&#xA;                }&#xA;                srcplanes[i] = nullptr;&#xA;            }&#xA;            else&#xA;            {&#xA;                srcplanes[1] = nullptr;&#xA;            }&#xA;            dstplanes[0] = convbuf;&#xA;            dstplanes[1] = nullptr;&#xA;&#xA;            swr_convert(swrctx, dstplanes, aframe->nb_samples, srcplanes, aframe->nb_samples);&#xA;            srcbuf = convbuf;&#xA;        }&#xA;        if(datalen > dstlen)&#xA;        {&#xA;            datalen = dstlen;&#xA;        }&#xA;&#xA;        std::copy(srcbuf, srcbuf &#x2B; datalen, dstbuf);&#xA;        dstbuf &#x2B;= datalen;&#xA;        dstlen -= datalen;&#xA;        filled &#x2B;= datalen;&#xA;&#xA;        pkt->size -= len;&#xA;        pkt->data &#x2B;= len;&#xA;        av_frame_unref(aframe);&#xA;    }&#xA;    pkt->data = saveptr;&#xA;    if(pkt->data)&#xA;        av_packet_unref(pkt);&#xA;    if(aframe != nullptr)&#xA;        av_frame_free(&amp;aframe);&#xA;    &#xA;    return filled;&#xA;}&#xA;</unsigned></iostream>

    &#xA;

    In packet_queue.cpp :

    &#xA;

    #include "packet_queue.h"&#xA;&#xA;using player::PacketQueue;&#xA;using lock_guard  = std::lock_guard;&#xA;using unique_lock = std::unique_lock;&#xA;using std::chrono::milliseconds;&#xA;&#xA;PacketQueue::PacketQueue(uint32_t playback_queue_silence, bool playback_queue_debug) :&#xA;        m_playback_queue_debug(playback_queue_debug), m_playback_queue_silence(playback_queue_silence)&#xA;{&#xA;}&#xA;&#xA;void PacketQueue::clear()&#xA;{&#xA;    lock_guard lk{m_mtx};&#xA;    for(auto&amp; pkt : queue)&#xA;        av_packet_unref(pkt);&#xA;&#xA;    m_size = 0;&#xA;    queue.clear();&#xA;}&#xA;&#xA;void PacketQueue::add_silence(int64_t silence_pts)&#xA;{&#xA;    if(m_playback_queue_silence == 0)&#xA;    {&#xA;        return;&#xA;    }&#xA;&#xA;    lock_guard lk{m_mtx};&#xA;    silence_pts = filtered_packets > 0 ? silence_pts : last_pts &#x2B; silence_pts;&#xA;&#xA;    auto tv  = std::chrono::microseconds{last_pts};&#xA;    auto tv2 = std::chrono::microseconds{silence_pts};&#xA;}&#xA;&#xA;bool PacketQueue::put(AVPacket* pkt)&#xA;{&#xA;    if(pkt == nullptr)&#xA;    {&#xA;        return false;&#xA;    }&#xA;&#xA;    lock_guard lk{m_mtx};&#xA;    if((silence_pts - pkt->pts) > (m_playback_queue_silence * 1000))&#xA;    {&#xA;        auto tv = std::chrono::microseconds{pkt->pts};&#xA;        filtered_packets&#x2B;&#x2B;;&#xA;        if(m_playback_queue_debug)&#xA;        return true;&#xA;    }&#xA;&#xA;    queue.push_back(pkt);&#xA;    filtered_packets = 0;&#xA;    return true;&#xA;}&#xA;&#xA;bool PacketQueue::get(AVPacket* pkt, bool block, milliseconds timeout)&#xA;{&#xA;    unique_lock lk{m_mtx};&#xA;&#xA;    for(;;)&#xA;    {&#xA;        if(queue.size() > 0)&#xA;        {&#xA;            auto ptr = queue.front();&#xA;            queue.pop_front();&#xA;            m_size -= ptr->size;&#xA;            last_pts = ptr->pts;&#xA;            av_packet_move_ref(pkt, ptr);&#xA;            return true;&#xA;        }&#xA;        else if(!block)&#xA;        {&#xA;            return false;&#xA;        }&#xA;        else if(!m_cv.wait_for(lk, timeout, [&amp;] { return !queue.empty(); }))&#xA;        {&#xA;            return false;&#xA;        }&#xA;&#xA;    }&#xA;    return false;&#xA;}&#xA;&#xA;bool PacketQueue::drop(size_t limit, size_t dropfactor)&#xA;{&#xA;    int dropped, count = 0;&#xA;&#xA;    lock_guard lk{m_mtx};&#xA;&#xA;    // queue size exceeded?&#xA;    if(queue.size() &lt;= limit)&#xA;    {&#xA;        return false;&#xA;    }&#xA;&#xA;    // start dropping&#xA;    dropped = queue.size() / dropfactor;&#xA;    // keep at least one&#xA;    if(dropped == queue.size())&#xA;        dropped--;&#xA;&#xA;    AVPacket* pkt;&#xA;    while(dropped-- > 0 &amp;&amp; !queue.empty())&#xA;    {&#xA;        pkt = queue.front();&#xA;&#xA;        if(pkt->flags != AV_PKT_FLAG_KEY)&#xA;        {&#xA;            queue.pop_front();&#xA;            m_size -= pkt->size;&#xA;            av_packet_unref(pkt);&#xA;            &#x2B;&#x2B;count;&#xA;        }&#xA;    }&#xA;&#xA;    return true; // count;&#xA;}&#xA;&#xA;int PacketQueue::drop2(size_t limit, bool error)&#xA;{&#xA;    int count = 0;&#xA;&#xA;    // dropping enabled?&#xA;    if(limit &lt;= 0 &amp;&amp; !error)&#xA;        return 0;&#xA;&#xA;    lock_guard lk{m_mtx};&#xA;    // queue size exceeded?&#xA;    if(queue.size() &lt;= limit &amp;&amp; !error)&#xA;        return false;&#xA;&#xA;    for(auto i = queue.begin(); i != queue.end();)&#xA;    {&#xA;        AVPacket* pkt = *i;&#xA;        if(pkt->flags != AV_PKT_FLAG_KEY)&#xA;        {&#xA;            m_size -= pkt->size;&#xA;            av_packet_unref(pkt);&#xA;            i = queue.erase(i);&#xA;            count&#x2B;&#x2B;;&#xA;        }&#xA;        else&#xA;            &#x2B;&#x2B;i;&#xA;    }&#xA;&#xA;    return count;&#xA;}&#xA;

    &#xA;

    1403 line of celt/bands.c :&#xA;screenshot

    &#xA;

    Versions of libraries that I tried on Debian 9 :&#xA;libavcodec.so.57.64.101 and libopus.so.0.5.3

    &#xA;

    also I built manually libavcodec.so.57.107.100 and libopus.so.0.8.0 and tried to use them - the same error appears.

    &#xA;

    As I already mentioned, everything works fine on Windows 10 and Ubuntu 18.04. So I have no clue what could be the reason of the issue. Any help is appreciated.

    &#xA;