winlin

for #474, decode the sequence header before hls.

@@ -1551,6 +1551,45 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg) @@ -1551,6 +1551,45 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg)
1551 bool is_aac_sequence_header = SrsFlvCodec::audio_is_sequence_header(msg->payload, msg->size); 1551 bool is_aac_sequence_header = SrsFlvCodec::audio_is_sequence_header(msg->payload, msg->size);
1552 bool is_sequence_header = is_aac_sequence_header; 1552 bool is_sequence_header = is_aac_sequence_header;
1553 1553
  1554 + // whether consumer should drop for the duplicated sequence header.
  1555 + bool drop_for_reduce = false;
  1556 + if (is_sequence_header && cache_sh_audio && _srs_config->get_reduce_sequence_header(_req->vhost)) {
  1557 + if (cache_sh_audio->size == msg->size) {
  1558 + drop_for_reduce = srs_bytes_equals(cache_sh_audio->payload, msg->payload, msg->size);
  1559 + srs_warn("drop for reduce sh audio, size=%d", msg->size);
  1560 + }
  1561 + }
  1562 +
  1563 + // cache the sequence header if aac
  1564 + // donot cache the sequence header to gop_cache, return here.
  1565 + if (is_aac_sequence_header) {
  1566 + // parse detail audio codec
  1567 + SrsAvcAacCodec codec;
  1568 + SrsCodecSample sample;
  1569 + if ((ret = codec.audio_aac_demux(msg->payload, msg->size, &sample)) != ERROR_SUCCESS) {
  1570 + srs_error("source codec demux audio failed. ret=%d", ret);
  1571 + return ret;
  1572 + }
  1573 +
  1574 + static int flv_sample_sizes[] = {8, 16, 0};
  1575 + static int flv_sound_types[] = {1, 2, 0};
  1576 +
  1577 + // when got audio stream info.
  1578 + SrsStatistic* stat = SrsStatistic::instance();
  1579 + if ((ret = stat->on_audio_info(_req, SrsCodecAudioAAC, sample.sound_rate, sample.sound_type, codec.aac_object)) != ERROR_SUCCESS) {
  1580 + return ret;
  1581 + }
  1582 +
  1583 + srs_trace("%dB audio sh, codec(%d, profile=%s, %dchannels, %dkbps, %dHZ), "
  1584 + "flv(%dbits, %dchannels, %dHZ)",
  1585 + msg->size, codec.audio_codec_id,
  1586 + srs_codec_aac_object2str(codec.aac_object).c_str(), codec.aac_channels,
  1587 + codec.audio_data_rate / 1000, aac_sample_rates[codec.aac_sample_rate],
  1588 + flv_sample_sizes[sample.sound_size], flv_sound_types[sample.sound_type],
  1589 + flv_sample_rates[sample.sound_rate]);
  1590 + return ret;
  1591 + }
  1592 +
