Add Packet Queue

This commit is contained in:
SteveGilvarry 2016-04-04 20:04:14 +10:00
parent 24ebc33ea4
commit 966fbb1a10
5 changed files with 593 additions and 470 deletions

View File

@ -4,7 +4,7 @@
configure_file(zm_config.h.in "${CMAKE_CURRENT_BINARY_DIR}/zm_config.h" @ONLY) configure_file(zm_config.h.in "${CMAKE_CURRENT_BINARY_DIR}/zm_config.h" @ONLY)
# Group together all the source files that are used by all the binaries (zmc, zma, zmu, zms etc) # Group together all the source files that are used by all the binaries (zmc, zma, zmu, zms etc)
set(ZM_BIN_SRC_FILES zm_box.cpp zm_buffer.cpp zm_camera.cpp zm_comms.cpp zm_config.cpp zm_coord.cpp zm_curl_camera.cpp zm.cpp zm_db.cpp zm_logger.cpp zm_event.cpp zm_exception.cpp zm_file_camera.cpp zm_ffmpeg_camera.cpp zm_image.cpp zm_jpeg.cpp zm_libvlc_camera.cpp zm_local_camera.cpp zm_monitor.cpp zm_ffmpeg.cpp zm_mpeg.cpp zm_poly.cpp zm_regexp.cpp zm_remote_camera.cpp zm_remote_camera_http.cpp zm_remote_camera_rtsp.cpp zm_rtp.cpp zm_rtp_ctrl.cpp zm_rtp_data.cpp zm_rtp_source.cpp zm_rtsp.cpp zm_rtsp_auth.cpp zm_sdp.cpp zm_signal.cpp zm_stream.cpp zm_thread.cpp zm_time.cpp zm_timer.cpp zm_user.cpp zm_utils.cpp zm_video.cpp zm_videostore.cpp zm_zone.cpp) set(ZM_BIN_SRC_FILES zm_box.cpp zm_buffer.cpp zm_camera.cpp zm_comms.cpp zm_config.cpp zm_coord.cpp zm_curl_camera.cpp zm.cpp zm_db.cpp zm_logger.cpp zm_event.cpp zm_exception.cpp zm_file_camera.cpp zm_ffmpeg_camera.cpp zm_image.cpp zm_jpeg.cpp zm_libvlc_camera.cpp zm_local_camera.cpp zm_monitor.cpp zm_ffmpeg.cpp zm_mpeg.cpp zm_packetqueue.cpp zm_poly.cpp zm_regexp.cpp zm_remote_camera.cpp zm_remote_camera_http.cpp zm_remote_camera_rtsp.cpp zm_rtp.cpp zm_rtp_ctrl.cpp zm_rtp_data.cpp zm_rtp_source.cpp zm_rtsp.cpp zm_rtsp_auth.cpp zm_sdp.cpp zm_signal.cpp zm_stream.cpp zm_thread.cpp zm_time.cpp zm_timer.cpp zm_user.cpp zm_utils.cpp zm_video.cpp zm_videostore.cpp zm_zone.cpp)
# A fix for cmake recompiling the source files for every target. # A fix for cmake recompiling the source files for every target.
add_library(zm STATIC ${ZM_BIN_SRC_FILES}) add_library(zm STATIC ${ZM_BIN_SRC_FILES})

View File

