summaryrefslogtreecommitdiffstats
path: root/media/formats/mp2t/ts_packet.h
blob: a232705fbd5596fce613fae13241a1bb3ac920af (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_FORMATS_MP2T_TS_PACKET_H_
#define MEDIA_FORMATS_MP2T_TS_PACKET_H_

#include "base/basictypes.h"

namespace media {

class BitReader;

namespace mp2t {

class TsPacket {
 public:
  static const int kPacketSize = 188;

  // Return the number of bytes to discard
  // to be synchronized on a TS syncword.
  static int Sync(const uint8* buf, int size);

  // Parse a TS packet.
  // Return a TsPacket only when parsing was successful.
  // Return NULL otherwise.
  static TsPacket* Parse(const uint8* buf, int size);

  ~TsPacket();

  // TS header accessors.
  bool payload_unit_start_indicator() const {
    return payload_unit_start_indicator_;
  }
  int pid() const { return pid_; }
  int continuity_counter() const { return continuity_counter_; }
  bool discontinuity_indicator() const { return discontinuity_indicator_; }
  bool random_access_indicator() const { return random_access_indicator_; }

  // Return the offset and the size of the payload.
  const uint8* payload() const { return payload_; }
  int payload_size() const { return payload_size_; }

 private:
  TsPacket();

  // Parse an Mpeg2 TS header.
  // The buffer size should be at least |kPacketSize|
  bool ParseHeader(const uint8* buf);
  bool ParseAdaptationField(BitReader* bit_reader,
                            int adaptation_field_length);

  // Size of the payload.
  const uint8* payload_;
  int payload_size_;

  // TS header.
  bool payload_unit_start_indicator_;
  int pid_;
  int continuity_counter_;

  // Params from the adaptation field.
  bool discontinuity_indicator_;
  bool random_access_indicator_;

  DISALLOW_COPY_AND_ASSIGN(TsPacket);
};

}  // namespace mp2t
}  // namespace media

#endif