MCPcopy Create free account
hub / github.com/apache/brpc / EncodePES

Method EncodePES

src/brpc/ts.cpp:1404–1475  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1402}
1403
1404butil::Status TsWriter::EncodePES(TsMessage* msg, TsStream sid, TsPid pid,
1405 bool pure_audio) {
1406 if (msg->payload.empty()) {
1407 return butil::Status::OK();
1408 }
1409 if (sid != TS_STREAM_VIDEO_H264 &&
1410 sid != TS_STREAM_AUDIO_MP3 &&
1411 sid != TS_STREAM_AUDIO_AAC) {
1412 LOG(WARNING) << "Ignore unknown stream_id=" << sid;
1413 return butil::Status::OK();
1414 }
1415
1416 TsChannel* channel = _tschan_group.get(pid);
1417 if (channel == NULL) {
1418 return butil::Status(EINVAL, "Fail to get channel on pid=%d", (int)pid);
1419 }
1420
1421 bool first_msg = true;
1422 while (!msg->payload.empty()) {
1423 TsPacket pkt(&_tschan_group);
1424 if (first_msg) {
1425 first_msg = false;
1426 bool write_pcr = msg->write_pcr;
1427 // for pure audio, always write pcr.
1428 // TODO: maybe only need to write at begin and end of ts.
1429 if (pure_audio && is_audio(msg->sid)) {
1430 write_pcr = true;
1431 }
1432
1433 // it's ok to set pcr equals to dts,
1434 int64_t pcr = write_pcr ? msg->dts : -1;
1435
1436 // TODO: FIXME: figure out why use discontinuity of msg
1437 pkt.CreateAsPESFirst(pid, msg->sid, channel->continuity_counter++,
1438 msg->is_discontinuity, pcr, msg->dts,
1439 msg->pts, msg->payload.size());
1440 } else {
1441 pkt.CreateAsPESContinue(pid, channel->continuity_counter++);
1442 }
1443
1444 char buf[TS_PACKET_SIZE];
1445
1446 // set the left bytes with 0xFF.
1447 size_t pkt_size = pkt.ByteSize();
1448 CHECK_LT(pkt_size, TS_PACKET_SIZE);
1449
1450 size_t left = std::min(msg->payload.size(), TS_PACKET_SIZE - pkt_size);
1451 const size_t nb_stuffings = TS_PACKET_SIZE - pkt_size - left;
1452 if (nb_stuffings > 0) {
1453 // set all bytes to stuffings.
1454 memset(buf, 0xFF, TS_PACKET_SIZE);
1455
1456 pkt.AddPadding(nb_stuffings);
1457
1458 pkt_size = pkt.ByteSize(); // size changed, recalculate.
1459 CHECK_LT(pkt_size, TS_PACKET_SIZE);
1460
1461 left = std::min(msg->payload.size(), TS_PACKET_SIZE - pkt_size);

Callers

nothing calls this directly

Calls 12

is_audioFunction · 0.85
CreateAsPESFirstMethod · 0.80
CreateAsPESContinueMethod · 0.80
AddPaddingMethod · 0.80
StatusEnum · 0.70
emptyMethod · 0.45
getMethod · 0.45
sizeMethod · 0.45
ByteSizeMethod · 0.45
cutnMethod · 0.45
EncodeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected