From 8518278476d70b9fd478117bc95bd886fd1441cb Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 12 Apr 2018 11:29:35 -0400 Subject: [PATCH] coverity scan fixes/cleanups --- src/zm_comms.h | 8 ++--- src/zm_ffmpeg.cpp | 4 +-- src/zm_ffmpeg_camera.cpp | 10 +------ src/zm_ffmpeg_camera.h | 3 -- src/zm_image.cpp | 61 +++++++++++++------------------------ src/zm_monitor.cpp | 3 +- src/zm_monitorstream.h | 4 ++- src/zm_mpeg.cpp | 54 +++++++++++++++++++-------------- src/zm_mpeg.h | 12 ++++---- src/zm_packetqueue.cpp | 2 +- src/zm_thread.h | 65 ++++++++++++++++------------------------ src/zm_timer.h | 3 +- src/zm_user.h | 2 +- src/zm_utils.cpp | 2 +- src/zm_utils.h | 4 +-- src/zm_videostore.cpp | 43 +++++++++++++------------- src/zm_videostore.h | 9 +----- 17 files changed, 124 insertions(+), 165 deletions(-) diff --git a/src/zm_comms.h b/src/zm_comms.h index b5974771d..133c0d19f 100644 --- a/src/zm_comms.h +++ b/src/zm_comms.h @@ -194,7 +194,7 @@ public: SockAddrUnix(); SockAddrUnix( const SockAddrUnix &addr ) : SockAddr( (const struct sockaddr *)&mAddrUn ), mAddrUn( addr.mAddrUn ) { } - SockAddrUnix( const struct sockaddr_un *addr ) : SockAddr( (const struct sockaddr *)&mAddrUn ), mAddrUn( *addr ) { + explicit SockAddrUnix( const struct sockaddr_un *addr ) : SockAddr( (const struct sockaddr *)&mAddrUn ), mAddrUn( *addr ) { } bool resolve( const char *path, const char *proto ); @@ -622,9 +622,9 @@ protected: public: Select(); - Select( struct timeval timeout ); - Select( int timeout ); - Select( double timeout ); + explicit Select( struct timeval timeout ); + explicit Select( int timeout ); + explicit Select( double timeout ); void setTimeout( int timeout ); void setTimeout( double timeout ); diff --git a/src/zm_ffmpeg.cpp b/src/zm_ffmpeg.cpp index 77e3c90aa..b4d7e323a 100644 --- a/src/zm_ffmpeg.cpp +++ b/src/zm_ffmpeg.cpp @@ -243,7 +243,7 @@ void zm_dump_codecpar ( const AVCodecParameters *par ) { } #endif -void zm_dump_codec ( const AVCodecContext *codec ) { +void zm_dump_codec(const AVCodecContext *codec) { Debug(1, "Dumping codec_context codec_type(%d) codec_id(%d) width(%d) height(%d) timebase(%d/%d) format(%s)", codec->codec_type, codec->codec_id, @@ -425,7 +425,7 @@ int zm_receive_frame( AVCodecContext *context, AVFrame *frame, AVPacket &packet #endif # else - int frameComplete; + int frameComplete = 0; while ( !frameComplete ) { if ( (ret = zm_avcodec_decode_video( context, frame, &frameComplete, &packet )) < 0 ) { Error( "Unable to decode frame at frame: %s, continuing", diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index a3fef31cb..5bda70c96 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -129,13 +129,13 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri } else { Panic("Unexpected colours: %d",colours); } - } FfmpegCamera::~FfmpegCamera() { if ( videoStore ) { delete videoStore; + videoStore = NULL; } CloseFfmpeg(); @@ -631,14 +631,6 @@ int FfmpegCamera::OpenFfmpeg() { return 0; } // int FfmpegCamera::OpenFfmpeg() -int FfmpegCamera::ReopenFfmpeg() { - - Debug(2, "ReopenFfmpeg called."); - - CloseFfmpeg(); - return OpenFfmpeg(); -} - int FfmpegCamera::CloseFfmpeg() { Debug(2, "CloseFfmpeg called."); diff --git a/src/zm_ffmpeg_camera.h b/src/zm_ffmpeg_camera.h index 17da647b4..65193623c 100644 --- a/src/zm_ffmpeg_camera.h +++ b/src/zm_ffmpeg_camera.h @@ -74,11 +74,8 @@ class FfmpegCamera : public Camera { AVPacket packet; int OpenFfmpeg(); - int ReopenFfmpeg(); int CloseFfmpeg(); - bool mIsOpening; bool mCanCapture; - int mOpenStart; #endif // HAVE_LIBAVFORMAT VideoStore *videoStore; diff --git a/src/zm_image.cpp b/src/zm_image.cpp index d21213788..ba629535b 100644 --- a/src/zm_image.cpp +++ b/src/zm_image.cpp @@ -1649,30 +1649,24 @@ void Image::Blend( const Image &image, int transparency ) AssignDirect( width, height, colours, subpixelorder, new_buffer, size, ZM_BUFTYPE_ZM); } -Image *Image::Merge( unsigned int n_images, Image *images[] ) -{ - if ( n_images <= 0 ) return( 0 ); - if ( n_images == 1 ) return( new Image( *images[0] ) ); +Image *Image::Merge( unsigned int n_images, Image *images[] ) { + if ( n_images == 1 ) return new Image(*images[0]); unsigned int width = images[0]->width; unsigned int height = images[0]->height; unsigned int colours = images[0]->colours; - for ( unsigned int i = 1; i < n_images; i++ ) - { - if ( !(width == images[i]->width && height == images[i]->height && colours == images[i]->colours) ) - { + for ( unsigned int i = 1; i < n_images; i++ ) { + if ( !(width == images[i]->width && height == images[i]->height && colours == images[i]->colours) ) { Panic( "Attempt to merge different sized images, expected %dx%dx%d, got %dx%dx%d, for image %d", width, height, colours, images[i]->width, images[i]->height, images[i]->colours, i ); } } Image *result = new Image( width, height, images[0]->colours, images[0]->subpixelorder); unsigned int size = result->size; - for ( unsigned int i = 0; i < size; i++ ) - { + for ( unsigned int i = 0; i < size; i++ ) { unsigned int total = 0; uint8_t *pdest = result->buffer; - for ( unsigned int j = 0; j < n_images; j++ ) - { + for ( unsigned int j = 0; j < n_images; j++ ) { uint8_t *psrc = images[j]->buffer; total += *psrc; psrc++; @@ -1680,21 +1674,17 @@ Image *Image::Merge( unsigned int n_images, Image *images[] ) *pdest = total/n_images; pdest++; } - return( result ); + return result; } -Image *Image::Merge( unsigned int n_images, Image *images[], double weight ) -{ - if ( n_images <= 0 ) return( 0 ); - if ( n_images == 1 ) return( new Image( *images[0] ) ); +Image *Image::Merge( unsigned int n_images, Image *images[], double weight ) { + if ( n_images == 1 ) return new Image(*images[0]); unsigned int width = images[0]->width; unsigned int height = images[0]->height; unsigned int colours = images[0]->colours; - for ( unsigned int i = 1; i < n_images; i++ ) - { - if ( !(width == images[i]->width && height == images[i]->height && colours == images[i]->colours) ) - { + for ( unsigned int i = 1; i < n_images; i++ ) { + if ( !(width == images[i]->width && height == images[i]->height && colours == images[i]->colours) ) { Panic( "Attempt to merge different sized images, expected %dx%dx%d, got %dx%dx%d, for image %d", width, height, colours, images[i]->width, images[i]->height, images[i]->colours, i ); } } @@ -1702,55 +1692,46 @@ Image *Image::Merge( unsigned int n_images, Image *images[], double weight ) Image *result = new Image( *images[0] ); unsigned int size = result->size; double factor = 1.0*weight; - for ( unsigned int i = 1; i < n_images; i++ ) - { + for ( unsigned int i = 1; i < n_images; i++ ) { uint8_t *pdest = result->buffer; uint8_t *psrc = images[i]->buffer; - for ( unsigned int j = 0; j < size; j++ ) - { + for ( unsigned int j = 0; j < size; j++ ) { *pdest = (uint8_t)(((*pdest)*(1.0-factor))+((*psrc)*factor)); pdest++; psrc++; } factor *= weight; } - return( result ); + return result; } Image *Image::Highlight( unsigned int n_images, Image *images[], const Rgb threshold, const Rgb ref_colour ) { - if ( n_images <= 0 ) return( 0 ); - if ( n_images == 1 ) return( new Image( *images[0] ) ); + if ( n_images == 1 ) return new Image(*images[0]); unsigned int width = images[0]->width; unsigned int height = images[0]->height; unsigned int colours = images[0]->colours; - for ( unsigned int i = 1; i < n_images; i++ ) - { - if ( !(width == images[i]->width && height == images[i]->height && colours == images[i]->colours) ) - { + for ( unsigned int i = 1; i < n_images; i++ ) { + if ( !(width == images[i]->width && height == images[i]->height && colours == images[i]->colours) ) { Panic( "Attempt to highlight different sized images, expected %dx%dx%d, got %dx%dx%d, for image %d", width, height, colours, images[i]->width, images[i]->height, images[i]->colours, i ); } } Image *result = new Image( width, height, images[0]->colours, images[0]->subpixelorder ); unsigned int size = result->size; - for ( unsigned int c = 0; c < colours; c++ ) - { + 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++ ) - { + for ( unsigned int i = 0; i < size; i++ ) { unsigned int count = 0; uint8_t *pdest = result->buffer+c; - for ( unsigned int j = 0; j < n_images; j++ ) - { + for ( unsigned int j = 0; j < n_images; j++ ) { uint8_t *psrc = images[j]->buffer+c; unsigned int diff = ((*psrc)-ref_colour_rgb) > 0 ? (*psrc)-ref_colour_rgb : ref_colour_rgb - (*psrc); - if (diff >= RGB_VAL(threshold,c)) - { + if (diff >= RGB_VAL(threshold,c)) { count++; } psrc += colours; diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 6aeef83d5..6aab818c7 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -1587,7 +1587,7 @@ bool Monitor::Analyse() { } } alarm_cause = cause+" "+alarm_cause; - strncpy( shared_data->alarm_cause,alarm_cause.c_str() , sizeof(shared_data->alarm_cause) ); + strncpy(shared_data->alarm_cause,alarm_cause.c_str(), sizeof(shared_data->alarm_cause)-1); //set up video store data snprintf(video_store_data->event_file, sizeof(video_store_data->event_file), "%s", event->getEventFile()); video_store_data->recording = event->StartTime(); @@ -1815,6 +1815,7 @@ void Monitor::Reload() { ready_count = image_count+warmup_count; ReloadLinkedMonitors( p_linked_monitors ); + delete row; } // end if row ReloadZones(); diff --git a/src/zm_monitorstream.h b/src/zm_monitorstream.h index 95f538765..f3351d3b7 100644 --- a/src/zm_monitorstream.h +++ b/src/zm_monitorstream.h @@ -58,7 +58,9 @@ class MonitorStream : public StreamBase { void SingleImageZip( int scale=100 ); public: - MonitorStream() : ttl(0), playback_buffer(0), delayed(false), frame_count(0) { + MonitorStream() : + temp_image_buffer(NULL), temp_image_buffer_count(0), temp_read_index(0), temp_write_index(0), + ttl(0), playback_buffer(0), delayed(false), frame_count(0) { } void setStreamBuffer( int p_playback_buffer ) { playback_buffer = p_playback_buffer; diff --git a/src/zm_mpeg.cpp b/src/zm_mpeg.cpp index 4ce2e0780..5cbea451d 100644 --- a/src/zm_mpeg.cpp +++ b/src/zm_mpeg.cpp @@ -293,7 +293,7 @@ const char *VideoStream::MimeType( ) const { return mime_type; } -void VideoStream::OpenStream( ) { +bool VideoStream::OpenStream( ) { int ret; /* now that all the parameters are set, we can open the @@ -308,7 +308,8 @@ void VideoStream::OpenStream( ) { if ( (ret = avcodec_open2(codec_context, codec, 0)) < 0 ) #endif { - Fatal( "Could not open codec. Error code %d \"%s\"", ret, av_err2str(ret) ); + Error("Could not open codec. Error code %d \"%s\"", ret, av_err2str(ret)); + return false; } Debug( 1, "Opened codec" ); @@ -316,22 +317,24 @@ void VideoStream::OpenStream( ) { /* allocate the encoded raw picture */ opicture = zm_av_frame_alloc( ); if ( !opicture ) { - Panic( "Could not allocate opicture" ); + Error("Could not allocate opicture"); + return false; } opicture->width = codec_context->width; opicture->height = codec_context->height; opicture->format = codec_context->pix_fmt; #if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) - int size = av_image_get_buffer_size( codec_context->pix_fmt, codec_context->width, codec_context->height, 1 ); + int size = av_image_get_buffer_size(codec_context->pix_fmt, codec_context->width, codec_context->height, 1); #else - int size = avpicture_get_size( codec_context->pix_fmt, codec_context->width, codec_context->height ); + int size = avpicture_get_size(codec_context->pix_fmt, codec_context->width, codec_context->height); #endif - uint8_t *opicture_buf = (uint8_t *)av_malloc( size ); + uint8_t *opicture_buf = (uint8_t *)av_malloc(size); if ( !opicture_buf ) { av_frame_free( &opicture ); - Panic( "Could not allocate opicture_buf" ); + Error( "Could not allocate opicture_buf" ); + return false; } #if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) av_image_fill_arrays(opicture->data, opicture->linesize, @@ -352,7 +355,8 @@ void VideoStream::OpenStream( ) { tmp_opicture = avcodec_alloc_frame( ); #endif if ( !tmp_opicture ) { - Panic( "Could not allocate tmp_opicture" ); + Error( "Could not allocate tmp_opicture" ); + return false; } #if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) int size = av_image_get_buffer_size( pf, codec_context->width, codec_context->height,1 ); @@ -362,7 +366,8 @@ void VideoStream::OpenStream( ) { uint8_t *tmp_opicture_buf = (uint8_t *)av_malloc( size ); if ( !tmp_opicture_buf ) { av_frame_free( &tmp_opicture ); - Panic( "Could not allocate tmp_opicture_buf" ); + Error( "Could not allocate tmp_opicture_buf" ); + return false; } #if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) av_image_fill_arrays(tmp_opicture->data, @@ -385,12 +390,14 @@ void VideoStream::OpenStream( ) { ret = url_fopen( &ofc->pb, filename, AVIO_FLAG_WRITE ); #endif if ( ret < 0 ) { - Fatal( "Could not open '%s'", filename ); + Error("Could not open '%s'", filename); + return false; } - Debug( 1, "Opened output \"%s\"", filename ); + Debug(1, "Opened output \"%s\"", filename); } else { - Fatal( "of->flags & AVFMT_NOFILE" ); + Error( "of->flags & AVFMT_NOFILE" ); + return false; } video_outbuf = NULL; @@ -417,14 +424,16 @@ void VideoStream::OpenStream( ) { #endif #if !LIBAVFORMAT_VERSION_CHECK(53, 2, 0, 4, 0) - ret = av_write_header( ofc ); + ret = av_write_header(ofc); #else - ret = avformat_write_header(ofc, NULL); + ret = avformat_write_header(ofc, NULL); #endif - if ( ret < 0 ) { - Fatal( "?_write_header failed with error %d \"%s\"", ret, av_err2str( ret ) ); - } + if ( ret < 0 ) { + Error("?_write_header failed with error %d \"%s\"", ret, av_err2str(ret)); + return false; + } + return true; } VideoStream::VideoStream( const char *in_filename, const char *in_format, int bitrate, double frame_rate, int colours, int subpixelorder, int width, int height ) : @@ -550,19 +559,20 @@ VideoStream::~VideoStream( ) { } double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool _add_timestamp, unsigned int _timestamp ) { - if ( pthread_mutex_lock( buffer_copy_lock ) != 0 ) { + if ( pthread_mutex_lock(buffer_copy_lock) != 0 ) { Fatal( "EncodeFrame: pthread_mutex_lock failed." ); } if (buffer_copy_size < buffer_size) { if ( buffer_copy ) { - av_free( buffer_copy ); + av_free(buffer_copy); } // Allocate a buffer to store source images for the streaming thread to encode. - buffer_copy = (uint8_t *)av_malloc( buffer_size ); + buffer_copy = (uint8_t *)av_malloc(buffer_size); if ( !buffer_copy ) { - Panic( "Could not allocate buffer_copy" ); + Error( "Could not allocate buffer_copy" ); + pthread_mutex_unlock(buffer_copy_lock); return 0; } buffer_copy_size = buffer_size; @@ -573,7 +583,7 @@ double VideoStream::EncodeFrame( const uint8_t *buffer, int buffer_size, bool _a buffer_copy_used = buffer_size; memcpy(buffer_copy, buffer, buffer_size); - if ( pthread_mutex_unlock( buffer_copy_lock ) != 0 ) { + if ( pthread_mutex_unlock(buffer_copy_lock) != 0 ) { Fatal( "EncodeFrame: pthread_mutex_unlock failed." ); } diff --git a/src/zm_mpeg.h b/src/zm_mpeg.h index fbc3eed4d..6e75f8743 100644 --- a/src/zm_mpeg.h +++ b/src/zm_mpeg.h @@ -1,21 +1,21 @@ /* * ZoneMinder MPEG Interface, $Date$, $Revision$ * Copyright (C) 2001-2008 Philip Coombes - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ +*/ #ifndef ZM_MPEG_H #define ZM_MPEG_H @@ -51,7 +51,7 @@ protected: uint8_t *video_outbuf; int video_outbuf_size; double last_pts; - + pthread_t streaming_thread; bool do_streaming; bool add_timestamp; @@ -78,7 +78,7 @@ public: VideoStream( const char *filename, const char *format, int bitrate, double frame_rate, int colours, int subpixelorder, int width, int height ); ~VideoStream(); const char *MimeType() const; - void OpenStream(); + bool OpenStream(); double EncodeFrame( const uint8_t *buffer, int buffer_size, bool add_timestamp=false, unsigned int timestamp=0 ); }; diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 950cfd576..92062eab0 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -101,7 +101,7 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream } if ( it != pktQueue.rend() ) { // We want to keep this packet, so advance to the next - it ++; + ++it; } unsigned int delete_count = 0; while ( it != pktQueue.rend() ) { diff --git a/src/zm_thread.h b/src/zm_thread.h index 199d1f764..66df9fd8e 100644 --- a/src/zm_thread.h +++ b/src/zm_thread.h @@ -54,7 +54,7 @@ private: pthread_t pid() { return( pthread_self() ); } #endif public: - explicit ThreadException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)pid() ) ) { + explicit ThreadException( const std::string &message ) : Exception( stringtf("(%d) ", (long int)pid())+message ) { } }; @@ -97,61 +97,53 @@ private: ScopedMutex( const ScopedMutex & ); }; -class Condition -{ +class Condition { private: Mutex &mMutex; pthread_cond_t mCondition; public: - Condition( Mutex &mutex ); + explicit Condition(Mutex &mutex); ~Condition(); void wait(); - bool wait( int secs ); - bool wait( double secs ); + bool wait(int secs); + bool wait(double secs); void signal(); void broadcast(); }; -class Semaphore : public Condition -{ +class Semaphore : public Condition { private: Mutex mMutex; public: - Semaphore() : Condition( mMutex ) - { + Semaphore() : Condition(mMutex) { } - void wait() - { + void wait() { mMutex.lock(); Condition::wait(); mMutex.unlock(); } - bool wait( int secs ) - { + bool wait(int secs) { mMutex.lock(); - bool result = Condition::wait( secs ); + bool result = Condition::wait(secs); mMutex.unlock(); - return( result ); + return result; } - bool wait( double secs ) - { + bool wait(double secs) { mMutex.lock(); - bool result = Condition::wait( secs ); + bool result = Condition::wait(secs); mMutex.unlock(); - return( result ); + return result; } - void signal() - { + void signal() { mMutex.lock(); Condition::signal(); mMutex.unlock(); } - void broadcast() - { + void broadcast() { mMutex.lock(); Condition::broadcast(); mMutex.unlock(); @@ -169,7 +161,7 @@ public: __attribute__((used)) ThreadData() : mValue(0), mCondition( mMutex ) { mChanged = false; } - __attribute__((used)) ThreadData( T value ) : mValue( value ), mCondition( mMutex ) { + explicit __attribute__((used)) ThreadData( T value ) : mValue( value ), mCondition( mMutex ) { mChanged = false; } //~ThreadData() {} @@ -200,8 +192,7 @@ public: __attribute__((used)) void updateValueBroadcast( const T value ); }; -class Thread -{ +class Thread { public: typedef void *(*ThreadFunc)( void * ); @@ -224,8 +215,7 @@ protected: virtual ~Thread(); #ifndef SOLARIS - pid_t id() const - { + pid_t id() const { pid_t tid; #ifdef __FreeBSD__ long lwpid; @@ -239,16 +229,14 @@ protected: tid=syscall(SYS_gettid); #endif #endif -return tid; + return tid; } #else - pthread_t id() const - { - return( pthread_self() ); + pthread_t id() const { + return pthread_self(); } #endif - void exit( int p_status = 0 ) - { + void exit( int p_status = 0 ) { //INFO( "Exiting" ); pthread_exit( (void *)&p_status ); } @@ -260,12 +248,11 @@ public: void start(); void join(); void kill( int signal ); - bool isThread() - { + bool isThread() { return( mPid > -1 && pthread_equal( pthread_self(), mThread ) ); } - bool isStarted() const { return( mStarted ); } - bool isRunning() const { return( mRunning ); } + bool isStarted() const { return mStarted; } + bool isRunning() const { return mRunning; } }; #endif // ZM_THREAD_H diff --git a/src/zm_timer.h b/src/zm_timer.h index 221aec222..da3b95783 100644 --- a/src/zm_timer.h +++ b/src/zm_timer.h @@ -53,8 +53,7 @@ private: pthread_t pid() { return( pthread_self() ); } #endif public: - TimerException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)pid() ) ) - { + explicit TimerException( const std::string &message ) : Exception( stringtf("(%d) ", (long int)pid())+message ) { } }; diff --git a/src/zm_user.h b/src/zm_user.h index 2c932dd74..37bf45736 100644 --- a/src/zm_user.h +++ b/src/zm_user.h @@ -59,7 +59,7 @@ public: ~User(); User( User &u ) { Copy(u); } void Copy( const User &u ); - User operator=(const User &u) { + User& operator=(const User &u) { Copy(u); return *this; } diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index 9ba8cc420..111dd8f09 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -49,7 +49,7 @@ std::string trimSet(std::string str, std::string trimset) { return str.substr( startpos, endpos-startpos+1 ); } -std::string trimSpaces(std::string str) { +std::string trimSpaces(const std::string &str) { return trimSet(str, " \t"); } diff --git a/src/zm_utils.h b/src/zm_utils.h index a10c89f48..8352edecb 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -28,12 +28,12 @@ typedef std::vector StringVector; -std::string trimSpaces(std::string str); +std::string trimSpaces(const std::string &str); std::string trimSet(std::string str, std::string trimset); std::string replaceAll(std::string str, std::string from, std::string to); const std::string stringtf( const char *format, ... ); -const std::string stringtf( const std::string format, ... ); +const std::string stringtf( const std::string &format, ... ); bool startsWith( const std::string &haystack, const std::string &needle ); StringVector split( const std::string &string, const std::string &chars, int limit=0 ); diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index 0e72a7c7e..6ca8f3f96 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -219,8 +219,7 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, if (audio_in_ctx->codec_id != AV_CODEC_ID_AAC) { static char error_buffer[256]; - avcodec_string(error_buffer, sizeof(error_buffer), audio_in_ctx, - 0); + avcodec_string(error_buffer, sizeof(error_buffer), audio_in_ctx, 0); Debug(2, "Got something other than AAC (%s)", error_buffer); if (!setup_resampler()) { @@ -361,8 +360,8 @@ VideoStore::~VideoStore() { // Put encoder into flushing mode avcodec_send_frame(audio_out_ctx, NULL); ret = avcodec_receive_packet(audio_out_ctx, &pkt); - if (ret < 0) { - if (AVERROR_EOF != ret) { + if ( ret < 0 ) { + if ( AVERROR_EOF != ret ) { Error("ERror encoding audio while flushing (%d) (%s)", ret, av_err2str(ret)); } @@ -372,13 +371,13 @@ VideoStore::~VideoStore() { int got_packet = 0; ret = avcodec_encode_audio2(audio_out_ctx, &pkt, NULL, &got_packet); - if (ret < 0) { + if ( ret < 0 ) { Error("ERror encoding audio while flushing (%d) (%s)", ret, av_err2str(ret)); break; } Debug(1, "Have audio encoder, need to flush it's out"); - if (!got_packet) { + if ( !got_packet ) { break; } #endif @@ -387,7 +386,7 @@ VideoStore::~VideoStore() { pkt.pts = audio_next_pts; pkt.dts = audio_next_dts; - if (pkt.duration > 0) + if ( pkt.duration > 0 ) pkt.duration = av_rescale_q(pkt.duration, audio_out_ctx->time_base, audio_out_stream->time_base); @@ -429,12 +428,12 @@ VideoStore::~VideoStore() { // 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_out_stream) { + if ( video_out_stream ) { #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) // We allocate and copy in newer ffmpeg, so need to free it avcodec_free_context(&video_in_ctx); #endif - video_in_ctx=NULL; + video_in_ctx = NULL; avcodec_close(video_out_ctx); #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) @@ -445,13 +444,13 @@ VideoStore::~VideoStore() { } if ( audio_out_stream ) { if ( audio_in_codec ) { - avcodec_close(audio_in_ctx); + avcodec_close(audio_in_ctx); #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) - // We allocate and copy in newer ffmpeg, so need to free it - avcodec_free_context(&audio_in_ctx); + // We allocate and copy in newer ffmpeg, so need to free it + avcodec_free_context(&audio_in_ctx); #endif - audio_in_ctx = NULL; - audio_in_codec = NULL; + audio_in_ctx = NULL; + audio_in_codec = NULL; } // end if audio_in_codec avcodec_close(audio_out_ctx); @@ -460,19 +459,19 @@ VideoStore::~VideoStore() { #endif audio_out_ctx = NULL; #ifdef HAVE_LIBAVRESAMPLE - if (resample_ctx) { + if ( resample_ctx ) { avresample_close(resample_ctx); avresample_free(&resample_ctx); } - if (in_frame) { + if ( in_frame ) { av_frame_free(&in_frame); in_frame = NULL; } - if (out_frame) { + if ( out_frame ) { av_frame_free(&out_frame); out_frame = NULL; } - if (converted_in_samples) { + if ( converted_in_samples ) { av_free(converted_in_samples); converted_in_samples = NULL; } @@ -481,11 +480,10 @@ VideoStore::~VideoStore() { /* free the stream */ avformat_free_context(oc); -} +} // VideoStore::~VideoStore() bool VideoStore::setup_resampler() { #ifdef HAVE_LIBAVRESAMPLE - static char error_buffer[256]; #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) // Newer ffmpeg wants to keep everything separate... so have to lookup our own @@ -576,9 +574,8 @@ bool VideoStore::setup_resampler() { } ret = avcodec_open2(audio_out_ctx, audio_out_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); + if ( ret < 0 ) { + Error("could not open codec (%d) (%s)\n", ret, av_make_error_string(ret).c_str()); audio_out_codec = NULL; audio_out_ctx = NULL; audio_out_stream = NULL; diff --git a/src/zm_videostore.h b/src/zm_videostore.h index 6d2b15cd0..282d96da7 100644 --- a/src/zm_videostore.h +++ b/src/zm_videostore.h @@ -3,8 +3,6 @@ #include "zm_ffmpeg.h" extern "C" { -#include "libavutil/audio_fifo.h" - #ifdef HAVE_LIBAVRESAMPLE #include "libavresample/avresample.h" #endif @@ -40,19 +38,14 @@ private: // The following are used when encoding the audio stream to AAC AVCodec *audio_out_codec; AVCodecContext *audio_out_ctx; - AVAudioFifo *fifo; - int out_frame_size; #ifdef HAVE_LIBAVRESAMPLE -AVAudioResampleContext* resample_ctx; + AVAudioResampleContext* resample_ctx; #endif uint8_t *converted_in_samples; const char *filename; const char *format; - bool keyframeMessage; - int keyframeSkipNumber; - // These are for in int64_t video_last_pts; int64_t video_last_dts;