Improve: files database.
This commit is contained in:
parent
8e07140ca3
commit
f10edc656b
|
@ -31,6 +31,7 @@ enum class InternalStateEvent
|
|||
KEY_EVENT_HANDLE,
|
||||
RESET_KEY_MEDIA_TASK,
|
||||
MEDIA_HANDLE_STATE_TASK_TIME_OUT,
|
||||
MEDIA_HANDLE_STATE_TASK_FINISHED,
|
||||
END
|
||||
};
|
||||
typedef struct key_event_data
|
||||
|
|
|
@ -14,9 +14,12 @@
|
|||
*/
|
||||
#include "MediaHandleState.h"
|
||||
#include "DataProcessing.h"
|
||||
#include "IFilesManager.h"
|
||||
#include "ILog.h"
|
||||
#include "IMediaManager.h"
|
||||
#include "IMissionManager.h"
|
||||
#include "IStateMachine.h"
|
||||
#include "MediaTask.h"
|
||||
#include "MediaTaskHandle.h"
|
||||
#include "MissionStateMachine.h"
|
||||
#include <functional>
|
||||
|
@ -26,6 +29,8 @@ MediaHandleState::MediaHandleState() : State("MediaHandleState")
|
|||
{
|
||||
mEventHandle[InternalStateEvent::RESET_KEY_MEDIA_TASK] =
|
||||
std::bind(&MediaHandleState::ResetKeyMediaTaskHandle, this, _1);
|
||||
mEventHandle[InternalStateEvent::MEDIA_HANDLE_STATE_TASK_FINISHED] =
|
||||
std::bind(&MediaHandleState::MediaTaskFinishedHandle, this, _1);
|
||||
}
|
||||
void MediaHandleState::GoInState()
|
||||
{
|
||||
|
@ -42,11 +47,28 @@ bool MediaHandleState::ExecuteStateMsg(VStateMachineData *msg)
|
|||
{
|
||||
return DataProcessing::EventHandle(msg);
|
||||
}
|
||||
void MediaHandleState::TaskResponse(const std::vector<MediaTaskResponse> &response)
|
||||
void MediaHandleState::TaskResponse(const MediaTaskInfo &taskinfo)
|
||||
{
|
||||
std::shared_ptr<VMissionData> message = std::make_shared<VMissionDataV2<MediaTaskInfo>>(
|
||||
static_cast<MissionEvent>(InternalStateEvent::MEDIA_HANDLE_STATE_TASK_FINISHED), taskinfo);
|
||||
MissionStateMachine::GetInstance()->SendStateMessage(message);
|
||||
}
|
||||
bool MediaHandleState::ResetKeyMediaTaskHandle(VStateMachineData *msg)
|
||||
{
|
||||
MakeSingleTask(InternalStateEvent::RESET_KEY_MEDIA_TASK, shared_from_this());
|
||||
MediaTaskHandle::MakeSingleTask(InternalStateEvent::RESET_KEY_MEDIA_TASK, shared_from_this());
|
||||
return EXECUTED;
|
||||
}
|
||||
bool MediaHandleState::MediaTaskFinishedHandle(VStateMachineData *msg)
|
||||
{
|
||||
std::shared_ptr<MissionMessage> message = std::dynamic_pointer_cast<MissionMessage>(msg->GetMessageObj());
|
||||
std::shared_ptr<VMissionDataV2<MediaTaskInfo>> data =
|
||||
std::dynamic_pointer_cast<VMissionDataV2<MediaTaskInfo>>(message->mMissionData);
|
||||
LogInfo("response files = %d.\n", data->mData.mResponse.size());
|
||||
std::vector<SyncFileInfo> files;
|
||||
for (auto &response : data->mData.mResponse) {
|
||||
SyncFileInfo file(data->mData.mSerialNumber, response.mFileName, 0, 0, FileCreateType::END, FileStatus::END);
|
||||
files.push_back(file);
|
||||
}
|
||||
IFilesManager::GetInstance()->SaveFiles(files);
|
||||
return EXECUTED;
|
||||
}
|
|
@ -31,7 +31,8 @@ public:
|
|||
bool ExecuteStateMsg(VStateMachineData *msg) override;
|
||||
|
||||
private:
|
||||
void TaskResponse(const std::vector<MediaTaskResponse> &response) override;
|
||||
void TaskResponse(const MediaTaskInfo &taskinfo) override;
|
||||
bool ResetKeyMediaTaskHandle(VStateMachineData *msg);
|
||||
bool MediaTaskFinishedHandle(VStateMachineData *msg);
|
||||
};
|
||||
#endif
|
|
@ -14,6 +14,7 @@
|
|||
*/
|
||||
#include "MediaTask.h"
|
||||
#include "DataProcessing.h"
|
||||
#include "ILog.h"
|
||||
#include "IMediaManager.h"
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
|
@ -24,6 +25,7 @@
|
|||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
MediaTask::MediaTask(const MediaTaskType &type, const InternalStateEvent &bindEvent,
|
||||
const std::weak_ptr<VMediaTaskIniator> &iniator, const unsigned long &serialNumber,
|
||||
const std::string &savePath)
|
||||
|
@ -49,4 +51,18 @@ std::string MediaTask::GetTargetNameForSaving(void)
|
|||
pathStream << mSavePath << std::setw(2) << std::setfill('0') << hour << std::setw(2) << std::setfill('0') << minute
|
||||
<< std::setw(2) << std::setfill('0') << second << ".mp4";
|
||||
return pathStream.str();
|
||||
}
|
||||
void MediaTask::Response(const std::vector<MediaTaskResponse> &response)
|
||||
{
|
||||
LogInfo("Response handle.\n");
|
||||
auto iniator = mIniator.lock();
|
||||
if (mIniator.expired()) {
|
||||
LogWarning("mIniator is expired.\n");
|
||||
return;
|
||||
}
|
||||
MediaTaskInfo info = {
|
||||
.mResponse = response,
|
||||
.mSerialNumber = mSerialNumber,
|
||||
};
|
||||
iniator->TaskResponse(info);
|
||||
}
|
|
@ -18,12 +18,17 @@
|
|||
#include "IMediaManager.h"
|
||||
#include <string>
|
||||
constexpr unsigned int MEDIA_TASK_TIMEOUT_MS = 1000 * 60;
|
||||
typedef struct media_task_info
|
||||
{
|
||||
const std::vector<MediaTaskResponse> mResponse;
|
||||
const unsigned long mSerialNumber;
|
||||
} MediaTaskInfo;
|
||||
class VMediaTaskIniator
|
||||
{
|
||||
public:
|
||||
VMediaTaskIniator() = default;
|
||||
virtual ~VMediaTaskIniator() = default;
|
||||
virtual void TaskResponse(const std::vector<MediaTaskResponse> &response) = 0;
|
||||
virtual void TaskResponse(const MediaTaskInfo &taskinfo) = 0;
|
||||
};
|
||||
class MediaTask : public VMediaTask
|
||||
{
|
||||
|
@ -34,6 +39,7 @@ public:
|
|||
virtual ~MediaTask() = default;
|
||||
virtual unsigned int GetTaskTimeOutMs(void);
|
||||
std::string GetTargetNameForSaving(void) override;
|
||||
void Response(const std::vector<MediaTaskResponse> &response) override;
|
||||
|
||||
private:
|
||||
const MediaTaskType mType;
|
||||
|
|
|
@ -48,7 +48,7 @@ void MediaTaskHandle::MakeSingleTask(const InternalStateEvent &bindEvent,
|
|||
}
|
||||
auto code = mMediaHandle->ExecuteTask(task);
|
||||
if (IsCodeOK(code)) {
|
||||
mRuningTask = task;
|
||||
mRuningTask = task; // task should be saved here.
|
||||
long long timeOut = std::dynamic_pointer_cast<MediaTask>(task)->GetTaskTimeOutMs();
|
||||
std::shared_ptr<VMissionData> message = std::make_shared<VMissionData>(
|
||||
static_cast<MissionEvent>(InternalStateEvent::MEDIA_HANDLE_STATE_TASK_TIME_OUT));
|
||||
|
|
|
@ -16,6 +16,40 @@
|
|||
#define I_FILES_MANAGER_H
|
||||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <vector>
|
||||
enum class FileCreateType
|
||||
{
|
||||
PIR = 0,
|
||||
MANUAL_TEST,
|
||||
MANUAL_PHONE,
|
||||
TIMED,
|
||||
END
|
||||
};
|
||||
enum class FileStatus
|
||||
{
|
||||
RECORDING = 0,
|
||||
FINISHED_RECORD,
|
||||
SHOULD_BE_UPLOAD,
|
||||
UPLOADING,
|
||||
UPLOADED,
|
||||
END
|
||||
};
|
||||
constexpr unsigned long UNDEFINE_SERIAL_NUMBER = -1;
|
||||
constexpr unsigned long UNDEFINE_FILE_SIZE = 0;
|
||||
constexpr time_t UNDEFINE_CREATE_TIME = -1;
|
||||
typedef struct sync_file_info
|
||||
{
|
||||
sync_file_info(const unsigned long &serialNumber, const std::string &fileName, const unsigned long &fileSize,
|
||||
const time_t &createTime_s, const FileCreateType &type, const FileStatus &status);
|
||||
const unsigned long mSerialNumber;
|
||||
const std::string mFileName;
|
||||
const unsigned long mFileSize;
|
||||
const time_t mCreateTime_s;
|
||||
const FileCreateType mType;
|
||||
const FileStatus mStatus;
|
||||
} SyncFileInfo;
|
||||
typedef struct save_file_info
|
||||
{
|
||||
save_file_info(const std::string &fileName);
|
||||
|
@ -39,5 +73,6 @@ public:
|
|||
virtual StatusCode UnInit(void);
|
||||
virtual StatusCode SaveFile(const SaveFileInfo &fileInfo);
|
||||
virtual InfoToBeSaved GetInfoForSavingFiles(const unsigned int &count);
|
||||
virtual StatusCode SaveFiles(const std::vector<SyncFileInfo> &info);
|
||||
};
|
||||
#endif
|
|
@ -24,7 +24,8 @@ public:
|
|||
virtual ~FilesDatabase() = default;
|
||||
void Init(void);
|
||||
void UnInit(void);
|
||||
StatusCode DatabaseSaveFile(const SaveFileInfo &fileInfo);
|
||||
StatusCode DbSaveFile(const SaveFileInfo &fileInfo);
|
||||
InfoToBeSaved CreateInfoForSavingFiles(const unsigned int &count);
|
||||
StatusCode DbSaveFiles(const std::vector<SyncFileInfo> &info);
|
||||
};
|
||||
#endif
|
|
@ -17,6 +17,7 @@
|
|||
// #include "IStorageManager.h"
|
||||
#include "FilesDatabase.h"
|
||||
#include "StatusCode.h"
|
||||
#include <vector>
|
||||
StatusCode FilesManagerImpl::Init(void)
|
||||
{
|
||||
FilesDatabase::Init();
|
||||
|
@ -29,9 +30,13 @@ StatusCode FilesManagerImpl::UnInit(void)
|
|||
}
|
||||
StatusCode FilesManagerImpl::SaveFile(const SaveFileInfo &fileInfo)
|
||||
{
|
||||
return FilesDatabase::DatabaseSaveFile(fileInfo);
|
||||
return FilesDatabase::DbSaveFile(fileInfo);
|
||||
}
|
||||
InfoToBeSaved FilesManagerImpl::GetInfoForSavingFiles(const unsigned int &count)
|
||||
{
|
||||
return FilesDatabase::CreateInfoForSavingFiles(count);
|
||||
}
|
||||
StatusCode FilesManagerImpl::SaveFiles(const std::vector<SyncFileInfo> &info)
|
||||
{
|
||||
return FilesDatabase::DbSaveFiles(info);
|
||||
}
|
|
@ -28,5 +28,6 @@ protected:
|
|||
StatusCode UnInit(void) override;
|
||||
StatusCode SaveFile(const SaveFileInfo &fileInfo) override;
|
||||
InfoToBeSaved GetInfoForSavingFiles(const unsigned int &count) override;
|
||||
StatusCode SaveFiles(const std::vector<SyncFileInfo> &info) override;
|
||||
};
|
||||
#endif
|
|
@ -17,9 +17,18 @@
|
|||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <vector>
|
||||
save_file_info::save_file_info(const std::string &fileName) : mFileName(fileName)
|
||||
{
|
||||
}
|
||||
sync_file_info::sync_file_info(const unsigned long &serialNumber, const std::string &fileName,
|
||||
const unsigned long &fileSize, const time_t &createTime_s, const FileCreateType &type,
|
||||
const FileStatus &status)
|
||||
: mSerialNumber(serialNumber), mFileName(fileName), mFileSize(fileSize), mCreateTime_s(createTime_s), mType(type),
|
||||
mStatus(status)
|
||||
{
|
||||
}
|
||||
info_to_be_saved::info_to_be_saved(const unsigned long &serialNumber, const std::string mSavingPath)
|
||||
: mSerialNumber(serialNumber), mSavingPath(mSavingPath)
|
||||
{
|
||||
|
@ -58,4 +67,9 @@ InfoToBeSaved IFilesManager::GetInfoForSavingFiles(const unsigned int &count)
|
|||
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
InfoToBeSaved info(0, ".");
|
||||
return info;
|
||||
}
|
||||
StatusCode IFilesManager::SaveFiles(const std::vector<SyncFileInfo> &info)
|
||||
{
|
||||
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
|
@ -20,6 +20,7 @@
|
|||
#include "SqliteHandle.h"
|
||||
#include "StatusCode.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#define DATABASE_FILE_NAME "/files.db"
|
||||
void FilesDatabase::Init(void)
|
||||
{
|
||||
|
@ -31,7 +32,7 @@ void FilesDatabase::UnInit(void)
|
|||
{
|
||||
SqliteHandle::GetInstance()->UnInit();
|
||||
}
|
||||
StatusCode FilesDatabase::DatabaseSaveFile(const SaveFileInfo &fileInfo)
|
||||
StatusCode FilesDatabase::DbSaveFile(const SaveFileInfo &fileInfo)
|
||||
{
|
||||
std::string saveFile = FilesHandle::CreateFilePathName(fileInfo.mFileName);
|
||||
LogInfo("Save file:%s\n", saveFile.c_str());
|
||||
|
@ -52,4 +53,15 @@ InfoToBeSaved FilesDatabase::CreateInfoForSavingFiles(const unsigned int &count)
|
|||
std::string savingPath = IStorageManager::GetInstance()->GetFilesSavingPath();
|
||||
InfoToBeSaved info(key, savingPath);
|
||||
return info;
|
||||
}
|
||||
StatusCode FilesDatabase::DbSaveFiles(const std::vector<SyncFileInfo> &info)
|
||||
{
|
||||
for (auto &each : info) {
|
||||
bool result = SqliteHandle::GetInstance()->SyncFile(each);
|
||||
if (!result) {
|
||||
LogError("Save file failed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
|
@ -13,11 +13,15 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
#include "SqliteHandle.h"
|
||||
#include "IFilesManager.h"
|
||||
#include "ILog.h"
|
||||
#include "sqlite3.h"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#define FILES_TABLE "files"
|
||||
#define TABLE_KEY "key"
|
||||
#define FILE_PATH "path"
|
||||
|
@ -25,11 +29,16 @@
|
|||
#define FILE_TYPE "type"
|
||||
#define FILE_SIZE "size"
|
||||
#define FILE_STATUS "status"
|
||||
#define FILE_TYPE_BE_RECORDING "recording"
|
||||
#define FILE_TYPE_COMPLETE_RECORD "complete_record"
|
||||
#define FILE_TYPE_SHOULD_BE_UPLOADED "should_be_uploaded"
|
||||
#define FILE_TYPE_UPLOADING "uploading"
|
||||
#define FILE_TYPE_UPLOADED "uploaded"
|
||||
#define FILE_STATUS_BE_RECORDING "recording"
|
||||
#define FILE_STATUS_COMPLETE_RECORD "complete_record"
|
||||
#define FILE_STATUS_SHOULD_BE_UPLOADED "should_be_uploaded"
|
||||
#define FILE_STATUS_UPLOADING "uploading"
|
||||
#define FILE_STATUS_UPLOADED "uploaded"
|
||||
#define FIEL_TYPE_PIR "pir"
|
||||
#define FIEL_TYPE_MANUAL_TEST "manual_test"
|
||||
#define FIEL_TYPE_MANUAL_PHONE "manual_phone"
|
||||
#define FIEL_TYPE_TIMED "timed"
|
||||
#define FILE_TYPE_UNDEFINE "undefine"
|
||||
SqliteHandle::SqliteHandle() : mDb(nullptr)
|
||||
{
|
||||
}
|
||||
|
@ -73,8 +82,9 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
|
|||
{
|
||||
char *err_msg = nullptr;
|
||||
int rc = SQLITE_UNDEFINE;
|
||||
const char *sql = "INSERT INTO " FILES_TABLE " (" FILE_PATH ", " CREATE_TIME ", " FILE_TYPE ", " FILE_SIZE
|
||||
") VALUES (\"\", 0, \"" FILE_TYPE_BE_RECORDING "\", 0);";
|
||||
const char *sql =
|
||||
"INSERT INTO " FILES_TABLE " (" FILE_PATH ", " CREATE_TIME ", " FILE_TYPE ", " FILE_SIZE ", " FILE_STATUS
|
||||
") VALUES (\"\", 0, \"" FILE_TYPE_UNDEFINE "\", 0, \"" FILE_STATUS_BE_RECORDING "\");";
|
||||
for (unsigned int i = 0; i < count; ++i) {
|
||||
rc = sqlite3_exec(mDb, sql, nullptr, nullptr, &err_msg);
|
||||
if (SQLITE_OK != rc) {
|
||||
|
@ -98,7 +108,30 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
|
|||
sqlite3_free(err_msg);
|
||||
return 0;
|
||||
}
|
||||
return last_rowid - count + 1;
|
||||
return last_rowid - count;
|
||||
}
|
||||
bool SqliteHandle::SyncFile(const SyncFileInfo &info)
|
||||
{
|
||||
if (UNDEFINE_SERIAL_NUMBER == info.mSerialNumber) {
|
||||
LogError("Serial number is undefine.\n");
|
||||
return false;
|
||||
}
|
||||
if (UpdateCreateTime(mDb, info.mSerialNumber, info.mCreateTime_s) == false) {
|
||||
return false;
|
||||
}
|
||||
if (UpdateFileName(mDb, info.mSerialNumber, info.mFileName) == false) {
|
||||
return false;
|
||||
}
|
||||
if (UpdateFileSize(mDb, info.mSerialNumber, info.mFileSize) == false) {
|
||||
return false;
|
||||
}
|
||||
if (UpdateFileStatus(mDb, info.mSerialNumber, info.mStatus) == false) {
|
||||
return false;
|
||||
}
|
||||
if (UpdateFileType(mDb, info.mSerialNumber, info.mType) == false) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void SqliteHandle::DbInit(sqlite3 *db)
|
||||
{
|
||||
|
@ -110,7 +143,7 @@ void SqliteHandle::DbInit(sqlite3 *db)
|
|||
int rc = SQLITE_UNDEFINE;
|
||||
const char *sql =
|
||||
"CREATE TABLE IF NOT EXISTS " FILES_TABLE " (" TABLE_KEY " INTEGER PRIMARY KEY AUTOINCREMENT, " FILE_PATH
|
||||
" TEXT, " CREATE_TIME " INTEGER," FILE_TYPE " TEXT, " FILE_SIZE " INTEGER);";
|
||||
" TEXT, " CREATE_TIME " INTEGER," FILE_TYPE " TEXT, " FILE_SIZE " INTEGER, " FILE_STATUS " TEXT);";
|
||||
rc = sqlite3_exec(db, sql, nullptr, nullptr, &errMsg);
|
||||
if (SQLITE_OK != rc) {
|
||||
LogError("Sql: %s, errMsg: %s\n", sql, errMsg);
|
||||
|
@ -119,4 +152,148 @@ void SqliteHandle::DbInit(sqlite3 *db)
|
|||
else {
|
||||
LogInfo("Sqlite init success. table = %s\n", FILES_TABLE);
|
||||
}
|
||||
}
|
||||
bool SqliteHandle::UpdateCreateTime(sqlite3 *db, const unsigned long &key, const time_t &createTime)
|
||||
{
|
||||
if (nullptr == db) {
|
||||
LogError("db is null.\n");
|
||||
return false;
|
||||
}
|
||||
char *errMsg = nullptr;
|
||||
int rc = SQLITE_UNDEFINE;
|
||||
if (UNDEFINE_CREATE_TIME != createTime) {
|
||||
std::stringstream sqlStream;
|
||||
sqlStream << "UPDATE " FILES_TABLE " SET " CREATE_TIME " = '" << createTime << "' WHERE " TABLE_KEY " = " << key
|
||||
<< ";";
|
||||
LogInfo("Sql: %s\n", sqlStream.str().c_str());
|
||||
rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
|
||||
if (SQLITE_OK != rc) {
|
||||
LogError("SQL error: %s\n", errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool SqliteHandle::UpdateFileName(sqlite3 *db, const unsigned long &key, const std::string &fileName)
|
||||
{
|
||||
if (nullptr == db) {
|
||||
LogError("db is null.\n");
|
||||
return false;
|
||||
}
|
||||
char *errMsg = nullptr;
|
||||
int rc = SQLITE_UNDEFINE;
|
||||
if (fileName.empty() == false) {
|
||||
std::stringstream sqlStream;
|
||||
sqlStream << "UPDATE " FILES_TABLE " SET " FILE_PATH " = '" << fileName << "' WHERE " TABLE_KEY " = " << key
|
||||
<< ";";
|
||||
LogInfo("Sql: %s\n", sqlStream.str().c_str());
|
||||
rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
|
||||
if (SQLITE_OK != rc) {
|
||||
LogError("SQL error: %s\n", errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool SqliteHandle::UpdateFileSize(sqlite3 *db, const unsigned long &key, const unsigned long &fileSize)
|
||||
{
|
||||
if (nullptr == db) {
|
||||
LogError("db is null.\n");
|
||||
return false;
|
||||
}
|
||||
char *errMsg = nullptr;
|
||||
int rc = SQLITE_UNDEFINE;
|
||||
if (UNDEFINE_FILE_SIZE != fileSize) {
|
||||
std::stringstream sqlStream;
|
||||
sqlStream << "UPDATE " FILES_TABLE " SET " FILE_SIZE " = '" << fileSize << "' WHERE " TABLE_KEY " = " << key
|
||||
<< ";";
|
||||
LogInfo("Sql: %s\n", sqlStream.str().c_str());
|
||||
rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
|
||||
if (SQLITE_OK != rc) {
|
||||
LogError("SQL error: %s\n", errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool SqliteHandle::UpdateFileType(sqlite3 *db, const unsigned long &key, const FileCreateType &type)
|
||||
{
|
||||
if (nullptr == db) {
|
||||
LogError("db is null.\n");
|
||||
return false;
|
||||
}
|
||||
char *errMsg = nullptr;
|
||||
int rc = SQLITE_UNDEFINE;
|
||||
if (FileCreateType::END != type) {
|
||||
std::stringstream sqlStream;
|
||||
sqlStream << "UPDATE " FILES_TABLE " SET " FILE_TYPE " = '" << ConvertFileType(type)
|
||||
<< "' WHERE " TABLE_KEY " = " << key << ";";
|
||||
LogInfo("Sql: %s\n", sqlStream.str().c_str());
|
||||
rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
|
||||
if (SQLITE_OK != rc) {
|
||||
LogError("SQL error: %s\n", errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::string SqliteHandle::ConvertFileType(const FileCreateType &type)
|
||||
{
|
||||
switch (type) {
|
||||
case FileCreateType::PIR:
|
||||
return "PIR";
|
||||
case FileCreateType::MANUAL_TEST:
|
||||
return "MANUAL_TEST";
|
||||
case FileCreateType::MANUAL_PHONE:
|
||||
return "MANUAL_PHONE";
|
||||
case FileCreateType::TIMED:
|
||||
return "TIMED";
|
||||
case FileCreateType::END:
|
||||
default:
|
||||
return "undefine";
|
||||
}
|
||||
}
|
||||
bool SqliteHandle::UpdateFileStatus(sqlite3 *db, const unsigned long &key, const FileStatus &status)
|
||||
{
|
||||
if (nullptr == db) {
|
||||
LogError("db is null.\n");
|
||||
return false;
|
||||
}
|
||||
char *errMsg = nullptr;
|
||||
int rc = SQLITE_UNDEFINE;
|
||||
if (FileStatus::END != status) {
|
||||
std::stringstream sqlStream;
|
||||
sqlStream << "UPDATE " FILES_TABLE " SET " FILE_STATUS " = '" << ConvertFileStatus(status)
|
||||
<< "' WHERE " TABLE_KEY " = " << key << ";";
|
||||
LogInfo("Sql: %s\n", sqlStream.str().c_str());
|
||||
rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
|
||||
if (SQLITE_OK != rc) {
|
||||
LogError("SQL error: %s\n", errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::string SqliteHandle::ConvertFileStatus(const FileStatus &status)
|
||||
{
|
||||
switch (status) {
|
||||
case FileStatus::RECORDING:
|
||||
return "RECORDING";
|
||||
case FileStatus::FINISHED_RECORD:
|
||||
return "FINISHED_RECORD";
|
||||
case FileStatus::SHOULD_BE_UPLOAD:
|
||||
return "SHOULD_BE_UPLOAD";
|
||||
case FileStatus::UPLOADING:
|
||||
return "UPLOADING";
|
||||
case FileStatus::UPLOADED:
|
||||
return "UPLOADED";
|
||||
case FileStatus::END:
|
||||
default:
|
||||
return "undefine";
|
||||
}
|
||||
}
|
|
@ -14,8 +14,11 @@
|
|||
*/
|
||||
#ifndef SQLITE_HANDLE_H
|
||||
#define SQLITE_HANDLE_H
|
||||
#include "IFilesManager.h"
|
||||
#include "sqlite3.h"
|
||||
#include <memory>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
constexpr int SQLITE_UNDEFINE = -1;
|
||||
class SqliteHandle
|
||||
{
|
||||
|
@ -26,13 +29,20 @@ public:
|
|||
void Init(const std::string &dbFileName);
|
||||
void UnInit(void);
|
||||
unsigned long int CreateFiles(const unsigned int &count);
|
||||
bool Add();
|
||||
bool SyncFile(const SyncFileInfo &info);
|
||||
bool Remove();
|
||||
bool Modified();
|
||||
bool Serach();
|
||||
|
||||
private:
|
||||
static void DbInit(sqlite3 *db);
|
||||
static bool UpdateCreateTime(sqlite3 *db, const unsigned long &key, const time_t &createTime);
|
||||
static bool UpdateFileName(sqlite3 *db, const unsigned long &key, const std::string &fileName);
|
||||
static bool UpdateFileSize(sqlite3 *db, const unsigned long &key, const unsigned long &fileSize);
|
||||
static bool UpdateFileType(sqlite3 *db, const unsigned long &key, const FileCreateType &type);
|
||||
static std::string ConvertFileType(const FileCreateType &type);
|
||||
static bool UpdateFileStatus(sqlite3 *db, const unsigned long &key, const FileStatus &status);
|
||||
static std::string ConvertFileStatus(const FileStatus &status);
|
||||
|
||||
private:
|
||||
sqlite3 *mDb;
|
||||
|
|
|
@ -78,10 +78,6 @@ public:
|
|||
virtual StatusCode StopTask(void);
|
||||
virtual StatusCode ClearTask(void);
|
||||
virtual StatusCode SetSpontaneousTaskMonitor(std::shared_ptr<VMediaTask> &task);
|
||||
// virtual StatusCode BeReadyForLive(void);
|
||||
// virtual StatusCode SetMediaMonitor(std::shared_ptr<VMediaMonitor> &monitor);
|
||||
// virtual StatusCode StartMedia(void);
|
||||
// virtual StatusCode StopMedia(void);
|
||||
};
|
||||
class IMediaManager
|
||||
{
|
||||
|
|
|
@ -120,19 +120,22 @@ void MediaHandle::TaskTimer(void)
|
|||
*/
|
||||
mTaskRuning = false;
|
||||
}
|
||||
mStreamHandle->StopHandleStream();
|
||||
std::vector<MediaTaskResponse> files;
|
||||
mStreamHandle->GetAllFiles(files);
|
||||
mStreamHandle->UnInit();
|
||||
if (mCameraHal) {
|
||||
mCameraHal->StopTask();
|
||||
}
|
||||
mStreamHandle.reset();
|
||||
mMutex.lock();
|
||||
auto runingTask = mCurrentTask.lock();
|
||||
if (mCurrentTask.expired()) {
|
||||
LogWarning("SdCardHal: monitor is expired.\n");
|
||||
LogWarning("mCurrentTask is expired.\n");
|
||||
return;
|
||||
}
|
||||
LogInfo("Task finished response to application.\n");
|
||||
std::vector<MediaTaskResponse> responses;
|
||||
runingTask->Response(responses);
|
||||
runingTask->Response(files);
|
||||
mCurrentTask.reset();
|
||||
mMutex.unlock();
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
RecordMp4::RecordMp4(std::shared_ptr<VMediaTask> &recordTask) : mRecordMp4Object(nullptr), mRecordTask(recordTask)
|
||||
{
|
||||
}
|
||||
|
@ -44,15 +45,24 @@ StatusCode RecordMp4::Init(void)
|
|||
}
|
||||
StatusCode RecordMp4::UnInit(void)
|
||||
{
|
||||
StopHandleStream();
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
void RecordMp4::StopHandleStream(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
if (mRecordMp4Object) {
|
||||
ICloseOutputFile(mRecordMp4Object);
|
||||
IMediaBaseFree(mRecordMp4Object);
|
||||
mRecordMp4Object = nullptr;
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
std::string videoPath = mRecordTask->GetTargetNameForSaving();
|
||||
MediaTaskResponse response(videoPath.c_str());
|
||||
mTaskResponse.push_back(response);
|
||||
}
|
||||
void RecordMp4::GetVideoStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
if (mRecordMp4Object) {
|
||||
StreamInfo info = {.mType = STREAM_TYPE_VIDEO_H264};
|
||||
IGetStreamData(mRecordMp4Object, stream, length, info);
|
||||
|
@ -60,8 +70,15 @@ void RecordMp4::GetVideoStream(const void *stream, const unsigned int &length, c
|
|||
}
|
||||
void RecordMp4::GetAudioStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
if (mRecordMp4Object) {
|
||||
StreamInfo info = {.mType = STREAM_TYPE_AUDIO_G711A};
|
||||
IGetStreamData(mRecordMp4Object, stream, length, info);
|
||||
}
|
||||
}
|
||||
StatusCode RecordMp4::GetAllFiles(std::vector<MediaTaskResponse> &files)
|
||||
{
|
||||
files = std::move(mTaskResponse);
|
||||
mTaskResponse.clear();
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
|
@ -17,6 +17,8 @@
|
|||
#include "IMediaManager.h"
|
||||
#include "VStreamHandle.h"
|
||||
#include <cstdio>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
class RecordMp4 : public VStreamHandle
|
||||
{
|
||||
public:
|
||||
|
@ -24,11 +26,15 @@ public:
|
|||
virtual ~RecordMp4() = default;
|
||||
StatusCode Init(void) override;
|
||||
StatusCode UnInit(void) override;
|
||||
void StopHandleStream(void) override;
|
||||
void GetVideoStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp) override;
|
||||
void GetAudioStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp) override;
|
||||
StatusCode GetAllFiles(std::vector<MediaTaskResponse> &files) override;
|
||||
|
||||
private:
|
||||
std::mutex mMutex;
|
||||
void *mRecordMp4Object;
|
||||
std::shared_ptr<VMediaTask> mRecordTask;
|
||||
std::vector<MediaTaskResponse> mTaskResponse;
|
||||
};
|
||||
#endif
|
|
@ -13,16 +13,33 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
#include "VStreamHandle.h"
|
||||
#include "ILog.h"
|
||||
#include "StatusCode.h"
|
||||
#include <vector>
|
||||
StatusCode VStreamHandle::Init(void)
|
||||
{
|
||||
LogInfo("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
StatusCode VStreamHandle::UnInit(void)
|
||||
{
|
||||
LogInfo("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
void VStreamHandle::StopHandleStream(void)
|
||||
{
|
||||
LogInfo("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
}
|
||||
void VStreamHandle::GetVideoStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp)
|
||||
{
|
||||
LogInfo("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
}
|
||||
void VStreamHandle::GetAudioStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp)
|
||||
{
|
||||
LogInfo("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
}
|
||||
StatusCode VStreamHandle::GetAllFiles(std::vector<MediaTaskResponse> &files)
|
||||
{
|
||||
LogInfo("STATUS_CODE_VIRTUAL_FUNCTION\n");
|
||||
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
|
@ -14,7 +14,9 @@
|
|||
*/
|
||||
#ifndef V_STREAM_HANDLE_H
|
||||
#define V_STREAM_HANDLE_H
|
||||
#include "IMediaManager.h"
|
||||
#include "StatusCode.h"
|
||||
#include <vector>
|
||||
class VStreamHandle
|
||||
{
|
||||
public:
|
||||
|
@ -22,7 +24,9 @@ public:
|
|||
virtual ~VStreamHandle() = default;
|
||||
virtual StatusCode Init(void);
|
||||
virtual StatusCode UnInit(void);
|
||||
virtual void StopHandleStream(void);
|
||||
virtual void GetVideoStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp);
|
||||
virtual void GetAudioStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp);
|
||||
virtual StatusCode GetAllFiles(std::vector<MediaTaskResponse> &files);
|
||||
};
|
||||
#endif
|
|
@ -49,7 +49,7 @@ bool StorageBase::CheckDirectory(const char *filepath)
|
|||
}
|
||||
if (stat(path, &st) == -1) {
|
||||
if (errno == ENOENT) {
|
||||
LogInfo("mkdir:%s\n", path);
|
||||
// LogInfo("mkdir:%s\n", path);
|
||||
if (mkdir(path, 0755) == -1) {
|
||||
LogError("mkdir path failed:%s\n", path);
|
||||
free(path);
|
||||
|
@ -74,28 +74,22 @@ const char *StorageBase::PrintStringStorageEvent(const StorageEvent &event)
|
|||
switch (event) {
|
||||
case StorageEvent::SD_CARD_INSERT: {
|
||||
return "SD_CARD_INSERT";
|
||||
break;
|
||||
}
|
||||
case StorageEvent::SD_CARD_REMOVE: {
|
||||
return "SD_CARD_REMOVE";
|
||||
break;
|
||||
}
|
||||
case StorageEvent::SD_ABNORMAL: {
|
||||
return "SD_ABNORMAL";
|
||||
break;
|
||||
}
|
||||
case StorageEvent::EMMC_NORMAL: {
|
||||
return "EMMC_NORMAL";
|
||||
break;
|
||||
}
|
||||
case StorageEvent::END: {
|
||||
return "END";
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
return "UNDEFINE";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -112,5 +106,6 @@ std::string StorageBase::CreateFilesSavingPath(void)
|
|||
std::ostringstream pathStream;
|
||||
pathStream << SD_CARD_MOUNT_PATH << "/DCIM/" << std::setw(4) << std::setfill('0') << year << "/" << std::setw(2)
|
||||
<< std::setfill('0') << month << "/" << std::setw(2) << std::setfill('0') << day << "/";
|
||||
CheckDirectory((pathStream.str() + "anyfile").c_str());
|
||||
return pathStream.str();
|
||||
}
|
|
@ -575,7 +575,7 @@ int FfmpegMuxStream::write_frame(AVFormatContext *fmt_ctx, AVCodecContext *c, AV
|
|||
pkt->stream_index = st->index;
|
||||
|
||||
/* Write the compressed frame to the media file. */
|
||||
log_packet(fmt_ctx, pkt);
|
||||
// log_packet(fmt_ctx, pkt);
|
||||
ret = av_interleaved_write_frame(fmt_ctx, pkt);
|
||||
/* pkt is now blank (av_interleaved_write_frame() takes ownership of
|
||||
* its contents and resets pkt), so that no unreferencing is necessary.
|
||||
|
|
Loading…
Reference in New Issue
Block a user