1554 #ifdef SRS_AUTO_HLS 1593 #ifdef SRS_AUTO_HLS
1555 if ((ret = hls->on_audio(msg)) != ERROR_SUCCESS) { 1594 if ((ret = hls->on_audio(msg)) != ERROR_SUCCESS) {
1556 // apply the error strategy for hls. 1595 // apply the error strategy for hls.
@@ -1604,13 +1643,6 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg) @@ -1604,13 +1643,6 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg)
1604 #endif 1643 #endif
1605 1644
1606 // copy to all consumer 1645 // copy to all consumer
1607 - bool drop_for_reduce = false;  
1608 - if (is_sequence_header && cache_sh_audio && _srs_config->get_reduce_sequence_header(_req->vhost)) {  
1609 - if (cache_sh_audio->size == msg->size) {  
1610 - drop_for_reduce = srs_bytes_equals(cache_sh_audio->payload, msg->payload, msg->size);  
1611 - srs_warn("drop for reduce sh audio, size=%d", msg->size);  
1612 - }  
1613 - }  
1614 if (!drop_for_reduce) { 1646 if (!drop_for_reduce) {
1615 for (int i = 0; i < (int)consumers.size(); i++) { 1647 for (int i = 0; i < (int)consumers.size(); i++) {
1616 SrsConsumer* consumer = consumers.at(i); 1648 SrsConsumer* consumer = consumers.at(i);
@@ -1641,37 +1673,6 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg) @@ -1641,37 +1673,6 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg)
1641 srs_freep(cache_sh_audio); 1673 srs_freep(cache_sh_audio);
1642 cache_sh_audio = msg->copy(); 1674 cache_sh_audio = msg->copy();
1643 } 1675 }
1644 -  
1645 - // cache the sequence header if aac  
1646 - // donot cache the sequence header to gop_cache, return here.  
1647 - if (is_aac_sequence_header) {  
1648 - // parse detail audio codec  
1649 - SrsAvcAacCodec codec;  
1650 - SrsCodecSample sample;  
1651 - if ((ret = codec.audio_aac_demux(msg->payload, msg->size, &sample)) != ERROR_SUCCESS) {  
1652 - srs_error("source codec demux audio failed. ret=%d", ret);  
1653 - return ret;  
1654 - }  
1655 -  
1656 - static int flv_sample_sizes[] = {8, 16, 0};  
1657 - static int flv_sound_types[] = {1, 2, 0};  
1658 -  
1659 - // when got audio stream info.  
1660 - SrsStatistic* stat = SrsStatistic::instance();  
1661 - if ((ret = stat->on_audio_info(_req, SrsCodecAudioAAC, sample.sound_rate, sample.sound_type, codec.aac_object)) != ERROR_SUCCESS) {  
1662 - return ret;  
1663 - }  
1664 -  
1665 - srs_trace("%dB audio sh, "  
1666 - "codec(%d, profile=%s, %dchannels, %dkbps, %dHZ), "  
1667 - "flv(%dbits, %dchannels, %dHZ)",  
1668 - msg->size, codec.audio_codec_id,  
1669 - srs_codec_aac_object2str(codec.aac_object).c_str(), codec.aac_channels,  
1670 - codec.audio_data_rate / 1000, aac_sample_rates[codec.aac_sample_rate],  
1671 - flv_sample_sizes[sample.sound_size], flv_sound_types[sample.sound_type],  
1672 - flv_sample_rates[sample.sound_rate]);  
1673 - return ret;  
1674 - }  
1675 1676
1676 // cache the last gop packets 1677 // cache the last gop packets
1677 if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) { 1678 if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) {
@@ -1761,6 +1762,43 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg) @@ -1761,6 +1762,43 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg)
1761 1762
1762 bool is_sequence_header = SrsFlvCodec::video_is_sequence_header(msg->payload, msg->size); 1763 bool is_sequence_header = SrsFlvCodec::video_is_sequence_header(msg->payload, msg->size);
1763 1764
  1765 + // whether consumer should drop for the duplicated sequence header.
  1766 + bool drop_for_reduce = false;
  1767 + if (is_sequence_header && cache_sh_video && _srs_config->get_reduce_sequence_header(_req->vhost)) {
  1768 + if (cache_sh_video->size == msg->size) {
  1769 + drop_for_reduce = srs_bytes_equals(cache_sh_video->payload, msg->payload, msg->size);
  1770 + srs_warn("drop for reduce sh video, size=%d", msg->size);
  1771 + }
  1772 + }
  1773 +
  1774 + // cache the sequence header if h264
  1775 + // donot cache the sequence header to gop_cache, return here.
  1776 + if (is_sequence_header) {
  1777 + srs_freep(cache_sh_video);
  1778 + cache_sh_video = msg->copy();
  1779 +
  1780 + // parse detail audio codec
  1781 + SrsAvcAacCodec codec;
  1782 + SrsCodecSample sample;
  1783 + if ((ret = codec.video_avc_demux(msg->payload, msg->size, &sample)) != ERROR_SUCCESS) {
  1784 + srs_error("source codec demux video failed. ret=%d", ret);
  1785 + return ret;
  1786 + }
  1787 +
  1788 + // when got video stream info.
  1789 + SrsStatistic* stat = SrsStatistic::instance();
  1790 + if ((ret = stat->on_video_info(_req, SrsCodecVideoAVC, codec.avc_profile, codec.avc_level)) != ERROR_SUCCESS) {
  1791 + return ret;
  1792 + }
  1793 +
  1794 + srs_trace("%dB video sh, codec(%d, profile=%s, level=%s, %dx%d, %dkbps, %dfps, %ds)",
  1795 + msg->size, codec.video_codec_id,
  1796 + srs_codec_avc_profile2str(codec.avc_profile).c_str(),
  1797 + srs_codec_avc_level2str(codec.avc_level).c_str(), codec.width, codec.height,
  1798 + codec.video_data_rate / 1000, codec.frame_rate, codec.duration);
  1799 + return ret;
  1800 + }
  1801 +
