winlin

for #738, demux the aac mp4a and esds

@@ -232,6 +232,7 @@ int SrsMp4Box::discovery(SrsBuffer* buf, SrsMp4Box** ppbox) @@ -232,6 +232,7 @@ int SrsMp4Box::discovery(SrsBuffer* buf, SrsMp4Box** ppbox)
232 case SRS_MP4_BOX_AVC1: box = new SrsMp4VisualSampleEntry(); break; 232 case SRS_MP4_BOX_AVC1: box = new SrsMp4VisualSampleEntry(); break;
233 case SRS_MP4_BOX_AVCC: box = new SrsMp4AvccBox(); break; 233 case SRS_MP4_BOX_AVCC: box = new SrsMp4AvccBox(); break;
234 case SRS_MP4_BOX_MP4A: box = new SrsMp4AudioSampleEntry(); break; 234 case SRS_MP4_BOX_MP4A: box = new SrsMp4AudioSampleEntry(); break;
  235 + case SRS_MP4_BOX_ESDS: box = new SrsMp4EsdsBox(); break;
235 default: 236 default:
236 ret = ERROR_MP4_BOX_ILLEGAL_TYPE; 237 ret = ERROR_MP4_BOX_ILLEGAL_TYPE;
237 srs_error("MP4 illegal box type=%d. ret=%d", type, ret); 238 srs_error("MP4 illegal box type=%d. ret=%d", type, ret);
@@ -1721,6 +1722,298 @@ int SrsMp4AudioSampleEntry::decode_header(SrsBuffer* buf) @@ -1721,6 +1722,298 @@ int SrsMp4AudioSampleEntry::decode_header(SrsBuffer* buf)
1721 return ret; 1722 return ret;
1722 } 1723 }
1723 1724
  1725 +SrsMp4BaseDescriptor::SrsMp4BaseDescriptor()
  1726 +{
  1727 + tag = SRS_MP4_ES_TAG_ES_forbidden;
  1728 +}
  1729 +
  1730 +SrsMp4BaseDescriptor::~SrsMp4BaseDescriptor()
  1731 +{
  1732 +}
  1733 +
  1734 +int SrsMp4BaseDescriptor::nb_bytes()
  1735 +{
  1736 + // 1 byte tag.
  1737 + int size = 1;
  1738 +
  1739 + // 1-3 bytes size.
  1740 + uint32_t length = nb_payload();
  1741 + if (length > 0x1fffff) {
  1742 + size += 4;
  1743 + } else if (length > 0x3fff) {
  1744 + size += 3;
  1745 + } else if (length > 0x7f) {
  1746 + size += 2;
  1747 + } else {
  1748 + size += 1;
  1749 + }
  1750 +
  1751 + // length bytes payload.
  1752 + size += length;
  1753 +
  1754 + return size;
  1755 +}
  1756 +
  1757 +int SrsMp4BaseDescriptor::encode(SrsBuffer* buf)
  1758 +{
  1759 + int ret = ERROR_SUCCESS;
  1760 +
  1761 + int size = nb_bytes();
  1762 + if (!buf->require(size)) {
  1763 + ret = ERROR_MP4_BOX_REQUIRE_SPACE;
  1764 + srs_error("MP4 ES requires %d bytes space. ret=%d", size, ret);
  1765 + return ret;
  1766 + }
  1767 +
  1768 + buf->write_1bytes((uint8_t)tag);
  1769 +
  1770 + // As an expandable class the size of each class instance in bytes is encoded and accessible
  1771 + // through the instance variable sizeOfInstance (see 8.3.3).
  1772 + uint32_t length = nb_payload(); // bit(8) to bit(32)
  1773 +
  1774 + return ret;
  1775 +}
  1776 +
  1777 +int SrsMp4BaseDescriptor::decode(SrsBuffer* buf)
  1778 +{
  1779 + int ret = ERROR_SUCCESS;
  1780 +
  1781 + int size = nb_bytes();
  1782 + if (!buf->require(size)) {
  1783 + ret = ERROR_MP4_BOX_REQUIRE_SPACE;
  1784 + srs_error("MP4 ES requires %d bytes space. ret=%d", size, ret);
  1785 + return ret;
  1786 + }
  1787 +
  1788 + tag = (SRS_MP4_ES_TAG_ES)buf->read_1bytes();
  1789 + return ret;
  1790 +}
  1791 +
  1792 +SrsMp4DecoderConfigDescriptor::SrsMp4DecoderConfigDescriptor()
  1793 +{
  1794 + tag = SRS_MP4_ES_TAG_ES_DecoderConfigDescrTag;
  1795 +}
  1796 +
  1797 +SrsMp4DecoderConfigDescriptor::~SrsMp4DecoderConfigDescriptor()
  1798 +{
  1799 +}
  1800 +
  1801 +uint32_t SrsMp4DecoderConfigDescriptor::nb_payload()
  1802 +{
  1803 + return 0;
  1804 +}
  1805 +
  1806 +int SrsMp4DecoderConfigDescriptor::encode_payload(SrsBuffer* buf)
  1807 +{
  1808 + int ret = ERROR_SUCCESS;
  1809 + return ret;
  1810 +}
  1811 +
  1812 +int SrsMp4DecoderConfigDescriptor::decode_payload(SrsBuffer* buf)
  1813 +{
  1814 + int ret = ERROR_SUCCESS;
  1815 + return ret;
  1816 +}
  1817 +
  1818 +SrsMp4SLConfigDescriptor::SrsMp4SLConfigDescriptor()
  1819 +{
  1820 + tag = SRS_MP4_ES_TAG_ES_SLConfigDescrTag;
  1821 +}
  1822 +
  1823 +SrsMp4SLConfigDescriptor::~SrsMp4SLConfigDescriptor()
  1824 +{
  1825 +}
  1826 +
  1827 +uint32_t SrsMp4SLConfigDescriptor::nb_payload()
  1828 +{
  1829 + return 0;
  1830 +}
  1831 +
  1832 +int SrsMp4SLConfigDescriptor::encode_payload(SrsBuffer* buf)
  1833 +{
  1834 + int ret = ERROR_SUCCESS;
  1835 + return ret;
  1836 +}
  1837 +
  1838 +int SrsMp4SLConfigDescriptor::decode_payload(SrsBuffer* buf)
  1839 +{
  1840 + int ret = ERROR_SUCCESS;
  1841 + return ret;
  1842 +}
  1843 +
  1844 +SrsMp4ES_Descriptor::SrsMp4ES_Descriptor()
  1845 +{
  1846 + tag = SRS_MP4_ES_TAG_ES_DescrTag;
  1847 + streamDependenceFlag = URL_Flag = OCRstreamFlag = 0;
  1848 + URLlength = 0;
  1849 + URLstring = NULL;
  1850 +}
  1851 +
  1852 +SrsMp4ES_Descriptor::~SrsMp4ES_Descriptor()
  1853 +{
  1854 + srs_freepa(URLstring);
  1855 +}
  1856 +
  1857 +uint32_t SrsMp4ES_Descriptor::nb_payload()
  1858 +{
  1859 + int size = 2 +1;
  1860 + size += streamDependenceFlag? 2:0;
  1861 + if (URL_Flag) {
  1862 + size += 1 + URLlength;
  1863 + }
  1864 + size += OCRstreamFlag? 2:0;
  1865 + size += decConfigDescr.nb_bytes() +slConfigDescr.nb_bytes();
  1866 + return size;
  1867 +}
  1868 +
  1869 +int SrsMp4ES_Descriptor::encode_payload(SrsBuffer* buf)
  1870 +{
  1871 + int ret = ERROR_SUCCESS;
  1872 +
  1873 + buf->write_2bytes(ES_ID);
  1874 +
  1875 + uint8_t v = streamPriority & 0x1f;
  1876 + v |= (streamDependenceFlag & 0x01) << 7;
  1877 + v |= (URL_Flag & 0x01) << 6;
  1878 + v |= (OCRstreamFlag & 0x01) << 5;
  1879 + buf->write_1bytes(v);
  1880 +
  1881 + if (streamDependenceFlag) {
  1882 + buf->write_2bytes(dependsOn_ES_ID);
  1883 + }
  1884 +
  1885 + if (URL_Flag && URLlength) {
  1886 + buf->write_1bytes(URLlength);
  1887 + buf->write_bytes((char*)URLstring, URLlength);
  1888 + }
  1889 +
  1890 + if (OCRstreamFlag) {
  1891 + buf->write_2bytes(OCR_ES_Id);
  1892 + }
  1893 +
  1894 + if ((ret = decConfigDescr.encode(buf)) != ERROR_SUCCESS) {
  1895 + return ret;
  1896 + }
  1897 +
  1898 + if ((ret = slConfigDescr.encode(buf)) != ERROR_SUCCESS) {
  1899 + return ret;
  1900 + }
  1901 +
  1902 + return ret;
  1903 +}
  1904 +
  1905 +int SrsMp4ES_Descriptor::decode_payload(SrsBuffer* buf)
  1906 +{
  1907 + int ret = ERROR_SUCCESS;
  1908 +
  1909 + ES_ID = buf->read_2bytes();
  1910 +
  1911 + uint8_t v = buf->read_1bytes();
  1912 + streamPriority = v & 0x1f;
  1913 + streamDependenceFlag = (v >> 7) & 0x01;
  1914 + URL_Flag = (v >> 6) & 0x01;
  1915 + OCRstreamFlag = (v >> 5) & 0x01;
  1916 +
  1917 + if (streamDependenceFlag) {
  1918 + if (!buf->require(2)) {
  1919 + ret = ERROR_MP4_BOX_REQUIRE_SPACE;
  1920 + srs_error("MP4 ES requires 2 bytes space. ret=%d", ret);
  1921 + return ret;
  1922 + }
  1923 + dependsOn_ES_ID = buf->read_2bytes();
  1924 + }
  1925 +
  1926 + if (URL_Flag) {
  1927 + if (!buf->require(1)) {
  1928 + ret = ERROR_MP4_BOX_REQUIRE_SPACE;
  1929 + srs_error("MP4 ES requires 1 byte space. ret=%d", ret);
  1930 + return ret;
  1931 + }
  1932 + URLlength = buf->read_1bytes();
  1933 +
  1934 + if (!buf->require(URLlength)) {
  1935 + ret = ERROR_MP4_BOX_REQUIRE_SPACE;
  1936 + srs_error("MP4 ES requires %d bytes space. ret=%d", URLlength, ret);
  1937 + return ret;
  1938 + }
  1939 + URLstring = new uint8_t[URLlength];
  1940 + buf->read_bytes((char*)URLstring, URLlength);
  1941 + }
  1942 +
  1943 + if (OCRstreamFlag) {
  1944 + if (!buf->require(2)) {
  1945 + ret = ERROR_MP4_BOX_REQUIRE_SPACE;
  1946 + srs_error("MP4 ES requires 2 bytes space. ret=%d", ret);
  1947 + return ret;
  1948 + }
  1949 + OCR_ES_Id = buf->read_2bytes();
  1950 + }
  1951 +
  1952 + if ((ret = decConfigDescr.decode(buf)) != ERROR_SUCCESS) {
  1953 + return ret;
  1954 + }
  1955 +
  1956 + if ((ret = slConfigDescr.decode(buf)) != ERROR_SUCCESS) {
  1957 + return ret;
  1958 + }
  1959 +
  1960 + return ret;
  1961 +}
  1962 +
  1963 +SrsMp4EsdsBox::SrsMp4EsdsBox()
  1964 +{
  1965 + type = SRS_MP4_BOX_ESDS;
  1966 + es = new SrsMp4ES_Descriptor();
  1967 +}
  1968 +
  1969 +SrsMp4EsdsBox::~SrsMp4EsdsBox()
  1970 +{
  1971 + srs_freep(es);
  1972 +}
  1973 +
  1974 +int SrsMp4EsdsBox::nb_header()
  1975 +{
  1976 + return SrsMp4FullBox::nb_header() + es->nb_bytes();
  1977 +}
  1978 +
  1979 +int SrsMp4EsdsBox::encode_header(SrsBuffer* buf)
  1980 +{
  1981 + int ret = ERROR_SUCCESS;
  1982 +
  1983 + if ((ret = SrsMp4FullBox::encode_header(buf)) != ERROR_SUCCESS) {
  1984 + return ret;
  1985 + }
  1986 +
  1987 + int left = left_space(buf);
  1988 + SrsBuffer buffer(buf->data() + buf->pos(), left);
  1989 + if ((ret = es->encode(&buffer)) != ERROR_SUCCESS) {
  1990 + return ret;
  1991 + }
  1992 +
  1993 + buf->skip(buffer.pos());
  1994 +
  1995 + return ret;
  1996 +}
  1997 +
  1998 +int SrsMp4EsdsBox::decode_header(SrsBuffer* buf)
  1999 +{
  2000 + int ret = ERROR_SUCCESS;
  2001 +
  2002 + if ((ret = SrsMp4FullBox::decode_header(buf)) != ERROR_SUCCESS) {
  2003 + return ret;
  2004 + }
  2005 +
  2006 + int left = left_space(buf);
  2007 + SrsBuffer buffer(buf->data() + buf->pos(), left);
  2008 + if ((ret = es->decode(&buffer)) != ERROR_SUCCESS) {
  2009 + return ret;
  2010 + }
  2011 +
  2012 + buf->skip(buffer.pos());
  2013 +
  2014 + return ret;
  2015 +}
  2016 +
