<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix">On 8/25/2022 12:16 PM, Reynolds
      Kosloskey wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:6e1299b9-ba79-168e-b3e4-192dca2bb55c@kosloskey.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <p><font face="monospace">Hello all.</font></p>
      <p><font face="monospace">I am working on a capture/encoder
          project.  I've gotten it to work well enough, for the most
          part.</font></p>
      <p><font face="monospace">If I record shorter segments, like in
          the 1-2 minute range, the output files are fine.</font></p>
      <p><font face="monospace">However, as the clip gets longer, it
          eventually starts hitting an I/O Error (-5) when executing </font><font
          face="monospace"><b>av_interleaved_write_frame(format_context,
            output_packet).  </b>It never recovers from this, all
          writing to the file stops, and attempting to close the file
          results in an unplayable file.</font></p>
      <p><font face="monospace">This is the part of the code that writes
          audio frames, and also where the error is detected:<br>
        </font></p>
      <p><font face="monospace">int Recorder::audioWriteFrame(void* ctx,
          AVFrame* frame) {<br>
              if (!decoding) return -1;<br>
              if (recording) {<br>
                  frames_all++;<br>
                  frames_audio++;<br>
                  AVPacket* output_packet = av_packet_alloc();<br>
                  int ret = avcodec_send_frame(audio_context, frame);<br>
                  while (ret >= 0)<br>
                  {<br>
                      ret = avcodec_receive_packet(audio_context,
          output_packet);<br>
                      if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
          {<br>
                          break;<br>
                      }<br>
                      else if (ret < 0) {<br>
                          cout << "AUD-AVCRPX: ";<br>
                          cout << ret;<br>
                          break;<br>
                      }<br>
                      chrono::steady_clock::time_point now =
          chrono::high_resolution_clock::now();<br>
                      long long ms_elapsed =
          chrono::duration_cast<chrono::microseconds>(now -
          rec_started).count();<br>
                      ms_elapsed = av_rescale_q(ms_elapsed,
          audio_context->time_base, audio_stream->time_base);<br>
                      output_packet->pts = ms_elapsed;<br>
                      output_packet->dts = ms_elapsed;<br>
                      output_packet->stream_index =
          audio_stream->index;<br>
          <br>
          <b>            ret =
            av_interleaved_write_frame(format_context,
            output_packet);            </b><b><br>
          </b><b>            if (ret < 0) {</b><b><br>
          </b><b>                char* errmsg = new char[4096];</b><b><br>
          </b><b>                av_strerror(ret, errmsg, 4096);</b><b><br>
          </b><b>                cout << "audio
            av_interleaved_write_frame ERROR: ";</b><b><br>
          </b><b>                cout << errmsg;</b><b><br>
          </b><b>                cout << "\r\n";</b><b><br>
          </b><b>            }</b><b><br>
          </b><b>            ret = ret;</b><br>
                  }<br>
          <br>
                  av_packet_unref(output_packet);<br>
                  av_packet_free(&output_packet);<br>
              }<br>
              return 0;<br>
          }<br>
        </font></p>
      <p><font face="monospace"><br>
        </font></p>
      <p><font face="monospace">Any ideas why this is happening would be
          helpful.</font></p>
      <p><font face="monospace">--Reynolds<br>
        </font></p>
    </blockquote>
    <p>If anyone else comes across this error, one thing to look at
      would be the size of the buffers you're using.  In my case, I had
      an audio decoding thread that I passed a buffer to <b>avio_alloc_context</b>. 
      Increasing that buffer size seems to have gotten rid of the issue.<br>
    </p>
  </body>
</html>