-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for compression to python API
- Loading branch information
Showing
5 changed files
with
183 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "rosbag2_compression/compression_options.hpp" | ||
|
||
#include "./pybind11.hpp" | ||
|
||
using CompressionMode = rosbag2_compression::CompressionMode; | ||
using CompressionOptions = rosbag2_compression::CompressionOptions; | ||
|
||
PYBIND11_MODULE(_compression_options, m) { | ||
m.doc() = "Python wrapper of the rosbag2_compression API"; | ||
|
||
pybind11::enum_<CompressionMode>(m, "CompressionMode") | ||
.value("NONE", CompressionMode::NONE) | ||
.value("FILE", CompressionMode::FILE) | ||
.value("MESSAGE", CompressionMode::MESSAGE) | ||
.export_values(); | ||
|
||
pybind11::class_<CompressionOptions>(m, "CompressionOptions") | ||
.def(pybind11::init<std::string &, CompressionMode, uint64_t &, uint64_t &>(), | ||
pybind11::arg("compression_format") = CompressionOptions{}.compression_format, | ||
pybind11::arg("compression_mode") = CompressionOptions{}.compression_mode, | ||
pybind11::arg("compression_queue_size") = CompressionOptions{}.compression_queue_size, | ||
pybind11::arg("compression_threads") = CompressionOptions{}.compression_threads) | ||
.def_readwrite("compression_format", &CompressionOptions::compression_format) | ||
.def_readwrite("compression_mode", &CompressionOptions::compression_mode) | ||
.def_readwrite("compression_queue_size", &CompressionOptions::compression_queue_size) | ||
.def_readwrite("compression_threads", &CompressionOptions::compression_threads); | ||
|
||
m.def( | ||
"compression_mode_from_string", | ||
&rosbag2_compression::compression_mode_from_string, | ||
"Converts a string into a rosbag2_compression::CompressionMode enum."); | ||
|
||
m.def( | ||
"compression_mode_to_string", | ||
&rosbag2_compression::compression_mode_to_string, | ||
"Converts a rosbag2_compression::CompressionMode enum into a string"); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright 2022, Foxglove Technologies. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import unittest | ||
|
||
from rosidl_runtime_py.utilities import get_message | ||
from rclpy.duration import Duration | ||
from rclpy.serialization import deserialize_message, serialize_message | ||
from rclpy.time import Time | ||
from rosbag2_py import ( | ||
CompressionMode, | ||
CompressionOptions, | ||
compression_mode_from_string, | ||
compression_mode_to_string, | ||
SequentialCompressionReader, | ||
SequentialCompressionWriter, | ||
TopicMetadata | ||
) | ||
|
||
from std_msgs.msg import String | ||
|
||
from common import get_rosbag_options | ||
|
||
|
||
def test_compression_mode_from_string(): | ||
"""Checks that we can cast a string to a compression mode""" | ||
|
||
assert CompressionMode.NONE == compression_mode_from_string("NONE") | ||
assert CompressionMode.MESSAGE == compression_mode_from_string("MESSAGE") | ||
assert CompressionMode.FILE == compression_mode_from_string("FILE") | ||
|
||
def test_compression_mode_to_string(): | ||
"""Checks that we can cast a compression mode to a string""" | ||
|
||
assert "NONE" == compression_mode_to_string(CompressionMode.NONE) | ||
assert "MESSAGE" == compression_mode_to_string(CompressionMode.MESSAGE) | ||
assert "FILE" == compression_mode_to_string(CompressionMode.FILE) | ||
|
||
def test_compression_options(): | ||
"""Checks that we can construct a CompressionOptions class""" | ||
|
||
compression_options = CompressionOptions( | ||
compression_format = "zstd", | ||
compression_mode = CompressionMode.MESSAGE, | ||
compression_queue_size = 0, | ||
compression_threads = 8) | ||
assert compression_options is not None | ||
assert compression_options.compression_format == "zstd" | ||
assert compression_options.compression_mode == CompressionMode.MESSAGE | ||
assert compression_options.compression_queue_size == 0 | ||
assert compression_options.compression_threads == 8 | ||
|
||
|
||
def test_sequential_compression_writer(tmp_path): | ||
"""Checks that we can do a compressed write and read""" | ||
|
||
bag_path = os.path.join(tmp_path, 'tmp_sequential_compressed_write_test') | ||
|
||
storage_id = 'mcap' | ||
|
||
storage_options, converter_options = get_rosbag_options( | ||
path = bag_path, | ||
storage_id = storage_id) | ||
|
||
compression_options = CompressionOptions( | ||
compression_format = 'zstd', | ||
compression_mode = CompressionMode.MESSAGE, | ||
compression_queue_size = 0, | ||
compression_threads = 16) | ||
|
||
writer = SequentialCompressionWriter(compression_options) | ||
writer.open(storage_options, converter_options) | ||
|
||
topic_name = '/chatter' | ||
topic_metadata = TopicMetadata( | ||
name=topic_name, | ||
type='std_msgs/msg/String', | ||
serialization_format='cdr') | ||
writer.create_topic(topic_metadata) | ||
|
||
for i in range(10): | ||
msg = String() | ||
msg.data = f'Hello, world! {str(i)}' | ||
time_stamp = i * 100 | ||
|
||
writer.write(topic_name, serialize_message(msg), time_stamp) | ||
|
||
# close bag and create new storage instance | ||
del writer |