Ctrl K
ComunidadGuías…

src/zm_ffmpeg_camera.cpp

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

Archivo original ↗Preguntar a KiBot
//
// ZoneMinder Ffmpeg Camera Class Implementation
// Copyright (C) 2001-2008 Philip Coombes
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#include "zm_ffmpeg_camera.h"

#include "zm_ffmpeg_input.h"
#include "zm_monitor.h"
#include "zm_packet.h"
#include "zm_signal.h"
#include "zm_utils.h"
#include "url.hpp"

#include <thread>
#include <vector>

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

TimePoint start_read_time;

FfmpegCamera::FfmpegCamera(
  const Monitor *monitor,
  const std::string &p_path,
  const std::string &p_second_path,
  const std::string &p_user,
  const std::string &p_pass,
  const std::string &p_method,
  const std::string &p_options,
  int p_width,
  int p_height,
  int p_colours,
  int p_brightness,
  int p_contrast,
  int p_hue,
  int p_colour,
  bool p_capture,
  bool p_record_audio) :
  Camera(
    monitor,
    FFMPEG_SRC,
    p_width,
    p_height,
    p_colours,
    ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours),
    p_brightness,
    p_contrast,
    p_hue,
    p_colour,
    p_capture,
    p_record_audio
  ),
  mPath(p_path),
  mSecondPath(p_second_path),
  mUser(p_user),
  mPass(p_pass),
  mMethod(p_method),
  mOptions(p_options),
  mSecondInput(nullptr),
  frameCount(0),
  mConvertContext(nullptr),
  error_count(0),
  stream_width(0),
  stream_height(0) {
  mMaskedPath = remove_authentication(mPath);
  mMaskedSecondPath = remove_authentication(mSecondPath);

  mLoop = false;
  mLoopVideoOffset = 0;
  mLoopAudioOffset = 0;
  mLoopVideoFrameDuration = 0;
  mLoopAudioFrameDuration = 0;

  mRealtime = false;
  mRealtimeAnchored = false;
  mRealtimeStartTS = 0;

  if ( capture ) {
    FFMPEGInit();
  }

#if HAVE_LIBAVUTIL_HWCONTEXT_H
#endif

  /* Has to be located inside the constructor so other components such as zma
   * will receive correct colours and subpixel order */
  if ( zm_is_rgb32(pixelFormat) ) {
    subpixelorder = ZM_SUBPIX_ORDER_RGBA;
    pixelFormat = AV_PIX_FMT_RGBA;
  } else if ( zm_is_rgb24(pixelFormat) ) {
    subpixelorder = ZM_SUBPIX_ORDER_RGB;
    pixelFormat = AV_PIX_FMT_RGB24;
  } else if ( pixelFormat == AV_PIX_FMT_GRAY8 ) {
    subpixelorder = ZM_SUBPIX_ORDER_NONE;
    pixelFormat = AV_PIX_FMT_GRAY8;
  } else {
    Panic("Unexpected pixel format %d (%s); legacy colours=%d subpixelorder=%d",
          pixelFormat, zm_get_pix_fmt_name(pixelFormat), colours, subpixelorder);
  }

  packet = av_packet_ptr{av_packet_alloc()};
}  // FfmpegCamera::FfmpegCamera

FfmpegCamera::~FfmpegCamera() {
  Close();

  FFMPEGDeInit();
}

int FfmpegCamera::PrimeCapture() {
  start_read_time = std::chrono::steady_clock::now();
  Close();
  mVideoStreamId = -1;
  mAudioStreamId = -1;
  Debug(1, "Priming capture from %s", mMaskedPath.c_str());

  return OpenFfmpeg();
}

int FfmpegCamera::PreCapture() {
  return 0;
}