1724 SrsMp4SampleDescriptionBox::SrsMp4SampleDescriptionBox() 2017 SrsMp4SampleDescriptionBox::SrsMp4SampleDescriptionBox()
1725 { 2018 {
1726 type = SRS_MP4_BOX_STSD; 2019 type = SRS_MP4_BOX_STSD;
@@ -724,6 +724,159 @@ protected: @@ -724,6 +724,159 @@ protected:
724 virtual int decode_header(SrsBuffer* buf); 724 virtual int decode_header(SrsBuffer* buf);
725 }; 725 };
726 726
  727 +// Table 1 — List of Class Tags for Descriptors
  728 +// ISO_IEC_14496-1-System-2010.pdf, page 31
  729 +enum SRS_MP4_ES_TAG_ES {
  730 + SRS_MP4_ES_TAG_ES_forbidden = 0x00,
  731 + SRS_MP4_ES_TAG_ES_ObjectDescrTag = 0x01,
  732 + SRS_MP4_ES_TAG_ES_InitialObjectDescrTag = 0x02,
  733 + SRS_MP4_ES_TAG_ES_DescrTag = 0x03,
  734 + SRS_MP4_ES_TAG_ES_DecoderConfigDescrTag = 0x04,
  735 + SRS_MP4_ES_TAG_ES_DecSpecificInfoTag = 0x05,
  736 + SRS_MP4_ES_TAG_ES_SLConfigDescrTag = 0x06,
  737 + SRS_MP4_ES_TAG_ES_ExtSLConfigDescrTag = 0x064,
  738 +};
  739 +
  740 +/**
  741 + * 7.2.2.2 BaseDescriptor
  742 + * ISO_IEC_14496-1-System-2010.pdf, page 32
  743 + */
  744 +class SrsMp4BaseDescriptor : public ISrsCodec
  745 +{
  746 +public:
  747 + // The values of the class tags are
  748 + // defined in Table 2. As an expandable class the size of each class instance in bytes is encoded and accessible
  749 + // through the instance variable sizeOfInstance (see 8.3.3).
  750 + SRS_MP4_ES_TAG_ES tag; // bit(8)
  751 +public:
  752 + SrsMp4BaseDescriptor();
  753 + virtual ~SrsMp4BaseDescriptor();
  754 +// Interface ISrsCodec
  755 +public:
  756 + virtual int nb_bytes();
  757 + virtual int encode(SrsBuffer* buf);
  758 + virtual int decode(SrsBuffer* buf);
  759 +protected:
  760 + virtual uint32_t nb_payload() = 0;
  761 + virtual int encode_payload(SrsBuffer* buf) = 0;
  762 + virtual int decode_payload(SrsBuffer* buf) = 0;
  763 +};
  764 +
  765 +/**
  766 + * 7.2.6.6 DecoderConfigDescriptor
  767 + * ISO_IEC_14496-1-System-2010.pdf, page 48
  768 + */
  769 +class SrsMp4DecoderConfigDescriptor : public SrsMp4BaseDescriptor
  770 +{
  771 +public:
  772 + uint8_t objectTypeIndication;
  773 + uint8_t streamType; // bit(6)
  774 + uint8_t upStream; // bit(1)
  775 + uint8_t reserved; // bit(1)
  776 + uint32_t bufferSizeDB; // bit(24)
  777 + uint32_t maxBitrate;
  778 + uint32_t avgBitrate;
  779 +public:
  780 + SrsMp4DecoderConfigDescriptor();
  781 + virtual ~SrsMp4DecoderConfigDescriptor();
  782 +protected:
  783 + virtual uint32_t nb_payload();
  784 + virtual int encode_payload(SrsBuffer* buf);
  785 + virtual int decode_payload(SrsBuffer* buf);
  786 +};
  787 +
  788 +/**
  789 + * 7.3.2.3 SL Packet Header Configuration
  790 + * ISO_IEC_14496-1-System-2010.pdf, page 92
  791 + */
  792 +class SrsMp4SLConfigDescriptor : public SrsMp4BaseDescriptor
  793 +{
  794 +public:
  795 + uint8_t predefined;
  796 + // if (predefined==0)
  797 + uint8_t useAccessUnitStartFlag; // bit(1)
  798 + uint8_t useAccessUnitEndFlag; // bit(1)
  799 + uint8_t useRandomAccessPointFlag; // bit(1)
  800 + uint8_t hasRandomAccessUnitsOnlyFlag; // bit(1)
  801 + uint8_t usePaddingFlag; // bit(1)
  802 + uint8_t useTimeStampsFlag; // bit(1)
  803 + uint8_t useIdleFlag; // bit(1)
  804 + uint8_t durationFlag; // bit(1)
  805 + uint32_t timeStampResolution;
  806 + uint32_t OCRResolution;
  807 + uint8_t timeStampLength; // must be ≤ 64
  808 + uint8_t OCRLength; // must be ≤ 64
  809 + uint8_t AU_Length; // must be ≤ 32
  810 + uint8_t instantBitrateLength;
  811 + uint8_t degradationPriorityLength; // bit(4)
  812 + uint8_t AU_seqNumLength; // bit(5) // must be ≤ 16
  813 + uint8_t packetSeqNumLength; // bit(5) // must be ≤ 16
  814 + uint8_t reserved; // bit(2) // 0b11
  815 + // if (durationFlag)
  816 + uint32_t timeScale;
  817 + uint16_t accessUnitDuration;
  818 + uint16_t compositionUnitDuration;
  819 + // if (!useTimeStampsFlag)
  820 + uint64_t startDecodingTimeStamp; // bit(timeStampLength)
  821 + uint64_t startCompositionTimeStamp; // bit(timeStampLength)
  822 +public:
  823 + SrsMp4SLConfigDescriptor();
  824 + virtual ~SrsMp4SLConfigDescriptor();
  825 +protected:
  826 + virtual uint32_t nb_payload();
  827 + virtual int encode_payload(SrsBuffer* buf);
  828 + virtual int decode_payload(SrsBuffer* buf);
  829 +};
  830 +
  831 +/**
  832 + * 7.2.6.5 ES_Descriptor
  833 + * ISO_IEC_14496-1-System-2010.pdf, page 47
  834 + */
  835 +class SrsMp4ES_Descriptor : public SrsMp4BaseDescriptor
  836 +{
  837 +public:
  838 + uint16_t ES_ID;
  839 + uint8_t streamDependenceFlag; // bit(1)
  840 + uint8_t URL_Flag; // bit(1)
  841 + uint8_t OCRstreamFlag; // bit(1)
  842 + uint8_t streamPriority; // bit(5)
  843 + // if (streamDependenceFlag)
  844 + uint16_t dependsOn_ES_ID;
  845 + // if (URL_Flag)
  846 + uint8_t URLlength;
  847 + uint8_t* URLstring;
  848 + // if (OCRstreamFlag)
  849 + uint16_t OCR_ES_Id;
  850 + SrsMp4DecoderConfigDescriptor decConfigDescr;
  851 + SrsMp4SLConfigDescriptor slConfigDescr;
  852 +public:
  853 + SrsMp4ES_Descriptor();
  854 + virtual ~SrsMp4ES_Descriptor();
  855 +protected:
  856 + virtual uint32_t nb_payload();
  857 + virtual int encode_payload(SrsBuffer* buf);
  858 + virtual int decode_payload(SrsBuffer* buf);
  859 +};
  860 +
  861 +/**
  862 + * 5.6 Sample Description Boxes
  863 + * Elementary Stream Descriptors (esds)
  864 + * ISO_IEC_14496-14-MP4-2003.pdf, page 15
  865 + * @see http://www.mp4ra.org/codecs.html
  866 + */
  867 +class SrsMp4EsdsBox : public SrsMp4FullBox
  868 +{
  869 +public:
  870 + SrsMp4ES_Descriptor* es;
  871 +public:
  872 + SrsMp4EsdsBox();
  873 + virtual ~SrsMp4EsdsBox();
  874 +protected:
  875 + virtual int nb_header();
  876 + virtual int encode_header(SrsBuffer* buf);
  877 + virtual int decode_header(SrsBuffer* buf);
  878 +};
  879 +
727 /** 880 /**
728 * 8.5.2 Sample Description Box (stsd), for Audio/Video. 881 * 8.5.2 Sample Description Box (stsd), for Audio/Video.
729 * ISO_IEC_14496-12-base-format-2012.pdf, page 40 882 * ISO_IEC_14496-12-base-format-2012.pdf, page 40