Ctrl K
ComunidadGuías…

src/zm_videostore.cpp

Rama master · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
// ZoneMinder Video Storage Implementation
// Written by Chris Wiggins
// http://chriswiggins.co.nz
// Modification by Steve Gilvarry
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//

#include "zm_videostore.h"

#include "zm_logger.h"
#include "zm_monitor.h"
#include "zm_mp4_sidx.h"
#include "zm_signal.h"
#include "zm_time.h"
#include "zm_utils.h"

extern "C" {
#include <libavutil/time.h>
#include <libavutil/display.h>
}

#include <fcntl.h>
#include <string>
#include <unistd.h>
#include <sys/stat.h>

VideoStore::VideoStore(
  const char *filename_in,
  const char *format_in,
  AVStream *p_video_in_stream,
  AVCodecContext *p_video_in_ctx,
  AVStream *p_audio_in_stream,
  Monitor *p_monitor
) :
  chosen_codec_data(nullptr),
  monitor(p_monitor),
  out_format(nullptr),
  oc(nullptr),
  video_out_stream(nullptr),
  audio_out_stream(nullptr),
  video_in_ctx(p_video_in_ctx),
  video_out_ctx(nullptr),
  video_in_stream(p_video_in_stream),
  audio_in_stream(p_audio_in_stream),
  audio_in_codec(nullptr),
  // Opened in open() from audio_in_stream's codecpar, not handed in. The
  // caller used to pass a context as well and this ignored it.
  audio_in_ctx(nullptr),
  audio_out_codec(nullptr),
  audio_out_ctx(nullptr),
  packets_written(0),
  frame_count(0),
  video_encoded(false),
  video_passthrough_fallback(false),
  hw_device_ctx(nullptr),
  resample_ctx(nullptr),
  fifo(nullptr),
  converted_in_samples(nullptr),
  filename(filename_in ? filename_in : ""),
  format(format_in),
  video_first_pts(AV_NOPTS_VALUE),
  video_first_dts(AV_NOPTS_VALUE),
  audio_first_pts(AV_NOPTS_VALUE),
  audio_first_dts(AV_NOPTS_VALUE),
  video_last_pts(AV_NOPTS_VALUE),
  audio_last_pts(AV_NOPTS_VALUE),
  next_dts(nullptr),
  audio_next_pts(0),
  max_stream_index(-1),
  reorder_queue_size(0),
  last_fragment_offset_(0),
  last_fragment_start_dts_(AV_NOPTS_VALUE),
  init_segment_end_(0),
  sidx_region_offset_(-1),
  sidx_region_size_(0),
  finalized_(false),
  m3u8_fragments_written_(0),
  m3u8_target_duration_(0),
  m3u8_init_segment_end_(-1),
  m3u8_bytes_written_(0) {
  FFMPEGInit();
  swscale.init();
  opkt = av_packet_ptr{av_packet_alloc()};
}  // VideoStore::VideoStore

/* Failure to open audio will not be a total failure. */
bool VideoStore::Encoding() const {
  return monitor->GetOptVideoWriter() == Monitor::ENCODE and !video_passthrough_fallback;
}