1764 #ifdef SRS_AUTO_HLS 1802 #ifdef SRS_AUTO_HLS
1765 if ((ret = hls->on_video(msg)) != ERROR_SUCCESS) { 1803 if ((ret = hls->on_video(msg)) != ERROR_SUCCESS) {
1766 // apply the error strategy for hls. 1804 // apply the error strategy for hls.
@@ -1814,13 +1852,6 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg) @@ -1814,13 +1852,6 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg)
1814 #endif 1852 #endif
1815 1853
1816 // copy to all consumer 1854 // copy to all consumer
1817 - bool drop_for_reduce = false;  
1818 - if (is_sequence_header && cache_sh_video && _srs_config->get_reduce_sequence_header(_req->vhost)) {  
1819 - if (cache_sh_video->size == msg->size) {  
1820 - drop_for_reduce = srs_bytes_equals(cache_sh_video->payload, msg->payload, msg->size);  
1821 - srs_warn("drop for reduce sh video, size=%d", msg->size);  
1822 - }  
1823 - }  
1824 if (!drop_for_reduce) { 1855 if (!drop_for_reduce) {
1825 for (int i = 0; i < (int)consumers.size(); i++) { 1856 for (int i = 0; i < (int)consumers.size(); i++) {
1826 SrsConsumer* consumer = consumers.at(i); 1857 SrsConsumer* consumer = consumers.at(i);
@@ -1844,35 +1875,6 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg) @@ -1844,35 +1875,6 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg)
1844 } 1875 }
1845 } 1876 }
1846 1877
1847 - // cache the sequence header if h264  
1848 - // donot cache the sequence header to gop_cache, return here.  
1849 - if (is_sequence_header) {  
1850 - srs_freep(cache_sh_video);  
1851 - cache_sh_video = msg->copy();  
1852 -  
1853 - // parse detail audio codec  
1854 - SrsAvcAacCodec codec;  
1855 - SrsCodecSample sample;  
1856 - if ((ret = codec.video_avc_demux(msg->payload, msg->size, &sample)) != ERROR_SUCCESS) {  
1857 - srs_error("source codec demux video failed. ret=%d", ret);  
1858 - return ret;  
1859 - }  
1860 -  
1861 - // when got video stream info.  
1862 - SrsStatistic* stat = SrsStatistic::instance();  
1863 - if ((ret = stat->on_video_info(_req, SrsCodecVideoAVC, codec.avc_profile, codec.avc_level)) != ERROR_SUCCESS) {  
1864 - return ret;  
1865 - }  
1866 -  
1867 - srs_trace("%dB video sh, "  
1868 - "codec(%d, profile=%s, level=%s, %dx%d, %dkbps, %dfps, %ds)",  
1869 - msg->size, codec.video_codec_id,  
1870 - srs_codec_avc_profile2str(codec.avc_profile).c_str(),  
1871 - srs_codec_avc_level2str(codec.avc_level).c_str(), codec.width, codec.height,  
1872 - codec.video_data_rate / 1000, codec.frame_rate, codec.duration);  
1873 - return ret;  
1874 - }  
1875 -  
1876 // cache the last gop packets 1878 // cache the last gop packets
1877 if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) { 1879 if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) {
1878 srs_error("gop cache msg failed. ret=%d", ret); 1880 srs_error("gop cache msg failed. ret=%d", ret);