bool FfmpegCamera::loopSeekToStart(AVFormatContext *ctx) {
  if (!(ctx->pb && ctx->pb->seekable)) {
    Debug(1, "loop: input is not seekable, cannot loop");
    return false;
  }

  // Bump the per-stream absolute offsets so the next packet continues one frame
  // after the last emitted dts. mLastVideoDTS/mLastAudioDTS already include the
  // offset that was in effect, and start_time is the raw container start, so
  // this accumulates correctly across repeated loops.
  // The audio stream lives in the secondary context only when one is in use;
  // otherwise both streams share mFormatContext.
  bool ctxHasVideo = (mVideoStream != nullptr) && (ctx == mFormatContext);
  bool ctxHasAudio = (mAudioStream != nullptr) &&
                     (ctx == (mSecondFormatContext ? mSecondFormatContext : mFormatContext));

  if (ctxHasVideo && (mLastVideoDTS != AV_NOPTS_VALUE)) {
    int64_t start = (mVideoStream->start_time != AV_NOPTS_VALUE) ? mVideoStream->start_time : 0;
    mLoopVideoOffset = mLastVideoDTS + mLoopVideoFrameDuration - start;
  }
  if (ctxHasAudio && (mLastAudioDTS != AV_NOPTS_VALUE)) {
    int64_t start = (mAudioStream->start_time != AV_NOPTS_VALUE) ? mAudioStream->start_time : 0;
    mLoopAudioOffset = mLastAudioDTS + mLoopAudioFrameDuration - start;
  }

  int ret = avformat_seek_file(ctx, -1, INT64_MIN, 0, INT64_MAX, AVSEEK_FLAG_BACKWARD);
  if (ret < 0) {
    ret = av_seek_frame(ctx, -1, 0, AVSEEK_FLAG_BACKWARD);
  }
  if (ret < 0) {
    Warning("loop: seek to start failed: %s", av_make_error_string(ret).c_str());
    return false;
  }
  Debug(1, "loop: sought to start; offsets video=%" PRId64 " audio=%" PRId64,
        mLoopVideoOffset, mLoopAudioOffset);
  return true;
}

int FfmpegCamera::readFrameWithLoop(AVFormatContext *ctx, AVPacket *pkt) {
  int ret = av_read_frame(ctx, pkt);
  if (ret >= 0) return ret;

  bool eof = (ret == AVERROR_EOF) || (ctx->pb && ctx->pb->eof_reached);
  if (eof && mLoop && loopSeekToStart(ctx)) {
    Info("loop: reached end of input, restarting from beginning");
    ret = av_read_frame(ctx, pkt);
  }
  return ret;
}

RealtimePaceDecision ComputeRealtimePace(
    int64_t ts_us, int64_t anchor_ts_us, Microseconds elapsed, Microseconds cap) {
  // Backward movement (e.g. a discontinuity the jump checks let through):
  // re-anchor so we never try to sleep on a negative delta.
  if (ts_us < anchor_ts_us)
    return {true, Microseconds(0)};

  Microseconds target_elapsed(ts_us - anchor_ts_us);
  Microseconds delay = target_elapsed - elapsed;

  // Already behind schedule: deliver immediately.
  if (delay <= Microseconds(0))
    return {false, Microseconds(0)};

  // A large gap usually means a timestamp discontinuity rather than a genuine
  // multi-second frame interval; re-anchor instead of stalling the capture.
  if (delay > cap)
    return {true, Microseconds(0)};

  return {false, delay};
}

void FfmpegCamera::paceRealtime(int64_t ts_us) {
  if (!mRealtime || (ts_us == AV_NOPTS_VALUE)) return;

  TimePoint now = std::chrono::steady_clock::now();

  // Anchor on the first packet.
  if (!mRealtimeAnchored) {
    mRealtimeStartWall = now;
    mRealtimeStartTS = ts_us;
    mRealtimeAnchored = true;
    return;
  }

  Microseconds elapsed = std::chrono::duration_cast<Microseconds>(now - mRealtimeStartWall);
  RealtimePaceDecision d = ComputeRealtimePace(ts_us, mRealtimeStartTS, elapsed, Seconds(10));

  if (d.reanchor) {
    Debug(1, "realtime: re-anchoring pacing at ts %" PRId64 "us", ts_us);
    mRealtimeStartWall = now;
    mRealtimeStartTS = ts_us;
    return;
  }

  if (d.sleep > Microseconds(0)) {
    Debug(4, "realtime: sleeping %" PRId64 "us to pace to stream rate",
          static_cast<int64_t>(d.sleep.count()));
    std::this_thread::sleep_for(d.sleep);
  }
}

