winlin

implements the pat/pmt write ts header.

@@ -517,6 +517,8 @@ ISrsTsHandler::~ISrsTsHandler() @@ -517,6 +517,8 @@ ISrsTsHandler::~ISrsTsHandler()
517 517
518 SrsTsContext::SrsTsContext() 518 SrsTsContext::SrsTsContext()
519 { 519 {
  520 + vcodec = SrsCodecVideoReserved;
  521 + acodec = SrsCodecAudioReserved1;
520 } 522 }
521 523
522 SrsTsContext::~SrsTsContext() 524 SrsTsContext::~SrsTsContext()
@@ -529,6 +531,30 @@ SrsTsContext::~SrsTsContext() @@ -529,6 +531,30 @@ SrsTsContext::~SrsTsContext()
529 pids.clear(); 531 pids.clear();
530 } 532 }
531 533
  534 +SrsTsChannel* SrsTsContext::get(int pid)
  535 +{
  536 + if (pids.find(pid) == pids.end()) {
  537 + return NULL;
  538 + }
  539 + return pids[pid];
  540 +}
  541 +
  542 +void SrsTsContext::set(int pid, SrsTsPidApply apply_pid, SrsTsStream stream)
  543 +{
  544 + SrsTsChannel* channel = NULL;
  545 +
  546 + if (pids.find(pid) == pids.end()) {
  547 + channel = new SrsTsChannel();
  548 + pids[pid] = channel;
  549 + } else {
  550 + channel = pids[pid];
  551 + }
  552 +
  553 + channel->pid = pid;
  554 + channel->apply = apply_pid;
  555 + channel->stream = stream;
  556 +}
  557 +