bool VideoStore::open() {
  Debug(1, "Opening video storage stream %s format: %s", filename.c_str(), format);

  int ret = avformat_alloc_output_context2(&oc, nullptr, nullptr, filename.c_str());
  if (ret < 0) {
    Warning(
      "Could not create video storage stream %s as no out ctx"
      " could be assigned based on filename: %s",
      filename.c_str(), av_make_error_string(ret).c_str());
  }

  // Couldn't deduce format from filename, trying from format name
  if (!oc) {
    avformat_alloc_output_context2(&oc, nullptr, format, filename.c_str());
    if (!oc) {
      Error(
        "Could not create video storage stream %s as no out ctx"
        " could not be assigned based on filename or format %s",
        filename.c_str(), format);
      return false;
    }
  } // end if ! oc

  AVDictionary *pmetadata = nullptr;
  ret = av_dict_set(&pmetadata, "title", "Zoneminder Security Recording", 0);
  if (ret < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__);

  std::string options = monitor->GetEncoderOptions();
  AVDictionary *opts = nullptr;
  ret = av_dict_parse_string(&opts, options.c_str(), "=", "#,\n", 0);
  if (ret < 0) {
    Warning("Could not parse ffmpeg output options '%s'", options.c_str());
  } else {
    const AVDictionaryEntry *entry = av_dict_get(opts, "reorder_queue_size", nullptr, AV_DICT_MATCH_CASE);
    if (entry) {
      if (monitor->GetOptVideoWriter() == Monitor::ENCODE) {
        Debug(1, "reorder_queue_size ignored for non-passthrough");
      } else {
        reorder_queue_size = std::stoul(entry->value);
        Debug(1, "reorder_queue_size set to %zu", reorder_queue_size);
      }
      // remove it to prevent complaining later.
      av_dict_set(&opts, "reorder_queue_size", nullptr, AV_DICT_MATCH_CASE);
    } else if (monitor->has_out_of_order_packets()
        and !monitor->WallClockTimestamps()
        // Only sort packets for passthrough. Encoding uses wallclock by default
        and monitor->GetOptVideoWriter() == Monitor::PASSTHROUGH
        ) {
      reorder_queue_size = 2*monitor->get_max_keyframe_interval();
      Debug(1, "reorder_queue_size set to %zu", reorder_queue_size);
    }
  }

  oc->metadata = pmetadata;
  // Dirty hack to allow us to set flags. Needed for ffmpeg5
  out_format = const_cast<AVOutputFormat *>(oc->oformat);
  // ffmpeg 5 crashes if we do this
#if !LIBAVFORMAT_VERSION_CHECK(59, 16,100, 9, 0)
  out_format->flags |= AVFMT_TS_NONSTRICT; // allow non increasing dts
#endif

  const AVCodec *video_out_codec = nullptr;

  if (video_in_stream) {
    zm_dump_codecpar(video_in_stream->codecpar);

    if (monitor->GetOptVideoWriter() == Monitor::PASSTHROUGH) {
      video_out_stream = avformat_new_stream(oc, nullptr);
      if (!video_out_stream) {
        Error("Unable to create video out stream");
        return false;
      }
      avcodec_parameters_copy(video_out_stream->codecpar, video_in_stream->codecpar);
      zm_dump_codecpar(video_out_stream->codecpar);

      video_out_stream->avg_frame_rate = video_in_stream->avg_frame_rate;
      // Only set orientation if doing passthrough, otherwise the frame image will be rotated
      Monitor::Orientation orientation = monitor->getOrientation();
      if (orientation > 1) { // 1 is ROTATE_0
#if LIBAVCODEC_VERSION_CHECK(59, 37, 100, 37, 100)
        int32_t* displaymatrix = static_cast<int32_t*>(av_malloc(sizeof(int32_t)*9));
        // Initialise to identity so the matrix is always fully written before
        // it is attached as side data; av_display_rotation_set(.,0) yields the
        // identity matrix.
        av_display_rotation_set(displaymatrix, 0);
        Debug(3, "Have orientation %d", orientation);
        switch (orientation) {
          case Monitor::ROTATE_90:
            av_display_rotation_set(displaymatrix, 90);
            break;
          case Monitor::ROTATE_180:
            av_display_rotation_set(displaymatrix, 180);
            break;
          case Monitor::ROTATE_270:
            av_display_rotation_set(displaymatrix, 270);
            break;
          case Monitor::FLIP_HORI:
            av_display_matrix_flip(displaymatrix, 1, 0);
            break;
          case Monitor::FLIP_VERT:
            av_display_matrix_flip(displaymatrix, 0, 1);
            break;
          default:
            Warning("Unsupported Orientation(%d)", orientation);
            break;
        }
#endif
#if LIBAVCODEC_VERSION_CHECK(60, 31, 102, 31, 102)
        av_packet_side_data_add(
            &video_out_stream->codecpar->coded_side_data,
            &video_out_stream->codecpar->nb_coded_side_data,
            AV_PKT_DATA_DISPLAYMATRIX,
            (int32_t *)displaymatrix, sizeof(int32_t)*9, 0);
#else
#if LIBAVCODEC_VERSION_CHECK(59, 37, 100, 37, 100)
        av_stream_add_side_data(video_out_stream,
            AV_PKT_DATA_DISPLAYMATRIX,
					(uint8_t *)displaymatrix,
					sizeof(int32_t) * 9);
#endif
#endif
        // The legacy "rotate" metadata tag only expresses rotation. Flips are
        // carried by the display matrix side data above, so don't warn for them.
        switch (orientation) {
          case Monitor::ROTATE_90:
            ret = av_dict_set(&video_out_stream->metadata, "rotate", "90", 0);
            if (ret < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__);
            break;
          case Monitor::ROTATE_180:
            ret = av_dict_set(&video_out_stream->metadata, "rotate", "180", 0);
            if (ret < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__);
            break;
          case Monitor::ROTATE_270:
            ret = av_dict_set(&video_out_stream->metadata, "rotate", "270", 0);
            if (ret < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__);
            break;
          default:
            break;
        }
      } // end if orientation

      if (av_dict_get(opts, "new_extradata", nullptr, AV_DICT_MATCH_CASE)) {
        av_dict_set(&opts, "new_extradata", nullptr, 0);
        // Special flag to tell us to open a codec to get new extraflags to fix weird h265
        video_out_codec = avcodec_find_encoder(video_in_stream->codecpar->codec_id);
        if (video_out_codec) {
          video_out_ctx = avcodec_alloc_context3(video_out_codec);
          ret = avcodec_parameters_to_context(video_out_ctx, video_in_stream->codecpar);

          if (ret < 0) {
            Error("Could not initialize ctx parameters");
            return false;
          }
          //video_out_ctx->pix_fmt = fix_deprecated_pix_fmt(video_out_ctx->pix_fmt);
          if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
            video_out_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
          }
          video_out_ctx->time_base = video_in_ctx->time_base;
          if (!(video_out_ctx->time_base.num && video_out_ctx->time_base.den)) {
            Debug(2,"No timebase found in video in context, defaulting to Q");
            video_out_ctx->time_base = AV_TIME_BASE_Q;
          }
          video_out_ctx->bit_rate = video_in_ctx->bit_rate;
          video_out_ctx->gop_size = video_in_ctx->gop_size;
          video_out_ctx->has_b_frames = video_in_ctx->has_b_frames;
          video_out_ctx->max_b_frames = video_in_ctx->max_b_frames;
          video_out_ctx->qmin = video_in_ctx->qmin;
          video_out_ctx->qmax = video_in_ctx->qmax;
          // In passthrough mode, use the input context's pix_fmt
          video_out_ctx->sw_pix_fmt = video_in_ctx->pix_fmt;

          if (!av_dict_get(opts, "crf", nullptr, AV_DICT_MATCH_CASE)) {
            if (av_dict_set(&opts, "crf", "23", 0)<0)
              Warning("Can't set crf to 23");
          }

          if ((ret = avcodec_open2(video_out_ctx, video_out_codec, &opts)) < 0) {
            Warning("Can't open video codec (%s) %s", video_out_codec->name, av_make_error_string(ret).c_str());
            video_out_codec = nullptr;
            avcodec_free_context(&video_out_ctx);
          }
        }  // end if video_out_codec

        if (video_out_ctx) {
          ret = avcodec_parameters_from_context(video_out_stream->codecpar, video_out_ctx);
          if (ret < 0) {
            Error("Could not initialize stream parameters");
          }
          // Free the codec context now — it was only opened to generate new
          // extradata for the stream parameters and is not used for encoding
          // in PASSTHROUGH mode.  Leaving it alive causes flush_codecs() to
          // attempt flushing a codec that never received any frames, which
          // crashes in avcodec_send_frame with newer FFmpeg.
          avcodec_free_context(&video_out_ctx);
        }
        av_dict_free(&opts);
        // Reload it for next attempt and/or avformat open
        ret = av_dict_parse_string(&opts, options.c_str(), "=", "#,\n", 0);
        if (ret < 0) {
          Warning("Could not parse ffmpeg output options '%s'", options.c_str());
        } else {
          if (reorder_queue_size) {
            av_dict_set(&opts, "reorder_queue_size", nullptr, AV_DICT_MATCH_CASE);
          }
        }
      }  // end if extradata_entry
    } else if (monitor->GetOptVideoWriter() == Monitor::ENCODE) {
      std::string wanted_codec = monitor->OutputCodec();
      if (wanted_codec.empty() or wanted_codec == "auto") {
        Debug(2, "Codec wanted %s", wanted_codec.c_str());
      }
      std::string wanted_encoder = monitor->Encoder();

      std::list<const CodecData *>codec_data = get_encoder_data(wanted_codec, wanted_encoder);
      if (!codec_data.size() and !wanted_encoder.empty()) {
        Warning("Did not find encoder matching %s. Trying without.", wanted_encoder.c_str());
        codec_data = get_encoder_data(wanted_codec, "");
      }
      if (!codec_data.size() and (!wanted_codec.empty() and wanted_codec != "auto")) {
        Warning("Did not find encoder matching codec %s. Trying without.", wanted_codec.c_str());
        codec_data = get_encoder_data("", "");
      }

      // Opening an encoder can fail because a previous event on this monitor is
      // still closing on the close thread and has not yet released the hardware
      // encoder session it holds. Wrapped so it can be run a second time once
      // that close has been waited for. Each iteration re-parses its own options
      // dictionary and frees its context on failure, so a second run starts
      // clean.
      auto attempt_open_encoders = [&]() {
        for (auto it = codec_data.begin(); it != codec_data.end(); it ++) {
          chosen_codec_data = *it;
          Debug(1, "Found video codec for %s", chosen_codec_data->codec_name);

          video_out_codec = avcodec_find_encoder_by_name(chosen_codec_data->codec_name);
          if (!video_out_codec) {
            // This ffmpeg build has no encoder by that name -- common for a hardware
            // encoder on a box that lacks it. Skip this candidate rather than allocate
            // a context with a null codec and later dereference video_out_codec->name
            // when avcodec_open2() fails. If every candidate is missing, video_out_codec
            // stays null and the passthrough fallback below takes over.
            Warning("No encoder named %s is available in this ffmpeg build; skipping", chosen_codec_data->codec_name);
            continue;
          }
          video_out_ctx = avcodec_alloc_context3(video_out_codec);
          if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
            video_out_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
          }

          // We have to re-parse the options because each attempt to open destroys the dictionary
          AVDictionary *opts = 0;
          ret = av_dict_parse_string(&opts, options.c_str(), "=", ",#\n", 0);
          if (ret < 0) {
            Warning("Could not parse ffmpeg encoder options list '%s'", options.c_str());
          } else {
            const AVDictionaryEntry *entry = av_dict_get(opts, "reorder_queue_size", nullptr, AV_DICT_MATCH_CASE);
            if (entry) {
              reorder_queue_size = std::stoul(entry->value);
              Debug(1, "reorder_queue_size set to %zu", reorder_queue_size);
              // remove it to prevent complaining later.
              av_dict_set(&opts, "reorder_queue_size", nullptr, AV_DICT_MATCH_CASE);
            }
          }
          const AVDictionaryEntry *opts_bitrate = av_dict_get(opts, "bitrate", nullptr, AV_DICT_MATCH_CASE);
          if (opts_bitrate) {
            video_out_ctx->bit_rate = std::stoul(opts_bitrate->value);
            av_dict_set(&opts, "bitrate", nullptr, AV_DICT_MATCH_CASE);
          } else {
            opts_bitrate = av_dict_get(opts, "bit_rate", nullptr, AV_DICT_MATCH_CASE);
            if (opts_bitrate) {
              video_out_ctx->bit_rate = std::stoul(opts_bitrate->value);
              av_dict_set(&opts, "bit_rate", nullptr, AV_DICT_MATCH_CASE);
            }
          }

          // When encoding, we are going to use the timestamp values instead of packet pts/dts
          video_out_ctx->time_base = AV_TIME_BASE_Q;
          video_out_ctx->codec_id = chosen_codec_data->codec_id;
          video_out_ctx->pix_fmt = chosen_codec_data->hw_pix_fmt;
          video_out_ctx->sw_pix_fmt = chosen_codec_data->sw_pix_fmt;
          Debug(1, "Setting pix fmt to %d %s", video_out_ctx->pix_fmt, av_get_pix_fmt_name(video_out_ctx->pix_fmt));
          const AVDictionaryEntry *opts_level = av_dict_get(opts, "level", nullptr, AV_DICT_MATCH_CASE);
          if (opts_level) {
            video_out_ctx->level = std::stoul(opts_level->value);
          }
          const AVDictionaryEntry *opts_gop_size = av_dict_get(opts, "gop_size", nullptr, AV_DICT_MATCH_CASE);
          if (opts_gop_size) {
            video_out_ctx->gop_size = std::stoul(opts_gop_size->value);
            av_dict_set(&opts, "gop_size", nullptr, AV_DICT_MATCH_CASE);
          }

          // Don't have an input stream, so need to tell it what we are sending it, or are transcoding
          video_out_ctx->width = monitor->Width();
          video_out_ctx->height = monitor->Height();
          video_out_ctx->codec_type = AVMEDIA_TYPE_VIDEO;

          if (video_out_ctx->codec_id == AV_CODEC_ID_H264) {
            if (!video_out_ctx->bit_rate) video_out_ctx->bit_rate = 2000000;
            video_out_ctx->max_b_frames = 1;
          } else if (video_out_ctx->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
            /* just for testing, we also add B frames */
            video_out_ctx->max_b_frames = 2;
          } else if (video_out_ctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
            /* Needed to avoid using macroblocks in which some coeffs overflow.
             * This does not happen with normal video, it just happens here as
             * the motion of the chroma plane does not match the luma plane. */
            video_out_ctx->mb_decision = 2;
          }
#if HAVE_LIBAVUTIL_HWCONTEXT_H && LIBAVCODEC_VERSION_CHECK(57, 107, 0, 107, 0)
          // Borrow the monitor's shared encoder device context rather than
          // letting setup_hwaccel open the render node again for this event.
          // Creating and destroying a VAAPI device per event leaked a
          // /dev/dri/renderD128 fd each time on some drivers (refs #5300). If
          // the monitor can't provide one, hw_device_ctx stays null and
          // setup_hwaccel falls back to creating its own.
          if (chosen_codec_data->hwdevice_type != AV_HWDEVICE_TYPE_NONE) {
            std::string hw_device = monitor->EncoderHWAccelDevice();
            if (hw_device.empty() and chosen_codec_data->hwdevice_default)
              hw_device = chosen_codec_data->hwdevice_default;
            hw_device_ctx = monitor->GetEncoderHWDeviceContext(chosen_codec_data->hwdevice_type, hw_device);
          }
#endif
          if (setup_hwaccel(video_out_ctx,
                chosen_codec_data, hw_device_ctx, monitor->EncoderHWAccelDevice(), monitor->Width(), monitor->Height())) {
            avcodec_free_context(&video_out_ctx);
            av_dict_free(&opts);
            if (hw_device_ctx) {
              av_buffer_unref(&hw_device_ctx);
            }
            continue;
          }

          zm_dump_codec(video_out_ctx);
          if ((ret = avcodec_open2(video_out_ctx, video_out_codec, &opts)) < 0) {
            if (wanted_encoder != "" and wanted_encoder != "auto") {
              Warning("Can't open video codec (%s) %s",
                      video_out_codec->name,
                      av_make_error_string(ret).c_str()
                     );
            } else {
              Debug(1, "Can't open video codec (%s) %s",
                    video_out_codec->name,
                    av_make_error_string(ret).c_str()
                   );
            }
            video_out_codec = nullptr;
          }

          AVDictionaryEntry *e = nullptr;
          while ((e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != nullptr) {
            Warning("Encoder Option %s not recognized by ffmpeg codec", e->key);
          }
          av_dict_free(&opts);

          if (video_out_codec) {
            zm_dump_codec(video_out_ctx);
            break;
          }
          // We allocate and copy in newer ffmpeg, so need to free it
          avcodec_free_context(&video_out_ctx);
          if (hw_device_ctx) {
            av_buffer_unref(&hw_device_ctx);
          }
        }  // end foreach codec
      };  // end lambda attempt_open_encoders

      attempt_open_encoders();

      // Only serialise against a close when we actually have to, and only when
      // there was one in flight to wait for.
      if (!video_out_codec and monitor->WaitForEventClose()) {
        Info("No video encoder would open and a previous event was still closing; "
             "waited for it to release its encoder, retrying");
        attempt_open_encoders();
      }

      if (!video_out_codec) {
        // Every candidate encoder refused to open. On a hardware encoder this is
        // usually the card declining for want of capacity, reported to ffmpeg as
        // nothing more specific than a generic external error. Returning here
        // would mean the event records no video at all, so copy the input stream
        // and write its packets through unchanged instead. If this keeps
        // happening the card is over-subscribed: fewer concurrent encodes, or a
        // lower resolution or frame rate.
        Warning("Can't open any video encoder; falling back to passthrough recording");
        if (video_out_ctx) avcodec_free_context(&video_out_ctx);
        video_passthrough_fallback = true;

        // Same guard the PASSTHROUGH path needs: a stream reporting no size at
        // all cannot be copied into the muxer, which aborts writing the trailer
        // rather than returning an error. Record jpegs for this event instead.
        if (video_in_stream->codecpar->width <= 0 || video_in_stream->codecpar->height <= 0) {
          Warning("Input video stream has invalid dimensions %dx%d; not recording video",
              video_in_stream->codecpar->width, video_in_stream->codecpar->height);
          return false;
        }

        video_out_stream = avformat_new_stream(oc, nullptr);
        if (!video_out_stream) {
          Error("Unable to create video out stream");
          return false;
        }
        avcodec_parameters_copy(video_out_stream->codecpar, video_in_stream->codecpar);
        video_out_stream->avg_frame_rate = video_in_stream->avg_frame_rate;
        zm_dump_codecpar(video_out_stream->codecpar);
      } else {
        Debug(2, "Success opening codec");

        video_out_stream = avformat_new_stream(oc, nullptr);
        if (!video_out_stream) {
          Error("Unable to create video out stream");
          return false;
        }
        ret = avcodec_parameters_from_context(video_out_stream->codecpar, video_out_ctx);
        if (ret < 0) {
          Error("Could not initialize stream parameters");
          return false;
        }
      }  // end if an encoder opened
    }  // end if copying or transcoding
  }  // end if video_in_stream

  max_stream_index = video_out_stream->index;
  last_dts[video_out_stream->index] = AV_NOPTS_VALUE;

  reorder_queues[video_out_stream->index] = {};

  video_out_stream->time_base = video_in_stream ? video_in_stream->time_base : AV_TIME_BASE_Q;

  if (audio_in_stream) {
    Debug(2, "Have audio_in_stream %p", audio_in_stream);

    if (CODEC(audio_in_stream)->codec_id != AV_CODEC_ID_AAC or CODEC(audio_in_stream)->codec_tag != 0) {
      audio_out_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
      if (!audio_out_codec) {
        Error("Could not find codec for AAC");
      } else {
        // Newer ffmpeg wants to keep everything separate... so have to lookup our own
        // decoder, can't reuse the one from the camera.
        audio_in_codec = avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
        audio_in_ctx = avcodec_alloc_context3(audio_in_codec);
        // Copy params from instream to ctx
        ret = avcodec_parameters_to_context(audio_in_ctx, audio_in_stream->codecpar);
        if (ret < 0) {
          Error("Unable to copy audio params to ctx %s", av_make_error_string(ret).c_str());
        }
        audio_in_ctx->time_base = audio_in_stream->time_base;

        // if the codec is already open, nothing is done.
        if ((ret = avcodec_open2(audio_in_ctx, audio_in_codec, nullptr)) < 0) {
          Error("Can't open audio in codec!");
        } else {
          audio_out_ctx = avcodec_alloc_context3(audio_out_codec);
          if (!audio_out_ctx) {
            Error("could not allocate codec ctx for AAC");
          } else {
            audio_out_stream = avformat_new_stream(oc, audio_out_codec);
            audio_out_stream->time_base = audio_in_stream->time_base;

            setup_resampler();
          } // end fail to alloc audio_out_ctx
        } // codec opened
      }  // end if found AAC codec
    } else {
      Debug(2, "Got AAC");

      // normally we want to pass params from codec in here
      // but since we are doing audio passthrough we don't care
      audio_out_stream = avformat_new_stream(oc, audio_out_codec);
      if (!audio_out_stream) {
        Error("Could not allocate new stream");
      } else {
        audio_out_stream->time_base = audio_in_stream->time_base;

        // Just use the ctx to copy the parameters over
        audio_out_ctx = avcodec_alloc_context3(audio_out_codec);
        if (!audio_out_ctx) {
          Error("Could not allocate new output_context");
        } else {

          // Copy params from instream to ctx
          ret = avcodec_parameters_to_context(audio_out_ctx, audio_in_stream->codecpar);
          if (ret < 0) {
            Error("Unable to copy audio params to ctx %s", av_make_error_string(ret).c_str());
          }
          ret = avcodec_parameters_from_context(audio_out_stream->codecpar, audio_out_ctx);
          if (ret < 0) {
            Error("Unable to copy audio params to stream %s", av_make_error_string(ret).c_str());
          }

#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
          /* Seems like technically we could have multiple channels, so let's not implement this for ffmpeg 5 */
#else
          if (audio_out_ctx->channels > 1) {
            Warning("Audio isn't mono, changing it.");
            audio_out_ctx->channels = 1;
          } else {
            Debug(3, "Audio is mono");
          }
#endif
          if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
            audio_out_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
          }
         
          // We will assume that subsequent stream allocations will increase the index
          max_stream_index = audio_out_stream->index;
          last_dts[audio_out_stream->index] = AV_NOPTS_VALUE;
        } // end if audio_out_ctx
      } // end if audio_out_stream
    } // end if is AAC

    if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
      audio_out_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }

    // We will assume that subsequent stream allocations will increase the index
    max_stream_index = audio_out_stream->index;
    last_dts[audio_out_stream->index] = AV_NOPTS_VALUE;
    reorder_queues[audio_out_stream->index] = {};
  }  // end if audio_in_stream

  //max_stream_index is 0-based, so add 1
  next_dts = new int64_t[max_stream_index+1];
  for (int i = 0; i <= max_stream_index; i++) {
    next_dts[i] = 0;
  }

  /* open the out file, if needed */
  if (!(out_format->flags & AVFMT_NOFILE)) {
    ret = avio_open2(&oc->pb, filename.c_str(), AVIO_FLAG_WRITE, nullptr, nullptr);
    if (ret < 0) {
      Error("Could not open out file '%s': %s", filename.c_str(), av_make_error_string(ret).c_str());
      return false;
    }
  }

  zm_dump_stream_format(oc, 0, 0, 1);
  if (audio_out_stream) zm_dump_stream_format(oc, 1, 0, 1);

  const AVDictionaryEntry *movflags_entry = av_dict_get(opts, "movflags", nullptr, AV_DICT_MATCH_CASE);
  if (!movflags_entry) {
    Debug(1, "setting movflags to frag_keyframe+empty_moov+default_base_moof");
    av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov+default_base_moof", 0);
  } else {
    Debug(1, "using movflags %s", movflags_entry->value);
    // faststart restructures a single non-fragmented moov atom by re-opening the
    // file after the trailer is written. It is incompatible with the fragmented
    // (frag_keyframe/empty_moov) + mfra/HLS output this class produces, and its
    // re-open-by-name pass fails if the file was renamed while open. Warn so it
    // is removed from the monitor's encoder options.
    if (strstr(movflags_entry->value, "faststart")) {
      Warning("movflags contains 'faststart', which is incompatible with the "
              "fragmented MP4 / HLS output used here. Remove it from the "
              "monitor's encoder options to avoid trailer-write failures.");
    }
  }
  if ((ret = avformat_write_header(oc, &opts)) < 0) {
    // we crash if we try again
    if (ENOSPC != ret) {
      Warning("Unable to set movflags trying with defaults.%d %s",
              ret, av_make_error_string(ret).c_str());

      ret = avformat_write_header(oc, nullptr);
      Debug(1, "Done %d", ret);
    } else {
      Error("ENOSPC. fail");
    }
  } else if (av_dict_count(opts) != 0) {
    Info("some options not used, turn on debugging for a list.");
    AVDictionaryEntry *e = nullptr;
    while ((e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != nullptr) {
      Debug(1, "Encoder Option %s=>%s", e->key, e->value);
      if (!e->value) {
        av_dict_set(&opts, e->key, nullptr, 0);
      }
    }
  }
  av_dict_free(&opts);
  if (ret < 0) {
    Error("Error occurred when writing out file header to %s: %s",
          filename.c_str(), av_make_error_string(ret).c_str());
    avio_closep(&oc->pb);
    return false;
  }

  zm_dump_stream_format(oc, 0, 0, 1);
  if (audio_out_stream) zm_dump_stream_format(oc, 1, 0, 1);

  // Record where the init segment ends (after ftyp+moov written by header)
  if (oc->pb) {
    init_segment_end_ = avio_tell(oc->pb);
    last_fragment_offset_ = init_segment_end_;
    Debug(1, "Init segment ends at byte %" PRId64, init_segment_end_);

    // Reserve room for a leading sidx, right here between moov and the first
    // fragment. It has to be here, and it has to be reserved up front: the
    // index must END where the fragments BEGIN for a player to accept it
    // (zm_mp4_sidx.h), and once a fragment is written nothing can be inserted
    // ahead of it without rewriting every offset in the file. For now it is a
    // `free` box the demuxers skip; finalize() fills it in once the fragments
    // are all on disk. The muxer takes its own offsets from avio_tell, so
    // these bytes are accounted for in every tfhd and tfra it goes on to write.
    // Only an MP4 whose moov is already written and whose fragments follow it
    // gets one (zm_mp4::fragments_follow_header).
    //
    // It is sized for a section's worth of fragments, one per GOP. The GOP is
    // the encoder's when encoding, else the longest the camera has sent so
    // far. Events that run past the section length, or GOPs shorter than
    // that, only merge references in the index. When either is unknown the
    // full kSidxReserve is taken.
    const double fps = monitor->get_capture_fps();
    const int gop_frames = Encoding() ? video_out_ctx->gop_size : monitor->get_max_keyframe_interval();
    const double gop_seconds = (fps > 0 && gop_frames > 0) ? gop_frames / fps : 0;
    sidx_region_size_ = zm_mp4::reserve_size(monitor->GetSectionLength().count(), gop_seconds);
    sidx_region_offset_ = zm_mp4::reserve_region(oc, sidx_region_size_);
    last_fragment_offset_ = avio_tell(oc->pb);
  }
  return true;
} // end bool VideoStore::open()