int FfmpegCamera::Capture(std::shared_ptr<ZMPacket> &zm_packet) {
  if (!mIsPrimed) return -1;

  start_read_time = std::chrono::steady_clock::now();
  int ret;
  AVFormatContext *formatContextPtr;
  int64_t lastPTS = -1;
  // Unref the packet on every return, including the early ones for packets we
  // drop. Before FFmpeg 7, av_read_frame() resets the packet it is given
  // without unreferencing it, so a packet left holding data leaks its buffer.
  av_packet_guard pkt_guard{packet};

  if ( mSecondFormatContext and mAudioStream and
       (
         av_rescale_q(mLastAudioPTS, mAudioStream->time_base, AV_TIME_BASE_Q)
         <
         av_rescale_q(mLastVideoPTS, mVideoStream->time_base, AV_TIME_BASE_Q)
       ) ) {
    // if audio stream is behind video stream, then read from audio, otherwise video
    formatContextPtr = mSecondFormatContext;
    lastPTS = mLastAudioPTS;
    Debug(4, "Using audio input because audio PTS %" PRId64 " < video PTS %" PRId64,
          av_rescale_q(mLastAudioPTS, mAudioStream->time_base, AV_TIME_BASE_Q),
          av_rescale_q(mLastVideoPTS, mVideoStream->time_base, AV_TIME_BASE_Q)
         );
    if ((ret = readFrameWithLoop(formatContextPtr, packet.get())) < 0) {
      if (
        // Check if EOF.
        (ret == AVERROR_EOF || (formatContextPtr->pb && formatContextPtr->pb->eof_reached)) ||
        // Check for Connection failure.
        (ret == -110)
      ) {
        Info("Unable to read packet from stream %d: error %d \"%s\".",
             packet->stream_index, ret, av_make_error_string(ret).c_str());
      } else {
        logPrintf(Logger::ERROR + monitor->Importance(),
            "Unable to read packet from stream %d: error %d \"%s\".",
            packet->stream_index, ret, av_make_error_string(ret).c_str());
      }
      return -1;
    }
  } else {
    formatContextPtr = mFormatContext;
    Debug(4, "Using video input because %" PRId64 " >= %" PRId64,
          (mAudioStream?av_rescale_q(mLastAudioPTS, mAudioStream->time_base, AV_TIME_BASE_Q):0),
          av_rescale_q(mLastVideoPTS, mVideoStream->time_base, AV_TIME_BASE_Q)
         );

    if ((ret = readFrameWithLoop(formatContextPtr, packet.get())) < 0) {
      if (
        // Check if EOF.
        (ret == AVERROR_EOF || (formatContextPtr->pb && formatContextPtr->pb->eof_reached)) ||
        // Check for Connection failure.
        (ret == -110)
      ) {
        Info("Unable to read packet from stream %d: error %d \"%s\".",
             packet->stream_index, ret, av_make_error_string(ret).c_str());
      } else {
        logPrintf(Logger::ERROR + monitor->Importance(),
            "Unable to read packet from stream %d: error %d \"%s\".",
            packet->stream_index, ret, av_make_error_string(ret).c_str());
      }
      return -1;
    }

    if ( packet->stream_index == mVideoStreamId) {
      lastPTS = mLastVideoPTS;
    } else if (packet->stream_index == mAudioStreamId) {
      lastPTS = mLastAudioPTS;
    } else {
      Debug(1, "Have packet (%d) which isn't for video (%d) or audio stream (%d).", packet->stream_index, mVideoStreamId, mAudioStreamId);
      return 0;
    }
  }

  AVStream *stream = formatContextPtr->streams[packet->stream_index];

  // Loop mode: shift this packet's timestamps by the accumulated per-stream
  // offset so they stay monotonically increasing across loop restarts. Done
  // before the pts/dts backward-jump checks and before the packet is queued, so
  // every consumer (analysis, recording) sees continuous timestamps. Also keep
  // the per-stream frame duration up to date for spacing the next loop.
  if (mLoop) {
    int64_t off = 0;
    if (packet->stream_index == mVideoStreamId) {
      off = mLoopVideoOffset;
      if (packet->duration > 0) mLoopVideoFrameDuration = packet->duration;
    } else if (packet->stream_index == mAudioStreamId) {
      off = mLoopAudioOffset;
      if (packet->duration > 0) mLoopAudioFrameDuration = packet->duration;
    }
    if (off) {
      if (packet->pts != AV_NOPTS_VALUE) packet->pts += off;
      if (packet->dts != AV_NOPTS_VALUE) packet->dts += off;
    }
  }

  ZM_DUMP_STREAM_PACKET(stream, packet, "ffmpeg_camera in");

  if ((packet->pts != AV_NOPTS_VALUE) and (lastPTS >= 0)) {
    if (packet->pts < 0) {
      // 32-bit wrap around?
      Info("Suspected 32bit wraparound in input pts. %" PRId64, packet->pts);
      return -1;
    } else if (packet->pts - lastPTS < -10*stream->time_base.den) {
      if (!monitor->WallClockTimestamps()) {
        // -10 is for 10 seconds. Avigilon cameras seem to jump around by about 36 constantly
        double pts_time = static_cast<double>(av_rescale_q(packet->pts, stream->time_base, AV_TIME_BASE_Q)) / AV_TIME_BASE;
        double last_pts_time = static_cast<double>(av_rescale_q(lastPTS, stream->time_base, AV_TIME_BASE_Q)) / AV_TIME_BASE;
        logPrintf(Logger::WARNING + monitor->Importance(), "Stream pts jumped back in time too far. pts %.2f - last pts %.2f = %.2f > 10seconds",
                  pts_time, last_pts_time, pts_time - last_pts_time);
      }
      if (error_count > 5)
        return -1;
      error_count += 1;
      return 0;
    }
  }

  // Check DTS for significant backward jumps. Some cameras/encoders produce
  // non-monotonic DTS (B-frames, stream restarts) that PTS checks won't catch.
  if (packet->dts != AV_NOPTS_VALUE) {
    int64_t lastDTS = (packet->stream_index == mVideoStreamId) ? mLastVideoDTS : mLastAudioDTS;
    if (lastDTS != AV_NOPTS_VALUE) {
      int64_t dts_delta = packet->dts - lastDTS;
      if (dts_delta < -10*stream->time_base.den) {
        double dts_time = static_cast<double>(av_rescale_q(packet->dts, stream->time_base, AV_TIME_BASE_Q)) / AV_TIME_BASE;
        double last_dts_time = static_cast<double>(av_rescale_q(lastDTS, stream->time_base, AV_TIME_BASE_Q)) / AV_TIME_BASE;
        logPrintf(Logger::WARNING + monitor->Importance(),
            "Stream dts jumped back in time too far. dts %.2f - last dts %.2f = %.2f > 10seconds stream %d",
            dts_time, last_dts_time, dts_time - last_dts_time, packet->stream_index);
        if (error_count > 5)
          return -1;
        error_count += 1;
        return 0;
      }
    }
  }

  // Real-time pacing: throttle delivery to the stream's native rate. Use dts
  // (monotonic in read order) when available, otherwise pts. Sleep happens here,
  // after all the drop/error filters above, so only packets we actually deliver
  // advance the schedule.
  if (mRealtime) {
    int64_t pace_ts = (packet->dts != AV_NOPTS_VALUE) ? packet->dts : packet->pts;
    if (pace_ts != AV_NOPTS_VALUE)
      paceRealtime(av_rescale_q(pace_ts, stream->time_base, AV_TIME_BASE_Q));
  }

  zm_packet->codec_type = stream->codecpar->codec_type;

  bytes += packet->size;
  zm_packet->set_packet(packet.get());
  zm_packet->stream = stream;
  zm_packet->pts = av_rescale_q(packet->pts, stream->time_base, AV_TIME_BASE_Q);
  if (packet->pts != AV_NOPTS_VALUE) {
    if (stream == mVideoStream) {
      if (mFirstVideoPTS == AV_NOPTS_VALUE)
        mFirstVideoPTS = packet->pts;

      mLastVideoPTS = packet->pts - mFirstVideoPTS;
    } else if (stream == mAudioStream) {
      if (mFirstAudioPTS == AV_NOPTS_VALUE)
        mFirstAudioPTS = packet->pts;

      mLastAudioPTS = packet->pts - mFirstAudioPTS;
    }
  }
  if (packet->dts != AV_NOPTS_VALUE) {
    if (packet->stream_index == mVideoStreamId)
      mLastVideoDTS = packet->dts;
    else if (packet->stream_index == mAudioStreamId)
      mLastAudioDTS = packet->dts;
  }

  return 1;
} // FfmpegCamera::Capture