532 int SrsTsContext::decode(SrsStream* stream, ISrsTsHandler* handler) 558 int SrsTsContext::decode(SrsStream* stream, ISrsTsHandler* handler)
533 { 559 {
534 int ret = ERROR_SUCCESS; 560 int ret = ERROR_SUCCESS;
@@ -559,28 +585,122 @@ int SrsTsContext::decode(SrsStream* stream, ISrsTsHandler* handler) @@ -559,28 +585,122 @@ int SrsTsContext::decode(SrsStream* stream, ISrsTsHandler* handler)
559 return ret; 585 return ret;
560 } 586 }
561 587
562 -SrsTsChannel* SrsTsContext::get(int pid) 588 +int SrsTsContext::encode(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload, SrsCodecVideo vc, SrsCodecAudio ac)
563 { 589 {
564 - if (pids.find(pid) == pids.end()) {  
565 - return NULL; 590 + int ret = ERROR_SUCCESS;
  591 +
  592 + // when any codec changed, write PAT/PMT table.
  593 + if (vcodec != vc || acodec != ac) {
  594 + vcodec = vc;
  595 + acodec = ac;
  596 + if ((ret = encode_pat_pmt(writer, vc, ac)) != ERROR_SUCCESS) {
  597 + return ret;
  598 + }
566 } 599 }
567 - return pids[pid]; 600 +
  601 + // encode the media frame to PES packets over TS.
  602 + return encode_pes(writer, frame, payload);
568 } 603 }
569 604
570 -void SrsTsContext::set(int pid, SrsTsPidApply apply_pid, SrsTsStream stream) 605 +int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCodecAudio ac)
571 { 606 {
572 - SrsTsChannel* channel = NULL; 607 + int ret = ERROR_SUCCESS;
573 608
574 - if (pids.find(pid) == pids.end()) {  
575 - channel = new SrsTsChannel();  
576 - pids[pid] = channel;  
577 - } else {  
578 - channel = pids[pid]; 609 + SrsTsStream vs = SrsTsStreamReserved;
  610 + SrsTsStream as = SrsTsStreamReserved;
  611 + switch (vc) {
  612 + case SrsCodecVideoAVC: vs = SrsTsStreamVideoH264; break;
  613 + case SrsCodecVideoReserved:
  614 + case SrsCodecVideoReserved1:
  615 + case SrsCodecVideoReserved2:
  616 + case SrsCodecVideoSorensonH263:
  617 + case SrsCodecVideoScreenVideo:
  618 + case SrsCodecVideoOn2VP6:
  619 + case SrsCodecVideoOn2VP6WithAlphaChannel:
  620 + case SrsCodecVideoScreenVideoVersion2:
  621 + break;
  622 + }
  623 + switch (ac) {
  624 + case SrsCodecAudioAAC: as = SrsTsStreamAudioAAC; break;
  625 + case SrsCodecAudioMP3: as = SrsTsStreamAudioMp3; break;
  626 + case SrsCodecAudioReserved1:
  627 + case SrsCodecAudioLinearPCMPlatformEndian:
  628 + case SrsCodecAudioADPCM:
  629 + case SrsCodecAudioLinearPCMLittleEndian:
  630 + case SrsCodecAudioNellymoser16kHzMono:
  631 + case SrsCodecAudioNellymoser8kHzMono:
  632 + case SrsCodecAudioNellymoser:
  633 + case SrsCodecAudioReservedG711AlawLogarithmicPCM:
  634 + case SrsCodecAudioReservedG711MuLawLogarithmicPCM:
  635 + case SrsCodecAudioReserved:
  636 + case SrsCodecAudioSpeex:
  637 + case SrsCodecAudioReservedMP3_8kHz:
  638 + case SrsCodecAudioReservedDeviceSpecificSound:
  639 + break;
  640 + }
  641 +
  642 + int16_t pmt_number = 1;
  643 + int16_t pmt_pid = 0x100;
  644 + if (true) {
  645 + SrsTsPacket* pkt = SrsTsPacket::create_pat(this, pmt_number, pmt_pid);
  646 + SrsAutoFree(SrsTsPacket, pkt);
  647 +
  648 + char* buf = new char[SRS_TS_PACKET_SIZE];
  649 + SrsAutoFree(char, buf);
  650 +
  651 + // set the left bytes with 0xFF.
  652 + int nb_buf = pkt->size();
  653 + srs_assert(nb_buf < SRS_TS_PACKET_SIZE);
  654 + memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf);
  655 +
  656 + SrsStream stream;
  657 + if ((ret = stream.initialize(buf, nb_buf)) != ERROR_SUCCESS) {
  658 + return ret;
  659 + }
  660 + if ((ret = pkt->encode(&stream)) != ERROR_SUCCESS) {
  661 + srs_error("ts encode ts packet failed. ret=%d", ret);
  662 + return ret;
  663 + }
  664 + if ((ret = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != ERROR_SUCCESS) {
  665 + srs_error("ts write ts packet failed. ret=%d", ret);
  666 + return ret;
  667 + }
579 } 668 }
  669 + int16_t video_pid = 0x101;
  670 + int16_t audio_pid = 0x102;
  671 + if (true) {
  672 + SrsTsPacket* pkt = SrsTsPacket::create_pmt(this, pmt_number, pmt_pid, video_pid, vs, audio_pid, as);
  673 + SrsAutoFree(SrsTsPacket, pkt);
580 674
581 - channel->pid = pid;  
582 - channel->apply = apply_pid;  
583 - channel->stream = stream; 675 + char* buf = new char[SRS_TS_PACKET_SIZE];
  676 + SrsAutoFree(char, buf);
  677 +
  678 + // set the left bytes with 0xFF.
  679 + int nb_buf = pkt->size();
  680 + srs_assert(nb_buf < SRS_TS_PACKET_SIZE);
  681 + memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf);
  682 +
  683 + SrsStream stream;
  684 + if ((ret = stream.initialize(buf, nb_buf)) != ERROR_SUCCESS) {
  685 + return ret;
  686 + }
  687 + if ((ret = pkt->encode(&stream)) != ERROR_SUCCESS) {
  688 + srs_error("ts encode ts packet failed. ret=%d", ret);
  689 + return ret;
  690 + }
  691 + if ((ret = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != ERROR_SUCCESS) {
  692 + srs_error("ts write ts packet failed. ret=%d", ret);
  693 + return ret;
  694 + }
  695 + }
  696 +
  697 + return ret;
  698 +}
  699 +
  700 +int SrsTsContext::encode_pes(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload)
  701 +{
  702 + int ret = ERROR_SUCCESS;
  703 + return ret;
584 } 704 }
585 705
586 SrsTsPacket::SrsTsPacket(SrsTsContext* c) 706 SrsTsPacket::SrsTsPacket(SrsTsContext* c)
@@ -688,6 +808,126 @@ int SrsTsPacket::decode(SrsStream* stream, SrsTsMessage** ppmsg) @@ -688,6 +808,126 @@ int SrsTsPacket::decode(SrsStream* stream, SrsTsMessage** ppmsg)
688 return ret; 808 return ret;
689 } 809 }
690 810
  811 +int SrsTsPacket::size()
  812 +{
  813 + int sz = 4;
  814 +
  815 + sz += adaptation_field? adaptation_field->size() : 0;
  816 + sz += payload? payload->size() : 0;
  817 +
  818 + return sz;
  819 +}
  820 +
  821 +int SrsTsPacket::encode(SrsStream* stream)
  822 +{
  823 + int ret = ERROR_SUCCESS;
  824 +
  825 + // 4B ts packet header.
  826 + if (!stream->require(4)) {
  827 + ret = ERROR_STREAM_CASTER_TS_HEADER;
  828 + srs_error("ts: mux header failed. ret=%d", ret);
  829 + return ret;
  830 + }
  831 +
  832 + stream->write_1bytes(sync_byte);
  833 +
  834 + int16_t pidv = pid & 0x1FFF;
  835 + pidv |= (transport_priority << 13) & 0x2000;
  836 + pidv |= (transport_error_indicator << 15) & 0x8000;
  837 + pidv |= (payload_unit_start_indicator << 14) & 0x4000;
  838 + stream->write_2bytes(pidv);
  839 +
  840 + int8_t ccv = continuity_counter & 0x0F;
  841 + ccv |= (transport_scrambling_control << 6) & 0xC0;
  842 + ccv |= (adaption_field_control << 4) & 0x30;
  843 + stream->write_1bytes(ccv);
  844 +
  845 + srs_info("ts: header sync=%#x error=%d unit_start=%d priotiry=%d pid=%d scrambling=%d adaption=%d counter=%d",
  846 + sync_byte, transport_error_indicator, payload_unit_start_indicator, transport_priority, pid,
  847 + transport_scrambling_control, adaption_field_control, continuity_counter);
  848 +
  849 + // optional: adaptation field
  850 + if (adaptation_field) {
  851 + if ((ret = adaptation_field->encode(stream)) != ERROR_SUCCESS) {
  852 + srs_error("ts: mux af faield. ret=%d", ret);
  853 + return ret;
  854 + }
  855 + srs_verbose("ts: mux af ok.");
  856 + }
  857 +
  858 + // optional: payload.
  859 + if (payload) {
  860 + if ((ret = payload->encode(stream)) != ERROR_SUCCESS) {
  861 + srs_error("ts: mux payload failed. ret=%d", ret);
  862 + return ret;
  863 + }
  864 + srs_verbose("ts: mux payload ok.");
  865 + }
  866 +
  867 + return ret;
  868 +}
  869 +
  870 +SrsTsPacket* SrsTsPacket::create_pat(SrsTsContext* context, int16_t pmt_number, int16_t pmt_pid)
  871 +{
  872 + SrsTsPacket* pkt = new SrsTsPacket(context);
  873 + pkt->sync_byte = 0x47;
  874 + pkt->transport_error_indicator = 0;
  875 + pkt->payload_unit_start_indicator = 1;
  876 + pkt->transport_priority = 0;
  877 + pkt->pid = SrsTsPidPAT;
  878 + pkt->transport_scrambling_control = SrsTsScrambledDisabled;
  879 + pkt->adaption_field_control = SrsTsAdaptationFieldTypePayloadOnly;
  880 + pkt->continuity_counter = 0;
  881 + pkt->adaptation_field = NULL;
  882 + SrsTsPayloadPAT* pat = new SrsTsPayloadPAT(pkt);
  883 + pkt->payload = pat;
  884 +
  885 + pat->pointer_field = 0;
  886 + pat->table_id = SrsTsPsiIdPas;
  887 + pat->section_syntax_indicator = 1;
  888 + pat->section_length = 0; // calc in size.
  889 + pat->transport_stream_id = 1;
  890 + pat->version_number = 0;
  891 + pat->current_next_indicator = 1;
  892 + pat->section_number = 0;
  893 + pat->last_section_number = 0;
  894 + pat->programs.push_back(new SrsTsPayloadPATProgram(pmt_number, pmt_pid));
  895 + pat->CRC_32 = 0; // calc in encode.
  896 + return pkt;
  897 +}
  898 +
  899 +SrsTsPacket* SrsTsPacket::create_pmt(SrsTsContext* context, int16_t pmt_number, int16_t pmt_pid, int16_t vpid, SrsTsStream vs, int16_t apid, SrsTsStream as)
  900 +{
  901 + SrsTsPacket* pkt = new SrsTsPacket(context);
  902 + pkt->sync_byte = 0x47;
  903 + pkt->transport_error_indicator = 0;
  904 + pkt->payload_unit_start_indicator = 1;
  905 + pkt->transport_priority = 0;
  906 + pkt->pid = (SrsTsPid)pmt_pid;
  907 + pkt->transport_scrambling_control = SrsTsScrambledDisabled;
  908 + pkt->adaption_field_control = SrsTsAdaptationFieldTypePayloadOnly;
  909 + pkt->continuity_counter = 0;
  910 + pkt->adaptation_field = NULL;
  911 + SrsTsPayloadPMT* pmt = new SrsTsPayloadPMT(pkt);
  912 + pkt->payload = pmt;
  913 +
  914 + pmt->pointer_field = 0;
  915 + pmt->table_id = SrsTsPsiIdPms;
  916 + pmt->section_syntax_indicator = 1;
  917 + pmt->section_length = 0; // calc in size.
  918 + pmt->program_number = pmt_number;
  919 + pmt->version_number = 0;
  920 + pmt->current_next_indicator = 1;
  921 + pmt->section_number = 0;
  922 + pmt->last_section_number = 0;
  923 + pmt->PCR_PID = vpid;
  924 + pmt->program_info_length = 0;
  925 + pmt->infos.push_back(new SrsTsPayloadPMTESInfo(vs, vpid));
  926 + pmt->infos.push_back(new SrsTsPayloadPMTESInfo(as, apid));
  927 + pmt->CRC_32 = 0; // calc in encode.
  928 + return pkt;
  929 +}
  930 +
691 SrsTsAdaptationField::SrsTsAdaptationField(SrsTsPacket* pkt) 931 SrsTsAdaptationField::SrsTsAdaptationField(SrsTsPacket* pkt)
692 { 932 {
693 packet = pkt; 933 packet = pkt;
@@ -929,6 +1169,27 @@ int SrsTsAdaptationField::decode(SrsStream* stream) @@ -929,6 +1169,27 @@ int SrsTsAdaptationField::decode(SrsStream* stream)
929 return ret; 1169 return ret;
930 } 1170 }
931 1171
  1172 +int SrsTsAdaptationField::size()
  1173 +{
  1174 + int sz = 2;
  1175 +
  1176 + sz += PCR_flag? 6 : 0;
  1177 + sz += OPCR_flag? 6 : 0;
  1178 + sz += splicing_point_flag? 1 : 0;
  1179 + sz += transport_private_data_flag? 1 + transport_private_data_length : 0;
  1180 + sz += adaptation_field_extension_flag? 2 + adaptation_field_extension_length : 0;
  1181 + sz += nb_af_ext_reserved;
  1182 + sz += nb_af_reserved;
  1183 +
  1184 + return sz;
  1185 +}
  1186 +
  1187 +int SrsTsAdaptationField::encode(SrsStream* stream)
  1188 +{
  1189 + int ret = ERROR_SUCCESS;
  1190 + return ret;
  1191 +}
  1192 +