void VideoStore::set_filename(const std::string &new_filename) {
  filename = new_filename;
  // oc->url is what FFmpeg's MOV muxer re-opens during the faststart trailer
  // pass; keep it pointing at the real on-disk name.
  if (oc) {
    av_freep(&oc->url);
    oc->url = av_strdup(new_filename.c_str());
  }
}

void VideoStore::flush_codecs() {
  // The codec queues data.  We need to send a flush command and out
  // whatever we get. Failures are not fatal.
  av_packet_ptr pkt{av_packet_alloc()};

  if (!pkt) {
    Error("Unable to allocate packet.");
    return;
  }

  // I got crashes if the codec didn't do DELAY, so let's test for it.
  // Also skip if no frames were ever sent — some encoders crash on flush
  // when their internal state was never initialized by a real frame.
  if (video_out_ctx && video_encoded && video_out_ctx->codec && (video_out_ctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
    // First drain any pending packets before entering flush mode
    // This prevents hangs when the encoder's internal buffer is full
    Debug(1, "Draining pending packets before flush");
    int ret;
    while ((ret = avcodec_receive_packet(video_out_ctx, pkt.get())) >= 0) {
      Debug(1, "Drained pending packet");
      av_packet_guard pkt_guard{pkt};
      av_packet_rescale_ts(pkt.get(), video_out_ctx->time_base, video_out_stream->time_base);
      write_packet(pkt.get(), video_out_stream);
    }

    // Put encoder into flushing mode
    Debug(1, "Sending flush");
    ret = avcodec_send_frame(video_out_ctx, nullptr);
    if (ret < 0) {
      if (ret != AVERROR_EOF)
        Error("Failure sending null to flush codec %d %s", ret, av_make_error_string(ret).c_str());
    } else {
      Debug(1, "Receiving flushed packets");
      while ((ret = avcodec_receive_packet(video_out_ctx, pkt.get())) >= 0) {
        Debug(1, "Received flushed packet");
        av_packet_guard pkt_guard{pkt};
        av_packet_rescale_ts(pkt.get(), video_out_ctx->time_base, video_out_stream->time_base);
        write_packet(pkt.get(), video_out_stream);
      } // while have buffered frames
    }
    Debug(1, "Done writing buffered video.");
  } // end if have delay capability

  if (audio_out_codec) {
    // The codec queues data.  We need to send a flush command and out
    // whatever we get. Failures are not fatal.

    Debug(1, "Sending audio flush");
    int frame_size = audio_out_ctx->frame_size;
    /*
     * At the end of the file, we pass the remaining samples to
     * the encoder. */
    while (zm_resample_get_delay(resample_ctx, audio_out_ctx->sample_rate)) {
      zm_resample_audio(resample_ctx, nullptr, out_frame.get());

      if (zm_add_samples_to_fifo(fifo, out_frame.get())) {
        // Should probably set the frame size to what is reported FIXME
        if (zm_get_samples_from_fifo(fifo, out_frame.get())) {
          if (zm_send_frame_receive_packet(audio_out_ctx, out_frame.get(), *pkt) > 0) {
            av_packet_guard pkt_guard{pkt};
            av_packet_rescale_ts(pkt.get(),
                                 audio_out_ctx->time_base,
                                 audio_out_stream->time_base);
            write_packet(pkt.get(), audio_out_stream);
          }
        }  // end if data returned from fifo
      }
    } // end while have buffered samples in the resampler

    Debug(2, "av_audio_fifo_size = %d", av_audio_fifo_size(fifo));
    while (av_audio_fifo_size(fifo) > 0) {
      /* Take one frame worth of audio samples from the FIFO buffer,
       * encode it and write it to the output file. */

      Debug(1, "Remaining samples in fifo for AAC codec frame_size %d > fifo size %d",
            frame_size, av_audio_fifo_size(fifo));

      // SHould probably set the frame size to what is reported FIXME also deprecate zm_send_frame_receive_packet
      if (av_audio_fifo_read(fifo, (void **)out_frame->data, frame_size)) {
        if (zm_send_frame_receive_packet(audio_out_ctx, out_frame.get(), *pkt)) {
          av_packet_guard pkt_guard{pkt};
          pkt->stream_index = audio_out_stream->index;

          av_packet_rescale_ts(pkt.get(),
                               audio_out_ctx->time_base,
                               audio_out_stream->time_base);
          write_packet(pkt.get(), audio_out_stream);
        }
      }  // end if data returned from fifo
    }  // end while still data in the fifo

    // Put encoder into flushing mode
    avcodec_send_frame(audio_out_ctx, nullptr);

    while (true) {
      if (0 >= zm_receive_packet(audio_out_ctx, *pkt)) {
        Debug(1, "No more packets");
        break;
      }
      av_packet_guard pkt_guard{pkt};

      ZM_DUMP_PACKET(pkt, "raw from encoder");
      av_packet_rescale_ts(pkt.get(), audio_out_ctx->time_base, audio_out_stream->time_base);
      ZM_DUMP_STREAM_PACKET(audio_out_stream, pkt, "writing flushed packet");
      write_packet(pkt.get(), audio_out_stream);
    }  // while have buffered frames
  }  // end if audio_out_codec
Debug(1, "Done flushing");
}  // end flush_codecs

VideoStore::~VideoStore() {

  // Run the shutdown path through finalize() so the queue-drain / trailer /
  // close logic lives in one place. finalize() is idempotent and bails early
  // if oc was never allocated, so the legacy "caller didn't call finalize"
  // path and the open()-failed-before-allocating-oc path both work.
  finalize();

  // I wonder if we should be closing the file first.
  // I also wonder if we really need to be doing all the ctx
  // allocation/de-allocation constantly, or whether we can just re-use it.
  // Just do a file open/close/writeheader/etc.
  // What if we were only doing audio recording?

  video_in_ctx = nullptr;

  if (video_out_ctx) {
    //avcodec_close(video_out_ctx);
    Debug(3, "Freeing video_out_ctx");
    avcodec_free_context(&video_out_ctx);
    if (hw_device_ctx) {
      Debug(3, "Freeing hw_device_ctx");
      av_buffer_unref(&hw_device_ctx);
    }
  }

  if (audio_out_stream) {
    audio_in_codec = nullptr;
    if (audio_in_ctx) {
      //avcodec_close(audio_in_ctx);
      avcodec_free_context(&audio_in_ctx);
    }

    if (audio_out_ctx) {
      Debug(4, "Success closing audio_out_ctx");
      //avcodec_close(audio_out_ctx);
      avcodec_free_context(&audio_out_ctx);
    }

    if (resample_ctx) {
      if (fifo) {
        av_audio_fifo_free(fifo);
        fifo = nullptr;
      }
      swr_free(&resample_ctx);
    }
    if (converted_in_samples) {
      av_free(converted_in_samples);
      converted_in_samples = nullptr;
    }
  } // end if audio_out_stream

  Debug(4, "free context");
  /* free the streams */
  avformat_free_context(oc);
  delete[] next_dts;
  next_dts = nullptr;
} // VideoStore::~VideoStore()

bool VideoStore::setup_resampler() {
  int ret;


  Debug(2, "Got something other than AAC (%s)", audio_in_codec->name);

  // Some formats (i.e. WAV) do not produce the proper channel layout
  // Perhaps we should not be modifying the audio_in_ctx....
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
  // channel_layout is deprecated need to use ch_layout
  // based on changes to ffmpeg/doc/examples/transcoding.c
  if (audio_in_ctx->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
    char buf[64];
    av_channel_layout_default(&audio_in_ctx->ch_layout, audio_in_ctx->ch_layout.nb_channels);
    ret = av_channel_layout_describe(&audio_in_ctx->ch_layout, buf, sizeof(buf));
    Info("Setting input channel layout fron unspecified to default ret=%d describe=%s)", ret, buf);
  }
#else
  if (audio_in_ctx->channel_layout == 0) {
    Debug(2, "Setting input channel layout to mono");
    audio_in_ctx->channel_layout = av_get_channel_layout("mono");
  }
#endif

  /* put sample parameters */
  audio_out_ctx->bit_rate = audio_in_ctx->bit_rate <= 32768 ? audio_in_ctx->bit_rate : 32768;
  audio_out_ctx->sample_rate = audio_in_ctx->sample_rate;
  audio_out_ctx->sample_fmt = audio_in_ctx->sample_fmt;
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
  av_channel_layout_copy(&audio_out_ctx->ch_layout, &audio_in_ctx->ch_layout);
#else
  audio_out_ctx->channels = audio_in_ctx->channels;
  audio_out_ctx->channel_layout = audio_in_ctx->channel_layout;
  if (!audio_out_ctx->channel_layout) {
    Debug(3, "Correcting channel layout from (%" PRIi64 ") to (%" PRIi64 ")",
          audio_out_ctx->channel_layout,
          av_get_default_channel_layout(audio_out_ctx->channels)
         );
    audio_out_ctx->channel_layout = av_get_default_channel_layout(audio_out_ctx->channels);
  }
#endif

#if LIBAVCODEC_VERSION_CHECK(61, 19,100, 19, 100)
  const enum AVSampleFormat *sample_fmts;
  const int *supported_samplerates;
  int num_sample_fmts, num_samplerates;
  ret = avcodec_get_supported_config(audio_out_ctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT,
      0, (const void **) &sample_fmts,
      &num_sample_fmts);
  if (ret < 0)
    return ret;
  if (sample_fmts) {
    int i;
    for (i = 0; i < num_sample_fmts; i++) {
      if (audio_out_ctx->sample_fmt == sample_fmts[i])
        break;
      if (audio_out_ctx->ch_layout.nb_channels == 1 &&
          av_get_planar_sample_fmt(audio_out_ctx->sample_fmt) ==
          av_get_planar_sample_fmt(sample_fmts[i])) {
        audio_out_ctx->sample_fmt = sample_fmts[i];
        break;
      }
    }
    if (i == num_sample_fmts) {
      Debug(1, "Specified sample format %s is not supported by the %s encoder",
          av_get_sample_fmt_name(audio_out_ctx->sample_fmt), audio_out_codec->name);

      Debug(1, "Supported sample formats:");
      for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
        Debug(1, "  %s", av_get_sample_fmt_name(sample_fmts[p]));
      }
      // AAC ONLY supports fltp
      audio_out_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
    }
  } // end if sample_fmts

  ret = avcodec_get_supported_config(audio_out_ctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE,
      0, (const void **) &supported_samplerates,
      &num_samplerates);
  if (ret < 0)
    return ret;
  if (supported_samplerates) {
    int i;
    for (i = 0; i < num_samplerates; i++)
      if (audio_out_ctx->sample_rate == supported_samplerates[i])
        break;
    if (i == num_samplerates) {
      Debug(1, "Specified sample rate %d is not supported by the %s encoder", audio_out_ctx->sample_rate, audio_out_codec->name);

      Debug(1, "Supported sample rates:");
      for (int p = 0; supported_samplerates[p]; p++)
        Debug(1, "  %d\n", supported_samplerates[p]);

      audio_out_ctx->sample_rate = supported_samplerates[0];
    }
  }
#else
  if (audio_out_codec->supported_samplerates) {
    int found = 0;
    for (unsigned int i = 0; audio_out_codec->supported_samplerates[i]; i++) {
      if (audio_out_ctx->sample_rate ==
          audio_out_codec->supported_samplerates[i]) {
        found = 1;
        break;
      }
    }
    if (found) {
      Debug(3, "Sample rate is good %d", audio_out_ctx->sample_rate);
    } else {
      audio_out_ctx->sample_rate = audio_out_codec->supported_samplerates[0];
      Debug(1, "Sample rate is no good, setting to (%d)",
            audio_out_codec->supported_samplerates[0]);
    }
  }

  /* check that the encoder supports s16 pcm in */
  if (!check_sample_fmt(audio_out_codec, audio_out_ctx->sample_fmt)) {
    Debug(3, "Encoder does not support sample format %s, setting to FLTP",
          av_get_sample_fmt_name(audio_out_ctx->sample_fmt));
    audio_out_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  }
#endif

  // Example code doesn't set the codec tb.  I think it just uses whatever defaults
  //audio_out_ctx->time_base = (AVRational){1, audio_out_ctx->sample_rate};

  AVDictionary *opts = nullptr;
  // Needed to allow AAC
  if ((ret = av_dict_set(&opts, "strict", "experimental", 0)) < 0) {
    Error("Couldn't set experimental");
  }
  ret = avcodec_open2(audio_out_ctx, audio_out_codec, &opts);
  av_dict_free(&opts);
  if (ret < 0) {
    Error("could not open codec (%d) (%s)",
          ret, av_make_error_string(ret).c_str());
    audio_out_codec = nullptr;
    audio_out_ctx = nullptr;
    audio_out_stream = nullptr;
    return false;
  }
  zm_dump_codec(audio_out_ctx);

  audio_out_stream->time_base = (AVRational) {1, audio_out_ctx->sample_rate};
  if ((ret = avcodec_parameters_from_context(audio_out_stream->codecpar, audio_out_ctx)) < 0) {
    Error("Could not initialize stream parameters");
    return false;
  }
  zm_dump_codecpar(audio_out_stream->codecpar);

  Debug(3,
        "Time bases: AUDIO in stream (%d/%d) in codec: (%d/%d) out "
        "stream: (%d/%d) out codec (%d/%d)",
        audio_in_stream->time_base.num, audio_in_stream->time_base.den,
        audio_in_ctx->time_base.num, audio_in_ctx->time_base.den,
        audio_out_stream->time_base.num, audio_out_stream->time_base.den,
        audio_out_ctx->time_base.num, audio_out_ctx->time_base.den);

#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
  Debug(1,
        "Audio in bit_rate (%" AV_PACKET_DURATION_FMT ") sample_rate(%d) channels(%d) fmt(%d) frame_size(%d)",
        audio_in_ctx->bit_rate, audio_in_ctx->sample_rate,
        audio_in_ctx->ch_layout.nb_channels,
        audio_in_ctx->sample_fmt,
        audio_in_ctx->frame_size);
#else
  Debug(1,
        "Audio in bit_rate (%" AV_PACKET_DURATION_FMT ") sample_rate(%d) channels(%d) fmt(%d) frame_size(%d)",
        audio_in_ctx->bit_rate, audio_in_ctx->sample_rate,
        audio_in_ctx->channels,
        audio_in_ctx->sample_fmt,
        audio_in_ctx->frame_size);
#endif
  Debug(1,
        "Audio out context bit_rate (%" AV_PACKET_DURATION_FMT ") sample_rate(%d) channels(%d) fmt(%d) frame_size(%d)",
        audio_out_ctx->bit_rate, audio_out_ctx->sample_rate,
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
        audio_out_ctx->ch_layout.nb_channels,
#else
        audio_out_ctx->channels,
#endif
        audio_out_ctx->sample_fmt,
        audio_out_ctx->frame_size);

  Debug(1,
        "Audio out stream bit_rate (%" PRIi64 ") sample_rate(%d) channels(%d) fmt(%d) frame_size(%d)",
        audio_out_stream->codecpar->bit_rate, audio_out_stream->codecpar->sample_rate,
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
        audio_out_stream->codecpar->ch_layout.nb_channels,
#else
        audio_out_stream->codecpar->channels,
#endif
        audio_out_stream->codecpar->format,
        audio_out_stream->codecpar->frame_size);

  /** Create a new frame to store the audio samples. */
  if (!in_frame) {
    if (!(in_frame = av_frame_ptr{av_frame_alloc()})) {
      Error("Could not allocate in frame");
      return false;
    }
  }

  /** Create a new frame to store the audio samples. */
  if (!(out_frame = av_frame_ptr{av_frame_alloc()})) {
    Error("Could not allocate out frame");
    return false;
  }
  out_frame->sample_rate = audio_out_ctx->sample_rate;

  if (!(fifo = av_audio_fifo_alloc(
                 audio_out_ctx->sample_fmt,
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
                 audio_out_ctx->ch_layout.nb_channels
#else
                 audio_out_ctx->channels
#endif
                 , 1))) {
    Error("Could not allocate FIFO");
    return false;
  }
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
  if ((ret = swr_alloc_set_opts2(&resample_ctx,
                                 &audio_out_ctx->ch_layout,
                                 audio_out_ctx->sample_fmt,
                                 audio_out_ctx->sample_rate,
                                 &audio_in_ctx->ch_layout,
                                 audio_in_ctx->sample_fmt,
                                 audio_in_ctx->sample_rate,
                                 0, nullptr)) < 0) {
    Error("Could not allocate resample context");
    return false;
  }
#else
  resample_ctx = swr_alloc_set_opts(nullptr,
                                    audio_out_ctx->channel_layout,
                                    audio_out_ctx->sample_fmt,
                                    audio_out_ctx->sample_rate,
                                    audio_in_ctx->channel_layout,
                                    audio_in_ctx->sample_fmt,
                                    audio_in_ctx->sample_rate,
                                    0, nullptr);
  if (!resample_ctx) {
    Error("Could not allocate resample context");
    return false;
  }
#endif
  if ((ret = swr_init(resample_ctx)) < 0) {
    Error("Could not open resampler %d", ret);
    swr_free(&resample_ctx);
    return false;
  }
  Debug(1,"Success setting up SWRESAMPLE");

  out_frame->nb_samples = audio_out_ctx->frame_size;
  out_frame->format = audio_out_ctx->sample_fmt;
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
  out_frame->ch_layout = audio_out_ctx->ch_layout,
#else
  out_frame->channels = audio_out_ctx->channels;
  out_frame->channel_layout = audio_out_ctx->channel_layout;
#endif
             out_frame->sample_rate = audio_out_ctx->sample_rate;

  // The codec gives us the frame size, in samples, we calculate the size of the
  // samples buffer in bytes
  unsigned int audioSampleBuffer_size = av_samples_get_buffer_size(
                                          nullptr,
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
                                          audio_out_ctx->ch_layout.nb_channels,
#else
                                          audio_out_ctx->channels,
#endif
                                          audio_out_ctx->frame_size,
                                          audio_out_ctx->sample_fmt, 0);
  converted_in_samples = reinterpret_cast<uint8_t *>(av_malloc(audioSampleBuffer_size));

  if (!converted_in_samples) {
    Error("Could not allocate converted in sample pointers");
    return false;
  } else {
    Debug(2, "Frame Size %d, sample buffer size %d", audio_out_ctx->frame_size, audioSampleBuffer_size);
  }

  // Setup the data pointers in the AVFrame
  if (avcodec_fill_audio_frame(
        out_frame.get(),
#if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0)
        audio_out_ctx->ch_layout.nb_channels,
#else
        audio_out_ctx->channels,
#endif
        audio_out_ctx->sample_fmt,
        (const uint8_t *)converted_in_samples,
        audioSampleBuffer_size, 0) < 0) {
    Error("Could not allocate converted in sample pointers");
    return false;
  }

  return true;
}  // end bool VideoStore::setup_resampler()

