Skip to content

Commit

Permalink
Shutdown the camera if we cannot record data, do not crash trying to
Browse files Browse the repository at this point in the history
  write to a closed file
  • Loading branch information
edman007 committed Sep 9, 2020
1 parent f32a02e commit fe659dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ void Camera::run(void){
}
new_output = fm.get_next_path(file_id, id, start);
out.change_path(new_output);
out.open();
if (!out.open()){
shutdown = true;
}
//save out this position
last_cut = av_mul_q(av_make_q(pkt.dts, 1), stream.get_format_context()->streams[pkt.stream_index]->time_base);
}
Expand Down
10 changes: 8 additions & 2 deletions stream_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
#include <assert.h>

StreamWriter::StreamWriter(Config& cfg, std::string path, StreamUnwrap &unwrap) : cfg(cfg), path(path), unwrap(unwrap) {
//nothing!
file_opened = false;
}

bool StreamWriter::open(void){
int error;

file_opened = false;
avformat_alloc_output_context2(&output_format_context, NULL, NULL, path.c_str());
if (!output_format_context) {
LERROR("Could not create output context");
Expand Down Expand Up @@ -105,6 +105,7 @@ bool StreamWriter::open(void){
LERROR("Error occurred: " + std::string(av_err2str(error)));
return false;
}
file_opened = true;
return true;

}
Expand All @@ -126,11 +127,16 @@ void StreamWriter::close(void){
}

av_write_trailer(output_format_context);
file_opened = false;
}

bool StreamWriter::write(const AVPacket &packet){
AVStream *in_stream, *out_stream;
AVPacket out_pkt;

if (!file_opened){
return false;
}
if (av_packet_ref(&out_pkt, &packet)){
LERROR("Could not allocate new output packet for writing");
return false;
Expand Down
6 changes: 5 additions & 1 deletion stream_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class StreamWriter {
StreamWriter(Config &cfg, std::string path, StreamUnwrap &unwrap);
~StreamWriter();

bool open();//open the file for writing, returns true on success
bool open();//open the file for writing, returns true on success
void close(void);
bool write(const AVPacket &pkt);//write the packet to the file
void change_path(std::string &new_path);
Expand All @@ -47,6 +47,10 @@ class StreamWriter {
//these offsets are used to shift the time when receiving something
std::vector<long> stream_offset;
std::vector<long> last_dts;//used to fix non-increasing DTS

//used to track if we were successful in getting a file opened (and skip writing anything if not successful)
//true means the file is opened
bool file_opened;

void log_packet(const AVFormatContext *fmt_ctx, const AVPacket &pkt, const std::string &tag);
};
Expand Down

0 comments on commit fe659dc

Please sign in to comment.