932 SrsTsPayload::SrsTsPayload(SrsTsPacket* p) 1193 SrsTsPayload::SrsTsPayload(SrsTsPacket* p)
933 { 1194 {
934 packet = p; 1195 packet = p;
@@ -1356,6 +1617,65 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg) @@ -1356,6 +1617,65 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg)
1356 return ret; 1617 return ret;
1357 } 1618 }
1358 1619
  1620 +int SrsTsPayloadPES::size()
  1621 +{
  1622 + int sz = 6;
  1623 +
  1624 + SrsTsPESStreamId sid = (SrsTsPESStreamId)stream_id;
  1625 +
  1626 + if (sid != SrsTsPESStreamIdProgramStreamMap
  1627 + && sid != SrsTsPESStreamIdPaddingStream
  1628 + && sid != SrsTsPESStreamIdPrivateStream2
  1629 + && sid != SrsTsPESStreamIdEcmStream
  1630 + && sid != SrsTsPESStreamIdEmmStream
  1631 + && sid != SrsTsPESStreamIdProgramStreamDirectory
  1632 + && sid != SrsTsPESStreamIdDsmccStream
  1633 + && sid != SrsTsPESStreamIdH2221TypeE
  1634 + ) {
  1635 + sz += 3;
  1636 +
  1637 + sz += (PTS_DTS_flags == 0x2)? 5:0;
  1638 + sz += (PTS_DTS_flags == 0x3)? 10:0;
  1639 + sz += ESCR_flag? 6:0;
  1640 + sz += ES_rate_flag? 3:0;
  1641 + sz += DSM_trick_mode_flag? 1:0;
  1642 + sz += additional_copy_info_flag? 1:0;
  1643 + sz += PES_CRC_flag? 2:0;
  1644 + sz += PES_extension_flag? 1:0;
  1645 +
  1646 + if (PES_extension_flag) {
  1647 + sz += PES_private_data_flag? 16:0;
  1648 + sz += pack_header_field_flag? 1 + pack_field_length:0; // 1+x bytes.
  1649 + sz += program_packet_sequence_counter_flag? 2:0;
  1650 + sz += P_STD_buffer_flag? 2:0;
  1651 + sz += PES_extension_flag_2? 1 + PES_extension_field_length:0; // 1+x bytes.
  1652 + }
  1653 +
  1654 + sz += nb_stuffings;
  1655 +
  1656 + // packet bytes
  1657 + } else if (sid == SrsTsPESStreamIdProgramStreamMap
  1658 + || sid == SrsTsPESStreamIdPrivateStream2
  1659 + || sid == SrsTsPESStreamIdEcmStream
  1660 + || sid == SrsTsPESStreamIdEmmStream
  1661 + || sid == SrsTsPESStreamIdProgramStreamDirectory
  1662 + || sid == SrsTsPESStreamIdDsmccStream
  1663 + || sid == SrsTsPESStreamIdH2221TypeE
  1664 + ) {
  1665 + // packet bytes
  1666 + } else {
  1667 + // nb_drop
  1668 + }
  1669 +
  1670 + return sz;
  1671 +}
  1672 +
  1673 +int SrsTsPayloadPES::encode(SrsStream* stream)
  1674 +{
  1675 + int ret = ERROR_SUCCESS;
  1676 + return ret;
  1677 +}
  1678 +
1359 int SrsTsPayloadPES::decode_33bits_dts_pts(SrsStream* stream, int64_t* pv) 1679 int SrsTsPayloadPES::decode_33bits_dts_pts(SrsStream* stream, int64_t* pv)
1360 { 1680 {
1361 int ret = ERROR_SUCCESS; 1681 int ret = ERROR_SUCCESS;
@@ -1420,6 +1740,8 @@ int SrsTsPayloadPES::decode_33bits_dts_pts(SrsStream* stream, int64_t* pv) @@ -1420,6 +1740,8 @@ int SrsTsPayloadPES::decode_33bits_dts_pts(SrsStream* stream, int64_t* pv)
1420 SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p) 1740 SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p)
1421 { 1741 {
1422 pointer_field = 0; 1742 pointer_field = 0;
  1743 + const0_value = 0;
  1744 + const1_value = 3;
1423 CRC_32 = 0; 1745 CRC_32 = 0;
1424 } 1746 }
1425 1747
@@ -1462,11 +1784,12 @@ int SrsTsPayloadPSI::decode(SrsStream* stream, SrsTsMessage** /*ppmsg*/) @@ -1462,11 +1784,12 @@ int SrsTsPayloadPSI::decode(SrsStream* stream, SrsTsMessage** /*ppmsg*/)
1462 table_id = (SrsTsPsiId)stream->read_1bytes(); 1784 table_id = (SrsTsPsiId)stream->read_1bytes();
1463 1785
1464 // 2B 1786 // 2B
1465 - section_length = stream->read_2bytes(); 1787 + int16_t slv = stream->read_2bytes();
1466 1788
1467 - section_syntax_indicator = (section_length >> 15) & 0x01;  
1468 - const0_value = (section_length >> 14) & 0x01;  
1469 - section_length &= 0x0FFF; 1789 + section_syntax_indicator = (slv >> 15) & 0x01;
  1790 + const0_value = (slv >> 14) & 0x01;
  1791 + const1_value = (slv >> 12) & 0x03;
  1792 + section_length = slv & 0x0FFF;
1470 1793
1471 // no section, ignore. 1794 // no section, ignore.
1472 if (section_length == 0) { 1795 if (section_length == 0) {
@@ -1521,18 +1844,139 @@ int SrsTsPayloadPSI::decode(SrsStream* stream, SrsTsMessage** /*ppmsg*/) @@ -1521,18 +1844,139 @@ int SrsTsPayloadPSI::decode(SrsStream* stream, SrsTsMessage** /*ppmsg*/)
1521 return ret; 1844 return ret;
1522 } 1845 }
1523 1846
1524 -SrsTsPayloadPATProgram::SrsTsPayloadPATProgram() 1847 +int SrsTsPayloadPSI::size()
1525 { 1848 {
1526 - number = 0;  
1527 - pid = 0; 1849 + int sz = 0;
  1850 +
  1851 + // section size is the sl plus the crc32
  1852 + section_length = psi_size() + 4;
  1853 +
  1854 + sz += packet->payload_unit_start_indicator? 1:0;
  1855 + sz += 3;
  1856 + sz += section_length;
  1857 +
  1858 + return sz;
  1859 +}
  1860 +
  1861 +int SrsTsPayloadPSI::encode(SrsStream* stream)
  1862 +{
  1863 + int ret = ERROR_SUCCESS;
  1864 +
  1865 + if (packet->payload_unit_start_indicator) {
  1866 + if (!stream->require(1)) {
  1867 + ret = ERROR_STREAM_CASTER_TS_PSI;
  1868 + srs_error("ts: mux PSI failed. ret=%d", ret);
  1869 + return ret;
  1870 + }
  1871 + stream->write_1bytes(pointer_field);
  1872 + }
  1873 +
  1874 + // to calc the crc32
  1875 + char* ppat = stream->data() + stream->pos();
  1876 + int pat_pos = stream->pos();
  1877 +
  1878 + // atleast 3B for all psi.
  1879 + if (!stream->require(3)) {
  1880 + ret = ERROR_STREAM_CASTER_TS_PSI;
  1881 + srs_error("ts: mux PSI failed. ret=%d", ret);
  1882 + return ret;
  1883 + }
  1884 + // 1B
  1885 + stream->write_1bytes(table_id);
  1886 +
  1887 + // 2B
  1888 + int16_t slv = section_length & 0x0FFF;
  1889 + slv |= (section_syntax_indicator << 15) & 0x8000;
  1890 + slv |= (const0_value << 14) & 0x4000;
  1891 + slv |= (const1_value << 12) & 0x3000;
  1892 + stream->write_2bytes(slv);
  1893 +
  1894 + // no section, ignore.
  1895 + if (section_length == 0) {
  1896 + srs_warn("ts: mux PAT ignore empty section");
  1897 + return ret;
  1898 + }
  1899 +
  1900 + if (!stream->require(section_length)) {
  1901 + ret = ERROR_STREAM_CASTER_TS_PSI;
  1902 + srs_error("ts: mux PAT section failed. ret=%d", ret);
  1903 + return ret;
  1904 + }
  1905 +
  1906 + // call the virtual method of actual PSI.
  1907 + if ((ret = psi_encode(stream)) != ERROR_SUCCESS) {
  1908 + return ret;
  1909 + }
  1910 +
  1911 + // 4B
  1912 + if (!stream->require(4)) {
  1913 + ret = ERROR_STREAM_CASTER_TS_PSI;
  1914 + srs_error("ts: mux PSI crc32 failed. ret=%d", ret);
  1915 + return ret;
  1916 + }
  1917 + CRC_32 = srs_crc32(ppat, stream->pos() - pat_pos);
  1918 + stream->write_4bytes(CRC_32);
  1919 +
  1920 + return ret;
  1921 +}
  1922 +
  1923 +SrsTsPayloadPATProgram::SrsTsPayloadPATProgram(int16_t n, int16_t p)
  1924 +{
  1925 + number = n;
  1926 + pid = p;
  1927 + const1_value = 0x07;
1528 } 1928 }
1529 1929
1530 SrsTsPayloadPATProgram::~SrsTsPayloadPATProgram() 1930 SrsTsPayloadPATProgram::~SrsTsPayloadPATProgram()
1531 { 1931 {
1532 } 1932 }
1533 1933
  1934 +int SrsTsPayloadPATProgram::decode(SrsStream* stream)
  1935 +{
  1936 + int ret = ERROR_SUCCESS;
  1937 +
  1938 + // atleast 4B for PAT program specified
  1939 + if (!stream->require(4)) {
  1940 + ret = ERROR_STREAM_CASTER_TS_PAT;
  1941 + srs_error("ts: demux PAT failed. ret=%d", ret);
  1942 + return ret;
  1943 + }
  1944 +
  1945 + int tmpv = stream->read_4bytes();
  1946 + number = (int16_t)((tmpv >> 16) & 0xFFFF);
  1947 + const1_value = (int16_t)((tmpv >> 13) & 0x07);
  1948 + pid = (int16_t)(tmpv & 0x1FFF);
  1949 +
  1950 + return ret;
  1951 +}
  1952 +
  1953 +int SrsTsPayloadPATProgram::size()
  1954 +{
  1955 + return 4;
  1956 +}
  1957 +
  1958 +int SrsTsPayloadPATProgram::encode(SrsStream* stream)
  1959 +{
  1960 + int ret = ERROR_SUCCESS;
  1961 +
  1962 + // atleast 4B for PAT program specified
  1963 + if (!stream->require(4)) {
  1964 + ret = ERROR_STREAM_CASTER_TS_PAT;
  1965 + srs_error("ts: mux PAT failed. ret=%d", ret);
  1966 + return ret;
  1967 + }
  1968 +
  1969 + int tmpv = pid & 0x1FFF;
  1970 + tmpv |= (number << 16) & 0xFFFF0000;
  1971 + tmpv |= (const1_value << 13) & 0xE000;
  1972 + stream->write_4bytes(tmpv);
  1973 +
  1974 + return ret;
  1975 +}
  1976 +