int VideoStore::writePacket(const std::shared_ptr<ZMPacket> zm_pkt) {
  int stream_index;
  if (zm_pkt->codec_type == AVMEDIA_TYPE_VIDEO) {
    stream_index = video_out_stream->index;
  } else if (zm_pkt->codec_type == AVMEDIA_TYPE_AUDIO) {
    if (!audio_out_stream) {
      Debug(1, "Called writeAudioFramePacket when no audio_out_stream");
      return 0;
      // FIXME -ve return codes do not free packet in ffmpeg_camera at the moment
    }
    stream_index = audio_out_stream->index;
  } else {
    Error("Unknown stream type in packet (%d)", zm_pkt->codec_type);
    return -1;
  }
  Debug(1, "Queue for %d", stream_index);
  auto &queue = reorder_queues[stream_index];
  Debug(1, "Queue size for %d is %zu", stream_index, queue.size());

  AVPacket *av_pkt = zm_pkt->packet.get();
  // queue the packet
  bool have_out_of_order = false;
  auto rit = queue.rbegin();
  // Find the previous packet for the stream, and check dts
  // Skip reordering when incoming dts is undefined — can't compare meaningfully
  if (av_pkt->dts != AV_NOPTS_VALUE) {
    while (rit != queue.rend()) {
      AVPacket *p = ((*rit)->packet).get();
      if (p->dts == AV_NOPTS_VALUE || p->dts <= av_pkt->dts) {
        Debug(1, "Found in order packet");
        // packets are in order, everything is fine
        break;
      } else {
        have_out_of_order = true;
      }
      rit++;
    }  // end while
  }

  if (have_out_of_order) {
    if (rit == queue.rend()) {
      Debug(1, "Unable to re-order packet, packet dts is %" PRId64, av_pkt->dts);
    } else {
      AVPacket *p = ((*rit)->packet).get();
      Debug(1, "Found out of order packet, inserting after %" PRId64, p->dts);
    }
    queue.insert(rit.base(), zm_pkt);
  } else {
    queue.push_back(zm_pkt);
    Debug(1, "Pushing on queue %d, size is %zu", stream_index, queue.size());
  }

  if (queue.size() > reorder_queue_size) {
    auto pkt = queue.front();
    queue.pop_front();
    if (pkt->codec_type == AVMEDIA_TYPE_VIDEO) {
      return writeVideoFramePacket(pkt);
    } else if (pkt->codec_type == AVMEDIA_TYPE_AUDIO) {
      return writeAudioFramePacket(pkt);
    }
  }
  return 0;
}

