Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protobuf support to @xviz/io #500

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@turf/turf": "^5.1.6",
"base64-js": "^1.3.0",
"math.gl": "^2.0.0",
"protobufjs": "^6.8.8",
"text-encoding": "^0.6.4"
},
"scripts": {},
Expand Down
2 changes: 2 additions & 0 deletions modules/io/src/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
export const XVIZ_FORMAT = Object.freeze({
// Binary GLB enocded in Buffer/ArrayBuffer
BINARY_GLB: 'BINARY_GLB',
// Protobuf encoded in a Buffer/ArrayBuffer
BINARY_PBE: 'BINARY_PBE',
// JSON encoded in a Buffer/ArrayBuffer
JSON_BUFFER: 'JSON_BUFFER',
// JSON encoded in a String
Expand Down
72 changes: 69 additions & 3 deletions modules/io/src/common/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {GLTFParser} from '@loaders.gl/gltf';

import {XVIZ_GLTF_EXTENSION} from './constants';
import {TextDecoder} from './text-encoding';
import {MAGIC_PBE1, XVIZ_PROTOBUF_MESSAGE} from './protobuf-support';

// XVIZ Type constants
const XVIZ_TYPE_PATTERN = /"type":\s*"(xviz\/\w*)"/;
Expand Down Expand Up @@ -93,14 +94,23 @@ function checkMagic(glbArrayBuffer, options = {}) {
return magic1 === magic || (magicAlt && magicAlt === magic1);
}

// Supports GLB format
// Supports GLB and Protobuf formats
export function isBinaryXVIZ(arrayBuffer) {
const isArrayBuffer = arrayBuffer instanceof ArrayBuffer;
return isArrayBuffer && checkMagic(arrayBuffer, {magic: MAGIC_XVIZ, magicAlt: MAGIC_GLTF});
return (
isArrayBuffer &&
(checkMagic(arrayBuffer, {magic: MAGIC_XVIZ, magicAlt: MAGIC_GLTF}) ||
checkMagic(arrayBuffer, {magic: MAGIC_PBE1}))
);
}

// Supports GLB format
// Supports GLB and Protobuf formats
export function parseBinaryXVIZ(arrayBuffer) {
if (checkMagic(arrayBuffer, {magic: MAGIC_PBE1})) {
const data = parsePBEXVIZ(arrayBuffer);
return data;
}

const gltfParser = new GLTFParser();
gltfParser.parse(arrayBuffer, {createImages: false});

Expand Down Expand Up @@ -170,6 +180,60 @@ function getGLBXVIZJSONBuffer(arrayBuffer, byteOffset = 0) {
return new Uint8Array(arrayBuffer, byteOffset + glb.jsonChunkByteOffset, glb.jsonChunkLength);
}

/* Protobuf Support */

export function isPBEXVIZ(arrayBuffer) {
const isArrayBuffer = arrayBuffer instanceof ArrayBuffer;
return isArrayBuffer && checkMagic(arrayBuffer, {magic: MAGIC_PBE1});
}

function getPBEXVIZType(arrayBuffer) {
const strippedBuffer = new Uint8Array(arrayBuffer, 4);
// TODO: this toObject is too expensive, we can do
// this with decode only
const envelope = XVIZ_PROTOBUF_MESSAGE.Envelope.toObject(strippedBuffer, {
enum: String
});

return envelope.type;
}

// TODO: unpackEnvelop produces namespace, type data
export function parsePBEXVIZ(arrayBuffer) {
const strippedBuffer = new Uint8Array(arrayBuffer, 4);
const envelope = XVIZ_PROTOBUF_MESSAGE.Envelope.decode(strippedBuffer);

const xviz = {
type: envelope.type,
data: null
};

switch (envelope.type) {
case 'xviz/metadata':
// TODO: support enum integers when parsing to avoid costly toObject() call
// xviz.data = XVIZ_PROTOBUF_MESSAGE.Metadata.decode(envelope.data.value);
const tmpMeta = XVIZ_PROTOBUF_MESSAGE.Metadata.decode(envelope.data.value);
// This toObject is required to change enums to strings for now
xviz.data = XVIZ_PROTOBUF_MESSAGE.Metadata.toObject(tmpMeta, {
enums: String
});
break;
case 'xviz/state_update':
// TODO: support enum integers when parsing to avoid costly toObject() call
// xviz.data = XVIZ_PROTOBUF_MESSAGE.StateUpdate.decode(envelope.data.value);
const tmpState = XVIZ_PROTOBUF_MESSAGE.StateUpdate.decode(envelope.data.value);
// This toObject is required to change enums to strings for now
xviz.data = XVIZ_PROTOBUF_MESSAGE.StateUpdate.toObject(tmpState, {
enums: String
});
break;
default:
throw new Error(`Unknown Message type ${envelope.type}`);
}

return xviz;
}

/* JSON Support */

// Return true if the ArrayBuffer represents a JSON string.
Expand Down Expand Up @@ -268,6 +332,8 @@ export function getXVIZMessageType(data) {
case 'binary':
if (isGLBXVIZ(data)) {
return getGLBXVIZType(data);
} else if (isPBEXVIZ(data)) {
return getPBEXVIZType(data);
}
if (data instanceof ArrayBuffer) {
data = new Uint8Array(data);
Expand Down
26 changes: 26 additions & 0 deletions modules/io/src/common/protobuf-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {loadProtos} from '@xviz/schema';

// All XVIZ messages
export const XVIZ_PROTOBUF_MESSAGE_NAME = {
ENVELOPE: 'xviz.v2.Envelope',
START: 'xviz.v2.Start',
TRANSFORM_LOG: 'xviz.v2.TransformLog',
TRANSFORM_LOG_POINT_IN_TIME: 'xviz.v2.TransformPointInTime',
TRANSFORM_LOG_DONE: 'xviz.v2.TransformLogDone',
STATE_UPDATE: 'xviz.v2.StateUpdate',
RECONFIGURE: 'xviz.v2.Reconfigure',
METADATA: 'xviz.v2.Metadata',
ERROR: 'xviz.v2.Error'
};

// PBE1
export const MAGIC_PBE1 = 0x50424531;
export const XVIZ_PROTOBUF_MAGIC = Uint8Array.from([0x50, 0x42, 0x45, 0x31]);

export const XVIZ_PROTOBUF_ROOT = loadProtos();

export const XVIZ_PROTOBUF_MESSAGE = {
Envelope: XVIZ_PROTOBUF_ROOT.lookupType(XVIZ_PROTOBUF_MESSAGE_NAME.ENVELOPE),
Metadata: XVIZ_PROTOBUF_ROOT.lookupType(XVIZ_PROTOBUF_MESSAGE_NAME.METADATA),
StateUpdate: XVIZ_PROTOBUF_ROOT.lookupType(XVIZ_PROTOBUF_MESSAGE_NAME.STATE_UPDATE)
};
11 changes: 10 additions & 1 deletion modules/io/src/common/xviz-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
parseBinaryXVIZ,
isGLBXVIZ,
isJSONString,
isPBEXVIZ,
getXVIZMessageType
} from './loaders';
import {XVIZMessage} from './xviz-message';
Expand Down Expand Up @@ -100,6 +101,12 @@ export class XVIZData {
}
msg = parseBinaryXVIZ(data);
break;
case XVIZ_FORMAT.BINARY_PBE:
if (data instanceof Buffer) {
data = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
}
msg = parseBinaryXVIZ(data);
break;
case XVIZ_FORMAT.JSON_BUFFER:
let jsonString = null;
if (data instanceof Buffer) {
Expand Down Expand Up @@ -141,7 +148,9 @@ export class XVIZData {
data = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
}

if (isGLBXVIZ(data)) {
if (isPBEXVIZ(data)) {
this._dataFormat = XVIZ_FORMAT.BINARY_PBE;
} else if (isGLBXVIZ(data)) {
this._dataFormat = XVIZ_FORMAT.BINARY_GLB;
} else {
if (data instanceof ArrayBuffer) {
Expand Down
7 changes: 7 additions & 0 deletions modules/io/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
// limitations under the License.
export {XVIZJSONWriter} from './writers/xviz-json-writer';
export {XVIZBinaryWriter, encodeBinaryXVIZ} from './writers/xviz-binary-writer';
export {XVIZProtobufWriter} from './writers/xviz-protobuf-writer';
export {XVIZFormatWriter} from './writers/xviz-format-writer';

export {XVIZJSONReader} from './readers/xviz-json-reader';
export {XVIZBinaryReader} from './readers/xviz-binary-reader';
export {XVIZProtobufReader} from './readers/xviz-protobuf-reader';

export {MemorySourceSink} from './io/memory-source-sink';

Expand All @@ -35,12 +37,17 @@ export {
isBinaryXVIZ,
parseBinaryXVIZ,
isGLBXVIZ,
isPBEXVIZ,
parsePBEXVIZ,
isJSONString,
getObjectXVIZType,
getXVIZMessageType,
isXVIZMessage
} from './common/loaders';

export {XVIZ_PROTOBUF_MESSAGE} from './common/protobuf-support';

export {XVIZProviderFactory} from './providers/index';
export {XVIZJSONProvider} from './providers/xviz-json-provider';
export {XVIZBinaryProvider} from './providers/xviz-binary-provider';
export {XVIZProtobufProvider} from './providers/xviz-protobuf-provider';
7 changes: 6 additions & 1 deletion modules/io/src/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
import {XVIZJSONProvider} from './xviz-json-provider';
import {XVIZBinaryProvider} from './xviz-binary-provider';
import {XVIZProtobufProvider} from './xviz-protobuf-provider';

async function createXVIZProvider(ProviderClass, args) {
let provider = null;
Expand All @@ -28,7 +29,11 @@ async function createXVIZProvider(ProviderClass, args) {

export class XVIZProviderFactoryClass {
constructor() {
this.providerClasses = [{className: XVIZJSONProvider}, {className: XVIZBinaryProvider}];
this.providerClasses = [
{className: XVIZJSONProvider},
{className: XVIZBinaryProvider},
{className: XVIZProtobufProvider}
];
}

addProviderClass(className, args) {
Expand Down
21 changes: 21 additions & 0 deletions modules/io/src/providers/xviz-protobuf-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2019 Uber Technologies, 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.
import {XVIZProtobufReader} from '@xviz/io';
import {XVIZBaseProvider} from './xviz-base-provider';

export class XVIZProtobufProvider extends XVIZBaseProvider {
constructor(params) {
super({...params, reader: new XVIZProtobufReader(params.source, params.options)});
}
}
20 changes: 20 additions & 0 deletions modules/io/src/readers/xviz-protobuf-reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2019 Uber Technologies, 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.
import {XVIZBaseReader} from './xviz-base-reader';

export class XVIZProtobufReader extends XVIZBaseReader {
constructor(source, options = {}) {
super(source, {...options, suffix: '-frame.pbe'});
}
}
4 changes: 4 additions & 0 deletions modules/io/src/writers/xviz-format-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import {XVIZBinaryWriter} from '../writers/xviz-binary-writer';
import {XVIZProtobufWriter} from '../writers/xviz-protobuf-writer';
import {XVIZJSONWriter} from '../writers/xviz-json-writer';
import {XVIZ_FORMAT} from '../common/constants';

Expand All @@ -28,6 +29,9 @@ function determineWriter(sink, format, options) {
case XVIZ_FORMAT.BINARY_GLB:
writer = new XVIZBinaryWriter(sink, options);
break;
case XVIZ_FORMAT.BINARY_PBE:
writer = new XVIZProtobufWriter(sink, options);
break;
case XVIZ_FORMAT.JSON_BUFFER:
writer = new XVIZJSONBufferWriter(sink, options);
break;
Expand Down
Loading