1534 SrsTsPayloadPAT::SrsTsPayloadPAT(SrsTsPacket* p) : SrsTsPayloadPSI(p) 1977 SrsTsPayloadPAT::SrsTsPayloadPAT(SrsTsPacket* p) : SrsTsPayloadPSI(p)
1535 { 1978 {
  1979 + const1_value = 3;
1536 } 1980 }
1537 1981
1538 SrsTsPayloadPAT::~SrsTsPayloadPAT() 1982 SrsTsPayloadPAT::~SrsTsPayloadPAT()
@@ -1562,10 +2006,11 @@ int SrsTsPayloadPAT::psi_decode(SrsStream* stream) @@ -1562,10 +2006,11 @@ int SrsTsPayloadPAT::psi_decode(SrsStream* stream)
1562 transport_stream_id = stream->read_2bytes(); 2006 transport_stream_id = stream->read_2bytes();
1563 2007
1564 // 1B 2008 // 1B
1565 - current_next_indicator = stream->read_1bytes(); 2009 + int8_t cniv = stream->read_1bytes();
1566 2010
1567 - version_number = (current_next_indicator >> 1) & 0x1F;  
1568 - current_next_indicator &= 0x01; 2011 + const1_value = (cniv >> 6) & 0x03;
  2012 + version_number = (cniv >> 1) & 0x1F;
  2013 + current_next_indicator = cniv & 0x01;
1569 2014
1570 // TODO: FIXME: check the indicator. 2015 // TODO: FIXME: check the indicator.
1571 2016
@@ -1579,9 +2024,9 @@ int SrsTsPayloadPAT::psi_decode(SrsStream* stream) @@ -1579,9 +2024,9 @@ int SrsTsPayloadPAT::psi_decode(SrsStream* stream)
1579 for (int i = 0; i < program_bytes; i += 4) { 2024 for (int i = 0; i < program_bytes; i += 4) {
1580 SrsTsPayloadPATProgram* program = new SrsTsPayloadPATProgram(); 2025 SrsTsPayloadPATProgram* program = new SrsTsPayloadPATProgram();
1581 2026
1582 - int tmpv = stream->read_4bytes();  
1583 - program->number = (int16_t)((tmpv >> 16) & 0xFFFF);  
1584 - program->pid = (int16_t)(tmpv & 0x1FFF); 2027 + if ((ret = program->decode(stream)) != ERROR_SUCCESS) {
  2028 + return ret;
  2029 + }
1585 2030
1586 // update the apply pid table. 2031 // update the apply pid table.
1587 packet->context->set(program->pid, SrsTsPidApplyPMT); 2032 packet->context->set(program->pid, SrsTsPidApplyPMT);
@@ -1595,8 +2040,59 @@ int SrsTsPayloadPAT::psi_decode(SrsStream* stream) @@ -1595,8 +2040,59 @@ int SrsTsPayloadPAT::psi_decode(SrsStream* stream)
1595 return ret; 2040 return ret;
1596 } 2041 }
1597 2042
1598 -SrsTsPayloadPMTESInfo::SrsTsPayloadPMTESInfo() 2043 +int SrsTsPayloadPAT::psi_size()
1599 { 2044 {
  2045 + int sz = 5;
  2046 + for (int i = 0; i < (int)programs.size(); i ++) {
  2047 + SrsTsPayloadPATProgram* program = programs.at(i);
  2048 + sz += program->size();
  2049 + }
  2050 + return sz;
  2051 +}
  2052 +
  2053 +int SrsTsPayloadPAT::psi_encode(SrsStream* stream)
  2054 +{
  2055 + int ret = ERROR_SUCCESS;
  2056 +
  2057 + // atleast 5B for PAT specified
  2058 + if (!stream->require(5)) {
  2059 + ret = ERROR_STREAM_CASTER_TS_PAT;
  2060 + srs_error("ts: mux PAT failed. ret=%d", ret);
  2061 + return ret;
  2062 + }
  2063 +
  2064 + // 2B
  2065 + stream->write_2bytes(transport_stream_id);
  2066 +
  2067 + // 1B
  2068 + int8_t cniv = current_next_indicator & 0x01;
  2069 + cniv |= (version_number << 1) & 0x3E;
  2070 + cniv |= (const1_value << 6) & 0xC0;
  2071 + stream->write_1bytes(cniv);
  2072 +
  2073 + // 1B
  2074 + stream->write_1bytes(section_number);
  2075 + // 1B
  2076 + stream->write_1bytes(last_section_number);
  2077 +
  2078 + // multiple 4B program data.
  2079 + for (int i = 0; i < (int)programs.size(); i ++) {
  2080 + SrsTsPayloadPATProgram* program = programs.at(i);
  2081 + if ((ret = program->encode(stream)) != ERROR_SUCCESS) {
  2082 + return ret;
  2083 + }
  2084 + }
  2085 +
  2086 + return ret;
  2087 +}
  2088 +
  2089 +SrsTsPayloadPMTESInfo::SrsTsPayloadPMTESInfo(SrsTsStream st, int16_t epid)
  2090 +{
  2091 + stream_type = st;
  2092 + elementary_PID = epid;
  2093 +
  2094 + const1_value0 = 7;
  2095 + const1_value1 = 0x0f;
1600 ES_info_length = 0; 2096 ES_info_length = 0;
1601 ES_info = NULL; 2097 ES_info = NULL;
1602 } 2098 }
@@ -1606,8 +2102,84 @@ SrsTsPayloadPMTESInfo::~SrsTsPayloadPMTESInfo() @@ -1606,8 +2102,84 @@ SrsTsPayloadPMTESInfo::~SrsTsPayloadPMTESInfo()
1606 srs_freep(ES_info); 2102 srs_freep(ES_info);
1607 } 2103 }
1608 2104
  2105 +int SrsTsPayloadPMTESInfo::decode(SrsStream* stream)
  2106 +{
  2107 + int ret = ERROR_SUCCESS;
  2108 +
  2109 + // 5B
  2110 + if (!stream->require(5)) {
  2111 + ret = ERROR_STREAM_CASTER_TS_PMT;
  2112 + srs_error("ts: demux PMT es info failed. ret=%d", ret);
  2113 + return ret;
  2114 + }
  2115 +
  2116 + stream_type = (SrsTsStream)stream->read_1bytes();
  2117 +
  2118 + int16_t epv = stream->read_2bytes();
  2119 + const1_value0 = (epv >> 13) & 0x07;
  2120 + elementary_PID = epv & 0x1FFF;
  2121 +
  2122 + int16_t eilv = stream->read_2bytes();
  2123 + const1_value1 = (epv >> 12) & 0x0f;
  2124 + ES_info_length = eilv & 0x0FFF;
  2125 +
  2126 + if (ES_info_length > 0) {
  2127 + if (!stream->require(ES_info_length)) {
  2128 + ret = ERROR_STREAM_CASTER_TS_PMT;
  2129 + srs_error("ts: demux PMT es info data failed. ret=%d", ret);
  2130 + return ret;
  2131 + }
  2132 + srs_freep(ES_info);
  2133 + ES_info = new char[ES_info_length];
  2134 + stream->read_bytes(ES_info, ES_info_length);
  2135 + }
  2136 +
  2137 + return ret;
  2138 +}
  2139 +
  2140 +int SrsTsPayloadPMTESInfo::size()
  2141 +{
  2142 + return 5 + ES_info_length;
  2143 +}
  2144 +
  2145 +int SrsTsPayloadPMTESInfo::encode(SrsStream* stream)
  2146 +{
  2147 + int ret = ERROR_SUCCESS;
  2148 +
  2149 + // 5B
  2150 + if (!stream->require(5)) {
  2151 + ret = ERROR_STREAM_CASTER_TS_PMT;
  2152 + srs_error("ts: mux PMT es info failed. ret=%d", ret);
  2153 + return ret;
  2154 + }
  2155 +
  2156 + stream->write_1bytes(stream_type);
  2157 +
  2158 + int16_t epv = elementary_PID & 0x1FFF;
  2159 + epv |= (const1_value0 << 13) & 0xE000;
  2160 + stream->write_2bytes(epv);
  2161 +
  2162 + int16_t eilv = ES_info_length & 0x0FFF;
  2163 + eilv |= (const1_value1 << 12) & 0xF000;
  2164 + stream->write_2bytes(eilv);
  2165 +
  2166 + if (ES_info_length > 0) {
  2167 + if (!stream->require(ES_info_length)) {
  2168 + ret = ERROR_STREAM_CASTER_TS_PMT;
  2169 + srs_error("ts: mux PMT es info data failed. ret=%d", ret);
  2170 + return ret;
  2171 + }
  2172 + stream->write_bytes(ES_info, ES_info_length);
  2173 + }
  2174 +
  2175 + return ret;
  2176 +}
  2177 +
