(w io.Writer, pt uint32, psi MpegTsPSI, data []byte)
| 141 | } |
| 142 | |
| 143 | func WritePSI(w io.Writer, pt uint32, psi MpegTsPSI, data []byte) (err error) { |
| 144 | var tableId, versionNumberAndCurrentNextIndicator, sectionNumber, lastSectionNumber uint8 |
| 145 | var sectionSyntaxIndicatorAndSectionLength, transportStreamIdOrProgramNumber uint16 |
| 146 | |
| 147 | switch pt { |
| 148 | case PSI_TYPE_PAT: |
| 149 | { |
| 150 | if psi.Pat.TableID != TABLE_PAS { |
| 151 | err = errors.New(fmt.Sprintf("%s, id=%d", "write pmt table id != 0", tableId)) |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | tableId = psi.Pat.TableID |
| 156 | sectionSyntaxIndicatorAndSectionLength = uint16(psi.Pat.SectionSyntaxIndicator)<<15 | 3<<12 | psi.Pat.SectionLength |
| 157 | transportStreamIdOrProgramNumber = psi.Pat.TransportStreamID |
| 158 | versionNumberAndCurrentNextIndicator = psi.Pat.VersionNumber<<1 | psi.Pat.CurrentNextIndicator |
| 159 | sectionNumber = psi.Pat.SectionNumber |
| 160 | lastSectionNumber = psi.Pat.LastSectionNumber |
| 161 | } |
| 162 | case PSI_TYPE_PMT: |
| 163 | { |
| 164 | if psi.Pmt.TableID != TABLE_TSPMS { |
| 165 | err = errors.New(fmt.Sprintf("%s, id=%d", "write pmt table id != 2", tableId)) |
| 166 | return |
| 167 | } |
| 168 | |
| 169 | tableId = psi.Pmt.TableID |
| 170 | sectionSyntaxIndicatorAndSectionLength = uint16(psi.Pmt.SectionSyntaxIndicator)<<15 | 3<<12 | psi.Pmt.SectionLength |
| 171 | transportStreamIdOrProgramNumber = psi.Pmt.ProgramNumber |
| 172 | versionNumberAndCurrentNextIndicator = psi.Pmt.VersionNumber<<1 | psi.Pmt.CurrentNextIndicator |
| 173 | sectionNumber = psi.Pmt.SectionNumber |
| 174 | lastSectionNumber = psi.Pmt.LastSectionNumber |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // pointer field(8) |
| 179 | if err = util.WriteUint8ToByte(w, 0); err != nil { |
| 180 | return |
| 181 | } |
| 182 | |
| 183 | cw := &util.Crc32Writer{W: w, Crc32: 0xffffffff} |
| 184 | |
| 185 | // table id(8) |
| 186 | if err = util.WriteUint8ToByte(cw, tableId); err != nil { |
| 187 | return |
| 188 | } |
| 189 | |
| 190 | // sectionSyntaxIndicator(1) + zero(1) + reserved1(2) + sectionLength(12) |
| 191 | // sectionLength 前两个字节固定为00 |
| 192 | // 1 0 11 sectionLength |
| 193 | if err = util.WriteUint16ToByte(cw, sectionSyntaxIndicatorAndSectionLength, true); err != nil { |
| 194 | return |
| 195 | } |
| 196 | |
| 197 | // PAT TransportStreamID(16) or PMT ProgramNumber(16) |
| 198 | if err = util.WriteUint16ToByte(cw, transportStreamIdOrProgramNumber, true); err != nil { |
| 199 | return |
| 200 | } |
no test coverage detected