Fix videostorage (#1885)

* merge relevant c bits to make h264 recording work

* h264 code requires libavresample and boost

* Need libavresample
This commit is contained in:
Isaac Connor 2017-05-16 12:04:56 -04:00 committed by GitHub
parent ca090a1165
commit b1f8485969
36 changed files with 4071 additions and 3771 deletions

View File

@ -571,6 +571,23 @@ if(NOT ZM_NO_FFMPEG)
set(optlibsnotfound "${optlibsnotfound} SWScale")
endif(SWSCALE_LIBRARIES)
# rescale (using find_library and find_path)
find_library(AVRESAMPLE_LIBRARIES avresample)
if(AVRESAMPLE_LIBRARIES)
set(HAVE_LIBAVRESAMPLE 1)
list(APPEND ZM_BIN_LIBS "${AVRESAMPLE_LIBRARIES}")
find_path(AVRESAMPLE_INCLUDE_DIR "libavresample/avresample.h" /usr/include/ffmpeg)
if(AVRESAMPLE_INCLUDE_DIR)
include_directories("${AVRESAMPLE_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${AVRESAMPLE_INCLUDE_DIR}")
endif(AVRESAMPLE_INCLUDE_DIR)
mark_as_advanced(FORCE AVRESAMPLE_LIBRARIES AVRESAMPLE_INCLUDE_DIR)
check_include_file("libavresample/avresample.h" HAVE_LIBAVRESAMPLE_AVRESAMPLE_H)
set(optlibsfound "${optlibsfound} AVResample")
else(AVRESAMPLE_LIBRARIES)
set(optlibsnotfound "${optlibsnotfound} AVResample")
endif(AVRESAMPLE_LIBRARIES)
# Find the path to the ffmpeg executable
find_program(FFMPEG_EXECUTABLE
NAMES ffmpeg avconv
@ -603,6 +620,13 @@ if(NOT ZM_NO_LIBVLC)
endif(LIBVLC_LIBRARIES)
endif(NOT ZM_NO_LIBVLC)
find_package(Boost 1.36.0)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
set(CMAKE_REQUIRED_INCLUDES "${Boost_INCLUDE_DIRS}")
list(APPEND ZM_BIN_LIBS "${Boost_LIBRARIES}")
endif()
# *** END OF LIBRARY CHECKS ***
# Check for gnutls or crypto

View File

@ -4,7 +4,7 @@
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)
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_packet.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.
add_library(zm STATIC ${ZM_BIN_SRC_FILES})

View File

@ -153,7 +153,7 @@ public:
mHead = mTail = mStorage;
else if ( level )
{
if ( (mHead-mStorage) > mSize )
if ( ((uintptr_t)mHead-(uintptr_t)mStorage) > mSize )
{
memcpy( mStorage, mHead, mSize );
mHead = mStorage;

View File

@ -20,7 +20,7 @@
#include "zm.h"
#include "zm_camera.h"
Camera::Camera( unsigned int p_monitor_id, SourceType p_type, int p_width, int p_height, int p_colours, int p_subpixelorder, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ) :
Camera::Camera( unsigned int p_monitor_id, SourceType p_type, unsigned int p_width, unsigned int p_height, int p_colours, int p_subpixelorder, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ) :
monitor_id( p_monitor_id ),
type( p_type ),
width( p_width),
@ -55,3 +55,8 @@ Monitor *Camera::getMonitor() {
monitor = Monitor::Load( monitor_id, false, Monitor::QUERY );
return monitor;
}
void Camera::setMonitor( Monitor *p_monitor ) {
monitor = p_monitor;
monitor_id = monitor->Id();
}

View File

@ -55,11 +55,12 @@ protected:
bool record_audio;
public:
Camera( unsigned int p_monitor_id, SourceType p_type, int p_width, int p_height, int p_colours, int p_subpixelorder, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio );
Camera( unsigned int p_monitor_id, SourceType p_type, unsigned int p_width, unsigned int p_height, int p_colours, int p_subpixelorder, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio );
virtual ~Camera();
unsigned int getId() const { return( monitor_id ); }
Monitor *getMonitor();
void setMonitor( Monitor *p_monitor );
SourceType Type() const { return( type ); }
bool IsLocal() const { return( type == LOCAL_SRC ); }
bool IsRemote() const { return( type == REMOTE_SRC ); }
@ -87,7 +88,7 @@ public:
virtual int PreCapture()=0;
virtual int Capture( Image &image )=0;
virtual int PostCapture()=0;
virtual int CaptureAndRecord( Image &image, bool recording, char* event_directory)=0;
virtual int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) = 0;
};
#endif // ZM_CAMERA_H

View File

@ -18,8 +18,11 @@
//
#include "zm.h"
#include "zm_curl_camera.h"
#include "zm_packetqueue.h"
#if HAVE_LIBCURL
#define CURL_MAXRETRY 5
@ -30,28 +33,24 @@ const char* content_type_match = "Content-Type:";
size_t content_length_match_len;
size_t content_type_match_len;
cURLCamera::cURLCamera( int p_id, const std::string &p_path, const std::string &p_user, const std::string &p_pass, 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 ) :
cURLCamera::cURLCamera( int p_id, const std::string &p_path, const std::string &p_user, const std::string &p_pass, unsigned int p_width, unsigned 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( p_id, CURL_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 ), mUser( p_user ), mPass ( p_pass ), bTerminate( false ), bReset( false ), mode ( MODE_UNSET )
{
if ( capture )
{
if ( capture ) {
Initialise();
}
}
cURLCamera::~cURLCamera()
{
if ( capture )
{
cURLCamera::~cURLCamera() {
if ( capture ) {
Terminate();
}
}
void cURLCamera::Initialise()
{
void cURLCamera::Initialise() {
content_length_match_len = strlen(content_length_match);
content_type_match_len = strlen(content_type_match);
@ -88,8 +87,7 @@ void cURLCamera::Initialise()
}
}
void cURLCamera::Terminate()
{
void cURLCamera::Terminate() {
/* Signal the thread to terminate */
bTerminate = true;
@ -108,20 +106,17 @@ void cURLCamera::Terminate()
}
int cURLCamera::PrimeCapture()
{
int cURLCamera::PrimeCapture() {
//Info( "Priming capture from %s", mPath.c_str() );
return 0;
}
int cURLCamera::PreCapture()
{
int cURLCamera::PreCapture() {
// Nothing to do here
return( 0 );
}
int cURLCamera::Capture( Image &image )
{
int cURLCamera::Capture( Image &image ) {
bool frameComplete = false;
/* MODE_STREAM specific variables */
@ -305,22 +300,19 @@ int cURLCamera::Capture( Image &image )
return 0;
}
int cURLCamera::PostCapture()
{
int cURLCamera::PostCapture() {
// Nothing to do here
return( 0 );
}
int cURLCamera::CaptureAndRecord( Image &image, bool recording, char* event_directory )
{
int cURLCamera::CaptureAndRecord( Image &image, struct timeval recording, char* event_directory ) {
Error("Capture and Record not implemented for the cURL camera type");
// Nothing to do here
return( 0 );
}
size_t cURLCamera::data_callback(void *buffer, size_t size, size_t nmemb, void *userdata)
{
size_t cURLCamera::data_callback(void *buffer, size_t size, size_t nmemb, void *userdata) {
lock();
/* Append the data we just received to our buffer */
@ -341,8 +333,7 @@ size_t cURLCamera::data_callback(void *buffer, size_t size, size_t nmemb, void *
size_t cURLCamera::header_callback( void *buffer, size_t size, size_t nmemb, void *userdata)
{
size_t cURLCamera::header_callback( void *buffer, size_t size, size_t nmemb, void *userdata) {
std::string header;
header.assign((const char*)buffer, size*nmemb);
@ -382,8 +373,7 @@ size_t cURLCamera::header_callback( void *buffer, size_t size, size_t nmemb, voi
return size*nmemb;
}
void* cURLCamera::thread_func()
{
void* cURLCamera::thread_func() {
long tRet;
double dSize;
@ -529,8 +519,7 @@ int cURLCamera::unlock() {
return nRet;
}
int cURLCamera::progress_callback(void *userdata, double dltotal, double dlnow, double ultotal, double ulnow)
{
int cURLCamera::progress_callback(void *userdata, double dltotal, double dlnow, double ultotal, double ulnow) {
/* Signal the curl thread to terminate */
if(bTerminate)
return -10;
@ -539,18 +528,15 @@ int cURLCamera::progress_callback(void *userdata, double dltotal, double dlnow,
}
/* These functions call the functions in the class for the correct object */
size_t data_callback_dispatcher(void *buffer, size_t size, size_t nmemb, void *userdata)
{
size_t data_callback_dispatcher(void *buffer, size_t size, size_t nmemb, void *userdata) {
return ((cURLCamera*)userdata)->data_callback(buffer,size,nmemb,userdata);
}
size_t header_callback_dispatcher(void *buffer, size_t size, size_t nmemb, void *userdata)
{
size_t header_callback_dispatcher(void *buffer, size_t size, size_t nmemb, void *userdata) {
return ((cURLCamera*)userdata)->header_callback(buffer,size,nmemb,userdata);
}
int progress_callback_dispatcher(void *userdata, double dltotal, double dlnow, double ultotal, double ulnow)
{
int progress_callback_dispatcher(void *userdata, double dltotal, double dlnow, double ultotal, double ulnow) {
return ((cURLCamera*)userdata)->progress_callback(userdata,dltotal,dlnow,ultotal,ulnow);
}
@ -558,6 +544,4 @@ void* thread_func_dispatcher(void* object) {
return ((cURLCamera*)object)->thread_func();
}
#endif // HAVE_LIBCURL

View File

@ -39,8 +39,7 @@
// Class representing 'curl' cameras, i.e. those which are
// accessed using the curl library
//
class cURLCamera : public Camera
{
class cURLCamera : public Camera {
protected:
typedef enum {MODE_UNSET, MODE_SINGLE, MODE_STREAM} mode_t;
@ -65,7 +64,7 @@ protected:
pthread_cond_t request_complete_cond;
public:
cURLCamera( int p_id, const std::string &path, const std::string &username, const std::string &password, 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 );
cURLCamera( int p_id, const std::string &path, const std::string &username, const std::string &password, unsigned int p_width, unsigned 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 );
~cURLCamera();
const std::string &Path() const { return( mPath ); }
@ -79,7 +78,7 @@ public:
int PreCapture();
int Capture( Image &image );
int PostCapture();
int CaptureAndRecord( Image &image, bool recording, char* event_directory);
int CaptureAndRecord( Image &image, struct timeval recording, char* event_directory );
size_t data_callback(void *buffer, size_t size, size_t nmemb, void *userdata);
size_t header_callback(void *buffer, size_t size, size_t nmemb, void *userdata);

View File

@ -72,8 +72,7 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
createNotes( notes );
bool untimedEvent = false;
if ( !start_time.tv_sec )
{
if ( !start_time.tv_sec ) {
untimedEvent = true;
gettimeofday( &start_time, 0 );
}
@ -82,14 +81,12 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
struct tm *stime = localtime( &start_time.tv_sec );
snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, Name, StartTime, Width, Height, Cause, Notes, Videoed ) values ( %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', '%d' )", monitor->Id(), start_time.tv_sec, monitor->Width(), monitor->Height(), cause.c_str(), notes.c_str(), videoEvent );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't insert event: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
id = mysql_insert_id( &dbconn );
if ( untimedEvent )
{
if ( untimedEvent ) {
Warning( "Event %d has zero time, setting to current", id );
}
end_time.tv_sec = 0;
@ -98,8 +95,7 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
tot_score = 0;
max_score = 0;
if ( config.use_deep_storage )
{
if ( config.use_deep_storage ) {
char *path_ptr = path;
path_ptr += snprintf( path_ptr, sizeof(path), "%s/%d", config.dir_events, monitor->Id() );
@ -114,17 +110,14 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
char date_path[PATH_MAX] = "";
char time_path[PATH_MAX] = "";
char *time_path_ptr = time_path;
for ( unsigned int i = 0; i < sizeof(dt_parts)/sizeof(*dt_parts); i++ )
{
for ( unsigned int i = 0; i < sizeof(dt_parts)/sizeof(*dt_parts); i++ ) {
path_ptr += snprintf( path_ptr, sizeof(path)-(path_ptr-path), "/%02d", dt_parts[i] );
struct stat statbuf;
errno = 0;
if ( stat( path, &statbuf ) ) {
if ( errno == ENOENT || errno == ENOTDIR )
{
if ( mkdir( path, 0755 ) )
{
if ( errno == ENOENT || errno == ENOTDIR ) {
if ( mkdir( path, 0755 ) ) {
Fatal( "Can't mkdir %s: %s", path, strerror(errno));
}
} else {
@ -147,18 +140,14 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
fclose( id_fp );
else
Fatal( "Can't fopen %s: %s", id_file, strerror(errno));
}
else
{
} else {
snprintf( path, sizeof(path), "%s/%d/%d", config.dir_events, monitor->Id(), id );
struct stat statbuf;
errno = 0;
stat( path, &statbuf );
if ( errno == ENOENT || errno == ENOTDIR )
{
if ( mkdir( path, 0755 ) )
{
if ( errno == ENOENT || errno == ENOTDIR ) {
if ( mkdir( path, 0755 ) ) {
Error( "Can't mkdir %s: %s", path, strerror(errno));
}
}
@ -169,7 +158,7 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
fclose( id_fp );
else
Fatal( "Can't fopen %s: %s", id_file, strerror(errno));
}
} // deep storage or not
last_db_frame = 0;
video_name[0] = 0;
@ -177,7 +166,6 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
/* Save as video */
if ( monitor->GetOptVideoWriter() != 0 ) {
int nRet;
snprintf( video_name, sizeof(video_name), "%d-%s", id, "video.mp4" );
snprintf( video_file, sizeof(video_file), video_file_format, path, video_name );
snprintf( timecodes_name, sizeof(timecodes_name), "%d-%s", id, "video.timecodes" );
@ -188,14 +176,13 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
#if ZM_HAVE_VIDEOWRITER_X264MP4
videowriter = new X264MP4Writer(video_file, monitor->Width(), monitor->Height(), monitor->Colours(), monitor->SubpixelOrder(), monitor->GetOptEncoderParams());
#else
videowriter = NULL;
Error("ZoneMinder was not compiled with the X264 MP4 video writer, check dependencies (x264 and mp4v2)");
#endif
}
if(videowriter != NULL) {
/* Open the video stream */
nRet = videowriter->Open();
int nRet = videowriter->Open();
if(nRet != 0) {
Error("Failed opening video stream");
delete videowriter;
@ -213,20 +200,18 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
videowriter = NULL;
}
}
} // Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string &p_cause, const StringSetMap &p_noteSetMap, bool p_videoEvent )
Event::~Event()
{
if ( frames > last_db_frame )
{
Event::~Event() {
static char sql[ZM_SQL_MED_BUFSIZ];
struct DeltaTimeval delta_time;
DELTA_TIMEVAL( delta_time, end_time, start_time, DT_PREC_2 );
if ( frames > last_db_frame ) {
Debug( 1, "Adding closing frame %d to DB", frames );
static char sql[ZM_SQL_SML_BUFSIZ];
snprintf( sql, sizeof(sql), "insert into Frames ( EventId, FrameId, TimeStamp, Delta ) values ( %d, %d, from_unixtime( %ld ), %s%ld.%02ld )", id, frames, end_time.tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't insert frame: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
@ -234,9 +219,7 @@ Event::~Event()
/* Close the video file */
if ( videowriter != NULL ) {
int nRet;
nRet = videowriter->Close();
int nRet = videowriter->Close();
if(nRet != 0) {
Error("Failed closing video stream");
}
@ -248,29 +231,20 @@ Event::~Event()
timecodes_fd = NULL;
}
static char sql[ZM_SQL_MED_BUFSIZ];
struct DeltaTimeval delta_time;
DELTA_TIMEVAL( delta_time, end_time, start_time, DT_PREC_2 );
snprintf( sql, sizeof(sql), "update Events set Name='%s%d', EndTime = from_unixtime( %ld ), Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d, DefaultVideo = '%s' where Id = %d", monitor->EventPrefix(), id, end_time.tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, frames, alarm_frames, tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score, video_name, id );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't update event: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
}
void Event::createNotes( std::string &notes )
{
void Event::createNotes( std::string &notes ) {
notes.clear();
for ( StringSetMap::const_iterator mapIter = noteSetMap.begin(); mapIter != noteSetMap.end(); mapIter++ )
{
for ( StringSetMap::const_iterator mapIter = noteSetMap.begin(); mapIter != noteSetMap.end(); mapIter++ ) {
notes += mapIter->first;
notes += ": ";
const StringSet &stringSet = mapIter->second;
for ( StringSet::const_iterator setIter = stringSet.begin(); setIter != stringSet.end(); setIter++ )
{
for ( StringSet::const_iterator setIter = stringSet.begin(); setIter != stringSet.end(); setIter++ ) {
if ( setIter != stringSet.begin() )
notes += ", ";
notes += *setIter;
@ -280,8 +254,7 @@ void Event::createNotes( std::string &notes )
int Event::sd = -1;
bool Event::WriteFrameImage( Image *image, struct timeval timestamp, const char *event_file, bool alarm_frame )
{
bool Event::WriteFrameImage( Image *image, struct timeval timestamp, const char *event_file, bool alarm_frame ) {
Image* ImgToWrite;
Image* ts_image = NULL;
@ -290,8 +263,7 @@ bool Event::WriteFrameImage( Image *image, struct timeval timestamp, const char
ts_image = new Image(*image);
monitor->TimestampImage( ts_image, &timestamp );
ImgToWrite=ts_image;
}
else
} else
ImgToWrite=image;
int thisquality = ( alarm_frame && (config.jpeg_alarm_file_quality > config.jpeg_file_quality) ) ? config.jpeg_alarm_file_quality : 0 ; // quality to use, zero is default
@ -301,14 +273,12 @@ bool Event::WriteFrameImage( Image *image, struct timeval timestamp, const char
return( true );
}
bool Event::WriteFrameVideo( const Image *image, const struct timeval timestamp, VideoWriter* videow )
{
bool Event::WriteFrameVideo( const Image *image, const struct timeval timestamp, VideoWriter* videow ) {
const Image* frameimg = image;
Image ts_image;
/* Checking for invalid parameters */
if ( videow == NULL )
{
if ( videow == NULL ) {
Error("NULL Video object");
return false;
}
@ -336,44 +306,32 @@ bool Event::WriteFrameVideo( const Image *image, const struct timeval timestamp,
return( true );
}
void Event::updateNotes( const StringSetMap &newNoteSetMap )
{
void Event::updateNotes( const StringSetMap &newNoteSetMap ) {
bool update = false;
//Info( "Checking notes, %d <> %d", noteSetMap.size(), newNoteSetMap.size() );
if ( newNoteSetMap.size() > 0 )
{
if ( noteSetMap.size() == 0 )
{
if ( newNoteSetMap.size() > 0 ) {
if ( noteSetMap.size() == 0 ) {
noteSetMap = newNoteSetMap;
update = true;
}
else
{
for ( StringSetMap::const_iterator newNoteSetMapIter = newNoteSetMap.begin(); newNoteSetMapIter != newNoteSetMap.end(); newNoteSetMapIter++ )
{
} else {
for ( StringSetMap::const_iterator newNoteSetMapIter = newNoteSetMap.begin(); newNoteSetMapIter != newNoteSetMap.end(); newNoteSetMapIter++ ) {
const std::string &newNoteGroup = newNoteSetMapIter->first;
const StringSet &newNoteSet = newNoteSetMapIter->second;
//Info( "Got %d new strings", newNoteSet.size() );
if ( newNoteSet.size() > 0 )
{
if ( newNoteSet.size() > 0 ) {
StringSetMap::iterator noteSetMapIter = noteSetMap.find( newNoteGroup );
if ( noteSetMapIter == noteSetMap.end() )
{
if ( noteSetMapIter == noteSetMap.end() ) {
//Info( "Can't find note group %s, copying %d strings", newNoteGroup.c_str(), newNoteSet.size() );
noteSetMap.insert( StringSetMap::value_type( newNoteGroup, newNoteSet ) );
update = true;
}
else
{
} else {
StringSet &noteSet = noteSetMapIter->second;
//Info( "Found note group %s, got %d strings", newNoteGroup.c_str(), newNoteSet.size() );
for ( StringSet::const_iterator newNoteSetIter = newNoteSet.begin(); newNoteSetIter != newNoteSet.end(); newNoteSetIter++ )
{
for ( StringSet::const_iterator newNoteSetIter = newNoteSet.begin(); newNoteSetIter != newNoteSet.end(); newNoteSetIter++ ) {
const std::string &newNote = *newNoteSetIter;
StringSet::iterator noteSetIter = noteSet.find( newNote );
if ( noteSetIter == noteSet.end() )
{
if ( noteSetIter == noteSet.end() ) {
noteSet.insert( newNote );
update = true;
}
@ -384,8 +342,7 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap )
}
}
if ( update )
{
if ( update ) {
std::string notes;
createNotes( notes );
@ -397,19 +354,16 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap )
char notesStr[ZM_SQL_MED_BUFSIZ] = "";
unsigned long notesLen = 0;
if ( !stmt )
{
if ( !stmt ) {
const char *sql = "update Events set Notes = ? where Id = ?";
stmt = mysql_stmt_init( &dbconn );
if ( mysql_stmt_prepare( stmt, sql, strlen(sql) ) )
{
if ( mysql_stmt_prepare( stmt, sql, strlen(sql) ) ) {
Fatal( "Unable to prepare sql '%s': %s", sql, mysql_stmt_error(stmt) );
}
/* Get the parameter count from the statement */
if ( mysql_stmt_param_count( stmt ) != 2 )
{
if ( mysql_stmt_param_count( stmt ) != 2 ) {
Fatal( "Unexpected parameter count %ld in sql '%s'", mysql_stmt_param_count( stmt ), sql );
}
@ -429,8 +383,7 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap )
bind[1].length= 0;
/* Bind the buffers */
if ( mysql_stmt_bind_param( stmt, bind ) )
{
if ( mysql_stmt_bind_param( stmt, bind ) ) {
Fatal( "Unable to bind sql '%s': %s", sql, mysql_stmt_error(stmt) );
}
}
@ -438,8 +391,7 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap )
strncpy( notesStr, notes.c_str(), sizeof(notesStr) );
notesLen = notes.length();
if ( mysql_stmt_execute( stmt ) )
{
if ( mysql_stmt_execute( stmt ) ) {
Fatal( "Unable to execute sql '%s': %s", sql, mysql_stmt_error(stmt) );
}
#else
@ -448,30 +400,25 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap )
mysql_real_escape_string( &dbconn, escapedNotes, notes.c_str(), notes.length() );
snprintf( sql, sizeof(sql), "update Events set Notes = '%s' where Id = %d", escapedNotes, id );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't insert event: %s", mysql_error( &dbconn ) );
}
#endif
}
}
void Event::AddFrames( int n_frames, Image **images, struct timeval **timestamps )
{
void Event::AddFrames( int n_frames, Image **images, struct timeval **timestamps ) {
for (int i = 0; i < n_frames; i += ZM_SQL_BATCH_SIZE) {
AddFramesInternal(n_frames, i, images, timestamps);
}
}
void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, struct timeval **timestamps )
{
void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, struct timeval **timestamps ) {
static char sql[ZM_SQL_LGE_BUFSIZ];
strncpy( sql, "insert into Frames ( EventId, FrameId, TimeStamp, Delta ) values ", sizeof(sql) );
int frameCount = 0;
for ( int i = start_frame; i < n_frames && i - start_frame < ZM_SQL_BATCH_SIZE; i++ )
{
if ( !timestamps[i]->tv_sec )
{
for ( int i = start_frame; i < n_frames && i - start_frame < ZM_SQL_BATCH_SIZE; i++ ) {
if ( !timestamps[i]->tv_sec ) {
Debug( 1, "Not adding pre-capture frame %d, zero timestamp", i );
continue;
}
@ -505,27 +452,21 @@ void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, st
frameCount++;
}
if ( frameCount )
{
if ( frameCount ) {
Debug( 1, "Adding %d/%d frames to DB", frameCount, n_frames );
*(sql+strlen(sql)-2) = '\0';
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't insert frames: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
last_db_frame = frames;
}
else
{
} else {
Debug( 1, "No valid pre-capture frames to add" );
}
}
void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *alarm_image )
{
if ( !timestamp.tv_sec )
{
void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *alarm_image ) {
if ( !timestamp.tv_sec ) {
Debug( 1, "Not adding new frame, zero timestamp" );
return;
}
@ -559,25 +500,21 @@ void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *
score = 0;
bool db_frame = (strcmp(frame_type,"Bulk") != 0) || ((frames%config.bulk_frame_interval)==0) || !frames;
if ( db_frame )
{
if ( db_frame ) {
Debug( 1, "Adding frame %d of type \"%s\" to DB", frames, frame_type );
static char sql[ZM_SQL_MED_BUFSIZ];
snprintf( sql, sizeof(sql), "insert into Frames ( EventId, FrameId, Type, TimeStamp, Delta, Score ) values ( %d, %d, '%s', from_unixtime( %ld ), %s%ld.%02ld, %d )", id, frames, frame_type, timestamp.tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, score );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't insert frame: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
last_db_frame = frames;
// We are writing a Bulk frame
if ( !strcmp( frame_type,"Bulk") )
{
if ( !strcmp( frame_type,"Bulk") ) {
snprintf( sql, sizeof(sql), "update Events set Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d where Id = %d", delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, frames, alarm_frames, tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score, id );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't update event: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
@ -587,16 +524,14 @@ void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *
end_time = timestamp;
// We are writing an Alarm frame
if ( !strcmp( frame_type,"Alarm") )
{
if ( !strcmp( frame_type,"Alarm") ) {
alarm_frames++;
tot_score += score;
if ( score > (int)max_score )
max_score = score;
if ( alarm_image )
{
if ( alarm_image ) {
snprintf( event_file, sizeof(event_file), analyse_file_format, path, frames );
Debug( 1, "Writing analysis frame %d", frames );
@ -650,28 +585,24 @@ void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *
*/
}
bool EventStream::loadInitialEventData( int monitor_id, time_t event_time )
{
bool EventStream::loadInitialEventData( int monitor_id, time_t event_time ) {
static char sql[ZM_SQL_SML_BUFSIZ];
snprintf( sql, sizeof(sql), "select Id from Events where MonitorId = %d and unix_timestamp( EndTime ) > %ld order by Id asc limit 1", monitor_id, event_time );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
MYSQL_RES *result = mysql_store_result( &dbconn );
if ( !result )
{
if ( !result ) {
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
MYSQL_ROW dbrow = mysql_fetch_row( result );
if ( mysql_errno( &dbconn ) )
{
if ( mysql_errno( &dbconn ) ) {
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
@ -682,17 +613,13 @@ bool EventStream::loadInitialEventData( int monitor_id, time_t event_time )
loadEventData( init_event_id );
if ( event_time )
{
if ( event_time ) {
curr_stream_time = event_time;
curr_frame_id = 1;
if ( event_time >= event_data->start_time )
{
for (unsigned int i = 0; i < event_data->frame_count; i++ )
{
if ( event_time >= event_data->start_time ) {
for (unsigned int i = 0; i < event_data->frame_count; i++ ) {
//Info( "eft %d > et %d", event_data->frames[i].timestamp, event_time );
if ( event_data->frames[i].timestamp >= event_time )
{
if ( event_data->frames[i].timestamp >= event_time ) {
curr_frame_id = i+1;
Debug( 3, "Set cst:%.2f", curr_stream_time );
Debug( 3, "Set cfid:%d", curr_frame_id );
@ -705,51 +632,42 @@ bool EventStream::loadInitialEventData( int monitor_id, time_t event_time )
return( true );
}
bool EventStream::loadInitialEventData( int init_event_id, unsigned int init_frame_id )
{
bool EventStream::loadInitialEventData( int init_event_id, unsigned int init_frame_id ) {
loadEventData( init_event_id );
if ( init_frame_id )
{
if ( init_frame_id ) {
curr_stream_time = event_data->frames[init_frame_id-1].timestamp;
curr_frame_id = init_frame_id;
}
else
{
} else {
curr_stream_time = event_data->start_time;
}
return( true );
}
bool EventStream::loadEventData( int event_id )
{
bool EventStream::loadEventData( int event_id ) {
static char sql[ZM_SQL_MED_BUFSIZ];
snprintf( sql, sizeof(sql), "select M.Id, M.Name, E.Frames, unix_timestamp( StartTime ) as StartTimestamp, max(F.Delta)-min(F.Delta) as Duration from Events as E inner join Monitors as M on E.MonitorId = M.Id inner join Frames as F on E.Id = F.EventId where E.Id = %d group by E.Id", event_id );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
MYSQL_RES *result = mysql_store_result( &dbconn );
if ( !result )
{
if ( !result ) {
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
if ( !mysql_num_rows( result ) )
{
if ( !mysql_num_rows( result ) ) {
Fatal( "Unable to load event %d, not found in DB", event_id );
}
MYSQL_ROW dbrow = mysql_fetch_row( result );
if ( mysql_errno( &dbconn ) )
{
if ( mysql_errno( &dbconn ) ) {
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
@ -759,16 +677,13 @@ bool EventStream::loadEventData( int event_id )
event_data->event_id = event_id;
event_data->monitor_id = atoi( dbrow[0] );
event_data->start_time = atoi(dbrow[3]);
if ( config.use_deep_storage )
{
if ( config.use_deep_storage ) {
struct tm *event_time = localtime( &event_data->start_time );
if ( config.dir_events[0] == '/' )
snprintf( event_data->path, sizeof(event_data->path), "%s/%ld/%02d/%02d/%02d/%02d/%02d/%02d", config.dir_events, event_data->monitor_id, event_time->tm_year-100, event_time->tm_mon+1, event_time->tm_mday, event_time->tm_hour, event_time->tm_min, event_time->tm_sec );
else
snprintf( event_data->path, sizeof(event_data->path), "%s/%s/%ld/%02d/%02d/%02d/%02d/%02d/%02d", staticConfig.PATH_WEB.c_str(), config.dir_events, event_data->monitor_id, event_time->tm_year-100, event_time->tm_mon+1, event_time->tm_mday, event_time->tm_hour, event_time->tm_min, event_time->tm_sec );
}
else
{
} else {
if ( config.dir_events[0] == '/' )
snprintf( event_data->path, sizeof(event_data->path), "%s/%ld/%ld", config.dir_events, event_data->monitor_id, event_data->event_id );
else
@ -782,15 +697,13 @@ bool EventStream::loadEventData( int event_id )
mysql_free_result( result );
snprintf( sql, sizeof(sql), "select FrameId, unix_timestamp( `TimeStamp` ), Delta from Frames where EventId = %d order by FrameId asc", event_id );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
result = mysql_store_result( &dbconn );
if ( !result )
{
if ( !result ) {
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
@ -801,17 +714,14 @@ bool EventStream::loadEventData( int event_id )
int id, last_id = 0;
time_t timestamp, last_timestamp = event_data->start_time;
double delta, last_delta = 0.0;
while ( ( dbrow = mysql_fetch_row( result ) ) )
{
while ( ( dbrow = mysql_fetch_row( result ) ) ) {
id = atoi(dbrow[0]);
timestamp = atoi(dbrow[1]);
delta = atof(dbrow[2]);
int id_diff = id - last_id;
double frame_delta = (delta-last_delta)/id_diff;
if ( id_diff > 1 )
{
for ( int i = last_id+1; i < id; i++ )
{
if ( id_diff > 1 ) {
for ( int i = last_id+1; i < id; i++ ) {
event_data->frames[i-1].timestamp = (time_t)(last_timestamp + ((i-last_id)*frame_delta));
event_data->frames[i-1].offset = (time_t)(event_data->frames[i-1].timestamp-event_data->start_time);
event_data->frames[i-1].delta = frame_delta;
@ -826,8 +736,7 @@ bool EventStream::loadEventData( int event_id )
last_delta = delta;
last_timestamp = timestamp;
}
if ( mysql_errno( &dbconn ) )
{
if ( mysql_errno( &dbconn ) ) {
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
@ -839,8 +748,7 @@ bool EventStream::loadEventData( int event_id )
mysql_free_result( result );
if ( forceEventChange || mode == MODE_ALL_GAPLESS )
{
if ( forceEventChange || mode == MODE_ALL_GAPLESS ) {
if ( replay_rate > 0 )
curr_stream_time = event_data->frames[0].timestamp;
else
@ -851,12 +759,10 @@ bool EventStream::loadEventData( int event_id )
return( true );
}
void EventStream::processCommand( const CmdMsg *msg )
{
void EventStream::processCommand( const CmdMsg *msg ) {
Debug( 2, "Got message, type %d, msg %d", msg->msg_type, msg->msg_data[0] )
// Check for incoming command
switch( (MsgCommand)msg->msg_data[0] )
{
switch( (MsgCommand)msg->msg_data[0] ) {
case CMD_PAUSE :
{
Debug( 1, "Got PAUSE command" );
@ -905,14 +811,12 @@ void EventStream::processCommand( const CmdMsg *msg )
case CMD_FASTFWD :
{
Debug( 1, "Got FAST FWD command" );
if ( paused )
{
if ( paused ) {
// Clear paused flag
paused = false;
}
// Set play rate
switch ( replay_rate )
{
switch ( replay_rate ) {
case 2 * ZM_RATE_BASE :
replay_rate = 5 * ZM_RATE_BASE;
break;
@ -957,14 +861,12 @@ void EventStream::processCommand( const CmdMsg *msg )
case CMD_FASTREV :
{
Debug( 1, "Got FAST REV command" );
if ( paused )
{
if ( paused ) {
// Clear paused flag
paused = false;
}
// Set play rate
switch ( replay_rate )
{
switch ( replay_rate ) {
case -2 * ZM_RATE_BASE :
replay_rate = -5 * ZM_RATE_BASE;
break;
@ -989,8 +891,7 @@ void EventStream::processCommand( const CmdMsg *msg )
x = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
y = ((unsigned char)msg->msg_data[3]<<8)|(unsigned char)msg->msg_data[4];
Debug( 1, "Got ZOOM IN command, to %d,%d", x, y );
switch ( zoom )
{
switch ( zoom ) {
case 100:
zoom = 150;
break;
@ -1013,8 +914,7 @@ void EventStream::processCommand( const CmdMsg *msg )
case CMD_ZOOMOUT :
{
Debug( 1, "Got ZOOM OUT command" );
switch ( zoom )
{
switch ( zoom ) {
case 500:
zoom = 400;
break;
@ -1115,8 +1015,7 @@ void EventStream::processCommand( const CmdMsg *msg )
DataMsg status_msg;
status_msg.msg_type = MSG_DATA_EVENT;
memcpy( &status_msg.msg_data, &status_data, sizeof(status_data) );
if ( sendto( sd, &status_msg, sizeof(status_msg), MSG_DONTWAIT, (sockaddr *)&rem_addr, sizeof(rem_addr) ) < 0 )
{
if ( sendto( sd, &status_msg, sizeof(status_msg), MSG_DONTWAIT, (sockaddr *)&rem_addr, sizeof(rem_addr) ) < 0 ) {
//if ( errno != EAGAIN )
{
Error( "Can't sendto on sd %d: %s", sd, strerror(errno) );
@ -1130,49 +1029,39 @@ void EventStream::processCommand( const CmdMsg *msg )
updateFrameRate( (double)event_data->frame_count/event_data->duration );
}
void EventStream::checkEventLoaded()
{
void EventStream::checkEventLoaded() {
bool reload_event = false;
static char sql[ZM_SQL_SML_BUFSIZ];
if ( curr_frame_id <= 0 )
{
if ( curr_frame_id <= 0 ) {
snprintf( sql, sizeof(sql), "select Id from Events where MonitorId = %ld and Id < %ld order by Id desc limit 1", event_data->monitor_id, event_data->event_id );
reload_event = true;
}
else if ( (unsigned int)curr_frame_id > event_data->frame_count )
{
} else if ( (unsigned int)curr_frame_id > event_data->frame_count ) {
snprintf( sql, sizeof(sql), "select Id from Events where MonitorId = %ld and Id > %ld order by Id asc limit 1", event_data->monitor_id, event_data->event_id );
reload_event = true;
}
if ( reload_event )
{
if ( forceEventChange || mode != MODE_SINGLE )
{
if ( reload_event ) {
if ( forceEventChange || mode != MODE_SINGLE ) {
//Info( "SQL:%s", sql );
if ( mysql_query( &dbconn, sql ) )
{
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
MYSQL_RES *result = mysql_store_result( &dbconn );
if ( !result )
{
if ( !result ) {
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
MYSQL_ROW dbrow = mysql_fetch_row( result );
if ( mysql_errno( &dbconn ) )
{
if ( mysql_errno( &dbconn ) ) {
Error( "Can't fetch row: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
if ( dbrow )
{
if ( dbrow ) {
int event_id = atoi(dbrow[0]);
Debug( 1, "Loading new event %d", event_id );
@ -1184,9 +1073,7 @@ void EventStream::checkEventLoaded()
else
curr_frame_id = 1;
Debug( 2, "New frame id = %d", curr_frame_id );
}
else
{
} else {
if ( curr_frame_id <= 0 )
curr_frame_id = 1;
else
@ -1195,9 +1082,7 @@ void EventStream::checkEventLoaded()
}
mysql_free_result( result );
forceEventChange = false;
}
else
{
} else {
if ( curr_frame_id <= 0 )
curr_frame_id = 1;
else
@ -1207,8 +1092,7 @@ void EventStream::checkEventLoaded()
}
}
bool EventStream::sendFrame( int delta_us )
{
bool EventStream::sendFrame( int delta_us ) {
Debug( 2, "Sending frame %d", curr_frame_id );
static char filepath[PATH_MAX];
@ -1218,21 +1102,18 @@ bool EventStream::sendFrame( int delta_us )
snprintf( filepath, sizeof(filepath), Event::capture_file_format, event_data->path, curr_frame_id );
#if HAVE_LIBAVCODEC
if ( type == STREAM_MPEG )
{
if ( type == STREAM_MPEG ) {
Image image( filepath );
Image *send_image = prepareImage( &image );
if ( !vid_stream )
{
if ( !vid_stream ) {
vid_stream = new VideoStream( "pipe:", format, bitrate, effective_fps, send_image->Colours(), send_image->SubpixelOrder(), send_image->Width(), send_image->Height() );
fprintf( stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType() );
vid_stream->OpenStream();
}
/* double pts = */ vid_stream->EncodeFrame( send_image->Buffer(), send_image->Size(), config.mpeg_timed_frames, delta_us*1000 );
}
else
} else
#endif // HAVE_LIBAVCODEC
{
static unsigned char temp_img_buffer[ZM_MAX_IMAGE_SIZE];
@ -1247,11 +1128,9 @@ bool EventStream::sendFrame( int delta_us )
if ( type != STREAM_JPEG )
send_raw = false;
if ( send_raw )
{
if ( send_raw ) {
fdj = fopen( filepath, "rb" );
if ( !fdj )
{
if ( !fdj ) {
Error( "Can't open %s: %s", filepath, strerror(errno) );
return( false );
}
@ -1263,15 +1142,12 @@ bool EventStream::sendFrame( int delta_us )
#else
img_buffer_size = fread( img_buffer, 1, sizeof(temp_img_buffer), fdj );
#endif
}
else
{
} else {
Image image( filepath );
Image *send_image = prepareImage( &image );
switch( type )
{
switch( type ) {
case STREAM_JPEG :
send_image->EncodeJpeg( img_buffer, &img_buffer_size );
break;
@ -1295,8 +1171,7 @@ bool EventStream::sendFrame( int delta_us )
}
}
switch( type )
{
switch( type ) {
case STREAM_JPEG :
fprintf( stdout, "Content-Type: image/jpeg\r\n" );
break;
@ -1333,8 +1208,7 @@ bool EventStream::sendFrame( int delta_us )
fclose(fdj); /* Close the file handle */
} else {
fprintf( stdout, "Content-Length: %d\r\n\r\n", img_buffer_size );
if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 )
{
if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 ) {
Error( "Unable to send stream frame: %s", strerror(errno) );
return( false );
}
@ -1347,8 +1221,7 @@ bool EventStream::sendFrame( int delta_us )
return( true );
}
void EventStream::runStream()
{
void EventStream::runStream() {
Event::Initialise();
openComms();
@ -1360,15 +1233,13 @@ void EventStream::runStream()
if ( type == STREAM_JPEG )
fprintf( stdout, "Content-Type: multipart/x-mixed-replace;boundary=ZoneMinderFrame\r\n\r\n" );
if ( !event_data )
{
if ( !event_data ) {
sendTextFrame( "No event data found" );
exit( 0 );
}
unsigned int delta_us = 0;
while( !zm_terminate )
{
while( !zm_terminate ) {
gettimeofday( &now, NULL );
while(checkCommandQueue());
@ -1384,27 +1255,21 @@ void EventStream::runStream()
//Info( "cst:%.2f", curr_stream_time );
//Info( "cfid:%d", curr_frame_id );
//Info( "fdt:%d", frame_data->timestamp );
if ( !paused )
{
if ( !paused ) {
bool in_event = true;
double time_to_event = 0;
if ( replay_rate > 0 )
{
if ( replay_rate > 0 ) {
time_to_event = event_data->frames[0].timestamp - curr_stream_time;
if ( time_to_event > 0 )
in_event = false;
}
else if ( replay_rate < 0 )
{
} else if ( replay_rate < 0 ) {
time_to_event = curr_stream_time - event_data->frames[event_data->frame_count-1].timestamp;
if ( time_to_event > 0 )
in_event = false;
}
if ( !in_event )
{
if ( !in_event ) {
double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
if ( actual_delta_time > 1 )
{
if ( actual_delta_time > 1 ) {
static char frame_text[64];
snprintf( frame_text, sizeof(frame_text), "Time to next event = %d seconds", (int)time_to_event );
if ( !sendTextFrame( frame_text ) )
@ -1422,11 +1287,9 @@ void EventStream::runStream()
// Figure out if we should send this frame
bool send_frame = false;
if ( !paused )
{
if ( !paused ) {
// If we are streaming and this frame is due to be sent
if ( ((curr_frame_id-1)%frame_mod) == 0 )
{
if ( ((curr_frame_id-1)%frame_mod) == 0 ) {
delta_us = (unsigned int)(frame_data->delta * 1000000);
// if effective > base we should speed up frame delivery
delta_us = (unsigned int)((delta_us * base_fps)/effective_fps);
@ -1434,19 +1297,14 @@ void EventStream::runStream()
delta_us = max(delta_us, 1000000 / maxfps);
send_frame = true;
}
}
else if ( step != 0 )
{
} else if ( step != 0 ) {
// We are paused and are just stepping forward or backward one frame
step = 0;
send_frame = true;
}
else
{
} else {
// We are paused, and doing nothing
double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
if ( actual_delta_time > MAX_STREAM_DELAY )
{
if ( actual_delta_time > MAX_STREAM_DELAY ) {
// Send keepalive
Debug( 2, "Sending keepalive frame" );
send_frame = true;
@ -1459,17 +1317,13 @@ void EventStream::runStream()
curr_stream_time = frame_data->timestamp;
if ( !paused )
{
if ( !paused ) {
curr_frame_id += replay_rate>0?1:-1;
if ( send_frame && type != STREAM_MPEG )
{
if ( send_frame && type != STREAM_MPEG ) {
Debug( 3, "dUs: %d", delta_us );
usleep( delta_us );
}
}
else
{
} else {
usleep( (unsigned long)((1000000 * ZM_RATE_BASE)/((base_fps?base_fps:1)*abs(replay_rate*2))) );
}
}

View File

@ -47,8 +47,7 @@ class Monitor;
//
// Class describing events, i.e. captured periods of activity.
//
class Event
{
class Event {
friend class EventStream;
protected:
@ -68,8 +67,7 @@ public:
protected:
typedef enum { NORMAL, BULK, ALARM } FrameType;
struct PreAlarmData
{
struct PreAlarmData {
Image *image;
struct timeval timestamp;
unsigned int score;
@ -103,8 +101,7 @@ protected:
int last_db_frame;
protected:
static void Initialise()
{
static void Initialise() {
if ( initialised )
return;
@ -148,33 +145,26 @@ private:
void AddFramesInternal( int n_frames, int start_frame, Image **images, struct timeval **timestamps );
public:
static const char *getSubPath( struct tm *time )
{
static const char *getSubPath( struct tm *time ) {
static char subpath[PATH_MAX] = "";
snprintf( subpath, sizeof(subpath), "%02d/%02d/%02d/%02d/%02d/%02d", time->tm_year-100, time->tm_mon+1, time->tm_mday, time->tm_hour, time->tm_min, time->tm_sec );
return( subpath );
}
static const char *getSubPath( time_t *time )
{
static const char *getSubPath( time_t *time ) {
return( Event::getSubPath( localtime( time ) ) );
}
char* getEventFile(void)
{
char* getEventFile(void) {
return video_file;
}
public:
static int PreAlarmCount()
{
static int PreAlarmCount() {
return( pre_alarm_count );
}
static void EmptyPreAlarmFrames()
{
if ( pre_alarm_count > 0 )
{
for ( int i = 0; i < MAX_PRE_ALARM_FRAMES; i++ )
{
static void EmptyPreAlarmFrames() {
if ( pre_alarm_count > 0 ) {
for ( int i = 0; i < MAX_PRE_ALARM_FRAMES; i++ ) {
delete pre_alarm_data[i].image;
delete pre_alarm_data[i].alarm_frame;
}
@ -182,29 +172,24 @@ public:
}
pre_alarm_count = 0;
}
static void AddPreAlarmFrame( Image *image, struct timeval timestamp, int score=0, Image *alarm_frame=NULL )
{
static void AddPreAlarmFrame( Image *image, struct timeval timestamp, int score=0, Image *alarm_frame=NULL ) {
pre_alarm_data[pre_alarm_count].image = new Image( *image );
pre_alarm_data[pre_alarm_count].timestamp = timestamp;
pre_alarm_data[pre_alarm_count].score = score;
if ( alarm_frame )
{
if ( alarm_frame ) {
pre_alarm_data[pre_alarm_count].alarm_frame = new Image( *alarm_frame );
}
pre_alarm_count++;
}
void SavePreAlarmFrames()
{
for ( int i = 0; i < pre_alarm_count; i++ )
{
void SavePreAlarmFrames() {
for ( int i = 0; i < pre_alarm_count; i++ ) {
AddFrame( pre_alarm_data[i].image, pre_alarm_data[i].timestamp, pre_alarm_data[i].score, pre_alarm_data[i].alarm_frame );
}
EmptyPreAlarmFrames();
}
};
class EventStream : public StreamBase
{
class EventStream : public StreamBase {
public:
typedef enum { MODE_SINGLE, MODE_ALL, MODE_ALL_GAPLESS } StreamMode;
@ -217,8 +202,7 @@ protected:
bool in_db;
};
struct EventData
{
struct EventData {
unsigned long event_id;
unsigned long monitor_id;
unsigned long frame_count;
@ -254,8 +238,7 @@ protected:
bool sendFrame( int delta_us );
public:
EventStream()
{
EventStream() {
mode = DEFAULT_MODE;
forceEventChange = false;
@ -265,18 +248,15 @@ public:
event_data = 0;
}
void setStreamStart( int init_event_id, unsigned int init_frame_id=0 )
{
void setStreamStart( int init_event_id, unsigned int init_frame_id=0 ) {
loadInitialEventData( init_event_id, init_frame_id );
loadMonitor( event_data->monitor_id );
}
void setStreamStart( int monitor_id, time_t event_time )
{
void setStreamStart( int monitor_id, time_t event_time ) {
loadInitialEventData( monitor_id, event_time );
loadMonitor( monitor_id );
}
void setStreamMode( StreamMode p_mode )
{
void setStreamMode( StreamMode p_mode ) {
mode = p_mode;
}
void runStream();

View File

@ -158,18 +158,10 @@ SWScale::SWScale() : gotdefaults(false), swscale_ctx(NULL), input_avframe(NULL),
SWScale::~SWScale() {
/* Free up everything */
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
av_frame_free( &input_avframe );
#else
av_freep( &input_avframe );
#endif
//input_avframe = NULL;
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
av_frame_free( &output_avframe );
#else
av_freep( &output_avframe );
#endif
//output_avframe = NULL;
if(swscale_ctx) {
@ -233,13 +225,14 @@ int SWScale::Convert(const uint8_t* in_buffer, const size_t in_buffer_size, uint
#else
size_t outsize = avpicture_get_size(out_pf, width, height);
#endif
if(outsize < out_buffer_size) {
Error("The output buffer is undersized for the output format. Required: %d Available: %d", outsize, out_buffer_size);
return -5;
}
/* Get the context */
swscale_ctx = sws_getCachedContext(swscale_ctx, width, height, in_pf, width, height, out_pf, 0, NULL, NULL, NULL);
swscale_ctx = sws_getCachedContext( swscale_ctx, width, height, in_pf, width, height, out_pf, SWS_FAST_BILINEAR, NULL, NULL, NULL );
if(swscale_ctx == NULL) {
Error("Failed getting swscale context");
return -6;
@ -366,6 +359,22 @@ int hacked_up_context2_for_older_ffmpeg(AVFormatContext **avctx, AVOutputFormat
}
}
if (!oformat) {
if (format) {
oformat = av_guess_format(format, NULL, NULL);
if (!oformat) {
av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
ret = AVERROR(EINVAL);
}
} else {
oformat = av_guess_format(NULL, filename, NULL);
if (!oformat) {
ret = AVERROR(EINVAL);
av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n", filename);
}
}
}
if (ret) {
avformat_free_context(s);
return ret;
@ -395,13 +404,13 @@ int hacked_up_context2_for_older_ffmpeg(AVFormatContext **avctx, AVOutputFormat
static void zm_log_fps(double d, const char *postfix) {
uint64_t v = lrintf(d * 100);
if (!v) {
Debug(3, "%1.4f %s", d, postfix);
Debug(1, "%1.4f %s", d, postfix);
} else if (v % 100) {
Debug(3, "%3.2f %s", d, postfix);
Debug(1, "%3.2f %s", d, postfix);
} else if (v % (100 * 1000)) {
Debug(3, "%1.0f %s", d, postfix);
Debug(1, "%1.0f %s", d, postfix);
} else
Debug(3, "%1.0fk %s", d / 1000, postfix);
Debug(1, "%1.0fk %s", d / 1000, postfix);
}
/* "user interface" functions */
@ -413,17 +422,16 @@ void zm_dump_stream_format(AVFormatContext *ic, int i, int index, int is_output)
AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
avcodec_string(buf, sizeof(buf), st->codec, is_output);
Debug(3, " Stream #%d:%d", index, i);
Debug(1, " Stream #%d:%d", index, i);
/* the pid is an important information, so we display it */
/* XXX: add a generic system */
if (flags & AVFMT_SHOW_IDS)
Debug(3, "[0x%x]", st->id);
Debug(1, "[0x%x]", st->id);
if (lang)
Debug(3, "(%s)", lang->value);
av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames,
st->time_base.num, st->time_base.den);
Debug(3, ": %s", buf);
Debug(1, "(%s)", lang->value);
Debug(1, ", %d, %d/%d", st->codec_info_nb_frames, st->time_base.num, st->time_base.den);
Debug(1, ": %s", buf);
if (st->sample_aspect_ratio.num && // default
av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
@ -432,7 +440,7 @@ void zm_dump_stream_format(AVFormatContext *ic, int i, int index, int is_output)
st->codec->width * (int64_t)st->sample_aspect_ratio.num,
st->codec->height * (int64_t)st->sample_aspect_ratio.den,
1024 * 1024);
Debug(3, ", SAR %d:%d DAR %d:%d",
Debug(1, ", SAR %d:%d DAR %d:%d",
st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
display_aspect_ratio.num, display_aspect_ratio.den);
}
@ -448,34 +456,56 @@ void zm_dump_stream_format(AVFormatContext *ic, int i, int index, int is_output)
if (fps)
zm_log_fps(av_q2d(st->avg_frame_rate), tbn || tbc ? "fps, " : "fps");
if (tbn)
zm_log_fps(1 / av_q2d(st->time_base), tbc ? "tbn, " : "tbn");
zm_log_fps(1 / av_q2d(st->time_base), tbc ? "stream tb numerator , " : "stream tb numerator");
if (tbc)
zm_log_fps(1 / av_q2d(st->codec->time_base), "tbc");
zm_log_fps(1 / av_q2d(st->codec->time_base), "codec time base:");
}
if (st->disposition & AV_DISPOSITION_DEFAULT)
Debug(3, " (default)");
Debug(1, " (default)");
if (st->disposition & AV_DISPOSITION_DUB)
Debug(3, " (dub)");
Debug(1, " (dub)");
if (st->disposition & AV_DISPOSITION_ORIGINAL)
Debug(3, " (original)");
Debug(1, " (original)");
if (st->disposition & AV_DISPOSITION_COMMENT)
Debug(3, " (comment)");
Debug(1, " (comment)");
if (st->disposition & AV_DISPOSITION_LYRICS)
Debug(3, " (lyrics)");
Debug(1, " (lyrics)");
if (st->disposition & AV_DISPOSITION_KARAOKE)
Debug(3, " (karaoke)");
Debug(1, " (karaoke)");
if (st->disposition & AV_DISPOSITION_FORCED)
Debug(3, " (forced)");
Debug(1, " (forced)");
if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
Debug(3, " (hearing impaired)");
Debug(1, " (hearing impaired)");
if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
Debug(3, " (visual impaired)");
Debug(1, " (visual impaired)");
if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
Debug(3, " (clean effects)");
Debug(3, "\n");
Debug(1, " (clean effects)");
Debug(1, "\n");
//dump_metadata(NULL, st->metadata, " ");
//dump_sidedata(NULL, st, " ");
}
int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt) {
const enum AVSampleFormat *p = codec->sample_fmts;
while (*p != AV_SAMPLE_FMT_NONE) {
if (*p == sample_fmt)
return 1;
else Debug(2, "Not %s", av_get_sample_fmt_name( *p ) );
p++;
}
return 0;
}
#if LIBAVCODEC_VERSION_CHECK(56, 8, 0, 60, 100)
#else
unsigned int zm_av_packet_ref( AVPacket *dst, AVPacket *src ) {
dst->data = reinterpret_cast<uint8_t*>(new uint64_t[(src->size + FF_INPUT_BUFFER_PADDING_SIZE)/sizeof(uint64_t) + 1]);
memcpy(dst->data, src->data, src->size );
return 0;
}
#endif

View File

@ -323,5 +323,29 @@ static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, in
#endif
void zm_dump_stream_format(AVFormatContext *ic, int i, int index, int is_output);
#if LIBAVCODEC_VERSION_CHECK(56, 8, 0, 60, 100)
#define zm_av_packet_unref( packet ) av_packet_unref( packet )
#define zm_av_packet_ref( dst, src ) av_packet_ref( dst, src )
#else
#define zm_av_packet_unref( packet ) av_free_packet( packet )
unsigned int zm_av_packet_ref( AVPacket *dst, AVPacket *src );
#endif
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
#define zm_avcodec_decode_video( context, rawFrame, frameComplete, packet ) avcodec_decode_video2( context, rawFrame, frameComplete, packet )
#else
#define zm_avcodec_decode_video(context, rawFrame, frameComplete, packet ) avcodec_decode_video( context, rawFrame, frameComplete, packet->data, packet->size)
#endif
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
#define zm_av_frame_alloc() av_frame_alloc()
#else
#define zm_av_frame_alloc() avcodec_alloc_frame()
#endif
#if ! LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
#define av_frame_free( input_avframe ) av_freep( input_avframe )
#endif
int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt);
#endif // ZM_FFMPEG_H

View File

@ -42,16 +42,17 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri
mMethod( p_method ),
mOptions( p_options )
{
if ( capture )
{
if ( capture ) {
Initialise();
}
mFormatContext = NULL;
mVideoStreamId = -1;
mAudioStreamId = -1;
mCodecContext = NULL;
mCodec = NULL;
mVideoCodecContext = NULL;
mAudioCodecContext = NULL;
mVideoCodec = NULL;
mAudioCodec = NULL;
mRawFrame = NULL;
mFrame = NULL;
frameCount = 0;
@ -60,8 +61,8 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri
mCanCapture = false;
mOpenStart = 0;
mReopenThread = 0;
wasRecording = false;
videoStore = NULL;
video_last_pts = 0;
#if HAVE_LIBSWSCALE
mConvertContext = NULL;
@ -82,32 +83,32 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri
}
FfmpegCamera::~FfmpegCamera()
{
FfmpegCamera::~FfmpegCamera() {
if ( videoStore ) {
delete videoStore;
}
CloseFfmpeg();
if ( capture )
{
if ( capture ) {
Terminate();
}
}
void FfmpegCamera::Initialise()
{
void FfmpegCamera::Initialise() {
if ( logDebugging() )
av_log_set_level( AV_LOG_DEBUG );
else
av_log_set_level( AV_LOG_QUIET );
av_register_all();
avformat_network_init();
}
void FfmpegCamera::Terminate()
{
void FfmpegCamera::Terminate() {
}
int FfmpegCamera::PrimeCapture()
{
int FfmpegCamera::PrimeCapture() {
mVideoStreamId = -1;
mAudioStreamId = -1;
Info( "Priming capture from %s", mPath.c_str() );
@ -144,7 +145,40 @@ int FfmpegCamera::Capture( Image &image )
mReopenThread = 0;
}
AVPacket packet;
int frameComplete = false;
while ( !frameComplete ) {
int avResult = av_read_frame( mFormatContext, &packet );
if ( avResult < 0 ) {
char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if (
// Check if EOF.
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure.
(avResult == -110)
) {
Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf );
ReopenFfmpeg();
}
Error( "Unable to read packet from stream %d: error %d \"%s\".", packet.stream_index, avResult, errbuf );
return( -1 );
}
Debug( 5, "Got packet from stream %d dts (%d) pts(%d)", packet.stream_index, packet.pts, packet.dts );
// What about audio stream? Maybe someday we could do sound detection...
if ( packet.stream_index == mVideoStreamId ) {
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if (avcodec_decode_video2(mVideoCodecContext, mRawFrame, &frameComplete, &packet) < 0)
#else
if (avcodec_decode_video(mVideoCodecContext, mRawFrame, &frameComplete, packet.data, packet.size) < 0)
#endif
Fatal( "Unable to decode frame at frame %d", frameCount );
Debug( 4, "Decoded video packet at frame %d", frameCount );
if ( frameComplete ) {
Debug( 4, "Got frame %d", frameCount );
uint8_t* directbuffer;
/* Request a writeable buffer of the target image */
@ -154,44 +188,6 @@ int FfmpegCamera::Capture( Image &image )
return (-1);
}
int frameComplete = false;
while ( !frameComplete )
{
int avResult = av_read_frame( mFormatContext, &packet );
if ( avResult < 0 )
{
char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if (
// Check if EOF.
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure.
(avResult == -110)
)
{
Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf);
ReopenFfmpeg();
}
Error( "Unable to read packet from stream %d: error %d \"%s\".", packet.stream_index, avResult, errbuf );
return( -1 );
}
Debug( 5, "Got packet from stream %d", packet.stream_index );
// What about audio stream? Maybe someday we could do sound detection...
if ( packet.stream_index == mVideoStreamId )
{
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
#else
if ( avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size ) < 0 )
#endif
Fatal( "Unable to decode frame at frame %d", frameCount );
Debug( 4, "Decoded video packet at frame %d", frameCount );
if ( frameComplete )
{
Debug( 3, "Got frame %d", frameCount );
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(mFrame->data, mFrame->linesize,
directbuffer, imagePixFormat, width, height, 1);
@ -202,14 +198,18 @@ int FfmpegCamera::Capture( Image &image )
#if HAVE_LIBSWSCALE
if(mConvertContext == NULL) {
mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL );
mConvertContext = sws_getContext(mVideoCodecContext->width,
mVideoCodecContext->height,
mVideoCodecContext->pix_fmt,
width, height, imagePixFormat,
SWS_BICUBIC, NULL, NULL, NULL);
if(mConvertContext == NULL)
Fatal( "Unable to create conversion context for %s", mPath.c_str() );
}
if ( sws_scale( mConvertContext, mRawFrame->data, mRawFrame->linesize, 0, mCodecContext->height, mFrame->data, mFrame->linesize ) < 0 )
Fatal( "Unable to convert raw format %u to target format %u at frame %d", mCodecContext->pix_fmt, imagePixFormat, frameCount );
if (sws_scale(mConvertContext, mRawFrame->data, mRawFrame->linesize, 0, mVideoCodecContext->height, mFrame->data, mFrame->linesize) < 0)
Fatal("Unable to convert raw format %u to target format %u at frame %d", mVideoCodecContext->pix_fmt, imagePixFormat, frameCount);
#else // HAVE_LIBSWSCALE
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
#endif // HAVE_LIBSWSCALE
@ -219,17 +219,12 @@ int FfmpegCamera::Capture( Image &image )
} else {
Debug( 4, "Different stream_index %d", packet.stream_index );
} // end if packet.stream_index == mVideoStreamId
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
av_packet_unref( &packet);
#else
av_free_packet( &packet );
#endif
zm_av_packet_unref( &packet );
} // end while ! frameComplete
return (0);
} // FfmpegCamera::Capture
int FfmpegCamera::PostCapture()
{
int FfmpegCamera::PostCapture() {
// Nothing to do here
return( 0 );
}
@ -291,7 +286,6 @@ int FfmpegCamera::OpenFfmpeg() {
Debug ( 1, "Opened input" );
Info( "Stream open %s", mPath.c_str() );
startTime=av_gettime();//FIXME here or after find_Stream_info
//FIXME can speed up initial analysis but need sensible parameters...
//mFormatContext->probesize = 32;
@ -306,19 +300,19 @@ int FfmpegCamera::OpenFfmpeg() {
#endif
Fatal( "Unable to find stream info from %s due to: %s", mPath.c_str(), strerror(errno) );
startTime = av_gettime();//FIXME here or after find_Stream_info
Debug ( 1, "Got stream info" );
// 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++ )
{
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 ( mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO )
if ( mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ) {
#else
if ( mFormatContext->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO )
if ( mFormatContext->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO ) {
#endif
{
if ( mVideoStreamId == -1 ) {
mVideoStreamId = i;
// if we break, then we won't find the audio stream
@ -328,59 +322,74 @@ int FfmpegCamera::OpenFfmpeg() {
}
}
#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_AUDIO )
if ( mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO ) {
#else
if ( mFormatContext->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
if ( mFormatContext->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO ) {
#endif
{
if ( mAudioStreamId == -1 ) {
mAudioStreamId = i;
} else {
Debug(2, "Have another audio stream." );
}
}
}
} // end foreach stream
if ( mVideoStreamId == -1 )
Fatal( "Unable to locate video stream in %s", mPath.c_str() );
if ( mAudioStreamId == -1 )
Debug( 3, "Unable to locate audio stream in %s", mPath.c_str() );
Debug ( 1, "Found video stream" );
Debug ( 3, "Found video stream at index %d", mVideoStreamId );
Debug ( 3, "Found audio stream at index %d", mAudioStreamId );
mCodecContext = mFormatContext->streams[mVideoStreamId]->codec;
mVideoCodecContext = mFormatContext->streams[mVideoStreamId]->codec;
// STolen from ispy
//this fixes issues with rtsp streams!! woot.
//mVideoCodecContext->flags2 |= CODEC_FLAG2_FAST | CODEC_FLAG2_CHUNKS | CODEC_FLAG_LOW_DELAY; // Enable faster H264 decode.
mVideoCodecContext->flags2 |= CODEC_FLAG2_FAST | CODEC_FLAG_LOW_DELAY;
// Try and get the codec from the codec context
if ( (mCodec = avcodec_find_decoder( mCodecContext->codec_id )) == NULL )
if ((mVideoCodec = avcodec_find_decoder(mVideoCodecContext->codec_id)) == NULL) {
Fatal("Can't find codec for video stream from %s", mPath.c_str());
Debug ( 1, "Found decoder" );
} else {
Debug(1, "Video Found decoder");
zm_dump_stream_format(mFormatContext, mVideoStreamId, 0, 0);
// Open the codec
#if !LIBAVFORMAT_VERSION_CHECK(53, 8, 0, 8, 0)
Debug ( 1, "Calling avcodec_open" );
if ( avcodec_open( mCodecContext, mCodec ) < 0 )
if (avcodec_open(mVideoCodecContext, mVideoCodec) < 0)
#else
Debug ( 1, "Calling avcodec_open2" );
if ( avcodec_open2( mCodecContext, mCodec, 0 ) < 0 )
if (avcodec_open2(mVideoCodecContext, mVideoCodec, 0) < 0)
#endif
Fatal( "Unable to open codec for video stream from %s", mPath.c_str() );
}
if (mAudioStreamId >= 0) {
mAudioCodecContext = mFormatContext->streams[mAudioStreamId]->codec;
if ((mAudioCodec = avcodec_find_decoder(mAudioCodecContext->codec_id)) == NULL) {
Debug(1, "Can't find codec for audio stream from %s", mPath.c_str());
} else {
Debug(1, "Audio Found decoder");
zm_dump_stream_format(mFormatContext, mAudioStreamId, 0, 0);
// Open the codec
#if !LIBAVFORMAT_VERSION_CHECK(53, 8, 0, 8, 0)
Debug ( 1, "Calling avcodec_open" );
if (avcodec_open(mAudioCodecContext, mAudioCodec) < 0)
#else
Debug ( 1, "Calling avcodec_open2" );
if (avcodec_open2(mAudioCodecContext, mAudioCodec, 0) < 0)
#endif
Fatal( "Unable to open codec for video stream from %s", mPath.c_str() );
}
}
Debug ( 1, "Opened codec" );
// Allocate space for the native video frame
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
mRawFrame = av_frame_alloc();
#else
mRawFrame = avcodec_alloc_frame();
#endif
mRawFrame = zm_av_frame_alloc();
// Allocate space for the converted video frame
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
mFrame = av_frame_alloc();
#else
mFrame = avcodec_alloc_frame();
#endif
mFrame = zm_av_frame_alloc();
if(mRawFrame == NULL || mFrame == NULL)
Fatal( "Unable to allocate frame for %s", mPath.c_str() );
@ -401,18 +410,30 @@ int FfmpegCamera::OpenFfmpeg() {
#if HAVE_LIBSWSCALE
Debug ( 1, "Calling sws_isSupportedInput" );
if(!sws_isSupportedInput(mCodecContext->pix_fmt)) {
Fatal("swscale does not support the codec format: %c%c%c%c",(mCodecContext->pix_fmt)&0xff,((mCodecContext->pix_fmt>>8)&0xff),((mCodecContext->pix_fmt>>16)&0xff),((mCodecContext->pix_fmt>>24)&0xff));
if (!sws_isSupportedInput(mVideoCodecContext->pix_fmt)) {
Fatal("swscale does not support the codec format: %c%c%c%c", (mVideoCodecContext->pix_fmt)&0xff, ((mVideoCodecContext->pix_fmt >> 8)&0xff), ((mVideoCodecContext->pix_fmt >> 16)&0xff), ((mVideoCodecContext->pix_fmt >> 24)&0xff));
}
if(!sws_isSupportedOutput(imagePixFormat)) {
Fatal("swscale does not support the target format: %c%c%c%c",(imagePixFormat)&0xff,((imagePixFormat>>8)&0xff),((imagePixFormat>>16)&0xff),((imagePixFormat>>24)&0xff));
}
mConvertContext = sws_getContext(mVideoCodecContext->width,
mVideoCodecContext->height,
mVideoCodecContext->pix_fmt,
width, height,
imagePixFormat, SWS_BICUBIC, NULL,
NULL, NULL);
if ( mConvertContext == NULL )
Fatal( "Unable to create conversion context for %s", mPath.c_str() );
#else // HAVE_LIBSWSCALE
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
#endif // HAVE_LIBSWSCALE
if ( (unsigned int)mVideoCodecContext->width != width || (unsigned int)mVideoCodecContext->height != height ) {
Warning( "Monitor dimensions are %dx%d but camera is sending %dx%d", width, height, mVideoCodecContext->width, mVideoCodecContext->height );
}
mCanCapture = true;
return 0;
@ -437,13 +458,8 @@ int FfmpegCamera::CloseFfmpeg(){
mCanCapture = false;
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
av_frame_free( &mFrame );
av_frame_free( &mRawFrame );
#else
av_freep( &mFrame );
av_freep( &mRawFrame );
#endif
#if HAVE_LIBSWSCALE
if ( mConvertContext )
@ -453,13 +469,16 @@ int FfmpegCamera::CloseFfmpeg(){
}
#endif
if ( mCodecContext )
{
avcodec_close( mCodecContext );
mCodecContext = NULL; // Freed by av_close_input_file
if (mVideoCodecContext) {
avcodec_close(mVideoCodecContext);
mVideoCodecContext = NULL; // Freed by av_close_input_file
}
if ( mFormatContext )
{
if (mAudioCodecContext) {
avcodec_close(mAudioCodecContext);
mAudioCodecContext = NULL; // Freed by av_close_input_file
}
if ( mFormatContext ) {
#if !LIBAVFORMAT_VERSION_CHECK(53, 17, 0, 25, 0)
av_close_input_file( mFormatContext );
#else
@ -509,15 +528,16 @@ void *FfmpegCamera::ReopenFfmpegThreadCallback(void *ctx){
}
//Function to handle capture and store
int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_file ){
int FfmpegCamera::CaptureAndRecord( Image &image, timeval recording, char* event_file ) {
if (!mCanCapture){
return -1;
}
int ret;
static char errbuf[AV_ERROR_MAX_STRING_SIZE];
// If the reopen thread has a value, but mCanCapture != 0, then we have just reopened the connection to the ffmpeg device, and we can clean up the thread.
if (mReopenThread != 0) {
void *retval = 0;
int ret;
ret = pthread_join(mReopenThread, &retval);
if (ret != 0){
@ -528,66 +548,63 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
mReopenThread = 0;
}
AVPacket packet;
uint8_t* directbuffer;
/* Request a writeable buffer of the target image */
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
if( directbuffer == NULL ) {
Error("Failed requesting writeable buffer for the captured image.");
return (-1);
}
if ( mCodecContext->codec_id != AV_CODEC_ID_H264 ) {
if (mVideoCodecContext->codec_id != AV_CODEC_ID_H264) {
Error( "Input stream is not h264. The stored event file may not be viewable in browser." );
}
int frameComplete = false;
while ( ! frameComplete ) {
int avResult = av_read_frame( mFormatContext, &packet );
if ( avResult < 0 ) {
char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
av_init_packet( &packet );
ret = av_read_frame( mFormatContext, &packet );
if ( ret < 0 ) {
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
if (
// Check if EOF.
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
(ret == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure.
(avResult == -110)
(ret == -110)
) {
Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf);
ReopenFfmpeg();
}
Error( "Unable to read packet from stream %d: error %d \"%s\".", packet.stream_index, avResult, errbuf );
Error( "Unable to read packet from stream %d: error %d \"%s\".", packet.stream_index, ret, errbuf );
return( -1 );
}
Debug( 5, "Got packet from stream %d", packet.stream_index );
if ( packet.stream_index == mVideoStreamId ) {
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
#else
if ( avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size ) < 0 )
#endif
{
Error( "Unable to decode frame at frame %d, continuing...", frameCount );
av_free_packet( &packet );
continue;
}
Debug( 4, "Decoded video packet at frame %d", frameCount );
int key_frame = packet.flags & AV_PKT_FLAG_KEY;
if ( frameComplete ) {
Debug( 3, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
//Keep the last keyframe so we can establish immediate video
/*if(packet.flags & AV_PKT_FLAG_KEY)
av_copy_packet(&lastKeyframePkt, &packet);*/
//TODO I think we need to store the key frame location for seeking as part of the event
Debug( 4, "Got packet from stream %d packet pts (%d) dts(%d), key?(%d)",
packet.stream_index, packet.pts, packet.dts,
key_frame
);
//Video recording
if ( recording && !wasRecording ) {
if ( recording.tv_sec ) {
// The directory we are recording to is no longer tied to the current event.
// Need to re-init the videostore with the correct directory and start recording again
// for efficiency's sake, we should test for keyframe before we test for directory change...
if ( videoStore && key_frame && (strcmp(oldDirectory, event_file) != 0 ) ) {
// don't open new videostore until we're on a key frame..would this require an offset adjustment for the event as a result?...
// if we store our key frame location with the event will that be enough?
Info("Re-starting video storage module");
// I don't know if this is important or not... but I figure we might as well write this last packet out to the store before closing it.
// Also don't know how much it matters for audio.
if ( packet.stream_index == mVideoStreamId ) {
//Write the packet to our video store
int ret = videoStore->writeVideoFramePacket( &packet );
if ( ret < 0 ) { //Less than zero and we skipped a frame
Warning("Error writing last packet to videostore.");
}
} // end if video
delete videoStore;
videoStore = NULL;
} // end if end of recording
if ( ( ! videoStore ) && key_frame && ( packet.stream_index == mVideoStreamId ) ) {
//Instantiate the video storage module
if (record_audio) {
@ -597,7 +614,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
mFormatContext->streams[mVideoStreamId],
NULL,
startTime,
this->getMonitor()->getOrientation());
this->getMonitor());
} else {
Debug(3, "Video module initiated with audio stream");
@ -605,7 +622,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
mFormatContext->streams[mVideoStreamId],
mFormatContext->streams[mAudioStreamId],
startTime,
this->getMonitor()->getOrientation());
this->getMonitor());
}
} else {
Debug(3, "Record_audio is false so exclude audio stream");
@ -613,107 +630,172 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
mFormatContext->streams[mVideoStreamId],
NULL,
startTime,
this->getMonitor()->getOrientation());
}
wasRecording = true;
this->getMonitor());
} // end if record_audio
strcpy(oldDirectory, event_file);
} else if ( ( ! recording ) && wasRecording && videoStore ) {
// Need to write out all the frames from the last keyframe?
// No... need to write out all frames from when the event began. Due to PreEventFrames, this could be more than since the last keyframe.
unsigned int packet_count = 0;
ZMPacket *queued_packet;
packetqueue.clear_unwanted_packets( &recording, mVideoStreamId );
while ( ( queued_packet = packetqueue.popPacket() ) ) {
AVPacket *avp = queued_packet->av_packet();
packet_count += 1;
//Write the packet to our video store
Debug(2, "Writing queued packet stream: %d KEY %d, remaining (%d)", avp->stream_index, avp->flags & AV_PKT_FLAG_KEY, packetqueue.size() );
if ( avp->stream_index == mVideoStreamId ) {
ret = videoStore->writeVideoFramePacket( avp );
} else if ( avp->stream_index == mAudioStreamId ) {
ret = videoStore->writeAudioFramePacket( avp );
} else {
Warning("Unknown stream id in queued packet (%d)", avp->stream_index );
ret = -1;
}
if ( ret < 0 ) {
//Less than zero and we skipped a frame
}
delete queued_packet;
} // end while packets in the packetqueue
Debug(2, "Wrote %d queued packets", packet_count );
} // end if ! was recording
} else {
// Not recording
if ( videoStore ) {
Info("Deleting videoStore instance");
delete videoStore;
videoStore = NULL;
}
// The directory we are recording to is no longer tied to the current
// event. Need to re-init the videostore with the correct directory and
// start recording again
if (recording && wasRecording && (strcmp(oldDirectory, event_file) != 0)
&& (packet.flags & AV_PKT_FLAG_KEY)) {
// Don't open new videostore until we're on a key frame..would this
// require an offset adjustment for the event as a result?...if we store
// our key frame location with the event will that be enough?
Info("Re-starting video storage module");
// Buffer video packets, since we are not recording.
// All audio packets are keyframes, so only if it's a video keyframe
if ( packet.stream_index == mVideoStreamId) {
if ( key_frame ) {
Debug(3, "Clearing queue");
packetqueue.clearQueue( monitor->GetPreEventCount(), mVideoStreamId );
}
#if 0
// Not sure this is valid. While a camera will PROBABLY always have an increasing pts... it doesn't have to.
// Also, I think there are integer wrap-around issues.
else if ( packet.pts && video_last_pts > packet.pts ) {
Warning( "Clearing queue due to out of order pts packet.pts(%d) < video_last_pts(%d)");
packetqueue.clearQueue();
}
#endif
}
if (
( packet.stream_index != mAudioStreamId || record_audio )
&&
( key_frame || packetqueue.size() )
) {
packetqueue.queuePacket( &packet );
}
} // end if recording or not
if ( packet.stream_index == mVideoStreamId ) {
if ( videoStore ) {
delete videoStore;
videoStore = NULL;
}
if (record_audio) {
if (mAudioStreamId == -1) {
Debug(3, "Record Audio on but no audio stream found");
videoStore = new VideoStore((const char *) event_file, "mp4",
mFormatContext->streams[mVideoStreamId],
NULL,
startTime,
this->getMonitor()->getOrientation());
} else {
Debug(3, "Video module initiated with audio stream");
videoStore = new VideoStore((const char *) event_file, "mp4",
mFormatContext->streams[mVideoStreamId],
mFormatContext->streams[mAudioStreamId],
startTime,
this->getMonitor()->getOrientation());
}
} else {
Debug(3, "Record_audio is false so exclude audio stream");
videoStore = new VideoStore((const char *) event_file, "mp4",
mFormatContext->streams[mVideoStreamId],
NULL, startTime,
this->getMonitor()->getOrientation());
}
strcpy(oldDirectory, event_file);
}
if ( videoStore && recording ) {
//Write the packet to our video store
int ret = videoStore->writeVideoFramePacket(&packet,
mFormatContext->streams[mVideoStreamId]); //, &lastKeyframePkt);
int ret = videoStore->writeVideoFramePacket( &packet );
if ( ret < 0 ) { //Less than zero and we skipped a frame
av_free_packet( &packet );
zm_av_packet_unref( &packet );
return 0;
}
}
Debug(4, "about to decode video" );
#if HAVE_LIBSWSCALE
if ( mConvertContext == NULL ) {
mConvertContext = sws_getContext(mCodecContext->width,
mCodecContext->height,
mCodecContext->pix_fmt,
width, height,
imagePixFormat, SWS_BICUBIC, NULL,
NULL, NULL);
if ( mConvertContext == NULL )
Fatal( "Unable to create conversion context for %s", mPath.c_str() );
#if LIBAVCODEC_VERSION_CHECK(58, 0, 0, 0, 0)
ret = avcodec_send_packet( mVideoCodecContext, &packet );
if ( ret < 0 ) {
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
Error( "Unable to send packet at frame %d: %s, continuing", frameCount, errbuf );
zm_av_packet_unref( &packet );
continue;
}
ret = avcodec_receive_frame( mVideoCodecContext, mRawFrame );
if ( ret < 0 ) {
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
Error( "Unable to send packet at frame %d: %s, continuing", frameCount, errbuf );
zm_av_packet_unref( &packet );
continue;
}
frameComplete = 1;
# else
ret = zm_avcodec_decode_video( mVideoCodecContext, mRawFrame, &frameComplete, &packet );
if ( ret < 0 ) {
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
Error( "Unable to decode frame at frame %d: %s, continuing", frameCount, errbuf );
zm_av_packet_unref( &packet );
continue;
}
#endif
Debug( 4, "Decoded video packet at frame %d", frameCount );
if ( frameComplete ) {
Debug( 4, "Got frame %d", frameCount );
uint8_t* directbuffer;
/* Request a writeable buffer of the target image */
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
if ( directbuffer == NULL ) {
Error("Failed requesting writeable buffer for the captured image.");
zm_av_packet_unref( &packet );
return (-1);
}
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(mFrame->data, mFrame->linesize, directbuffer, imagePixFormat, width, height, 1);
#else
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
#endif
if (sws_scale(mConvertContext, mRawFrame->data, mRawFrame->linesize,
0, mCodecContext->height, mFrame->data, mFrame->linesize) < 0)
0, mVideoCodecContext->height, mFrame->data, mFrame->linesize) < 0) {
Fatal("Unable to convert raw format %u to target format %u at frame %d",
mCodecContext->pix_fmt, imagePixFormat, frameCount);
#else // HAVE_LIBSWSCALE
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
#endif // HAVE_LIBSWSCALE
mVideoCodecContext->pix_fmt, imagePixFormat, frameCount);
}
frameCount++;
} else {
Debug( 3, "Not framecomplete after av_read_frame" );
} // end if frameComplete
} else if ( packet.stream_index == mAudioStreamId ) { //FIXME best way to copy all other streams
if ( videoStore && recording ) {
if ( videoStore ) {
if ( record_audio ) {
Debug(4, "Recording audio packet" );
Debug(3, "Recording audio packet streamindex(%d) packetstreamindex(%d)", mAudioStreamId, packet.stream_index );
//Write the packet to our video store
int ret = videoStore->writeAudioFramePacket(&packet,
mFormatContext->streams[packet.stream_index]); //FIXME no relevance of last key frame
//FIXME no relevance of last key frame
int ret = videoStore->writeAudioFramePacket( &packet );
if ( ret < 0 ) {//Less than zero and we skipped a frame
av_free_packet( &packet );
Warning("Failure to write audio packet.");
zm_av_packet_unref( &packet );
return 0;
}
} else {
Debug(4, "Not recording audio packet" );
}
}
} else {
#if LIBAVUTIL_VERSION_CHECK(56, 23, 0, 23, 0)
Debug( 3, "Some other stream index %d, %s", packet.stream_index, av_get_media_type_string( mFormatContext->streams[packet.stream_index]->codecpar->codec_type) );
#else
Debug( 3, "Some other stream index %d", packet.stream_index );
#endif
}
av_free_packet( &packet );
//if ( videoStore ) {
// the packet contents are ref counted... when queuing, we allocate another packet and reference it with that one, so we should always need to unref here, which should not affect the queued version.
zm_av_packet_unref( &packet );
//}
} // end while ! frameComplete
return (frameCount);
}
} // end FfmpegCamera::CaptureAndRecord
#endif // HAVE_LIBAVFORMAT

View File

@ -26,13 +26,13 @@
//#include "zm_utils.h"
#include "zm_ffmpeg.h"
#include "zm_videostore.h"
#include "zm_packetqueue.h"
//
// Class representing 'ffmpeg' cameras, i.e. those which are
// accessed using ffmpeg multimedia framework
//
class FfmpegCamera : public Camera
{
class FfmpegCamera : public Camera {
protected:
std::string mPath;
std::string mMethod;
@ -44,12 +44,25 @@ protected:
AVFormatContext *mFormatContext;
int mVideoStreamId;
int mAudioStreamId;
AVCodecContext *mCodecContext;
AVCodec *mCodec;
AVCodecContext *mVideoCodecContext;
AVCodecContext *mAudioCodecContext;
AVCodec *mVideoCodec;
AVCodec *mAudioCodec;
AVFrame *mRawFrame;
AVFrame *mFrame;
_AVPIXELFORMAT imagePixFormat;
// Need to keep track of these because apparently the stream can start with values for pts/dts and then subsequent packets start at zero.
int64_t audio_last_pts;
int64_t audio_last_dts;
int64_t video_last_pts;
int64_t video_last_dts;
// Used to store the incoming packet, it will get copied when queued.
// We only ever need one at a time, so instead of constantly allocating
// and freeing this structure, we will just make it a member of the object.
AVPacket packet;
int OpenFfmpeg();
int ReopenFfmpeg();
int CloseFfmpeg();
@ -61,10 +74,10 @@ protected:
pthread_t mReopenThread;
#endif // HAVE_LIBAVFORMAT
bool wasRecording;
VideoStore *videoStore;
char oldDirectory[4096];
//AVPacket lastKeyframePkt;
unsigned int old_event_id;
zm_packetqueue packetqueue;
#if HAVE_LIBSWSCALE
struct SwsContext *mConvertContext;
@ -86,7 +99,7 @@ public:
int PrimeCapture();
int PreCapture();
int Capture( Image &image );
int CaptureAndRecord( Image &image, bool recording, char* event_directory );
int CaptureAndRecord( Image &image, timeval recording, char* event_directory );
int PostCapture();
};

View File

@ -23,6 +23,7 @@
#include "zm_camera.h"
#include "zm_buffer.h"
#include "zm_regexp.h"
#include "zm_packetqueue.h"
#include <sys/param.h>
@ -46,7 +47,7 @@ public:
int PreCapture();
int Capture( Image &image );
int PostCapture();
int CaptureAndRecord( Image &image, bool recording, char* event_directory ) {return(0);};
int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) {return(0);};
};
#endif // ZM_FILE_CAMERA_H

View File

@ -74,8 +74,7 @@ static deinterlace_4field_fptr_t fptr_deinterlace_4field_gray8;
/* Pointer to image buffer memory copy function */
imgbufcpy_fptr_t fptr_imgbufcpy;
Image::Image()
{
Image::Image() {
if ( !initialised )
Initialise();
width = 0;
@ -91,8 +90,7 @@ Image::Image()
text[0] = '\0';
}
Image::Image( const char *filename )
{
Image::Image( const char *filename ) {
if ( !initialised )
Initialise();
width = 0;
@ -1015,8 +1013,7 @@ bool Image::WriteJpeg( const char *filename, int quality_override, struct timeva
cinfo->dct_method = JDCT_FASTEST;
jpeg_start_compress( cinfo, TRUE );
if ( config.add_jpeg_comments && text[0] )
{
if ( config.add_jpeg_comments && text[0] ) {
jpeg_write_marker( cinfo, JPEG_COM, (const JOCTET *)text, strlen(text) );
}
// If we have a non-zero time (meaning a parameter was passed in), then form a simple exif segment with that time as DateTimeOriginal and SubsecTimeOriginal
@ -1029,6 +1026,7 @@ bool Image::WriteJpeg( const char *filename, int quality_override, struct timeva
#define EXIFTIMES_LEN 0x13 // = 19
#define EXIF_CODE 0xE1
// This is a lot of stuff to allocate on the stack. Recommend char *timebuf[64];
char timebuf[64], msbuf[64];
strftime(timebuf, sizeof timebuf, "%Y:%m:%d %H:%M:%S", localtime(&(timestamp.tv_sec)));
snprintf(msbuf, sizeof msbuf, "%06d",(int)(timestamp.tv_usec)); // we only use milliseconds because that's all defined in exif, but this is the whole microseconds because we have it
@ -1729,6 +1727,8 @@ Image *Image::Highlight( unsigned int n_images, Image *images[], const Rgb thres
unsigned int size = result->size;
for ( unsigned int c = 0; c < colours; c++ )
{
unsigned int ref_colour_rgb = RGB_VAL(ref_colour,c);
for ( unsigned int i = 0; i < size; i++ )
{
unsigned int count = 0;
@ -1737,7 +1737,7 @@ Image *Image::Highlight( unsigned int n_images, Image *images[], const Rgb thres
{
uint8_t *psrc = images[j]->buffer+c;
unsigned int diff = ((*psrc)-RGB_VAL(ref_colour,c)) > 0 ? (*psrc)-RGB_VAL(ref_colour,c) : RGB_VAL(ref_colour,c) - (*psrc);
unsigned int diff = ((*psrc)-ref_colour_rgb) > 0 ? (*psrc)-ref_colour_rgb : ref_colour_rgb - (*psrc);
if (diff >= RGB_VAL(threshold,c))
{
@ -2074,18 +2074,14 @@ void Image::Annotate( const char *p_text, const Coord &coord, const unsigned int
}
}
void Image::Timestamp( const char *label, const time_t when, const Coord &coord, const int size )
{
void Image::Timestamp( const char *label, const time_t when, const Coord &coord, const int size ) {
char time_text[64];
strftime( time_text, sizeof(time_text), "%y/%m/%d %H:%M:%S", localtime( &when ) );
char text[64];
if ( label )
{
if ( label ) {
snprintf( text, sizeof(text), "%s - %s", label, time_text );
Annotate( text, coord, size );
}
else
{
} else {
Annotate( time_text, coord, size );
}
}

View File

@ -212,7 +212,7 @@ int LibvlcCamera::Capture( Image &image )
}
// Should not return -1 as cancels capture. Always wait for image if available.
int LibvlcCamera::CaptureAndRecord( Image &image, bool recording, char* event_directory )
int LibvlcCamera::CaptureAndRecord(Image &image, timeval recording, char* event_directory)
{
while(!mLibvlcData.newImage.getValueImmediate())
mLibvlcData.newImage.getUpdatedValue(1);

View File

@ -70,7 +70,7 @@ public:
int PrimeCapture();
int PreCapture();
int Capture( Image &image );
int CaptureAndRecord( Image &image, bool recording, char* event_directory );
int CaptureAndRecord( Image &image, timeval recording, char* event_directory );
int PostCapture();
};

View File

@ -677,11 +677,7 @@ LocalCamera::~LocalCamera()
sws_freeContext(imgConversionContext);
imgConversionContext = NULL;
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
av_frame_free( &tmpPicture );
#else
av_freep( &tmpPicture );
#endif
}
#endif
}
@ -810,7 +806,7 @@ void LocalCamera::Initialise()
Debug(3,"Failed to get updated JPEG compression options: %s", strerror(errno) );
} else {
Debug(4, "JPEG quality: %d",jpeg_comp.quality);
Debug(4, "JPEG markers: 0x%x",jpeg_comp.jpeg_markers);
Debug(4, "JPEG markers: %#x",jpeg_comp.jpeg_markers);
}
}
}

View File

@ -23,6 +23,7 @@
#include "zm.h"
#include "zm_camera.h"
#include "zm_image.h"
#include "zm_packetqueue.h"
#if ZM_HAS_V4L
@ -161,7 +162,7 @@ public:
int PreCapture();
int Capture( Image &image );
int PostCapture();
int CaptureAndRecord( Image &image, bool recording, char* event_directory ) {return(0);};
int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) {return(0);};
static bool GetCurrentSettings( const char *device, char *output, int version, bool verbose );
};

File diff suppressed because it is too large Load Diff

View File

@ -46,20 +46,17 @@ class Monitor;
// This is the main class for monitors. Each monitor is associated
// with a camera and is effectively a collector for events.
//
class Monitor
{
class Monitor {
friend class MonitorStream;
public:
typedef enum
{
typedef enum {
QUERY=0,
CAPTURE,
ANALYSIS
} Purpose;
typedef enum
{
typedef enum {
NONE=1,
MONITOR,
MODECT,
@ -68,8 +65,7 @@ public:
NODECT
} Function;
typedef enum
{
typedef enum {
ROTATE_0=1,
ROTATE_90,
ROTATE_180,
@ -78,8 +74,7 @@ public:
FLIP_VERT
} Orientation;
typedef enum
{
typedef enum {
IDLE,
PREALARM,
ALARM,
@ -87,6 +82,12 @@ public:
TAPE
} State;
typedef enum {
DISABLED,
X264ENCODE,
H264PASSTHROUGH,
} VideoWriter;
protected:
typedef std::set<Zone *> ZoneSet;
@ -95,8 +96,7 @@ protected:
typedef enum { CLOSE_TIME, CLOSE_IDLE, CLOSE_ALARM } EventCloseMode;
/* sizeof(SharedData) expected to be 336 bytes on 32bit and 64bit */
typedef struct
{
typedef struct {
uint32_t size; /* +0 */
uint32_t last_write_index; /* +4 */
uint32_t last_read_index; /* +8 */
@ -135,8 +135,7 @@ protected:
typedef enum { TRIGGER_CANCEL, TRIGGER_ON, TRIGGER_OFF } TriggerState;
/* sizeof(TriggerData) expected to be 560 on 32bit & and 64bit */
typedef struct
{
typedef struct {
uint32_t size;
uint32_t trigger_state;
uint32_t trigger_score;
@ -147,8 +146,7 @@ protected:
} TriggerData;
/* sizeof(Snapshot) expected to be 16 bytes on 32bit and 32 bytes on 64bit */
struct Snapshot
{
struct Snapshot {
struct timeval *timestamp;
Image *image;
void* padding;
@ -157,18 +155,16 @@ protected:
//TODO: Technically we can't exclude this struct when people don't have avformat as the Memory.pm module doesn't know about avformat
#if 1
//sizeOf(VideoStoreData) expected to be 4104 bytes on 32bit and 64bit
typedef struct
{
typedef struct {
uint32_t size;
char event_file[4096];
uint32_t recording; //bool arch dependent so use uint32 instead
timeval recording; //bool arch dependent so use uint32 instead
//uint32_t frameNumber;
} VideoStoreData;
#endif // HAVE_LIBAVFORMAT
class MonitorLink
{
class MonitorLink {
protected:
unsigned int id;
char name[64];
@ -196,21 +192,17 @@ protected:
MonitorLink( int p_id, const char *p_name );
~MonitorLink();
inline int Id() const
{
inline int Id() const {
return( id );
}
inline const char *Name() const
{
inline const char *Name() const {
return( name );
}
inline bool isConnected() const
{
inline bool isConnected() const {
return( connected );
}
inline time_t getLastConnectTime() const
{
inline time_t getLastConnectTime() const {
return( last_connect_time );
}
@ -237,7 +229,7 @@ protected:
unsigned int deinterlacing;
int savejpegspref;
int videowriterpref;
VideoWriter videowriter;
std::string encoderparams;
std::vector<EncoderParameter_t> encoderparamsvec;
bool record_audio; // Whether to store the audio that we receive
@ -342,7 +334,7 @@ public:
int p_orientation,
unsigned int p_deinterlacing,
int p_savejpegs,
int p_videowriter,
VideoWriter p_videowriter,
std::string p_encoderparams,
bool p_record_audio,
const char *p_event_prefix,
@ -378,47 +370,38 @@ public:
void AddPrivacyBitmask( Zone *p_zones[] );
bool connect();
inline int ShmValid() const
{
inline int ShmValid() const {
return( shared_data->valid );
}
inline int Id() const
{
inline int Id() const {
return( id );
}
inline const char *Name() const
{
inline const char *Name() const {
return( name );
}
inline Function GetFunction() const
{
inline Function GetFunction() const {
return( function );
}
inline bool Enabled()
{
inline bool Enabled() {
if ( function <= MONITOR )
return( false );
return( enabled );
}
inline const char *EventPrefix() const
{
inline const char *EventPrefix() const {
return( event_prefix );
}
inline bool Ready()
{
inline bool Ready() {
if ( function <= MONITOR )
return( false );
return( image_count > ready_count );
}
inline bool Active()
{
inline bool Active() {
if ( function <= MONITOR )
return( false );
return( enabled && shared_data->active );
}
inline bool Exif()
{
inline bool Exif() {
return( embed_exif );
}
Orientation getOrientation() const;
@ -429,9 +412,10 @@ public:
unsigned int SubpixelOrder() const;
int GetOptSaveJPEGs() const { return( savejpegspref ); }
int GetOptVideoWriter() const { return( videowriterpref ); }
VideoWriter GetOptVideoWriter() const { return( videowriter ); }
const std::vector<EncoderParameter_t>* GetOptEncoderParams() const { return( &encoderparamsvec ); }
unsigned int GetPreEventCount() const { return pre_event_count; };
State GetState() const;
int GetImage( int index=-1, int scale=100 );
struct timeval GetTimestamp( int index=-1 ) const;
@ -504,8 +488,7 @@ public:
#define MOD_ADD( var, delta, limit ) (((var)+(limit)+(delta))%(limit))
class MonitorStream : public StreamBase
{
class MonitorStream : public StreamBase {
protected:
typedef struct SwapImage {
bool valid;
@ -536,19 +519,15 @@ protected:
void processCommand( const CmdMsg *msg );
public:
MonitorStream() : playback_buffer( 0 ), delayed( false ), frame_count( 0 )
{
MonitorStream() : playback_buffer( 0 ), delayed( false ), frame_count( 0 ) {
}
void setStreamBuffer( int p_playback_buffer )
{
void setStreamBuffer( int p_playback_buffer ) {
playback_buffer = p_playback_buffer;
}
void setStreamTTL( time_t p_ttl )
{
void setStreamTTL( time_t p_ttl ) {
ttl = p_ttl;
}
bool setStreamStart( int monitor_id )
{
bool setStreamStart( int monitor_id ) {
return loadMonitor( monitor_id );
}
void runStream();

44
src/zm_packet.cpp Normal file
View File

@ -0,0 +1,44 @@
//ZoneMinder Packet Implementation Class
//Copyright 2017 ZoneMinder LLC
//
//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_packet.h"
#include "zm_ffmpeg.h"
using namespace std;
ZMPacket::ZMPacket( AVPacket *p ) {
av_init_packet( &packet );
if ( zm_av_packet_ref( &packet, p ) < 0 ) {
Error("error refing packet");
}
gettimeofday( &timestamp, NULL );
}
ZMPacket::ZMPacket( AVPacket *p, struct timeval *t ) {
av_init_packet( &packet );
if ( zm_av_packet_ref( &packet, p ) < 0 ) {
Error("error refing packet");
}
timestamp = *t;
}
ZMPacket::~ZMPacket() {
zm_av_packet_unref( &packet );
}

39
src/zm_packet.h Normal file
View File

@ -0,0 +1,39 @@
//ZoneMinder Packet Wrapper Class
//Copyright 2017 ZoneMinder LLC
//
//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_PACKET_H
#define ZM_PACKET_H
extern "C" {
#include <libavformat/avformat.h>
}
class ZMPacket {
public:
AVPacket packet;
struct timeval timestamp;
public:
AVPacket *av_packet() { return &packet; }
ZMPacket( AVPacket *packet, struct timeval *timestamp );
ZMPacket( AVPacket *packet );
~ZMPacket();
};
#endif /* ZM_PACKET_H */

152
src/zm_packetqueue.cpp Normal file
View File

@ -0,0 +1,152 @@
//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"
#include "zm_ffmpeg.h"
#define VIDEO_QUEUESIZE 200
#define AUDIO_QUEUESIZE 50
using namespace std;
zm_packetqueue::zm_packetqueue(){
}
zm_packetqueue::~zm_packetqueue() {
}
bool zm_packetqueue::queuePacket( ZMPacket* zm_packet ) {
pktQueue.push_back( zm_packet );
return true;
}
bool zm_packetqueue::queuePacket( AVPacket* av_packet ) {
ZMPacket *zm_packet = new ZMPacket( av_packet );
pktQueue.push_back( zm_packet );
return true;
}
ZMPacket* zm_packetqueue::popPacket( ) {
if ( pktQueue.empty() ) {
return NULL;
}
ZMPacket *packet = pktQueue.front();
pktQueue.pop_front();
return packet;
}
unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id ) {
Debug(3, "Clearing all but %d frames", frames_to_keep );
frames_to_keep += 1;
if ( pktQueue.empty() ) {
Debug(3, "Queue is empty");
return 0;
} else {
Debug(3, "Queue has (%d)", pktQueue.size() );
}
list<ZMPacket *>::reverse_iterator it;
ZMPacket *packet = NULL;
for ( it = pktQueue.rbegin(); it != pktQueue.rend() && frames_to_keep; ++it ) {
ZMPacket *zm_packet = *it;
AVPacket *av_packet = &(zm_packet->packet);
Debug(3, "Looking at packet with stream index (%d) with keyframe (%d), frames_to_keep is (%d)", av_packet->stream_index, ( av_packet->flags & AV_PKT_FLAG_KEY ), frames_to_keep );
// Want frames_to_keep video keyframes. Otherwise, we may not have enough
if ( ( av_packet->stream_index == stream_id) && ( av_packet->flags & AV_PKT_FLAG_KEY ) ) {
if (!frames_to_keep)
break;
frames_to_keep --;
}
}
unsigned int delete_count = 0;
while ( it != pktQueue.rend() ) {
Debug(3, "Deleting a packet from the front, count is (%d)", delete_count );
packet = pktQueue.front();
pktQueue.pop_front();
delete packet;
delete_count += 1;
}
return delete_count;
} // end unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream_id )
void zm_packetqueue::clearQueue() {
ZMPacket *packet = NULL;
while(!pktQueue.empty()) {
packet = pktQueue.front();
pktQueue.pop_front();
delete packet;
}
}
unsigned int zm_packetqueue::size() {
return pktQueue.size();
}
void zm_packetqueue::clear_unwanted_packets( timeval *recording_started, int mVideoStreamId ) {
// Need to find the keyframe <= recording_started. Can get rid of audio packets.
if ( pktQueue.empty() ) {
return;
}
// Step 1 - find keyframe < recording_started.
// Step 2 - pop packets until we get to the packet in step 2
list<ZMPacket *>::reverse_iterator it;
for ( it = pktQueue.rbegin(); it != pktQueue.rend(); ++ it ) {
ZMPacket *zm_packet = *it;
AVPacket *av_packet = &(zm_packet->packet);
Debug(1, "Looking for keyframe after start" );
if (
( av_packet->flags & AV_PKT_FLAG_KEY )
&&
( av_packet->stream_index == mVideoStreamId )
&&
timercmp( &(zm_packet->timestamp), recording_started, < )
) {
Debug(1, "Found keyframe before start" );
break;
}
}
if ( it == pktQueue.rend() ) {
Debug(1, "Didn't find a keyframe packet keeping all" );
return;
}
ZMPacket *packet = NULL;
while ( pktQueue.rend() != it ) {
packet = pktQueue.front();
pktQueue.pop_front();
delete packet;
}
}

52
src/zm_packetqueue.h Normal file
View File

@ -0,0 +1,52 @@
//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 <boost/interprocess/managed_shared_memory.hpp>
//#include <boost/interprocess/containers/map.hpp>
//#include <boost/interprocess/allocators/allocator.hpp>
#include <list>
#include "zm_packet.h"
extern "C" {
#include <libavformat/avformat.h>
}
class zm_packetqueue {
public:
zm_packetqueue();
virtual ~zm_packetqueue();
bool queuePacket( AVPacket* packet, struct timeval *timestamp );
bool queuePacket( ZMPacket* packet );
bool queuePacket( AVPacket* packet );
ZMPacket * popPacket( );
bool popVideoPacket(ZMPacket* packet);
bool popAudioPacket(ZMPacket* packet);
unsigned int clearQueue( unsigned int video_frames_to_keep, int stream_id );
void clearQueue( );
unsigned int size();
void clear_unwanted_packets( timeval *recording, int mVideoStreamId );
private:
std::list<ZMPacket *> pktQueue;
};
#endif /* ZM_PACKETQUEUE_H */

View File

@ -89,7 +89,7 @@ public:
virtual int PreCapture() = 0;
virtual int Capture( Image &image ) = 0;
virtual int PostCapture() = 0;
virtual int CaptureAndRecord( Image &image, bool recording, char* event_directory )=0;
virtual int CaptureAndRecord( Image &image, timeval recording, char* event_directory )=0;
};
#endif // ZM_REMOTE_CAMERA_H

View File

@ -189,7 +189,7 @@ int RemoteCameraHttp::SendRequest()
* > 0 is the # of bytes read.
*/
int RemoteCameraHttp::ReadData( Buffer &buffer, int bytes_expected )
int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected )
{
fd_set rfds;
FD_ZERO(&rfds);

View File

@ -53,12 +53,12 @@ public:
int Connect();
int Disconnect();
int SendRequest();
int ReadData( Buffer &buffer, int bytes_expected=0 );
int ReadData( Buffer &buffer, unsigned int bytes_expected=0 );
int GetResponse();
int PreCapture();
int Capture( Image &image );
int PostCapture();
int CaptureAndRecord( Image &image, bool recording, char* event_directory ) {return(0);};
int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) {return(0);};
};
#endif // ZM_REMOTE_CAMERA_HTTP_H

View File

@ -58,7 +58,6 @@ RemoteCameraRtsp::RemoteCameraRtsp( unsigned int p_monitor_id, const std::string
mRawFrame = NULL;
mFrame = NULL;
frameCount = 0;
wasRecording = false;
startTime=0;
#if HAVE_LIBSWSCALE
@ -82,13 +81,8 @@ RemoteCameraRtsp::RemoteCameraRtsp( unsigned int p_monitor_id, const std::string
RemoteCameraRtsp::~RemoteCameraRtsp()
{
#if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101)
av_frame_free( &mFrame );
av_frame_free( &mRawFrame );
#else
av_freep( &mFrame );
av_freep( &mRawFrame );
#endif
#if HAVE_LIBSWSCALE
if ( mConvertContext )
@ -117,7 +111,7 @@ void RemoteCameraRtsp::Initialise()
int max_size = width*height*colours;
// This allocates a buffer able to hold a raw fframe, which is a little artbitrary. Might be nice to get some
// decent data on how large a buffer is really needed.
// decent data on how large a buffer is really needed. I think in ffmpeg there are now some functions to do that.
buffer.size( max_size );
if ( logDebugging() )
@ -172,6 +166,7 @@ int RemoteCameraRtsp::PrimeCapture()
// Find first video stream present
mVideoStreamId = -1;
mAudioStreamId = -1;
// Find the first video stream.
for ( unsigned int i = 0; i < mFormatContext->nb_streams; i++ ) {
@ -181,12 +176,31 @@ int RemoteCameraRtsp::PrimeCapture()
if ( mFormatContext->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO )
#endif
{
if ( mVideoStreamId == -1 ) {
mVideoStreamId = i;
break;
continue;
} else {
Debug(2, "Have another video stream." );
}
}
#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_AUDIO )
#else
if ( mFormatContext->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
#endif
{
if ( mAudioStreamId == -1 ) {
mAudioStreamId = i;
} else {
Debug(2, "Have another audio stream." );
}
}
} // end foreach stream
if ( mVideoStreamId == -1 )
Fatal( "Unable to locate video stream" );
if ( mAudioStreamId == -1 )
Debug( 3, "Unable to locate audio stream" );
// Get a pointer to the codec context for the video stream
mCodecContext = mFormatContext->streams[mVideoStreamId]->codec;
@ -248,8 +262,7 @@ int RemoteCameraRtsp::PrimeCapture()
return( 0 );
}
int RemoteCameraRtsp::PreCapture()
{
int RemoteCameraRtsp::PreCapture() {
if ( !rtspThread->isRunning() )
return( -1 );
if ( !rtspThread->hasSources() )
@ -260,8 +273,7 @@ int RemoteCameraRtsp::PreCapture()
return( 0 );
}
int RemoteCameraRtsp::Capture( Image &image )
{
int RemoteCameraRtsp::Capture( Image &image ) {
AVPacket packet;
uint8_t* directbuffer;
int frameComplete = false;
@ -273,14 +285,12 @@ int RemoteCameraRtsp::Capture( Image &image )
return (-1);
}
while ( true )
{
while ( true ) {
buffer.clear();
if ( !rtspThread->isRunning() )
return (-1);
if ( rtspThread->getFrame( buffer ) )
{
if ( rtspThread->getFrame( buffer ) ) {
Debug( 3, "Read frame %d bytes", buffer.size() );
Debug( 4, "Address %p", buffer.head() );
Hexdump( 4, buffer.head(), 16 );
@ -288,18 +298,17 @@ int RemoteCameraRtsp::Capture( Image &image )
if ( !buffer.size() )
return( -1 );
if(mCodecContext->codec_id == AV_CODEC_ID_H264)
{
if(mCodecContext->codec_id == AV_CODEC_ID_H264) {
// SPS and PPS frames should be saved and appended to IDR frames
int nalType = (buffer.head()[3] & 0x1f);
// SPS
// SPS The SPS NAL unit contains parameters that apply to a series of consecutive coded video pictures
if(nalType == 7)
{
lastSps = buffer;
continue;
}
// PPS
// PPS The PPS NAL unit contains parameters that apply to the decoding of one or more individual pictures inside a coded video sequence
else if(nalType == 8)
{
lastPps = buffer;
@ -311,6 +320,8 @@ int RemoteCameraRtsp::Capture( Image &image )
buffer += lastSps;
buffer += lastPps;
}
} else {
Debug(3, "Not an h264 packet");
}
av_init_packet( &packet );
@ -341,6 +352,7 @@ int RemoteCameraRtsp::Capture( Image &image )
if ( frameComplete ) {
Debug( 3, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height );
#if HAVE_LIBSWSCALE
@ -356,15 +368,12 @@ int RemoteCameraRtsp::Capture( Image &image )
#else // HAVE_LIBSWSCALE
Fatal( "You must compile ffmpeg with the --enable-swscale option to use RTSP cameras" );
#endif // HAVE_LIBSWSCALE
frameCount++;
} /* frame complete */
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
av_packet_unref( &packet );
#else
av_free_packet( &packet );
#endif
zm_av_packet_unref( &packet );
} /* getFrame() */
if(frameComplete)
@ -377,23 +386,54 @@ int RemoteCameraRtsp::Capture( Image &image )
}
//Function to handle capture and store
int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* event_file ) {
int RemoteCameraRtsp::CaptureAndRecord(Image &image, timeval recording, char* event_file ) {
AVPacket packet;
uint8_t* directbuffer;
int frameComplete = false;
/* Request a writeable buffer of the target image */
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
if(directbuffer == NULL) {
Error("Failed requesting writeable buffer for the captured image.");
return (-1);
}
while ( true ) {
// WHY Are we clearing it? Might be something good in it.
buffer.clear();
if ( !rtspThread->isRunning() )
return (-1);
//Video recording
if ( recording.tv_sec ) {
// The directory we are recording to is no longer tied to the current event.
// Need to re-init the videostore with the correct directory and start recording again
// Not sure why we are only doing this on keyframe, al
if ( videoStore && (strcmp(oldDirectory, event_file)!=0) ) {
//don't open new videostore until we're on a key frame..would this require an offset adjustment for the event as a result?...if we store our key frame location with the event will that be enough?
Info("Re-starting video storage module");
if ( videoStore ) {
delete videoStore;
videoStore = NULL;
}
} // end if changed to new event
if ( ! videoStore ) {
//Instantiate the video storage module
videoStore = new VideoStore((const char *)event_file, "mp4",
mFormatContext->streams[mVideoStreamId],
mAudioStreamId==-1?NULL:mFormatContext->streams[mAudioStreamId],
startTime,
this->getMonitor() );
strcpy(oldDirectory, event_file);
} // end if ! videoStore
} else {
if ( videoStore ) {
Info("Deleting videoStore instance");
delete videoStore;
videoStore = NULL;
}
} // end if recording or not
if ( rtspThread->getFrame( buffer ) ) {
Debug( 3, "Read frame %d bytes", buffer.size() );
Debug( 4, "Address %p", buffer.head() );
@ -425,13 +465,14 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
av_init_packet( &packet );
// Why are we checking for it being the video stream
if ( packet.stream_index == mVideoStreamId ) {
// Keep decoding until a complete frame is had.
while ( !frameComplete && buffer.size() > 0 ) {
packet.data = buffer.head();
packet.size = buffer.size();
// Why are we checking for it being the video stream? Because it might be audio or something else.
// Um... we just initialized packet... we can't be testing for what it is yet....
if ( packet.stream_index == mVideoStreamId ) {
// So this does the decode
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
int len = avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet );
@ -449,12 +490,18 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
//Hexdump( 0, buffer.head(), buffer.size() );
buffer -= len;
} // end while get & decode a frame
if ( frameComplete ) {
Debug( 3, "Got frame %d", frameCount );
/* Request a writeable buffer of the target image */
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
if(directbuffer == NULL) {
Error("Failed requesting writeable buffer for the captured image.");
return (-1);
}
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
av_image_fill_arrays(mFrame->data, mFrame->linesize,
directbuffer, imagePixFormat, width, height, 1);
@ -463,52 +510,20 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
imagePixFormat, width, height);
#endif
//Video recording
if ( recording && !wasRecording ) {
//Instantiate the video storage module
} // endif frameComplete
videoStore = new VideoStore((const char *)event_file, "mp4",
mFormatContext->streams[mVideoStreamId],
mAudioStreamId==-1?NULL:mFormatContext->streams[mAudioStreamId],
startTime,
this->getMonitor()->getOrientation() );
wasRecording = true;
strcpy(oldDirectory, event_file);
} else if ( !recording && wasRecording && videoStore ) {
// Why are we deleting the videostore? Becase for soem reason we are no longer recording? How does that happen?
Info("Deleting videoStore instance");
delete videoStore;
videoStore = NULL;
}
//The directory we are recording to is no longer tied to the current event. Need to re-init the videostore with the correct directory and start recording again
if ( recording && wasRecording && (strcmp(oldDirectory, event_file)!=0) && (packet.flags & AV_PKT_FLAG_KEY) ) {
//don't open new videostore until we're on a key frame..would this require an offset adjustment for the event as a result?...if we store our key frame location with the event will that be enough?
Info("Re-starting video storage module");
if ( videoStore ) {
delete videoStore;
videoStore = NULL;
}
videoStore = new VideoStore((const char *)event_file, "mp4",
mFormatContext->streams[mVideoStreamId],
mAudioStreamId==-1?NULL:mFormatContext->streams[mAudioStreamId],
startTime,
this->getMonitor()->getOrientation() );
strcpy( oldDirectory, event_file );
}
if ( videoStore && recording ) {
//Write the packet to our video store
int ret = videoStore->writeVideoFramePacket(&packet, mFormatContext->streams[mVideoStreamId]);//, &lastKeyframePkt);
int ret = videoStore->writeVideoFramePacket(&packet);//, &lastKeyframePkt);
if ( ret < 0 ) {//Less than zero and we skipped a frame
av_free_packet( &packet );
// Should not
zm_av_packet_unref( &packet );
return 0;
}
}
} // end if videoStore, so we are recording
#if HAVE_LIBSWSCALE
// Why are we re-scaling after writing out the packet?
if ( mConvertContext == NULL ) {
mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL );
@ -524,42 +539,32 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
frameCount++;
} /* frame complete */
} else if ( packet.stream_index == mAudioStreamId ) {
Debug( 4, "Got audio packet" );
if ( videoStore && recording ) {
if ( record_audio ) {
if ( videoStore && record_audio ) {
Debug( 4, "Storing Audio packet" );
//Write the packet to our video store
int ret = videoStore->writeAudioFramePacket(&packet, mFormatContext->streams[packet.stream_index]); //FIXME no relevance of last key frame
int ret = videoStore->writeAudioFramePacket( &packet ); //FIXME no relevance of last key frame
if ( ret < 0 ) { //Less than zero and we skipped a frame
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
av_packet_unref( &packet );
#else
av_free_packet( &packet );
#endif
zm_av_packet_unref( &packet );
return 0;
}
}
}
} // end if video or audio packet
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
av_packet_unref( &packet );
#else
av_free_packet( &packet );
#endif
} /* getFrame() */
zm_av_packet_unref( &packet );
} // end while ! framecomplete and buffer.size()
if(frameComplete)
return (0);
} /* getFrame() */
} // end while true
// can never get here.
return (0) ;
} // int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* event_file )
int RemoteCameraRtsp::PostCapture()
{
int RemoteCameraRtsp::PostCapture() {
return( 0 );
}
#endif // HAVE_LIBAVFORMAT

View File

@ -85,7 +85,7 @@ public:
int PreCapture();
int Capture( Image &image );
int PostCapture();
int CaptureAndRecord( Image &image, bool recording, char* event_directory );
int CaptureAndRecord( Image &image, timeval recording, char* event_directory );
};
#endif // ZM_REMOTE_CAMERA_RTSP_H

View File

@ -395,6 +395,18 @@ void timespec_diff(struct timespec *start, struct timespec *end, struct timespec
}
}
char *timeval_to_string( struct timeval tv ) {
time_t nowtime;
struct tm *nowtm;
static char tmbuf[64], buf[64];
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, tv.tv_usec);
return buf;
}
std::string UriDecode( const std::string &encoded ) {
#ifdef HAVE_LIBCURL
CURL *curl = curl_easy_init();

View File

@ -61,6 +61,7 @@ void hwcaps_detect();
extern unsigned int sseversion;
extern unsigned int neonversion;
char *timeval_to_string( struct timeval tv );
std::string UriDecode( const std::string &encoded );
#endif // ZM_UTILS_H

View File

@ -1,4 +1,3 @@
//
// ZoneMinder Video Storage Implementation
// Written by Chris Wiggins
// http://chriswiggins.co.nz
@ -33,14 +32,15 @@ extern "C"{
}
VideoStore::VideoStore(const char *filename_in, const char *format_in,
AVStream *input_st,
AVStream *inpaud_st,
AVStream *p_video_input_stream,
AVStream *p_audio_input_stream,
int64_t nStartTime,
Monitor::Orientation orientation
Monitor * monitor
) {
video_input_stream = p_video_input_stream;
audio_input_stream = p_audio_input_stream;
AVDictionary *pmetadata = NULL;
int dsr;
video_input_context = video_input_stream->codec;
//store inputs in variables local to class
filename = filename_in;
@ -49,11 +49,10 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
keyframeMessage = false;
keyframeSkipNumber = 0;
Info("Opening video storage stream %s format: %d\n", filename, format);
Info("Opening video storage stream %s format: %s\n", filename, format);
//Init everything we need
int ret;
av_register_all();
//Init everything we need, shouldn't have to do this, ffmpeg_camera or something else will call it.
//av_register_all();
ret = avformat_alloc_output_context2(&oc, NULL, NULL, filename);
if ( ret < 0 ) {
@ -62,6 +61,8 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
filename,
av_make_error_string(ret).c_str()
);
} else {
Debug(2, "Success alocateing output context");
}
//Couldn't deduce format from filename, trying from format name
@ -72,90 +73,148 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
" could not be assigned based on filename or format %s",
filename, format);
}
} else {
Debug(2, "Success alocateing output context");
}
dsr = av_dict_set(&pmetadata, "title", "Zoneminder Security Recording", 0);
AVDictionary *pmetadata = NULL;
int dsr = av_dict_set(&pmetadata, "title", "Zoneminder Security Recording", 0);
if (dsr < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__ );
oc->metadata = pmetadata;
fmt = oc->oformat;
output_format = oc->oformat;
video_st = avformat_new_stream(oc, (AVCodec *)input_st->codec->codec);
if (!video_st) {
video_output_stream = avformat_new_stream(oc, (AVCodec*)video_input_context->codec);
if (!video_output_stream) {
Fatal("Unable to create video out stream\n");
} else {
Debug(2, "Success creating video out stream" );
}
ret = avcodec_copy_context(video_st->codec, input_st->codec);
video_output_context = video_output_stream->codec;
#if LIBAVCODEC_VERSION_CHECK(58, 0, 0, 0, 0)
Debug(2, "setting parameters");
ret = avcodec_parameters_to_context( video_output_context, video_input_stream->codecpar );
if ( ret < 0 ) {
Fatal("Unable to copy input video context to output video context "
"%s\n", av_make_error_string(ret).c_str());
Error( "Could not initialize stream parameteres");
return;
} else {
Debug(2, "Success getting parameters");
}
#else
ret = avcodec_copy_context(video_output_context, video_input_context );
if (ret < 0) {
Fatal("Unable to copy input video context to output video context %s\n",
av_make_error_string(ret).c_str());
} else {
Debug(3, "Success copying context" );
}
#endif
// Just copy them from the input, no reason to choose different
video_output_context->time_base = video_input_context->time_base;
video_output_stream->time_base = video_input_stream->time_base;
Debug(3, "Time bases: VIDEO input stream (%d/%d) input codec: (%d/%d) output stream: (%d/%d) output codec (%d/%d)",
video_input_stream->time_base.num,
video_input_stream->time_base.den,
video_input_context->time_base.num,
video_input_context->time_base.den,
video_output_stream->time_base.num,
video_output_stream->time_base.den,
video_output_context->time_base.num,
video_output_context->time_base.den
);
// WHY?
//video_output_context->codec_tag = 0;
if (!video_output_context->codec_tag) {
Debug(2, "No codec_tag");
if (! oc->oformat->codec_tag
|| av_codec_get_id (oc->oformat->codec_tag, video_input_context->codec_tag) == video_output_context->codec_id
|| av_codec_get_tag(oc->oformat->codec_tag, video_input_context->codec_id) <= 0) {
Warning("Setting codec tag");
video_output_context->codec_tag = video_input_context->codec_tag;
}
}
if ( video_st->sample_aspect_ratio.den != video_st->codec->sample_aspect_ratio.den ) {
Warning("Fixingample_aspect_ratio.den");
video_st->sample_aspect_ratio.den = video_st->codec->sample_aspect_ratio.den;
}
if ( video_st->sample_aspect_ratio.num != input_st->codec->sample_aspect_ratio.num ) {
Warning("Fixingample_aspect_ratio.num");
video_st->sample_aspect_ratio.num = input_st->codec->sample_aspect_ratio.num;
}
if ( video_st->codec->codec_id != input_st->codec->codec_id ) {
Warning("Fixing video_st->codec->codec_id");
video_st->codec->codec_id = input_st->codec->codec_id;
}
if ( ! video_st->codec->time_base.num ) {
Warning("video_st->codec->time_base.num is not set%d/%d. Fixing by setting it to 1", video_st->codec->time_base.num, video_st->codec->time_base.den);
Warning("video_st->codec->time_base.num is not set%d/%d. Fixing by setting it to 1", video_st->time_base.num, video_st->time_base.den);
video_st->codec->time_base.num = video_st->time_base.num;
video_st->codec->time_base.den = video_st->time_base.den;
}
video_st->codec->codec_tag = 0;
if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
video_st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
video_output_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
Monitor::Orientation orientation = monitor->getOrientation();
if ( orientation ) {
if ( orientation == Monitor::ROTATE_0 ) {
} else if ( orientation == Monitor::ROTATE_90 ) {
dsr = av_dict_set( &video_st->metadata, "rotate", "90", 0);
dsr = av_dict_set( &video_output_stream->metadata, "rotate", "90", 0);
if (dsr < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__ );
} else if ( orientation == Monitor::ROTATE_180 ) {
dsr = av_dict_set( &video_st->metadata, "rotate", "180", 0);
dsr = av_dict_set( &video_output_stream->metadata, "rotate", "180", 0);
if (dsr < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__ );
} else if ( orientation == Monitor::ROTATE_270 ) {
dsr = av_dict_set( &video_st->metadata, "rotate", "270", 0);
dsr = av_dict_set( &video_output_stream->metadata, "rotate", "270", 0);
if (dsr < 0) Warning("%s:%d: title set failed", __FILE__, __LINE__ );
} else {
Warning( "Unsupported Orientation(%d)", orientation );
}
}
audio_output_codec = NULL;
audio_input_context = NULL;
audio_output_stream = NULL;
#ifdef HAVE_LIBAVRESAMPLE
resample_context = NULL;
#endif
if (inpaud_st) {
audio_st = avformat_new_stream(oc, inpaud_st->codec->codec);
if (!audio_st) {
if (audio_input_stream) {
audio_input_context = audio_input_stream->codec;
if ( audio_input_context->codec_id != AV_CODEC_ID_AAC ) {
static char error_buffer[256];
avcodec_string(error_buffer, sizeof(error_buffer), audio_input_context, 0 );
Debug(3, "Got something other than AAC (%s)", error_buffer );
if ( ! setup_resampler() ) {
return;
}
} else {
Debug(3, "Got AAC" );
audio_output_stream = avformat_new_stream(oc, (AVCodec*)audio_input_context->codec);
if ( ! audio_output_stream ) {
Error("Unable to create audio out stream\n");
audio_st = NULL;
audio_output_stream = NULL;
} else {
ret = avcodec_copy_context(audio_st->codec, inpaud_st->codec);
audio_output_context = audio_output_stream->codec;
ret = avcodec_copy_context(audio_output_context, audio_input_context);
if (ret < 0) {
Fatal("Unable to copy audio context %s\n", av_make_error_string(ret).c_str());
}
audio_st->codec->codec_tag = 0;
if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
audio_st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
}
Error("Unable to copy audio context %s\n", av_make_error_string(ret).c_str());
audio_output_stream = NULL;
} else {
Debug(3, "No Audio output stream");
audio_st = NULL;
audio_output_context->codec_tag = 0;
if ( audio_output_context->channels > 1 ) {
Warning("Audio isn't mono, changing it.");
audio_output_context->channels = 1;
} else {
Debug(3, "Audio is mono");
}
}
} // end if audio_output_stream
} // end if is AAC
if ( audio_output_stream ) {
if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
audio_output_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
}
} // end if audio_input_stream
/* open the output file, if needed */
if (!(fmt->flags & AVFMT_NOFILE)) {
if (!(output_format->flags & AVFMT_NOFILE)) {
ret = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,NULL,NULL);
if (ret < 0) {
Fatal("Could not open output file '%s': %s\n", filename,
@ -163,33 +222,84 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
}
}
//av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
//if ((ret = avformat_write_header(ctx, &opts)) < 0) {
//}
//os->ctx_inited = 1;
//avio_flush(ctx->pb);
//av_dict_free(&opts);
zm_dump_stream_format( oc, 0, 0, 1 );
if ( audio_output_stream )
zm_dump_stream_format( oc, 1, 0, 1 );
AVDictionary * opts = NULL;
//av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
//av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
//av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov+default_base_moof", 0);
if ((ret = avformat_write_header(oc, &opts)) < 0) {
Warning("Unable to set movflags to frag_custom+dash+delay_moov");
/* Write the stream header, if any. */
ret = avformat_write_header(oc, NULL);
} else if (av_dict_count(opts) != 0) {
Warning("some options not set\n");
}
if (ret < 0) {
zm_dump_stream_format( oc, 0, 0, 1 );
Fatal("Error occurred when writing output file header to %s: %s\n",
Error("Error occurred when writing output file header to %s: %s\n",
filename,
av_make_error_string(ret).c_str());
}
if ( opts )
av_dict_free(&opts);
prevDts = 0;
startPts = 0;
startDts = 0;
filter_in_rescale_delta_last = AV_NOPTS_VALUE;
video_last_pts = 0;
video_last_dts = 0;
audio_last_pts = 0;
audio_last_dts = 0;
previous_pts = 0;
previous_dts = 0;
startTime=av_gettime()-nStartTime;//oc->start_time;
Info("VideoStore startTime=%d\n",startTime);
} // VideoStore::VideoStore
VideoStore::~VideoStore(){
if ( audio_output_codec ) {
// Do we need to flush the outputs? I have no idea.
AVPacket pkt;
int got_packet;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int64_t size;
while(1) {
#if LIBAVCODEC_VERSION_CHECK(58, 0, 0, 0, 0)
ret = avcodec_receive_packet( audio_output_context, &pkt );
#else
ret = avcodec_encode_audio2( audio_output_context, &pkt, NULL, &got_packet );
#endif
if (ret < 0) {
Error("ERror encoding audio while flushing");
break;
}
Debug(1, "Have audio encoder, need to flush it's output" );
size += pkt.size;
if (!got_packet) {
break;
}
Debug(2, "writing flushed packet pts(%d) dts(%d) duration(%d)", pkt.pts, pkt.dts, pkt.duration );
if (pkt.pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(pkt.pts, audio_output_context->time_base, audio_output_stream->time_base);
if (pkt.dts != AV_NOPTS_VALUE)
pkt.dts = av_rescale_q(pkt.dts, audio_output_context->time_base, audio_output_stream->time_base);
if (pkt.duration > 0)
pkt.duration = av_rescale_q(pkt.duration, audio_output_context->time_base, audio_output_stream->time_base);
Debug(2, "writing flushed packet pts(%d) dts(%d) duration(%d)", pkt.pts, pkt.dts, pkt.duration );
pkt.stream_index = audio_output_stream->index;
av_interleaved_write_frame( oc, &pkt );
zm_av_packet_unref( &pkt );
} // while 1
}
// Flush Queues
av_interleaved_write_frame( oc, NULL );
/* Write the trailer before close */
if ( int rc = av_write_trailer(oc) ) {
Error("Error writing trailer %s", av_err2str( rc ) );
@ -200,15 +310,21 @@ VideoStore::~VideoStore(){
// I wonder if we should be closing the file first.
// I also wonder if we really need to be doing all the context 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?
if ( video_st ) {
avcodec_close(video_st->codec);
if ( video_output_stream ) {
avcodec_close(video_output_context);
}
if (audio_st) {
avcodec_close(audio_st->codec);
if (audio_output_stream) {
avcodec_close(audio_output_context);
#ifdef HAVE_LIBAVRESAMPLE
if ( resample_context ) {
avresample_close( resample_context );
avresample_free( &resample_context );
}
#endif
}
// WHen will be not using a file ?
if (!(fmt->flags & AVFMT_NOFILE)) {
if (!(output_format->flags & AVFMT_NOFILE)) {
/* Close the output file. */
if ( int rc = avio_close(oc->pb) ) {
Error("Error closing avio %s", av_err2str( rc ) );
@ -221,6 +337,198 @@ VideoStore::~VideoStore(){
avformat_free_context(oc);
}
bool VideoStore::setup_resampler() {
#ifdef HAVE_LIBAVRESAMPLE
static char error_buffer[256];
audio_output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if ( ! audio_output_codec ) {
Error("Could not find codec for AAC");
return false;
}
Debug(2, "Have audio output codec");
audio_output_stream = avformat_new_stream( oc, audio_output_codec );
audio_output_context = audio_output_stream->codec;
if ( ! audio_output_context ) {
Error( "could not allocate codec context for AAC\n");
audio_output_stream = NULL;
return false;
}
Debug(2, "Have audio_output_context");
AVDictionary *opts = NULL;
av_dict_set(&opts, "strict", "experimental", 0);
/* put sample parameters */
audio_output_context->bit_rate = audio_input_context->bit_rate;
audio_output_context->sample_rate = audio_input_context->sample_rate;
audio_output_context->channels = audio_input_context->channels;
audio_output_context->channel_layout = audio_input_context->channel_layout;
audio_output_context->sample_fmt = audio_input_context->sample_fmt;
//audio_output_context->refcounted_frames = 1;
if (audio_output_codec->supported_samplerates) {
int found = 0;
for ( unsigned int i = 0; audio_output_codec->supported_samplerates[i]; i++) {
if ( audio_output_context->sample_rate == audio_output_codec->supported_samplerates[i] ) {
found = 1;
break;
}
}
if ( found ) {
Debug(3, "Sample rate is good");
} else {
audio_output_context->sample_rate = audio_output_codec->supported_samplerates[0];
Debug(1, "Sampel rate is no good, setting to (%d)", audio_output_codec->supported_samplerates[0] );
}
}
/* check that the encoder supports s16 pcm input */
if (!check_sample_fmt( audio_output_codec, audio_output_context->sample_fmt)) {
Debug( 3, "Encoder does not support sample format %s, setting to FLTP",
av_get_sample_fmt_name( audio_output_context->sample_fmt));
audio_output_context->sample_fmt = AV_SAMPLE_FMT_FLTP;
}
//audio_output_stream->time_base = audio_input_stream->time_base;
audio_output_context->time_base = (AVRational){ 1, audio_output_context->sample_rate };
Debug(3, "Audio Time bases input stream (%d/%d) input codec: (%d/%d) output_stream (%d/%d) output codec (%d/%d)",
audio_input_stream->time_base.num,
audio_input_stream->time_base.den,
audio_input_context->time_base.num,
audio_input_context->time_base.den,
audio_output_stream->time_base.num,
audio_output_stream->time_base.den,
audio_output_context->time_base.num,
audio_output_context->time_base.den
);
ret = avcodec_open2(audio_output_context, audio_output_codec, &opts );
av_dict_free(&opts);
if ( ret < 0 ) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
Fatal( "could not open codec (%d) (%s)\n", ret, error_buffer );
audio_output_codec = NULL;
audio_output_context = NULL;
audio_output_stream = NULL;
return false;
}
Debug(1, "Audio output bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) layout(%d) frame_size(%d)",
audio_output_context->bit_rate,
audio_output_context->sample_rate,
audio_output_context->channels,
audio_output_context->sample_fmt,
audio_output_context->channel_layout,
audio_output_context->frame_size
);
output_frame_size = audio_output_context->frame_size;
/** Create a new frame to store the audio samples. */
if (!(input_frame = zm_av_frame_alloc())) {
Error("Could not allocate input frame");
return false;
}
/** Create a new frame to store the audio samples. */
if (!(output_frame = zm_av_frame_alloc())) {
Error("Could not allocate output frame");
av_frame_free( &input_frame );
return false;
}
// Setup the audio resampler
resample_context = avresample_alloc_context();
if ( ! resample_context ) {
Error( "Could not allocate resample context\n");
return false;
}
// Some formats (i.e. WAV) do not produce the proper channel layout
if ( audio_input_context->channel_layout == 0 ) {
Error( "Bad channel layout. Need to set it to mono.\n");
av_opt_set_int( resample_context, "in_channel_layout", av_get_channel_layout( "mono" ), 0 );
} else {
av_opt_set_int( resample_context, "in_channel_layout", audio_input_context->channel_layout, 0 );
}
av_opt_set_int( resample_context, "in_sample_fmt", audio_input_context->sample_fmt, 0);
av_opt_set_int( resample_context, "in_sample_rate", audio_input_context->sample_rate, 0);
av_opt_set_int( resample_context, "in_channels", audio_input_context->channels,0);
//av_opt_set_int( resample_context, "out_channel_layout", audio_output_context->channel_layout, 0);
av_opt_set_int( resample_context, "out_channel_layout", av_get_channel_layout( "mono" ), 0 );
av_opt_set_int( resample_context, "out_sample_fmt", audio_output_context->sample_fmt, 0);
av_opt_set_int( resample_context, "out_sample_rate", audio_output_context->sample_rate, 0);
av_opt_set_int( resample_context, "out_channels", audio_output_context->channels, 0);
ret = avresample_open( resample_context );
if ( ret < 0 ) {
Error( "Could not open resample context\n");
return false;
}
#if 0
/**
* Allocate as many pointers as there are audio channels.
* Each pointer will later point to the audio samples of the corresponding
* channels (although it may be NULL for interleaved formats).
*/
if (!( converted_input_samples = (uint8_t *)calloc( audio_output_context->channels, sizeof(*converted_input_samples))) ) {
Error( "Could not allocate converted input sample pointers\n");
return;
}
/**
* Allocate memory for the samples of all channels in one consecutive
* block for convenience.
*/
if ((ret = av_samples_alloc( &converted_input_samples, NULL,
audio_output_context->channels,
audio_output_context->frame_size,
audio_output_context->sample_fmt, 0)) < 0) {
Error( "Could not allocate converted input samples (error '%s')\n",
av_make_error_string(ret).c_str() );
av_freep(converted_input_samples);
free(converted_input_samples);
return;
}
#endif
output_frame->nb_samples = audio_output_context->frame_size;
output_frame->format = audio_output_context->sample_fmt;
output_frame->channel_layout = audio_output_context->channel_layout;
// 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( NULL, audio_output_context->channels, audio_output_context->frame_size, audio_output_context->sample_fmt, 0 );
converted_input_samples = (uint8_t*) av_malloc( audioSampleBuffer_size );
if ( !converted_input_samples ) {
Error( "Could not allocate converted input sample pointers\n");
return false;
}
// Setup the data pointers in the AVFrame
if ( avcodec_fill_audio_frame(
output_frame,
audio_output_context->channels,
audio_output_context->sample_fmt,
(const uint8_t*) converted_input_samples,
audioSampleBuffer_size, 0 ) < 0 ) {
Error( "Could not allocate converted input sample pointers\n");
return false;
}
return true;
#else
Error("Not built with libavresample library. Cannot do audio conversion to AAC");
return false;
#endif
}
void VideoStore::dumpPacket( AVPacket *pkt ){
char b[10240];
@ -233,40 +541,84 @@ void VideoStore::dumpPacket( AVPacket *pkt ){
, pkt->stream_index
, pkt->flags
, pkt->pos
, pkt->convergence_duration
, pkt->duration
);
Info("%s:%d:DEBUG: %s", __FILE__, __LINE__, b);
Debug(1, "%s:%d:DEBUG: %s", __FILE__, __LINE__, b);
}
int VideoStore::writeVideoFramePacket(AVPacket *ipkt, AVStream *input_st){//, AVPacket *lastKeyframePkt){
//Debug(3, "before ost_tbcket %d", startTime );
//zm_dump_stream_format( oc, ipkt->stream_index, 0, 1 );
//Debug(3, "before ost_tbcket %d", startTime );
int64_t ost_tb_start_time = av_rescale_q(startTime, AV_TIME_BASE_Q, video_st->time_base);
AVPacket opkt, safepkt;
AVPicture pict;
int VideoStore::writeVideoFramePacket( AVPacket *ipkt ) {
av_init_packet(&opkt);
int duration;
//Scale the PTS of the outgoing packet to be the correct time base
if (ipkt->pts != AV_NOPTS_VALUE) {
opkt.pts = av_rescale_q(ipkt->pts-startPts, input_st->time_base, video_st->time_base) - ost_tb_start_time;
if ( ! video_last_pts ) {
// This is the first packet.
opkt.pts = 0;
Debug(2, "Starting video video_last_pts will become (%d)", ipkt->pts );
} else {
if ( ipkt->pts < video_last_pts ) {
Debug(1, "Resetting video_last_pts from (%d) to (%d)", video_last_pts, ipkt->pts );
// wrap around, need to figure out the distance FIXME having this wrong should cause a jump, but then play ok?
opkt.pts = previous_pts + av_rescale_q( ipkt->pts, video_input_stream->time_base, video_output_stream->time_base);
} else {
opkt.pts = previous_pts + av_rescale_q( ipkt->pts - video_last_pts, video_input_stream->time_base, video_output_stream->time_base);
}
}
Debug(3, "opkt.pts = %d from ipkt->pts(%d) - last_pts(%d)", opkt.pts, ipkt->pts, video_last_pts );
duration = ipkt->pts - video_last_pts;
video_last_pts = ipkt->pts;
} else {
Debug(3, "opkt.pts = undef");
opkt.pts = AV_NOPTS_VALUE;
}
//Scale the DTS of the outgoing packet to be the correct time base
if(ipkt->dts == AV_NOPTS_VALUE) {
opkt.dts = av_rescale_q(input_st->cur_dts-startDts, AV_TIME_BASE_Q, video_st->time_base);
// Just because the input stream wraps, doesn't mean the output needs to. Really, if we are limiting ourselves to 10min segments I can't imagine every wrapping in the output. So need to handle input wrap, without causing output wrap.
if ( ! video_last_dts ) {
// This is the first packet.
opkt.dts = 0;
Debug(1, "Starting video video_last_dts will become (%d)", ipkt->dts );
video_last_dts = ipkt->dts;
} else {
opkt.dts = av_rescale_q(ipkt->dts-startDts, input_st->time_base, video_st->time_base);
if ( ipkt->dts == AV_NOPTS_VALUE ) {
// why are we using cur_dts instead of packet.dts? I think cur_dts is in AV_TIME_BASE_Q, but ipkt.dts is in video_input_stream->time_base
if ( video_input_stream->cur_dts < video_last_dts ) {
Debug(1, "Resetting video_last_dts from (%d) to (%d) p.dts was (%d)", video_last_dts, video_input_stream->cur_dts, ipkt->dts );
opkt.dts = previous_dts + av_rescale_q(video_input_stream->cur_dts, AV_TIME_BASE_Q, video_output_stream->time_base);
} else {
opkt.dts = previous_dts + av_rescale_q(video_input_stream->cur_dts - video_last_dts, AV_TIME_BASE_Q, video_output_stream->time_base);
}
Debug(3, "opkt.dts = %d from video_input_stream->cur_dts(%d) - previus_dts(%d)",
opkt.dts, video_input_stream->cur_dts, video_last_dts
);
video_last_dts = video_input_stream->cur_dts;
} else {
if ( ipkt->dts < video_last_dts ) {
Debug(1, "Resetting video_last_dts from (%d) to (%d)", video_last_dts, ipkt->dts );
opkt.dts = previous_dts + av_rescale_q( ipkt->dts, video_input_stream->time_base, video_output_stream->time_base);
} else {
opkt.dts = previous_dts + av_rescale_q( ipkt->dts - video_last_dts, video_input_stream->time_base, video_output_stream->time_base);
}
Debug(3, "opkt.dts = %d from ipkt.dts(%d) - previus_dts(%d)",
opkt.dts, ipkt->dts, video_last_dts
);
video_last_dts = ipkt->dts;
}
}
if ( opkt.dts > opkt.pts ) {
Debug( 1, "opkt.dts(%d) must be <= opkt.pts(%d). Decompression must happen before presentation.", opkt.dts, opkt.pts );
opkt.dts = opkt.pts;
}
opkt.dts -= ost_tb_start_time;
opkt.duration = av_rescale_q(ipkt->duration, input_st->time_base, video_st->time_base);
if ( ipkt->duration == AV_NOPTS_VALUE ) {
opkt.duration = av_rescale_q( duration, video_input_stream->time_base, video_output_stream->time_base);
} else {
opkt.duration = av_rescale_q(ipkt->duration, video_input_stream->time_base, video_output_stream->time_base);
}
opkt.flags = ipkt->flags;
opkt.pos=-1;
@ -274,38 +626,31 @@ int VideoStore::writeVideoFramePacket(AVPacket *ipkt, AVStream *input_st){//, AV
opkt.size = ipkt->size;
// Some camera have audio on stream 0 and video on stream 1. So when we remove the audio, video stream has to go on 0
if ( ipkt->stream_index > 0 and ! audio_st ) {
if ( ipkt->stream_index > 0 and ! audio_output_stream ) {
Debug(1,"Setting stream index to 0 instead of %d", ipkt->stream_index );
opkt.stream_index = 0;
} else {
opkt.stream_index = ipkt->stream_index;
}
/*opkt.flags |= AV_PKT_FLAG_KEY;*/
if (video_st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (fmt->flags & AVFMT_RAWPICTURE)) {
/* store AVPicture in AVPacket, as expected by the output format */
avpicture_fill(&pict, opkt.data, video_st->codec->pix_fmt, video_st->codec->width, video_st->codec->height);
opkt.data = (uint8_t *)&pict;
opkt.size = sizeof(AVPicture);
opkt.flags |= AV_PKT_FLAG_KEY;
}
AVPacket safepkt;
memcpy(&safepkt, &opkt, sizeof(AVPacket));
Debug(1, "writing video packet pts(%d) dts(%d) duration(%d)", opkt.pts, opkt.dts, opkt.duration );
if ((opkt.data == NULL)||(opkt.size < 1)) {
Warning("%s:%d: Mangled AVPacket: discarding frame", __FILE__, __LINE__ );
dumpPacket( ipkt);
dumpPacket(&opkt);
} else if ((prevDts > 0) && (prevDts >= opkt.dts)) {
Warning("%s:%d: DTS out of order: %lld \u226E %lld; discarding frame", __FILE__, __LINE__, prevDts, opkt.dts);
prevDts = opkt.dts;
} else if ((previous_dts > 0) && (previous_dts > opkt.dts)) {
Warning("%s:%d: DTS out of order: %lld \u226E %lld; discarding frame", __FILE__, __LINE__, previous_dts, opkt.dts);
previous_dts = opkt.dts;
dumpPacket(&opkt);
} else {
int ret;
prevDts = opkt.dts; // Unsure if av_interleaved_write_frame() clobbers opkt.dts when out of order, so storing in advance
previous_dts = opkt.dts; // Unsure if av_interleaved_write_frame() clobbers opkt.dts when out of order, so storing in advance
previous_pts = opkt.pts;
ret = av_interleaved_write_frame(oc, &opkt);
if(ret<0){
// There's nothing we can really do if the frame is rejected, just drop it and get on with the next
@ -314,79 +659,218 @@ int VideoStore::writeVideoFramePacket(AVPacket *ipkt, AVStream *input_st){//, AV
}
}
av_free_packet(&opkt);
zm_av_packet_unref(&opkt);
return 0;
}
int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) {
Debug(4, "writeAudioFrame");
if(!audio_output_stream) {
Debug(1, "Called writeAudioFramePacket when no audio_output_stream");
return 0;//FIXME -ve return codes do not free packet in ffmpeg_camera at the moment
}
if ( audio_output_codec ) {
#ifdef HAVE_LIBAVRESAMPLE
#if 0
ret = avcodec_send_packet( audio_input_context, ipkt );
if ( ret < 0 ) {
Error("avcodec_send_packet fail %s", av_make_error_string(ret).c_str());
return 0;
}
int VideoStore::writeAudioFramePacket(AVPacket *ipkt, AVStream *input_st){
if(!audio_st) {
Error("Called writeAudioFramePacket when no audio_st");
return -1;//FIXME -ve return codes do not free packet in ffmpeg_camera at the moment
ret = avcodec_receive_frame( audio_input_context, input_frame );
if ( ret < 0 ) {
Error("avcodec_receive_frame fail %s", av_make_error_string(ret).c_str());
return 0;
}
/*if(!keyframeMessage)
return -1;*/
//zm_dump_stream_format( oc, ipkt->stream_index, 0, 1 );
Debug(2, "Frame: samples(%d), format(%d), sample_rate(%d), channel layout(%d) refd(%d)",
input_frame->nb_samples,
input_frame->format,
input_frame->sample_rate,
input_frame->channel_layout,
audio_output_context->refcounted_frames
);
// What is this doing? Getting the time of the start of this video chunk? Does that actually make sense?
int64_t ost_tb_start_time = av_rescale_q(startTime, AV_TIME_BASE_Q, audio_st->time_base);
ret = avcodec_send_frame( audio_output_context, input_frame );
if ( ret < 0 ) {
av_frame_unref( input_frame );
Error("avcodec_send_frame fail(%d), %s codec is open(%d) is_encoder(%d)", ret, av_make_error_string(ret).c_str(),
avcodec_is_open( audio_output_context ),
av_codec_is_encoder( audio_output_context->codec)
);
return 0;
}
ret = avcodec_receive_packet( audio_output_context, &opkt );
if ( ret < 0 ) {
av_frame_unref( input_frame );
Error("avcodec_receive_packet fail %s", av_make_error_string(ret).c_str());
return 0;
}
av_frame_unref( input_frame );
#else
AVPacket opkt;
/**
* Decode the audio frame stored in the packet.
* The input audio stream decoder is used to do this.
* If we are at the end of the file, pass an empty packet to the decoder
* to flush it.
*/
if ((ret = avcodec_decode_audio4(audio_input_context, input_frame,
&data_present, ipkt)) < 0) {
Error( "Could not decode frame (error '%s')\n",
av_make_error_string(ret).c_str());
dumpPacket( ipkt );
av_frame_free( &input_frame );
zm_av_packet_unref( &opkt );
return 0;
}
if ( ! data_present ) {
Debug(2, "Not ready to transcode a frame yet.");
zm_av_packet_unref(&opkt);
return 0;
}
int frame_size = input_frame->nb_samples;
Debug(4, "Frame size: %d", frame_size );
// Resample the input into the audioSampleBuffer until we proceed the whole decoded data
if ( (ret = avresample_convert( resample_context,
NULL,
0,
0,
input_frame->data,
0,
input_frame->nb_samples )) < 0 ) {
Error( "Could not resample frame (error '%s')\n",
av_make_error_string(ret).c_str());
return 0;
}
if ( avresample_available( resample_context ) < output_frame->nb_samples ) {
Debug(1, "No enough samples yet");
return 0;
}
// Read a frame audio data from the resample fifo
if ( avresample_read( resample_context, output_frame->data, output_frame->nb_samples ) != output_frame->nb_samples ) {
Warning( "Error reading resampled audio: " );
return 0;
}
av_init_packet(&opkt);
Debug(3, "after init packet" );
Debug(5, "after init packet" );
/** Set a timestamp based on the sample rate for the container. */
//output_frame->pts = av_rescale_q( opkt.pts, audio_output_context->time_base, audio_output_stream->time_base );
// convert the packet to the codec timebase from the stream timebase
//Debug(3, "output_frame->pts(%d) best effort(%d)", output_frame->pts,
//av_frame_get_best_effort_timestamp(output_frame)
//);
/**
* Encode the audio frame and store it in the temporary packet.
* The output audio stream encoder is used to do this.
*/
#if LIBAVCODEC_VERSION_CHECK(58, 0, 0, 0, 0)
if (( ret = avcodec_receive_packet( audio_output_context, &opkt )) < 0 ) {
#else
if (( ret = avcodec_encode_audio2( audio_output_context, &opkt, output_frame, &data_present )) < 0) {
#endif
Error( "Could not encode frame (error '%s')",
av_make_error_string(ret).c_str());
zm_av_packet_unref(&opkt);
return 0;
}
if ( ! data_present ) {
Debug(2, "Not ready to output a frame yet.");
zm_av_packet_unref(&opkt);
return 0;
}
#endif
#endif
} else {
av_init_packet(&opkt);
Debug(5, "after init packet" );
opkt.data = ipkt->data;
opkt.size = ipkt->size;
}
// PTS is difficult, because of the buffering of the audio packets in the resampler. So we have to do it once we actually have a packet...
//Scale the PTS of the outgoing packet to be the correct time base
if ( ipkt->pts != AV_NOPTS_VALUE ) {
Debug(3, "Rescaling output pts");
opkt.pts = av_rescale_q(ipkt->pts-startPts, input_st->time_base, audio_st->time_base) - ost_tb_start_time;
if ( !audio_last_pts ) {
opkt.pts = 0;
} else {
Debug(3, "Setting output pts to AV_NOPTS_VALUE");
if ( audio_last_pts > ipkt->pts ) {
Debug(1, "Resetting audeo_start_pts from (%d) to (%d)", audio_last_pts, ipkt->pts );
}
opkt.pts = previous_pts + av_rescale_q(ipkt->pts - audio_last_pts, audio_input_stream->time_base, audio_output_stream->time_base);
Debug(2, "opkt.pts = %d from ipkt->pts(%d) - last_pts(%d)", opkt.pts, ipkt->pts, audio_last_pts );
}
audio_last_pts = ipkt->pts;
} else {
Debug(2, "opkt.pts = undef");
opkt.pts = AV_NOPTS_VALUE;
}
//Scale the DTS of the outgoing packet to be the correct time base
if(ipkt->dts == AV_NOPTS_VALUE) {
Debug(4, "ipkt->dts == AV_NOPTS_VALUE %d to %d", AV_NOPTS_VALUE, opkt.dts );
opkt.dts = av_rescale_q(input_st->cur_dts-startDts, AV_TIME_BASE_Q, audio_st->time_base);
Debug(4, "ipkt->dts == AV_NOPTS_VALUE %d to %d", AV_NOPTS_VALUE, opkt.dts );
if ( ! audio_last_dts ) {
opkt.dts = 0;
} else {
Debug(4, "ipkt->dts != AV_NOPTS_VALUE %d to %d", AV_NOPTS_VALUE, opkt.dts );
opkt.dts = av_rescale_q(ipkt->dts-startDts, input_st->time_base, audio_st->time_base);
Debug(4, "ipkt->dts != AV_NOPTS_VALUE %d to %d", AV_NOPTS_VALUE, opkt.dts );
if( ipkt->dts == AV_NOPTS_VALUE ) {
// So if the input has no dts assigned... still need an output dts... so we use cur_dts?
if ( audio_last_dts > audio_input_stream->cur_dts ) {
Debug(1, "Resetting audio_last_pts from (%d) to cur_dts (%d)", audio_last_dts, audio_input_stream->cur_dts );
opkt.dts = previous_dts + av_rescale_q( audio_input_stream->cur_dts, AV_TIME_BASE_Q, audio_output_stream->time_base);
} else {
opkt.dts = previous_dts + av_rescale_q( audio_input_stream->cur_dts - audio_last_dts, AV_TIME_BASE_Q, audio_output_stream->time_base);
}
opkt.dts -= ost_tb_start_time;
// Seems like it would be really weird for the codec type to NOT be audiu
if (audio_st->codec->codec_type == AVMEDIA_TYPE_AUDIO && ipkt->dts != AV_NOPTS_VALUE) {
Debug( 4, "code is audio, dts != AV_NOPTS_VALUE " );
int duration = av_get_audio_frame_duration(input_st->codec, ipkt->size);
if(!duration)
duration = input_st->codec->frame_size;
//FIXME where to get filter_in_rescale_delta_last
//FIXME av_rescale_delta doesn't exist in ubuntu vivid libavtools
opkt.dts = opkt.pts = av_rescale_delta(input_st->time_base, ipkt->dts,
(AVRational){1, input_st->codec->sample_rate}, duration, &filter_in_rescale_delta_last,
audio_st->time_base) - ost_tb_start_time;
audio_last_dts = audio_input_stream->cur_dts;
Debug(2, "opkt.dts = %d from video_input_stream->cur_dts(%d) - last_dts(%d)", opkt.dts, audio_input_stream->cur_dts, audio_last_dts );
} else {
if ( audio_last_dts > ipkt->dts ) {
Debug(1, "Resetting audio_last_dts from (%d) to (%d)", audio_last_dts, ipkt->dts );
opkt.dts = previous_dts + av_rescale_q(ipkt->dts, audio_input_stream->time_base, audio_output_stream->time_base);
} else {
opkt.dts = previous_dts + av_rescale_q(ipkt->dts - audio_last_dts, audio_input_stream->time_base, audio_output_stream->time_base);
}
Debug(2, "opkt.dts = %d from ipkt->dts(%d) - last_dts(%d)", opkt.dts, ipkt->dts, audio_last_dts );
}
}
if ( opkt.dts > opkt.pts ) {
Debug(1,"opkt.dts(%d) must be <= opkt.pts(%d). Decompression must happen before presentation.", opkt.dts, opkt.pts );
opkt.dts = opkt.pts;
}
opkt.duration = av_rescale_q(ipkt->duration, input_st->time_base, audio_st->time_base);
// I wonder if we could just use duration instead of all the hoop jumping above?
opkt.duration = av_rescale_q(ipkt->duration, audio_input_stream->time_base, audio_output_stream->time_base);
// pkt.pos: byte position in stream, -1 if unknown
opkt.pos = -1;
opkt.flags = ipkt->flags;
opkt.data = ipkt->data;
opkt.size = ipkt->size;
opkt.stream_index = ipkt->stream_index;
Debug(2, "Stream index is %d", opkt.stream_index );
int ret;
AVPacket safepkt;
memcpy(&safepkt, &opkt, sizeof(AVPacket));
ret = av_interleaved_write_frame(oc, &opkt);
if(ret!=0){
Fatal("Error encoding audio frame packet: %s\n", av_make_error_string(ret).c_str());
Error("Error writing audio frame packet: %s\n", av_make_error_string(ret).c_str());
dumpPacket(&safepkt);
} else {
Debug(2,"Success writing audio frame" );
}
Debug(4,"Success writing audio frame" );
av_free_packet(&opkt);
zm_av_packet_unref(&opkt);
return 0;
}
} // end int VideoStore::writeAudioFramePacket( AVPacket *ipkt )

View File

@ -2,6 +2,13 @@
#define ZM_VIDEOSTORE_H
#include "zm_ffmpeg.h"
extern "C" {
#include "libavutil/audio_fifo.h"
#ifdef HAVE_LIBAVRESAMPLE
#include "libavresample/avresample.h"
#endif
}
#if HAVE_LIBAVCODEC
@ -9,11 +16,37 @@
class VideoStore {
private:
unsigned int packets_written;
AVOutputFormat *fmt;
AVOutputFormat *output_format;
AVFormatContext *oc;
AVStream *video_st;
AVStream *audio_st;
AVStream *video_output_stream;
AVStream *audio_output_stream;
AVCodecContext *video_output_context;
AVStream *video_input_stream;
AVStream *audio_input_stream;
// Move this into the object so that we aren't constantly allocating/deallocating it on the stack
AVPacket opkt;
// we are transcoding
AVFrame *input_frame;
AVFrame *output_frame;
AVCodecContext *video_input_context;
AVCodecContext *audio_input_context;
int ret;
// The following are used when encoding the audio stream to AAC
AVCodec *audio_output_codec;
AVCodecContext *audio_output_context;
int data_present;
AVAudioFifo *fifo;
int output_frame_size;
#ifdef HAVE_LIBAVRESAMPLE
AVAudioResampleContext* resample_context;
#endif
uint8_t *converted_input_samples = NULL;
const char *filename;
const char *format;
@ -21,33 +54,29 @@ private:
bool keyframeMessage;
int keyframeSkipNumber;
int64_t startTime;
int64_t startPts;
int64_t startDts;
int64_t prevDts;
// These are for input
int64_t video_last_pts;
int64_t video_last_dts;
int64_t audio_last_pts;
int64_t audio_last_dts;
// These are for output, should start at zero. We assume they do not wrap because we just aren't going to save files that big.
int64_t previous_pts;
int64_t previous_dts;
int64_t filter_in_rescale_delta_last;
bool setup_resampler();
public:
VideoStore(const char *filename_in, const char *format_in, AVStream *input_st, AVStream *inpaud_st, int64_t nStartTime, Monitor::Orientation p_orientation );
VideoStore(const char *filename_in, const char *format_in, AVStream *video_input_stream, AVStream *audio_input_stream, int64_t nStartTime, Monitor * p_monitor );
~VideoStore();
int writeVideoFramePacket(AVPacket *pkt, AVStream *input_st);//, AVPacket *lastKeyframePkt);
int writeAudioFramePacket(AVPacket *pkt, AVStream *input_st);
int writeVideoFramePacket( AVPacket *pkt );
int writeAudioFramePacket( AVPacket *pkt );
void dumpPacket( AVPacket *pkt );
};
/*
class VideoEvent {
public:
VideoEvent(unsigned int eid);
~VideoEvent();
int createEventImage(unsigned int fid, char *&pBuff);
private:
unsigned int m_eid;
};*/
#endif //havelibav
#endif //zm_videostore_h

View File

@ -42,12 +42,6 @@
#cmakedefine HAVE_GNUTLS_GNUTLS_H 1
#cmakedefine HAVE_LIBMYSQLCLIENT 1
#cmakedefine HAVE_MYSQL_H 1
#cmakedefine HAVE_LIBX264 1
#cmakedefine HAVE_X264_H 1
#cmakedefine HAVE_LIBMP4V2 1
#cmakedefine HAVE_MP4V2_MP4V2_H 1
#cmakedefine HAVE_MP4V2_H 1
#cmakedefine HAVE_MP4_H 1
#cmakedefine HAVE_LIBAVFORMAT 1
#cmakedefine HAVE_LIBAVFORMAT_AVFORMAT_H 1
#cmakedefine HAVE_LIBAVCODEC 1
@ -59,8 +53,16 @@
#cmakedefine HAVE_LIBAVUTIL_MATHEMATICS_H 1
#cmakedefine HAVE_LIBSWSCALE 1
#cmakedefine HAVE_LIBSWSCALE_SWSCALE_H 1
#cmakedefine HAVE_LIBAVRESAMPLE 1
#cmakedefine HAVE_LIBAVRESAMPLE_AVRESAMPLE_H 1
#cmakedefine HAVE_LIBVLC 1
#cmakedefine HAVE_VLC_VLC_H 1
#cmakedefine HAVE_LIBX264 1
#cmakedefine HAVE_X264_H 1
#cmakedefine HAVE_LIBMP4V2 1
#cmakedefine HAVE_MP4_H 1
#cmakedefine HAVE_MP4V2_H 1
#cmakedefine HAVE_MP4V2_MP4V2_H 1
/* Authenication checks */
#cmakedefine HAVE_MD5_OPENSSL 1