1609 SrsTsPayloadPMT::SrsTsPayloadPMT(SrsTsPacket* p) : SrsTsPayloadPSI(p) 2178 SrsTsPayloadPMT::SrsTsPayloadPMT(SrsTsPacket* p) : SrsTsPayloadPSI(p)
1610 { 2179 {
  2180 + const1_value0 = 3;
  2181 + const1_value1 = 7;
  2182 + const1_value2 = 0x0f;
1611 program_info_length = 0; 2183 program_info_length = 0;
1612 program_info_desc = NULL; 2184 program_info_desc = NULL;
1613 } 2185 }
@@ -1639,10 +2211,11 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream) @@ -1639,10 +2211,11 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
1639 program_number = stream->read_2bytes(); 2211 program_number = stream->read_2bytes();
1640 2212
1641 // 1B 2213 // 1B
1642 - current_next_indicator = stream->read_1bytes(); 2214 + int8_t cniv = stream->read_1bytes();
1643 2215
1644 - version_number = (current_next_indicator >> 1) & 0x1F;  
1645 - current_next_indicator &= 0x01; 2216 + const1_value0 = (cniv >> 6) & 0x03;
  2217 + version_number = (cniv >> 1) & 0x1F;
  2218 + current_next_indicator = cniv & 0x01;
1646 2219
1647 // 1B 2220 // 1B
1648 section_number = stream->read_1bytes(); 2221 section_number = stream->read_1bytes();
@@ -1651,15 +2224,15 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream) @@ -1651,15 +2224,15 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
1651 last_section_number = stream->read_1bytes(); 2224 last_section_number = stream->read_1bytes();
1652 2225
1653 // 2B 2226 // 2B
1654 - PCR_PID = stream->read_2bytes();  
1655 -  
1656 - PCR_PID &= 0x1FFF; 2227 + int16_t ppv = stream->read_2bytes();
  2228 + const1_value1 = (ppv >> 13) & 0x07;
  2229 + PCR_PID = ppv & 0x1FFF;
1657 2230
1658 // 2B 2231 // 2B
1659 - program_info_length = stream->read_2bytes(); 2232 + int16_t pilv = stream->read_2bytes();
  2233 + const1_value2 = (pilv >> 12) & 0x0F;
  2234 + program_info_length = pilv & 0xFFF;
