gstreamer
Loading...
Searching...
No Matches
gstreamerJSONConnection.cc
Go to the documentation of this file.
1// gstreamer
4
5// c++
6#include <iomanip>
7
8bool GstreamerJsonFactory::openConnection() {
9 if (ofile.is_open()) {
10 // Already open for this thread; nothing to do.
11 return true;
12 }
13
14 ofile.clear();
15 ofile.open(filename(), std::ios::out | std::ios::trunc);
16
17 if (!ofile.is_open() || !ofile) {
18 log->error(ERR_CANTOPENOUTPUT, SFUNCTION_NAME, " could not open file ", filename());
19 return false;
20 }
21
22 log->info(0, "GstreamerJsonFactory: opened file " + filename());
23
24 // Defer writing the top-level JSON object until we know whether this is an event or stream writer.
25 is_file_initialized = false;
26 wrote_first_top_level_entry = false;
27 top_level_type.clear();
28
29 return true;
30}
31
32bool GstreamerJsonFactory::closeConnectionImpl() {
33 // Ensure buffered events are written before closing the JSON structure.
35
36 // Close the JSON document if we ever started it.
37 closeTopLevelObjectIfNeeded();
38
39 if (ofile.is_open()) ofile.close();
40 if (ofile.is_open()) {
41 log->error(ERR_CANTCLOSEOUTPUT, SFUNCTION_NAME, " could not close file ", filename());
42 return false;
43 }
44
45 return true;
46}
47
48void GstreamerJsonFactory::ensureFileInitializedForType(const std::string& type) {
49 if (is_file_initialized) return;
50
51 top_level_type = type;
52
53 // Single top-level object with a single array.
54 // { "type":"event", "events":[ ... ] }
55 ofile << "{\n";
56 ofile << " \"type\": \"" << jsonEscape(type) << "\",\n";
57
58 if (type == "event") {
59 ofile << " \"events\": [\n";
60 }
61 else {
62 ofile << " \"frames\": [\n";
63 }
64
65 is_file_initialized = true;
66 wrote_first_top_level_entry = false;
67}
68
69void GstreamerJsonFactory::writeTopLevelEntry(const std::string& entry_json) {
70 // The caller must have initialized the file for the correct type.
71 if (!is_file_initialized) {
72 log->error(ERR_PUBLISH_ERROR, "JSON file is not initialized in GstreamerJsonFactory::writeTopLevelEntry");
73 return;
74 }
75
76 // Comma-separate entries inside the array.
77 if (wrote_first_top_level_entry) {
78 ofile << ",\n";
79 }
80 wrote_first_top_level_entry = true;
81
82 // Indent entries consistently.
83 ofile << " " << entry_json;
84}
85
86void GstreamerJsonFactory::closeTopLevelObjectIfNeeded() {
87 if (!is_file_initialized) return;
88
89 // Close array and object.
90 ofile << "\n ]\n";
91 ofile << "}\n";
92 is_file_initialized = false;
93}
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
void flushEventBuffer()
Flush the internal event buffer, writing all buffered events to the output medium.
Definition gstreamer.cc:34
Shared constants and error codes for the gstreamer module.
#define ERR_CANTCLOSEOUTPUT
Output medium could not be closed cleanly.
#define ERR_PUBLISH_ERROR
Generic publish-time error (null pointers, invalid state).
#define ERR_CANTOPENOUTPUT
Output medium could not be opened (file/device not accessible).