---
sidebar:
  hidden: true
title: roboto.formats.mcap.reader
---
## Module Contents

### END_OF_STREAM

```python
roboto.formats.mcap.reader.END_OF_STREAM
```

`from roboto.formats.mcap import END_OF_STREAM`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/mcap/reader.py#L41-L41)

Value `McapReader.next_decoded` returns in place of a message once the reader has no more messages.

A message can decode to `None`, such as a JSON `null` payload, so `None` cannot mark the end.

### McapEnvelopeTimestamp

```python
class roboto.formats.mcap.reader.McapEnvelopeTimestamp
```

`from roboto.formats.mcap.reader import McapEnvelopeTimestamp`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/mcap/reader.py#L48-L55)

Bases: `NamedTuple`

Log time and publish time of an MCAP message, as `McapReader.next_envelope_timestamp` returns them.

**Attributes**

- **McapEnvelopeTimestamp.log_time** (`int | float`): Time the message was recorded, in nanoseconds.
- **McapEnvelopeTimestamp.publish_time** (`int | float`): Time the message's producer published it, in nanoseconds.

### McapReader

```python
class roboto.formats.mcap.reader.McapReader(
    stream: IO[bytes],
    fields: collections.abc.Sequence[roboto.formats.fields.FieldSelection],
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
    log_time_order: bool = True,
    topic_name: Optional[str] = None,
)
```

`from roboto.formats.mcap import McapReader`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/mcap/reader.py#L58-L207)

Reader that decodes the messages of an MCAP stream and keeps only selected fields.

Reads the messages whose log time falls in a time range and, optionally, that are on one topic. Decodes messages encoded as JSON, msgpack, ROS 1, or CDR (ROS 2 and OMG IDL schemas), and skips a ROS 1 or CDR message it cannot decode.

**Parameters**

- **stream** (`IO[bytes]`): Binary stream of the MCAP data to read.
- **fields** (`collections.abc.Sequence[roboto.formats.fields.FieldSelection]`): Fields to keep in each message `next` returns.
- **start_time** (`Optional[int]`): Inclusive start of the log-time range, in nanoseconds. `None` starts at the first message.
- **end_time** (`Optional[int]`): Exclusive end of the log-time range, in nanoseconds. `None` reads to the last message.
- **log_time_order** (`bool`): Whether to return messages in log-time order. `False` returns them in the order the file stores them, which avoids sorting messages across chunks, and still applies the time range. Pass `False` only when message order does not matter; code that merges readers by log time needs `True`.
- **topic_name** (`Optional[str]`): Topic to read the messages of. `None` reads every topic. Messages on every MCAP channel of the topic are read.

**Raises**

- [`RobotoInternalException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoInternalException): `topic_name` is given and the file's summary section lists no channel on that topic. A stream that cannot seek, or a file with no summary section, is not checked.

**Properties**

- **McapReader.field_paths** (`list[tuple[str, ...]]`): Path through the schema of each selected field, in the order of `fields`.

  Each path is the field's [`path_in_schema`](/docs/reference/python-sdk/roboto/formats/fields#roboto.formats.fields.FieldSelection.path_in_schema).

- **McapReader.has_next** (`bool`): Whether the reader has another message to return.

- **McapReader.next_envelope_timestamp** (`McapEnvelopeTimestamp`): Log time and publish time of the next message, both `math.inf` once the reader has no more messages.

  Since `math.inf` is greater than every timestamp, code that merges readers by taking the smallest next log time never picks an exhausted reader.

#### McapReader.next()

```python
def next() -> Union[roboto.formats.mcap.decoded_message.DecodedMessage, None]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/mcap/reader.py#L142-L166)

Return the next message, keeping only the selected fields, and advance the reader.

**Returns**

- `Union[roboto.formats.mcap.decoded_message.DecodedMessage, None]`: The next message, or `None` once the reader has no more messages.

**Usage**

```python
from roboto.formats import FieldSelection
from roboto.formats.mcap import McapReader
with open("data.mcap", "rb") as f:
    reader = McapReader(f, [FieldSelection(("position", "x"))], topic_name="/pose")
    while reader.has_next:
        message = reader.next()
        print(message.to_dict())
```

#### McapReader.next_decoded()

```python
def next_decoded() -> Any
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/mcap/reader.py#L168-L182)

Return the next message as its decoder produced it, with every field, and advance the reader.

The value is nested dictionaries, sequences, and scalars. To keep only the selected fields, use `next` and [`to_dict()`](/docs/reference/python-sdk/roboto/formats/mcap/decoded_message#roboto.formats.mcap.decoded_message.DecodedMessage.to_dict) instead.

**Returns**

- `Any`: The decoded message, or `END_OF_STREAM` once the reader has no more messages. A message can decode to `None`, so test `result is END_OF_STREAM`, not `result is None`, to detect the end.

#### McapReader.next_message_is_time_aligned()

```python
def next_message_is_time_aligned(timestamp: Union[int, float]) -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/mcap/reader.py#L184-L197)

Return whether the next message's log time equals `timestamp`.

Code that merges several readers by log time uses it to find the readers whose next message has the earliest log time. Returns `False` once the reader has no more messages.

**Parameters**

- **timestamp** (`Union[int, float]`): Log time to compare with, in nanoseconds.

**Returns**

- `bool`