1660 2235
1661 - program_info_length &= 0xFFF;  
1662 -  
1663 if (program_info_length > 0) { 2236 if (program_info_length > 0) {
1664 if (!stream->require(program_info_length)) { 2237 if (!stream->require(program_info_length)) {
1665 ret = ERROR_STREAM_CASTER_TS_PMT; 2238 ret = ERROR_STREAM_CASTER_TS_PMT;
@@ -1678,31 +2251,10 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream) @@ -1678,31 +2251,10 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
1678 SrsTsPayloadPMTESInfo* info = new SrsTsPayloadPMTESInfo(); 2251 SrsTsPayloadPMTESInfo* info = new SrsTsPayloadPMTESInfo();
1679 infos.push_back(info); 2252 infos.push_back(info);
1680 2253
1681 - // 5B  
1682 - if (!stream->require(5)) {  
1683 - ret = ERROR_STREAM_CASTER_TS_PMT;  
1684 - srs_error("ts: demux PMT es info failed. ret=%d", ret); 2254 + if ((ret = info->decode(stream)) != ERROR_SUCCESS) {
1685 return ret; 2255 return ret;
1686 } 2256 }
1687 2257
1688 - info->stream_type = (SrsTsStream)stream->read_1bytes();  
1689 - info->elementary_PID = stream->read_2bytes();  
1690 - info->ES_info_length = stream->read_2bytes();  
1691 -  
1692 - info->elementary_PID &= 0x1FFF;  
1693 - info->ES_info_length &= 0x0FFF;  
1694 -  
1695 - if (info->ES_info_length > 0) {  
1696 - if (!stream->require(info->ES_info_length)) {  
1697 - ret = ERROR_STREAM_CASTER_TS_PMT;  
1698 - srs_error("ts: demux PMT es info data failed. ret=%d", ret);  
1699 - return ret;  
1700 - }  
1701 - srs_freep(info->ES_info);  
1702 - info->ES_info = new char[info->ES_info_length];  
1703 - stream->read_bytes(info->ES_info, info->ES_info_length);  
1704 - }  
1705 -  
1706 // update the apply pid table 2258 // update the apply pid table
1707 switch (info->stream_type) { 2259 switch (info->stream_type) {
1708 case SrsTsStreamVideoH264: 2260 case SrsTsStreamVideoH264:
@@ -1727,14 +2279,82 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream) @@ -1727,14 +2279,82 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
1727 return ret; 2279 return ret;
1728 } 2280 }
1729 2281
  2282 +int SrsTsPayloadPMT::psi_size()
  2283 +{
  2284 + int sz = 9;
  2285 + sz += program_info_length;
  2286 + for (int i = 0; i < (int)infos.size(); i ++) {
  2287 + SrsTsPayloadPMTESInfo* info = infos.at(i);
  2288 + sz += info->size();
  2289 + }
  2290 + return sz;
  2291 +}
  2292 +
  2293 +int SrsTsPayloadPMT::psi_encode(SrsStream* stream)
  2294 +{
  2295 + int ret = ERROR_SUCCESS;
  2296 +
  2297 + // atleast 9B for PMT specified
  2298 + if (!stream->require(9)) {
  2299 + ret = ERROR_STREAM_CASTER_TS_PMT;
  2300 + srs_error("ts: mux PMT failed. ret=%d", ret);
  2301 + return ret;
  2302 + }
  2303 +
  2304 + // 2B
  2305 + stream->write_2bytes(program_number);
  2306 +
  2307 + // 1B
  2308 + int8_t cniv = current_next_indicator & 0x01;
  2309 + cniv |= (const1_value0 << 6) & 0xC0;
  2310 + cniv |= (version_number << 1) & 0xFE;
  2311 + stream->write_1bytes(cniv);
  2312 +
  2313 + // 1B
  2314 + stream->write_1bytes(section_number);
  2315 +
  2316 + // 1B
  2317 + stream->write_1bytes(last_section_number);
  2318 +
  2319 + // 2B
  2320 + int16_t ppv = PCR_PID & 0x1FFF;
  2321 + ppv |= (const1_value1 << 13) & 0xE000;
  2322 + stream->write_2bytes(ppv);
  2323 +
  2324 + // 2B
  2325 + int16_t pilv = program_info_length & 0xFFF;
  2326 + pilv |= (const1_value2 << 12) & 0xF000;
  2327 + stream->write_2bytes(pilv);
  2328 +
  2329 + if (program_info_length > 0) {
  2330 + if (!stream->require(program_info_length)) {
  2331 + ret = ERROR_STREAM_CASTER_TS_PMT;
  2332 + srs_error("ts: mux PMT program info failed. ret=%d", ret);
  2333 + return ret;
  2334 + }
  2335 +
  2336 + stream->write_bytes(program_info_desc, program_info_length);
  2337 + }
  2338 +
  2339 + for (int i = 0; i < (int)infos.size(); i ++) {
  2340 + SrsTsPayloadPMTESInfo* info = infos.at(i);
  2341 + if ((ret = info->encode(stream)) != ERROR_SUCCESS) {
  2342 + return ret;
  2343 + }
  2344 + }
  2345 +
  2346 + return ret;
  2347 +}
  2348 +
1730 SrsTSMuxer::SrsTSMuxer(SrsFileWriter* w) 2349 SrsTSMuxer::SrsTSMuxer(SrsFileWriter* w)
1731 { 2350 {
1732 writer = w; 2351 writer = w;
  2352 + context = NULL;
1733 2353
1734 - // reserved is not written.  
1735 - previous = SrsCodecAudioReserved1;  
1736 - // current default to aac.  
1737 - current = SrsCodecAudioAAC; 2354 + // default to aac.
  2355 + acodec = SrsCodecAudioAAC;
  2356 + // default to avc(h.264)
  2357 + vcodec = SrsCodecVideoAVC;
1738 } 2358 }
1739 2359
1740 SrsTSMuxer::~SrsTSMuxer() 2360 SrsTSMuxer::~SrsTSMuxer()
@@ -1749,6 +2369,10 @@ int SrsTSMuxer::open(string _path) @@ -1749,6 +2369,10 @@ int SrsTSMuxer::open(string _path)
1749 path = _path; 2369 path = _path;
1750 2370
1751 close(); 2371 close();
  2372 +
  2373 + // use context to write ts file.
  2374 + srs_freep(context);
  2375 + context = new SrsTsContext();
1752 2376
1753 if ((ret = writer->open(path)) != ERROR_SUCCESS) { 2377 if ((ret = writer->open(path)) != ERROR_SUCCESS) {
1754 return ret; 2378 return ret;
@@ -1759,31 +2383,19 @@ int SrsTSMuxer::open(string _path) @@ -1759,31 +2383,19 @@ int SrsTSMuxer::open(string _path)
1759 2383
1760 int SrsTSMuxer::update_acodec(SrsCodecAudio ac) 2384 int SrsTSMuxer::update_acodec(SrsCodecAudio ac)
1761 { 2385 {
1762 - int ret = ERROR_SUCCESS;  
1763 -  
1764 - if (current == ac) {  
1765 - return ret;  
1766 - }  
1767 - current = ac;  
1768 -  
1769 - return ret; 2386 + acodec = ac;
  2387 + return ERROR_SUCCESS;
1770 } 2388 }
1771 2389
1772 int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab) 2390 int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab)
1773 { 2391 {
1774 int ret = ERROR_SUCCESS; 2392 int ret = ERROR_SUCCESS;
1775 2393
1776 - // when acodec changed, write header.  
1777 - if (current != previous) {  
1778 - previous = current;  
1779 - if ((ret = SrsMpegtsWriter::write_header(writer, previous)) != ERROR_SUCCESS) {  
1780 - return ret;  
1781 - }  
1782 - }  
1783 -  
1784 - if ((ret = SrsMpegtsWriter::write_frame(writer, af, ab)) != ERROR_SUCCESS) { 2394 + if ((ret = context->encode(writer, af, ab, vcodec, acodec)) != ERROR_SUCCESS) {
  2395 + srs_error("hls encode audio failed. ret=%d", ret);
1785 return ret; 2396 return ret;
1786 } 2397 }
  2398 + srs_info("hls encode audio ok");
1787 2399
1788 return ret; 2400 return ret;
1789 } 2401 }
@@ -1792,23 +2404,18 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsSimpleBuffer* vb) @@ -1792,23 +2404,18 @@ int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsSimpleBuffer* vb)
1792 { 2404 {
1793 int ret = ERROR_SUCCESS; 2405 int ret = ERROR_SUCCESS;
1794 2406
1795 - // when acodec changed, write header.  
1796 - if (current != previous) {  
1797 - previous = current;  
1798 - if ((ret = SrsMpegtsWriter::write_header(writer, previous)) != ERROR_SUCCESS) {  
1799 - return ret;  
1800 - }  
1801 - }  
1802 -  
1803 - if ((ret = SrsMpegtsWriter::write_frame(writer, vf, vb)) != ERROR_SUCCESS) { 2407 + if ((ret = context->encode(writer, vf, vb, vcodec, acodec)) != ERROR_SUCCESS) {
  2408 + srs_error("hls encode video failed. ret=%d", ret);
1804 return ret; 2409 return ret;
1805 } 2410 }
  2411 + srs_info("hls encode video ok");
1806 2412
1807 return ret; 2413 return ret;
1808 } 2414 }
1809 2415
1810 void SrsTSMuxer::close() 2416 void SrsTSMuxer::close()
1811 { 2417 {
  2418 + srs_freep(context);
1812 writer->close(); 2419 writer->close();
1813 } 2420 }
1814 2421
@@ -59,7 +59,7 @@ public: @@ -59,7 +59,7 @@ public:
59 int64_t dts; 59 int64_t dts;
60 int pid; 60 int pid;
61 int sid; 61 int sid;
62 - int cc; 62 + int cc; // continuity_counter
63 bool write_pcr; 63 bool write_pcr;
64 64
65 SrsMpegtsFrame(); 65 SrsMpegtsFrame();
@@ -327,18 +327,18 @@ public: @@ -327,18 +327,18 @@ public:
327 */ 327 */
328 class SrsTsContext 328 class SrsTsContext
329 { 329 {
  330 +// codec
330 private: 331 private:
331 std::map<int, SrsTsChannel*> pids; 332 std::map<int, SrsTsChannel*> pids;
  333 +// encoder
  334 +private:
  335 + // when any codec changed, write the PAT/PMT.
  336 + SrsCodecVideo vcodec;
  337 + SrsCodecAudio acodec;
332 public: 338 public:
333 SrsTsContext(); 339 SrsTsContext();
334 virtual ~SrsTsContext(); 340 virtual ~SrsTsContext();
335 -public:  
336 - /**  
337 - * the stream contains only one ts packet.  
338 - * @param handler the ts message handler to process the msg.  
339 - * @remark we will consume all bytes in stream.  
340 - */  
341 - virtual int decode(SrsStream* stream, ISrsTsHandler* handler); 341 +// codec
342 public: 342 public:
343 /** 343 /**
344 * get the pid apply, the parsed pid. 344 * get the pid apply, the parsed pid.
@@ -349,6 +349,27 @@ public: @@ -349,6 +349,27 @@ public:
349 * set the pid apply, the parsed pid. 349 * set the pid apply, the parsed pid.
350 */ 350 */
351 virtual void set(int pid, SrsTsPidApply apply_pid, SrsTsStream stream = SrsTsStreamReserved); 351 virtual void set(int pid, SrsTsPidApply apply_pid, SrsTsStream stream = SrsTsStreamReserved);
  352 +// decode methods
  353 +public:
  354 + /**
  355 + * the stream contains only one ts packet.
  356 + * @param handler the ts message handler to process the msg.
  357 + * @remark we will consume all bytes in stream.
  358 + */
  359 + virtual int decode(SrsStream* stream, ISrsTsHandler* handler);
  360 +// encode methods
  361 +public:
  362 + /**
  363 + * write the PES packet, the video/audio stream.
  364 + * @param frame the video/audio frame info.
  365 + * @param payload the video/audio payload bytes.
  366 + * @param vc the video codec, write the PAT/PMT table when changed.
  367 + * @param ac the audio codec, write the PAT/PMT table when changed.
  368 + */
  369 + virtual int encode(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload, SrsCodecVideo vc, SrsCodecAudio ac);
  370 +private:
  371 + virtual int encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vcodec, SrsCodecAudio acodec);
  372 + virtual int encode_pes(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload);
352 }; 373 };
353 374
354 /** 375 /**
@@ -454,6 +475,12 @@ public: @@ -454,6 +475,12 @@ public:
454 virtual ~SrsTsPacket(); 475 virtual ~SrsTsPacket();
455 public: 476 public:
456 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg); 477 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg);
  478 +public:
  479 + virtual int size();
  480 + virtual int encode(SrsStream* stream);
  481 +public:
  482 + static SrsTsPacket* create_pat(SrsTsContext* context, int16_t pmt_number, int16_t pmt_pid);
  483 + static SrsTsPacket* create_pmt(SrsTsContext* context, int16_t pmt_number, int16_t pmt_pid, int16_t vpid, SrsTsStream vs, int16_t apid, SrsTsStream as);
457 }; 484 };
458 485
459 /** 486 /**
@@ -755,6 +782,9 @@ public: @@ -755,6 +782,9 @@ public:
755 virtual ~SrsTsAdaptationField(); 782 virtual ~SrsTsAdaptationField();
756 public: 783 public:
757 virtual int decode(SrsStream* stream); 784 virtual int decode(SrsStream* stream);
  785 +public:
  786 + virtual int size();
  787 + virtual int encode(SrsStream* stream);
758 }; 788 };
759 789
760 /** 790 /**
@@ -800,6 +830,9 @@ public: @@ -800,6 +830,9 @@ public:
800 virtual ~SrsTsPayload(); 830 virtual ~SrsTsPayload();
801 public: 831 public:
802 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg) = 0; 832 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg) = 0;
  833 +public:
  834 + virtual int size() = 0;
  835 + virtual int encode(SrsStream* stream) = 0;
803 }; 836 };
804 837
805 /** 838 /**
@@ -1141,6 +1174,9 @@ public: @@ -1141,6 +1174,9 @@ public:
1141 virtual ~SrsTsPayloadPES(); 1174 virtual ~SrsTsPayloadPES();
1142 public: 1175 public:
1143 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg); 1176 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg);
  1177 +public:
  1178 + virtual int size();
  1179 + virtual int encode(SrsStream* stream);
1144 private: 1180 private:
1145 virtual int decode_33bits_dts_pts(SrsStream* stream, int64_t* pv); 1181 virtual int decode_33bits_dts_pts(SrsStream* stream, int64_t* pv);
1146 }; 1182 };
@@ -1179,7 +1215,10 @@ public: @@ -1179,7 +1215,10 @@ public:
1179 * const value, must be '0' 1215 * const value, must be '0'
1180 */ 1216 */
1181 int8_t const0_value; //1bit 1217 int8_t const0_value; //1bit
1182 - // 2bits reserved. 1218 + /**
  1219 + * reverved value, must be '1'
  1220 + */
  1221 + int8_t const1_value; //2bits
