forked from edman007/chiton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
93 lines (82 loc) · 2.7 KB
/
camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**************************************************************************
*
* This file is part of Chiton.
*
* Chiton 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.
*
* Chiton 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 Chiton. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright 2020 Ed Martin <[email protected]>
*
**************************************************************************
*/
#include "camera.hpp"
#include "util.hpp"
#include "stream_writer.hpp"
Camera::Camera(int camera, Database& db) : id(camera), db(db), stream(cfg), fm(db, cfg) {
//load the config
load_cfg();
//stream = StreamUnwrap(cfg);
shutdown = false;
alive = true;
watchdog = false;
}
Camera::~Camera(){
}
void Camera::load_cfg(void){
//loads the global and then overwrites it with the local
DatabaseResult *res = db.query("SELECT name, value FROM config WHERE camera IS NULL OR camera = " + std::to_string(id) + " ORDER by camera DESC" );
while (res && res->next_row()){
cfg.set_value(res->get_field(0), res->get_field(1));
}
delete res;
}
void Camera::run(void){
LINFO("Camera " + std::to_string(id) + " starting...");
if (!stream.connect()){
alive = false;
return;
}
LINFO("Camera " + std::to_string(id) + " connected...");
long int file_id;
struct timeval start;
Util::get_videotime(start);
std::string new_output = fm.get_next_path(file_id, id, start);
StreamWriter out = StreamWriter(cfg, new_output, stream);
out.open();
AVPacket pkt;
bool valid_keyframe = false;
long max_dts = 0;
while (!shutdown && stream.get_next_frame(pkt)){
watchdog = true;
if (valid_keyframe || pkt.flags & AV_PKT_FLAG_KEY){
out.write(pkt);//log it
valid_keyframe = true;
//LINFO("Got Frame " + std::to_string(id));
} else {
LINFO("Waiting for a keyframe...");
}
stream.unref_frame(pkt);
}
LINFO("Camera " + std::to_string(id)+ " is exiting");
out.close();
alive = false;
}
void Camera::stop(void){
shutdown = true;
}
bool Camera::ping(void){
return !watchdog.exchange(false) || !alive;
}
int Camera::get_id(void){
return id;
}