int FfmpegCamera::PostCapture() {
  // Nothing to do here
  return 0;
}

int FfmpegCamera::OpenFfmpeg() {
  int ret = 0;
  error_count = 0;

#if LIBAVFORMAT_VERSION_CHECK(59, 16, 100, 16, 100)
  const
#endif
  AVInputFormat *input_format = nullptr;
  // Handle options
  AVDictionary *opts = nullptr;
  if (!mOptions.empty()) {
    ret = av_dict_parse_string(&opts, mOptions.c_str(), "=", kOptionSeparators, 0);
    if (ret < 0) {
      Error("Could not parse ffmpeg input options '%s'. Options must be key=value pairs separated by commas or "
            "new lines, such as tls_verify=0, not ffmpeg command line flags like -tls_verify 0.", mOptions.c_str());
    }

    // "loop=1" is handled by us (seek-to-start on EOF), not by ffmpeg, which
    // does not understand it for most demuxers. Consume it so it is not passed
    // through and reported as an unrecognized option below.
    AVDictionaryEntry *loop_entry = av_dict_get(opts, "loop", nullptr, 0);
    if (loop_entry) {
      mLoop = (loop_entry->value != nullptr) && (atoi(loop_entry->value) != 0);
      av_dict_set(&opts, "loop", nullptr, 0);
      Debug(1, "Loop-on-EOF mode %s from options", mLoop ? "enabled" : "disabled");
    }

    // "realtime=1" (alias "re=1") is handled by us, like ffmpeg's -re flag:
    // pace packet delivery to the stream's native rate rather than reading the
    // file as fast as possible. Consume it so it is not passed through to the
    // demuxer and reported as an unrecognized option below.
    AVDictionaryEntry *re_entry = av_dict_get(opts, "realtime", nullptr, 0);
    if (!re_entry) re_entry = av_dict_get(opts, "re", nullptr, 0);
    if (re_entry) {
      mRealtime = (re_entry->value != nullptr) && (atoi(re_entry->value) != 0);
      av_dict_set(&opts, "realtime", nullptr, 0);
      av_dict_set(&opts, "re", nullptr, 0);
      Debug(1, "Real-time pacing %s from options", mRealtime ? "enabled" : "disabled");
    }
  }

  // Fresh prime: drop any real-time anchor so pacing restarts cleanly, and
  // start with no loop offset.
  mRealtimeAnchored = false;
  mLoopVideoOffset = 0;
  mLoopAudioOffset = 0;

  // Set transport method as specified by method field, rtpUni is default
  std::string protocol = mPath.substr(0, 4);
  protocol = StringToUpper(protocol);
  if ( protocol == "RTSP" ) {
    const std::string method = Method();
    if ( method == "rtpMulti" ) {
      ret = av_dict_set(&opts, "rtsp_transport", "udp_multicast", 0);
    } else if ( method == "rtpRtsp" ) {
      ret = av_dict_set(&opts, "rtsp_transport", "tcp", 0);
    } else if ( method == "rtpRtspHttp" ) {
      ret = av_dict_set(&opts, "rtsp_transport", "http", 0);
    } else if ( method == "rtpUni" ) {
      ret = av_dict_set(&opts, "rtsp_transport", "udp", 0);
    } else {
      Warning("Unknown method (%s)", method.c_str());
    }
    if (ret < 0) {
      Warning("Could not set rtsp_transport method '%s'", method.c_str());
    }
  } else if (protocol == "V4L2") {
    avdevice_register_all();
    input_format = av_find_input_format("video4linux2");
    if (!input_format) {
      Error("Cannot find v4l2 input format");
      return -1;
    }
    mPath = mPath.substr(7);
  }  // end if RTSP

  Debug(1, "Calling avformat_open_input for %s", mMaskedPath.c_str());

  mFormatContext = avformat_alloc_context();
  if (!mFormatContext) {
    Error("Unable to allocate format context");
    av_dict_free(&opts);
    return -1;
  }
  mFormatContext->interrupt_callback.callback = FfmpegInterruptCallback;
  mFormatContext->interrupt_callback.opaque = this;
  mFormatContext->flags |= AVFMT_FLAG_NOBUFFER | AVFMT_FLAG_FLUSH_PACKETS;

  if (mUser.length() > 0) {
    try {
      Url url(mPath);
      if (url.user_info().empty()) {
        url.user_info(mUser + ":" + mPass);
        mPath = url.str();
        Debug(1, "Rebuilt URI with encoded parameters: '%s'", mMaskedPath.c_str());
      }
    } catch (const Url::parse_error &e) {
      Debug(1, "Could not parse path as URL: %s", e.what());
    }
  }

  ret = avformat_open_input(&mFormatContext, mPath.c_str(), input_format, &opts);
  if (ret != 0) {
    logPrintf(Logger::ERROR + monitor->Importance(),
              "Unable to open input %s due to: %s", mMaskedPath.c_str(),
              av_make_error_string(ret).c_str());
    avformat_close_input(&mFormatContext);
    mFormatContext = nullptr;
    av_dict_free(&opts);
    return -1;
  }

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

  ret = avformat_find_stream_info(mFormatContext, nullptr);
  if (ret < 0) {
    logPrintf(Logger::ERROR + monitor->Importance(),
              "Unable to find stream info from %s due to: %s",
              mMaskedPath.c_str(), av_make_error_string(ret).c_str());
    avformat_close_input(&mFormatContext);
    return -1;
  }

  // Find first video stream present, the one we want Might not be the first
  mVideoStreamId = -1;
  mAudioStreamId = -1;
  for (unsigned int i=0; i < mFormatContext->nb_streams; i++) {
    AVStream *stream = mFormatContext->streams[i];
    zm_dump_stream_format(mFormatContext, i, 0, 0);
    if (is_video_stream(stream)) {
      if (!(stream->codecpar->width && stream->codecpar->height)) {
        logPrintf(Logger::WARNING + monitor->Importance(), "No width and height in video stream. Trying again");
        continue;
      }
      if (mVideoStreamId == -1) {
        mVideoStreamId = i;
        mVideoStream = stream;
      } else {
        Debug(2, "Have another video stream.");
        if (stream->codecpar->width == width and stream->codecpar->height == height) {
          Debug(1, "Choosing alternate video stream because it matches our resolution.");
          mVideoStreamId = i;
          mVideoStream = stream;
        } else {
          stream->discard = AVDISCARD_ALL;
        }
      }
    } else if (is_audio_stream(stream)) {
      if (mAudioStreamId == -1) {
        mAudioStreamId = i;
        mAudioStream = mFormatContext->streams[i];
      } else {
        Debug(2, "Have another audio stream.");
      }
    } else {
	    Debug(1, "Unknown stream type for stream %d", i);
    }
  }  // end foreach stream

  if (mVideoStreamId == -1) {
    avformat_close_input(&mFormatContext);
    return -1;
  }

  Debug(3, "Found video stream at index %d, audio stream at index %d",
        mVideoStreamId, mAudioStreamId);

  // No decoder is opened here any more. The monitor opens one against this
  // stream in Monitor::OpenDecoder() once PrimeCapture() returns, and owns it
  // for as long as the decoder thread needs it. This camera's job is to
  // produce packets.

  if (mAudioStreamId >= 0) {
    // No audio decoder is opened here. Both things that decode audio build
    // their own from the stream's codecpar: VideoStore for transcoding to aac,
    // and AudioDetector for levels. This camera only has to find the stream.
    zm_dump_stream_format((mSecondFormatContext?mSecondFormatContext:mFormatContext), mAudioStreamId, 0, 0);
  } else if (!monitor->GetSecondPath().empty()) {
    Debug(1, "Trying secondary stream at %s", mMaskedSecondPath.c_str());
    std::string secondPath = mSecondPath;
    if (mUser.length() > 0) {
      try {
        Url url(mSecondPath);
        if (url.user_info().empty()) {
          url.user_info(mUser + ":" + mPass);
          secondPath = url.str();
          Debug(1, "Rebuilt secondary URI with encoded parameters");
        } else {
          Debug(1, "Secondary path already has authentication, not overriding");
        }
      } catch (const Url::parse_error &e) {
        Debug(1, "Could not parse secondary path as URL: %s", e.what());
      }
    }
    mSecondInput = zm::make_unique<FFmpeg_Input>();
    if (mSecondInput->Open(secondPath.c_str()) > 0) {
      mSecondFormatContext = mSecondInput->get_format_context();
      mAudioStreamId = mSecondInput->get_audio_stream_id();
      mAudioStream = mSecondInput->get_audio_stream();
    } else {
      Warning("Failed to open secondary input");
    }
  }  // end if have audio stream

  // Ask the stream, not the decoder. Both carry the same dimensions -- the
  // decoder context is filled from the stream's codecpar -- but the stream is
  // what the camera is actually sending, and reading it here means this check
  // does not depend on a decoder having been opened.
  if (
    ((unsigned int)mVideoStream->codecpar->width != width)
    ||
    ((unsigned int)mVideoStream->codecpar->height != height)
  ) {
    Debug(1, "Monitor dimensions are %dx%d but camera is sending %dx%d",
          width, height, mVideoStream->codecpar->width, mVideoStream->codecpar->height);
  }

  // Seed fallback per-frame durations (in each stream's time_base) used to space
  // loops apart when a packet's own duration is missing. Live values from
  // packet->duration override these as packets are read.
  if (mVideoStream && mVideoStream->avg_frame_rate.num > 0) {
    mLoopVideoFrameDuration = av_rescale_q(1, av_inv_q(mVideoStream->avg_frame_rate), mVideoStream->time_base);
  }
  if (mLoopVideoFrameDuration <= 0) mLoopVideoFrameDuration = 1;
  if (mAudioStream && mAudioStream->codecpar->sample_rate > 0) {
    // Typical AAC frame is 1024 samples; good enough as a fallback spacing.
    mLoopAudioFrameDuration = av_rescale_q(1024, av_make_q(1, mAudioStream->codecpar->sample_rate), mAudioStream->time_base);
  }
  if (mLoopAudioFrameDuration <= 0) mLoopAudioFrameDuration = 1;

  mIsPrimed = true;

  return 1;
} // int FfmpegCamera::OpenFfmpeg()

int FfmpegCamera::Close() {
  mIsPrimed = false;
  mLastVideoPTS = 0;
  mLastAudioPTS = 0;
  mLastVideoDTS = AV_NOPTS_VALUE;
  mLastAudioDTS = AV_NOPTS_VALUE;


  if ( mFormatContext ) {
    avformat_close_input(&mFormatContext);
    mFormatContext = nullptr;
  }

  return 0;
}  // end FfmpegCamera::Close

int FfmpegCamera::FfmpegInterruptCallback(void *ctx) {
  if (zm_terminate) {
    Debug(1, "Received terminate in cb");
    return zm_terminate;
  }

  TimePoint now = std::chrono::steady_clock::now();
  if (now - start_read_time > Seconds(10)) {
    Debug(1, "timeout in ffmpeg camera now %" PRIi64 " - %" PRIi64 " > 10 s",
          static_cast<int64>(std::chrono::duration_cast<Seconds>(now.time_since_epoch()).count()),
          static_cast<int64>(std::chrono::duration_cast<Seconds>(start_read_time.time_since_epoch()).count()));
    return 1;
  }
  return 0;
}