1183 /** 1222 /**
1184 * This is a 12-bit field, the first two bits of which shall be '00'. The remaining 10 bits specify the number 1223 * This is a 12-bit field, the first two bits of which shall be '00'. The remaining 10 bits specify the number
1185 * of bytes of the section, starting immediately following the section_length field, and including the CRC. The value in this 1224 * of bytes of the section, starting immediately following the section_length field, and including the CRC. The value in this
@@ -1187,6 +1226,8 @@ public: @@ -1187,6 +1226,8 @@ public:
1187 */ 1226 */
1188 u_int16_t section_length; //12bits 1227 u_int16_t section_length; //12bits
1189 public: 1228 public:
  1229 + // the specified psi info, for example, PAT fields.
  1230 +public:
1190 // 4B 1231 // 4B
1191 /** 1232 /**
1192 * This is a 32-bit field that contains the CRC value that gives a zero output of the registers in the decoder 1233 * This is a 32-bit field that contains the CRC value that gives a zero output of the registers in the decoder
@@ -1199,7 +1240,12 @@ public: @@ -1199,7 +1240,12 @@ public:
1199 virtual ~SrsTsPayloadPSI(); 1240 virtual ~SrsTsPayloadPSI();
1200 public: 1241 public:
1201 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg); 1242 virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg);
  1243 +public:
  1244 + virtual int size();
  1245 + virtual int encode(SrsStream* stream);
1202 protected: 1246 protected:
  1247 + virtual int psi_size() = 0;
  1248 + virtual int psi_encode(SrsStream* stream) = 0;
1203 virtual int psi_decode(SrsStream* stream) = 0; 1249 virtual int psi_decode(SrsStream* stream) = 0;
1204 }; 1250 };
1205 1251
@@ -1217,7 +1263,10 @@ public: @@ -1217,7 +1263,10 @@ public:
1217 * Association Table. 1263 * Association Table.
1218 */ 1264 */
1219 int16_t number; // 16bits 1265 int16_t number; // 16bits
1220 - // reserved 3bits 1266 + /**
  1267 + * reverved value, must be '1'
  1268 + */
  1269 + int8_t const1_value; //3bits
1221 /** 1270 /**
1222 * program_map_PID/network_PID 13bits 1271 * program_map_PID/network_PID 13bits
1223 * network_PID - The network_PID is a 13-bit field, which is used only in conjunction with the value of the 1272 * network_PID - The network_PID is a 13-bit field, which is used only in conjunction with the value of the
@@ -1225,10 +1274,15 @@ public: @@ -1225,10 +1274,15 @@ public:
1225 * Information Table. The value of the network_PID field is defined by the user, but shall only take values as specified in 1274 * Information Table. The value of the network_PID field is defined by the user, but shall only take values as specified in
1226 * Table 2-3. The presence of the network_PID is optional. 1275 * Table 2-3. The presence of the network_PID is optional.
1227 */ 1276 */
1228 - int16_t pid; 1277 + int16_t pid; //13bits
1229 public: 1278 public:
1230 - SrsTsPayloadPATProgram(); 1279 + SrsTsPayloadPATProgram(int16_t n = 0, int16_t p = 0);
1231 virtual ~SrsTsPayloadPATProgram(); 1280 virtual ~SrsTsPayloadPATProgram();
  1281 +public:
  1282 + virtual int decode(SrsStream* stream);
  1283 +public:
  1284 + virtual int size();
  1285 + virtual int encode(SrsStream* stream);
