zoneminder/src/zm_packet.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.2 KiB
C
Raw Normal View History

//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
#include "zm_logger.h"
#include <condition_variable>
#include <mutex>
extern "C" {
#include <libavformat/avformat.h>
}
#ifdef __FreeBSD__
#include <sys/time.h>
#endif // __FreeBSD__
class Image;
class ZMPacket {
public:
std::mutex mutex_;
std::condition_variable condition_;
int keyframe;
AVStream *stream; // Input stream
AVPacket packet; // Input packet, undecoded
AVFrame *in_frame; // Input image, decoded Theoretically only filled if needed.
AVFrame *out_frame; // output image, Only filled if needed.
2017-11-22 08:55:40 +08:00
struct timeval *timestamp;
uint8_t *buffer; // buffer used in image
Image *image;
Image *analysis_image;
int score;
2017-11-21 00:48:56 +08:00
AVMediaType codec_type;
2017-11-22 08:55:40 +08:00
int image_index;
int codec_imgsize;
int64_t pts; // pts in the packet can be in another time base. This MUST be in AV_TIME_BASE_Q
bool decoded;
2017-12-01 03:37:36 +08:00
public:
AVPacket *av_packet() { return &packet; }
2020-12-28 01:01:01 +08:00
AVPacket *set_packet(AVPacket *p) ;
AVFrame *av_frame() { return out_frame; }
2020-12-28 01:01:01 +08:00
Image *get_image(Image *i=nullptr);
Image *set_image(Image *);
int is_keyframe() { return keyframe; };
int decode( AVCodecContext *ctx );
2020-11-13 01:36:36 +08:00
explicit ZMPacket(Image *image);
explicit ZMPacket(ZMPacket &packet);
ZMPacket();
~ZMPacket();
AVFrame *get_out_frame(const AVCodecContext *ctx);
int get_codec_imgsize() { return codec_imgsize; };
};
class ZMLockedPacket {
public:
ZMPacket *packet_;
std::unique_lock<std::mutex> lck_;
bool locked;
ZMLockedPacket(ZMPacket *p) :
packet_(p),
lck_(packet_->mutex_, std::defer_lock),
locked(false) {
}
~ZMLockedPacket() {
if (locked) unlock();
}
void lock() {
Debug(4, "locking packet %d", packet_->image_index);
lck_.lock();
locked = true;
Debug(4, "packet %d locked", packet_->image_index);
};
bool trylock() {
Debug(4, "TryLocking packet %d", packet_->image_index);
locked = lck_.try_lock();
return locked;
};
void unlock() {
Debug(4, "packet %d unlocked", packet_->image_index);
locked = false;
lck_.unlock();
packet_->condition_.notify_all();
};
void wait() {
Debug(4, "packet %d waiting", packet_->image_index);
// We already have a lock, but it's a recursive mutex.. so this may be ok
packet_->condition_.wait(lck_);
}
};
#endif /* ZM_PACKET_H */