Skip to content

Commit

Permalink
Add bz2 decompression support with compressjs (cruise-automation#316)
Browse files Browse the repository at this point in the history
We already proved that compressjs works as a bz2 decoder in the rosbag.js tests: https://github.com/cruise-automation/rosbag.js/blob/68526faa242bb76943b6e42fa452f1eadb6cbc73/src/bag.test.js#L175-L178 But it wasn't imported in webviz, so bz2-compressed bags (such as the ones at https://vision.in.tum.de/data/datasets/rgbd-dataset/download#freiburg3_cabinet) didn't work by default.

It was almost very straightforward to add, except that a webpack/AMD module issue (webpack/webpack#5316) prevents us from actually using compressjs in our webpack build. I worked around it by using a string-replace-loader to just remove the offending line. It's a pretty horrible hack, but it works...let me know if you have any better suggestions.
  • Loading branch information
jtbandes authored and janpaul123 committed Jan 9, 2020
1 parent 6540d25 commit f9a6894
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 74 deletions.
138 changes: 91 additions & 47 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"seedrandom": "^2.4.4",
"sinon": "^7.1.1",
"storybook-chrome-screenshot": "^1.4.0",
"string-replace-loader": "^2.2.0",
"style-loader": "^0.23.1",
"styled-components": "4.4.1",
"terser-webpack-plugin": "^1.1.0",
Expand Down
54 changes: 28 additions & 26 deletions packages/webviz-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/webviz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"chartjs-plugin-datalabels": "0.4.0",
"chartjs-plugin-zoom": "0.6.6",
"classnames": "2.2.6",
"compressjs": "1.0.3",
"connected-react-router": "6.5.2",
"downshift": "4.0.1",
"fuzzysort": "1.1.4",
Expand Down
Binary file not shown.
12 changes: 11 additions & 1 deletion packages/webviz-core/src/dataProviders/BagDataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// found in the LICENSE file in the root directory of this source tree.
// You may not use this file except in compliance with the License.

import Bzip2 from "compressjs/lib/Bzip2";
import { keyBy } from "lodash";
import Bag, { open, Time, BagReader } from "rosbag";
import decompress from "wasm-lz4";
Expand Down Expand Up @@ -115,7 +116,16 @@ export default class BagDataProvider implements DataProvider {
message: data.buffer.slice(data.byteOffset, data.byteOffset + data.length),
});
};
const options = { topics, startTime: start, endTime: end, noParse: true, decompress: { lz4: decompress } };
const options = {
topics,
startTime: start,
endTime: end,
noParse: true,
decompress: {
bz2: (buffer: Buffer) => Buffer.from(Bzip2.decompressFile(buffer)),
lz4: decompress,
},
};
await this._bag.readMessages(options, onMessage);
return messages;
}
Expand Down
34 changes: 34 additions & 0 deletions packages/webviz-core/src/dataProviders/BagDataProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ describe("BagDataProvider", () => {
]);
});

it("initializes with bz2 bag", async () => {
const provider = new BagDataProvider(
{ bagPath: { type: "file", file: `${__dirname}/../../public/fixtures/example-bz2.bag` } },
[]
);
const result = await provider.initialize(dummyExtensionPoint);
expect(result.start).toEqual({ sec: 1396293887, nsec: 844783943 });
expect(result.end).toEqual({ sec: 1396293909, nsec: 544870199 });
expect(result.topics).toContainOnly([
{ datatype: "rosgraph_msgs/Log", name: "/rosout" },
{ datatype: "turtlesim/Color", name: "/turtle1/color_sensor" },
{ datatype: "tf2_msgs/TFMessage", name: "/tf_static" },
{ datatype: "turtlesim/Color", name: "/turtle2/color_sensor" },
{ datatype: "turtlesim/Pose", name: "/turtle1/pose" },
{ datatype: "turtlesim/Pose", name: "/turtle2/pose" },
{ datatype: "tf/tfMessage", name: "/tf" },
{ datatype: "geometry_msgs/Twist", name: "/turtle2/cmd_vel" },
{ datatype: "geometry_msgs/Twist", name: "/turtle1/cmd_vel" },
]);
expect(Object.keys(result.datatypes)).toContainOnly([
"rosgraph_msgs/Log",
"std_msgs/Header",
"turtlesim/Color",
"tf2_msgs/TFMessage",
"geometry_msgs/TransformStamped",
"geometry_msgs/Transform",
"geometry_msgs/Vector3",
"geometry_msgs/Quaternion",
"turtlesim/Pose",
"tf/tfMessage",
"geometry_msgs/Twist",
]);
});

it("calls progress callback while initializing with a local bag", async () => {
const provider = new BagDataProvider(
{ bagPath: { type: "file", file: `${__dirname}/../../public/fixtures/example.bag` } },
Expand Down
8 changes: 8 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ module.exports = {
{ test: /\.scss$/, loader: "sass-loader", options: { sourceMap: true } },
{ test: /\.woff2?$/, loader: "url-loader" },
{ test: /\.(glb|bag)$/, loader: "file-loader" },
{
test: /node_modules\/compressjs\/.*\.js/,
loader: "string-replace-loader",
options: {
search: "if (typeof define !== 'function') { var define = require('amdefine')(module); }",
replace: "/* webviz: removed broken amdefine shim (https://github.com/webpack/webpack/issues/5316) */",
},
},
],
},
optimization: {
Expand Down

0 comments on commit f9a6894

Please sign in to comment.