int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> zm_packet) {
  av_packet_guard pkt_guard;

  frame_count += 1;

  // if we have to transcode
  if (monitor->GetOptVideoWriter() == Monitor::ENCODE) {
    Debug(3, "Have encoding video frame count (%d)", frame_count);

    av_frame_ptr frame(av_frame_alloc());

    if (zm_packet->out_frame) {
      av_frame_ref(frame.get(), zm_packet->out_frame.get());
    } else {
      Debug(3, "Have no out frame. codec is %s sw_pf %d %s hw_pf %d %s %dx%d",
            chosen_codec_data->codec_name,
            chosen_codec_data->sw_pix_fmt, av_get_pix_fmt_name(chosen_codec_data->sw_pix_fmt),
            chosen_codec_data->hw_pix_fmt, av_get_pix_fmt_name(chosen_codec_data->hw_pix_fmt),
            video_out_ctx->width, video_out_ctx->height
           );

      if (zm_packet->image) {
        Debug(2, "Have an image, convert it");
        //Go straight to out frame
        if (
            zm_packet->image->Width() == static_cast<unsigned int>(video_out_ctx->width)
            and
            zm_packet->image->Height() == static_cast<unsigned int>(video_out_ctx->height)
            and
            zm_packet->image->AVPixFormat() == chosen_codec_data->sw_pix_fmt
           ) {
          zm_packet->image->PopulateFrame(frame.get());
          Debug(2, "populating");
        } else {
          Debug(2, "converting");
          zm_packet->get_out_frame(video_out_ctx->width, video_out_ctx->height, chosen_codec_data->sw_pix_fmt);
          av_frame_ref(frame.get(), zm_packet->out_frame.get());

          // The destination is out_frame's buffer, which get_out_frame laid
          // out at alignment (width % 32 ? 1 : 32) — mirror that choice here
          // so sws writes the layout the encoder will read via
          // out_frame->linesize.
          swscale.Convert(
              zm_packet->image,
              frame->buf[0]->data,
              zm_packet->codec_imgsize,
              zm_packet->image->AVPixFormat(),
              chosen_codec_data->sw_pix_fmt,
              video_out_ctx->width,
              video_out_ctx->height,
              (video_out_ctx->width % 32) ? 1 : 32
              );
        }
      } else if (zm_packet->in_frame) {
        if (
            zm_packet->in_frame->width == video_out_ctx->width
            and
            zm_packet->in_frame->height == video_out_ctx->height
            and
            static_cast<AVPixelFormat>(zm_packet->in_frame->format) == chosen_codec_data->sw_pix_fmt
           ) {
          av_frame_ref(frame.get(), zm_packet->in_frame.get());
        } else {
          zm_packet->get_out_frame(video_out_ctx->width, video_out_ctx->height, chosen_codec_data->sw_pix_fmt);
          av_frame_ref(frame.get(), zm_packet->out_frame.get());
          // Have in_frame.... may need to convert it to out_frame
          swscale.Convert(zm_packet->in_frame.get(), frame.get());
          av_frame_copy_props(frame.get(), zm_packet->in_frame.get());
        }
      } else {
        Error("Have neither in_frame or image in packet %d!", zm_packet->image_index);
        return 0;
      } // end if no in_frame
    } // end if no out_frame

#if HAVE_LIBAVUTIL_HWCONTEXT_H
    if (video_out_ctx->hw_frames_ctx) {
      int ret;
      av_frame_ptr hw_frame = av_frame_ptr{av_frame_alloc()};
      if (!hw_frame) {
        return AVERROR(ENOMEM);
      }
      if ((ret = av_hwframe_get_buffer(video_out_ctx->hw_frames_ctx, hw_frame.get(), 0)) < 0) {
        Error("Error code: %s", av_err2str(ret));
        return ret;
      }
      if (!hw_frame->hw_frames_ctx) {
        Error("Outof ram!");
        return 0;
      }
      if ((ret = av_hwframe_transfer_data(hw_frame.get(), frame.get(), 0)) < 0) {
        Error("Error while transferring frame data to surface: %s.", av_err2str(ret));
        return ret;
      }

      frame = std::move(hw_frame);
    }  // end if hwaccel
#endif

    if (1 or (monitor->WallClockTimestamps() or !(zm_packet->in_frame || zm_packet->packet))) {
      if (video_first_pts == AV_NOPTS_VALUE) {
        video_first_pts = static_cast<int64>(std::chrono::duration_cast<Microseconds>(zm_packet->timestamp.time_since_epoch()).count());
        Debug(2, "No video_first_pts, set to (%" PRId64 ") secs(%.2f)",
            video_first_pts,
            FPSeconds(zm_packet->timestamp.time_since_epoch()).count());

        frame->pts = 0;
      } else {
        Microseconds useconds = std::chrono::duration_cast<Microseconds>(
            zm_packet->timestamp - SystemTimePoint(Microseconds(video_first_pts)));
        frame->pts = av_rescale_q(useconds.count(), AV_TIME_BASE_Q, video_out_ctx->time_base);
        Debug(2,
            "Setting pts for frame(%d) to (%" PRId64 ") from (zm_packet->timestamp(%" PRIi64 " - first %" PRId64 " us %" PRId64 " ) @ %d/%d",
            frame_count,
            frame->pts,
            static_cast<int64>(std::chrono::duration_cast<Microseconds>(zm_packet->timestamp.time_since_epoch()).count()),
            video_first_pts,
            static_cast<int64>(std::chrono::duration_cast<Microseconds>(useconds).count()),
            video_out_ctx->time_base.num,
            video_out_ctx->time_base.den);
      }
    } else {
      if (zm_packet->in_frame) {
        if (video_first_pts == AV_NOPTS_VALUE) {
          video_first_pts = zm_packet->in_frame->pts;
          Debug(2, "No video_first_pts, set to (%" PRId64 ")", video_first_pts);
          frame->pts = 0;
        } else {
          frame->pts = av_rescale_q(zm_packet->in_frame->pts - video_first_pts, video_in_stream->time_base, video_out_ctx->time_base);
          Debug(2,
              "Setting pts for frame(%d) to (%" PRId64 ") from (zm_packet->in_frame(%" PRIi64 " - first %" PRId64 " ) @ %d/%d=>%d/%d",
              frame_count,
              frame->pts,
              zm_packet->in_frame->pts,
              video_first_pts,
              video_in_stream->time_base.num,
              video_in_stream->time_base.den,
              video_out_ctx->time_base.num,
              video_out_ctx->time_base.den);
        }
      } else if (zm_packet->packet) {
        if (video_first_pts == AV_NOPTS_VALUE) {
          video_first_pts = zm_packet->packet->pts;
          Debug(2, "No video_first_pts, set to (%" PRId64 ")", video_first_pts);
          frame->pts = 0;
        } else {
          frame->pts = av_rescale_q(zm_packet->packet->pts-video_first_pts, video_in_stream->time_base, video_out_ctx->time_base);
          Debug(2,
              "Setting pts for frame(%d) to (%" PRId64 ") from (zm_packet->in_frame(%" PRIi64 " - first %" PRId64 " ) @ %d/%d=>%d/%d",
              frame_count,
              frame->pts,
              zm_packet->packet->pts,
              video_first_pts,
              video_in_stream->time_base.num,
              video_in_stream->time_base.den,
              video_out_ctx->time_base.num,
              video_out_ctx->time_base.den);
        }
      } else {
        Error("No pts from any source.");
      }
    }

    // Some hwaccel codecs may get full if we only receive one pkt for one frame

    do {
      zm_dump_frame(frame.get(), "sending frame");
      int ret = avcodec_send_frame(video_out_ctx, frame.get());
      if (ret < 0) {
        if (AVERROR(EAGAIN) != ret) {
          Error("Could not send frame (error '%s')", av_make_error_string(ret).c_str());
          return ret;
        } else {
          Debug(3, "Got EAGAIN");
        }
      } else {
        video_encoded = true;
        break;
      }
    } while (!zm_terminate);

    // EAGAIN can mean that we need to send another frame, so don't loop on it.
    while (!zm_terminate) {
      int ret = avcodec_receive_packet(video_out_ctx, opkt.get());
      if (ret < 0) {
        if (ret != AVERROR(EAGAIN)) {
          Error("Could not receive packet (error %d = %s)", ret, av_make_error_string(ret).c_str());
        } else {
          Debug(1, "Could not receive packet (error %d = %s)", ret, av_make_error_string(ret).c_str());
        }
        return ret;
      }
      pkt_guard.acquire(opkt);
      ZM_DUMP_PACKET(opkt, "packet returned by codec");

      // Need to adjust pts/dts values from codec time to stream time
      if (opkt->pts != AV_NOPTS_VALUE)
        opkt->pts = av_rescale_q(opkt->pts, video_out_ctx->time_base, video_out_stream->time_base);
      if (opkt->dts != AV_NOPTS_VALUE)// and opkt->dts > 0)
        opkt->dts = av_rescale_q(opkt->dts, video_out_ctx->time_base, video_out_stream->time_base);
      Debug(1, "Timebase conversions using %d/%d -> %d/%d",
            video_out_ctx->time_base.num,
            video_out_ctx->time_base.den,
            video_out_stream->time_base.num,
            video_out_stream->time_base.den);
      ZM_DUMP_PACKET(opkt, "packet returned by codec after timebase conversions");

      if (video_last_pts != AV_NOPTS_VALUE) {
        opkt->duration = opkt->pts - video_last_pts;
        Debug(1, "Duration %" PRId64 " from pts %" PRId64 " - last %" PRId64, opkt->duration, opkt->pts, video_last_pts);
        if (opkt->duration < 0) opkt->duration = 0;
      }
      video_last_pts = opkt->pts;
      write_packet(opkt.get(), video_out_stream);
    } // end while receive_packet
  } else { // Passthrough
    AVPacket *ipkt = zm_packet->packet.get();
    ZM_DUMP_STREAM_PACKET(video_in_stream, ipkt, "Doing passthrough, just copy packet");
    // Just copy it because the codec is the same
    av_packet_ref(opkt.get(), ipkt);
    pkt_guard.acquire(opkt);

    if (monitor->WallClockTimestamps()) {
      int64_t ts = static_cast<int64>(std::chrono::duration_cast<Microseconds>(zm_packet->timestamp.time_since_epoch()).count());
      if (video_first_dts == AV_NOPTS_VALUE) {
        Debug(2, "Starting video first_dts will become %" PRId64, ts);
        video_first_dts = ts;
      }
      opkt->pts = opkt->dts = av_rescale_q(ts-video_first_dts, AV_TIME_BASE_Q, video_out_stream->time_base);

      Debug(2, "dts from timestamp, set to (%" PRId64 ") secs(%.2f), minus first_dts %" PRId64 " = %" PRId64,
          ts,
          FPSeconds(zm_packet->timestamp.time_since_epoch()).count(),
          video_first_dts, ts - video_first_dts);
    } else {
      if (ipkt->dts != AV_NOPTS_VALUE) {
        if (video_first_dts == AV_NOPTS_VALUE) {
          Debug(2, "Starting video first_dts will become %" PRId64, ipkt->dts);
          video_first_dts = ipkt->dts;
        }
        opkt->dts = ipkt->dts - video_first_dts;
      }
      if (ipkt->pts != AV_NOPTS_VALUE) {
        opkt->pts = ipkt->pts - video_first_dts;
      }
      if ((ipkt->pts != AV_NOPTS_VALUE) and (ipkt->dts != AV_NOPTS_VALUE)) {
        av_packet_rescale_ts(opkt.get(), video_in_stream->time_base, video_out_stream->time_base);
      } else if ((ipkt->pts == AV_NOPTS_VALUE) and (ipkt->dts == AV_NOPTS_VALUE)) {
        // Both undefined — use last_dts+1 as minimal monotonic increment
        if (last_dts[video_out_stream->index] != AV_NOPTS_VALUE) {
          opkt->dts = last_dts[video_out_stream->index] + 1;
        } else {
          opkt->dts = 0;
        }
        opkt->pts = opkt->dts;
        Debug(2, "No pts/dts, synthesized dts %" PRId64 " from last_dts", opkt->dts);
      }
    }  // end if wallclock or not
    write_packet(opkt.get(), video_out_stream);
  }  // end if codec matches

  return 1;
}  // end int VideoStore::writeVideoFramePacket( AVPacket *ipkt )

