You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
433 lines
16 KiB
C++
433 lines
16 KiB
C++
#ifndef __PHONE_DEVICE_H__
|
|
#define __PHONE_DEVICE_H__
|
|
|
|
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
#include <map>
|
|
#include <atomic>
|
|
#include <filesystem>
|
|
#include <thread>
|
|
#include <memory>
|
|
|
|
#include <camera/NdkCameraManager.h>
|
|
#include <camera/NdkCameraError.h>
|
|
#include <camera/NdkCameraDevice.h>
|
|
#include <camera/NdkCameraMetadataTags.h>
|
|
#include <media/NdkImageReader.h>
|
|
#include <Client/Device.h>
|
|
#include <string>
|
|
|
|
#include "camera2/Camera2Helper.h"
|
|
#include "camera2/ndkcamera.h"
|
|
|
|
#include <opencv2/opencv.hpp>
|
|
#include <android/bitmap.h>
|
|
#include <android/multinetwork.h>
|
|
#include "SensorsProtocol.h"
|
|
|
|
#include "PtzController.h"
|
|
|
|
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "error", __VA_ARGS__))
|
|
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "debug", __VA_ARGS__))
|
|
|
|
#if 0
|
|
void BitmapToMat2(JNIEnv *env, jobject& bitmap, cv::Mat& mat, jboolean needUnPremultiplyAlpha) {
|
|
AndroidBitmapInfo info;
|
|
void *pixels = 0;
|
|
cv::Mat &dst = mat;
|
|
|
|
// try {
|
|
ALOGD("nBitmapToMat");
|
|
CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0);
|
|
CV_Assert(info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 ||
|
|
info.format == ANDROID_BITMAP_FORMAT_RGB_565);
|
|
CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0);
|
|
CV_Assert(pixels);
|
|
dst.create(info.height, info.width, CV_8UC4);
|
|
if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
|
|
ALOGD("nBitmapToMat: RGBA_8888 -> CV_8UC4");
|
|
cv::Mat tmp(info.height, info.width, CV_8UC4, pixels);
|
|
if (needUnPremultiplyAlpha) cvtColor(tmp, dst, cv::COLOR_mRGBA2RGBA);
|
|
else tmp.copyTo(dst);
|
|
} else {
|
|
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
|
|
ALOGD("nBitmapToMat: RGB_565 -> CV_8UC4");
|
|
cv::Mat tmp(info.height, info.width, CV_8UC2, pixels);
|
|
cvtColor(tmp, dst, cv::COLOR_BGR5652RGBA);
|
|
}
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
return;
|
|
/*
|
|
} catch (const cv::Exception &e) {
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
LOGE("nBitmapToMat catched cv::Exception: %s", e.what());
|
|
jclass je = env->FindClass("org/opencv/core/CvException");
|
|
if (!je) je = env->FindClass("java/lang/Exception");
|
|
env->ThrowNew(je, e.what());
|
|
return;
|
|
} catch (...) {
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
LOGE("nBitmapToMat catched unknown exception (...)");
|
|
jclass je = env->FindClass("java/lang/Exception");
|
|
env->ThrowNew(je, "Unknown exception in JNI code {nBitmapToMat}");
|
|
return;
|
|
}
|
|
*/
|
|
}
|
|
|
|
void BitmapToMat(JNIEnv *env, jobject& bitmap, cv::Mat& mat) {
|
|
BitmapToMat2(env, bitmap, mat, false);
|
|
}
|
|
|
|
void MatToBitmap2
|
|
(JNIEnv *env, cv::Mat& mat, jobject& bitmap, jboolean needPremultiplyAlpha) {
|
|
AndroidBitmapInfo info;
|
|
void *pixels = 0;
|
|
cv::Mat &src = mat;
|
|
|
|
// try {
|
|
ALOGD("nMatToBitmap");
|
|
CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0);
|
|
CV_Assert(info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 ||
|
|
info.format == ANDROID_BITMAP_FORMAT_RGB_565);
|
|
CV_Assert(src.dims == 2 && info.height == (uint32_t) src.rows &&
|
|
info.width == (uint32_t) src.cols);
|
|
CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4);
|
|
CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0);
|
|
CV_Assert(pixels);
|
|
if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
|
|
cv::Mat tmp(info.height, info.width, CV_8UC4, pixels);
|
|
if (src.type() == CV_8UC1) {
|
|
ALOGD("nMatToBitmap: CV_8UC1 -> RGBA_8888");
|
|
cvtColor(src, tmp, cv::COLOR_GRAY2RGBA);
|
|
} else if (src.type() == CV_8UC3) {
|
|
ALOGD("nMatToBitmap: CV_8UC3 -> RGBA_8888");
|
|
cvtColor(src, tmp, cv::COLOR_RGB2RGBA);
|
|
} else if (src.type() == CV_8UC4) {
|
|
ALOGD("nMatToBitmap: CV_8UC4 -> RGBA_8888");
|
|
if (needPremultiplyAlpha)
|
|
cvtColor(src, tmp, cv::COLOR_RGBA2mRGBA);
|
|
else
|
|
src.copyTo(tmp);
|
|
}
|
|
} else {
|
|
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
|
|
cv::Mat tmp(info.height, info.width, CV_8UC2, pixels);
|
|
if (src.type() == CV_8UC1) {
|
|
ALOGD("nMatToBitmap: CV_8UC1 -> RGB_565");
|
|
cvtColor(src, tmp, cv::COLOR_GRAY2BGR565);
|
|
} else if (src.type() == CV_8UC3) {
|
|
ALOGD("nMatToBitmap: CV_8UC3 -> RGB_565");
|
|
cvtColor(src, tmp, cv::COLOR_RGB2BGR565);
|
|
} else if (src.type() == CV_8UC4) {
|
|
ALOGD("nMatToBitmap: CV_8UC4 -> RGB_565");
|
|
cvtColor(src, tmp, cv::COLOR_RGBA2BGR565);
|
|
}
|
|
}
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
return;
|
|
/*
|
|
} catch (const cv::Exception &e) {
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
LOGE("nMatToBitmap catched cv::Exception: %s", e.what());
|
|
jclass je = env->FindClass("org/opencv/core/CvException");
|
|
if (!je) je = env->FindClass("java/lang/Exception");
|
|
env->ThrowNew(je, e.what());
|
|
return;
|
|
} catch (...) {
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
LOGE("nMatToBitmap catched unknown exception (...)");
|
|
jclass je = env->FindClass("java/lang/Exception");
|
|
env->ThrowNew(je, "Unknown exception in JNI code {nMatToBitmap}");
|
|
return;
|
|
}*/
|
|
|
|
}
|
|
|
|
void MatToBitmap(JNIEnv *env, cv::Mat& mat, jobject& bitmap) {
|
|
MatToBitmap2(env, mat, bitmap, false);
|
|
}
|
|
#endif
|
|
|
|
class PowerControl;
|
|
|
|
class CPhoneDevice : public IDevice
|
|
{
|
|
public:
|
|
|
|
friend PtzController;
|
|
|
|
struct NETWORK
|
|
{
|
|
std::string iface;
|
|
std::string ip;
|
|
std::string netmask;
|
|
std::string gateway;
|
|
};
|
|
|
|
class CPhoneCamera : public NdkCamera
|
|
{
|
|
public:
|
|
CPhoneCamera(CPhoneDevice* dev, int32_t width, int32_t height, const NdkCamera::CAMERA_PARAMS& params);
|
|
virtual ~CPhoneCamera();
|
|
virtual bool on_image(cv::Mat rgb);
|
|
virtual void on_error(const std::string& msg);
|
|
virtual void onDisconnected(ACameraDevice* device);
|
|
virtual bool onBurstCapture(std::shared_ptr<ACameraMetadata> characteristics, std::vector<std::shared_ptr<ACameraMetadata> >& results, uint32_t ldr, uint32_t duration, std::vector<std::vector<uint8_t> >& frames);
|
|
virtual bool onOneCapture(std::shared_ptr<ACameraMetadata> characteristics, std::shared_ptr<ACameraMetadata> results, uint32_t ldr, uint32_t duration, cv::Mat rgb);
|
|
virtual bool onBurstCapture(std::shared_ptr<ACameraMetadata> characteristics, std::vector<std::shared_ptr<ACameraMetadata> >& results, uint32_t ldr, uint32_t duration, std::vector<std::shared_ptr<AImage> >& frames);
|
|
|
|
protected:
|
|
CPhoneDevice* m_dev;
|
|
};
|
|
|
|
class CJpegCamera : public CPhoneCamera
|
|
{
|
|
public:
|
|
CJpegCamera(CPhoneDevice* dev, int32_t width, int32_t height, const std::string& path, const NdkCamera::CAMERA_PARAMS& params);
|
|
|
|
virtual void onImageAvailable(AImageReader* reader);
|
|
virtual int32_t getOutputFormat() const;
|
|
virtual bool onOneCapture(std::shared_ptr<ACameraMetadata> characteristics, std::shared_ptr<ACameraMetadata> results, uint32_t ldr, uint32_t duration, cv::Mat rgb);
|
|
virtual bool onBurstCapture(std::shared_ptr<ACameraMetadata> characteristics, std::vector<std::shared_ptr<ACameraMetadata> >& results, uint32_t ldr, uint32_t duration, std::vector<std::vector<uint8_t> >& frames);
|
|
virtual bool onBurstCapture(std::shared_ptr<ACameraMetadata> characteristics, std::vector<std::shared_ptr<ACameraMetadata> >& results, uint32_t ldr, uint32_t duration, std::vector<std::shared_ptr<AImage> >& frames);
|
|
|
|
protected:
|
|
std::string m_path;
|
|
};
|
|
|
|
struct TIMER_CONTEXT
|
|
{
|
|
CPhoneDevice* device;
|
|
unsigned int timerType;
|
|
uint64_t times;
|
|
void* data;
|
|
uint64_t expectedTimes;
|
|
uint64_t uid;
|
|
};
|
|
|
|
CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPath, uint64_t activeNetHandle, unsigned int versionCode, const std::string& nativeLibDir);
|
|
virtual ~CPhoneDevice();
|
|
|
|
virtual void SetListener(IListener* listener);
|
|
virtual void SetRecognizationCfg(const CFG_RECOGNIZATION* pRecognizationCfg);
|
|
virtual bool BindNetwork(int sock);
|
|
virtual bool SelfTest(std::string& result);
|
|
virtual bool UpdateTime(time_t ts);
|
|
virtual bool UpdateSchedules();
|
|
virtual bool QuerySystemProperties(map<string, string>& properties);
|
|
virtual bool InstallAPP(const std::string& path, unsigned int delayedTime);
|
|
virtual bool Reboot(int resetType, bool manually, const std::string& reason, uint32_t timeout = 1000);
|
|
virtual bool EnableGPS(bool enabled);
|
|
virtual float QueryBattaryVoltage(int timesForAvg, bool* isCharging);
|
|
virtual bool RequestPosition();
|
|
virtual timer_uid_t RegisterHeartbeat(unsigned int timerType, unsigned int timeout, time_t tsForNextPhoto);
|
|
virtual bool TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector<OSD_INFO>& osds, const std::string& path);
|
|
virtual bool CloseCamera();
|
|
virtual timer_uid_t RegisterTimer(unsigned int timerType, unsigned int timeout, void* data, uint64_t times = 0);
|
|
virtual bool UnregisterTimer(timer_uid_t uid);
|
|
virtual uint64_t RequestWakelock(uint64_t timeout);
|
|
virtual bool ReleaseWakelock(uint64_t wakelock);
|
|
|
|
virtual int GetWData(WEATHER_INFO *weatherInfo, D_SENSOR_PARAM *sensorParam);
|
|
virtual int GetIceData(ICE_INFO *iceInfo, ICE_TAIL *icetail, D_SENSOR_PARAM *sensorParam);
|
|
virtual bool OpenSensors(int sensortype);
|
|
virtual bool CloseSensors(int sensortype, uint32_t delayedCloseTime);
|
|
virtual bool OpenPTZSensors(uint32_t sec);
|
|
virtual bool ClosePTZSensors(uint32_t delayedCloseTime);
|
|
virtual bool GetPTZSensorsStatus(time_t waittime);
|
|
virtual bool GetCameraStatus();
|
|
virtual void CameraCtrl(unsigned short waitTime, unsigned short delayTime, unsigned char channel, int cmdidx, unsigned char presetno, const char *serfile, unsigned int baud, int addr);
|
|
virtual int GetSerialPhoto(int devno, D_IMAGE_DEF *photo);
|
|
virtual void InitSerialComm(D_SENSOR_PARAM *sensorParam, char *filedir,const char *logpath);
|
|
|
|
bool LoadNetworkInfo();
|
|
bool GetNextScheduleItem(uint32_t tsBasedZero, uint32_t scheduleTime, vector<uint32_t>& items);
|
|
|
|
void UpdatePosition(double lon, double lat, double radius, time_t ts);
|
|
bool OnVideoReady(bool photoOrVideo, bool result, const char* path, unsigned int photoId);
|
|
bool OnCaptureReady(bool photoOrVideo, bool result, cv::Mat mat, unsigned int photoId);
|
|
|
|
void UpdateSignalLevel(int signalLevel);
|
|
void UpdateTfCardPath(const std::string& tfCardPath)
|
|
{
|
|
m_tfCardPath = tfCardPath;
|
|
}
|
|
void SetBuildTime(time_t buildTime)
|
|
{
|
|
mBuildTime = buildTime;
|
|
}
|
|
void UpdateSimcard(const std::string& simcard);
|
|
void UpdateNetwork(net_handle_t nethandle, bool available, bool defaultOrEthernet, bool& changed);
|
|
|
|
net_handle_t GetEthnetHandle() const;
|
|
|
|
protected:
|
|
|
|
std::string GetFileName() const;
|
|
std::string GetVersion() const;
|
|
|
|
bool SendBroadcastMessage(std::string action, int value);
|
|
// bool MatchCaptureSizeRequest(ACameraManager *cameraManager, const char *selectedCameraId, unsigned int width, unsigned int height, uint32_t cameraOrientation_,
|
|
|
|
bool TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, const std::string& path, std::vector<IDevice::OSD_INFO>& osds, std::shared_ptr<PowerControl> powerCtrlPtr);
|
|
bool TakeVideoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, const std::string& path, std::vector<IDevice::OSD_INFO>& osds, std::shared_ptr<PowerControl> powerCtrlPtr);
|
|
bool StartPushStreaming(IDevice::PHOTO_INFO& localPhotoInfo, const std::string& url, std::vector<IDevice::OSD_INFO>& osds, std::shared_ptr<PowerControl> powerCtrlPtr);
|
|
bool PostProcessPhoto(const PHOTO_INFO& photoInfo, const vector<IDevice::OSD_INFO>& osds, const std::string& path, const std::string& cameraInfo, cv::Mat mat);
|
|
inline bool TakePhotoCb(int res, const IDevice::PHOTO_INFO& photoInfo, const string& path, time_t photoTime, const std::vector<IDevice::RECOG_OBJECT>& objects) const
|
|
{
|
|
if (m_listener != NULL)
|
|
{
|
|
return m_listener->OnPhotoTaken(res, photoInfo, path, photoTime, objects);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
inline bool TakePhotoCb(int result, const IDevice::PHOTO_INFO& photoInfo, const string& path, time_t photoTime) const
|
|
{
|
|
if (m_listener != NULL)
|
|
{
|
|
std::vector<IDevice::RECOG_OBJECT> objects;
|
|
return m_listener->OnPhotoTaken(result, photoInfo, path, photoTime, objects);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
inline bool TakePTZPhotoCb(int result, const IDevice::PHOTO_INFO& photoInfo) const
|
|
{
|
|
if (m_listener != NULL)
|
|
{
|
|
std::vector<IDevice::RECOG_OBJECT> objects;
|
|
return m_listener->OnPTZPhotoTaken(result, photoInfo);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
inline bool GetPhotoSerialsParamCb(SerialsPhotoParam ¶m) const
|
|
{
|
|
if (m_listener != NULL)
|
|
{
|
|
return m_listener->OnPhotoSerialsParamGet(param);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void QueryPowerInfo(std::map<std::string, std::string>& powerInfo);
|
|
std::string QueryCpuTemperature();
|
|
|
|
bool OnImageReady(cv::Mat mat);
|
|
bool onOneCapture(std::shared_ptr<ACameraMetadata> characteristics, std::shared_ptr<ACameraMetadata> results, uint32_t ldr, uint32_t duration, cv::Mat rgb);
|
|
bool onBurstCapture(std::shared_ptr<ACameraMetadata> characteristics, std::vector<std::shared_ptr<ACameraMetadata> >& results, uint32_t ldr, uint32_t duration, std::vector<std::vector<uint8_t> >& frames);
|
|
bool onBurstCapture(std::shared_ptr<ACameraMetadata> characteristics, std::vector<std::shared_ptr<ACameraMetadata> >& results, uint32_t ldr, uint32_t duration, std::vector<std::shared_ptr<AImage> >& frames);
|
|
void onError(const std::string& msg);
|
|
void onDisconnected(ACameraDevice* device);
|
|
|
|
void CloseCamera2(CPhoneCamera* camera, unsigned int photoId, unsigned char cameraType);
|
|
|
|
static void handleSignal(int sig, siginfo_t *si, void *uc);
|
|
bool RegisterHandlerForSignal(int sig);
|
|
void static handleTimer(union sigval v);
|
|
void handleTimerImpl(TIMER_CONTEXT* context);
|
|
void static handleRebootTimer(union sigval v);
|
|
// void handleRebootTimerImpl();
|
|
void RestartApp(int rebootType, long timeout, const std::string& reason);
|
|
|
|
int QueryBatteryVoltage(int retries);
|
|
|
|
int CallExecv(int rotation, int frontCamera, const std::string& outputPath, const std::vector<std::string>& images);
|
|
|
|
void SetStaticIp(const std::string& iface, const std::string& ip, const std::string& netmask, const std::string& gateway);
|
|
void ConvertDngToPng(const std::string& dngPath, const std::string& pngPath);
|
|
void SetStaticIp();
|
|
|
|
protected:
|
|
|
|
mutable std::mutex m_devLocker;
|
|
|
|
JavaVM* m_vm;
|
|
jobject m_javaService;
|
|
std::string m_appPath;
|
|
std::string m_tfCardPath;
|
|
std::string m_nativeLibraryDir;
|
|
|
|
NETWORK* m_network;
|
|
net_handle_t m_defNetHandle;
|
|
net_handle_t m_ethnetHandle;
|
|
|
|
jmethodID mRegisterHeartbeatMid;
|
|
jmethodID mUpdateCaptureScheduleMid;
|
|
jmethodID mUpdateTimeMid;
|
|
jmethodID mStartRecordingMid;
|
|
|
|
jmethodID mRequestWakelockMid;
|
|
jmethodID mReleaseWakelockMid;
|
|
|
|
jmethodID mGetSystemInfoMid;
|
|
|
|
jmethodID mRebootMid;
|
|
jmethodID mInstallAppMid;
|
|
jmethodID mEnableGpsMid;
|
|
jmethodID mRequestPositionMid;
|
|
jmethodID mExecHdrplusMid;
|
|
jmethodID mSetStaticIpMid;
|
|
|
|
jmethodID mConvertDngToPngMid;
|
|
|
|
jmethodID mCallSysCameraMid;
|
|
|
|
std::string mPath;
|
|
IDevice::PHOTO_INFO mPhotoInfo;
|
|
vector<IDevice::OSD_INFO> mOsds;
|
|
IListener* m_listener;
|
|
const CFG_RECOGNIZATION* m_pRecognizationCfg;
|
|
bool mAIInitialized;
|
|
unsigned int mVersionCode;
|
|
time_t mBuildTime;
|
|
|
|
atomic_ullong m_timerUidFeed;
|
|
atomic_ullong m_wakelockIdFeed;
|
|
atomic_ullong m_uniqueIdFeed;
|
|
std::map<IDevice::timer_uid_t, TIMER_CONTEXT*> mTimers;
|
|
|
|
mutable CPhoneCamera* mCamera;
|
|
|
|
time_t mHeartbeatStartTime;
|
|
unsigned int mHeartbeatDuration;
|
|
|
|
std::thread m_threadClose;
|
|
std::shared_ptr<PowerControl> m_powerCtrlPtr;
|
|
|
|
int m_signalLevel;
|
|
time_t m_signalLevelUpdateTime;
|
|
|
|
std::string m_simcard;
|
|
|
|
mutable std::mutex m_cameraLocker;
|
|
time_t m_lastTime;
|
|
std::atomic<bool> m_shouldStopWaiting;
|
|
std::atomic<bool> m_isSelfTesting{false};
|
|
|
|
IDevice::ICE_TAIL m_tempData;
|
|
mutable std::mutex m_dataLocker;
|
|
|
|
mutable std::mutex m_collectDataLocker;
|
|
std::condition_variable m_CollectDatacv;
|
|
std::atomic<bool> m_collecting;
|
|
unsigned long long localDelayTime;
|
|
|
|
|
|
PtzController* m_ptzController;
|
|
|
|
};
|
|
|
|
|
|
#endif // __PHONE_DEVICE_H__
|