@ -23,6 +23,7 @@
#include "zm_ffmpeg_camera.h" #include "zm_ffmpeg_camera.h"
extern "C" { extern "C" {
#include "libavutil/time.h" #include "libavutil/time.h"
} }
@ -40,10 +41,8 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri
Camera(p_id, 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), Camera(p_id, 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),
mPath(p_path), mPath(p_path),
mMethod(p_method), mMethod(p_method),
mOptions( p_options ) mOptions(p_options) {
{ if (capture) {
if ( capture )
{
Initialise(); Initialise();
} }
@ -81,18 +80,15 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri
} }
FfmpegCamera::~FfmpegCamera() FfmpegCamera::~FfmpegCamera() {
{
CloseFfmpeg(); CloseFfmpeg();
if ( capture ) if (capture) {
{
Terminate(); Terminate();
} }
} }
void FfmpegCamera::Initialise() void FfmpegCamera::Initialise() {
{
if (logDebugging()) if (logDebugging())
av_log_set_level(AV_LOG_DEBUG); av_log_set_level(AV_LOG_DEBUG);
else else
@ -101,12 +97,10 @@ void FfmpegCamera::Initialise()
av_register_all(); av_register_all();
} }
void FfmpegCamera::Terminate() void FfmpegCamera::Terminate() {
{
} }
int FfmpegCamera::PrimeCapture() int FfmpegCamera::PrimeCapture() {
{
mVideoStreamId = -1; mVideoStreamId = -1;
mAudioStreamId = -1; mAudioStreamId = -1;
Info("Priming capture from %s", mPath.c_str()); Info("Priming capture from %s", mPath.c_str());
@ -117,14 +111,12 @@ int FfmpegCamera::PrimeCapture()
return 0; return 0;
} }
int FfmpegCamera::PreCapture() int FfmpegCamera::PreCapture() {
{
// Nothing to do here // Nothing to do here
return ( 0); return ( 0);
} }
int FfmpegCamera::Capture( Image &image ) int FfmpegCamera::Capture(Image &image) {
{
if (!mCanCapture) { if (!mCanCapture) {
return -1; return -1;
} }
@ -154,11 +146,9 @@ int FfmpegCamera::Capture( Image &image )
} }
int frameComplete = false; int frameComplete = false;
while ( !frameComplete ) while (!frameComplete) {
{
int avResult = av_read_frame(mFormatContext, &packet); int avResult = av_read_frame(mFormatContext, &packet);
if ( avResult < 0 ) if (avResult < 0) {
{
char errbuf[AV_ERROR_MAX_STRING_SIZE]; char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE); av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if ( if (
@ -166,8 +156,7 @@ int FfmpegCamera::Capture( Image &image )
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) || (avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure. // Check for Connection failure.
(avResult == -110) (avResult == -110)
) ) {
{
Info("av_read_frame returned \"%s\". Reopening stream.", errbuf); Info("av_read_frame returned \"%s\". Reopening stream.", errbuf);
ReopenFfmpeg(); ReopenFfmpeg();
} }
@ -176,8 +165,7 @@ int FfmpegCamera::Capture( Image &image )
return ( -1); return ( -1);
} }
Debug(5, "Got packet from stream %d", packet.stream_index); Debug(5, "Got packet from stream %d", packet.stream_index);
if ( packet.stream_index == mVideoStreamId ) if (packet.stream_index == mVideoStreamId) {
{
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0) #if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if (avcodec_decode_video2(mCodecContext, mRawFrame, &frameComplete, &packet) < 0) if (avcodec_decode_video2(mCodecContext, mRawFrame, &frameComplete, &packet) < 0)
#else #else
@ -187,8 +175,7 @@ int FfmpegCamera::Capture( Image &image )
Debug(4, "Decoded video packet at frame %d", frameCount); Debug(4, "Decoded video packet at frame %d", frameCount);
if ( frameComplete ) if (frameComplete) {
{
Debug(3, "Got frame %d", frameCount); Debug(3, "Got frame %d", frameCount);
avpicture_fill((AVPicture *) mFrame, directbuffer, imagePixFormat, width, height); avpicture_fill((AVPicture *) mFrame, directbuffer, imagePixFormat, width, height);
@ -219,8 +206,7 @@ int FfmpegCamera::Capture( Image &image )
return (0); return (0);
} }
int FfmpegCamera::PostCapture() int FfmpegCamera::PostCapture() {
{
// Nothing to do here // Nothing to do here
return ( 0); return ( 0);
} }
@ -250,17 +236,14 @@ int FfmpegCamera::OpenFfmpeg() {
opVect.push_back("rtsp_transport=http"); opVect.push_back("rtsp_transport=http");
Debug(2, "Number of Options: %d", opVect.size()); Debug(2, "Number of Options: %d", opVect.size());
for (size_t i=0; i<opVect.size(); i++) for (size_t i = 0; i < opVect.size(); i++) {
{
StringVector parts = split(opVect[i], "="); StringVector parts = split(opVect[i], "=");
if (parts.size() > 1) { if (parts.size() > 1) {
parts[0] = trimSpaces(parts[0]); parts[0] = trimSpaces(parts[0]);
parts[1] = trimSpaces(parts[1]); parts[1] = trimSpaces(parts[1]);
if (av_dict_set(&opts, parts[0].c_str(), parts[1].c_str(), 0) == 0) { if (av_dict_set(&opts, parts[0].c_str(), parts[1].c_str(), 0) == 0) {
Debug(2, "set option %d '%s' to '%s'", i, parts[0].c_str(), parts[1].c_str()); Debug(2, "set option %d '%s' to '%s'", i, parts[0].c_str(), parts[1].c_str());
} } else {
else
{
Warning("Error trying to set option %d '%s' to '%s'", i, parts[0].c_str(), parts[1].c_str()); Warning("Error trying to set option %d '%s' to '%s'", i, parts[0].c_str(), parts[1].c_str());
} }
@ -303,8 +286,7 @@ int FfmpegCamera::OpenFfmpeg() {
// Find first video stream present // Find first video stream present
mVideoStreamId = -1; mVideoStreamId = -1;
for (unsigned int i=0; i < mFormatContext->nb_streams; i++ ) for (unsigned int i = 0; i < mFormatContext->nb_streams; i++) {
{
#if (LIBAVCODEC_VERSION_CHECK(52, 64, 0, 64, 0) || LIBAVUTIL_VERSION_CHECK(50, 14, 0, 14, 0)) #if (LIBAVCODEC_VERSION_CHECK(52, 64, 0, 64, 0) || LIBAVUTIL_VERSION_CHECK(50, 14, 0, 14, 0))
if (mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) if (mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
#else #else
@ -424,20 +406,17 @@ int FfmpegCamera::CloseFfmpeg(){
#endif #endif
#if HAVE_LIBSWSCALE #if HAVE_LIBSWSCALE
if ( mConvertContext ) if (mConvertContext) {
{
sws_freeContext(mConvertContext); sws_freeContext(mConvertContext);
mConvertContext = NULL; mConvertContext = NULL;
} }
#endif #endif
if ( mCodecContext ) if (mCodecContext) {
{
avcodec_close(mCodecContext); avcodec_close(mCodecContext);
mCodecContext = NULL; // Freed by av_close_input_file mCodecContext = NULL; // Freed by av_close_input_file
} }
if ( mFormatContext ) if (mFormatContext) {
{
#if !LIBAVFORMAT_VERSION_CHECK(53, 17, 0, 25, 0) #if !LIBAVFORMAT_VERSION_CHECK(53, 17, 0, 25, 0)
av_close_input_file(mFormatContext); av_close_input_file(mFormatContext);
#else #else
@ -449,8 +428,7 @@ int FfmpegCamera::CloseFfmpeg(){
return 0; return 0;
} }
int FfmpegCamera::FfmpegInterruptCallback(void *ctx) int FfmpegCamera::FfmpegInterruptCallback(void *ctx) {
{
FfmpegCamera* camera = reinterpret_cast<FfmpegCamera*> (ctx); FfmpegCamera* camera = reinterpret_cast<FfmpegCamera*> (ctx);
if (camera->mIsOpening) { if (camera->mIsOpening) {
int now = time(NULL); int now = time(NULL);
@ -487,8 +465,8 @@ void *FfmpegCamera::ReopenFfmpegThreadCallback(void *ctx){
} }
//Function to handle capture and store //Function to handle capture and store
int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_file )
{ int FfmpegCamera::CaptureAndRecord(Image &image, bool recording, char* event_file) {
if (!mCanCapture) { if (!mCanCapture) {
return -1; return -1;
} }
@ -509,6 +487,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
AVPacket packet; AVPacket packet;
uint8_t* directbuffer; uint8_t* directbuffer;
zm_packetqueue packetqueue;
/* Request a writeable buffer of the target image */ /* Request a writeable buffer of the target image */
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder); directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
@ -518,11 +497,9 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
} }
int frameComplete = false; int frameComplete = false;
while ( !frameComplete ) while (!frameComplete) {
{
int avResult = av_read_frame(mFormatContext, &packet); int avResult = av_read_frame(mFormatContext, &packet);
if ( avResult < 0 ) if (avResult < 0) {
{
char errbuf[AV_ERROR_MAX_STRING_SIZE]; char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE); av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if ( if (
@ -530,8 +507,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) || (avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure. // Check for Connection failure.
(avResult == -110) (avResult == -110)
) ) {
{
Info("av_read_frame returned \"%s\". Reopening stream.", errbuf); Info("av_read_frame returned \"%s\". Reopening stream.", errbuf);
ReopenFfmpeg(); ReopenFfmpeg();
} }
@ -540,8 +516,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
return ( -1); return ( -1);
} }
Debug(5, "Got packet from stream %d", packet.stream_index); Debug(5, "Got packet from stream %d", packet.stream_index);
if ( packet.stream_index == mVideoStreamId ) if (packet.stream_index == mVideoStreamId) {
{
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0) #if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if (avcodec_decode_video2(mCodecContext, mRawFrame, &frameComplete, &packet) < 0) if (avcodec_decode_video2(mCodecContext, mRawFrame, &frameComplete, &packet) < 0)
#else #else
@ -551,16 +526,18 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
Debug(4, "Decoded video packet at frame %d", frameCount); Debug(4, "Decoded video packet at frame %d", frameCount);
if ( frameComplete ) if (frameComplete) {
{
Debug(3, "Got frame %d", frameCount); Debug(3, "Got frame %d", frameCount);
avpicture_fill((AVPicture *) mFrame, directbuffer, imagePixFormat, width, height); avpicture_fill((AVPicture *) mFrame, directbuffer, imagePixFormat, width, height);
//Keep the last keyframe so we can establish immediate video //Buffer video packets
/*if(packet.flags & AV_PKT_FLAG_KEY) if (!recording) {
av_copy_packet(&lastKeyframePkt, &packet);*/ if(packet.flags & AV_PKT_FLAG_KEY){
//TODO I think we need to store the key frame location for seeking as part of the event packetqueue.clearQueues();
}
packetqueue.queueVideoPacket(&packet);
}
//Video recording //Video recording
if (recording && !wasRecording) { if (recording && !wasRecording) {

View File

@ -26,6 +26,7 @@
//#include "zm_utils.h" //#include "zm_utils.h"
#include "zm_ffmpeg.h" #include "zm_ffmpeg.h"
#include "zm_videostore.h" #include "zm_videostore.h"
#include "zm_packetqueue.h"
// //
// Class representing 'ffmpeg' cameras, i.e. those which are // Class representing 'ffmpeg' cameras, i.e. those which are

92
src/zm_packetqueue.cpp Normal file
View File

@ -0,0 +1,92 @@
//ZoneMinder Packet Queue Implementation Class
//Copyright 2016 Steve Gilvarry
//
//This file is part of ZoneMinder.
//
//ZoneMinder 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 3 of the License, or
//(at your option) any later version.
//
//ZoneMinder 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 ZoneMinder. If not, see <http://www.gnu.org/licenses/>.
#include "zm_packetqueue.h"
#define VIDEO_QUEUESIZE 200
#define AUDIO_QUEUESIZE 50
using namespace std;
zm_packetqueue::zm_packetqueue() {
}
zm_packetqueue::zm_packetqueue(const zm_packetqueue& orig) {
}
zm_packetqueue::~zm_packetqueue() {
}
bool zm_packetqueue::queueVideoPacket(AVPacket* packet){
return queuePacket(m_VideoQueue, packet);
}
bool zm_packetqueue::queueAudioPacket(AVPacket* packet)
{
return queuePacket(m_AudioQueue, packet);
}
bool zm_packetqueue::queuePacket(queue<AVPacket>& packetQueue, AVPacket* packet){
AVPacket input_ref = { 0 };
if (av_packet_ref(&input_ref, packet) < 0){
return false;
}
packetQueue.push(*packet);
return true;
}
bool zm_packetqueue::popPacket(queue<AVPacket>& packetQueue, AVPacket* packet)
{
if (packetQueue.empty())
{
return false;
}
*packet = packetQueue.front();
packetQueue.pop();
return true;
}
void zm_packetqueue::clearQueue(std::queue<AVPacket>& packetQueue)
{
while(!packetQueue.empty())
{
packetQueue.pop();
}
}
void zm_packetqueue::clearQueues()
{
clearQueue(m_VideoQueue);
clearQueue(m_AudioQueue);
}
bool zm_packetqueue::popAudioPacket(AVPacket* packet)
{
return popPacket(m_AudioQueue, packet);
}
bool zm_packetqueue::popVideoPacket(AVPacket* packet)
{
return popPacket(m_VideoQueue, packet);
}

53
src/zm_packetqueue.h Normal file
View File

@ -0,0 +1,53 @@
//ZoneMinder Packet Queue Interface Class
//Copyright 2016 Steve Gilvarry
//
//This file is part of ZoneMinder.
//
//ZoneMinder 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 3 of the License, or
//(at your option) any later version.
//
//ZoneMinder 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 ZoneMinder. If not, see <http://www.gnu.org/licenses/>.
#ifndef ZM_PACKETQUEUE_H
#define ZM_PACKETQUEUE_H
#include <queue>
extern "C" {
#include <libavformat/avformat.h>
}
class zm_packetqueue {
public:
zm_packetqueue();
zm_packetqueue(const zm_packetqueue& orig);
virtual ~zm_packetqueue();
bool queuePacket(std::queue<AVPacket>& packetQueue, AVPacket* packet);
bool queueVideoPacket(AVPacket* packet);
bool queueAudioPacket(AVPacket* packet);
bool popPacket(std::queue<AVPacket>& packetQueue, AVPacket* packet);
bool popVideoPacket(AVPacket* packet);
bool popAudioPacket(AVPacket* packet);
void clearQueues();
void clearQueue(std::queue<AVPacket>& packetQueue);
private:
int m_MaxVideoQueueSize;
int m_MaxAudioQueueSize;
std::queue<AVPacket> m_VideoQueue;
std::queue<AVPacket> m_AudioQueue;
};
#endif /* ZM_PACKETQUEUE_H */