int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> zm_packet) {
  AVPacket *ipkt = zm_packet->packet.get();
  ZM_DUMP_STREAM_PACKET(audio_in_stream, ipkt, "input packet");

  if (monitor->WallClockTimestamps()) {
    int64_t ts = static_cast<int64>(std::chrono::duration_cast<Microseconds>(zm_packet->timestamp.time_since_epoch()).count());
    ipkt->pts = ipkt->dts = av_rescale_q(ts, AV_TIME_BASE_Q, audio_in_stream->time_base);

    Debug(2, "dts %" PRId64 " from timestamp %" PRId64 " secs(%.2f)",
        ipkt->dts, ts, FPSeconds(zm_packet->timestamp.time_since_epoch()).count());
  }

  if (audio_first_dts == AV_NOPTS_VALUE && ipkt->dts != AV_NOPTS_VALUE) {
    audio_first_dts = ipkt->dts;
    audio_next_pts = audio_out_ctx->frame_size;
    Debug(3, "audio first_dts to %" PRId64, audio_first_dts);
  }

  // Need to adjust pts before feeding to decoder.... should really copy the pkt instead of modifying it

  if (audio_out_codec) {
    // I wonder if we can get multiple frames per packet? Probably
    int ret = zm_send_packet_receive_frame(audio_in_ctx, in_frame.get(), *ipkt);
    if (ret < 0) {
      Debug(3, "failed to receive frame code: %d", ret);
      return 0;
    }
    zm_dump_frame(in_frame, "In frame from decode");

    AVFrame *input_frame = in_frame.get();

    while (zm_resample_audio(resample_ctx, input_frame, out_frame.get())) {
      //out_frame->pkt_duration = in_frame->pkt_duration; // resampling doesn't alter duration
      if (zm_add_samples_to_fifo(fifo, out_frame.get()) <= 0)
        break;

      // We put the samples into the fifo so we are basically resetting the frame
      out_frame->nb_samples = audio_out_ctx->frame_size;

      if (zm_get_samples_from_fifo(fifo, out_frame.get()) <= 0)
        break;

      out_frame->pts = audio_next_pts;
      audio_next_pts += out_frame->nb_samples;

      zm_dump_frame(out_frame, "Out frame after resample");

      if (zm_send_frame_receive_packet(audio_out_ctx, out_frame.get(), *opkt) <= 0)
        break;

      // Scale the PTS of the outgoing packet to be the correct time base
      av_packet_rescale_ts(opkt.get(),
                           audio_out_ctx->time_base,
                           audio_out_stream->time_base);

      write_packet(opkt.get(), audio_out_stream);
      zm_av_packet_unref(opkt.get());

      if (zm_resample_get_delay(resample_ctx, out_frame->sample_rate) < out_frame->nb_samples)
        break;
      // This will send a null frame, emptying out the resample buffer
      input_frame = nullptr;
    }  // end while there is data in the resampler
  } else {
    opkt->data = ipkt->data;
    opkt->size = ipkt->size;
    opkt->flags = ipkt->flags;
    opkt->duration = ipkt->duration;
    if (audio_first_dts != AV_NOPTS_VALUE) {
      opkt->pts = (ipkt->pts != AV_NOPTS_VALUE) ? ipkt->pts - audio_first_dts : AV_NOPTS_VALUE;
      opkt->dts = (ipkt->dts != AV_NOPTS_VALUE) ? ipkt->dts - audio_first_dts : AV_NOPTS_VALUE;
    } else {
      opkt->pts = ipkt->pts;
      opkt->dts = ipkt->dts;
    }

    ZM_DUMP_STREAM_PACKET(audio_in_stream, opkt, "after pts adjustment");
    av_packet_rescale_ts(opkt.get(), audio_in_stream->time_base, audio_out_stream->time_base);
    ZM_DUMP_STREAM_PACKET(audio_out_stream, opkt, "after stream pts adjustment");
    write_packet(opkt.get(), audio_out_stream);

    zm_av_packet_unref(opkt.get());
  }  // end if encoding or copying

  return 0;
}  // end int VideoStore::writeAudioFramePacket(AVPacket *ipkt)

