| 306 | } |
| 307 | |
| 308 | int TsPacket::Encode(void* data) const { |
| 309 | char* p = (char*)data; |
| 310 | |
| 311 | policy::Write1Byte(&p, TS_SYNC_BYTE); |
| 312 | |
| 313 | int16_t pidv = _pid & 0x1FFF; |
| 314 | pidv |= (_transport_priority << 13) & 0x2000; |
| 315 | pidv |= (_transport_error_indicator << 15) & 0x8000; |
| 316 | pidv |= (_payload_unit_start_indicator << 14) & 0x4000; |
| 317 | policy::WriteBigEndian2Bytes(&p, pidv); |
| 318 | |
| 319 | int8_t ccv = _continuity_counter & 0x0F; |
| 320 | ccv |= (_transport_scrambling_control << 6) & 0xC0; |
| 321 | TsAdaptationFieldType af_control = _adaptation_field_control; |
| 322 | if (af_control == TS_AF_RESERVED) { |
| 323 | // In the case of a null packet, af_control shall be set to '01'. |
| 324 | af_control = TS_AF_PAYLOAD_ONLY; |
| 325 | } |
| 326 | ccv |= (af_control << 4) & 0x30; |
| 327 | policy::Write1Byte(&p, ccv); |
| 328 | |
| 329 | if (_adaptation_field) { |
| 330 | if (_adaptation_field->Encode(p, af_control) != 0) { |
| 331 | LOG(ERROR) << "Fail to encode _adaptation_field"; |
| 332 | return -1; |
| 333 | } |
| 334 | p += _adaptation_field->ByteSize(); |
| 335 | } |
| 336 | |
| 337 | if (_payload) { |
| 338 | if (_payload->Encode(p) != 0) { |
| 339 | LOG(ERROR) << "Fail to encode _payload"; |
| 340 | return -1; |
| 341 | } |
| 342 | p += _payload->ByteSize(); |
| 343 | } |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | void TsPacket::AddPadding(size_t num_stuffings) { |
| 348 | const bool no_af_before = (_adaptation_field == NULL); |