保存yuv格式的图片
char buf[256] ={0};
static int k = 0;
sprintf(buf, "./yuv%dframe.yuv",k++);
if(k % 5==0)
{
FILE *pFile = fopen(buf,"ab+");
fwrite(sendToEncodeAndRtmp->data.get(),sizeof(uint8_t),sendToEncodeAndRtmp->data_size,pFile);
fclose(pFile);
}
===========================================================
新建一个智能指针数组
新建一个大小为PER_FRAME_SIZE的uint8_t类型的数组。
std::shared_ptr<std::uint8_t> temp(new std::uint8_t[PER_FRAME_SIZE](), std::default_delete<std::uint8_t[]>());
===========================================================
新建一个线程
std::thread threadRtmp;
threadRtmp=std::thread(&SceneManager::encodeAndRtmpLoop,this);
threadRtmp.detach();
===========================================================
用线程方式等待时间
std::this_thread::sleep_for(std::chrono::milliseconds(10));
实现一个单例类
singleton.hpp
#ifndef SEEMMO_UTILITY_SINGLETON_HPP_
#define SEEMMO_UTILITY_SINGLETON_HPP_
namespace seemmo {
namespace utility {
// T must be: no-throw default constructible and no-throw destructible
template <typename T>
struct Singleton {
private:
struct ObjectCreator {
// This constructor does nothing more than ensure that Instance()
// is called before main() begins, thus creating the static
// T object before multithreading race issues can come up.
ObjectCreator() { Singleton<T>::Instance(); }
inline void DoNothing() const { }
};
static ObjectCreator create_object;
Singleton();
public:
typedef T ObjectType;
// If, at any point (in user code), Singleton<T>::Instance()
// is called, then the following function is instantiated.
static ObjectType& Instance() {
// This is the object that we return a reference to.
// It is guaranteed to be created before main() begins because of
// the next line.
static ObjectType obj;
// The following line does nothing else than force the instantiation
// of Singleton<T>::create_object, whose constructor is
// called before main() begins.
create_object.DoNothing();
return obj;
}
};
template <typename T>
typename Singleton<T>::ObjectCreator
Singleton<T>::create_object;
} // namespace seemmo
} // namespace utility
#endif // SEEMMO_UTILITY_SINGLETON_HPP_
SceneManager.h
#pragma once
#include "Scene.h"
#include "utility/singleton.hpp"
#include "Track.h"
#include "Target.h"
#include <thread>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace std;
#define CIF_WIDTH 352
#define CIF_HEIGHT 288
#define MAX_FRAME_NUM 100
#define PER_FRAME_SIZE (int)(CIF_WIDTH*CIF_HEIGHT*3/2) //YUV格式
struct frameData {
long frame_id;
bool end_of_stream;
int channel_id;
// data.get() + c * channel_step + w * width_step + h * height_step
int channel;
int channel_step;
int width;
int width_step;
int height;
int height_step;
std::size_t data_size =0;
uint8_t data[PER_FRAME_SIZE];
};
struct frameDataCache
{
std::list<frameData> framedata;
std::mutex frameDataMutex_;
int channel_id;
};
class SceneManager
{
private:
SceneManager();
~SceneManager();
int curVideoChannelID = -1; //-1代表没有请求
int lastVideoChannelID = -1;
int lastFrameID = -1;
void encodeAndRtmpLoop();
std::thread threadRtmp;
public:
friend struct seemmo::utility::Singleton<SceneManager>;
void Initialize(int start_channelID, int end_channelID);
void updateSceneConfig(int channelID, vector<SceneEngine::ROI*>& rois, float original_width, float original_height);
SceneEngine::UpdateResult feedAIData(int channelID,int frameIndex, vector<SceneEngine::Track>& tracks);
void feedFrame(int channelID, frameData frame);
// void updateSceneConfig(int channelID, vector<SceneEngine::Track>& tracks);
void reportEventResult(int channelID, int frameIndex,vector<SceneEngine::Track>& tracks);
frameData eventImageShot(int channelID,int frameIndex);
bool saveEventImage( int channelId, int frameIndex, IVATrafficEvent ivaEvent, const std::string& szImgPath);
void onRequestVideo(int channelID);
void onStopVideo(int channelID);
void onChannelRestored(int channelID);
std::map<int, SceneEngine::Scene *> channelId_scene_maps;
std::map<int, frameDataCache*> channelId_image_caches;
};
typedef seemmo::utility::Singleton<SceneManager> SingletonSceneManager;
使用单例类
#define SCENE_MANAGER SingletonSceneManager::Instance()
===========================================================