1232 }; 1286 };
1233 1287
1234 /** 1288 /**
@@ -1249,7 +1303,10 @@ public: @@ -1249,7 +1303,10 @@ public:
1249 u_int16_t transport_stream_id; //16bits 1303 u_int16_t transport_stream_id; //16bits
1250 1304
1251 // 1B 1305 // 1B
1252 - // 2bits reerverd. 1306 + /**
  1307 + * reverved value, must be '1'
  1308 + */
  1309 + int8_t const1_value; //2bits
1253 /** 1310 /**
1254 * This 5-bit field is the version number of the whole Program Association Table. The version number 1311 * This 5-bit field is the version number of the whole Program Association Table. The version number
1255 * shall be incremented by 1 modulo 32 whenever the definition of the Program Association Table changes. When the 1312 * shall be incremented by 1 modulo 32 whenever the definition of the Program Association Table changes. When the
@@ -1285,8 +1342,11 @@ public: @@ -1285,8 +1342,11 @@ public:
1285 public: 1342 public:
1286 SrsTsPayloadPAT(SrsTsPacket* p); 1343 SrsTsPayloadPAT(SrsTsPacket* p);
1287 virtual ~SrsTsPayloadPAT(); 1344 virtual ~SrsTsPayloadPAT();
1288 -public: 1345 +protected:
1289 virtual int psi_decode(SrsStream* stream); 1346 virtual int psi_decode(SrsStream* stream);
  1347 +protected:
  1348 + virtual int psi_size();
  1349 + virtual int psi_encode(SrsStream* stream);
1290 }; 1350 };
1291 1351
1292 /** 1352 /**
@@ -1303,15 +1363,21 @@ public: @@ -1303,15 +1363,21 @@ public:
1303 SrsTsStream stream_type; //8bits 1363 SrsTsStream stream_type; //8bits
1304 1364
1305 // 2B 1365 // 2B
1306 - // 3bits reserved 1366 + /**
  1367 + * reverved value, must be '1'
  1368 + */
  1369 + int8_t const1_value0; //3bits
1307 /** 1370 /**
1308 * This is a 13-bit field specifying the PID of the Transport Stream packets which carry the associated 1371 * This is a 13-bit field specifying the PID of the Transport Stream packets which carry the associated
1309 * program element. 1372 * program element.
1310 */ 1373 */
1311 int16_t elementary_PID; //13bits 1374 int16_t elementary_PID; //13bits
1312 1375
1313 - // 2B  
1314 - // 4bits reserved 1376 + // (2+x)B
  1377 + /**
  1378 + * reverved value, must be '1'
  1379 + */
  1380 + int8_t const1_value1; //4bits
1315 /** 1381 /**
1316 * This is a 12-bit field, the first two bits of which shall be '00'. The remaining 10 bits specify the number 1382 * This is a 12-bit field, the first two bits of which shall be '00'. The remaining 10 bits specify the number
1317 * of bytes of the descriptors of the associated program element immediately following the ES_info_length field. 1383 * of bytes of the descriptors of the associated program element immediately following the ES_info_length field.
@@ -1319,8 +1385,13 @@ public: @@ -1319,8 +1385,13 @@ public:
1319 int16_t ES_info_length; //12bits 1385 int16_t ES_info_length; //12bits
1320 char* ES_info; //[ES_info_length] bytes. 1386 char* ES_info; //[ES_info_length] bytes.
1321 public: 1387 public:
1322 - SrsTsPayloadPMTESInfo(); 1388 + SrsTsPayloadPMTESInfo(SrsTsStream st = SrsTsStreamReserved, int16_t epid = 0);
1323 virtual ~SrsTsPayloadPMTESInfo(); 1389 virtual ~SrsTsPayloadPMTESInfo();
  1390 +public:
  1391 + virtual int decode(SrsStream* stream);
  1392 +public:
  1393 + virtual int size();
  1394 + virtual int encode(SrsStream* stream);
1324 }; 1395 };
1325 1396
1326 /** 1397 /**
@@ -1349,7 +1420,10 @@ public: @@ -1349,7 +1420,10 @@ public:
1349 u_int16_t program_number; //16bits 1420 u_int16_t program_number; //16bits
1350 1421
1351 // 1B 1422 // 1B
1352 - // 2bits reerverd. 1423 + /**
  1424 + * reverved value, must be '1'
  1425 + */
  1426 + int8_t const1_value0; //2bits
1353 /** 1427 /**
1354 * This 5-bit field is the version number of the TS_program_map_section. The version number shall be 1428 * This 5-bit field is the version number of the TS_program_map_section. The version number shall be
1355 * incremented by 1 modulo 32 when a change in the information carried within the section occurs. Version number refers 1429 * incremented by 1 modulo 32 when a change in the information carried within the section occurs. Version number refers
@@ -1378,17 +1452,20 @@ public: @@ -1378,17 +1452,20 @@ public:
1378 u_int8_t last_section_number; //8bits 1452 u_int8_t last_section_number; //8bits
1379 1453
1380 // 2B 1454 // 2B
1381 - // 2bits reserved. 1455 + /**
  1456 + * reverved value, must be '1'
  1457 + */
  1458 + int8_t const1_value1; //3bits
1382 /** 1459 /**
1383 * This is a 13-bit field indicating the PID of the Transport Stream packets which shall contain the PCR fields 1460 * This is a 13-bit field indicating the PID of the Transport Stream packets which shall contain the PCR fields
1384 * valid for the program specified by program_number. If no PCR is associated with a program definition for private 1461 * valid for the program specified by program_number. If no PCR is associated with a program definition for private
1385 * streams, then this field shall take the value of 0x1FFF. Refer to the semantic definition of PCR in 2.4.3.5 and Table 2-3 1462 * streams, then this field shall take the value of 0x1FFF. Refer to the semantic definition of PCR in 2.4.3.5 and Table 2-3
1386 * for restrictions on the choice of PCR_PID value. 1463 * for restrictions on the choice of PCR_PID value.
1387 */ 1464 */
1388 - int16_t PCR_PID; //16bits 1465 + int16_t PCR_PID; //13bits
1389 1466
1390 // 2B 1467 // 2B
1391 - // 4bits reserved. 1468 + int8_t const1_value2; //4bits
1392 /** 1469 /**
1393 * This is a 12-bit field, the first two bits of which shall be '00'. The remaining 10 bits specify the 1470 * This is a 12-bit field, the first two bits of which shall be '00'. The remaining 10 bits specify the
1394 * number of bytes of the descriptors immediately following the program_info_length field. 1471 * number of bytes of the descriptors immediately following the program_info_length field.
@@ -1401,8 +1478,11 @@ public: @@ -1401,8 +1478,11 @@ public:
1401 public: 1478 public:
1402 SrsTsPayloadPMT(SrsTsPacket* p); 1479 SrsTsPayloadPMT(SrsTsPacket* p);
1403 virtual ~SrsTsPayloadPMT(); 1480 virtual ~SrsTsPayloadPMT();
1404 -public: 1481 +protected:
1405 virtual int psi_decode(SrsStream* stream); 1482 virtual int psi_decode(SrsStream* stream);
  1483 +protected:
  1484 + virtual int psi_size();
  1485 + virtual int psi_encode(SrsStream* stream);
1406 }; 1486 };
1407 1487
1408 /** 1488 /**
@@ -1412,9 +1492,10 @@ public: @@ -1412,9 +1492,10 @@ public:
1412 class SrsTSMuxer 1492 class SrsTSMuxer
1413 { 1493 {
1414 private: 1494 private:
1415 - SrsCodecAudio previous;  
1416 - SrsCodecAudio current; 1495 + SrsCodecVideo vcodec;
  1496 + SrsCodecAudio acodec;
1417 private: 1497 private:
  1498 + SrsTsContext* context;
1418 SrsFileWriter* writer; 1499 SrsFileWriter* writer;
1419 std::string path; 1500 std::string path;
1420 public: 1501 public: