rtci-rtcp 0.1.0

RTCP packet parsing and serialization (RFC 3550, RFC 4585, RFC 5104, RFC 3611, RFC 8888) for WebRTC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#[cfg(test)]
mod source_description_items_test;
#[cfg(test)]
mod source_description_test;

use crate::{header::*, packet::*, util::*};
use shared::{
    error::{Error, Result},
    marshal::{Marshal, MarshalSize, Unmarshal},
};

use bytes::{Buf, BufMut, Bytes};
use std::any::Any;
use std::fmt;

const SDES_SOURCE_LEN: usize = 4;
const SDES_TYPE_LEN: usize = 1;
const SDES_TYPE_OFFSET: usize = 0;
const SDES_OCTET_COUNT_LEN: usize = 1;
const SDES_OCTET_COUNT_OFFSET: usize = 1;
const SDES_MAX_OCTET_COUNT: usize = (1 << 8) - 1;
const SDES_TEXT_OFFSET: usize = 2;

/// SDESType is the item type used in the RTCP SDES control packet.
/// RTP SDES item types registered with IANA. See: <https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-5>
///
/// The enum converts to and from the type octet with `u8::from` / `SdesType::from`. The
/// registry grows, so an item of a type that is not named here is kept as
/// [`SdesType::Other`]: a receiver skips such an item by its length (RFC 3550 §6.5).
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SdesType {
    #[default]
    /// End of the SDES item list ([RFC 3550] §6.5).
    SdesEnd, // 0, end of SDES list                RFC 3550, 6.5
    /// CNAME: the canonical end-point identifier, which ties an SSRC to a participant.
    ///
    /// The one item WebRTC always sends — it is how a receiver associates streams that belong
    /// together.
    SdesCname, // 1, canonical name                  RFC 3550, 6.5.1
    /// NAME: the participant's display name.
    SdesName, // 2, user name                       RFC 3550, 6.5.2
    /// EMAIL: the participant's email address.
    SdesEmail, // 3, user's electronic mail address  RFC 3550, 6.5.3
    /// PHONE: the participant's phone number.
    SdesPhone, // 4, user's phone number             RFC 3550, 6.5.4
    /// LOC: the participant's geographic location.
    SdesLocation, // 5, geographic user location        RFC 3550, 6.5.5
    /// TOOL: the name and version of the sending application.
    SdesTool, // 6, name of application or tool     RFC 3550, 6.5.6
    /// NOTE: a transient note about the source, such as "on hold".
    SdesNote, // 7, notice about the source         RFC 3550, 6.5.7
    /// PRIV: a private extension: a prefix that names it, and a value
    /// ([`SourceDescriptionItem::private`]).
    SdesPrivate, // 8, private extensions              RFC 3550, 6.5.8
    /// RtpStreamId: the RID of the source stream ([RFC 8852] §3.1), ASCII, not null terminated.
    ///
    /// [RFC 8852]: https://datatracker.ietf.org/doc/html/rfc8852
    SdesRtpStreamId, // 12, RFC 8852, 3.1
    /// RepairedRtpStreamId: on a redundancy stream, the RID of the source stream it repairs
    /// ([RFC 8852] §3.2).
    ///
    /// [RFC 8852]: https://datatracker.ietf.org/doc/html/rfc8852
    SdesRepairedRtpStreamId, // 13, RFC 8852, 3.2
    /// MID: the media identification that ties the source to an SDP media section
    /// ([RFC 8843] §15.1, carried as [RFC 7941] describes).
    ///
    /// [RFC 8843]: https://datatracker.ietf.org/doc/html/rfc8843
    /// [RFC 7941]: https://datatracker.ietf.org/doc/html/rfc7941
    SdesMid, // 15, RFC 8843, 15.1
    /// An item type that is not named above, with its type octet.
    Other(u8),
}

impl fmt::Display for SdesType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            SdesType::SdesEnd => "END",
            SdesType::SdesCname => "CNAME",
            SdesType::SdesName => "NAME",
            SdesType::SdesEmail => "EMAIL",
            SdesType::SdesPhone => "PHONE",
            SdesType::SdesLocation => "LOC",
            SdesType::SdesTool => "TOOL",
            SdesType::SdesNote => "NOTE",
            SdesType::SdesPrivate => "PRIV",
            SdesType::SdesRtpStreamId => "RtpStreamId",
            SdesType::SdesRepairedRtpStreamId => "RepairedRtpStreamId",
            SdesType::SdesMid => "MID",
            SdesType::Other(t) => return write!(f, "SDES{t}"),
        };
        write!(f, "{s}")
    }
}

impl From<u8> for SdesType {
    fn from(b: u8) -> Self {
        match b {
            0 => SdesType::SdesEnd,
            1 => SdesType::SdesCname,
            2 => SdesType::SdesName,
            3 => SdesType::SdesEmail,
            4 => SdesType::SdesPhone,
            5 => SdesType::SdesLocation,
            6 => SdesType::SdesTool,
            7 => SdesType::SdesNote,
            8 => SdesType::SdesPrivate,
            12 => SdesType::SdesRtpStreamId,
            13 => SdesType::SdesRepairedRtpStreamId,
            15 => SdesType::SdesMid,
            other => SdesType::Other(other),
        }
    }
}

impl From<SdesType> for u8 {
    fn from(t: SdesType) -> Self {
        match t {
            SdesType::SdesEnd => 0,
            SdesType::SdesCname => 1,
            SdesType::SdesName => 2,
            SdesType::SdesEmail => 3,
            SdesType::SdesPhone => 4,
            SdesType::SdesLocation => 5,
            SdesType::SdesTool => 6,
            SdesType::SdesNote => 7,
            SdesType::SdesPrivate => 8,
            SdesType::SdesRtpStreamId => 12,
            SdesType::SdesRepairedRtpStreamId => 13,
            SdesType::SdesMid => 15,
            SdesType::Other(other) => other,
        }
    }
}

/// A SourceDescriptionChunk contains items describing a single RTP source
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct SourceDescriptionChunk {
    /// The source (ssrc) or contributing source (csrc) identifier this packet describes
    pub source: u32,
    /// The items describing this source.
    pub items: Vec<SourceDescriptionItem>,
}

impl SourceDescriptionChunk {
    fn raw_size(&self) -> usize {
        let mut len = SDES_SOURCE_LEN;
        for it in &self.items {
            len += it.marshal_size();
        }
        len += SDES_TYPE_LEN; // for terminating null octet
        len
    }
}

impl MarshalSize for SourceDescriptionChunk {
    fn marshal_size(&self) -> usize {
        let l = self.raw_size();
        // align to 32-bit boundary
        l + get_padding_size(l)
    }
}

impl Marshal for SourceDescriptionChunk {
    /// Marshal encodes the SourceDescriptionChunk in binary
    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
        if buf.remaining_mut() < self.marshal_size() {
            return Err(Error::BufferTooShort);
        }
        /*
         *  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         *  |                          SSRC/CSRC_1                          |
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |                           SDES items                          |
         *  |                              ...                              |
         *  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         */

        buf.put_u32(self.source);

        for it in &self.items {
            let n = it.marshal_to(buf)?;
            buf = &mut buf[n..];
        }

        // The list of items in each chunk MUST be terminated by one or more null octets
        buf.put_u8(SdesType::SdesEnd.into());

        // additional null octets MUST be included if needed to pad until the next 32-bit boundary
        // (null octets: this is not the padding of the P bit, whose last octet is a count)
        buf.put_bytes(0, get_padding_size(self.raw_size()));
        Ok(self.marshal_size())
    }
}

impl Unmarshal for SourceDescriptionChunk {
    /// Unmarshal decodes the SourceDescriptionChunk from binary
    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
    where
        Self: Sized,
        B: Buf,
    {
        /*
         *  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         *  |                          SSRC/CSRC_1                          |
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |                           SDES items                          |
         *  |                              ...                              |
         *  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         */
        if raw_packet.remaining() < (SDES_SOURCE_LEN + SDES_TYPE_LEN) {
            return Err(Error::PacketTooShort);
        }

        let source = raw_packet.get_u32();

        // Octets of this chunk read so far. A chunk starts on a 32-bit boundary, so this also
        // says how far the next boundary is.
        let mut offset = SDES_SOURCE_LEN;
        let mut items = vec![];
        loop {
            // "The list of items in each chunk MUST be terminated by one or more null octets":
            // a chunk that runs out without one is cut short.
            let item = SourceDescriptionItem::unmarshal(raw_packet)?;
            if item.sdes_type == SdesType::SdesEnd {
                // offset + 1 (one byte for SdesEnd), then null octets up to the boundary.
                let padding_len = get_padding_size(offset + 1);
                if raw_packet.remaining() < padding_len {
                    return Err(Error::PacketTooShort);
                }
                raw_packet.advance(padding_len);
                return Ok(SourceDescriptionChunk { source, items });
            }
            // Every item is kept, of a known type or not: an unknown one is skipped by its
            // length here, and it is the reader of `items` that passes over it.
            offset += item.marshal_size();
            items.push(item);
        }
    }
}

/// A SourceDescriptionItem is a part of a SourceDescription that describes a stream.
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct SourceDescriptionItem {
    /// The type identifier for this item. eg, SDESCNAME for canonical name description.
    ///
    /// Type zero or SDESEnd is interpreted as the end of an item list and cannot be used.
    pub sdes_type: SdesType,
    /// Text is a unicode text blob associated with the item. Its meaning varies based on the item's Type.
    pub text: Bytes,
}

impl SourceDescriptionItem {
    /// A PRIV item (RFC 3550 §6.5.8): `prefix` names the extension, `value` is its content.
    ///
    /// The two share the item's 255 octets with the prefix length octet, so together they are
    /// at most 254 octets long.
    pub fn private(prefix: &[u8], value: &[u8]) -> Result<Self> {
        if 1 + prefix.len() + value.len() > SDES_MAX_OCTET_COUNT {
            return Err(Error::SdesTextTooLong);
        }
        let mut text = Vec::with_capacity(1 + prefix.len() + value.len());
        text.push(prefix.len() as u8);
        text.extend_from_slice(prefix);
        text.extend_from_slice(value);
        Ok(SourceDescriptionItem {
            sdes_type: SdesType::SdesPrivate,
            text: Bytes::from(text),
        })
    }

    /// The prefix and the value of a PRIV item, or `None` when this is not one or its prefix
    /// length runs past the item.
    pub fn as_private(&self) -> Option<(Bytes, Bytes)> {
        if self.sdes_type != SdesType::SdesPrivate {
            return None;
        }
        let prefix_len = *self.text.first()? as usize;
        if 1 + prefix_len > self.text.len() {
            return None;
        }
        Some((
            self.text.slice(1..1 + prefix_len),
            self.text.slice(1 + prefix_len..),
        ))
    }
}

impl MarshalSize for SourceDescriptionItem {
    fn marshal_size(&self) -> usize {
        /*
         *   0                   1                   2                   3
         *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |    CNAME=1    |     length    | user and domain name        ...
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         */
        SDES_TYPE_LEN + SDES_OCTET_COUNT_LEN + self.text.len()
    }
}

impl Marshal for SourceDescriptionItem {
    /// Marshal encodes the SourceDescriptionItem in binary
    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
        /*
         *   0                   1                   2                   3
         *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |    CNAME=1    |     length    | user and domain name        ...
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         */

        // Type zero ends the list and cannot be an item, whichever way it is spelled.
        let sdes_type = u8::from(self.sdes_type);
        if sdes_type == 0 {
            return Err(Error::SdesMissingType);
        }

        if buf.remaining_mut() < self.marshal_size() {
            return Err(Error::BufferTooShort);
        }

        buf.put_u8(sdes_type);

        if self.text.len() > SDES_MAX_OCTET_COUNT {
            return Err(Error::SdesTextTooLong);
        }
        buf.put_u8(self.text.len() as u8);
        buf.put(self.text.clone());

        //no padding for each SourceDescriptionItem
        Ok(self.marshal_size())
    }
}

impl Unmarshal for SourceDescriptionItem {
    /// Unmarshal decodes the SourceDescriptionItem from binary
    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
    where
        Self: Sized,
        B: Buf,
    {
        /*
         *   0                   1                   2                   3
         *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |    CNAME=1    |     length    | user and domain name        ...
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         */
        let raw_packet_len = raw_packet.remaining();
        if raw_packet_len < SDES_TYPE_LEN {
            return Err(Error::PacketTooShort);
        }

        let sdes_type = SdesType::from(raw_packet.get_u8());
        if sdes_type == SdesType::SdesEnd {
            return Ok(SourceDescriptionItem {
                sdes_type,
                text: Bytes::new(),
            });
        }

        if raw_packet_len < (SDES_TYPE_LEN + SDES_OCTET_COUNT_LEN) {
            return Err(Error::PacketTooShort);
        }

        let octet_count = raw_packet.get_u8() as usize;
        if SDES_TEXT_OFFSET + octet_count > raw_packet_len {
            return Err(Error::PacketTooShort);
        }

        let text = raw_packet.copy_to_bytes(octet_count);

        Ok(SourceDescriptionItem { sdes_type, text })
    }
}

/// A SourceDescription (SDES) packet describes the sources in an RTP stream.
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct SourceDescription {
    /// One chunk per source described by this packet.
    pub chunks: Vec<SourceDescriptionChunk>,
}

impl fmt::Display for SourceDescription {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut out = "Source Description:\n".to_string();
        for c in &self.chunks {
            out += format!("\t{:x}\n", c.source).as_str();
            for it in &c.items {
                out += format!("\t\t{it:?}\n").as_str();
            }
        }
        write!(f, "{out}")
    }
}

impl Packet for SourceDescription {
    /// Header returns the Header associated with this packet.
    fn header(&self) -> Header {
        Header {
            // Every chunk ends on a 32-bit boundary, so the packet does too.
            padding: false,
            count: self.chunks.len() as u8,
            packet_type: PacketType::SourceDescription,
            length: ((self.marshal_size() / 4) - 1) as u16,
        }
    }

    /// destination_ssrc returns an array of SSRC values that this packet refers to.
    fn destination_ssrc(&self) -> Vec<u32> {
        self.chunks.iter().map(|x| x.source).collect()
    }

    fn raw_size(&self) -> usize {
        let mut chunks_length = 0;
        for c in &self.chunks {
            chunks_length += c.marshal_size();
        }

        HEADER_LENGTH + chunks_length
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn equal(&self, other: &dyn Packet) -> bool {
        other.as_any().downcast_ref::<SourceDescription>() == Some(self)
    }

    fn cloned(&self) -> Box<dyn Packet> {
        Box::new(self.clone())
    }
}

impl MarshalSize for SourceDescription {
    fn marshal_size(&self) -> usize {
        let l = self.raw_size();
        // align to 32-bit boundary
        l + get_padding_size(l)
    }
}

impl Marshal for SourceDescription {
    /// Marshal encodes the SourceDescription in binary
    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
        if self.chunks.len() > COUNT_MAX {
            return Err(Error::TooManyChunks);
        }

        if buf.remaining_mut() < self.marshal_size() {
            return Err(Error::BufferTooShort);
        }

        /*
         *         0                   1                   2                   3
         *         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         * header |V=2|P|    SC   |  PT=SDES=202  |             length            |
         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         * chunk  |                          SSRC/CSRC_1                          |
         *   1    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *        |                           SDES items                          |
         *        |                              ...                              |
         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         * chunk  |                          SSRC/CSRC_2                          |
         *   2    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *        |                           SDES items                          |
         *        |                              ...                              |
         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         */

        let h = self.header();
        let n = h.marshal_to(buf)?;
        buf = &mut buf[n..];

        for c in &self.chunks {
            let n = c.marshal_to(buf)?;
            buf = &mut buf[n..];
        }

        if h.padding {
            put_padding(buf, self.raw_size());
        }

        Ok(self.marshal_size())
    }
}

impl Unmarshal for SourceDescription {
    /// Unmarshal decodes the SourceDescription from binary
    fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
    where
        Self: Sized,
        B: Buf,
    {
        /*
         *         0                   1                   2                   3
         *         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
         *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         * header |V=2|P|    SC   |  PT=SDES=202  |             length            |
         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         * chunk  |                          SSRC/CSRC_1                          |
         *   1    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *        |                           SDES items                          |
         *        |                              ...                              |
         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         * chunk  |                          SSRC/CSRC_2                          |
         *   2    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *        |                           SDES items                          |
         *        |                              ...                              |
         *        +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
         */
        let (h, mut body) = read_packet(raw_packet)?;
        if h.packet_type != PacketType::SourceDescription {
            return Err(Error::WrongType);
        }

        // SC chunks, each ending on a 32-bit boundary, and nothing after them.
        let mut chunks = Vec::with_capacity(h.count as usize);
        for _ in 0..h.count {
            chunks.push(SourceDescriptionChunk::unmarshal(&mut body)?);
        }
        if body.has_remaining() {
            return Err(Error::InvalidHeader);
        }

        Ok(SourceDescription { chunks })
    }
}