int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) {
  pkt->pos = -1;
  pkt->stream_index = stream->index;
  ZM_DUMP_PACKET(pkt, "packet in write_packet");

  if (pkt->dts == AV_NOPTS_VALUE) {
    if (last_dts[stream->index] != AV_NOPTS_VALUE) {
      pkt->dts = last_dts[stream->index] + 1;
      Debug(1, "undef dts, synthesized %" PRId64 " from last_dts %" PRId64 " + 1",
            pkt->dts, last_dts[stream->index]);
    } else {
      pkt->dts = 0;
      Debug(1, "undef dts and no last_dts, setting to 0");
    }
  } else {
    if (last_dts[stream->index] != AV_NOPTS_VALUE) {
      if (pkt->dts < last_dts[stream->index]) {
        Warning("non increasing dts, fixing. our dts %" PRId64 " stream %d last_dts %" PRId64 " last_duration %" PRId64 " (%.3f seconds back). reorder_queue_size=%zu",
            pkt->dts, stream->index, last_dts[stream->index], last_duration[stream->index],
            (last_dts[stream->index] - pkt->dts) * av_q2d(stream->time_base), reorder_queue_size);
        pkt->dts = last_dts[stream->index]+last_duration[stream->index];
        if (pkt->dts > pkt->pts) pkt->pts = pkt->dts; // Do it here to avoid warning below
      } else if (pkt->dts == last_dts[stream->index]) {
        // Commonly seen
        Debug(1, "non increasing dts, fixing. our dts %" PRId64 " stream %d last_dts %" PRId64 " stream %d. reorder_queue_size=%zu",
            pkt->dts, stream->index, last_dts[stream->index], stream->index, reorder_queue_size);
        // dts MUST monotonically increase, so add 1 which should be a small enough time difference to not matter.
        pkt->dts = last_dts[stream->index]+1;
        if (pkt->dts > pkt->pts) pkt->pts = pkt->dts; // Do it here to avoid warning below
      }
    }
  }
  // Always update tracking now that dts has a valid value
  next_dts[stream->index] = pkt->dts + pkt->duration;
  last_dts[stream->index] = pkt->dts;
  last_duration[stream->index] = pkt->duration;

  if (pkt->pts == AV_NOPTS_VALUE) {
    pkt->pts = pkt->dts;
  } else if (pkt->dts > pkt->pts) {
    Warning("pkt.dts(%" PRId64 ") must be <= pkt.pts(%" PRId64 ")."
            "Decompression must happen before presentation.",
            pkt->dts, pkt->pts);
    /*    Debug(1,
              "pkt.dts(%" PRId64 ") must be <= pkt.pts(%" PRId64 ")."
              "Decompression must happen before presentation.",
              pkt->dts, pkt->pts);*/
    pkt->pts = pkt->dts;
  }

  ZM_DUMP_STREAM_PACKET(stream, pkt, "finished pkt");
  Debug(3, "next_dts for stream %d has become %" PRId64 " last_dts %" PRId64,
        stream->index, next_dts[stream->index], last_dts[stream->index]);

  bool is_video_keyframe = (stream == video_out_stream) && (pkt->flags & AV_PKT_FLAG_KEY);
  // Snapshot the keyframe's dts before the write call may modify the packet.
  int64_t this_keyframe_dts = is_video_keyframe ? pkt->dts : AV_NOPTS_VALUE;

  int ret = av_interleaved_write_frame(oc, pkt);
  if (ret != 0) {
    Error("Error writing packet: %s", av_make_error_string(ret).c_str());
  } else {
    Debug(4, "Success writing packet");
  }

  // HLS fragment tracking: with movflags=frag_keyframe, the muxer flushes the
  // previous fragment to disk inside av_interleaved_write_frame() when a new
  // keyframe arrives. So the position *after* this call equals the end of the
  // just-flushed fragment, and last_fragment_offset_/_dts_ describe that
  // fragment. Record it, then move tracking to the new fragment.
  if (is_video_keyframe && oc && oc->pb) {
    avio_flush(oc->pb);
    int64_t pos_after = avio_tell(oc->pb);

    if (last_fragment_start_dts_ != AV_NOPTS_VALUE && pos_after > last_fragment_offset_) {
      int64_t frag_size = pos_after - last_fragment_offset_;
      double duration = 0;
      if (video_out_stream->time_base.den > 0) {
        duration = static_cast<double>(this_keyframe_dts - last_fragment_start_dts_)
                   * video_out_stream->time_base.num
                   / video_out_stream->time_base.den;
      }
      if (duration > 0 && frag_size > 0) {
        fragments_.push_back({last_fragment_offset_, frag_size, duration});
        Debug(1, "HLS fragment %zu: offset=%" PRId64 " size=%" PRId64 " duration=%.3f",
              fragments_.size() - 1, last_fragment_offset_, frag_size, duration);
      }
    }
    last_fragment_offset_ = pos_after;
    last_fragment_start_dts_ = this_keyframe_dts;
  }

  return ret;
}  // end int VideoStore::write_packet(AVPacket *pkt, AVStream *stream)

