diff --git a/CMakeLists.txt b/CMakeLists.txt index e6316e1..9b9f74d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,9 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb") # Dependencies. # +# https://cmake.org/cmake/help/v3.0/module/FindProtobuf.html +find_package(Protobuf REQUIRED) + # https://gflags.github.io/gflags/#cmake mentions a cmake module, but at # least on Ubuntu 15.10, libgflags-dev does not include it. There's no # pkgconfig either. Do this by hand. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6941d97..ccedc8a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,15 +28,16 @@ # along with this program. If not, see . include_directories(${CMAKE_CURRENT_BINARY_DIR}) -find_package(Threads REQUIRED) +PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS config.protodevel) + set(MOONFIRE_DEPS - ${CMAKE_THREAD_LIBS_INIT} ${FFMPEG_LIBRARIES} ${LIBEVENT_LIBRARIES} ${GFLAGS_LIBRARIES} ${GLOG_LIBRARIES} ${OPENSSL_LIBRARIES} ${PROFILER_LIBRARIES} + ${PROTOBUF_LIBRARIES} ${RE2_LIBRARIES} ${SQLITE_LIBRARIES} ${UUID_LIBRARIES}) @@ -59,7 +60,7 @@ set(MOONFIRE_NVR_SRCS uuid.cc web.cc) -add_library(moonfire-nvr-lib ${MOONFIRE_NVR_SRCS}) +add_library(moonfire-nvr-lib ${MOONFIRE_NVR_SRCS} ${PROTO_SRCS} ${PROTO_HDRS}) target_link_libraries(moonfire-nvr-lib ${MOONFIRE_DEPS}) add_executable(moonfire-nvr moonfire-nvr-main.cc) diff --git a/src/moonfire-db.cc b/src/moonfire-db.cc index 83dc04f..7e77a39 100644 --- a/src/moonfire-db.cc +++ b/src/moonfire-db.cc @@ -59,11 +59,6 @@ bool MoonfireDatabase::Init(Database *db, std::string *error_message) { camera.uuid, camera.short_name, camera.description, - camera.host, - camera.username, - camera.password, - camera.main_rtsp_path, - camera.sub_rtsp_path, camera.retain_bytes, min(recording.start_time_90k), max(recording.start_time_90k + recording.duration_90k), @@ -85,26 +80,21 @@ bool MoonfireDatabase::Init(Database *db, std::string *error_message) { Uuid uuid; if (!uuid.ParseBinary(list_cameras_run.ColumnBlob(1))) { *error_message = - StrCat("bad uuid ", ToHex(list_cameras_run.ColumnBlob(1)), + StrCat("bad uuid ", ToHex(list_cameras_run.ColumnBlob(2)), " for camera id ", data.id); return false; } data.short_name = list_cameras_run.ColumnText(2).as_string(); data.description = list_cameras_run.ColumnText(3).as_string(); - data.host = list_cameras_run.ColumnText(4).as_string(); - data.username = list_cameras_run.ColumnText(5).as_string(); - data.password = list_cameras_run.ColumnText(6).as_string(); - data.main_rtsp_path = list_cameras_run.ColumnText(7).as_string(); - data.sub_rtsp_path = list_cameras_run.ColumnText(8).as_string(); - data.retain_bytes = list_cameras_run.ColumnInt64(9); - data.min_start_time_90k = list_cameras_run.ColumnType(10) == SQLITE_NULL + data.retain_bytes = list_cameras_run.ColumnInt64(4); + data.min_start_time_90k = list_cameras_run.ColumnType(5) == SQLITE_NULL ? -1 - : list_cameras_run.ColumnInt64(10); - data.max_end_time_90k = list_cameras_run.ColumnType(11) == SQLITE_NULL + : list_cameras_run.ColumnInt64(5); + data.max_end_time_90k = list_cameras_run.ColumnType(6) == SQLITE_NULL ? -1 - : list_cameras_run.ColumnInt64(11); - data.total_duration_90k = list_cameras_run.ColumnInt64(12); - data.total_sample_file_bytes = list_cameras_run.ColumnInt64(13); + : list_cameras_run.ColumnInt64(6); + data.total_duration_90k = list_cameras_run.ColumnInt64(7); + data.total_sample_file_bytes = list_cameras_run.ColumnInt64(8); auto ret = cameras_by_uuid_.insert(std::make_pair(uuid, data)); if (!ret.second) { @@ -317,15 +307,9 @@ void MoonfireDatabase::ListCameras( DatabaseContext ctx(db_); ListCamerasRow row; for (const auto &entry : cameras_by_uuid_) { - row.id = entry.second.id; row.uuid = entry.first; row.short_name = entry.second.short_name; row.description = entry.second.description; - row.host = entry.second.host; - row.username = entry.second.username; - row.password = entry.second.password; - row.main_rtsp_path = entry.second.main_rtsp_path; - row.sub_rtsp_path = entry.second.sub_rtsp_path; row.retain_bytes = entry.second.retain_bytes; row.min_start_time_90k = entry.second.min_start_time_90k; row.max_end_time_90k = entry.second.max_end_time_90k; @@ -563,10 +547,11 @@ std::vector MoonfireDatabase::ReserveSampleFiles( if (n == 0) { return std::vector(); } + auto *gen = GetRealUuidGenerator(); std::vector uuids; uuids.reserve(n); for (int i = 0; i < n; ++i) { - uuids.push_back(GetRealUuidGenerator->Generate()); + uuids.push_back(gen->Generate()); } DatabaseContext ctx(db_); if (!ctx.BeginTransaction(error_message)) { @@ -616,10 +601,7 @@ bool MoonfireDatabase::InsertVideoSampleEntry(VideoSampleEntry *entry, insert_run.BindInt64(":height", entry->height); insert_run.BindBlob(":data", entry->data); if (insert_run.Step() != SQLITE_DONE) { - *error_message = - StrCat("insert video sample entry: ", insert_run.error_message(), - ": sha1=", ToHex(entry->sha1), ", dimensions=", entry->width, - "x", entry->height, ", data=", ToHex(entry->data)); + *error_message = insert_run.error_message(); return false; } entry->id = ctx.last_insert_rowid(); @@ -678,19 +660,7 @@ bool MoonfireDatabase::InsertRecording(Recording *recording, insert_run.BindBlob(":sample_file_sha1", recording->sample_file_sha1); insert_run.BindBlob(":video_index", recording->video_index); if (insert_run.Step() != SQLITE_DONE) { - LOG(ERROR) << "insert_run failed: " << insert_run.error_message() - << ", camera_id=" << recording->camera_id - << ", sample_file_bytes=" << recording->sample_file_bytes - << ", start_time_90k=" << recording->start_time_90k - << ", duration_90k=" - << recording->end_time_90k - recording->start_time_90k - << ", video_samples=" << recording->video_samples - << ", video_sync_samples=" << recording->video_sync_samples - << ", video_sample_entry_id=" << recording->video_sample_entry_id - << ", sample_file_uuid=" - << recording->sample_file_uuid.UnparseText() - << ", sample_file_sha1=" << ToHex(recording->sample_file_sha1) - << ", video_index length " << recording->video_index.size(); + LOG(ERROR) << "insert_run failed: " << insert_run.error_message(); *error_message = insert_run.error_message(); ctx.RollbackTransaction(); return false; diff --git a/src/moonfire-db.h b/src/moonfire-db.h index 1c8d36c..4b020e4 100644 --- a/src/moonfire-db.h +++ b/src/moonfire-db.h @@ -75,15 +75,9 @@ namespace moonfire_nvr { // For use with MoonfireDatabase::ListCameras. struct ListCamerasRow { - int64_t id = -1; Uuid uuid; std::string short_name; std::string description; - std::string host; - std::string username; - std::string password; - std::string main_rtsp_path; - std::string sub_rtsp_path; int64_t retain_bytes = -1; // Aggregates summarizing completed recordings. @@ -215,11 +209,6 @@ class MoonfireDatabase { int64_t id = -1; std::string short_name; std::string description; - std::string host; - std::string username; - std::string password; - std::string main_rtsp_path; - std::string sub_rtsp_path; int64_t retain_bytes = -1; // Aggregates of all recordings associated with the camera. @@ -237,7 +226,7 @@ class MoonfireDatabase { int64_t *max_end_time_90k, std::string *error_message); - Environment *env_ = nullptr; + Database *db_ = nullptr; Statement list_camera_recordings_stmt_; Statement build_mp4_stmt_; Statement insert_reservation_stmt_; diff --git a/src/web.cc b/src/web.cc index e16a9c2..25c08c8 100644 --- a/src/web.cc +++ b/src/web.cc @@ -136,7 +136,8 @@ void WebInterface::HandleCameraDetail(evhttp_request *req, void *arg) { // Rather than listing each 60-second recording, generate a HTML row for // aggregated .mp4 files of up to kForceSplitDuration90k each, provided // there is no gap or change in video parameters between recordings. - static const int64_t kForceSplitDuration90k = 60 * 60 * kTimeUnitsPerSecond; + static const int64_t kForceSplitDuration90k = + 60 * 60 * kTimeUnitsPerSecond; ListCameraRecordingsRow aggregated; auto maybe_finish_html_row = [&]() { if (aggregated.start_time_90k == -1) {