48 #define foreach BOOST_FOREACH 51 using std::priority_queue;
56 using std::shared_ptr;
63 mode_(bagmode::
Write),
66 chunk_threshold_(768 * 1024),
74 curr_chunk_data_pos_(0),
76 decompressed_chunk_(0)
201 std::map<std::string, uint64_t> compression_counts;
202 std::map<std::string, uint64_t> compression_uncompressed;
203 std::map<std::string, uint64_t> compression_compressed;
209 curr_chunk_info = chunk_info;
225 auto chunk_count =
chunks_.size();
226 uint64_t main_compression_count = 0;
227 std::string main_compression;
228 for (
auto&&
kvp : compression_counts)
230 if (
kvp.second > main_compression_count)
232 main_compression =
kvp.first;
233 main_compression_count =
kvp.second;
239 return std::make_tuple(main_compression, compressed, uncompressed);
249 (
format(
"Unknown compression type: %i") % compression).
str());
272 char logtypename[100];
273 int version_major, version_minor;
274 #if defined(_MSC_VER) 275 if (sscanf_s(version_line.c_str(),
"#ROS%s V%d.%d", logtypename,
static_cast<unsigned>(
sizeof(logtypename)), &version_major, &version_minor) != 3)
277 if (sscanf(version_line.c_str(),
"#ROS%s V%d.%d", logtypename, &version_major, &version_minor) != 3)
281 version_ = version_major * 100 + version_minor;
369 multiset<IndexEntry>
const&
index =
i->second;
370 IndexEntry const& first_entry = *index.begin();
396 std::vector<uint8_t> header_buffer;
402 write((
char*) &header_len, 4);
403 write((
char*) header_buffer.data(), header_len);
404 write((
char*) &data_len, 4);
409 padding.resize(data_len,
' ');
428 if (index_data_pos_ == 0)
441 seek(data_size, std::ios::cur);
485 seek(end_of_chunk_pos);
498 switch (compression) {
540 multiset<IndexEntry>
const&
index =
i->second;
585 if (index_version != 0)
595 connection_info->id = connection_id;
596 connection_info->topic = topic;
602 connection_id = topic_conn_id_iter->second;
610 read((
char*) &sec, 4);
611 read((
char*) &nsec, 4);
623 connection_index.insert(connection_index.end(), index_entry);
645 CONSOLE_BRIDGE_logDebug(
"Read INDEX_DATA: ver=%d connection=%d count=%d", index_version, connection_id, count);
647 if (index_version != 1)
659 read((
char*) &sec, 4);
660 read((
char*) &nsec, 4);
671 connection_index.insert(connection_index.end(), index_entry);
727 map<uint32_t, ConnectionInfo*>::iterator key =
connections_.find(
id);
730 connection_info->
id =
id;
731 connection_info->
topic = topic;
732 connection_info->
header = std::make_shared<M_string>();
733 for (M_string::const_iterator
i = connection_header.
getValues()->begin();
i != connection_header.
getValues()->end();
i++)
734 (*connection_info->
header)[
i->first] =
i->second;
735 connection_info->
msg_def = (*connection_info->
header)[
"message_definition"];
737 connection_info->
md5sum = (*connection_info->
header)[
"md5sum"];
766 CONSOLE_BRIDGE_logDebug(
"Creating connection: topic=%s md5sum=%s datatype=%s", topic.c_str(), md5sum.c_str(), datatype.c_str());
768 connection_info->id =
id;
769 connection_info->topic = topic;
775 connection_info =
connections_[topic_conn_id_iter->second];
777 connection_info->
msg_def = message_definition;
780 connection_info->
header = std::make_shared<rs2rosinternal::M_string>();
782 (*connection_info->
header)[
"md5sum"] = connection_info->
md5sum;
783 (*connection_info->
header)[
"message_definition"] = connection_info->
msg_def;
785 CONSOLE_BRIDGE_logDebug(
"Read MSG_DEF: topic=%s md5sum=%s datatype=%s", topic.c_str(), md5sum.c_str(), datatype.c_str());
949 write((
char*) &connection_id, 4);
950 write((
char*) &count, 4);
978 uint32_t chunk_connection_count = 0;
982 (
unsigned long long) chunk_info.
pos, chunk_connection_count,
987 for (
uint32_t i = 0;
i < chunk_connection_count;
i ++) {
988 uint32_t connection_id, connection_count;
989 read((
char*) &connection_id, 4);
990 read((
char*) &connection_count, 4);
1009 std::vector<uint8_t> header_buffer;
1012 write((
char*) &header_len, 4);
1013 write((
char*) header_buffer.data(), header_len);
1017 write((
char*) &data_len, 4);
1021 std::vector<uint8_t> header_buffer;
1031 memcpy(buf.
getData() +
offset, header_buffer.data(), header_len);
1052 memcpy(&header_len, ptr, 4);
1057 bool parsed = header.
parse(ptr, header_len, error_msg);
1063 memcpy(&data_size, ptr, 4);
1071 total_bytes_read = 0;
1078 offset += bytes_read;
1079 total_bytes_read += bytes_read;
1092 read((
char*) &header_len, 4);
1108 read((
char*) &data_size, 4);
1112 M_string::const_iterator
Bag::checkField(
M_string const& fields,
string const& field,
unsigned int min_len,
unsigned int max_len,
bool required)
const {
1113 M_string::const_iterator fitr = fields.find(field);
1114 if (fitr == fields.end()) {
1118 else if ((fitr->second.size() < min_len) || (fitr->second.size() > max_len))
1125 return readField(fields, field_name, 1, UINT_MAX, required, data);
1128 bool Bag::readField(
M_string const& fields,
string const& field_name,
unsigned int min_len,
unsigned int max_len,
bool required,
string& data)
const {
1129 M_string::const_iterator fitr =
checkField(fields, field_name, min_len, max_len, required);
1130 if (fitr == fields.end())
1133 data = fitr->second;
1139 if (!
readField(fields, field_name, required, &packed_time))
1142 uint64_t bitmask = (1LL << 33) - 1;
static const std::string COMPRESSION_BZ2
void openRead(std::string const &filename)
open file for reading
static const std::string END_TIME_FIELD_NAME
const char * md5sum()
returns MD5Sum<M>::value();
static const std::string CHUNK_COUNT_FIELD_NAME
void read(void *ptr, size_t size)
read size bytes from the file into ptr
static const std::string COMPRESSION_FIELD_NAME
std::map< std::string, uint32_t > topic_connection_ids_
uint32_t getMajorVersion() const
Get the major-version of the open bag file.
BagMode getMode() const
Get the mode the bag is in.
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
void decompressLz4Chunk(ChunkHeader const &chunk_header) const
uint64_t pos
latest timestamp of a message in the chunk
void openRead(std::string const &filename)
static const std::string VER_FIELD_NAME
CompressionType getCompression() const
Get the compression method to use for writing chunks.
static const std::string COMPRESSION_LZ4
bool isOpen() const
return true if file is open for reading or writing
rs2rosinternal::Time start_time
void open(std::string const &filename, uint32_t mode=bagmode::Read)
Open a bag file.
GLsizei const GLchar *const * string
rs2rosinternal::Time end_time
earliest timestamp of a message in the chunk
std::map< uint32_t, ConnectionInfo * > connections_
ROSTIME_DECL const Time TIME_MAX
std::string toHeaderString(T const *field) const
static const std::string CONNECTION_FIELD_NAME
void startReadingVersion102()
void write(std::string const &s)
static const std::string INDEX_POS_FIELD_NAME
void setChunkThreshold(uint32_t chunk_threshold)
Set the threshold for creating new chunks.
void readTopicIndexRecord102()
std::map< uint32_t, uint32_t > connection_counts
absolute byte offset of chunk record in bag file
rs2rosinternal::Header readMessageDataHeader(IndexEntry const &index_entry)
Buffer header_buffer_
reusable buffer in which to assemble the record header before writing to file
static const unsigned char OP_CONNECTION
std::map< rs2rosinternal::M_string, uint32_t > header_connection_ids_
void openWrite(std::string const &filename)
uint32_t getMinorVersion() const
Get the minor-version of the open bag file.
Base class for rosbag exceptions.
bool truncate(uint64_t length)
void seek(uint64_t offset, int origin=std::ios_base::beg)
seek to given offset from origin
std_msgs::Header * header(M &m)
returns Header<M>::pointer(m);
rs2rosinternal::M_string::const_iterator checkField(rs2rosinternal::M_string const &fields, std::string const &field, unsigned int min_len, unsigned int max_len, bool required) const
void writeConnectionRecords()
static const std::string DEF_FIELD_NAME
void writeConnectionRecord(ConnectionInfo const *connection_info)
void openAppend(std::string const &filename)
void close()
close the file
void seek(uint64_t pos, int origin=std::ios_base::beg) const
void appendDataLengthToBuffer(Buffer &buf, uint32_t data_len)
std::string getFileName() const
return path of currently open file
BagMode
The possible modes to open a bag in.
uint64_t getOffset() const
return current offset from the beginning of the file
void setWriteMode(CompressionType type)
static const unsigned char OP_MSG_DATA
void close()
Close the bag file.
const char * datatype()
returns DataType<M>::value();
uint32_t getCompressedBytesIn() const
return the number of bytes written to current compressed stream
GLenum GLuint GLenum GLsizei const GLchar * buf
static const unsigned char OP_CHUNK
void readChunkHeader(ChunkHeader &chunk_header) const
static const std::string VERSION
Buffer decompress_buffer_
reusable buffer to decompress chunks into
uint64_t getSize() const
Get the current size of the bag file (a lower bound)
void appendConnectionRecordToBuffer(Buffer &buf, ConnectionInfo const *connection_info)
void readMessageDefinitionRecord102()
void writeFileHeaderRecord()
static const std::string CHUNK_POS_FIELD_NAME
uint64_t decompressed_chunk_
position of decompressed chunk
#define assert(condition)
std::shared_ptr< rs2rosinternal::M_string > header
static const std::string START_TIME_FIELD_NAME
uint32_t getChunkOffset() const
Exception thrown on problems reading the bag index.
static const uint32_t CHUNK_INFO_VERSION
void readMessageDataRecord102(uint64_t offset, rs2rosinternal::Header &header) const
Exception thrown when on IO problems.
GLint GLint GLsizei GLint GLenum format
void readMessageDataHeaderFromBuffer(Buffer &buffer, uint32_t offset, rs2rosinternal::Header &header, uint32_t &data_size, uint32_t &bytes_read) const
void write(std::string const &topic, rs2rosinternal::MessageEvent< T > const &event)
Write a message into the bag file.
Time representation. May either represent wall clock time or ROS clock time.
std::map< std::string, std::string > M_string
void setCompression(CompressionType compression)
Set the compression method to use for writing chunks.
unsigned __int64 uint64_t
void writeChunkHeader(CompressionType compression, uint32_t compressed_size, uint32_t uncompressed_size)
static const std::string OP_FIELD_NAME
uint64_t file_header_pos_
Buffer record_buffer_
reusable buffer in which to assemble the record data before writing to file
static const std::string TYPE_FIELD_NAME
uint32_t readMessageDataSize(IndexEntry const &index_entry) const
void startWritingChunk(rs2rosinternal::Time time)
Buffer chunk_buffer_
reusable buffer to read chunk into
ChunkInfo curr_chunk_info_
basic_format< char > format
static const std::string COUNT_FIELD_NAME
void decompressChunk(uint64_t chunk_pos) const
std::map< uint32_t, std::multiset< IndexEntry > > connection_indexes_
#define CONSOLE_BRIDGE_logDebug(fmt,...)
uint64_t chunk_pos
timestamp of the message
static const unsigned char OP_FILE_HEADER
static const unsigned char OP_INDEX_DATA
uint32_t connection_count_
void readConnectionIndexRecord200()
bool isOp(rs2rosinternal::M_string &fields, uint8_t reqOp) const
void readChunkInfoRecord()
void decompressRawChunk(ChunkHeader const &chunk_header) const
void openWrite(std::string const &filename)
open file for writing
GLboolean GLboolean GLboolean b
uint32_t offset
absolute byte offset of the chunk record containing the message
uint32_t chunk_threshold_
std::vector< ChunkInfo > chunks_
void startReadingVersion200()
void read(char *b, std::streamsize n) const
ROSTIME_DECL const Time TIME_MIN
std::tuple< std::string, uint64_t, uint64_t > getCompressionInfo() const
void writeDataLength(uint32_t data_len)
uint32_t getChunkThreshold() const
Get the threshold for creating new chunks.
bool readHeader(rs2rosinternal::Header &header) const
void readHeaderFromBuffer(Buffer &buffer, uint32_t offset, rs2rosinternal::Header &header, uint32_t &data_size, uint32_t &bytes_read) const
CompressionType compression_
void decompress(CompressionType compression, uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
std::map< uint32_t, std::multiset< IndexEntry > > curr_chunk_connection_indexes_
static const std::string CONNECTION_COUNT_FIELD_NAME
static const uint32_t FILE_HEADER_LENGTH
uint64_t curr_chunk_data_pos_
static const std::string MD5_FIELD_NAME
Buffer outgoing_chunk_buffer_
reusable buffer to read chunk into
std::string getFileName() const
Get the filename of the bag.
static const unsigned char OP_CHUNK_INFO
bool readDataLength(uint32_t &data_size) const
static const std::string SIZE_FIELD_NAME
void readConnectionRecord()
static const std::string COMPRESSION_NONE
void appendHeaderToBuffer(Buffer &buf, rs2rosinternal::M_string const &fields)
rs2rosinternal::Time time
void writeChunkInfoRecords()
void writeHeader(rs2rosinternal::M_string const &fields)
static const std::string TOPIC_FIELD_NAME
::std_msgs::Time_< std::allocator< void > > Time
void openReadWrite(std::string const &filename)
open file for reading & writing
static const unsigned char OP_MSG_DEF
void setSize(uint32_t size)
bool readField(rs2rosinternal::M_string const &fields, std::string const &field_name, bool required, T *data) const
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
void readFileHeaderRecord()
static const uint32_t INDEX_VERSION
void decompressBz2Chunk(ChunkHeader const &chunk_header) const
#define CONSOLE_BRIDGE_logError(fmt,...)