void VideoStore::finalize() {
  if (finalized_) return;
  finalized_ = true;

  if (!oc || !oc->pb) return;

  // Drain reorder queues before writing the trailer — the destructor would
  // otherwise try to run these packets through av_interleaved_write_frame()
  // after we've already closed oc->pb here.
  for (auto &n : reorder_queues) {
    auto &queue = n.second;
    Debug(1, "Queue for %d length is %zu", n.first, queue.size());
    while (!queue.empty()) {
      auto pkt = queue.front();
      queue.pop_front();
      if (pkt->codec_type == AVMEDIA_TYPE_VIDEO) {
        writeVideoFramePacket(pkt);
      } else if (pkt->codec_type == AVMEDIA_TYPE_AUDIO) {
        writeAudioFramePacket(pkt);
      }
    }
  }

  flush_codecs();

  Debug(4, "Flushing interleaved queues");
  av_interleaved_write_frame(oc, nullptr);

  Debug(1, "Writing trailer");
  int rc = av_write_trailer(oc);
  if (rc < 0) {
    Error("Error writing trailer %s", av_err2str(rc));
  } else {
    Debug(3, "Success Writing trailer");
  }

  // After av_write_trailer, the file contains init+fragments_1..N + mfra trailer.
  // Capture the on-disk length so we can size the final fragment.
  avio_flush(oc->pb);
  int64_t file_size = avio_tell(oc->pb);

  // Close the output file before reading it back to inspect the mfra box.
  if (!(out_format->flags & AVFMT_NOFILE)) {
    Debug(4, "Closing");
    if ((rc = avio_close(oc->pb)) < 0) {
      Error("Error closing avio %s", av_err2str(rc));
    }
  }
  oc->pb = nullptr;

  // The MOV muxer writes an mfra (Movie Fragment Random Access) box at the end
  // of the file when fragmentation is on, so the final fragment's mdat ends
  // where the mfra begins.
  int64_t fragment_n_end = file_size;
  if (!filename.empty()) {
    const int fd = ::open(filename.c_str(), O_RDONLY | O_CLOEXEC);
    if (fd >= 0) {
      fragment_n_end = zm_mp4::media_end(fd, file_size);
      close(fd);
      if (fragment_n_end != file_size) {
        Debug(1, "mfra trailer is %" PRId64 " bytes; final fragment ends at %" PRId64,
              file_size - fragment_n_end, fragment_n_end);
      }
    }
  }

  // Record the final fragment that no subsequent keyframe was around to record.
  if (last_fragment_start_dts_ != AV_NOPTS_VALUE
      && fragment_n_end > last_fragment_offset_
      && video_out_stream && video_out_stream->time_base.den > 0
      && last_dts.count(video_out_stream->index)
      && last_dts[video_out_stream->index] != AV_NOPTS_VALUE) {
    int64_t frag_size = fragment_n_end - last_fragment_offset_;
    double duration = static_cast<double>(
        last_dts[video_out_stream->index]
        + last_duration[video_out_stream->index]
        - last_fragment_start_dts_)
        * video_out_stream->time_base.num
        / video_out_stream->time_base.den;
    if (duration > 0 && frag_size > 0) {
      fragments_.push_back({last_fragment_offset_, frag_size, duration});
      Debug(1, "HLS final fragment: offset=%" PRId64 " size=%" PRId64 " duration=%.3f",
            last_fragment_offset_, frag_size, duration);
    }
  }

  // Now that every fragment is on disk, fill the region open() reserved with
  // an index of them. Without it a player has to read the whole file before
  // it can show the first frame. Nothing here can lose a recording: on any
  // doubt the region stays the `free` box it already is, and the file is
  // exactly what ZoneMinder wrote before.
  if (sidx_region_offset_ >= 0 && !filename.empty()) {
    if (!zm_mp4::write_leading_sidx(filename, sidx_region_offset_, sidx_region_size_)) {
      Warning("Could not index %s; it will play, but a player must read it all "
              "before the first frame", filename.c_str());
    }
  }
}

int VideoStore::m3u8TargetDuration(const std::vector<Fragment> &frags) {
  double max_duration = 0;
  for (const auto &frag : frags) {
    if (frag.duration > max_duration) max_duration = frag.duration;
  }
  int target_duration = static_cast<int>(ceil(max_duration));
  return (target_duration < 1) ? 1 : target_duration;
}

std::string VideoStore::m3u8Header(int target_duration,
                                   const std::string &video_url,
                                   int64_t init_segment_end,
                                   bool is_complete) {
  return stringtf("#EXTM3U\n"
                  "#EXT-X-VERSION:7\n"
                  "#EXT-X-TARGETDURATION:%d\n"
                  "#EXT-X-MEDIA-SEQUENCE:0\n"
                  "#EXT-X-PLAYLIST-TYPE:%s\n"
                  "#EXT-X-MAP:URI=\"%s\",BYTERANGE=\"%" PRId64 "@0\"\n",
                  target_duration,
                  is_complete ? "VOD" : "EVENT",
                  video_url.c_str(),
                  init_segment_end);
}

std::string VideoStore::m3u8Fragment(const Fragment &frag, const std::string &video_url) {
  return stringtf("#EXTINF:%.3f,\n"
                  "#EXT-X-BYTERANGE:%" PRId64 "@%" PRId64 "\n"
                  "%s\n",
                  frag.duration, frag.size, frag.offset, video_url.c_str());
}

void VideoStore::writeM3U8(const std::string &m3u8_path, const std::string &video_url, bool is_complete) {
  if (fragments_.empty()) return;

  const int target_duration = m3u8TargetDuration(fragments_);

  // Rewriting the whole manifest for every new fragment costs O(fragments)
  // each time, so an event pays O(fragments^2) overall. That is free for the
  // ten minute events a section length produces, and ruinous for anything
  // longer: a monitor whose events stopped closing reached a 17MB manifest
  // being rewritten every 1.2s, which saturated the disk, starved the analysis
  // thread, and so prevented the very close that would have ended it.
  //
  // An EVENT playlist only ever gains fragments and never rewrites a line it
  // has already written, so the new ones can simply go on the end. Anything
  // that would change what is already there -- a different header, a different
  // url repeated on every line, or the closing ENDLIST -- falls back to the
  // full rewrite.
  bool can_append =
      !is_complete
      and (m3u8_fragments_written_ > 0)
      and (m3u8_fragments_written_ <= fragments_.size())
      and (target_duration == m3u8_target_duration_)
      and (init_segment_end_ == m3u8_init_segment_end_)
      and (video_url == m3u8_video_url_)
      and (m3u8_path == m3u8_path_);

  if (can_append) {
    // The offsets we remember only describe the file we left behind. If
    // anything else has replaced, truncated or removed it, start again rather
    // than append to something we cannot account for.
    struct stat st;
    const bool statted = (stat(m3u8_path.c_str(), &st) == 0);
    if (!statted or (st.st_size != m3u8_bytes_written_)) {
      Debug(1, "m3u8 %s is not the file we left (size %jd, expected %jd), rewriting",
            m3u8_path.c_str(),
            static_cast<intmax_t>(statted ? st.st_size : -1),
            static_cast<intmax_t>(m3u8_bytes_written_));
      can_append = false;
    } else if (m3u8_fragments_written_ == fragments_.size()) {
      return;  // nothing new to say
    }
  }

  FILE *fp = fopen(m3u8_path.c_str(), can_append ? "a" : "w");
  if (!fp) {
    Error("Failed to open %s for writing: %s", m3u8_path.c_str(), strerror(errno));
    m3u8_fragments_written_ = 0;
    return;
  }

  size_t first = 0;
  if (can_append) {
    first = m3u8_fragments_written_;
  } else {
    std::string header = m3u8Header(target_duration, video_url, init_segment_end_, is_complete);
    fwrite(header.c_str(), 1, header.size(), fp);
  }

  for (size_t i = first; i < fragments_.size(); i++) {
    std::string line = m3u8Fragment(fragments_[i], video_url);
    fwrite(line.c_str(), 1, line.size(), fp);
  }

  if (is_complete) {
    fprintf(fp, "#EXT-X-ENDLIST\n");
  }

  const bool ok = (fclose(fp) == 0);

  // Measure the file rather than trusting ftell, so the next call's guard is
  // comparing against what is really on disk. Anything unreadable leaves the
  // state zeroed, which forces a rewrite next time.
  struct stat after;
  if (ok and !is_complete and (stat(m3u8_path.c_str(), &after) == 0)) {
    m3u8_fragments_written_ = fragments_.size();
    m3u8_target_duration_ = target_duration;
    m3u8_init_segment_end_ = init_segment_end_;
    m3u8_bytes_written_ = after.st_size;
    m3u8_path_ = m3u8_path;
    m3u8_video_url_ = video_url;
  } else {
    // A completed manifest ends with ENDLIST; nothing may be appended after
    // it, so any later call has to rewrite.
    m3u8_fragments_written_ = 0;
    if (!ok) Error("Failed to write %s: %s", m3u8_path.c_str(), strerror(errno));
  }

  Debug(1, "%s m3u8 %s: %zu of %zu fragments (complete=%d)",
        can_append ? "Appended to" : "Wrote",
        m3u8_path.c_str(), fragments_.size() - first, fragments_.size(), is_complete);
}