mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-11-25 12:06:11 -05:00
Initial commit, with basic functionality.
This commit is contained in:
59
src/CMakeLists.txt
Normal file
59
src/CMakeLists.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
# Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
#
|
||||
# This program 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.
|
||||
#
|
||||
# In addition, as a special exception, the copyright holders give
|
||||
# permission to link the code of portions of this program with the
|
||||
# OpenSSL library under certain conditions as described in each
|
||||
# individual source file, and distribute linked combinations including
|
||||
# the two.
|
||||
#
|
||||
# You must obey the GNU General Public License in all respects for all
|
||||
# of the code used other than OpenSSL. If you modify file(s) with this
|
||||
# exception, you may extend this exception to your version of the
|
||||
# file(s), but you are not obligated to do so. If you do not wish to do
|
||||
# so, delete this exception statement from your version. If you delete
|
||||
# this exception statement from all source files in the program, then
|
||||
# also delete it here.
|
||||
#
|
||||
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS config.protodevel)
|
||||
|
||||
set(MOONFIRE_DEPS
|
||||
${FFMPEG_LIBRARIES}
|
||||
${LIBEVENT_LIBRARIES}
|
||||
${GFLAGS_LIBRARIES}
|
||||
${GLOG_LIBRARIES}
|
||||
${PROFILER_LIBRARIES}
|
||||
${PROTOBUF_LIBRARIES}
|
||||
${RE2_LIBRARIES})
|
||||
|
||||
add_library(moonfire-nvr-lib moonfire-nvr.cc ffmpeg.cc http.cc string.cc profiler.cc filesystem.cc time.cc ${PROTO_SRCS} ${PROTO_HDRS})
|
||||
target_link_libraries(moonfire-nvr-lib ${MOONFIRE_DEPS})
|
||||
|
||||
add_executable(moonfire-nvr moonfire-nvr-main.cc)
|
||||
target_link_libraries(moonfire-nvr moonfire-nvr-lib)
|
||||
install_programs(/bin FILES moonfire-nvr)
|
||||
|
||||
# Tests.
|
||||
include_directories(${GTest_INCLUDE_DIR})
|
||||
include_directories(${GMock_INCLUDE_DIR})
|
||||
|
||||
foreach(test http moonfire-nvr string)
|
||||
add_executable(${test}-test ${test}-test.cc testutil.cc)
|
||||
target_link_libraries(${test}-test GTest GMock moonfire-nvr-lib)
|
||||
add_test(NAME ${test}-test
|
||||
COMMAND ${test}-test
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||
endforeach(test)
|
||||
93
src/config.protodevel
Normal file
93
src/config.protodevel
Normal file
@@ -0,0 +1,93 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// config.protodevel: protocol buffer definition for configuration.
|
||||
// Currently this is used only for an ASCII-format protocol buffer config
|
||||
// file, so tag numbers are insignificant. Additionally, as moonfire-nvr has not
|
||||
// yet reached 1.0, there is no promise of compatibility between versions---
|
||||
// names and structure are free to change as desired.
|
||||
//
|
||||
// Quick note to those unfamiliar with protocol buffers: the word "optional"
|
||||
// below does *not* imply that a field may be omitted. If the comment does not
|
||||
// say that a field may be omitted, supply the field. "optional" is
|
||||
// just a word that proto2 syntax wants everywhere, like "please". See also:
|
||||
// http://stackoverflow.com/questions/31801257/why-required-and-optional-is-removed-in-protocol-buffers-3
|
||||
|
||||
syntax = "proto2";
|
||||
|
||||
package moonfire_nvr;
|
||||
|
||||
message Camera {
|
||||
// A short name of the camera, used as a path component and in log messages.
|
||||
optional string short_name = 1;
|
||||
|
||||
// A short description of the camera.
|
||||
optional string description = 2;
|
||||
|
||||
// The host (or IP address) to use in rtsp:// URLs when accessing the camera.
|
||||
optional string host = 3;
|
||||
|
||||
// The username to use when accessing the camera.
|
||||
// If empty, no username or password will be supplied.
|
||||
optional string user = 4;
|
||||
|
||||
// The password to use when accessing the camera.
|
||||
optional string password = 5;
|
||||
|
||||
// The path (starting with "/") to use in rtsp:// URLs to reference this
|
||||
// camera's "main" (full-quality) video stream.
|
||||
optional string main_rtsp_path = 6;
|
||||
|
||||
// The path (starting with "/") to use in rtsp:// URLs to reference this
|
||||
// camera's "main" (full-quality) video stream. Currently unused.
|
||||
optional string sub_rtsp_path = 7;
|
||||
|
||||
// The number of bytes of video to retain, excluding the currently-recording
|
||||
// file. Older files will be deleted as necessary to stay within this limit.
|
||||
optional uint64 retain_bytes = 8;
|
||||
}
|
||||
|
||||
message Config {
|
||||
// The base path for recorded files. E.g., "/var/lib/surveillance".
|
||||
// Each camera will record to a subdirectory of this path.
|
||||
optional string base_path = 1;
|
||||
|
||||
// Approximate number of seconds after which to close a video file and
|
||||
// open a new one. E.g., 600 (meaning 10 minutes). All files will begin with
|
||||
// a key frame, so video files will often be slightly over this limit.
|
||||
optional uint32 rotate_sec = 2;
|
||||
|
||||
// A port to use for a HTTP server. For example, "8080".
|
||||
// This webserver currently allows unauthenticated access to video files, so
|
||||
// it should not be exposed to the Internet.
|
||||
optional uint32 http_port = 4;
|
||||
|
||||
repeated Camera camera = 3;
|
||||
}
|
||||
84
src/ffmpeg-test.cc
Normal file
84
src/ffmpeg-test.cc
Normal file
@@ -0,0 +1,84 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ffmpeg-test.cc: tests of the ffmpeg.h interface.
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
#include <glog/logging.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "ffmpeg.h"
|
||||
|
||||
DECLARE_bool(alsologtostderr);
|
||||
|
||||
namespace moonfire_nvr {
|
||||
namespace {
|
||||
|
||||
class FfmpegTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase() {
|
||||
FfmpegGlobalSetup();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(FfmpegTest, Read) {
|
||||
// The cwd should be the cmake build directory, which should be a subdir of
|
||||
// the project.
|
||||
InputVideoPacketStream stream;
|
||||
std::string error_msg;
|
||||
ASSERT_TRUE(
|
||||
stream.Open("../src/testdata/ffmpeg-bug-5018.mp4", &error_msg))
|
||||
<< error_msg;
|
||||
VideoPacket pkt;
|
||||
std::vector<int64_t> ptses;
|
||||
while (stream.GetNext(&pkt, &error_msg)) {
|
||||
ptses.push_back(pkt.pts());
|
||||
}
|
||||
ASSERT_EQ("", error_msg);
|
||||
int64_t kExpectedPtses[] = {
|
||||
71001, 81005, 88995, 99005, 107043, 117022, 125000, 135022, 142992,
|
||||
153101, 161005, 171005, 178996, 189008, 197039, 207023, 215003, 225004,
|
||||
232993, 243007, 251000, 261014, 268991, 279009, 287079, 297037, 305008,
|
||||
315010, 322995, 333016, 341001, 351105, 359009, 369017, 377005, 387029
|
||||
};
|
||||
ASSERT_THAT(ptses, testing::ElementsAreArray(kExpectedPtses));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FLAGS_alsologtostderr = true;
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
429
src/ffmpeg.cc
Normal file
429
src/ffmpeg.cc
Normal file
@@ -0,0 +1,429 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ffmpeg.cc: See ffmpeg.h for description.
|
||||
|
||||
#include "ffmpeg.h"
|
||||
|
||||
#include <mutex>
|
||||
|
||||
extern "C" {
|
||||
#include <libavutil/buffer.h>
|
||||
#include <libavutil/mathematics.h>
|
||||
#include <libavutil/version.h>
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavcodec/version.h>
|
||||
#include <libavformat/version.h>
|
||||
} // extern "C"
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
|
||||
#include "string.h"
|
||||
|
||||
// libav lacks this ffmpeg constant.
|
||||
#ifndef AV_ERROR_MAX_STRING_SIZE
|
||||
#define AV_ERROR_MAX_STRING_SIZE 64
|
||||
#endif
|
||||
|
||||
DEFINE_int32(avlevel, AV_LOG_INFO,
|
||||
"maximum logging level for ffmpeg/libav; "
|
||||
"higher levels will be ignored.");
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string AvError2Str(re2::StringPiece function, int err) {
|
||||
char str[AV_ERROR_MAX_STRING_SIZE];
|
||||
if (av_strerror(err, str, sizeof(str)) == 0) {
|
||||
return StrCat(function, ": ", str);
|
||||
}
|
||||
return StrCat(function, ": unknown error ", err);
|
||||
}
|
||||
|
||||
struct Dictionary {
|
||||
Dictionary() {}
|
||||
Dictionary(const Dictionary &) = delete;
|
||||
Dictionary &operator=(const Dictionary &) = delete;
|
||||
~Dictionary() { av_dict_free(&dict); }
|
||||
|
||||
bool Set(const char *key, const char *value, std::string *error_message) {
|
||||
int ret = av_dict_set(&dict, key, value, 0);
|
||||
if (ret < 0) {
|
||||
*error_message = AvError2Str("av_dict_set", ret);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool size() const { return av_dict_count(dict); }
|
||||
|
||||
AVDictionary *dict = nullptr;
|
||||
};
|
||||
|
||||
google::LogSeverity GlogLevelFromAvLevel(int avlevel) {
|
||||
if (avlevel >= AV_LOG_INFO) {
|
||||
return google::GLOG_INFO;
|
||||
} else if (avlevel >= AV_LOG_WARNING) {
|
||||
return google::GLOG_WARNING;
|
||||
} else if (avlevel > AV_LOG_PANIC) {
|
||||
return google::GLOG_ERROR;
|
||||
} else {
|
||||
return google::GLOG_FATAL;
|
||||
}
|
||||
}
|
||||
|
||||
void AvLogCallback(void *avcl, int avlevel, const char *fmt, va_list vl) {
|
||||
if (avlevel > FLAGS_avlevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
// google::LogMessage expects a "file" and "line" to be prefixed to the
|
||||
// log message, like so:
|
||||
//
|
||||
// W1210 11:00:32.224936 28739 ffmpeg_rtsp:0] Estimating duration ...
|
||||
// ^file ^line
|
||||
//
|
||||
// Normally this is filled in via the __FILE__ and __LINE__
|
||||
// C preprocessor macros. In this case, try to fill in something useful
|
||||
// based on the information ffmpeg supplies.
|
||||
std::string file("ffmpeg");
|
||||
if (avcl != nullptr) {
|
||||
auto *avclass = *reinterpret_cast<AVClass **>(avcl);
|
||||
file.push_back('_');
|
||||
file.append(avclass->item_name(avcl));
|
||||
}
|
||||
char line[512];
|
||||
vsnprintf(line, sizeof(line), fmt, vl);
|
||||
google::LogSeverity glog_level = GlogLevelFromAvLevel(avlevel);
|
||||
google::LogMessage(file.c_str(), 0, glog_level).stream() << line;
|
||||
}
|
||||
|
||||
int AvLockCallback(void **mutex, enum AVLockOp op) {
|
||||
auto typed_mutex = reinterpret_cast<std::mutex **>(mutex);
|
||||
switch (op) {
|
||||
case AV_LOCK_CREATE:
|
||||
LOG_IF(DFATAL, *typed_mutex != nullptr)
|
||||
<< "creating mutex over existing value.";
|
||||
*typed_mutex = new std::mutex;
|
||||
break;
|
||||
case AV_LOCK_DESTROY:
|
||||
delete *typed_mutex;
|
||||
*typed_mutex = nullptr;
|
||||
break;
|
||||
case AV_LOCK_OBTAIN:
|
||||
(*typed_mutex)->lock();
|
||||
break;
|
||||
case AV_LOCK_RELEASE:
|
||||
(*typed_mutex)->unlock();
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string StringifyVersion(int version_int) {
|
||||
return StrCat((version_int >> 16) & 0xFF, ".", (version_int >> 8) & 0xFF, ".",
|
||||
(version_int)&0xFF);
|
||||
}
|
||||
|
||||
void LogVersion(const char *library_name, int compiled_version,
|
||||
int running_version, const char *configuration) {
|
||||
LOG(INFO) << library_name << ": compiled with version "
|
||||
<< StringifyVersion(compiled_version) << ", running with version "
|
||||
<< StringifyVersion(running_version)
|
||||
<< ", configuration: " << configuration;
|
||||
}
|
||||
|
||||
class RealInputVideoPacketStream : public InputVideoPacketStream {
|
||||
public:
|
||||
RealInputVideoPacketStream() {
|
||||
ctx_ = CHECK_NOTNULL(avformat_alloc_context());
|
||||
}
|
||||
|
||||
RealInputVideoPacketStream(const RealInputVideoPacketStream &) = delete;
|
||||
RealInputVideoPacketStream &operator=(const RealInputVideoPacketStream &) =
|
||||
delete;
|
||||
|
||||
~RealInputVideoPacketStream() final {
|
||||
avformat_close_input(&ctx_);
|
||||
avformat_free_context(ctx_);
|
||||
}
|
||||
|
||||
bool GetNext(VideoPacket *pkt, std::string *error_message) final {
|
||||
while (true) {
|
||||
av_packet_unref(pkt->pkt());
|
||||
int ret = av_read_frame(ctx_, pkt->pkt());
|
||||
if (ret != 0) {
|
||||
if (ret == AVERROR_EOF) {
|
||||
error_message->clear();
|
||||
} else {
|
||||
*error_message = AvError2Str("av_read_frame", ret);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (pkt->pkt()->stream_index != stream_index_) {
|
||||
VLOG(3) << "Ignoring packet for stream " << pkt->pkt()->stream_index
|
||||
<< "; only interested in " << stream_index_;
|
||||
continue;
|
||||
}
|
||||
VLOG(2) << "Read packet with pts=" << pkt->pkt()->pts
|
||||
<< ", dts=" << pkt->pkt()->dts << ", key=" << pkt->is_key();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const AVStream *stream() const final { return ctx_->streams[stream_index_]; }
|
||||
|
||||
private:
|
||||
friend class RealVideoSource;
|
||||
int64_t min_next_pts_ = std::numeric_limits<int64_t>::min();
|
||||
int64_t min_next_dts_ = std::numeric_limits<int64_t>::min();
|
||||
AVFormatContext *ctx_ = nullptr; // owned.
|
||||
int stream_index_ = -1;
|
||||
};
|
||||
|
||||
class RealVideoSource : public VideoSource {
|
||||
public:
|
||||
RealVideoSource() {
|
||||
CHECK_GE(0, av_lockmgr_register(&AvLockCallback));
|
||||
av_log_set_callback(&AvLogCallback);
|
||||
av_register_all();
|
||||
avformat_network_init();
|
||||
LogVersion("avutil", LIBAVUTIL_VERSION_INT, avutil_version(),
|
||||
avutil_configuration());
|
||||
LogVersion("avformat", LIBAVFORMAT_VERSION_INT, avformat_version(),
|
||||
avformat_configuration());
|
||||
LogVersion("avcodec", LIBAVCODEC_VERSION_INT, avcodec_version(),
|
||||
avcodec_configuration());
|
||||
}
|
||||
|
||||
std::unique_ptr<InputVideoPacketStream> OpenRtsp(
|
||||
const std::string &url, std::string *error_message) final {
|
||||
std::unique_ptr<InputVideoPacketStream> stream;
|
||||
Dictionary open_options;
|
||||
if (!open_options.Set("rtsp_transport", "tcp", error_message) ||
|
||||
!open_options.Set("user-agent", "moonfire-nvr", error_message) ||
|
||||
// 10-second socket timeout, in microseconds.
|
||||
!open_options.Set("stimeout", "10000000", error_message)) {
|
||||
return stream;
|
||||
}
|
||||
|
||||
stream = OpenCommon(url, &open_options.dict, error_message);
|
||||
if (stream == nullptr) {
|
||||
return stream;
|
||||
}
|
||||
|
||||
// Discard the first packet.
|
||||
LOG(INFO) << "Discarding the first packet to work around "
|
||||
"https://trac.ffmpeg.org/ticket/5018";
|
||||
VideoPacket dummy;
|
||||
if (!stream->GetNext(&dummy, error_message)) {
|
||||
stream.reset();
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
std::unique_ptr<InputVideoPacketStream> OpenFile(
|
||||
const std::string &filename, std::string *error_message) final {
|
||||
AVDictionary *open_options = nullptr;
|
||||
return OpenCommon(filename, &open_options, error_message);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<InputVideoPacketStream> OpenCommon(
|
||||
const std::string &source, AVDictionary **dict,
|
||||
std::string *error_message) {
|
||||
std::unique_ptr<RealInputVideoPacketStream> stream(
|
||||
new RealInputVideoPacketStream);
|
||||
|
||||
int ret = avformat_open_input(&stream->ctx_, source.c_str(), nullptr, dict);
|
||||
if (ret != 0) {
|
||||
*error_message = AvError2Str("avformat_open_input", ret);
|
||||
return std::unique_ptr<InputVideoPacketStream>();
|
||||
}
|
||||
|
||||
if (av_dict_count(*dict) != 0) {
|
||||
std::vector<std::string> ignored;
|
||||
AVDictionaryEntry *ent = nullptr;
|
||||
while ((ent = av_dict_get(*dict, "", ent, AV_DICT_IGNORE_SUFFIX)) !=
|
||||
nullptr) {
|
||||
ignored.push_back(StrCat(ent->key, "=", ent->value));
|
||||
}
|
||||
LOG(WARNING) << "avformat_open_input ignored " << ignored.size()
|
||||
<< " options: " << Join(ignored, ", ");
|
||||
}
|
||||
|
||||
ret = avformat_find_stream_info(stream->ctx_, nullptr);
|
||||
if (ret < 0) {
|
||||
*error_message = AvError2Str("avformat_find_stream_info", ret);
|
||||
return std::unique_ptr<InputVideoPacketStream>();
|
||||
}
|
||||
|
||||
// Find the video stream.
|
||||
for (unsigned int i = 0; i < stream->ctx_->nb_streams; ++i) {
|
||||
if (stream->ctx_->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
||||
VLOG(1) << "Video stream index is " << i;
|
||||
stream->stream_index_ = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stream->stream() == nullptr) {
|
||||
*error_message = StrCat("no video stream");
|
||||
return std::unique_ptr<InputVideoPacketStream>();
|
||||
}
|
||||
|
||||
return std::unique_ptr<InputVideoPacketStream>(stream.release());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
bool OutputVideoPacketStream::OpenFile(const std::string &filename,
|
||||
const InputVideoPacketStream &input,
|
||||
std::string *error_message) {
|
||||
CHECK(ctx_ == nullptr) << ": already open.";
|
||||
CHECK(stream_ == nullptr);
|
||||
|
||||
if ((ctx_ = avformat_alloc_context()) == nullptr) {
|
||||
*error_message = "avformat_alloc_context failed.";
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx_->oformat = av_guess_format(nullptr, filename.c_str(), nullptr);
|
||||
if (ctx_->oformat == nullptr) {
|
||||
*error_message =
|
||||
StrCat("Can't find output format for filename: ", filename.c_str());
|
||||
avformat_free_context(ctx_);
|
||||
ctx_ = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
int ret = avio_open2(&ctx_->pb, filename.c_str(), AVIO_FLAG_WRITE, nullptr,
|
||||
nullptr);
|
||||
if (ret < 0) {
|
||||
avformat_free_context(ctx_);
|
||||
ctx_ = nullptr;
|
||||
*error_message = AvError2Str("avio_open2", ret);
|
||||
return false;
|
||||
}
|
||||
stream_ = avformat_new_stream(ctx_, input.stream()->codec->codec);
|
||||
if (stream_ == nullptr) {
|
||||
avformat_free_context(ctx_);
|
||||
ctx_ = nullptr;
|
||||
unlink(filename.c_str());
|
||||
*error_message = AvError2Str("avformat_new_stream", ret);
|
||||
return false;
|
||||
}
|
||||
stream_->time_base = input.stream()->time_base;
|
||||
ret = avcodec_copy_context(stream_->codec, input.stream()->codec);
|
||||
if (ret != 0) {
|
||||
avformat_free_context(ctx_);
|
||||
ctx_ = nullptr;
|
||||
stream_ = nullptr;
|
||||
unlink(filename.c_str());
|
||||
*error_message = AvError2Str("avcodec_copy_context", ret);
|
||||
return false;
|
||||
}
|
||||
stream_->codec->codec_tag = 0;
|
||||
if ((ctx_->oformat->flags & AVFMT_GLOBALHEADER) != 0) {
|
||||
stream_->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
||||
}
|
||||
|
||||
ret = avformat_write_header(ctx_, nullptr);
|
||||
if (ret != 0) {
|
||||
avformat_free_context(ctx_);
|
||||
ctx_ = nullptr;
|
||||
stream_ = nullptr;
|
||||
unlink(filename.c_str());
|
||||
*error_message = AvError2Str("avformat_write_header", ret);
|
||||
return false;
|
||||
}
|
||||
frames_written_ = 0;
|
||||
key_frames_written_ = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OutputVideoPacketStream::Write(VideoPacket *pkt,
|
||||
std::string *error_message) {
|
||||
#if 0
|
||||
if (pkt->pkt()->pts < min_next_pts_ || pkt->pkt()->dts < min_next_dts_) {
|
||||
*error_message = StrCat("refusing to write non-increasing pts/dts, pts=",
|
||||
pkt->pkt()->pts, " vs min ", min_next_pts_, " dts=",
|
||||
pkt->pkt()->dts, " vs min ", min_next_dts_);
|
||||
return false;
|
||||
}
|
||||
min_next_pts_ = pkt->pkt()->pts + 1;
|
||||
min_next_dts_ = pkt->pkt()->dts + 1;
|
||||
#endif
|
||||
VLOG(2) << "Writing packet with pts=" << pkt->pkt()->pts
|
||||
<< " dts=" << pkt->pkt()->dts << ", key=" << pkt->is_key();
|
||||
int ret = av_write_frame(ctx_, pkt->pkt());
|
||||
if (ret < 0) {
|
||||
*error_message = AvError2Str("av_write_frame", ret);
|
||||
return false;
|
||||
}
|
||||
++frames_written_;
|
||||
if (pkt->is_key()) {
|
||||
key_frames_written_++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void OutputVideoPacketStream::Close() {
|
||||
if (ctx_ == nullptr) {
|
||||
CHECK(stream_ == nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
int ret = av_write_trailer(ctx_);
|
||||
if (ret != 0) {
|
||||
LOG(WARNING) << AvError2Str("av_write_trailer", ret);
|
||||
}
|
||||
|
||||
ret = avio_closep(&ctx_->pb);
|
||||
if (ret != 0) {
|
||||
LOG(WARNING) << AvError2Str("avio_closep", ret);
|
||||
}
|
||||
avformat_free_context(ctx_);
|
||||
ctx_ = nullptr;
|
||||
stream_ = nullptr;
|
||||
frames_written_ = -1;
|
||||
key_frames_written_ = -1;
|
||||
min_next_pts_ = std::numeric_limits<int64_t>::min();
|
||||
min_next_dts_ = std::numeric_limits<int64_t>::min();
|
||||
}
|
||||
|
||||
VideoSource *GetRealVideoSource() {
|
||||
static auto *real_video_source = new RealVideoSource; // never deleted.
|
||||
return real_video_source;
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
164
src/ffmpeg.h
Normal file
164
src/ffmpeg.h
Normal file
@@ -0,0 +1,164 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ffmpeg.h: ffmpeg (or libav) wrappers for operations needed by moonfire_nvr.
|
||||
// This is not a general-purpose wrapper. It makes assumptions about the
|
||||
// data we will be operated on and the desired operations, such as:
|
||||
//
|
||||
// * The input should contain no "B" frames (bi-directionally predicted
|
||||
// pictures) and thus input frames should be strictly in order of ascending
|
||||
// PTS as well as DTS.
|
||||
//
|
||||
// * Only video frames are of interest.
|
||||
|
||||
#ifndef MOONFIRE_NVR_FFMPEG_H
|
||||
#define MOONFIRE_NVR_FFMPEG_H
|
||||
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
extern "C" {
|
||||
#include <libavformat/avformat.h>
|
||||
} // extern "C"
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
// An encoded video packet.
|
||||
class VideoPacket {
|
||||
public:
|
||||
VideoPacket() { av_init_packet(&pkt_); }
|
||||
VideoPacket(const VideoPacket &) = delete;
|
||||
VideoPacket &operator=(const VideoPacket &) = delete;
|
||||
~VideoPacket() { av_packet_unref(&pkt_); }
|
||||
|
||||
// Returns iff this packet represents a key frame.
|
||||
//
|
||||
// (A key frame is one that can be decoded without previous frames.)
|
||||
//
|
||||
// PRE: this packet is valid, as if it has been filled by
|
||||
// InputVideoPacketStream::Next.
|
||||
bool is_key() const { return (pkt_.flags & AV_PKT_FLAG_KEY) != 0; }
|
||||
|
||||
int64_t pts() const { return pkt_.pts; }
|
||||
|
||||
AVPacket *pkt() { return &pkt_; }
|
||||
|
||||
private:
|
||||
AVPacket pkt_;
|
||||
};
|
||||
|
||||
// An input stream of (still-encoded) video packets.
|
||||
class InputVideoPacketStream {
|
||||
public:
|
||||
InputVideoPacketStream() {}
|
||||
InputVideoPacketStream(const InputVideoPacketStream &) = delete;
|
||||
InputVideoPacketStream &operator=(const InputVideoPacketStream &) = delete;
|
||||
|
||||
// Closes the stream.
|
||||
virtual ~InputVideoPacketStream() {}
|
||||
|
||||
// Get the next packet.
|
||||
//
|
||||
// Returns true iff one is available, false on EOF or failure.
|
||||
// |error_message| will be filled on failure, empty on EOF.
|
||||
//
|
||||
// PRE: the stream is healthy: there was no prior Close() call or GetNext()
|
||||
// failure.
|
||||
virtual bool GetNext(VideoPacket *pkt, std::string *error_message) = 0;
|
||||
|
||||
// Returns the video stream.
|
||||
virtual const AVStream *stream() const = 0;
|
||||
};
|
||||
|
||||
// A class which opens streams.
|
||||
// There's one of these for proudction use; see GetRealVideoSource().
|
||||
// It's an abstract class for testability.
|
||||
class VideoSource {
|
||||
public:
|
||||
virtual ~VideoSource() {}
|
||||
|
||||
// Open the given RTSP URL, accessing the first video stream.
|
||||
//
|
||||
// The RTSP URL will be opened with TCP and a hardcoded socket timeout.
|
||||
//
|
||||
// The first frame will be automatically discarded as a bug workaround.
|
||||
// https://trac.ffmpeg.org/ticket/5018
|
||||
//
|
||||
// Returns success, filling |error_message| on failure.
|
||||
//
|
||||
// PRE: closed.
|
||||
virtual std::unique_ptr<InputVideoPacketStream> OpenRtsp(
|
||||
const std::string &url, std::string *error_message) = 0;
|
||||
|
||||
// Open the given video file, accessing the first video stream.
|
||||
//
|
||||
// Returns the stream. On failure, returns nullptr and fills
|
||||
// |error_message|.
|
||||
virtual std::unique_ptr<InputVideoPacketStream> OpenFile(
|
||||
const std::string &filename, std::string *error_message) = 0;
|
||||
};
|
||||
|
||||
// Returns a VideoSource for production use, which will never be deleted.
|
||||
VideoSource *GetRealVideoSource();
|
||||
|
||||
class OutputVideoPacketStream {
|
||||
public:
|
||||
OutputVideoPacketStream() {}
|
||||
OutputVideoPacketStream(const OutputVideoPacketStream &) = delete;
|
||||
OutputVideoPacketStream &operator=(const OutputVideoPacketStream &) = delete;
|
||||
|
||||
~OutputVideoPacketStream() { Close(); }
|
||||
|
||||
bool OpenFile(const std::string &filename,
|
||||
const InputVideoPacketStream &input,
|
||||
std::string *error_message);
|
||||
|
||||
bool Write(VideoPacket *pkt, std::string *error_message);
|
||||
|
||||
void Close();
|
||||
|
||||
bool is_open() const { return ctx_ != nullptr; }
|
||||
AVRational time_base() const { return stream_->time_base; }
|
||||
|
||||
private:
|
||||
int64_t key_frames_written_ = -1;
|
||||
int64_t frames_written_ = -1;
|
||||
int64_t min_next_dts_ = std::numeric_limits<int64_t>::min();
|
||||
int64_t min_next_pts_ = std::numeric_limits<int64_t>::min();
|
||||
AVFormatContext *ctx_ = nullptr; // owned.
|
||||
AVStream *stream_ = nullptr; // ctx_ owns.
|
||||
};
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_FFMPEG_H
|
||||
84
src/filesystem.cc
Normal file
84
src/filesystem.cc
Normal file
@@ -0,0 +1,84 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// filesystem.cc: See filesystem.h.
|
||||
|
||||
#include "filesystem.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/keyvalq_struct.h>
|
||||
#include <event2/http.h>
|
||||
#include <gperftools/profiler.h>
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include "string.h"
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
bool DirForEach(const std::string &dir_path,
|
||||
std::function<IterationControl(const dirent *)> fn,
|
||||
std::string *error_message) {
|
||||
DIR *owned_dir = opendir(dir_path.c_str());
|
||||
if (owned_dir == nullptr) {
|
||||
int err = errno;
|
||||
*error_message =
|
||||
StrCat("Unable to examine ", dir_path, ": ", strerror(err));
|
||||
return false;
|
||||
}
|
||||
struct dirent *ent;
|
||||
while (errno = 0, (ent = readdir(owned_dir)) != nullptr) {
|
||||
if (fn(ent) == IterationControl::kBreak) {
|
||||
closedir(owned_dir);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
int err = errno;
|
||||
closedir(owned_dir);
|
||||
if (err != 0) {
|
||||
*error_message = StrCat("readdir failed: ", strerror(err));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
69
src/filesystem.h
Normal file
69
src/filesystem.h
Normal file
@@ -0,0 +1,69 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// filesystem.h: helpers for dealing with the local filesystem.
|
||||
|
||||
#ifndef MOONFIRE_NVR_FILESYSTEM_H
|
||||
#define MOONFIRE_NVR_FILESYSTEM_H
|
||||
|
||||
#include <dirent.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/http.h>
|
||||
#include <glog/logging.h>
|
||||
#include <re2/stringpiece.h>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
// Return value for *ForEach callbacks.
|
||||
enum class IterationControl {
|
||||
kContinue, // indicates the caller should proceed with the loop.
|
||||
kBreak // indicates the caller should terminate the loop with success.
|
||||
};
|
||||
|
||||
// Execute |fn| for each directory entry in |dir_path|, stopping early
|
||||
// (successfully) if the callback returns IterationControl::kBreak.
|
||||
//
|
||||
// On success, returns true.
|
||||
// On failure, returns false and updates |error_msg|.
|
||||
bool DirForEach(const std::string &dir_path,
|
||||
std::function<IterationControl(const dirent *)> fn,
|
||||
std::string *error_msg);
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_FILESYSTEM_H
|
||||
162
src/http-test.cc
Normal file
162
src/http-test.cc
Normal file
@@ -0,0 +1,162 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// util_test.cc: tests of the util.h interface.
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
#include <glog/logging.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "http.h"
|
||||
#include "string.h"
|
||||
#include "testutil.h"
|
||||
|
||||
DECLARE_bool(alsologtostderr);
|
||||
|
||||
using moonfire_nvr::internal::ParseRangeHeader;
|
||||
using moonfire_nvr::internal::RangeHeaderType;
|
||||
|
||||
namespace moonfire_nvr {
|
||||
namespace {
|
||||
|
||||
TEST(EvBufferTest, AddFileTest) {
|
||||
std::string dir = PrepareTempDirOrDie("http");
|
||||
std::string foo_filename = StrCat(dir, "/foo");
|
||||
WriteFileOrDie(foo_filename, "foo");
|
||||
|
||||
int in_fd = open(foo_filename.c_str(), O_RDONLY);
|
||||
PCHECK(in_fd >= 0) << "open: " << foo_filename;
|
||||
std::string error_message;
|
||||
|
||||
// Ensure adding the whole file succeeds.
|
||||
EvBuffer buf1;
|
||||
ASSERT_TRUE(buf1.AddFile(in_fd, 0, 3, &error_message)) << error_message;
|
||||
EXPECT_EQ(3u, evbuffer_get_length(buf1.get()));
|
||||
|
||||
// Ensure adding an empty region succeeds.
|
||||
EvBuffer buf2;
|
||||
ASSERT_TRUE(buf2.AddFile(in_fd, 0, 0, &error_message)) << error_message;
|
||||
EXPECT_EQ(0u, evbuffer_get_length(buf2.get()));
|
||||
}
|
||||
|
||||
// Test the specific examples enumerated in RFC 2616 section 14.35.1.
|
||||
TEST(RangeHeaderTest, Rfc_2616_Section_14_35_1) {
|
||||
std::vector<ByteRange> ranges;
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=0-499", 10000, &ranges));
|
||||
EXPECT_THAT(ranges, testing::ElementsAre(ByteRange(0, 500)));
|
||||
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=500-999", 10000, &ranges));
|
||||
EXPECT_THAT(ranges, testing::ElementsAre(ByteRange(500, 1000)));
|
||||
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=-500", 10000, &ranges));
|
||||
EXPECT_THAT(ranges, testing::ElementsAre(ByteRange(9500, 10000)));
|
||||
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=9500-", 10000, &ranges));
|
||||
EXPECT_THAT(ranges, testing::ElementsAre(ByteRange(9500, 10000)));
|
||||
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=0-0,-1", 10000, &ranges));
|
||||
EXPECT_THAT(ranges,
|
||||
testing::ElementsAre(ByteRange(0, 1), ByteRange(9999, 10000)));
|
||||
|
||||
// Non-canonical ranges. Possibly the point of these is that the adjacent
|
||||
// and overlapping ranges are supposed to be coalesced into one? I'm not
|
||||
// going to do that for now...just trying to get something working...
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=500-600,601-999", 10000, &ranges));
|
||||
EXPECT_THAT(ranges,
|
||||
testing::ElementsAre(ByteRange(500, 601), ByteRange(601, 1000)));
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=500-700,601-999", 10000, &ranges));
|
||||
EXPECT_THAT(ranges,
|
||||
testing::ElementsAre(ByteRange(500, 701), ByteRange(601, 1000)));
|
||||
}
|
||||
|
||||
TEST(RangeHeaderTest, Satisfiability) {
|
||||
std::vector<ByteRange> ranges;
|
||||
EXPECT_EQ(RangeHeaderType::kNotSatisfiable,
|
||||
ParseRangeHeader("bytes=10000-", 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=0-499,10000-", 10000, &ranges));
|
||||
EXPECT_THAT(ranges, testing::ElementsAre(ByteRange(0, 500)));
|
||||
EXPECT_EQ(RangeHeaderType::kNotSatisfiable,
|
||||
ParseRangeHeader("bytes=-1", 0, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kNotSatisfiable,
|
||||
ParseRangeHeader("bytes=0-0", 0, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kNotSatisfiable,
|
||||
ParseRangeHeader("bytes=0-", 0, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=0-0", 1, &ranges));
|
||||
EXPECT_THAT(ranges, testing::ElementsAre(ByteRange(0, 1)));
|
||||
EXPECT_EQ(RangeHeaderType::kSatisfiable,
|
||||
ParseRangeHeader("bytes=0-", 1, &ranges));
|
||||
EXPECT_THAT(ranges, testing::ElementsAre(ByteRange(0, 1)));
|
||||
}
|
||||
|
||||
TEST(RangeHeaderTest, AbsentOrInvalid) {
|
||||
std::vector<ByteRange> ranges;
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader(nullptr, 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader("", 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader("foo=0-499", 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader("foo=0-499", 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader("bytes=499-0", 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader("bytes=", 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader("bytes=,", 10000, &ranges));
|
||||
EXPECT_EQ(RangeHeaderType::kAbsentOrInvalid,
|
||||
ParseRangeHeader("bytes=-", 10000, &ranges));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FLAGS_alsologtostderr = true;
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
283
src/http.cc
Normal file
283
src/http.cc
Normal file
@@ -0,0 +1,283 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// http.cc: See http.h.
|
||||
|
||||
#include "http.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/keyvalq_struct.h>
|
||||
#include <event2/http.h>
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include "string.h"
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
namespace {
|
||||
|
||||
class RealFile : public VirtualFile {
|
||||
public:
|
||||
RealFile(re2::StringPiece mime_type, re2::StringPiece filename,
|
||||
const struct stat &statbuf)
|
||||
: mime_type_(mime_type.as_string()),
|
||||
filename_(filename.as_string()),
|
||||
stat_(statbuf) {}
|
||||
~RealFile() final {}
|
||||
|
||||
int64_t size() const final { return stat_.st_size; }
|
||||
time_t last_modified() const final { return stat_.st_mtime; }
|
||||
std::string etag() const final {
|
||||
return StrCat("\"", stat_.st_ino, ":", stat_.st_size, ":",
|
||||
stat_.st_mtim.tv_sec, ":", stat_.st_mtim.tv_nsec, "\"");
|
||||
}
|
||||
std::string mime_type() const final { return mime_type_; }
|
||||
std::string filename() const final { return filename_; }
|
||||
|
||||
// Add the given range of the file to the buffer.
|
||||
bool AddRange(ByteRange range, EvBuffer *buf,
|
||||
std::string *error_message) const final {
|
||||
int fd = open(filename_.c_str(), O_RDONLY);
|
||||
if (fd < 0) {
|
||||
int err = errno;
|
||||
*error_message = StrCat("open: ", strerror(err));
|
||||
return false;
|
||||
}
|
||||
if (!buf->AddFile(fd, range.begin, range.end - range.begin,
|
||||
error_message)) {
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
// |buf| now owns |fd|.
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string mime_type_;
|
||||
const std::string filename_;
|
||||
const struct stat stat_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace internal {
|
||||
|
||||
RangeHeaderType ParseRangeHeader(const char *inptr, int64_t size,
|
||||
std::vector<ByteRange> *ranges) {
|
||||
if (inptr == nullptr) {
|
||||
return RangeHeaderType::kAbsentOrInvalid; // absent.
|
||||
}
|
||||
if (strncmp(inptr, "bytes=", strlen("bytes=")) != 0) {
|
||||
return RangeHeaderType::kAbsentOrInvalid; // invalid syntax.
|
||||
}
|
||||
inptr += strlen("bytes=");
|
||||
ranges->clear();
|
||||
int n_ranges = 0;
|
||||
while (*inptr != 0) { // have more byte-range-sets.
|
||||
++n_ranges;
|
||||
ByteRange r;
|
||||
|
||||
// Parse a number.
|
||||
const char *endptr;
|
||||
int64_t value;
|
||||
if (!strto64(inptr, 10, &endptr, &value)) {
|
||||
return RangeHeaderType::kAbsentOrInvalid; // invalid syntax.
|
||||
}
|
||||
|
||||
if (value < 0) { // just parsed suffix-byte-range-spec.
|
||||
r.begin = std::max(size + value, INT64_C(0));
|
||||
r.end = size;
|
||||
if (r.begin < r.end) { // satisfiable.
|
||||
ranges->emplace_back(std::move(r));
|
||||
}
|
||||
inptr = endptr;
|
||||
|
||||
} else { // just parsed start of byte-range-spec.
|
||||
if (*endptr != '-') {
|
||||
return RangeHeaderType::kAbsentOrInvalid;
|
||||
}
|
||||
r.begin = value;
|
||||
inptr = endptr + 1; // move past the '-'.
|
||||
if (*inptr == ',' || *inptr == 0) { // no end specified; use EOF.
|
||||
r.end = size;
|
||||
} else { // explicit range.
|
||||
if (!strto64(inptr, 10, &endptr, &value) || value < r.begin) {
|
||||
return RangeHeaderType::kAbsentOrInvalid; // invalid syntax.
|
||||
}
|
||||
inptr = endptr;
|
||||
r.end = std::min(size, value + 1); // note inclusive->exclusive.
|
||||
}
|
||||
if (r.begin < size) {
|
||||
ranges->emplace_back(std::move(r)); // satisfiable.
|
||||
}
|
||||
}
|
||||
|
||||
if (*inptr == ',') {
|
||||
inptr++;
|
||||
if (*inptr == 0) {
|
||||
return RangeHeaderType::kAbsentOrInvalid; // invalid syntax.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (n_ranges == 0) { // must be at least one range.
|
||||
return RangeHeaderType::kAbsentOrInvalid;
|
||||
}
|
||||
|
||||
return ranges->empty() ? RangeHeaderType::kNotSatisfiable
|
||||
: RangeHeaderType::kSatisfiable;
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
bool EvBuffer::AddFile(int fd, ev_off_t offset, ev_off_t length,
|
||||
std::string *error_message) {
|
||||
if (length == 0) {
|
||||
// evbuffer_add_file fails in this trivial case, at least when using mmap.
|
||||
// Just return true since there's nothing to be done.
|
||||
return true;
|
||||
}
|
||||
|
||||
if (evbuffer_add_file(buf_, fd, offset, length) != 0) {
|
||||
int err = errno;
|
||||
*error_message = StrCat("evbuffer_add_file failed with offset ", offset,
|
||||
", length ", length, ": ", strerror(err));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void HttpSendError(evhttp_request *req, int http_err, const std::string &prefix,
|
||||
int posix_err) {
|
||||
evhttp_send_error(req, http_err,
|
||||
EscapeHtml(prefix + strerror(posix_err)).c_str());
|
||||
}
|
||||
|
||||
void HttpServe(const VirtualFile &file, evhttp_request *req) {
|
||||
// We could support HEAD, but there's probably no need.
|
||||
if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
|
||||
return evhttp_send_error(req, HTTP_BADMETHOD, "only GET allowed");
|
||||
}
|
||||
|
||||
const struct evkeyvalq *in_hdrs = evhttp_request_get_input_headers(req);
|
||||
struct evkeyvalq *out_hdrs = evhttp_request_get_output_headers(req);
|
||||
|
||||
// Construct a Last-Modified: header.
|
||||
time_t last_modified = file.last_modified();
|
||||
struct tm last_modified_tm;
|
||||
if (gmtime_r(&last_modified, &last_modified_tm) == 0) {
|
||||
return HttpSendError(req, HTTP_INTERNAL, "gmtime_r failed: ", errno);
|
||||
}
|
||||
char last_modified_str[50];
|
||||
if (strftime(last_modified_str, sizeof(last_modified_str),
|
||||
"%a, %d %b %Y %H:%M:%S GMT", &last_modified_tm) == 0) {
|
||||
return HttpSendError(req, HTTP_INTERNAL, "strftime failed: ", errno);
|
||||
}
|
||||
std::string etag = file.etag();
|
||||
|
||||
// Ignore the "Range:" header if "If-Range:" specifies an incorrect etag.
|
||||
const char *if_range = evhttp_find_header(in_hdrs, "If-Range");
|
||||
const char *range_hdr = evhttp_find_header(in_hdrs, "Range");
|
||||
if (if_range != nullptr && etag != if_range) {
|
||||
LOG(INFO) << "Ignoring Range: because If-Range: is stale.";
|
||||
range_hdr = nullptr;
|
||||
}
|
||||
|
||||
EvBuffer buf;
|
||||
std::vector<ByteRange> ranges;
|
||||
auto range_type = internal::ParseRangeHeader(range_hdr, file.size(), &ranges);
|
||||
std::string error_message;
|
||||
int http_status;
|
||||
const char *http_status_str;
|
||||
switch (range_type) {
|
||||
case internal::RangeHeaderType::kNotSatisfiable: {
|
||||
std::string range_hdr = StrCat("bytes */", file.size());
|
||||
evhttp_add_header(out_hdrs, "Content-Range", range_hdr.c_str());
|
||||
http_status = 416;
|
||||
http_status_str = "Range Not Satisfiable";
|
||||
LOG(INFO) << "Replying to non-satisfiable range request for "
|
||||
<< file.filename() << ": " << range_hdr;
|
||||
break;
|
||||
}
|
||||
|
||||
case internal::RangeHeaderType::kSatisfiable:
|
||||
// We only support the simpler single-range case for now.
|
||||
if (ranges.size() == 1) {
|
||||
std::string range_hdr = StrCat("bytes ", ranges[0].begin, "-",
|
||||
ranges[0].end - 1, "/", file.size());
|
||||
if (!file.AddRange(ranges[0], &buf, &error_message)) {
|
||||
LOG(ERROR) << "Unable to serve " << file.filename() << " "
|
||||
<< ranges[0] << ": " << error_message;
|
||||
return evhttp_send_error(req, HTTP_INTERNAL,
|
||||
EscapeHtml(error_message).c_str());
|
||||
}
|
||||
evhttp_add_header(out_hdrs, "Content-Range", range_hdr.c_str());
|
||||
http_status = 206;
|
||||
http_status_str = "Partial Content";
|
||||
LOG(INFO) << "Replying to range request for " << file.filename();
|
||||
break;
|
||||
}
|
||||
// FALLTHROUGH
|
||||
|
||||
case internal::RangeHeaderType::kAbsentOrInvalid:
|
||||
if (!file.AddRange(ByteRange(0, file.size()), &buf, &error_message)) {
|
||||
LOG(ERROR) << "Unable to serve " << file.filename() << ": "
|
||||
<< error_message;
|
||||
return evhttp_send_error(req, HTTP_INTERNAL,
|
||||
EscapeHtml(error_message).c_str());
|
||||
}
|
||||
LOG(INFO) << "Replying to whole-file request for " << file.filename();
|
||||
http_status = HTTP_OK;
|
||||
http_status_str = "OK";
|
||||
}
|
||||
|
||||
// Successful reply ready; add common headers and send.
|
||||
evhttp_add_header(out_hdrs, "Content-Type", file.mime_type().c_str());
|
||||
evhttp_add_header(out_hdrs, "Accept-Ranges", "bytes");
|
||||
evhttp_add_header(out_hdrs, "Last-Modified", last_modified_str);
|
||||
evhttp_add_header(out_hdrs, "ETag", etag.c_str());
|
||||
evhttp_send_reply(req, http_status, http_status_str, buf.get());
|
||||
}
|
||||
|
||||
void HttpServeFile(evhttp_request *req, const std::string &mime_type,
|
||||
const std::string &filename, const struct stat &statbuf) {
|
||||
return HttpServe(RealFile(mime_type, filename, statbuf), req);
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
171
src/http.h
Normal file
171
src/http.h
Normal file
@@ -0,0 +1,171 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// http.h: classes for HTTP serving. In particular, there are helpers for
|
||||
// serving HTTP byte range requests with libevent.
|
||||
|
||||
#ifndef MOONFIRE_NVR_HTTP_H
|
||||
#define MOONFIRE_NVR_HTTP_H
|
||||
|
||||
#include <dirent.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/http.h>
|
||||
#include <glog/logging.h>
|
||||
#include <re2/stringpiece.h>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
// Wrapped version of libevent's "struct evbuffer" which uses RAII and simply
|
||||
// aborts the process if allocations fail. (Moonfire NVR is intended to run on
|
||||
// Linux systems with the default vm.overcommit_memory=0, so there's probably
|
||||
// no point in trying to gracefully recover from a condition that's unlikely
|
||||
// to ever happen.)
|
||||
class EvBuffer {
|
||||
public:
|
||||
EvBuffer() { buf_ = CHECK_NOTNULL(evbuffer_new()); }
|
||||
EvBuffer(const EvBuffer &) = delete;
|
||||
EvBuffer &operator=(const EvBuffer &) = delete;
|
||||
~EvBuffer() { evbuffer_free(buf_); }
|
||||
|
||||
struct evbuffer *get() {
|
||||
return buf_;
|
||||
}
|
||||
|
||||
void Add(const re2::StringPiece &s) {
|
||||
CHECK_EQ(0, evbuffer_add(buf_, s.data(), s.size()));
|
||||
}
|
||||
|
||||
void AddPrintf(const char *fmt, ...) __attribute__((format(printf, 2, 3))) {
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
CHECK_LE(0, evbuffer_add_vprintf(buf_, fmt, argp));
|
||||
va_end(argp);
|
||||
}
|
||||
|
||||
// Delegates to evbuffer_add_file.
|
||||
// On success, |fd| will be closed by libevent. On failure, it remains open.
|
||||
bool AddFile(int fd, ev_off_t offset, ev_off_t length,
|
||||
std::string *error_message);
|
||||
|
||||
private:
|
||||
struct evbuffer *buf_;
|
||||
};
|
||||
|
||||
struct ByteRange {
|
||||
ByteRange() {}
|
||||
ByteRange(int64_t begin, int64_t end) : begin(begin), end(end) {}
|
||||
int64_t begin = 0;
|
||||
int64_t end = 0; // exclusive.
|
||||
bool operator==(const ByteRange &o) const {
|
||||
return begin == o.begin && end == o.end;
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, const ByteRange &range) {
|
||||
out << "[" << range.begin << ", " << range.end << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
// Helper for sending HTTP errors based on POSIX error returns.
|
||||
void HttpSendError(evhttp_request *req, int http_err, const std::string &prefix,
|
||||
int posix_errno);
|
||||
|
||||
class VirtualFile {
|
||||
public:
|
||||
virtual ~VirtualFile() {}
|
||||
|
||||
// Return the given property of the file.
|
||||
virtual int64_t size() const = 0;
|
||||
virtual time_t last_modified() const = 0;
|
||||
virtual std::string etag() const = 0;
|
||||
virtual std::string mime_type() const = 0;
|
||||
virtual std::string filename() const = 0; // for logging.
|
||||
|
||||
// Add the given range of the file to the buffer.
|
||||
virtual bool AddRange(ByteRange range, EvBuffer *buf,
|
||||
std::string *error_message) const = 0;
|
||||
};
|
||||
|
||||
// Serve an HTTP request |req| from |file|, handling byte range and
|
||||
// conditional serving. (Similar to golang's http.ServeContent.)
|
||||
//
|
||||
// |file| only needs to live through the call to HttpServe itself.
|
||||
// This contract may change in the future; currently all the ranges are added
|
||||
// at the beginning of the request, so if large memory-backed buffers (as
|
||||
// opposed to file-backed buffers) are used, the program's memory usage will
|
||||
// spike, even if the HTTP client aborts early in the request. If this becomes
|
||||
// problematic, this interface may change to take advantage of
|
||||
// evbuffer_add_cb, adding buffers incrementally, and some mechanism will be
|
||||
// added to guarantee VirtualFile objects outlive the HTTP requests they serve.
|
||||
void HttpServe(const VirtualFile &file, evhttp_request *req);
|
||||
|
||||
// Serve a file over HTTP. Expects the caller to supply a sanitized |filename|
|
||||
// (rather than taking it straight from the path specified in |req|).
|
||||
void HttpServeFile(evhttp_request *req, const std::string &mime_type,
|
||||
const std::string &filename, const struct stat &statbuf);
|
||||
|
||||
namespace internal {
|
||||
|
||||
// Value to represent result of parsing HTTP 1.1 "Range:" header.
|
||||
enum class RangeHeaderType {
|
||||
// Ignore the header, serving all bytes in the file.
|
||||
kAbsentOrInvalid,
|
||||
|
||||
// The server SHOULD return a response with status 416 (Requested range not
|
||||
// satisfiable).
|
||||
kNotSatisfiable,
|
||||
|
||||
// The server SHOULD return a response with status 406 (Partial Content).
|
||||
kSatisfiable
|
||||
};
|
||||
|
||||
// Parse an HTTP 1.1 "Range:" header value, following RFC 2616 section 14.35.
|
||||
// This function is for use by HttpServe; it is exposed for testing only.
|
||||
//
|
||||
// |value| on entry should be the header value (after the ": "), or nullptr.
|
||||
// |size| on entry should be the number of bytes available to serve.
|
||||
// On kSatisfiable return, |ranges| will be filled with the satisfiable ranges.
|
||||
// Otherwise, its contents are undefined.
|
||||
RangeHeaderType ParseRangeHeader(const char *value, int64_t size,
|
||||
std::vector<ByteRange> *ranges);
|
||||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_HTTP_H
|
||||
176
src/moonfire-nvr-main.cc
Normal file
176
src/moonfire-nvr-main.cc
Normal file
@@ -0,0 +1,176 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// moonfire-nvr-main.cc: main program. This should be kept as short as
|
||||
// practical, so that individual parts of the program can be tested with the
|
||||
// googletest framework.
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/event_struct.h>
|
||||
#include <event2/http.h>
|
||||
#include <gflags/gflags.h>
|
||||
#include <google/protobuf/io/zero_copy_stream_impl.h>
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include "config.pb.h"
|
||||
#include "ffmpeg.h"
|
||||
#include "profiler.h"
|
||||
#include "moonfire-nvr.h"
|
||||
|
||||
DEFINE_string(config, "/etc/moonfire-nvr.conf", "Path to configuration file.");
|
||||
|
||||
namespace {
|
||||
|
||||
const struct timeval kLogFlushInterval = {1, 0};
|
||||
|
||||
struct event_base* base;
|
||||
|
||||
void EventLogCallback(int severity, const char* msg) {
|
||||
int vlog_level = 0;
|
||||
google::LogSeverity glog_level;
|
||||
if (severity <= EVENT_LOG_DEBUG) {
|
||||
vlog_level = 1;
|
||||
glog_level = google::GLOG_INFO;
|
||||
} else if (severity <= EVENT_LOG_MSG) {
|
||||
glog_level = google::GLOG_INFO;
|
||||
} else if (severity <= EVENT_LOG_WARN) {
|
||||
glog_level = google::GLOG_WARNING;
|
||||
} else {
|
||||
glog_level = google::GLOG_ERROR;
|
||||
}
|
||||
|
||||
if (vlog_level > 0 && !VLOG_IS_ON(vlog_level)) {
|
||||
return;
|
||||
}
|
||||
google::LogMessage("libevent", 0, glog_level).stream() << msg;
|
||||
}
|
||||
|
||||
bool LoadConfiguration(const std::string& filename,
|
||||
moonfire_nvr::Config* config) {
|
||||
int fd = open(filename.c_str(), O_RDONLY);
|
||||
if (fd == -1) {
|
||||
PLOG(ERROR) << "can't open " << filename;
|
||||
return false;
|
||||
}
|
||||
google::protobuf::io::FileInputStream file(fd);
|
||||
file.SetCloseOnDelete(true);
|
||||
// TODO(slamb): report more specific errors via an ErrorCollector.
|
||||
if (!google::protobuf::TextFormat::Parse(&file, config)) {
|
||||
LOG(ERROR) << "can't parse " << filename;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Called on SIGTERM or SIGINT.
|
||||
void SignalCallback(evutil_socket_t, short, void*) {
|
||||
event_base_loopexit(base, nullptr);
|
||||
}
|
||||
|
||||
void FlushLogsCallback(evutil_socket_t, short, void* ev) {
|
||||
google::FlushLogFiles(google::GLOG_INFO);
|
||||
CHECK_EQ(0,
|
||||
event_add(reinterpret_cast<struct event*>(ev), &kLogFlushInterval));
|
||||
}
|
||||
|
||||
void HttpCallback(evhttp_request* req, void* arg) {
|
||||
auto* nvr = reinterpret_cast<moonfire_nvr::Nvr*>(arg);
|
||||
nvr->HttpCallback(req);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
google::InstallFailureSignalHandler();
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
moonfire_nvr::Config config;
|
||||
if (!LoadConfiguration(FLAGS_config, &config)) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
event_set_log_callback(&EventLogCallback);
|
||||
LOG(INFO) << "libevent: compiled with version " << LIBEVENT_VERSION
|
||||
<< ", running with version " << event_get_version();
|
||||
base = CHECK_NOTNULL(event_base_new());
|
||||
|
||||
std::unique_ptr<moonfire_nvr::Nvr> nvr(new moonfire_nvr::Nvr);
|
||||
std::string error_msg;
|
||||
if (!nvr->Init(config, &error_msg)) {
|
||||
LOG(ERROR) << "Unable to initialize: " << error_msg;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
evhttp* http = CHECK_NOTNULL(evhttp_new(base));
|
||||
moonfire_nvr::RegisterProfiler(base, http);
|
||||
evhttp_set_gencb(http, &HttpCallback, nvr.get());
|
||||
if (evhttp_bind_socket(http, "0.0.0.0", config.http_port()) != 0) {
|
||||
LOG(ERROR) << "Unable to bind to port " << config.http_port();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Register for termination signals.
|
||||
struct event ev_sigterm;
|
||||
struct event ev_sigint;
|
||||
CHECK_EQ(0, event_assign(&ev_sigterm, base, SIGTERM, EV_SIGNAL | EV_PERSIST,
|
||||
&SignalCallback, nullptr));
|
||||
CHECK_EQ(0, event_assign(&ev_sigint, base, SIGINT, EV_SIGNAL | EV_PERSIST,
|
||||
&SignalCallback, nullptr));
|
||||
CHECK_EQ(0, event_add(&ev_sigterm, nullptr));
|
||||
CHECK_EQ(0, event_add(&ev_sigint, nullptr));
|
||||
|
||||
// Flush the logfiles regularly for debuggability.
|
||||
struct event ev_flushlogs;
|
||||
CHECK_EQ(0, event_assign(&ev_flushlogs, base, 0, 0, &FlushLogsCallback,
|
||||
&ev_flushlogs));
|
||||
CHECK_EQ(0, event_add(&ev_flushlogs, &kLogFlushInterval));
|
||||
|
||||
// Wait for events.
|
||||
LOG(INFO) << "Main thread entering event loop.";
|
||||
CHECK_EQ(0, event_base_loop(base, 0));
|
||||
|
||||
LOG(INFO) << "Shutting down.";
|
||||
google::FlushLogFiles(google::GLOG_INFO);
|
||||
nvr.reset();
|
||||
LOG(INFO) << "Done.";
|
||||
google::ShutdownGoogleLogging();
|
||||
exit(0);
|
||||
}
|
||||
428
src/moonfire-nvr-test.cc
Normal file
428
src/moonfire-nvr-test.cc
Normal file
@@ -0,0 +1,428 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// moonfire-nvr-test.cc: tests of the moonfire-nvr.cc interface.
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "moonfire-nvr.h"
|
||||
#include "string.h"
|
||||
#include "testutil.h"
|
||||
|
||||
DECLARE_bool(alsologtostderr);
|
||||
|
||||
using testing::_;
|
||||
using testing::AnyNumber;
|
||||
using testing::HasSubstr;
|
||||
using testing::Invoke;
|
||||
using testing::Return;
|
||||
|
||||
namespace moonfire_nvr {
|
||||
namespace {
|
||||
|
||||
class MockVideoSource : public VideoSource {
|
||||
public:
|
||||
// Proxy, as gmock doesn't support non-copyable return values.
|
||||
std::unique_ptr<InputVideoPacketStream> OpenRtsp(
|
||||
const std::string &url, std::string *error_message) final {
|
||||
return std::unique_ptr<InputVideoPacketStream>(
|
||||
OpenRtspRaw(url, error_message));
|
||||
}
|
||||
std::unique_ptr<InputVideoPacketStream> OpenFile(
|
||||
const std::string &file, std::string *error_message) final {
|
||||
return std::unique_ptr<InputVideoPacketStream>(
|
||||
OpenFileRaw(file, error_message));
|
||||
}
|
||||
|
||||
MOCK_METHOD2(OpenRtspRaw,
|
||||
InputVideoPacketStream *(const std::string &, std::string *));
|
||||
MOCK_METHOD2(OpenFileRaw,
|
||||
InputVideoPacketStream *(const std::string &, std::string *));
|
||||
};
|
||||
|
||||
class FileManagerTest : public testing::Test {
|
||||
protected:
|
||||
FileManagerTest() {
|
||||
test_dir_ = PrepareTempDirOrDie("moonfire-nvr-file-manager");
|
||||
}
|
||||
|
||||
std::vector<std::string> GetFilenames(const FileManager &mgr) {
|
||||
std::vector<std::string> out;
|
||||
mgr.ForEachFile([&out](const std::string &f, const struct stat &) {
|
||||
out.push_back(f);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string test_dir_;
|
||||
};
|
||||
|
||||
TEST_F(FileManagerTest, InitWithNoDirectory) {
|
||||
std::string subdir = test_dir_ + "/" + "subdir";
|
||||
FileManager manager("foo", subdir, 0);
|
||||
|
||||
// Should succeed.
|
||||
std::string error_message;
|
||||
EXPECT_TRUE(manager.Init(&error_message)) << error_message;
|
||||
|
||||
// Should create the directory.
|
||||
struct stat buf;
|
||||
ASSERT_EQ(0, lstat(subdir.c_str(), &buf)) << strerror(errno);
|
||||
EXPECT_TRUE(S_ISDIR(buf.st_mode));
|
||||
|
||||
// Should report empty.
|
||||
EXPECT_EQ(0, manager.total_bytes());
|
||||
EXPECT_THAT(GetFilenames(manager), testing::ElementsAre());
|
||||
|
||||
// Adding files: nonexistent, simple, out of order.
|
||||
EXPECT_FALSE(manager.AddFile("nonexistent.mp4", &error_message));
|
||||
WriteFileOrDie(subdir + "/1.mp4", "1");
|
||||
WriteFileOrDie(subdir + "/2.mp4", "123");
|
||||
EXPECT_TRUE(manager.AddFile("2.mp4", &error_message)) << error_message;
|
||||
EXPECT_EQ(3, manager.total_bytes());
|
||||
EXPECT_THAT(GetFilenames(manager), testing::ElementsAre("2.mp4"));
|
||||
EXPECT_TRUE(manager.AddFile("1.mp4", &error_message)) << error_message;
|
||||
EXPECT_EQ(4, manager.total_bytes());
|
||||
EXPECT_THAT(GetFilenames(manager), testing::ElementsAre("1.mp4", "2.mp4"));
|
||||
|
||||
EXPECT_TRUE(manager.Rotate(&error_message)) << error_message;
|
||||
EXPECT_EQ(0, manager.total_bytes());
|
||||
EXPECT_THAT(GetFilenames(manager), testing::ElementsAre());
|
||||
}
|
||||
|
||||
TEST_F(FileManagerTest, InitAndRotateWithExistingFiles) {
|
||||
WriteFileOrDie(test_dir_ + "/1.mp4", "1");
|
||||
WriteFileOrDie(test_dir_ + "/2.mp4", "123");
|
||||
WriteFileOrDie(test_dir_ + "/3.mp4", "12345");
|
||||
WriteFileOrDie(test_dir_ + "/other", "1234567");
|
||||
FileManager manager("foo", test_dir_, 8);
|
||||
|
||||
// Should succeed.
|
||||
std::string error_message;
|
||||
EXPECT_TRUE(manager.Init(&error_message)) << error_message;
|
||||
|
||||
EXPECT_THAT(GetFilenames(manager),
|
||||
testing::ElementsAre("1.mp4", "2.mp4", "3.mp4"));
|
||||
EXPECT_EQ(1 + 3 + 5, manager.total_bytes());
|
||||
|
||||
EXPECT_TRUE(manager.Rotate(&error_message)) << error_message;
|
||||
EXPECT_THAT(GetFilenames(manager), testing::ElementsAre("2.mp4", "3.mp4"));
|
||||
EXPECT_EQ(8, manager.total_bytes());
|
||||
}
|
||||
|
||||
class StreamTest : public testing::Test {
|
||||
public:
|
||||
StreamTest() {
|
||||
test_dir_ = PrepareTempDirOrDie("moonfire-nvr-stream-copier");
|
||||
env_.clock = &clock_;
|
||||
env_.video_source = &video_source_;
|
||||
clock_.Sleep({1430006400, 0}); // 2016-04-26 00:00:00 UTC
|
||||
|
||||
config_.set_base_path(test_dir_);
|
||||
config_.set_rotate_sec(5);
|
||||
auto *camera = config_.add_camera();
|
||||
camera->set_short_name("test");
|
||||
camera->set_host("test-camera");
|
||||
camera->set_user("foo");
|
||||
camera->set_password("bar");
|
||||
camera->set_main_rtsp_path("/main");
|
||||
camera->set_sub_rtsp_path("/sub");
|
||||
camera->set_retain_bytes(1000000);
|
||||
}
|
||||
|
||||
// A function to use in OpenRtspRaw invocations which shuts down the stream
|
||||
// and indicates that the input video source can't be opened.
|
||||
InputVideoPacketStream *Shutdown(const std::string &url,
|
||||
std::string *error_message) {
|
||||
*error_message = "(shutting down)";
|
||||
signal_.Shutdown();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct Frame {
|
||||
Frame(bool is_key, int64_t pts, int64_t duration)
|
||||
: is_key(is_key), pts(pts), duration(duration) {}
|
||||
bool is_key;
|
||||
int64_t pts;
|
||||
int64_t duration;
|
||||
|
||||
bool operator==(const Frame &o) const {
|
||||
return is_key == o.is_key && pts == o.pts && duration == o.duration;
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Frame &f) {
|
||||
return os << "Frame(" << f.is_key << ", " << f.pts << ", " << f.duration
|
||||
<< ")";
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<Frame> GetFrames(const std::string &path) {
|
||||
std::vector<Frame> frames;
|
||||
std::string error_message;
|
||||
std::string full_path = StrCat(test_dir_, "/test/", path);
|
||||
auto f = GetRealVideoSource()->OpenFile(full_path, &error_message);
|
||||
if (f == nullptr) {
|
||||
ADD_FAILURE() << full_path << ": " << error_message;
|
||||
return frames;
|
||||
}
|
||||
VideoPacket pkt;
|
||||
while (f->GetNext(&pkt, &error_message)) {
|
||||
frames.push_back(Frame(pkt.is_key(), pkt.pts(), pkt.pkt()->duration));
|
||||
}
|
||||
EXPECT_EQ("", error_message);
|
||||
return frames;
|
||||
}
|
||||
|
||||
ShutdownSignal signal_;
|
||||
Config config_;
|
||||
SimulatedClock clock_;
|
||||
testing::StrictMock<MockVideoSource> video_source_;
|
||||
Environment env_;
|
||||
std::string test_dir_;
|
||||
};
|
||||
|
||||
class ProxyingInputVideoPacketStream : public InputVideoPacketStream {
|
||||
public:
|
||||
explicit ProxyingInputVideoPacketStream(
|
||||
std::unique_ptr<InputVideoPacketStream> base, SimulatedClock *clock)
|
||||
: base_(std::move(base)), clock_(clock) {}
|
||||
|
||||
bool GetNext(VideoPacket *pkt, std::string *error_message) final {
|
||||
if (pkts_left_-- == 0) {
|
||||
*error_message = "(pkt limit reached)";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Advance time to when this packet starts.
|
||||
clock_->Sleep(SecToTimespec(last_duration_sec_));
|
||||
if (!base_->GetNext(pkt, error_message)) {
|
||||
return false;
|
||||
}
|
||||
last_duration_sec_ =
|
||||
pkt->pkt()->duration * av_q2d(base_->stream()->time_base);
|
||||
|
||||
// Adjust timestamps.
|
||||
if (ts_offset_pkts_left_ > 0) {
|
||||
pkt->pkt()->pts += ts_offset_;
|
||||
pkt->pkt()->dts += ts_offset_;
|
||||
--ts_offset_pkts_left_;
|
||||
}
|
||||
|
||||
// Use a fixed duration, as the duration from a real RTSP stream is only
|
||||
// an estimate. Our test video is 1 fps, 90 kHz time base.
|
||||
pkt->pkt()->duration = 90000;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const AVStream *stream() const final { return base_->stream(); }
|
||||
|
||||
void set_ts_offset(int64_t offset, int pkts) {
|
||||
ts_offset_ = offset;
|
||||
ts_offset_pkts_left_ = pkts;
|
||||
}
|
||||
|
||||
void set_pkts(int num) { pkts_left_ = num; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<InputVideoPacketStream> base_;
|
||||
SimulatedClock *clock_ = nullptr;
|
||||
double last_duration_sec_ = 0.;
|
||||
int64_t ts_offset_ = 0;
|
||||
int ts_offset_pkts_left_ = 0;
|
||||
int pkts_left_ = std::numeric_limits<int>::max();
|
||||
};
|
||||
|
||||
TEST_F(StreamTest, Basic) {
|
||||
Stream stream(&signal_, config_, &env_, config_.camera(0));
|
||||
std::string error_message;
|
||||
ASSERT_TRUE(stream.Init(&error_message)) << error_message;
|
||||
|
||||
// This is a ~1 fps test video with a timebase of 90 kHz.
|
||||
auto in_stream = GetRealVideoSource()->OpenFile("../src/testdata/clip.mp4",
|
||||
&error_message);
|
||||
ASSERT_TRUE(in_stream != nullptr) << error_message;
|
||||
auto *proxy_stream =
|
||||
new ProxyingInputVideoPacketStream(std::move(in_stream), &clock_);
|
||||
|
||||
// The starting pts of the input should be irrelevant.
|
||||
proxy_stream->set_ts_offset(180000, std::numeric_limits<int>::max());
|
||||
|
||||
EXPECT_CALL(video_source_, OpenRtspRaw("rtsp://foo:bar@test-camera/main", _))
|
||||
.WillOnce(Return(proxy_stream))
|
||||
.WillOnce(Invoke(this, &StreamTest::Shutdown));
|
||||
stream.Run();
|
||||
|
||||
// Compare frame-by-frame.
|
||||
// Note below that while the rotation is scheduled to happen near 5-second
|
||||
// boundaries (such as 2016-04-26 00:00:05), it gets deferred until the next
|
||||
// key frame, which in this case is 00:00:07.
|
||||
EXPECT_THAT(stream.GetFilesForTesting(),
|
||||
testing::ElementsAre("20150426000000_test.mp4",
|
||||
"20150426000007_test.mp4"));
|
||||
EXPECT_THAT(GetFrames("20150426000000_test.mp4"),
|
||||
testing::ElementsAre(
|
||||
Frame(true, 0, 90379), Frame(false, 90379, 89884),
|
||||
Frame(false, 180263, 89749), Frame(false, 270012, 89981),
|
||||
Frame(true, 359993, 90055),
|
||||
Frame(false, 450048,
|
||||
89967), // pts_time 5.000533, past rotation time.
|
||||
Frame(false, 540015, 90021),
|
||||
Frame(false, 630036,
|
||||
90000))); // XXX: duration=89958 would be better!
|
||||
EXPECT_THAT(
|
||||
GetFrames("20150426000007_test.mp4"),
|
||||
testing::ElementsAre(Frame(true, 0, 90011), Frame(false, 90011, 90000)));
|
||||
// Note that the final "90000" duration is ffmpeg's estimate based on frame
|
||||
// rate. For non-final packets, the correct duration gets written based on
|
||||
// the next packet's timestamp. The same currently applies to the first
|
||||
// written segment---it uses an estimated time, not the real time until the
|
||||
// next packet. This probably should be fixed...
|
||||
}
|
||||
|
||||
TEST_F(StreamTest, NonIncreasingTimestamp) {
|
||||
Stream stream(&signal_, config_, &env_, config_.camera(0));
|
||||
std::string error_message;
|
||||
ASSERT_TRUE(stream.Init(&error_message)) << error_message;
|
||||
auto in_stream = GetRealVideoSource()->OpenFile("../src/testdata/clip.mp4",
|
||||
&error_message);
|
||||
ASSERT_TRUE(in_stream != nullptr) << error_message;
|
||||
auto *proxy_stream =
|
||||
new ProxyingInputVideoPacketStream(std::move(in_stream), &clock_);
|
||||
proxy_stream->set_ts_offset(12345678, 1);
|
||||
EXPECT_CALL(video_source_, OpenRtspRaw("rtsp://foo:bar@test-camera/main", _))
|
||||
.WillOnce(Return(proxy_stream))
|
||||
.WillOnce(Invoke(this, &StreamTest::Shutdown));
|
||||
|
||||
{
|
||||
ScopedMockLog log;
|
||||
EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
|
||||
EXPECT_CALL(log,
|
||||
Log(_, _, HasSubstr("Rejecting non-increasing pts=90379")));
|
||||
log.Start();
|
||||
stream.Run();
|
||||
}
|
||||
|
||||
// The output file should still be added to the file manager, with the one
|
||||
// packet that made it.
|
||||
EXPECT_THAT(stream.GetFilesForTesting(),
|
||||
testing::ElementsAre("20150426000000_test.mp4"));
|
||||
EXPECT_THAT(
|
||||
GetFrames("20150426000000_test.mp4"),
|
||||
testing::ElementsAre(Frame(true, 0, 90000))); // estimated duration.
|
||||
}
|
||||
|
||||
TEST_F(StreamTest, RetryOnInputError) {
|
||||
Stream stream(&signal_, config_, &env_, config_.camera(0));
|
||||
std::string error_message;
|
||||
ASSERT_TRUE(stream.Init(&error_message)) << error_message;
|
||||
|
||||
auto in_stream_1 = GetRealVideoSource()->OpenFile("../src/testdata/clip.mp4",
|
||||
&error_message);
|
||||
ASSERT_TRUE(in_stream_1 != nullptr) << error_message;
|
||||
auto *proxy_stream_1 =
|
||||
new ProxyingInputVideoPacketStream(std::move(in_stream_1), &clock_);
|
||||
proxy_stream_1->set_pkts(1);
|
||||
|
||||
auto in_stream_2 = GetRealVideoSource()->OpenFile("../src/testdata/clip.mp4",
|
||||
&error_message);
|
||||
ASSERT_TRUE(in_stream_2 != nullptr) << error_message;
|
||||
auto *proxy_stream_2 =
|
||||
new ProxyingInputVideoPacketStream(std::move(in_stream_2), &clock_);
|
||||
proxy_stream_2->set_pkts(1);
|
||||
|
||||
EXPECT_CALL(video_source_, OpenRtspRaw("rtsp://foo:bar@test-camera/main", _))
|
||||
.WillOnce(Return(proxy_stream_1))
|
||||
.WillOnce(Return(proxy_stream_2))
|
||||
.WillOnce(Invoke(this, &StreamTest::Shutdown));
|
||||
stream.Run();
|
||||
|
||||
// Each attempt should have resulted in a file with one packet.
|
||||
EXPECT_THAT(stream.GetFilesForTesting(),
|
||||
testing::ElementsAre("20150426000000_test.mp4",
|
||||
"20150426000001_test.mp4"));
|
||||
EXPECT_THAT(GetFrames("20150426000000_test.mp4"),
|
||||
testing::ElementsAre(Frame(true, 0, 90000)));
|
||||
EXPECT_THAT(GetFrames("20150426000001_test.mp4"),
|
||||
testing::ElementsAre(Frame(true, 0, 90000)));
|
||||
}
|
||||
|
||||
TEST_F(StreamTest, DiscardInitialNonKeyFrames) {
|
||||
Stream stream(&signal_, config_, &env_, config_.camera(0));
|
||||
std::string error_message;
|
||||
ASSERT_TRUE(stream.Init(&error_message)) << error_message;
|
||||
auto in_stream = GetRealVideoSource()->OpenFile("../src/testdata/clip.mp4",
|
||||
&error_message);
|
||||
ASSERT_TRUE(in_stream != nullptr) << error_message;
|
||||
|
||||
// Discard the initial key frame packet.
|
||||
VideoPacket dummy;
|
||||
ASSERT_TRUE(in_stream->GetNext(&dummy, &error_message)) << error_message;
|
||||
|
||||
auto *proxy_stream =
|
||||
new ProxyingInputVideoPacketStream(std::move(in_stream), &clock_);
|
||||
EXPECT_CALL(video_source_, OpenRtspRaw("rtsp://foo:bar@test-camera/main", _))
|
||||
.WillOnce(Return(proxy_stream))
|
||||
.WillOnce(Invoke(this, &StreamTest::Shutdown));
|
||||
stream.Run();
|
||||
|
||||
// Skipped: initial key frame packet (duration 90379)
|
||||
// Ignored: duration 89884, 89749, 89981 (total pts time: 2.99571... sec)
|
||||
// Thus, the first output file should start at 00:00:02.
|
||||
EXPECT_THAT(stream.GetFilesForTesting(),
|
||||
testing::ElementsAre("20150426000002_test.mp4",
|
||||
"20150426000006_test.mp4"));
|
||||
EXPECT_THAT(
|
||||
GetFrames("20150426000002_test.mp4"),
|
||||
testing::ElementsAre(
|
||||
Frame(true, 0, 90055),
|
||||
Frame(false, 90055, 89967), // pts_time 5.000533, past rotation time.
|
||||
Frame(false, 180022, 90021),
|
||||
Frame(false, 270043,
|
||||
90000))); // XXX: duration=89958 would be better!
|
||||
EXPECT_THAT(
|
||||
GetFrames("20150426000006_test.mp4"),
|
||||
testing::ElementsAre(Frame(true, 0, 90011), Frame(false, 90011, 90000)));
|
||||
}
|
||||
|
||||
// TODO: test output stream error (on open, writing packet, closing).
|
||||
|
||||
} // namespace
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FLAGS_alsologtostderr = true;
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
551
src/moonfire-nvr.cc
Normal file
551
src/moonfire-nvr.cc
Normal file
@@ -0,0 +1,551 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// moonfire-nvr.cc: implementation of moonfire-nvr.h.
|
||||
|
||||
#define _BSD_SOURCE // for timegm(3).
|
||||
|
||||
#include "moonfire-nvr.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <event2/http.h>
|
||||
#include <gflags/gflags.h>
|
||||
#include <glog/logging.h>
|
||||
#include <re2/re2.h>
|
||||
|
||||
#include "filesystem.h"
|
||||
#include "http.h"
|
||||
#include "string.h"
|
||||
#include "time.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace moonfire_nvr {
|
||||
namespace {
|
||||
|
||||
const char kFilenameSuffix[] = ".mp4";
|
||||
|
||||
} // namespace
|
||||
FileManager::FileManager(const std::string &short_name, const std::string &path,
|
||||
uint64_t byte_limit)
|
||||
: short_name_(short_name), path_(path), byte_limit_(byte_limit) {}
|
||||
|
||||
bool FileManager::Init(std::string *error_message) {
|
||||
// Create the directory if it doesn't exist.
|
||||
// If the path exists, assume it is a valid directory.
|
||||
if (mkdir(path_.c_str(), 0700) < 0 && errno != EEXIST) {
|
||||
int err = errno;
|
||||
*error_message = StrCat("Unable to create ", path_, ": ", strerror(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
|
||||
auto file_fn = [this, &ok, error_message](const dirent *ent) {
|
||||
string filename(ent->d_name);
|
||||
if (ent->d_type != DT_REG) {
|
||||
VLOG(1) << short_name_ << ": Ignoring non-plain file " << filename;
|
||||
return IterationControl::kContinue;
|
||||
}
|
||||
if (!re2::StringPiece(filename).ends_with(kFilenameSuffix)) {
|
||||
VLOG(1) << short_name_ << ": Ignoring non-matching file " << filename
|
||||
<< " of size " << ent->d_reclen;
|
||||
return IterationControl::kContinue;
|
||||
}
|
||||
|
||||
if (!AddFile(filename, error_message)) {
|
||||
ok = false;
|
||||
return IterationControl::kBreak; // no point in doing more.
|
||||
}
|
||||
return IterationControl::kContinue;
|
||||
};
|
||||
|
||||
if (!DirForEach(path_, file_fn, error_message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool FileManager::Rotate(std::string *error_message) {
|
||||
mu_.lock();
|
||||
while (total_bytes_ > byte_limit_) {
|
||||
CHECK(!files_.empty()) << "total_bytes_=" << total_bytes_
|
||||
<< " vs retain=" << byte_limit_;
|
||||
auto it = files_.begin();
|
||||
const string filename = it->first;
|
||||
int64_t size = it->second.st_size;
|
||||
|
||||
// Release the lock while doing (potentially slow) I/O.
|
||||
// Don't mark the file as deleted yet, so that a simultaneous Rotate() call
|
||||
// won't return prematurely.
|
||||
mu_.unlock();
|
||||
string fpath = StrCat(path_, "/", filename);
|
||||
if (unlink(fpath.c_str()) == 0) {
|
||||
LOG(INFO) << short_name_ << ": Deleted " << filename << " to reclaim "
|
||||
<< size << " bytes.";
|
||||
} else if (errno == ENOENT) {
|
||||
// This may have happened due to a racing Rotate() call.
|
||||
// In any case, the file is gone, so proceed to mark it as such.
|
||||
LOG(INFO) << short_name_ << ": File " << filename
|
||||
<< " was already deleted.";
|
||||
} else {
|
||||
int err = errno;
|
||||
*error_message =
|
||||
StrCat("unlink failed on ", filename, ": ", strerror(err));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Note that the file has been deleted.
|
||||
mu_.lock();
|
||||
if (!files_.empty()) {
|
||||
it = files_.begin();
|
||||
if (it->first == filename) {
|
||||
size = it->second.st_size;
|
||||
files_.erase(it);
|
||||
CHECK_GE(total_bytes_, size);
|
||||
total_bytes_ -= size;
|
||||
}
|
||||
}
|
||||
}
|
||||
int64_t total_bytes_copy = total_bytes_;
|
||||
mu_.unlock();
|
||||
LOG(INFO) << short_name_ << ": Path " << path_ << " total size is "
|
||||
<< total_bytes_copy << ", within limit of " << byte_limit_;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileManager::AddFile(const std::string &filename,
|
||||
std::string *error_message) {
|
||||
struct stat buf;
|
||||
string fpath = StrCat(path_, "/", filename);
|
||||
if (lstat(fpath.c_str(), &buf) != 0) {
|
||||
int err = errno;
|
||||
*error_message = StrCat("lstat on ", fpath, " failed: ", strerror(err));
|
||||
return false;
|
||||
}
|
||||
VLOG(1) << short_name_ << ": adding file " << filename << " size "
|
||||
<< buf.st_size;
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
CHECK_GE(buf.st_size, 0) << fpath;
|
||||
uint64_t size = buf.st_size;
|
||||
if (!files_.emplace(filename, std::move(buf)).second) {
|
||||
*error_message = StrCat("filename ", filename, " already present.");
|
||||
return false;
|
||||
}
|
||||
total_bytes_ += size;
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileManager::ForEachFile(FileManager::FileCallback fn) const {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
for (const auto &f : files_) {
|
||||
fn(f.first, f.second);
|
||||
}
|
||||
}
|
||||
|
||||
bool FileManager::Lookup(const std::string &filename,
|
||||
struct stat *statbuf) const {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
const auto it = files_.find(filename);
|
||||
if (it != files_.end()) {
|
||||
*statbuf = it->second;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Stream::Init(std::string *error_message) {
|
||||
// Validate configuration.
|
||||
if (!IsWord(camera_.short_name())) {
|
||||
*error_message = StrCat("Camera name ", camera_.short_name(), " invalid.");
|
||||
return false;
|
||||
}
|
||||
if (rotate_interval_ <= 0) {
|
||||
*error_message = StrCat("Rotate interval for ", camera_.short_name(),
|
||||
" must be positive.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return manager_.Init(error_message);
|
||||
}
|
||||
|
||||
// Call from dedicated thread. Runs until shutdown requested.
|
||||
void Stream::Run() {
|
||||
std::string error_message;
|
||||
|
||||
// Do an initial rotation so that if retain_bytes has been reduced, the
|
||||
// bulk deletion happens now, rather than while an input stream is open.
|
||||
if (!manager_.Rotate(&error_message)) {
|
||||
LOG(WARNING) << short_name()
|
||||
<< ": initial rotation failed: " << error_message;
|
||||
}
|
||||
|
||||
while (!signal_->ShouldShutdown()) {
|
||||
if (in_ == nullptr && !OpenInput(&error_message)) {
|
||||
LOG(WARNING) << short_name()
|
||||
<< ": Failed to open input; sleeping before retrying: "
|
||||
<< error_message;
|
||||
env_->clock->Sleep({1, 0});
|
||||
continue;
|
||||
}
|
||||
|
||||
LOG(INFO) << short_name() << ": Calling ProcessPackets.";
|
||||
ProcessPacketsResult res = ProcessPackets(&error_message);
|
||||
if (res == kInputError) {
|
||||
CloseOutput();
|
||||
in_.reset();
|
||||
LOG(WARNING) << short_name()
|
||||
<< ": Input error; sleeping before retrying: "
|
||||
<< error_message;
|
||||
env_->clock->Sleep({1, 0});
|
||||
continue;
|
||||
} else if (res == kOutputError) {
|
||||
CloseOutput();
|
||||
LOG(WARNING) << short_name()
|
||||
<< ": Output error; sleeping before retrying: "
|
||||
<< error_message;
|
||||
env_->clock->Sleep({1, 0});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
CloseOutput();
|
||||
}
|
||||
|
||||
Stream::ProcessPacketsResult Stream::ProcessPackets(
|
||||
std::string *error_message) {
|
||||
moonfire_nvr::VideoPacket pkt;
|
||||
CHECK(in_ != nullptr);
|
||||
CHECK(!out_.is_open());
|
||||
while (!signal_->ShouldShutdown()) {
|
||||
if (!in_->GetNext(&pkt, error_message)) {
|
||||
if (error_message->empty()) {
|
||||
*error_message = "unexpected end of stream";
|
||||
}
|
||||
return kInputError;
|
||||
}
|
||||
|
||||
// With gcc 4.9 (Raspbian Jessie),
|
||||
// #define AV_NOPTS_VALUE INT64_C(0x8000000000000000)
|
||||
// produces an unsigned value. Argh. Work around.
|
||||
static const int64_t kAvNoptsValue = AV_NOPTS_VALUE;
|
||||
if (pkt.pkt()->pts == kAvNoptsValue || pkt.pkt()->dts == kAvNoptsValue) {
|
||||
*error_message = "Rejecting packet with missing pts/dts";
|
||||
return kInputError;
|
||||
}
|
||||
|
||||
if (pkt.pkt()->pts != pkt.pkt()->dts) {
|
||||
*error_message =
|
||||
StrCat("Rejecting packet with pts=", pkt.pkt()->pts, " != dts=",
|
||||
pkt.pkt()->dts, "; expecting only I or P frames.");
|
||||
return kInputError;
|
||||
}
|
||||
|
||||
if (pkt.pkt()->pts < min_next_pts_) {
|
||||
*error_message = StrCat("Rejecting non-increasing pts=", pkt.pkt()->pts,
|
||||
"; expected at least ", min_next_pts_);
|
||||
return kInputError;
|
||||
}
|
||||
min_next_pts_ = pkt.pkt()->pts + 1;
|
||||
|
||||
frame_realtime_ = env_->clock->Now();
|
||||
|
||||
if (out_.is_open() && frame_realtime_.tv_sec >= rotate_time_ &&
|
||||
pkt.is_key()) {
|
||||
LOG(INFO) << short_name() << ": Reached rotation time; closing "
|
||||
<< out_file_ << ".";
|
||||
VLOG(2) << short_name() << ": (Rotation time=" << rotate_time_
|
||||
<< " vs current time=" << frame_realtime_.tv_sec << ")";
|
||||
out_.Close();
|
||||
|
||||
if (!manager_.AddFile(out_file_, error_message)) {
|
||||
return kOutputError;
|
||||
}
|
||||
} else if (out_.is_open()) {
|
||||
VLOG(2) << short_name() << ": Rotation time=" << rotate_time_
|
||||
<< " vs current time=" << frame_realtime_.tv_sec;
|
||||
}
|
||||
|
||||
// Discard the initial, non-key frames from the input.
|
||||
if (!seen_key_frame_ && !pkt.is_key()) {
|
||||
continue;
|
||||
} else if (!seen_key_frame_) {
|
||||
seen_key_frame_ = true;
|
||||
}
|
||||
|
||||
if (!out_.is_open()) {
|
||||
start_pts_ = pkt.pts();
|
||||
if (!OpenOutput(error_message)) {
|
||||
return kOutputError;
|
||||
}
|
||||
rotate_time_ = frame_realtime_.tv_sec -
|
||||
(frame_realtime_.tv_sec % rotate_interval_) +
|
||||
rotate_interval_;
|
||||
}
|
||||
|
||||
// In the output stream, the pts and dts should start at 0.
|
||||
pkt.pkt()->pts -= start_pts_;
|
||||
pkt.pkt()->dts -= start_pts_;
|
||||
|
||||
// The input's byte position and stream index aren't relevant to the
|
||||
// output.
|
||||
pkt.pkt()->pos = -1;
|
||||
pkt.pkt()->stream_index = 0;
|
||||
|
||||
if (!out_.Write(&pkt, error_message)) {
|
||||
return kOutputError;
|
||||
}
|
||||
}
|
||||
return kStopped;
|
||||
}
|
||||
|
||||
bool Stream::OpenInput(std::string *error_message) {
|
||||
CHECK(in_ == nullptr);
|
||||
string url = StrCat("rtsp://", camera_.user(), ":", camera_.password(), "@",
|
||||
camera_.host(), camera_.main_rtsp_path());
|
||||
string redacted_url = StrCat("rtsp://", camera_.user(), ":redacted@",
|
||||
camera_.host(), camera_.main_rtsp_path());
|
||||
LOG(INFO) << short_name() << ": Opening input: " << redacted_url;
|
||||
in_ = env_->video_source->OpenRtsp(url, error_message);
|
||||
min_next_pts_ = std::numeric_limits<int64_t>::min();
|
||||
seen_key_frame_ = false;
|
||||
return in_ != nullptr;
|
||||
}
|
||||
|
||||
void Stream::CloseOutput() {
|
||||
out_.Close();
|
||||
// TODO: should know if the file was written or not.
|
||||
std::string error_message;
|
||||
if (!manager_.AddFile(out_file_, &error_message)) {
|
||||
VLOG(1) << short_name() << ": AddFile on recently closed output file "
|
||||
<< out_file_ << "failed; the file may never have been written: "
|
||||
<< error_message;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Stream::MakeOutputFilename() {
|
||||
const size_t kTimeBufLen = sizeof("YYYYmmDDHHMMSS");
|
||||
char formatted_time[kTimeBufLen];
|
||||
struct tm mytm;
|
||||
gmtime_r(&frame_realtime_.tv_sec, &mytm);
|
||||
strftime(formatted_time, kTimeBufLen, "%Y%m%d%H%M%S", &mytm);
|
||||
return StrCat(formatted_time, "_", camera_.short_name(), kFilenameSuffix);
|
||||
}
|
||||
|
||||
bool Stream::OpenOutput(std::string *error_message) {
|
||||
if (!manager_.Rotate(error_message)) {
|
||||
return false;
|
||||
}
|
||||
CHECK(!out_.is_open());
|
||||
string filename = MakeOutputFilename();
|
||||
if (!out_.OpenFile(StrCat(camera_path_, "/", filename), *in_,
|
||||
error_message)) {
|
||||
return false;
|
||||
}
|
||||
LOG(INFO) << short_name() << ": Opened output " << filename
|
||||
<< ", using start_pts=" << start_pts_
|
||||
<< ", input timebase=" << in_->stream()->time_base.num << "/"
|
||||
<< in_->stream()->time_base.den
|
||||
<< ", output timebase=" << out_.time_base().num << "/"
|
||||
<< out_.time_base().den;
|
||||
out_file_ = std::move(filename);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Stream::HttpCallbackForDirectory(evhttp_request *req) {
|
||||
EvBuffer buf;
|
||||
buf.AddPrintf(
|
||||
"<!DOCTYPE html>\n"
|
||||
"<html>\n"
|
||||
"<head>\n"
|
||||
"<title>%s camera recordings</title>\n"
|
||||
"<style type=\"text/css\">\n"
|
||||
"th, td { text-align: left; padding-right: 3em; }\n"
|
||||
".filename { font: 90%% monospace; }\n"
|
||||
"</style>\n"
|
||||
"</head>\n"
|
||||
"<body>\n"
|
||||
"<h1>%s camera recordings</h1>\n"
|
||||
"<p>%s</p>\n"
|
||||
"<table>\n"
|
||||
"<tr><th>Filename</th><th>Start</th><th>End</th></tr>\n",
|
||||
// short_name passed IsWord(); there's no need to escape it.
|
||||
camera_.short_name().c_str(), camera_.short_name().c_str(),
|
||||
EscapeHtml(camera_.description()).c_str());
|
||||
manager_.ForEachFile(
|
||||
[&buf](const std::string &filename, const struct stat &statbuf) {
|
||||
// Attempt to make a pretty version of the timestamp embedded in the
|
||||
// filename: with separators and in the local time zone. If this fails,
|
||||
// just leave it blank.
|
||||
string pretty_start_time;
|
||||
struct tm mytm;
|
||||
memset(&mytm, 0, sizeof(mytm));
|
||||
const size_t kTimeBufLen = 50;
|
||||
char tmbuf[kTimeBufLen];
|
||||
static const RE2 kFilenameRe(
|
||||
// YYYY mm DD HH MM SS
|
||||
"^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})_");
|
||||
if (RE2::PartialMatch(filename, kFilenameRe, &mytm.tm_year,
|
||||
&mytm.tm_mon, &mytm.tm_mday, &mytm.tm_hour,
|
||||
&mytm.tm_min, &mytm.tm_sec)) {
|
||||
mytm.tm_year -= 1900;
|
||||
mytm.tm_mon--;
|
||||
time_t start = timegm(&mytm);
|
||||
localtime_r(&start, &mytm);
|
||||
strftime(tmbuf, kTimeBufLen, "%a, %d %b %Y %H:%M:%S %Z", &mytm);
|
||||
pretty_start_time = tmbuf;
|
||||
}
|
||||
string pretty_end_time;
|
||||
localtime_r(&statbuf.st_mtime, &mytm);
|
||||
strftime(tmbuf, kTimeBufLen, "%a, %d %b %Y %H:%M:%S %Z", &mytm);
|
||||
pretty_end_time = tmbuf;
|
||||
|
||||
buf.AddPrintf(
|
||||
"<tr><td class=\"filename\"><a href=\"%s\">%s</td>"
|
||||
"<td>%s</td><td>%s</td></tr>\n",
|
||||
filename.c_str(), filename.c_str(),
|
||||
EscapeHtml(pretty_start_time).c_str(),
|
||||
EscapeHtml(pretty_end_time).c_str());
|
||||
});
|
||||
buf.AddPrintf("</table>\n</html>\n");
|
||||
evhttp_send_reply(req, HTTP_OK, "OK", buf.get());
|
||||
}
|
||||
|
||||
std::vector<std::string> Stream::GetFilesForTesting() {
|
||||
std::vector<std::string> files;
|
||||
manager_.ForEachFile(
|
||||
[&files](const std::string &filename, const struct stat &statbuf) {
|
||||
files.push_back(filename);
|
||||
});
|
||||
return files;
|
||||
}
|
||||
|
||||
void Stream::HttpCallbackForFile(evhttp_request *req, const string &filename) {
|
||||
struct stat s;
|
||||
if (!manager_.Lookup(filename, &s)) {
|
||||
return evhttp_send_error(req, HTTP_NOTFOUND, "File not found.");
|
||||
}
|
||||
HttpServeFile(req, "video/mp4", StrCat(camera_path_, "/", filename), s);
|
||||
}
|
||||
|
||||
Nvr::Nvr() {
|
||||
env_.clock = GetRealClock();
|
||||
env_.video_source = GetRealVideoSource();
|
||||
}
|
||||
|
||||
Nvr::~Nvr() {
|
||||
signal_.Shutdown();
|
||||
for (auto &thread : stream_threads_) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
bool Nvr::Init(const moonfire_nvr::Config &config, std::string *error_msg) {
|
||||
if (config.base_path().empty()) {
|
||||
*error_msg = "base_path must be configured.";
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto &camera : config.camera()) {
|
||||
streams_.emplace_back(new Stream(&signal_, config, &env_, camera));
|
||||
if (!streams_.back()->Init(error_msg)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (auto &stream : streams_) {
|
||||
stream_threads_.emplace_back([&stream]() { stream->Run(); });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Nvr::HttpCallback(evhttp_request *req) {
|
||||
if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
|
||||
return evhttp_send_error(req, HTTP_BADMETHOD, "only GET allowed");
|
||||
}
|
||||
|
||||
evhttp_uri *uri = evhttp_uri_parse(evhttp_request_get_uri(req));
|
||||
if (uri == nullptr || evhttp_uri_get_path(uri) == nullptr) {
|
||||
return evhttp_send_error(req, HTTP_INTERNAL, "Failed to parse URI.");
|
||||
}
|
||||
|
||||
std::string uri_path = evhttp_uri_get_path(uri);
|
||||
evhttp_uri_free(uri);
|
||||
uri = nullptr;
|
||||
|
||||
if (uri_path == "/") {
|
||||
return HttpCallbackForTopLevel(req);
|
||||
} else if (!re2::StringPiece(uri_path).starts_with("/c/")) {
|
||||
return evhttp_send_error(req, HTTP_NOTFOUND, "Not found.");
|
||||
}
|
||||
size_t camera_name_start = strlen("/c/");
|
||||
size_t next_slash = uri_path.find('/', camera_name_start);
|
||||
if (next_slash == std::string::npos) {
|
||||
CHECK_EQ(0, evhttp_add_header(evhttp_request_get_output_headers(req),
|
||||
"Location", StrCat(uri_path, "/").c_str()));
|
||||
return evhttp_send_reply(req, HTTP_MOVEPERM, "OK", EvBuffer().get());
|
||||
}
|
||||
re2::StringPiece camera_name =
|
||||
uri_path.substr(camera_name_start, next_slash - camera_name_start);
|
||||
for (const auto &stream : streams_) {
|
||||
if (stream->camera_name() == camera_name) {
|
||||
if (uri_path.size() == next_slash + 1) {
|
||||
return stream->HttpCallbackForDirectory(req);
|
||||
} else {
|
||||
return stream->HttpCallbackForFile(req,
|
||||
uri_path.substr(next_slash + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
return evhttp_send_error(req, HTTP_NOTFOUND, "No such camera.");
|
||||
}
|
||||
|
||||
void Nvr::HttpCallbackForTopLevel(evhttp_request *req) {
|
||||
EvBuffer buf;
|
||||
buf.Add("<ul>\n");
|
||||
for (const auto &stream : streams_) {
|
||||
// Camera name passed IsWord; there's no need to escape it.
|
||||
const string &name = stream->camera_name();
|
||||
string escaped_description = EscapeHtml(stream->camera_description());
|
||||
buf.AddPrintf("<li><a href=\"/c/%s/\">%s</a>: %s</li>\n", name.c_str(),
|
||||
name.c_str(), escaped_description.c_str());
|
||||
}
|
||||
buf.Add("</ul>\n");
|
||||
return evhttp_send_reply(req, HTTP_OK, "OK", buf.get());
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
236
src/moonfire-nvr.h
Normal file
236
src/moonfire-nvr.h
Normal file
@@ -0,0 +1,236 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// moonfire-nvr.h: main digital video recorder components.
|
||||
|
||||
#ifndef MOONFIRE_NVR_NVR_H
|
||||
#define MOONFIRE_NVR_NVR_H
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <event2/http.h>
|
||||
|
||||
#include "config.pb.h"
|
||||
#include "ffmpeg.h"
|
||||
#include "time.h"
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
// A signal that all streams associated with an Nvr should shut down.
|
||||
class ShutdownSignal {
|
||||
public:
|
||||
ShutdownSignal() {}
|
||||
ShutdownSignal(const ShutdownSignal &) = delete;
|
||||
ShutdownSignal &operator=(const ShutdownSignal &) = delete;
|
||||
|
||||
void Shutdown() { shutdown_.store(true, std::memory_order_relaxed); }
|
||||
|
||||
bool ShouldShutdown() const {
|
||||
return shutdown_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic_bool shutdown_{false};
|
||||
};
|
||||
|
||||
// Environment for streams to use. This is supplied for testability.
|
||||
struct Environment {
|
||||
WallClock *clock = nullptr;
|
||||
VideoSource *video_source = nullptr;
|
||||
};
|
||||
|
||||
// Delete old ".mp4" files within a specified directory, keeping them within a
|
||||
// byte limit. In particular, "old" means "lexographically smaller filename".
|
||||
// Thread-safe.
|
||||
//
|
||||
// On startup, FileManager reads the directory and stats every matching file.
|
||||
// Afterward, it assumes that (1) it is informed of every added file and (2)
|
||||
// files are deleted only through calls to Rotate.
|
||||
class FileManager {
|
||||
public:
|
||||
using FileCallback = std::function<void(const std::string &filename,
|
||||
const struct stat &statbuf)>;
|
||||
|
||||
// |short_name| will be prepended to log messages.
|
||||
FileManager(const std::string &short_name, const std::string &path,
|
||||
uint64_t byte_limit);
|
||||
FileManager(const FileManager &) = delete;
|
||||
FileManager &operator=(const FileManager &) = delete;
|
||||
|
||||
// Initialize the FileManager by examining existing directory contents.
|
||||
// Create the directory if necessary.
|
||||
bool Init(std::string *error_message);
|
||||
|
||||
// Delete files to go back within the byte limit if necessary.
|
||||
bool Rotate(std::string *error_message);
|
||||
|
||||
// Note that a file has been added. This may bring the FileManager over the
|
||||
// byte limit; no files will be deleted immediately.
|
||||
bool AddFile(const std::string &filename, std::string *error_message);
|
||||
|
||||
// Call |fn| for each file, while holding the lock.
|
||||
void ForEachFile(FileCallback) const;
|
||||
|
||||
// Look up a file.
|
||||
// If |filename| is known to the manager, returns true and fills |statbuf|.
|
||||
// Otherwise returns false.
|
||||
bool Lookup(const std::string &filename, struct stat *statbuf) const;
|
||||
|
||||
int64_t total_bytes() const {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
return total_bytes_;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string short_name_;
|
||||
const std::string path_;
|
||||
const uint64_t byte_limit_;
|
||||
|
||||
mutable std::mutex mu_;
|
||||
std::map<std::string, struct stat> files_;
|
||||
uint64_t total_bytes_ = 0; // total bytes of all |files_|.
|
||||
};
|
||||
|
||||
// A single video stream, currently always a camera's "main" (as opposed to
|
||||
// "sub") stream. Methods are thread-compatible rather than thread-safe; the
|
||||
// Nvr should call Init + Run in a dedicated thread.
|
||||
class Stream {
|
||||
public:
|
||||
Stream(const ShutdownSignal *signal, const moonfire_nvr::Config &config,
|
||||
const Environment *env, const moonfire_nvr::Camera &camera)
|
||||
: signal_(signal),
|
||||
env_(env),
|
||||
camera_path_(config.base_path() + "/" + camera.short_name()),
|
||||
rotate_interval_(config.rotate_sec()),
|
||||
camera_(camera),
|
||||
manager_(camera_.short_name(), camera_path_, camera.retain_bytes()) {}
|
||||
Stream(const Stream &) = delete;
|
||||
Stream &operator=(const Stream &) = delete;
|
||||
|
||||
// Call once on startup, before Run().
|
||||
bool Init(std::string *error_message);
|
||||
|
||||
const std::string &camera_name() const { return camera_.short_name(); }
|
||||
const std::string &camera_description() const {
|
||||
return camera_.description();
|
||||
}
|
||||
|
||||
// Call from dedicated thread. Runs until shutdown requested.
|
||||
void Run();
|
||||
|
||||
// Handle HTTP requests which have been pre-determined to be for the
|
||||
// directory view of this stream or a particular file, respectively.
|
||||
// Thread-safe.
|
||||
void HttpCallbackForDirectory(evhttp_request *req);
|
||||
void HttpCallbackForFile(evhttp_request *req, const std::string &filename);
|
||||
|
||||
std::vector<std::string> GetFilesForTesting();
|
||||
|
||||
private:
|
||||
enum ProcessPacketsResult { kInputError, kOutputError, kStopped };
|
||||
|
||||
const std::string &short_name() const { return camera_.short_name(); }
|
||||
|
||||
ProcessPacketsResult ProcessPackets(std::string *error_message);
|
||||
bool OpenInput(std::string *error_message);
|
||||
void CloseOutput();
|
||||
std::string MakeOutputFilename();
|
||||
bool OpenOutput(std::string *error_message);
|
||||
bool RotateFiles();
|
||||
bool Stat(const std::string &filename, struct stat *file,
|
||||
std::string *error_message);
|
||||
|
||||
const ShutdownSignal *signal_;
|
||||
const Environment *env_;
|
||||
const std::string camera_path_;
|
||||
const int32_t rotate_interval_;
|
||||
const moonfire_nvr::Camera camera_;
|
||||
|
||||
FileManager manager_; // thread-safe.
|
||||
|
||||
//
|
||||
// State below is used only by the thread in Run().
|
||||
//
|
||||
|
||||
std::unique_ptr<moonfire_nvr::InputVideoPacketStream> in_;
|
||||
int64_t min_next_pts_ = std::numeric_limits<int64_t>::min();
|
||||
bool seen_key_frame_ = false;
|
||||
|
||||
// Current output segment.
|
||||
moonfire_nvr::OutputVideoPacketStream out_;
|
||||
time_t rotate_time_ = 0; // rotate when frame_realtime_ >= rotate_time_.
|
||||
std::string out_file_; // current output filename.
|
||||
int64_t start_pts_ = -1;
|
||||
|
||||
// Packet-to-packet state.
|
||||
struct timespec frame_realtime_ = {0, 0};
|
||||
};
|
||||
|
||||
// The main network video recorder, which manages a collection of streams.
|
||||
class Nvr {
|
||||
public:
|
||||
Nvr();
|
||||
Nvr(const Nvr &) = delete;
|
||||
Nvr &operator=(const Nvr &) = delete;
|
||||
|
||||
// Shut down, blocking for outstanding streams.
|
||||
// Caller only has to guarantee that HttpCallback is not being called / will
|
||||
// not be called again, likely by having already shut down the event loop.
|
||||
~Nvr();
|
||||
|
||||
// Initialize the NVR. Call before any other operation.
|
||||
// Verifies configuration and starts background threads to capture/rotate
|
||||
// streams.
|
||||
bool Init(const moonfire_nvr::Config &config, std::string *error_msg);
|
||||
|
||||
// Handle an HTTP request.
|
||||
void HttpCallback(evhttp_request *req);
|
||||
|
||||
private:
|
||||
void HttpCallbackForTopLevel(evhttp_request *req);
|
||||
|
||||
Environment env_;
|
||||
moonfire_nvr::Config config_;
|
||||
std::vector<std::unique_ptr<Stream>> streams_;
|
||||
std::vector<std::thread> stream_threads_;
|
||||
ShutdownSignal signal_;
|
||||
};
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_NVR_H
|
||||
148
src/profiler.cc
Normal file
148
src/profiler.cc
Normal file
@@ -0,0 +1,148 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// profiler.cc: See profiler.h.
|
||||
|
||||
#include "profiler.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/keyvalq_struct.h>
|
||||
#include <event2/http.h>
|
||||
#include <gperftools/profiler.h>
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include "http.h"
|
||||
#include "string.h"
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
namespace {
|
||||
|
||||
const int kDefaultProfileSeconds = 30;
|
||||
|
||||
// Only a single CPU profile may be active at once. Track if it is active now.
|
||||
bool profiling;
|
||||
|
||||
struct ProfileRequestContext {
|
||||
#define TEMPLATE "/tmp/moonfire-nvr.profile.XXXXXX"
|
||||
char filename[sizeof(TEMPLATE)] = TEMPLATE;
|
||||
#undef TEMPLATE
|
||||
evhttp_request *req = nullptr;
|
||||
event *timer = nullptr;
|
||||
int fd = -1;
|
||||
};
|
||||
|
||||
// End a CPU profile. Serve the result from the temporary file and delete it.
|
||||
void EndProfileCallback(evutil_socket_t, short, void *arg) {
|
||||
CHECK(profiling);
|
||||
ProfilerStop();
|
||||
profiling = false;
|
||||
std::unique_ptr<ProfileRequestContext> ctx(
|
||||
reinterpret_cast<ProfileRequestContext *>(arg));
|
||||
if (unlink(ctx->filename) < 0) {
|
||||
int err = errno;
|
||||
LOG(WARNING) << "Unable to unlink temporary profile file: " << ctx->filename
|
||||
<< ": " << strerror(err);
|
||||
}
|
||||
event_free(ctx->timer);
|
||||
struct stat statbuf;
|
||||
if (fstat(ctx->fd, &statbuf) < 0) {
|
||||
close(ctx->fd);
|
||||
return HttpSendError(ctx->req, HTTP_INTERNAL, "fstat: ", errno);
|
||||
}
|
||||
EvBuffer buf;
|
||||
std::string error_message;
|
||||
if (!buf.AddFile(ctx->fd, 0, statbuf.st_size, &error_message)) {
|
||||
evhttp_send_error(ctx->req, HTTP_INTERNAL,
|
||||
EscapeHtml(error_message).c_str());
|
||||
close(ctx->fd);
|
||||
return;
|
||||
}
|
||||
evhttp_send_reply(ctx->req, HTTP_OK, "OK", buf.get());
|
||||
}
|
||||
|
||||
// Start a CPU profile. Creates a temporary file for the profiler library
|
||||
// to use and schedules a call to EndProfileCallback.
|
||||
void StartProfileCallback(struct evhttp_request *req, void *arg) {
|
||||
auto *base = reinterpret_cast<event_base *>(arg);
|
||||
if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
|
||||
return evhttp_send_error(req, HTTP_BADMETHOD, "only GET allowed");
|
||||
}
|
||||
if (profiling) {
|
||||
return evhttp_send_error(req, HTTP_SERVUNAVAIL,
|
||||
"Profiling already in progress");
|
||||
}
|
||||
struct timeval timeout = {0, 0};
|
||||
struct evkeyvalq query_options;
|
||||
evhttp_parse_query(evhttp_request_get_uri(req), &query_options);
|
||||
const char *seconds_value = evhttp_find_header(&query_options, "seconds");
|
||||
timeout.tv_sec =
|
||||
seconds_value == nullptr ? kDefaultProfileSeconds : atoi(seconds_value);
|
||||
evhttp_clear_headers(&query_options);
|
||||
if (timeout.tv_sec <= 0) {
|
||||
return evhttp_send_error(req, HTTP_BADREQUEST, "invalid seconds");
|
||||
}
|
||||
|
||||
auto *ctx = new ProfileRequestContext;
|
||||
ctx->fd = mkstemp(ctx->filename);
|
||||
if (ctx->fd < 0) {
|
||||
delete ctx;
|
||||
return HttpSendError(req, HTTP_INTERNAL, "mkstemp: ", errno);
|
||||
}
|
||||
|
||||
if (ProfilerStart(ctx->filename) == 0) {
|
||||
delete ctx;
|
||||
return evhttp_send_error(req, HTTP_INTERNAL, "ProfilerStart failed");
|
||||
}
|
||||
profiling = true;
|
||||
ctx->req = req;
|
||||
ctx->timer = evtimer_new(base, &EndProfileCallback, ctx);
|
||||
evtimer_add(ctx->timer, &timeout);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void RegisterProfiler(event_base *base, evhttp *http) {
|
||||
evhttp_set_cb(http, "/pprof/profile", &StartProfileCallback, base);
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
51
src/profiler.h
Normal file
51
src/profiler.h
Normal file
@@ -0,0 +1,51 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// profiler.h: support for on-demand profiling. The interface is described here:
|
||||
// <https://github.com/gperftools/gperftools/blob/master/doc/pprof_remote_servers.html>
|
||||
// Currently this only supports CPU profiling; heap profiling may be added
|
||||
// later.
|
||||
|
||||
#ifndef MOONFIRE_NVR_PROFILER_H
|
||||
#define MOONFIRE_NVR_PROFILER_H
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/http.h>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
// Register a HTTP URI for a CPU profile with google-perftools.
|
||||
// Not thread-safe, so |http| must be running on |base|, and |base| must be
|
||||
// using a single thread.
|
||||
void RegisterProfiler(event_base *base, evhttp *http);
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_PROFILER_H
|
||||
104
src/string-test.cc
Normal file
104
src/string-test.cc
Normal file
@@ -0,0 +1,104 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// string-test.cc: tests of the string.h interface.
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
#include <glog/logging.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "string.h"
|
||||
|
||||
DECLARE_bool(alsologtostderr);
|
||||
|
||||
namespace moonfire_nvr {
|
||||
namespace {
|
||||
|
||||
TEST(StrCatTest, Simple) {
|
||||
EXPECT_EQ("foo", StrCat("foo"));
|
||||
EXPECT_EQ("foobar", StrCat("foo", "bar"));
|
||||
EXPECT_EQ("foo", StrCat(std::string("foo")));
|
||||
|
||||
EXPECT_EQ("42", StrCat(uint64_t(42)));
|
||||
EXPECT_EQ("0", StrCat(uint64_t(0)));
|
||||
EXPECT_EQ("18446744073709551615",
|
||||
StrCat(std::numeric_limits<uint64_t>::max()));
|
||||
|
||||
EXPECT_EQ("42", StrCat(int64_t(42)));
|
||||
EXPECT_EQ("0", StrCat(int64_t(0)));
|
||||
EXPECT_EQ("-9223372036854775808",
|
||||
StrCat(std::numeric_limits<int64_t>::min()));
|
||||
EXPECT_EQ("9223372036854775807", StrCat(std::numeric_limits<int64_t>::max()));
|
||||
}
|
||||
|
||||
TEST(JoinTest, Simple) {
|
||||
EXPECT_EQ("", Join(std::initializer_list<std::string>(), ","));
|
||||
EXPECT_EQ("a", Join(std::initializer_list<std::string>({"a"}), ","));
|
||||
EXPECT_EQ("a,b", Join(std::initializer_list<const char *>({"a", "b"}), ","));
|
||||
EXPECT_EQ(
|
||||
"a,b,c",
|
||||
Join(std::initializer_list<re2::StringPiece>({"a", "b", "c"}), ","));
|
||||
}
|
||||
|
||||
TEST(IsWordTest, Simple) {
|
||||
EXPECT_TRUE(IsWord(""));
|
||||
EXPECT_TRUE(IsWord("0123456789"));
|
||||
EXPECT_TRUE(IsWord("abcdefghijklmnopqrstuvwxyz"));
|
||||
EXPECT_TRUE(IsWord("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
||||
EXPECT_TRUE(IsWord("_"));
|
||||
|
||||
EXPECT_TRUE(IsWord("4bJ_"));
|
||||
|
||||
EXPECT_FALSE(IsWord("/"));
|
||||
EXPECT_FALSE(IsWord("abc/"));
|
||||
EXPECT_FALSE(IsWord(" "));
|
||||
EXPECT_FALSE(IsWord("@"));
|
||||
EXPECT_FALSE(IsWord("["));
|
||||
EXPECT_FALSE(IsWord("`"));
|
||||
EXPECT_FALSE(IsWord("{"));
|
||||
}
|
||||
|
||||
TEST(EscapeTest, Simple) {
|
||||
EXPECT_EQ("", moonfire_nvr::EscapeHtml(""));
|
||||
EXPECT_EQ("no special chars", moonfire_nvr::EscapeHtml("no special chars"));
|
||||
EXPECT_EQ("<tag> & text", moonfire_nvr::EscapeHtml("<tag> & text"));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FLAGS_alsologtostderr = true;
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
114
src/string.cc
Normal file
114
src/string.cc
Normal file
@@ -0,0 +1,114 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// string.cc: See string.h.
|
||||
|
||||
#include "string.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
namespace internal {
|
||||
|
||||
StrCatPiece::StrCatPiece(uint64_t p) {
|
||||
if (p == 0) {
|
||||
piece_ = "0";
|
||||
} else {
|
||||
size_t i = sizeof(buf_);
|
||||
while (p != 0) {
|
||||
buf_[--i] = '0' + (p % 10);
|
||||
p /= 10;
|
||||
}
|
||||
piece_.set(buf_ + i, sizeof(buf_) - i);
|
||||
}
|
||||
}
|
||||
|
||||
StrCatPiece::StrCatPiece(int64_t p) {
|
||||
if (p == 0) {
|
||||
piece_ = "0";
|
||||
} else {
|
||||
bool negative = p < 0;
|
||||
size_t i = sizeof(buf_);
|
||||
while (p != 0) {
|
||||
buf_[--i] = '0' + std::abs(p % 10);
|
||||
p /= 10;
|
||||
}
|
||||
if (negative) {
|
||||
buf_[--i] = '-';
|
||||
}
|
||||
piece_.set(buf_ + i, sizeof(buf_) - i);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
bool IsWord(const std::string &str) {
|
||||
for (char c : str) {
|
||||
if (!(('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') ||
|
||||
('a' <= c && c <= 'z') || c == '_')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string EscapeHtml(const std::string &input) {
|
||||
std::string output;
|
||||
output.reserve(input.size());
|
||||
for (char c : input) {
|
||||
switch (c) {
|
||||
case '&':
|
||||
output.append("&");
|
||||
break;
|
||||
case '<':
|
||||
output.append("<");
|
||||
break;
|
||||
case '>':
|
||||
output.append(">");
|
||||
break;
|
||||
default:
|
||||
output.push_back(c);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
bool strto64(const char *str, int base, const char **endptr, int64_t *value) {
|
||||
static_assert(sizeof(int64_t) == sizeof(long long int),
|
||||
"unknown memory model");
|
||||
errno = 0;
|
||||
*value = ::strtoll(str, const_cast<char **>(endptr), base);
|
||||
return *endptr != str && errno == 0;
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
127
src/string.h
Normal file
127
src/string.h
Normal file
@@ -0,0 +1,127 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// string.h: convenience methods for dealing with strings.
|
||||
|
||||
#ifndef MOONFIRE_NVR_STRING_H
|
||||
#define MOONFIRE_NVR_STRING_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <re2/stringpiece.h>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
namespace internal {
|
||||
|
||||
// Only used within StrCat() and Join().
|
||||
// Note implicit constructor, which is necessary to avoid a copy,
|
||||
// though it could be wrapped in another type.
|
||||
// http://stackoverflow.com/questions/34112755/can-i-avoid-a-c11-move-when-initializing-an-array/34113744
|
||||
class StrCatPiece {
|
||||
public:
|
||||
StrCatPiece(uint64_t p);
|
||||
StrCatPiece(int64_t p);
|
||||
StrCatPiece(uint32_t p) : StrCatPiece(static_cast<uint64_t>(p)) {}
|
||||
StrCatPiece(int32_t p) : StrCatPiece(static_cast<int64_t>(p)) {}
|
||||
|
||||
#ifndef __LP64__ // if sizeof(long) == sizeof(int32_t)
|
||||
// Need to resolve ambiguity.
|
||||
StrCatPiece(long p) : StrCatPiece(static_cast<int32_t>(p)) {}
|
||||
StrCatPiece(unsigned long p) : StrCatPiece(static_cast<uint32_t>(p)) {}
|
||||
#endif
|
||||
|
||||
StrCatPiece(re2::StringPiece p) : piece_(p) {}
|
||||
|
||||
StrCatPiece(const StrCatPiece &) = delete;
|
||||
StrCatPiece &operator=(const StrCatPiece &) = delete;
|
||||
|
||||
const char *data() const { return piece_.data(); }
|
||||
size_t size() const { return piece_.size(); }
|
||||
|
||||
private:
|
||||
// Not allowed: ambiguous meaning.
|
||||
StrCatPiece(char);
|
||||
|
||||
// |piece_| points either to within buf_ (numeric constructors) or to unowned
|
||||
// string data (StringPiece constructor).
|
||||
re2::StringPiece piece_;
|
||||
char buf_[20]; // length of maximum uint64 (no terminator needed).
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
// Concatenate any number of strings, StringPieces, and numeric values into a
|
||||
// single string.
|
||||
template <typename... Types>
|
||||
std::string StrCat(Types... args) {
|
||||
internal::StrCatPiece pieces[] = {{args}...};
|
||||
size_t size = 0;
|
||||
for (const auto &p : pieces) {
|
||||
size += p.size();
|
||||
}
|
||||
std::string out;
|
||||
out.reserve(size);
|
||||
for (const auto &p : pieces) {
|
||||
out.append(p.data(), p.size());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Join any number of string fragments (of like type) together into a single
|
||||
// string, with a separator.
|
||||
template <typename Container>
|
||||
std::string Join(const Container &pieces, re2::StringPiece separator) {
|
||||
std::string out;
|
||||
bool first = true;
|
||||
for (const auto &p : pieces) {
|
||||
if (!first) {
|
||||
out.append(separator.data(), separator.size());
|
||||
}
|
||||
first = false;
|
||||
internal::StrCatPiece piece(p);
|
||||
out.append(piece.data(), piece.size());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Return true if every character in |str| is in [A-Za-z0-9_].
|
||||
bool IsWord(const std::string &str);
|
||||
|
||||
// HTML-escape the given UTF-8-encoded string.
|
||||
std::string EscapeHtml(const std::string &input);
|
||||
|
||||
// Wrapper around ::strtol that returns true iff valid and corrects
|
||||
// constness.
|
||||
bool strto64(const char *str, int base, const char **endptr, int64_t *value);
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_STRING_H
|
||||
BIN
src/testdata/clip.mp4
vendored
Normal file
BIN
src/testdata/clip.mp4
vendored
Normal file
Binary file not shown.
113
src/testutil.cc
Normal file
113
src/testutil.cc
Normal file
@@ -0,0 +1,113 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// testutil.cc: implementation of testutil.h interface.
|
||||
|
||||
#include "testutil.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include "filesystem.h"
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
namespace {
|
||||
|
||||
bool DeleteChildrenRecursively(const std::string &dirname,
|
||||
std::string *error_msg) {
|
||||
bool ok = true;
|
||||
auto fn = [&dirname, &ok, error_msg](const struct dirent *ent) {
|
||||
std::string name(ent->d_name);
|
||||
std::string path = dirname + "/" + name;
|
||||
if (name == "." || name == "..") {
|
||||
return IterationControl::kContinue;
|
||||
}
|
||||
bool is_dir = (ent->d_type == DT_DIR);
|
||||
if (ent->d_type == DT_UNKNOWN) {
|
||||
struct stat buf;
|
||||
PCHECK(stat(path.c_str(), &buf) == 0) << path;
|
||||
is_dir = S_ISDIR(buf.st_mode);
|
||||
}
|
||||
if (is_dir) {
|
||||
ok = ok && DeleteChildrenRecursively(path, error_msg);
|
||||
if (!ok) {
|
||||
return IterationControl::kBreak;
|
||||
}
|
||||
if (rmdir(path.c_str()) != 0) {
|
||||
*error_msg =
|
||||
std::string("rmdir failed on ") + path + ": " + strerror(errno);
|
||||
ok = false;
|
||||
return IterationControl::kBreak;
|
||||
}
|
||||
} else {
|
||||
if (unlink(path.c_str()) != 0) {
|
||||
*error_msg =
|
||||
std::string("unlink failed on ") + path + ": " + strerror(errno);
|
||||
ok = false;
|
||||
return IterationControl::kBreak;
|
||||
}
|
||||
}
|
||||
return IterationControl::kContinue;
|
||||
};
|
||||
if (!DirForEach(dirname, fn, error_msg)) {
|
||||
return false;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string PrepareTempDirOrDie(const std::string &test_name) {
|
||||
std::string dirname = std::string("/tmp/test.") + test_name;
|
||||
int res = mkdir(dirname.c_str(), 0700);
|
||||
if (res != 0) {
|
||||
int err = errno;
|
||||
CHECK_EQ(err, EEXIST) << "mkdir failed: " << strerror(err);
|
||||
std::string error_msg;
|
||||
CHECK(DeleteChildrenRecursively(dirname, &error_msg)) << error_msg;
|
||||
}
|
||||
return dirname;
|
||||
}
|
||||
|
||||
void WriteFileOrDie(const std::string &path, const std::string &contents) {
|
||||
std::ofstream f(path);
|
||||
f << contents;
|
||||
f.close();
|
||||
CHECK(!f.fail()) << "failed to write: " << path;
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
104
src/testutil.h
Normal file
104
src/testutil.h
Normal file
@@ -0,0 +1,104 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// testutil.h: utilities for testing.
|
||||
|
||||
#ifndef MOONFIRE_NVR_TESTUTIL_H
|
||||
#define MOONFIRE_NVR_TESTUTIL_H
|
||||
|
||||
#include <glog/logging.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
// Create or empty the given test directory, or die.
|
||||
// Returns the full path.
|
||||
std::string PrepareTempDirOrDie(const std::string &test_name);
|
||||
|
||||
// Write the given file contents to the given path, or die.
|
||||
void WriteFileOrDie(const std::string &path, const std::string &contents);
|
||||
|
||||
// A scoped log sink for testing that the right log messages are sent.
|
||||
// Modelled after glog's "mock-log.h", which is not exported.
|
||||
// Use as follows:
|
||||
//
|
||||
// {
|
||||
// ScopedMockLog log;
|
||||
// EXPECT_CALL(log, Log(ERROR, _, HasSubstr("blah blah")));
|
||||
// log.Start();
|
||||
// ThingThatLogs();
|
||||
// }
|
||||
class ScopedMockLog : public google::LogSink {
|
||||
public:
|
||||
~ScopedMockLog() final { google::RemoveLogSink(this); }
|
||||
|
||||
// Start logging to this sink.
|
||||
// This is not done at construction time so that it's possible to set
|
||||
// expectations first, which is important if some background thread is
|
||||
// already logging.
|
||||
void Start() { google::AddLogSink(this); }
|
||||
|
||||
// Set expectations here.
|
||||
MOCK_METHOD3(Log, void(google::LogSeverity severity,
|
||||
const std::string &full_filename,
|
||||
const std::string &message));
|
||||
|
||||
private:
|
||||
struct LogEntry {
|
||||
google::LogSeverity severity = -1;
|
||||
std::string full_filename;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
// This method is called with locks held and thus shouldn't call Log.
|
||||
// It just stashes away the log entry for later.
|
||||
void send(google::LogSeverity severity, const char *full_filename,
|
||||
const char *base_filename, int line, const tm *tm_time,
|
||||
const char *message, size_t message_len) final {
|
||||
pending_.severity = severity;
|
||||
pending_.full_filename = full_filename;
|
||||
pending_.message.assign(message, message_len);
|
||||
}
|
||||
|
||||
// This method is always called after send() without locks.
|
||||
// It does the actual work of calling Log. It moves data away from
|
||||
// pending_ in case Log() logs itself (causing a nested call to send() and
|
||||
// WaitTillSent()).
|
||||
void WaitTillSent() final {
|
||||
LogEntry entry = std::move(pending_);
|
||||
Log(entry.severity, entry.full_filename, entry.message);
|
||||
}
|
||||
|
||||
LogEntry pending_;
|
||||
};
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_TESTUTIL_H
|
||||
90
src/time.cc
Normal file
90
src/time.cc
Normal file
@@ -0,0 +1,90 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// time.cc: implementation of time.h interface.
|
||||
|
||||
#include "time.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
namespace {
|
||||
|
||||
class RealClock : public WallClock {
|
||||
public:
|
||||
struct timespec Now() const final {
|
||||
struct timespec now;
|
||||
CHECK_EQ(0, clock_gettime(CLOCK_REALTIME, &now)) << strerror(errno);
|
||||
return now;
|
||||
}
|
||||
|
||||
void Sleep(struct timespec req) final {
|
||||
struct timespec rem;
|
||||
while (true) {
|
||||
int ret = nanosleep(&req, &rem);
|
||||
if (ret != 0 && errno != EINTR) {
|
||||
PLOG(FATAL) << "nanosleep";
|
||||
}
|
||||
if (ret == 0) {
|
||||
return;
|
||||
}
|
||||
req = rem;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
// Returns the real wall clock, which will never be deleted.
|
||||
WallClock *GetRealClock() {
|
||||
static RealClock *real_clock = new RealClock; // never deleted.
|
||||
return real_clock;
|
||||
}
|
||||
|
||||
struct timespec SimulatedClock::Now() const {
|
||||
std::lock_guard<std::mutex> l(mu_);
|
||||
return now_;
|
||||
}
|
||||
|
||||
void SimulatedClock::Sleep(struct timespec req) {
|
||||
std::lock_guard<std::mutex> l(mu_);
|
||||
now_.tv_sec += req.tv_sec;
|
||||
now_.tv_nsec += req.tv_nsec;
|
||||
if (now_.tv_nsec > kNanos) {
|
||||
now_.tv_nsec -= kNanos;
|
||||
now_.tv_sec++;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
74
src/time.h
Normal file
74
src/time.h
Normal file
@@ -0,0 +1,74 @@
|
||||
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
||||
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
||||
//
|
||||
// This program 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.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// time.h: functions dealing with (wall) time.
|
||||
|
||||
#ifndef MOONFIRE_NVR_TIME_H
|
||||
#define MOONFIRE_NVR_TIME_H
|
||||
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace moonfire_nvr {
|
||||
|
||||
constexpr long kNanos = 1000000000;
|
||||
|
||||
class WallClock {
|
||||
public:
|
||||
virtual ~WallClock() {}
|
||||
virtual struct timespec Now() const = 0;
|
||||
virtual void Sleep(struct timespec) = 0;
|
||||
};
|
||||
|
||||
class SimulatedClock : public WallClock {
|
||||
public:
|
||||
SimulatedClock() : now_({0, 0}) {}
|
||||
struct timespec Now() const final;
|
||||
void Sleep(struct timespec req) final;
|
||||
|
||||
private:
|
||||
mutable std::mutex mu_;
|
||||
struct timespec now_;
|
||||
};
|
||||
|
||||
inline struct timespec SecToTimespec(double sec) {
|
||||
double intpart;
|
||||
double fractpart = modf(sec, &intpart);
|
||||
return {static_cast<time_t>(intpart), static_cast<long>(fractpart * kNanos)};
|
||||
}
|
||||
|
||||
// Returns the real wall clock, which will never be deleted.
|
||||
WallClock *GetRealClock();
|
||||
|
||||
} // namespace moonfire_nvr
|
||||
|
||||
#endif // MOONFIRE_NVR_TIME_H
|
||||
Reference in New Issue
Block a user