正在显示
5 个修改的文件
包含
814 行增加
和
313 行删除
| @@ -313,7 +313,7 @@ int SrsHlsMuxer::update_acodec(SrsCodecAudio ac) | @@ -313,7 +313,7 @@ int SrsHlsMuxer::update_acodec(SrsCodecAudio ac) | ||
| 313 | return current->muxer->update_acodec(ac); | 313 | return current->muxer->update_acodec(ac); |
| 314 | } | 314 | } |
| 315 | 315 | ||
| 316 | -int SrsHlsMuxer::flush_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab) | 316 | +int SrsHlsMuxer::flush_audio(SrsTsCache* cache) |
| 317 | { | 317 | { |
| 318 | int ret = ERROR_SUCCESS; | 318 | int ret = ERROR_SUCCESS; |
| 319 | 319 | ||
| @@ -323,24 +323,24 @@ int SrsHlsMuxer::flush_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab) | @@ -323,24 +323,24 @@ int SrsHlsMuxer::flush_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab) | ||
| 323 | return ret; | 323 | return ret; |
| 324 | } | 324 | } |
| 325 | 325 | ||
| 326 | - if (ab->length() <= 0) { | 326 | + if (!cache->audio || cache->audio->payload->length() <= 0) { |
| 327 | return ret; | 327 | return ret; |
| 328 | } | 328 | } |
| 329 | 329 | ||
| 330 | // update the duration of segment. | 330 | // update the duration of segment. |
| 331 | - current->update_duration(af->pts); | 331 | + current->update_duration(cache->audio->pts); |
| 332 | 332 | ||
| 333 | - if ((ret = current->muxer->write_audio(af, ab)) != ERROR_SUCCESS) { | 333 | + if ((ret = current->muxer->write_audio(cache->audio)) != ERROR_SUCCESS) { |
| 334 | return ret; | 334 | return ret; |
| 335 | } | 335 | } |
| 336 | 336 | ||
| 337 | - // write success, clear and free the buffer | ||
| 338 | - ab->erase(ab->length()); | 337 | + // write success, clear and free the msg |
| 338 | + srs_freep(cache->audio); | ||
| 339 | 339 | ||
| 340 | return ret; | 340 | return ret; |
| 341 | } | 341 | } |
| 342 | 342 | ||
| 343 | -int SrsHlsMuxer::flush_video(SrsMpegtsFrame* /*af*/, SrsSimpleBuffer* /*ab*/, SrsMpegtsFrame* vf, SrsSimpleBuffer* vb) | 343 | +int SrsHlsMuxer::flush_video(SrsTsCache* cache) |
| 344 | { | 344 | { |
| 345 | int ret = ERROR_SUCCESS; | 345 | int ret = ERROR_SUCCESS; |
| 346 | 346 | ||
| @@ -350,17 +350,21 @@ int SrsHlsMuxer::flush_video(SrsMpegtsFrame* /*af*/, SrsSimpleBuffer* /*ab*/, Sr | @@ -350,17 +350,21 @@ int SrsHlsMuxer::flush_video(SrsMpegtsFrame* /*af*/, SrsSimpleBuffer* /*ab*/, Sr | ||
| 350 | return ret; | 350 | return ret; |
| 351 | } | 351 | } |
| 352 | 352 | ||
| 353 | + if (!cache->video || cache->video->payload->length() <= 0) { | ||
| 354 | + return ret; | ||
| 355 | + } | ||
| 356 | + | ||
| 353 | srs_assert(current); | 357 | srs_assert(current); |
| 354 | 358 | ||
| 355 | // update the duration of segment. | 359 | // update the duration of segment. |
| 356 | - current->update_duration(vf->dts); | 360 | + current->update_duration(cache->video->dts); |
| 357 | 361 | ||
| 358 | - if ((ret = current->muxer->write_video(vf, vb)) != ERROR_SUCCESS) { | 362 | + if ((ret = current->muxer->write_video(cache->video)) != ERROR_SUCCESS) { |
| 359 | return ret; | 363 | return ret; |
| 360 | } | 364 | } |
| 361 | 365 | ||
| 362 | - // write success, clear and free the buffer | ||
| 363 | - vb->erase(vb->length()); | 366 | + // write success, clear and free the msg |
| 367 | + srs_freep(cache->video); | ||
| 364 | 368 | ||
| 365 | return ret; | 369 | return ret; |
| 366 | } | 370 | } |
| @@ -649,7 +653,7 @@ int SrsHlsCache::on_unpublish(SrsHlsMuxer* muxer) | @@ -649,7 +653,7 @@ int SrsHlsCache::on_unpublish(SrsHlsMuxer* muxer) | ||
| 649 | { | 653 | { |
| 650 | int ret = ERROR_SUCCESS; | 654 | int ret = ERROR_SUCCESS; |
| 651 | 655 | ||
| 652 | - if ((ret = muxer->flush_audio(cache->af, cache->ab)) != ERROR_SUCCESS) { | 656 | + if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) { |
| 653 | srs_error("m3u8 muxer flush audio failed. ret=%d", ret); | 657 | srs_error("m3u8 muxer flush audio failed. ret=%d", ret); |
| 654 | return ret; | 658 | return ret; |
| 655 | } | 659 | } |
| @@ -682,8 +686,8 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | @@ -682,8 +686,8 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | ||
| 682 | } | 686 | } |
| 683 | 687 | ||
| 684 | // flush if buffer exceed max size. | 688 | // flush if buffer exceed max size. |
| 685 | - if (cache->ab->length() > SRS_AUTO_HLS_AUDIO_CACHE_SIZE) { | ||
| 686 | - if ((ret = muxer->flush_audio(cache->af, cache->ab)) != ERROR_SUCCESS) { | 689 | + if (cache->audio->payload->length() > SRS_AUTO_HLS_AUDIO_CACHE_SIZE) { |
| 690 | + if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) { | ||
| 687 | return ret; | 691 | return ret; |
| 688 | } | 692 | } |
| 689 | } | 693 | } |
| @@ -692,8 +696,8 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | @@ -692,8 +696,8 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | ||
| 692 | // in ms, audio delay to flush the audios. | 696 | // in ms, audio delay to flush the audios. |
| 693 | int64_t audio_delay = SRS_CONF_DEFAULT_AAC_DELAY; | 697 | int64_t audio_delay = SRS_CONF_DEFAULT_AAC_DELAY; |
| 694 | // flush if audio delay exceed | 698 | // flush if audio delay exceed |
| 695 | - if (pts - cache->audio_buffer_start_pts > audio_delay * 90) { | ||
| 696 | - if ((ret = muxer->flush_audio(cache->af, cache->ab)) != ERROR_SUCCESS) { | 699 | + if (pts - cache->audio->start_pts > audio_delay * 90) { |
| 700 | + if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) { | ||
| 697 | return ret; | 701 | return ret; |
| 698 | } | 702 | } |
| 699 | } | 703 | } |
| @@ -707,7 +711,7 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | @@ -707,7 +711,7 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | ||
| 707 | // we use absolutely overflow of segment to make jwplayer/ffplay happy | 711 | // we use absolutely overflow of segment to make jwplayer/ffplay happy |
| 708 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/151#issuecomment-71155184 | 712 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/151#issuecomment-71155184 |
| 709 | if (muxer->is_segment_absolutely_overflow()) { | 713 | if (muxer->is_segment_absolutely_overflow()) { |
| 710 | - if ((ret = reap_segment("audio", muxer, cache->af->pts)) != ERROR_SUCCESS) { | 714 | + if ((ret = reap_segment("audio", muxer, cache->audio->pts)) != ERROR_SUCCESS) { |
| 711 | return ret; | 715 | return ret; |
| 712 | } | 716 | } |
| 713 | } | 717 | } |
| @@ -728,14 +732,14 @@ int SrsHlsCache::write_video(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | @@ -728,14 +732,14 @@ int SrsHlsCache::write_video(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t | ||
| 728 | // 1. base on gop. | 732 | // 1. base on gop. |
| 729 | // 2. some gops duration overflow. | 733 | // 2. some gops duration overflow. |
| 730 | if (sample->frame_type == SrsCodecVideoAVCFrameKeyFrame && muxer->is_segment_overflow()) { | 734 | if (sample->frame_type == SrsCodecVideoAVCFrameKeyFrame && muxer->is_segment_overflow()) { |
| 731 | - if ((ret = reap_segment("video", muxer, cache->vf->dts)) != ERROR_SUCCESS) { | 735 | + if ((ret = reap_segment("video", muxer, cache->video->dts)) != ERROR_SUCCESS) { |
| 732 | return ret; | 736 | return ret; |
| 733 | } | 737 | } |
| 734 | return ret; | 738 | return ret; |
| 735 | } | 739 | } |
| 736 | 740 | ||
| 737 | // flush video when got one | 741 | // flush video when got one |
| 738 | - if ((ret = muxer->flush_video(cache->af, cache->ab, cache->vf, cache->vb)) != ERROR_SUCCESS) { | 742 | + if ((ret = muxer->flush_video(cache)) != ERROR_SUCCESS) { |
| 739 | srs_error("m3u8 muxer flush video failed. ret=%d", ret); | 743 | srs_error("m3u8 muxer flush video failed. ret=%d", ret); |
| 740 | return ret; | 744 | return ret; |
| 741 | } | 745 | } |
| @@ -761,7 +765,7 @@ int SrsHlsCache::reap_segment(string log_desc, SrsHlsMuxer* muxer, int64_t segme | @@ -761,7 +765,7 @@ int SrsHlsCache::reap_segment(string log_desc, SrsHlsMuxer* muxer, int64_t segme | ||
| 761 | // TODO: fresh segment begin with audio or video? | 765 | // TODO: fresh segment begin with audio or video? |
| 762 | 766 | ||
| 763 | // segment open, flush video first. | 767 | // segment open, flush video first. |
| 764 | - if ((ret = muxer->flush_video(cache->af, cache->ab, cache->vf, cache->vb)) != ERROR_SUCCESS) { | 768 | + if ((ret = muxer->flush_video(cache)) != ERROR_SUCCESS) { |
| 765 | srs_error("m3u8 muxer flush video failed. ret=%d", ret); | 769 | srs_error("m3u8 muxer flush video failed. ret=%d", ret); |
| 766 | return ret; | 770 | return ret; |
| 767 | } | 771 | } |
| @@ -769,7 +773,7 @@ int SrsHlsCache::reap_segment(string log_desc, SrsHlsMuxer* muxer, int64_t segme | @@ -769,7 +773,7 @@ int SrsHlsCache::reap_segment(string log_desc, SrsHlsMuxer* muxer, int64_t segme | ||
| 769 | // segment open, flush the audio. | 773 | // segment open, flush the audio. |
| 770 | // @see: ngx_rtmp_hls_open_fragment | 774 | // @see: ngx_rtmp_hls_open_fragment |
| 771 | /* start fragment with audio to make iPhone happy */ | 775 | /* start fragment with audio to make iPhone happy */ |
| 772 | - if ((ret = muxer->flush_audio(cache->af, cache->ab)) != ERROR_SUCCESS) { | 776 | + if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) { |
| 773 | srs_error("m3u8 muxer flush audio failed. ret=%d", ret); | 777 | srs_error("m3u8 muxer flush audio failed. ret=%d", ret); |
| 774 | return ret; | 778 | return ret; |
| 775 | } | 779 | } |
| @@ -42,7 +42,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -42,7 +42,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 42 | 42 | ||
| 43 | class SrsSharedPtrMessage; | 43 | class SrsSharedPtrMessage; |
| 44 | class SrsCodecSample; | 44 | class SrsCodecSample; |
| 45 | -class SrsMpegtsFrame; | ||
| 46 | class SrsAmf0Object; | 45 | class SrsAmf0Object; |
| 47 | class SrsRtmpJitter; | 46 | class SrsRtmpJitter; |
| 48 | class SrsTSMuxer; | 47 | class SrsTSMuxer; |
| @@ -55,6 +54,7 @@ class SrsSimpleBuffer; | @@ -55,6 +54,7 @@ class SrsSimpleBuffer; | ||
| 55 | class SrsTsAacJitter; | 54 | class SrsTsAacJitter; |
| 56 | class SrsTsCache; | 55 | class SrsTsCache; |
| 57 | class SrsHlsSegment; | 56 | class SrsHlsSegment; |
| 57 | +class SrsTsCache; | ||
| 58 | 58 | ||
| 59 | /** | 59 | /** |
| 60 | * the handler for hls event. | 60 | * the handler for hls event. |
| @@ -224,8 +224,8 @@ public: | @@ -224,8 +224,8 @@ public: | ||
| 224 | virtual bool is_segment_absolutely_overflow(); | 224 | virtual bool is_segment_absolutely_overflow(); |
| 225 | public: | 225 | public: |
| 226 | virtual int update_acodec(SrsCodecAudio ac); | 226 | virtual int update_acodec(SrsCodecAudio ac); |
| 227 | - virtual int flush_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab); | ||
| 228 | - virtual int flush_video(SrsMpegtsFrame* af, SrsSimpleBuffer* ab, SrsMpegtsFrame* vf, SrsSimpleBuffer* vb); | 227 | + virtual int flush_audio(SrsTsCache* cache); |
| 228 | + virtual int flush_video(SrsTsCache* cache); | ||
| 229 | /** | 229 | /** |
| 230 | * close segment(ts). | 230 | * close segment(ts). |
| 231 | * @param log_desc the description for log. | 231 | * @param log_desc the description for log. |
| @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 31 | // current release version | 31 | // current release version |
| 32 | #define VERSION_MAJOR 2 | 32 | #define VERSION_MAJOR 2 |
| 33 | #define VERSION_MINOR 0 | 33 | #define VERSION_MINOR 0 |
| 34 | -#define VERSION_REVISION 116 | 34 | +#define VERSION_REVISION 117 |
| 35 | 35 | ||
| 36 | // server info. | 36 | // server info. |
| 37 | #define RTMP_SIG_SRS_KEY "SRS" | 37 | #define RTMP_SIG_SRS_KEY "SRS" |
| @@ -50,14 +50,11 @@ using namespace std; | @@ -50,14 +50,11 @@ using namespace std; | ||
| 50 | #define _SRS_AAC_SAMPLE_SIZE 1024 | 50 | #define _SRS_AAC_SAMPLE_SIZE 1024 |
| 51 | 51 | ||
| 52 | // the mpegts header specifed the video/audio pid. | 52 | // the mpegts header specifed the video/audio pid. |
| 53 | -#define TS_VIDEO_PID 256 | ||
| 54 | -#define TS_AUDIO_PID 257 | ||
| 55 | - | ||
| 56 | -// ts aac stream id. | ||
| 57 | -#define TS_AUDIO_AAC 0xc0 | ||
| 58 | -#define TS_AUDIO_MP3 0x04 | ||
| 59 | -// ts avc stream id. | ||
| 60 | -#define TS_VIDEO_AVC 0xe0 | 53 | +#define TS_PMT_NUMBER 1 |
| 54 | +#define TS_PMT_PID 0x100 | ||
| 55 | +#define TS_VIDEO_AVC_PID 0x101 | ||
| 56 | +#define TS_AUDIO_AAC_PID 0x102 | ||
| 57 | +#define TS_AUDIO_MP3_PID 0x103 | ||
| 61 | 58 | ||
| 62 | /** | 59 | /** |
| 63 | * the public data, event HLS disable, others can use it. | 60 | * the public data, event HLS disable, others can use it. |
| @@ -420,6 +417,7 @@ SrsTsChannel::SrsTsChannel() | @@ -420,6 +417,7 @@ SrsTsChannel::SrsTsChannel() | ||
| 420 | apply = SrsTsPidApplyReserved; | 417 | apply = SrsTsPidApplyReserved; |
| 421 | stream = SrsTsStreamReserved; | 418 | stream = SrsTsStreamReserved; |
| 422 | msg = NULL; | 419 | msg = NULL; |
| 420 | + continuity_counter = 0; | ||
| 423 | } | 421 | } |
| 424 | 422 | ||
| 425 | SrsTsChannel::~SrsTsChannel() | 423 | SrsTsChannel::~SrsTsChannel() |
| @@ -437,6 +435,9 @@ SrsTsMessage::SrsTsMessage(SrsTsChannel* c, SrsTsPacket* p) | @@ -437,6 +435,9 @@ SrsTsMessage::SrsTsMessage(SrsTsChannel* c, SrsTsPacket* p) | ||
| 437 | continuity_counter = 0; | 435 | continuity_counter = 0; |
| 438 | PES_packet_length = 0; | 436 | PES_packet_length = 0; |
| 439 | payload = new SrsSimpleBuffer(); | 437 | payload = new SrsSimpleBuffer(); |
| 438 | + | ||
| 439 | + start_pts = 0; | ||
| 440 | + write_pcr = false; | ||
| 440 | } | 441 | } |
| 441 | 442 | ||
| 442 | SrsTsMessage::~SrsTsMessage() | 443 | SrsTsMessage::~SrsTsMessage() |
| @@ -489,12 +490,12 @@ bool SrsTsMessage::fresh() | @@ -489,12 +490,12 @@ bool SrsTsMessage::fresh() | ||
| 489 | 490 | ||
| 490 | bool SrsTsMessage::is_audio() | 491 | bool SrsTsMessage::is_audio() |
| 491 | { | 492 | { |
| 492 | - return ((sid >> 5) & 0x07) == SrsTsPESStreamIdAudio; | 493 | + return ((sid >> 5) & 0x07) == SrsTsPESStreamIdAudioChecker; |
| 493 | } | 494 | } |
| 494 | 495 | ||
| 495 | bool SrsTsMessage::is_video() | 496 | bool SrsTsMessage::is_video() |
| 496 | { | 497 | { |
| 497 | - return ((sid >> 4) & 0x0f) == SrsTsPESStreamIdVideo; | 498 | + return ((sid >> 4) & 0x0f) == SrsTsPESStreamIdVideoChecker; |
| 498 | } | 499 | } |
| 499 | 500 | ||
| 500 | int SrsTsMessage::stream_number() | 501 | int SrsTsMessage::stream_number() |
| @@ -585,31 +586,17 @@ int SrsTsContext::decode(SrsStream* stream, ISrsTsHandler* handler) | @@ -585,31 +586,17 @@ int SrsTsContext::decode(SrsStream* stream, ISrsTsHandler* handler) | ||
| 585 | return ret; | 586 | return ret; |
| 586 | } | 587 | } |
| 587 | 588 | ||
| 588 | -int SrsTsContext::encode(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload, SrsCodecVideo vc, SrsCodecAudio ac) | ||
| 589 | -{ | ||
| 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 | - } | ||
| 599 | - } | ||
| 600 | - | ||
| 601 | - // encode the media frame to PES packets over TS. | ||
| 602 | - return encode_pes(writer, frame, payload); | ||
| 603 | -} | ||
| 604 | - | ||
| 605 | -int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCodecAudio ac) | 589 | +int SrsTsContext::encode(SrsFileWriter* writer, SrsTsMessage* msg, SrsCodecVideo vc, SrsCodecAudio ac) |
| 606 | { | 590 | { |
| 607 | int ret = ERROR_SUCCESS; | 591 | int ret = ERROR_SUCCESS; |
| 608 | 592 | ||
| 609 | - SrsTsStream vs = SrsTsStreamReserved; | ||
| 610 | - SrsTsStream as = SrsTsStreamReserved; | 593 | + SrsTsStream vs, as; |
| 594 | + int16_t video_pid, audio_pid; | ||
| 611 | switch (vc) { | 595 | switch (vc) { |
| 612 | - case SrsCodecVideoAVC: vs = SrsTsStreamVideoH264; break; | 596 | + case SrsCodecVideoAVC: |
| 597 | + vs = SrsTsStreamVideoH264; | ||
| 598 | + video_pid = TS_VIDEO_AVC_PID; | ||
| 599 | + break; | ||
| 613 | case SrsCodecVideoReserved: | 600 | case SrsCodecVideoReserved: |
| 614 | case SrsCodecVideoReserved1: | 601 | case SrsCodecVideoReserved1: |
| 615 | case SrsCodecVideoReserved2: | 602 | case SrsCodecVideoReserved2: |
| @@ -618,11 +605,18 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | @@ -618,11 +605,18 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | ||
| 618 | case SrsCodecVideoOn2VP6: | 605 | case SrsCodecVideoOn2VP6: |
| 619 | case SrsCodecVideoOn2VP6WithAlphaChannel: | 606 | case SrsCodecVideoOn2VP6WithAlphaChannel: |
| 620 | case SrsCodecVideoScreenVideoVersion2: | 607 | case SrsCodecVideoScreenVideoVersion2: |
| 608 | + vs = SrsTsStreamReserved; | ||
| 621 | break; | 609 | break; |
| 622 | } | 610 | } |
| 623 | switch (ac) { | 611 | switch (ac) { |
| 624 | - case SrsCodecAudioAAC: as = SrsTsStreamAudioAAC; break; | ||
| 625 | - case SrsCodecAudioMP3: as = SrsTsStreamAudioMp3; break; | 612 | + case SrsCodecAudioAAC: |
| 613 | + as = SrsTsStreamAudioAAC; | ||
| 614 | + audio_pid = TS_AUDIO_AAC_PID; | ||
| 615 | + break; | ||
| 616 | + case SrsCodecAudioMP3: | ||
| 617 | + as = SrsTsStreamAudioMp3; | ||
| 618 | + audio_pid = TS_AUDIO_MP3_PID; | ||
| 619 | + break; | ||
| 626 | case SrsCodecAudioReserved1: | 620 | case SrsCodecAudioReserved1: |
| 627 | case SrsCodecAudioLinearPCMPlatformEndian: | 621 | case SrsCodecAudioLinearPCMPlatformEndian: |
| 628 | case SrsCodecAudioADPCM: | 622 | case SrsCodecAudioADPCM: |
| @@ -636,11 +630,33 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | @@ -636,11 +630,33 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | ||
| 636 | case SrsCodecAudioSpeex: | 630 | case SrsCodecAudioSpeex: |
| 637 | case SrsCodecAudioReservedMP3_8kHz: | 631 | case SrsCodecAudioReservedMP3_8kHz: |
| 638 | case SrsCodecAudioReservedDeviceSpecificSound: | 632 | case SrsCodecAudioReservedDeviceSpecificSound: |
| 633 | + as = SrsTsStreamReserved; | ||
| 639 | break; | 634 | break; |
| 640 | } | 635 | } |
| 636 | + | ||
| 637 | + // when any codec changed, write PAT/PMT table. | ||
| 638 | + if (vcodec != vc || acodec != ac) { | ||
| 639 | + vcodec = vc; | ||
| 640 | + acodec = ac; | ||
| 641 | + if ((ret = encode_pat_pmt(writer, video_pid, vs, audio_pid, as)) != ERROR_SUCCESS) { | ||
| 642 | + return ret; | ||
| 643 | + } | ||
| 644 | + } | ||
| 645 | + | ||
| 646 | + // encode the media frame to PES packets over TS. | ||
| 647 | + if (msg->is_audio()) { | ||
| 648 | + return encode_pes(writer, msg, audio_pid, as); | ||
| 649 | + } else { | ||
| 650 | + return encode_pes(writer, msg, video_pid, vs); | ||
| 651 | + } | ||
| 652 | +} | ||
| 641 | 653 | ||
| 642 | - int16_t pmt_number = 1; | ||
| 643 | - int16_t pmt_pid = 0x100; | 654 | +int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStream vs, int16_t apid, SrsTsStream as) |
| 655 | +{ | ||
| 656 | + int ret = ERROR_SUCCESS; | ||
| 657 | + | ||
| 658 | + int16_t pmt_number = TS_PMT_NUMBER; | ||
| 659 | + int16_t pmt_pid = TS_PMT_PID; | ||
| 644 | if (true) { | 660 | if (true) { |
| 645 | SrsTsPacket* pkt = SrsTsPacket::create_pat(this, pmt_number, pmt_pid); | 661 | SrsTsPacket* pkt = SrsTsPacket::create_pat(this, pmt_number, pmt_pid); |
| 646 | SrsAutoFree(SrsTsPacket, pkt); | 662 | SrsAutoFree(SrsTsPacket, pkt); |
| @@ -666,10 +682,8 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | @@ -666,10 +682,8 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | ||
| 666 | return ret; | 682 | return ret; |
| 667 | } | 683 | } |
| 668 | } | 684 | } |
| 669 | - int16_t video_pid = 0x101; | ||
| 670 | - int16_t audio_pid = 0x102; | ||
| 671 | if (true) { | 685 | if (true) { |
| 672 | - SrsTsPacket* pkt = SrsTsPacket::create_pmt(this, pmt_number, pmt_pid, video_pid, vs, audio_pid, as); | 686 | + SrsTsPacket* pkt = SrsTsPacket::create_pmt(this, pmt_number, pmt_pid, vpid, vs, apid, as); |
| 673 | SrsAutoFree(SrsTsPacket, pkt); | 687 | SrsAutoFree(SrsTsPacket, pkt); |
| 674 | 688 | ||
| 675 | char* buf = new char[SRS_TS_PACKET_SIZE]; | 689 | char* buf = new char[SRS_TS_PACKET_SIZE]; |
| @@ -697,9 +711,76 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | @@ -697,9 +711,76 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vc, SrsCod | ||
| 697 | return ret; | 711 | return ret; |
| 698 | } | 712 | } |
| 699 | 713 | ||
| 700 | -int SrsTsContext::encode_pes(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload) | 714 | +int SrsTsContext::encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, int16_t pid, SrsTsStream sid) |
| 701 | { | 715 | { |
| 702 | int ret = ERROR_SUCCESS; | 716 | int ret = ERROR_SUCCESS; |
| 717 | + | ||
| 718 | + if (msg->payload->length() == 0) { | ||
| 719 | + return ret; | ||
| 720 | + } | ||
| 721 | + | ||
| 722 | + SrsTsChannel* channel = get(pid); | ||
| 723 | + srs_assert(channel); | ||
| 724 | + | ||
| 725 | + char* start = msg->payload->bytes(); | ||
| 726 | + char* end = start + msg->payload->length(); | ||
| 727 | + char* p = start; | ||
| 728 | + | ||
| 729 | + while (p < end) { | ||
| 730 | + SrsTsPacket* pkt = NULL; | ||
| 731 | + if (p == start) { | ||
| 732 | + pkt = SrsTsPacket::create_pes_first(this, | ||
| 733 | + pid, msg->sid, channel->continuity_counter++, msg->discontinuity, | ||
| 734 | + msg->write_pcr? msg->dts:-1, msg->dts, msg->pts, msg->payload->length() | ||
| 735 | + ); | ||
| 736 | + } else { | ||
| 737 | + pkt = SrsTsPacket::create_pes_continue(this, | ||
| 738 | + pid, msg->sid, channel->continuity_counter++ | ||
| 739 | + ); | ||
| 740 | + } | ||
| 741 | + SrsAutoFree(SrsTsPacket, pkt); | ||
| 742 | + | ||
| 743 | + char* buf = new char[SRS_TS_PACKET_SIZE]; | ||
| 744 | + SrsAutoFree(char, buf); | ||
| 745 | + | ||
| 746 | + // set the left bytes with 0xFF. | ||
| 747 | + int nb_buf = pkt->size(); | ||
| 748 | + srs_assert(nb_buf < SRS_TS_PACKET_SIZE); | ||
| 749 | + | ||
| 750 | + int left = srs_min(end - p, SRS_TS_PACKET_SIZE - nb_buf); | ||
| 751 | + int nb_stuffings = SRS_TS_PACKET_SIZE - nb_buf - left; | ||
| 752 | + if (nb_stuffings > 0) { | ||
| 753 | + // set all bytes to stuffings. | ||
| 754 | + memset(buf, 0xFF, SRS_TS_PACKET_SIZE); | ||
| 755 | + | ||
| 756 | + // padding with stuffings. | ||
| 757 | + pkt->padding(nb_stuffings); | ||
| 758 | + | ||
| 759 | + // size changed, recalc it. | ||
| 760 | + nb_buf = pkt->size(); | ||
| 761 | + srs_assert(nb_buf < SRS_TS_PACKET_SIZE); | ||
| 762 | + | ||
| 763 | + left = srs_min(end - p, SRS_TS_PACKET_SIZE - nb_buf); | ||
| 764 | + nb_stuffings = SRS_TS_PACKET_SIZE - nb_buf - left; | ||
| 765 | + srs_assert(nb_stuffings == 0); | ||
| 766 | + } | ||
| 767 | + memcpy(buf + nb_buf, p, left); | ||
| 768 | + p += left; | ||
| 769 | + | ||
| 770 | + SrsStream stream; | ||
| 771 | + if ((ret = stream.initialize(buf, nb_buf)) != ERROR_SUCCESS) { | ||
| 772 | + return ret; | ||
| 773 | + } | ||
| 774 | + if ((ret = pkt->encode(&stream)) != ERROR_SUCCESS) { | ||
| 775 | + srs_error("ts encode ts packet failed. ret=%d", ret); | ||
| 776 | + return ret; | ||
| 777 | + } | ||
| 778 | + if ((ret = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != ERROR_SUCCESS) { | ||
| 779 | + srs_error("ts write ts packet failed. ret=%d", ret); | ||
| 780 | + return ret; | ||
| 781 | + } | ||
| 782 | + } | ||
| 783 | + | ||
| 703 | return ret; | 784 | return ret; |
| 704 | } | 785 | } |
| 705 | 786 | ||
| @@ -867,6 +948,34 @@ int SrsTsPacket::encode(SrsStream* stream) | @@ -867,6 +948,34 @@ int SrsTsPacket::encode(SrsStream* stream) | ||
| 867 | return ret; | 948 | return ret; |
| 868 | } | 949 | } |
| 869 | 950 | ||
| 951 | +void SrsTsPacket::padding(int nb_stuffings) | ||
| 952 | +{ | ||
| 953 | + if (!adaptation_field) { | ||
| 954 | + SrsTsAdaptationField* af = new SrsTsAdaptationField(this); | ||
| 955 | + adaptation_field = af; | ||
| 956 | + | ||
| 957 | + af->adaption_field_length = 0; // calc in size. | ||
| 958 | + af->discontinuity_indicator = 0; | ||
| 959 | + af->random_access_indicator = 0; | ||
| 960 | + af->elementary_stream_priority_indicator = 0; | ||
| 961 | + af->PCR_flag = 0; | ||
| 962 | + af->OPCR_flag = 0; | ||
| 963 | + af->splicing_point_flag = 0; | ||
| 964 | + af->transport_private_data_flag = 0; | ||
| 965 | + af->adaptation_field_extension_flag = 0; | ||
| 966 | + | ||
| 967 | + // consume the af size if possible. | ||
| 968 | + nb_stuffings = srs_max(0, nb_stuffings - af->size()); | ||
| 969 | + } | ||
| 970 | + | ||
| 971 | + adaptation_field->nb_af_reserved = nb_stuffings; | ||
| 972 | + | ||
| 973 | + // set payload with af. | ||
| 974 | + if (adaption_field_control == SrsTsAdaptationFieldTypePayloadOnly) { | ||
| 975 | + adaption_field_control = SrsTsAdaptationFieldTypeBoth; | ||
| 976 | + } | ||
| 977 | +} | ||
| 978 | + | ||
| 870 | SrsTsPacket* SrsTsPacket::create_pat(SrsTsContext* context, int16_t pmt_number, int16_t pmt_pid) | 979 | SrsTsPacket* SrsTsPacket::create_pat(SrsTsContext* context, int16_t pmt_number, int16_t pmt_pid) |
| 871 | { | 980 | { |
| 872 | SrsTsPacket* pkt = new SrsTsPacket(context); | 981 | SrsTsPacket* pkt = new SrsTsPacket(context); |
| @@ -906,6 +1015,7 @@ SrsTsPacket* SrsTsPacket::create_pmt(SrsTsContext* context, int16_t pmt_number, | @@ -906,6 +1015,7 @@ SrsTsPacket* SrsTsPacket::create_pmt(SrsTsContext* context, int16_t pmt_number, | ||
| 906 | pkt->pid = (SrsTsPid)pmt_pid; | 1015 | pkt->pid = (SrsTsPid)pmt_pid; |
| 907 | pkt->transport_scrambling_control = SrsTsScrambledDisabled; | 1016 | pkt->transport_scrambling_control = SrsTsScrambledDisabled; |
| 908 | pkt->adaption_field_control = SrsTsAdaptationFieldTypePayloadOnly; | 1017 | pkt->adaption_field_control = SrsTsAdaptationFieldTypePayloadOnly; |
| 1018 | + // TODO: FIXME: maybe should continuous in channel. | ||
| 909 | pkt->continuity_counter = 0; | 1019 | pkt->continuity_counter = 0; |
| 910 | pkt->adaptation_field = NULL; | 1020 | pkt->adaptation_field = NULL; |
| 911 | SrsTsPayloadPMT* pmt = new SrsTsPayloadPMT(pkt); | 1021 | SrsTsPayloadPMT* pmt = new SrsTsPayloadPMT(pkt); |
| @@ -928,6 +1038,80 @@ SrsTsPacket* SrsTsPacket::create_pmt(SrsTsContext* context, int16_t pmt_number, | @@ -928,6 +1038,80 @@ SrsTsPacket* SrsTsPacket::create_pmt(SrsTsContext* context, int16_t pmt_number, | ||
| 928 | return pkt; | 1038 | return pkt; |
| 929 | } | 1039 | } |
| 930 | 1040 | ||
| 1041 | +SrsTsPacket* SrsTsPacket::create_pes_first(SrsTsContext* context, | ||
| 1042 | + int16_t pid, SrsTsPESStreamId sid, u_int8_t continuity_counter, bool discontinuity, | ||
| 1043 | + int64_t pcr, int64_t dts, int64_t pts, int size | ||
| 1044 | +) { | ||
| 1045 | + SrsTsPacket* pkt = new SrsTsPacket(context); | ||
| 1046 | + pkt->sync_byte = 0x47; | ||
| 1047 | + pkt->transport_error_indicator = 0; | ||
| 1048 | + pkt->payload_unit_start_indicator = 1; | ||
| 1049 | + pkt->transport_priority = 0; | ||
| 1050 | + pkt->pid = (SrsTsPid)pid; | ||
| 1051 | + pkt->transport_scrambling_control = SrsTsScrambledDisabled; | ||
| 1052 | + pkt->adaption_field_control = SrsTsAdaptationFieldTypePayloadOnly; | ||
| 1053 | + pkt->continuity_counter = continuity_counter; | ||
| 1054 | + pkt->adaptation_field = NULL; | ||
| 1055 | + SrsTsPayloadPES* pes = new SrsTsPayloadPES(pkt); | ||
| 1056 | + pkt->payload = pes; | ||
| 1057 | + | ||
| 1058 | + if (pcr >= 0) { | ||
| 1059 | + SrsTsAdaptationField* af = new SrsTsAdaptationField(pkt); | ||
| 1060 | + pkt->adaptation_field = af; | ||
| 1061 | + pkt->adaption_field_control = SrsTsAdaptationFieldTypeBoth; | ||
| 1062 | + | ||
| 1063 | + af->adaption_field_length = 0; // calc in size. | ||
| 1064 | + af->discontinuity_indicator = discontinuity; | ||
| 1065 | + af->random_access_indicator = 0; | ||
| 1066 | + af->elementary_stream_priority_indicator = 0; | ||
| 1067 | + af->PCR_flag = 1; | ||
| 1068 | + af->OPCR_flag = 0; | ||
| 1069 | + af->splicing_point_flag = 0; | ||
| 1070 | + af->transport_private_data_flag = 0; | ||
| 1071 | + af->adaptation_field_extension_flag = 0; | ||
| 1072 | + af->program_clock_reference_base = pcr; | ||
| 1073 | + af->program_clock_reference_extension = 0; | ||
| 1074 | + } | ||
| 1075 | + | ||
| 1076 | + pes->packet_start_code_prefix = 0x01; | ||
| 1077 | + pes->stream_id = (u_int8_t)sid; | ||
| 1078 | + pes->PES_packet_length = (size > 0xFFFF)? 0:size; | ||
| 1079 | + pes->PES_scrambling_control = 0; | ||
| 1080 | + pes->PES_priority = 0; | ||
| 1081 | + pes->data_alignment_indicator = 0; | ||
| 1082 | + pes->copyright = 0; | ||
| 1083 | + pes->original_or_copy = 0; | ||
| 1084 | + pes->PTS_DTS_flags = (dts == pts)? 0x02:0x03; | ||
| 1085 | + pes->ESCR_flag = 0; | ||
| 1086 | + pes->ES_rate_flag = 0; | ||
| 1087 | + pes->DSM_trick_mode_flag = 0; | ||
| 1088 | + pes->additional_copy_info_flag = 0; | ||
| 1089 | + pes->PES_CRC_flag = 0; | ||
| 1090 | + pes->PES_extension_flag = 0; | ||
| 1091 | + pes->PES_header_data_length = 0; // calc in size. | ||
| 1092 | + pes->pts = pts; | ||
| 1093 | + pes->dts = dts; | ||
| 1094 | + return pkt; | ||
| 1095 | +} | ||
| 1096 | + | ||
| 1097 | +SrsTsPacket* SrsTsPacket::create_pes_continue(SrsTsContext* context, | ||
| 1098 | + int16_t pid, SrsTsPESStreamId sid, u_int8_t continuity_counter | ||
| 1099 | +) { | ||
| 1100 | + SrsTsPacket* pkt = new SrsTsPacket(context); | ||
| 1101 | + pkt->sync_byte = 0x47; | ||
| 1102 | + pkt->transport_error_indicator = 0; | ||
| 1103 | + pkt->payload_unit_start_indicator = 0; | ||
| 1104 | + pkt->transport_priority = 0; | ||
| 1105 | + pkt->pid = (SrsTsPid)pid; | ||
| 1106 | + pkt->transport_scrambling_control = SrsTsScrambledDisabled; | ||
| 1107 | + pkt->adaption_field_control = SrsTsAdaptationFieldTypePayloadOnly; | ||
| 1108 | + pkt->continuity_counter = continuity_counter; | ||
| 1109 | + pkt->adaptation_field = NULL; | ||
| 1110 | + pkt->payload = NULL; | ||
| 1111 | + | ||
| 1112 | + return pkt; | ||
| 1113 | +} | ||
| 1114 | + | ||
| 931 | SrsTsAdaptationField::SrsTsAdaptationField(SrsTsPacket* pkt) | 1115 | SrsTsAdaptationField::SrsTsAdaptationField(SrsTsPacket* pkt) |
| 932 | { | 1116 | { |
| 933 | packet = pkt; | 1117 | packet = pkt; |
| @@ -964,6 +1148,10 @@ SrsTsAdaptationField::SrsTsAdaptationField(SrsTsPacket* pkt) | @@ -964,6 +1148,10 @@ SrsTsAdaptationField::SrsTsAdaptationField(SrsTsPacket* pkt) | ||
| 964 | marker_bit2 = 0; | 1148 | marker_bit2 = 0; |
| 965 | nb_af_ext_reserved = 0; | 1149 | nb_af_ext_reserved = 0; |
| 966 | nb_af_reserved = 0; | 1150 | nb_af_reserved = 0; |
| 1151 | + | ||
| 1152 | + const1_value0 = 0x3F; | ||
| 1153 | + const1_value1 = 0x1F; | ||
| 1154 | + const1_value2 = 0x3F; | ||
| 967 | } | 1155 | } |
| 968 | 1156 | ||
| 969 | SrsTsAdaptationField::~SrsTsAdaptationField() | 1157 | SrsTsAdaptationField::~SrsTsAdaptationField() |
| @@ -1014,7 +1202,7 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | @@ -1014,7 +1202,7 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | ||
| 1014 | OPCR_flag = (tmpv >> 3) & 0x01; | 1202 | OPCR_flag = (tmpv >> 3) & 0x01; |
| 1015 | splicing_point_flag = (tmpv >> 2) & 0x01; | 1203 | splicing_point_flag = (tmpv >> 2) & 0x01; |
| 1016 | transport_private_data_flag = (tmpv >> 1) & 0x01; | 1204 | transport_private_data_flag = (tmpv >> 1) & 0x01; |
| 1017 | - adaptation_field_extension_flag = (tmpv >> 0) & 0x01; | 1205 | + adaptation_field_extension_flag = tmpv & 0x01; |
| 1018 | 1206 | ||
| 1019 | if (PCR_flag) { | 1207 | if (PCR_flag) { |
| 1020 | if (!stream->require(6)) { | 1208 | if (!stream->require(6)) { |
| @@ -1027,7 +1215,8 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | @@ -1027,7 +1215,8 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | ||
| 1027 | char* p = stream->data() + stream->pos(); | 1215 | char* p = stream->data() + stream->pos(); |
| 1028 | stream->skip(6); | 1216 | stream->skip(6); |
| 1029 | 1217 | ||
| 1030 | - pp = (char*)&program_clock_reference_base; | 1218 | + int64_t pcrv = 0; |
| 1219 | + pp = (char*)&pcrv; | ||
| 1031 | pp[5] = *p++; | 1220 | pp[5] = *p++; |
| 1032 | pp[4] = *p++; | 1221 | pp[4] = *p++; |
| 1033 | pp[3] = *p++; | 1222 | pp[3] = *p++; |
| @@ -1037,8 +1226,9 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | @@ -1037,8 +1226,9 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | ||
| 1037 | 1226 | ||
| 1038 | // @remark, use pcr base and ignore the extension | 1227 | // @remark, use pcr base and ignore the extension |
| 1039 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/250#issuecomment-71349370 | 1228 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/250#issuecomment-71349370 |
| 1040 | - program_clock_reference_extension = program_clock_reference_base & 0x1ff; | ||
| 1041 | - program_clock_reference_base = (program_clock_reference_base >> 15) & 0x1ffffffffLL; | 1229 | + program_clock_reference_extension = pcrv & 0x1ff; |
| 1230 | + const1_value0 = (pcrv >> 9) & 0x3F; | ||
| 1231 | + program_clock_reference_base = (pcrv >> 15) & 0x1ffffffffLL; | ||
| 1042 | } | 1232 | } |
| 1043 | 1233 | ||
| 1044 | if (OPCR_flag) { | 1234 | if (OPCR_flag) { |
| @@ -1051,8 +1241,9 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | @@ -1051,8 +1241,9 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | ||
| 1051 | char* pp = NULL; | 1241 | char* pp = NULL; |
| 1052 | char* p = stream->data() + stream->pos(); | 1242 | char* p = stream->data() + stream->pos(); |
| 1053 | stream->skip(6); | 1243 | stream->skip(6); |
| 1054 | - | ||
| 1055 | - pp = (char*)&original_program_clock_reference_base; | 1244 | + |
| 1245 | + int64_t opcrv = 0; | ||
| 1246 | + pp = (char*)&opcrv; | ||
| 1056 | pp[5] = *p++; | 1247 | pp[5] = *p++; |
| 1057 | pp[4] = *p++; | 1248 | pp[4] = *p++; |
| 1058 | pp[3] = *p++; | 1249 | pp[3] = *p++; |
| @@ -1062,8 +1253,9 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | @@ -1062,8 +1253,9 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | ||
| 1062 | 1253 | ||
| 1063 | // @remark, use pcr base and ignore the extension | 1254 | // @remark, use pcr base and ignore the extension |
| 1064 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/250#issuecomment-71349370 | 1255 | // @see https://github.com/winlinvip/simple-rtmp-server/issues/250#issuecomment-71349370 |
| 1065 | - original_program_clock_reference_extension = program_clock_reference_base & 0x1ff; | ||
| 1066 | - original_program_clock_reference_base = (program_clock_reference_base >> 15) & 0x1ffffffffLL; | 1256 | + original_program_clock_reference_extension = opcrv & 0x1ff; |
| 1257 | + const1_value2 = (opcrv >> 9) & 0x3F; | ||
| 1258 | + original_program_clock_reference_base = (opcrv >> 15) & 0x1ffffffffLL; | ||
| 1067 | } | 1259 | } |
| 1068 | 1260 | ||
| 1069 | if (splicing_point_flag) { | 1261 | if (splicing_point_flag) { |
| @@ -1104,11 +1296,12 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | @@ -1104,11 +1296,12 @@ int SrsTsAdaptationField::decode(SrsStream* stream) | ||
| 1104 | return ret; | 1296 | return ret; |
| 1105 | } | 1297 | } |
| 1106 | adaptation_field_extension_length = (u_int8_t)stream->read_1bytes(); | 1298 | adaptation_field_extension_length = (u_int8_t)stream->read_1bytes(); |
| 1107 | - ltw_flag = stream->read_1bytes(); | 1299 | + int8_t ltwfv = stream->read_1bytes(); |
| 1108 | 1300 | ||
| 1109 | - piecewise_rate_flag = (ltw_flag >> 6) & 0x01; | ||
| 1110 | - seamless_splice_flag = (ltw_flag >> 5) & 0x01; | ||
| 1111 | - ltw_flag = (ltw_flag >> 7) & 0x01; | 1301 | + piecewise_rate_flag = (ltwfv >> 6) & 0x01; |
| 1302 | + seamless_splice_flag = (ltwfv >> 5) & 0x01; | ||
| 1303 | + ltw_flag = (ltwfv >> 7) & 0x01; | ||
| 1304 | + const1_value1 = ltwfv & 0x1F; | ||
| 1112 | 1305 | ||
| 1113 | if (ltw_flag) { | 1306 | if (ltw_flag) { |
| 1114 | if (!stream->require(2)) { | 1307 | if (!stream->require(2)) { |
| @@ -1181,12 +1374,172 @@ int SrsTsAdaptationField::size() | @@ -1181,12 +1374,172 @@ int SrsTsAdaptationField::size() | ||
| 1181 | sz += nb_af_ext_reserved; | 1374 | sz += nb_af_ext_reserved; |
| 1182 | sz += nb_af_reserved; | 1375 | sz += nb_af_reserved; |
| 1183 | 1376 | ||
| 1377 | + adaption_field_length = sz - 1; | ||
| 1378 | + | ||
| 1184 | return sz; | 1379 | return sz; |
| 1185 | } | 1380 | } |
| 1186 | 1381 | ||
| 1187 | int SrsTsAdaptationField::encode(SrsStream* stream) | 1382 | int SrsTsAdaptationField::encode(SrsStream* stream) |
| 1188 | { | 1383 | { |
| 1189 | int ret = ERROR_SUCCESS; | 1384 | int ret = ERROR_SUCCESS; |
| 1385 | + | ||
| 1386 | + if (!stream->require(2)) { | ||
| 1387 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1388 | + srs_error("ts: mux af failed. ret=%d", ret); | ||
| 1389 | + return ret; | ||
| 1390 | + } | ||
| 1391 | + stream->write_1bytes(adaption_field_length); | ||
| 1392 | + | ||
| 1393 | + // When the adaptation_field_control value is '11', the value of the adaptation_field_length shall | ||
| 1394 | + // be in the range 0 to 182. | ||
| 1395 | + if (packet->adaption_field_control == SrsTsAdaptationFieldTypeBoth && adaption_field_length > 182) { | ||
| 1396 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1397 | + srs_error("ts: mux af length failed, must in [0, 182], actual=%d. ret=%d", adaption_field_length, ret); | ||
| 1398 | + return ret; | ||
| 1399 | + } | ||
| 1400 | + // When the adaptation_field_control value is '10', the value of the adaptation_field_length shall | ||
| 1401 | + // be 183. | ||
| 1402 | + if (packet->adaption_field_control == SrsTsAdaptationFieldTypeAdaptionOnly && adaption_field_length != 183) { | ||
| 1403 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1404 | + srs_error("ts: mux af length failed, must be 183, actual=%d. ret=%d", adaption_field_length, ret); | ||
| 1405 | + return ret; | ||
| 1406 | + } | ||
| 1407 | + | ||
| 1408 | + // no adaptation field. | ||
| 1409 | + if (adaption_field_length == 0) { | ||
| 1410 | + srs_info("ts: mux af empty."); | ||
| 1411 | + return ret; | ||
| 1412 | + } | ||
| 1413 | + int8_t tmpv = adaptation_field_extension_flag & 0x01; | ||
| 1414 | + tmpv |= (discontinuity_indicator << 7) & 0x80; | ||
| 1415 | + tmpv |= (random_access_indicator << 6) & 0x40; | ||
| 1416 | + tmpv |= (elementary_stream_priority_indicator << 5) & 0x20; | ||
| 1417 | + tmpv |= (PCR_flag << 4) & 0x10; | ||
| 1418 | + tmpv |= (OPCR_flag << 3) & 0x08; | ||
| 1419 | + tmpv |= (splicing_point_flag << 2) & 0x04; | ||
| 1420 | + tmpv |= (transport_private_data_flag << 1) & 0x02; | ||
| 1421 | + stream->write_1bytes(tmpv); | ||
| 1422 | + | ||
| 1423 | + if (PCR_flag) { | ||
| 1424 | + if (!stream->require(6)) { | ||
| 1425 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1426 | + srs_error("ts: mux af PCR_flag failed. ret=%d", ret); | ||
| 1427 | + return ret; | ||
| 1428 | + } | ||
| 1429 | + | ||
| 1430 | + char* pp = NULL; | ||
| 1431 | + char* p = stream->data() + stream->pos(); | ||
| 1432 | + stream->skip(6); | ||
| 1433 | + | ||
| 1434 | + // @remark, use pcr base and ignore the extension | ||
| 1435 | + // @see https://github.com/winlinvip/simple-rtmp-server/issues/250#issuecomment-71349370 | ||
| 1436 | + int64_t pcrv = program_clock_reference_extension & 0x1ff; | ||
| 1437 | + pcrv |= (const1_value0 << 9) & 0x7E00; | ||
| 1438 | + pcrv |= (program_clock_reference_base << 15) & 0x1FFFFFFFF000000; | ||
| 1439 | + | ||
| 1440 | + pp = (char*)&pcrv; | ||
| 1441 | + *p++ = pp[5]; | ||
| 1442 | + *p++ = pp[4]; | ||
| 1443 | + *p++ = pp[3]; | ||
| 1444 | + *p++ = pp[2]; | ||
| 1445 | + *p++ = pp[1]; | ||
| 1446 | + *p++ = pp[0]; | ||
| 1447 | + } | ||
| 1448 | + | ||
| 1449 | + if (OPCR_flag) { | ||
| 1450 | + if (!stream->require(6)) { | ||
| 1451 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1452 | + srs_error("ts: demux af OPCR_flag failed. ret=%d", ret); | ||
| 1453 | + return ret; | ||
| 1454 | + } | ||
| 1455 | + stream->skip(6); | ||
| 1456 | + srs_warn("ts: mux af ignore OPCR"); | ||
| 1457 | + } | ||
| 1458 | + | ||
| 1459 | + if (splicing_point_flag) { | ||
| 1460 | + if (!stream->require(1)) { | ||
| 1461 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1462 | + srs_error("ts: mux af splicing_point_flag failed. ret=%d", ret); | ||
| 1463 | + return ret; | ||
| 1464 | + } | ||
| 1465 | + stream->write_1bytes(splice_countdown); | ||
| 1466 | + } | ||
| 1467 | + | ||
| 1468 | + if (transport_private_data_flag) { | ||
| 1469 | + if (!stream->require(1)) { | ||
| 1470 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1471 | + srs_error("ts: mux af transport_private_data_flag failed. ret=%d", ret); | ||
| 1472 | + return ret; | ||
| 1473 | + } | ||
| 1474 | + stream->write_1bytes(transport_private_data_length); | ||
| 1475 | + | ||
| 1476 | + if (transport_private_data_length> 0) { | ||
| 1477 | + if (!stream->require(transport_private_data_length)) { | ||
| 1478 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1479 | + srs_error("ts: mux af transport_private_data_flag failed. ret=%d", ret); | ||
| 1480 | + return ret; | ||
| 1481 | + } | ||
| 1482 | + stream->write_bytes(transport_private_data, transport_private_data_length); | ||
| 1483 | + } | ||
| 1484 | + } | ||
| 1485 | + | ||
| 1486 | + if (adaptation_field_extension_flag) { | ||
| 1487 | + if (!stream->require(2)) { | ||
| 1488 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1489 | + srs_error("ts: mux af adaptation_field_extension_flag failed. ret=%d", ret); | ||
| 1490 | + return ret; | ||
| 1491 | + } | ||
| 1492 | + stream->write_1bytes(adaptation_field_extension_length); | ||
| 1493 | + int8_t ltwfv = const1_value1 & 0x1F; | ||
| 1494 | + ltwfv |= (ltw_flag << 7) & 0x80; | ||
| 1495 | + ltwfv |= (piecewise_rate_flag << 6) & 0x40; | ||
| 1496 | + ltwfv |= (seamless_splice_flag << 5) & 0x20; | ||
| 1497 | + stream->write_1bytes(ltwfv); | ||
| 1498 | + | ||
| 1499 | + if (ltw_flag) { | ||
| 1500 | + if (!stream->require(2)) { | ||
| 1501 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1502 | + srs_error("ts: mux af ltw_flag failed. ret=%d", ret); | ||
| 1503 | + return ret; | ||
| 1504 | + } | ||
| 1505 | + stream->skip(2); | ||
| 1506 | + srs_warn("ts: mux af ignore ltw"); | ||
| 1507 | + } | ||
| 1508 | + | ||
| 1509 | + if (piecewise_rate_flag) { | ||
| 1510 | + if (!stream->require(3)) { | ||
| 1511 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1512 | + srs_error("ts: mux af piecewise_rate_flag failed. ret=%d", ret); | ||
| 1513 | + return ret; | ||
| 1514 | + } | ||
| 1515 | + stream->skip(3); | ||
| 1516 | + srs_warn("ts: mux af ignore piecewise_rate"); | ||
| 1517 | + } | ||
| 1518 | + | ||
| 1519 | + if (seamless_splice_flag) { | ||
| 1520 | + if (!stream->require(5)) { | ||
| 1521 | + ret = ERROR_STREAM_CASTER_TS_AF; | ||
| 1522 | + srs_error("ts: mux af seamless_splice_flag failed. ret=%d", ret); | ||
| 1523 | + return ret; | ||
| 1524 | + } | ||
| 1525 | + stream->skip(5); | ||
| 1526 | + srs_warn("ts: mux af ignore seamless_splice"); | ||
| 1527 | + } | ||
| 1528 | + | ||
| 1529 | + if (nb_af_ext_reserved) { | ||
| 1530 | + stream->skip(nb_af_ext_reserved); | ||
| 1531 | + } | ||
| 1532 | + } | ||
| 1533 | + | ||
| 1534 | + if (nb_af_reserved) { | ||
| 1535 | + stream->skip(nb_af_reserved); | ||
| 1536 | + } | ||
| 1537 | + | ||
| 1538 | + srs_info("ts: af parsed, discontinuity=%d random=%d priority=%d PCR=%d OPCR=%d slicing=%d private=%d extension=%d/%d pcr=%"PRId64"/%d opcr=%"PRId64"/%d", | ||
| 1539 | + discontinuity_indicator, random_access_indicator, elementary_stream_priority_indicator, PCR_flag, OPCR_flag, splicing_point_flag, | ||
| 1540 | + transport_private_data_flag, adaptation_field_extension_flag, adaptation_field_extension_length, program_clock_reference_base, | ||
| 1541 | + program_clock_reference_extension, original_program_clock_reference_base, original_program_clock_reference_extension); | ||
| 1542 | + | ||
| 1190 | return ret; | 1543 | return ret; |
| 1191 | } | 1544 | } |
| 1192 | 1545 | ||
| @@ -1207,6 +1560,8 @@ SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p) | @@ -1207,6 +1560,8 @@ SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p) | ||
| 1207 | nb_stuffings = 0; | 1560 | nb_stuffings = 0; |
| 1208 | nb_bytes = 0; | 1561 | nb_bytes = 0; |
| 1209 | nb_paddings = 0; | 1562 | nb_paddings = 0; |
| 1563 | + const2bits = 0x02; | ||
| 1564 | + const1_value0 = 0x07; | ||
| 1210 | } | 1565 | } |
| 1211 | 1566 | ||
| 1212 | SrsTsPayloadPES::~SrsTsPayloadPES() | 1567 | SrsTsPayloadPES::~SrsTsPayloadPES() |
| @@ -1353,28 +1708,28 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg) | @@ -1353,28 +1708,28 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg) | ||
| 1353 | return ret; | 1708 | return ret; |
| 1354 | } | 1709 | } |
| 1355 | // 1B | 1710 | // 1B |
| 1356 | - original_or_copy = stream->read_1bytes(); | 1711 | + int8_t oocv = stream->read_1bytes(); |
| 1357 | // 1B | 1712 | // 1B |
| 1358 | - PES_extension_flag = stream->read_1bytes(); | 1713 | + int8_t pefv = stream->read_1bytes(); |
| 1359 | // 1B | 1714 | // 1B |
| 1360 | PES_header_data_length = stream->read_1bytes(); | 1715 | PES_header_data_length = stream->read_1bytes(); |
| 1361 | // position of header start. | 1716 | // position of header start. |
| 1362 | int pos_header = stream->pos(); | 1717 | int pos_header = stream->pos(); |
| 1363 | 1718 | ||
| 1364 | - //int8_t const2bits = (original_or_copy >> 6) & 0x03; | ||
| 1365 | - PES_scrambling_control = (original_or_copy >> 4) & 0x03; | ||
| 1366 | - PES_priority = (original_or_copy >> 3) & 0x01; | ||
| 1367 | - data_alignment_indicator = (original_or_copy >> 2) & 0x01; | ||
| 1368 | - copyright = (original_or_copy >> 1) & 0x01; | ||
| 1369 | - original_or_copy &= 0x01; | ||
| 1370 | - | ||
| 1371 | - PTS_DTS_flags = (PES_extension_flag >> 6) & 0x03; | ||
| 1372 | - ESCR_flag = (PES_extension_flag >> 5) & 0x01; | ||
| 1373 | - ES_rate_flag = (PES_extension_flag >> 4) & 0x01; | ||
| 1374 | - DSM_trick_mode_flag = (PES_extension_flag >> 3) & 0x01; | ||
| 1375 | - additional_copy_info_flag = (PES_extension_flag >> 2) & 0x01; | ||
| 1376 | - PES_CRC_flag = (PES_extension_flag >> 1) & 0x01; | ||
| 1377 | - PES_extension_flag &= 0x01; | 1719 | + const2bits = (oocv >> 6) & 0x03; |
| 1720 | + PES_scrambling_control = (oocv >> 4) & 0x03; | ||
| 1721 | + PES_priority = (oocv >> 3) & 0x01; | ||
| 1722 | + data_alignment_indicator = (oocv >> 2) & 0x01; | ||
| 1723 | + copyright = (oocv >> 1) & 0x01; | ||
| 1724 | + original_or_copy = oocv & 0x01; | ||
| 1725 | + | ||
| 1726 | + PTS_DTS_flags = (pefv >> 6) & 0x03; | ||
| 1727 | + ESCR_flag = (pefv >> 5) & 0x01; | ||
| 1728 | + ES_rate_flag = (pefv >> 4) & 0x01; | ||
| 1729 | + DSM_trick_mode_flag = (pefv >> 3) & 0x01; | ||
| 1730 | + additional_copy_info_flag = (pefv >> 2) & 0x01; | ||
| 1731 | + PES_CRC_flag = (pefv >> 1) & 0x01; | ||
| 1732 | + PES_extension_flag = pefv & 0x01; | ||
| 1378 | 1733 | ||
| 1379 | // check required together. | 1734 | // check required together. |
| 1380 | int nb_required = 0; | 1735 | int nb_required = 0; |
| @@ -1462,13 +1817,14 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg) | @@ -1462,13 +1817,14 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg) | ||
| 1462 | 1817 | ||
| 1463 | // 1B | 1818 | // 1B |
| 1464 | if (PES_extension_flag) { | 1819 | if (PES_extension_flag) { |
| 1465 | - PES_extension_flag_2 = stream->read_1bytes(); | 1820 | + int8_t efv = stream->read_1bytes(); |
| 1466 | 1821 | ||
| 1467 | - PES_private_data_flag = (PES_extension_flag_2 >> 7) & 0x01; | ||
| 1468 | - pack_header_field_flag = (PES_extension_flag_2 >> 6) & 0x01; | ||
| 1469 | - program_packet_sequence_counter_flag = (PES_extension_flag_2 >> 5) & 0x01; | ||
| 1470 | - P_STD_buffer_flag = (PES_extension_flag_2 >> 4) & 0x01; | ||
| 1471 | - PES_extension_flag_2 &= PES_extension_flag_2 & 0x01; | 1822 | + PES_private_data_flag = (efv >> 7) & 0x01; |
| 1823 | + pack_header_field_flag = (efv >> 6) & 0x01; | ||
| 1824 | + program_packet_sequence_counter_flag = (efv >> 5) & 0x01; | ||
| 1825 | + P_STD_buffer_flag = (efv >> 4) & 0x01; | ||
| 1826 | + const1_value0 = (efv >> 1) & 0x07; | ||
| 1827 | + PES_extension_flag_2 = efv & 0x01; | ||
| 1472 | 1828 | ||
| 1473 | nb_required = 0; | 1829 | nb_required = 0; |
| 1474 | nb_required += PES_private_data_flag? 16:0; | 1830 | nb_required += PES_private_data_flag? 16:0; |
| @@ -1619,8 +1975,9 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg) | @@ -1619,8 +1975,9 @@ int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg) | ||
| 1619 | 1975 | ||
| 1620 | int SrsTsPayloadPES::size() | 1976 | int SrsTsPayloadPES::size() |
| 1621 | { | 1977 | { |
| 1622 | - int sz = 6; | 1978 | + int sz = 0; |
| 1623 | 1979 | ||
| 1980 | + PES_header_data_length = 0; | ||
| 1624 | SrsTsPESStreamId sid = (SrsTsPESStreamId)stream_id; | 1981 | SrsTsPESStreamId sid = (SrsTsPESStreamId)stream_id; |
| 1625 | 1982 | ||
| 1626 | if (sid != SrsTsPESStreamIdProgramStreamMap | 1983 | if (sid != SrsTsPESStreamIdProgramStreamMap |
| @@ -1632,7 +1989,9 @@ int SrsTsPayloadPES::size() | @@ -1632,7 +1989,9 @@ int SrsTsPayloadPES::size() | ||
| 1632 | && sid != SrsTsPESStreamIdDsmccStream | 1989 | && sid != SrsTsPESStreamIdDsmccStream |
| 1633 | && sid != SrsTsPESStreamIdH2221TypeE | 1990 | && sid != SrsTsPESStreamIdH2221TypeE |
| 1634 | ) { | 1991 | ) { |
| 1992 | + sz += 6; | ||
| 1635 | sz += 3; | 1993 | sz += 3; |
| 1994 | + PES_header_data_length = sz; | ||
| 1636 | 1995 | ||
| 1637 | sz += (PTS_DTS_flags == 0x2)? 5:0; | 1996 | sz += (PTS_DTS_flags == 0x2)? 5:0; |
| 1638 | sz += (PTS_DTS_flags == 0x3)? 10:0; | 1997 | sz += (PTS_DTS_flags == 0x3)? 10:0; |
| @@ -1650,6 +2009,7 @@ int SrsTsPayloadPES::size() | @@ -1650,6 +2009,7 @@ int SrsTsPayloadPES::size() | ||
| 1650 | sz += P_STD_buffer_flag? 2:0; | 2009 | sz += P_STD_buffer_flag? 2:0; |
| 1651 | sz += PES_extension_flag_2? 1 + PES_extension_field_length:0; // 1+x bytes. | 2010 | sz += PES_extension_flag_2? 1 + PES_extension_field_length:0; // 1+x bytes. |
| 1652 | } | 2011 | } |
| 2012 | + PES_header_data_length = sz - PES_header_data_length; | ||
| 1653 | 2013 | ||
| 1654 | sz += nb_stuffings; | 2014 | sz += nb_stuffings; |
| 1655 | 2015 | ||
| @@ -1673,6 +2033,161 @@ int SrsTsPayloadPES::size() | @@ -1673,6 +2033,161 @@ int SrsTsPayloadPES::size() | ||
| 1673 | int SrsTsPayloadPES::encode(SrsStream* stream) | 2033 | int SrsTsPayloadPES::encode(SrsStream* stream) |
| 1674 | { | 2034 | { |
| 1675 | int ret = ERROR_SUCCESS; | 2035 | int ret = ERROR_SUCCESS; |
| 2036 | + | ||
| 2037 | + // 6B fixed header. | ||
| 2038 | + if (!stream->require(6)) { | ||
| 2039 | + ret = ERROR_STREAM_CASTER_TS_PSE; | ||
| 2040 | + srs_error("ts: mux PSE failed. ret=%d", ret); | ||
| 2041 | + return ret; | ||
| 2042 | + } | ||
| 2043 | + | ||
| 2044 | + // 3B | ||
| 2045 | + stream->write_3bytes(packet_start_code_prefix); | ||
| 2046 | + // 1B | ||
| 2047 | + stream->write_1bytes(stream_id); | ||
| 2048 | + // 2B | ||
| 2049 | + // the PES_packet_length is the actual bytes size, the pplv write to ts | ||
| 2050 | + // is the actual bytes plus the header size. | ||
| 2051 | + int32_t pplv = 0; | ||
| 2052 | + if (PES_packet_length > 0) { | ||
| 2053 | + pplv = PES_packet_length + 3 + PES_header_data_length; | ||
| 2054 | + pplv = (pplv > 0xFFFF)? 0 : pplv; | ||
| 2055 | + } | ||
| 2056 | + stream->write_2bytes(pplv); | ||
| 2057 | + | ||
| 2058 | + // check the packet start prefix. | ||
| 2059 | + packet_start_code_prefix &= 0xFFFFFF; | ||
| 2060 | + if (packet_start_code_prefix != 0x01) { | ||
| 2061 | + ret = ERROR_STREAM_CASTER_TS_PSE; | ||
| 2062 | + srs_error("ts: mux PSE start code failed, expect=0x01, actual=%#x. ret=%d", packet_start_code_prefix, ret); | ||
| 2063 | + return ret; | ||
| 2064 | + } | ||
| 2065 | + | ||
| 2066 | + // 3B flags. | ||
| 2067 | + if (!stream->require(3)) { | ||
| 2068 | + ret = ERROR_STREAM_CASTER_TS_PSE; | ||
| 2069 | + srs_error("ts: mux PSE flags failed. ret=%d", ret); | ||
| 2070 | + return ret; | ||
| 2071 | + } | ||
| 2072 | + // 1B | ||
| 2073 | + int8_t oocv = original_or_copy & 0x01; | ||
| 2074 | + oocv |= (const2bits << 6) & 0xC0; | ||
| 2075 | + oocv |= (PES_scrambling_control << 4) & 0x30; | ||
| 2076 | + oocv |= (PES_priority << 3) & 0x08; | ||
| 2077 | + oocv |= (data_alignment_indicator << 2) & 0x04; | ||
| 2078 | + oocv |= (copyright << 1) & 0x02; | ||
| 2079 | + stream->write_1bytes(oocv); | ||
| 2080 | + // 1B | ||
| 2081 | + int8_t pefv = PES_extension_flag & 0x01; | ||
| 2082 | + pefv |= (PTS_DTS_flags << 6) & 0xC0; | ||
| 2083 | + pefv |= (ESCR_flag << 5) & 0x20; | ||
| 2084 | + pefv |= (ES_rate_flag << 4) & 0x10; | ||
| 2085 | + pefv |= (DSM_trick_mode_flag << 3) & 0x08; | ||
| 2086 | + pefv |= (additional_copy_info_flag << 2) & 0x04; | ||
| 2087 | + pefv |= (PES_CRC_flag << 1) & 0x02; | ||
| 2088 | + stream->write_1bytes(pefv); | ||
| 2089 | + // 1B | ||
| 2090 | + stream->write_1bytes(PES_header_data_length); | ||
| 2091 | + | ||
| 2092 | + // check required together. | ||
| 2093 | + int nb_required = 0; | ||
| 2094 | + nb_required += (PTS_DTS_flags == 0x2)? 5:0; | ||
| 2095 | + nb_required += (PTS_DTS_flags == 0x3)? 10:0; | ||
| 2096 | + nb_required += ESCR_flag? 6:0; | ||
| 2097 | + nb_required += ES_rate_flag? 3:0; | ||
| 2098 | + nb_required += DSM_trick_mode_flag? 1:0; | ||
| 2099 | + nb_required += additional_copy_info_flag? 1:0; | ||
| 2100 | + nb_required += PES_CRC_flag? 2:0; | ||
| 2101 | + nb_required += PES_extension_flag? 1:0; | ||
| 2102 | + if (!stream->require(nb_required)) { | ||
| 2103 | + ret = ERROR_STREAM_CASTER_TS_PSE; | ||
| 2104 | + srs_error("ts: mux PSE payload failed. ret=%d", ret); | ||
| 2105 | + return ret; | ||
| 2106 | + } | ||
| 2107 | + | ||
| 2108 | + // 5B | ||
| 2109 | + if (PTS_DTS_flags == 0x2) { | ||
| 2110 | + if ((ret = encode_33bits_dts_pts(stream, 0x02, pts)) != ERROR_SUCCESS) { | ||
| 2111 | + return ret; | ||
| 2112 | + } | ||
| 2113 | + } | ||
| 2114 | + | ||
| 2115 | + // 10B | ||
| 2116 | + if (PTS_DTS_flags == 0x3) { | ||
| 2117 | + if ((ret = encode_33bits_dts_pts(stream, 0x03, pts)) != ERROR_SUCCESS) { | ||
| 2118 | + return ret; | ||
| 2119 | + } | ||
| 2120 | + if ((ret = encode_33bits_dts_pts(stream, 0x01, dts)) != ERROR_SUCCESS) { | ||
| 2121 | + return ret; | ||
| 2122 | + } | ||
| 2123 | + | ||
| 2124 | + // check sync, the diff of dts and pts should never greater than 1s. | ||
| 2125 | + if (dts - pts > 90000 || pts - dts > 90000) { | ||
| 2126 | + srs_warn("ts: sync dts=%"PRId64", pts=%"PRId64, dts, pts); | ||
| 2127 | + } | ||
| 2128 | + } | ||
| 2129 | + | ||
| 2130 | + // 6B | ||
| 2131 | + if (ESCR_flag) { | ||
| 2132 | + stream->skip(6); | ||
| 2133 | + srs_warn("ts: demux PES, ignore the escr."); | ||
| 2134 | + } | ||
| 2135 | + | ||
| 2136 | + // 3B | ||
| 2137 | + if (ES_rate_flag) { | ||
| 2138 | + stream->skip(3); | ||
| 2139 | + srs_warn("ts: demux PES, ignore the ES_rate."); | ||
| 2140 | + } | ||
| 2141 | + | ||
| 2142 | + // 1B | ||
| 2143 | + if (DSM_trick_mode_flag) { | ||
| 2144 | + stream->skip(1); | ||
| 2145 | + srs_warn("ts: demux PES, ignore the DSM_trick_mode."); | ||
| 2146 | + } | ||
| 2147 | + | ||
| 2148 | + // 1B | ||
| 2149 | + if (additional_copy_info_flag) { | ||
| 2150 | + stream->skip(1); | ||
| 2151 | + srs_warn("ts: demux PES, ignore the additional_copy_info."); | ||
| 2152 | + } | ||
| 2153 | + | ||
| 2154 | + // 2B | ||
| 2155 | + if (PES_CRC_flag) { | ||
| 2156 | + stream->skip(2); | ||
| 2157 | + srs_warn("ts: demux PES, ignore the PES_CRC."); | ||
| 2158 | + } | ||
| 2159 | + | ||
| 2160 | + // 1B | ||
| 2161 | + if (PES_extension_flag) { | ||
| 2162 | + int8_t efv = PES_extension_flag_2 & 0x01; | ||
| 2163 | + efv |= (PES_private_data_flag << 7) & 0x80; | ||
| 2164 | + efv |= (pack_header_field_flag << 6) & 0x40; | ||
| 2165 | + efv |= (program_packet_sequence_counter_flag << 5) & 0x20; | ||
| 2166 | + efv |= (P_STD_buffer_flag << 4) & 0x10; | ||
| 2167 | + efv |= (const1_value0 << 1) & 0xE0; | ||
| 2168 | + stream->write_1bytes(efv); | ||
| 2169 | + | ||
| 2170 | + nb_required = 0; | ||
| 2171 | + nb_required += PES_private_data_flag? 16:0; | ||
| 2172 | + nb_required += pack_header_field_flag? 1+pack_field_length:0; // 1+x bytes. | ||
| 2173 | + nb_required += program_packet_sequence_counter_flag? 2:0; | ||
| 2174 | + nb_required += P_STD_buffer_flag? 2:0; | ||
| 2175 | + nb_required += PES_extension_flag_2? 1+PES_extension_field_length:0; // 1+x bytes. | ||
| 2176 | + if (!stream->require(nb_required)) { | ||
| 2177 | + ret = ERROR_STREAM_CASTER_TS_PSE; | ||
| 2178 | + srs_error("ts: mux PSE ext payload failed. ret=%d", ret); | ||
| 2179 | + return ret; | ||
| 2180 | + } | ||
| 2181 | + stream->skip(nb_required); | ||
| 2182 | + srs_warn("ts: demux PES, ignore the PES_extension."); | ||
| 2183 | + } | ||
| 2184 | + | ||
| 2185 | + // stuffing_byte | ||
| 2186 | + if (nb_stuffings) { | ||
| 2187 | + stream->skip(nb_stuffings); | ||
| 2188 | + srs_warn("ts: demux PES, ignore the stuffings."); | ||
| 2189 | + } | ||
| 2190 | + | ||
| 1676 | return ret; | 2191 | return ret; |
| 1677 | } | 2192 | } |
| 1678 | 2193 | ||
| @@ -1737,6 +2252,35 @@ int SrsTsPayloadPES::decode_33bits_dts_pts(SrsStream* stream, int64_t* pv) | @@ -1737,6 +2252,35 @@ int SrsTsPayloadPES::decode_33bits_dts_pts(SrsStream* stream, int64_t* pv) | ||
| 1737 | return ret; | 2252 | return ret; |
| 1738 | } | 2253 | } |
| 1739 | 2254 | ||
| 2255 | +int SrsTsPayloadPES::encode_33bits_dts_pts(SrsStream* stream, u_int8_t fb, int64_t v) | ||
| 2256 | +{ | ||
| 2257 | + int ret = ERROR_SUCCESS; | ||
| 2258 | + | ||
| 2259 | + if (!stream->require(5)) { | ||
| 2260 | + ret = ERROR_STREAM_CASTER_TS_PSE; | ||
| 2261 | + srs_error("ts: mux PSE dts/pts failed. ret=%d", ret); | ||
| 2262 | + return ret; | ||
| 2263 | + } | ||
| 2264 | + | ||
| 2265 | + char* p = stream->data() + stream->pos(); | ||
| 2266 | + stream->skip(5); | ||
| 2267 | + | ||
| 2268 | + int32_t val; | ||
| 2269 | + | ||
| 2270 | + val = fb << 4 | (((pts >> 30) & 0x07) << 1) | 1; | ||
| 2271 | + *p++ = val; | ||
| 2272 | + | ||
| 2273 | + val = (((pts >> 15) & 0x7fff) << 1) | 1; | ||
| 2274 | + *p++ = (val >> 8); | ||
| 2275 | + *p++ = val; | ||
| 2276 | + | ||
| 2277 | + val = (((pts) & 0x7fff) << 1) | 1; | ||
| 2278 | + *p++ = (val >> 8); | ||
| 2279 | + *p++ = val; | ||
| 2280 | + | ||
| 2281 | + return ret; | ||
| 2282 | +} | ||
| 2283 | + | ||
| 1740 | SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p) | 2284 | SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p) |
| 1741 | { | 2285 | { |
| 1742 | pointer_field = 0; | 2286 | pointer_field = 0; |
| @@ -2081,8 +2625,14 @@ int SrsTsPayloadPAT::psi_encode(SrsStream* stream) | @@ -2081,8 +2625,14 @@ int SrsTsPayloadPAT::psi_encode(SrsStream* stream) | ||
| 2081 | if ((ret = program->encode(stream)) != ERROR_SUCCESS) { | 2625 | if ((ret = program->encode(stream)) != ERROR_SUCCESS) { |
| 2082 | return ret; | 2626 | return ret; |
| 2083 | } | 2627 | } |
| 2628 | + | ||
| 2629 | + // update the apply pid table. | ||
| 2630 | + packet->context->set(program->pid, SrsTsPidApplyPMT); | ||
| 2084 | } | 2631 | } |
| 2085 | 2632 | ||
| 2633 | + // update the apply pid table. | ||
| 2634 | + packet->context->set(packet->pid, SrsTsPidApplyPAT); | ||
| 2635 | + | ||
| 2086 | return ret; | 2636 | return ret; |
| 2087 | } | 2637 | } |
| 2088 | 2638 | ||
| @@ -2341,8 +2891,28 @@ int SrsTsPayloadPMT::psi_encode(SrsStream* stream) | @@ -2341,8 +2891,28 @@ int SrsTsPayloadPMT::psi_encode(SrsStream* stream) | ||
| 2341 | if ((ret = info->encode(stream)) != ERROR_SUCCESS) { | 2891 | if ((ret = info->encode(stream)) != ERROR_SUCCESS) { |
| 2342 | return ret; | 2892 | return ret; |
| 2343 | } | 2893 | } |
| 2894 | + | ||
| 2895 | + // update the apply pid table | ||
| 2896 | + switch (info->stream_type) { | ||
| 2897 | + case SrsTsStreamVideoH264: | ||
| 2898 | + case SrsTsStreamVideoMpeg4: | ||
| 2899 | + packet->context->set(info->elementary_PID, SrsTsPidApplyVideo, info->stream_type); | ||
| 2900 | + break; | ||
| 2901 | + case SrsTsStreamAudioAAC: | ||
| 2902 | + case SrsTsStreamAudioAC3: | ||
| 2903 | + case SrsTsStreamAudioDTS: | ||
| 2904 | + case SrsTsStreamAudioMp3: | ||
| 2905 | + packet->context->set(info->elementary_PID, SrsTsPidApplyAudio, info->stream_type); | ||
| 2906 | + break; | ||
| 2907 | + default: | ||
| 2908 | + srs_warn("ts: drop pid=%#x, stream=%#x", info->elementary_PID, info->stream_type); | ||
| 2909 | + break; | ||
| 2910 | + } | ||
| 2344 | } | 2911 | } |
| 2345 | 2912 | ||
| 2913 | + // update the apply pid table. | ||
| 2914 | + packet->context->set(packet->pid, SrsTsPidApplyPMT); | ||
| 2915 | + | ||
| 2346 | return ret; | 2916 | return ret; |
| 2347 | } | 2917 | } |
| 2348 | 2918 | ||
| @@ -2387,11 +2957,11 @@ int SrsTSMuxer::update_acodec(SrsCodecAudio ac) | @@ -2387,11 +2957,11 @@ int SrsTSMuxer::update_acodec(SrsCodecAudio ac) | ||
| 2387 | return ERROR_SUCCESS; | 2957 | return ERROR_SUCCESS; |
| 2388 | } | 2958 | } |
| 2389 | 2959 | ||
| 2390 | -int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab) | 2960 | +int SrsTSMuxer::write_audio(SrsTsMessage* audio) |
| 2391 | { | 2961 | { |
| 2392 | int ret = ERROR_SUCCESS; | 2962 | int ret = ERROR_SUCCESS; |
| 2393 | 2963 | ||
| 2394 | - if ((ret = context->encode(writer, af, ab, vcodec, acodec)) != ERROR_SUCCESS) { | 2964 | + if ((ret = context->encode(writer, audio, vcodec, acodec)) != ERROR_SUCCESS) { |
| 2395 | srs_error("hls encode audio failed. ret=%d", ret); | 2965 | srs_error("hls encode audio failed. ret=%d", ret); |
| 2396 | return ret; | 2966 | return ret; |
| 2397 | } | 2967 | } |
| @@ -2400,11 +2970,11 @@ int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab) | @@ -2400,11 +2970,11 @@ int SrsTSMuxer::write_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab) | ||
| 2400 | return ret; | 2970 | return ret; |
| 2401 | } | 2971 | } |
| 2402 | 2972 | ||
| 2403 | -int SrsTSMuxer::write_video(SrsMpegtsFrame* vf, SrsSimpleBuffer* vb) | 2973 | +int SrsTSMuxer::write_video(SrsTsMessage* video) |
| 2404 | { | 2974 | { |
| 2405 | int ret = ERROR_SUCCESS; | 2975 | int ret = ERROR_SUCCESS; |
| 2406 | 2976 | ||
| 2407 | - if ((ret = context->encode(writer, vf, vb, vcodec, acodec)) != ERROR_SUCCESS) { | 2977 | + if ((ret = context->encode(writer, video, vcodec, acodec)) != ERROR_SUCCESS) { |
| 2408 | srs_error("hls encode video failed. ret=%d", ret); | 2978 | srs_error("hls encode video failed. ret=%d", ret); |
| 2409 | return ret; | 2979 | return ret; |
| 2410 | } | 2980 | } |
| @@ -2419,143 +2989,45 @@ void SrsTSMuxer::close() | @@ -2419,143 +2989,45 @@ void SrsTSMuxer::close() | ||
| 2419 | writer->close(); | 2989 | writer->close(); |
| 2420 | } | 2990 | } |
| 2421 | 2991 | ||
| 2422 | -SrsTsAacJitter::SrsTsAacJitter() | ||
| 2423 | -{ | ||
| 2424 | - base_pts = 0; | ||
| 2425 | - nb_samples = 0; | ||
| 2426 | - | ||
| 2427 | - // TODO: config it, 0 means no adjust | ||
| 2428 | - sync_ms = SRS_CONF_DEFAULT_AAC_SYNC; | ||
| 2429 | -} | ||
| 2430 | - | ||
| 2431 | -SrsTsAacJitter::~SrsTsAacJitter() | ||
| 2432 | -{ | ||
| 2433 | -} | ||
| 2434 | - | ||
| 2435 | -int64_t SrsTsAacJitter::on_buffer_start(int64_t flv_pts, int sample_rate, int aac_sample_rate) | ||
| 2436 | -{ | ||
| 2437 | - // use sample rate in flv/RTMP. | ||
| 2438 | - int flv_sample_rate = flv_sample_rates[sample_rate & 0x03]; | ||
| 2439 | - | ||
| 2440 | - // override the sample rate by sequence header | ||
| 2441 | - if (aac_sample_rate != __SRS_AAC_SAMPLE_RATE_UNSET) { | ||
| 2442 | - flv_sample_rate = aac_sample_rates[aac_sample_rate]; | ||
| 2443 | - } | ||
| 2444 | - | ||
| 2445 | - // sync time set to 0, donot adjust the aac timestamp. | ||
| 2446 | - if (!sync_ms) { | ||
| 2447 | - return flv_pts; | ||
| 2448 | - } | ||
| 2449 | - | ||
| 2450 | - // @see: ngx_rtmp_hls_audio | ||
| 2451 | - // drop the rtmp audio packet timestamp, re-calc it by sample rate. | ||
| 2452 | - // | ||
| 2453 | - // resample for the tbn of ts is 90000, flv is 1000, | ||
| 2454 | - // we will lost timestamp if use audio packet timestamp, | ||
| 2455 | - // so we must resample. or audio will corupt in IOS. | ||
| 2456 | - int64_t est_pts = base_pts + nb_samples * 90000LL * _SRS_AAC_SAMPLE_SIZE / flv_sample_rate; | ||
| 2457 | - int64_t dpts = (int64_t) (est_pts - flv_pts); | ||
| 2458 | - | ||
| 2459 | - if (dpts <= (int64_t) sync_ms * 90 && dpts >= (int64_t) sync_ms * -90) { | ||
| 2460 | - srs_info("HLS correct aac pts " | ||
| 2461 | - "from %"PRId64" to %"PRId64", base=%"PRId64", nb_samples=%d, sample_rate=%d", | ||
| 2462 | - flv_pts, est_pts, nb_samples, flv_sample_rate, base_pts); | ||
| 2463 | - | ||
| 2464 | - nb_samples++; | ||
| 2465 | - | ||
| 2466 | - return est_pts; | ||
| 2467 | - } | ||
| 2468 | - | ||
| 2469 | - // resync | ||
| 2470 | - srs_trace("HLS aac resync, dpts=%"PRId64", pts=%"PRId64 | ||
| 2471 | - ", base=%"PRId64", nb_samples=%"PRId64", sample_rate=%d", | ||
| 2472 | - dpts, flv_pts, base_pts, nb_samples, flv_sample_rate); | ||
| 2473 | - | ||
| 2474 | - base_pts = flv_pts; | ||
| 2475 | - nb_samples = 1; | ||
| 2476 | - | ||
| 2477 | - return flv_pts; | ||
| 2478 | -} | ||
| 2479 | - | ||
| 2480 | -void SrsTsAacJitter::on_buffer_continue() | ||
| 2481 | -{ | ||
| 2482 | - nb_samples++; | ||
| 2483 | -} | ||
| 2484 | - | ||
| 2485 | SrsTsCache::SrsTsCache() | 2992 | SrsTsCache::SrsTsCache() |
| 2486 | { | 2993 | { |
| 2487 | - aac_jitter = new SrsTsAacJitter(); | ||
| 2488 | - | ||
| 2489 | - ab = new SrsSimpleBuffer(); | ||
| 2490 | - vb = new SrsSimpleBuffer(); | ||
| 2491 | - | ||
| 2492 | - af = new SrsMpegtsFrame(); | ||
| 2493 | - vf = new SrsMpegtsFrame(); | ||
| 2494 | - | ||
| 2495 | - audio_buffer_start_pts = 0; | 2994 | + audio = NULL; |
| 2995 | + video = NULL; | ||
| 2496 | } | 2996 | } |
| 2497 | 2997 | ||
| 2498 | SrsTsCache::~SrsTsCache() | 2998 | SrsTsCache::~SrsTsCache() |
| 2499 | { | 2999 | { |
| 2500 | - srs_freep(aac_jitter); | ||
| 2501 | - | ||
| 2502 | - ab->erase(ab->length()); | ||
| 2503 | - vb->erase(vb->length()); | ||
| 2504 | - | ||
| 2505 | - srs_freep(ab); | ||
| 2506 | - srs_freep(vb); | ||
| 2507 | - | ||
| 2508 | - srs_freep(af); | ||
| 2509 | - srs_freep(vf); | 3000 | + srs_freep(audio); |
| 3001 | + srs_freep(video); | ||
| 2510 | } | 3002 | } |
| 2511 | 3003 | ||
| 2512 | -int SrsTsCache::cache_audio(SrsAvcAacCodec* codec, int64_t pts, SrsCodecSample* sample) | 3004 | +int SrsTsCache::cache_audio(SrsAvcAacCodec* codec, int64_t dts, SrsCodecSample* sample) |
| 2513 | { | 3005 | { |
| 2514 | int ret = ERROR_SUCCESS; | 3006 | int ret = ERROR_SUCCESS; |
| 2515 | 3007 | ||
| 2516 | - // @remark, always use the orignal pts. | ||
| 2517 | - if (ab->length() == 0) { | ||
| 2518 | - audio_buffer_start_pts = pts; | 3008 | + // create the ts audio message. |
| 3009 | + if (!audio) { | ||
| 3010 | + audio = new SrsTsMessage(); | ||
| 3011 | + audio->write_pcr = false; | ||
| 3012 | + audio->start_pts = dts; | ||
| 2519 | } | 3013 | } |
| 3014 | + | ||
| 3015 | + audio->dts = dts; | ||
| 3016 | + audio->pts = audio->dts; | ||
| 3017 | + audio->sid = SrsTsPESStreamIdAudioCommon; | ||
| 2520 | 3018 | ||
| 2521 | // must be aac or mp3 | 3019 | // must be aac or mp3 |
| 2522 | SrsCodecAudio acodec = (SrsCodecAudio)codec->audio_codec_id; | 3020 | SrsCodecAudio acodec = (SrsCodecAudio)codec->audio_codec_id; |
| 2523 | srs_assert(acodec == SrsCodecAudioAAC || acodec == SrsCodecAudioMP3); | 3021 | srs_assert(acodec == SrsCodecAudioAAC || acodec == SrsCodecAudioMP3); |
| 2524 | 3022 | ||
| 2525 | - // cache the aac audio. | 3023 | + // write video to cache. |
| 2526 | if (codec->audio_codec_id == SrsCodecAudioAAC) { | 3024 | if (codec->audio_codec_id == SrsCodecAudioAAC) { |
| 2527 | - // for aac audio, recalc the timestamp by aac jitter. | ||
| 2528 | - if (ab->length() == 0) { | ||
| 2529 | - pts = aac_jitter->on_buffer_start(pts, sample->sound_rate, codec->aac_sample_rate); | ||
| 2530 | - | ||
| 2531 | - af->dts = af->pts = pts; | ||
| 2532 | - af->pid = TS_AUDIO_PID; | ||
| 2533 | - af->sid = TS_AUDIO_AAC; | ||
| 2534 | - } else { | ||
| 2535 | - aac_jitter->on_buffer_continue(); | ||
| 2536 | - } | ||
| 2537 | - | ||
| 2538 | - // write aac audio to cache. | ||
| 2539 | - if ((ret = do_cache_audio(codec, sample)) != ERROR_SUCCESS) { | 3025 | + if ((ret = do_cache_aac(codec, sample)) != ERROR_SUCCESS) { |
| 2540 | return ret; | 3026 | return ret; |
| 2541 | } | 3027 | } |
| 2542 | - | ||
| 2543 | - return ret; | ||
| 2544 | - } | ||
| 2545 | - | ||
| 2546 | - // cache the mp3 audio. | ||
| 2547 | - if (codec->audio_codec_id == SrsCodecAudioMP3) { | ||
| 2548 | - // for mp3 audio, recalc the timestamp by mp3 jitter. | ||
| 2549 | - // TODO: FIXME: implements it. | ||
| 2550 | - af->dts = af->pts = pts; | ||
| 2551 | - af->pid = TS_AUDIO_PID; | ||
| 2552 | - af->sid = SrsCodecAudioMP3; | ||
| 2553 | - | ||
| 2554 | - // for mp3, directly write to cache. | ||
| 2555 | - // TODO: FIXME: implements it. | ||
| 2556 | - for (int i = 0; i < sample->nb_sample_units; i++) { | ||
| 2557 | - SrsCodecSampleUnit* sample_unit = &sample->sample_units[i]; | ||
| 2558 | - ab->append(sample_unit->bytes, sample_unit->size); | 3028 | + } else { |
| 3029 | + if ((ret = do_cache_mp3(codec, sample)) != ERROR_SUCCESS) { | ||
| 3030 | + return ret; | ||
| 2559 | } | 3031 | } |
| 2560 | } | 3032 | } |
| 2561 | 3033 | ||
| @@ -2566,21 +3038,40 @@ int SrsTsCache::cache_video(SrsAvcAacCodec* codec, int64_t dts, SrsCodecSample* | @@ -2566,21 +3038,40 @@ int SrsTsCache::cache_video(SrsAvcAacCodec* codec, int64_t dts, SrsCodecSample* | ||
| 2566 | { | 3038 | { |
| 2567 | int ret = ERROR_SUCCESS; | 3039 | int ret = ERROR_SUCCESS; |
| 2568 | 3040 | ||
| 3041 | + // create the ts video message. | ||
| 3042 | + if (!video) { | ||
| 3043 | + video = new SrsTsMessage(); | ||
| 3044 | + video->write_pcr = sample->frame_type == SrsCodecVideoAVCFrameKeyFrame; | ||
| 3045 | + video->start_pts = dts; | ||
| 3046 | + } | ||
| 3047 | + | ||
| 3048 | + video->dts = dts; | ||
| 3049 | + video->pts = video->dts + sample->cts * 90; | ||
| 3050 | + video->sid = SrsTsPESStreamIdVideoCommon; | ||
| 3051 | + | ||
| 2569 | // write video to cache. | 3052 | // write video to cache. |
| 2570 | - if ((ret = do_cache_video(codec, sample)) != ERROR_SUCCESS) { | 3053 | + if ((ret = do_cache_avc(codec, sample)) != ERROR_SUCCESS) { |
| 2571 | return ret; | 3054 | return ret; |
| 2572 | } | 3055 | } |
| 2573 | - | ||
| 2574 | - vf->dts = dts; | ||
| 2575 | - vf->pts = vf->dts + sample->cts * 90; | ||
| 2576 | - vf->pid = TS_VIDEO_PID; | ||
| 2577 | - vf->sid = TS_VIDEO_AVC; | ||
| 2578 | - vf->write_pcr = sample->frame_type == SrsCodecVideoAVCFrameKeyFrame; | 3056 | + |
| 3057 | + return ret; | ||
| 3058 | +} | ||
| 3059 | + | ||
| 3060 | +int SrsTsCache::do_cache_mp3(SrsAvcAacCodec* codec, SrsCodecSample* sample) | ||
| 3061 | +{ | ||
| 3062 | + int ret = ERROR_SUCCESS; | ||
| 3063 | + | ||
| 3064 | + // for mp3, directly write to cache. | ||
| 3065 | + // TODO: FIXME: implements the ts jitter. | ||
| 3066 | + for (int i = 0; i < sample->nb_sample_units; i++) { | ||
| 3067 | + SrsCodecSampleUnit* sample_unit = &sample->sample_units[i]; | ||
| 3068 | + audio->payload->append(sample_unit->bytes, sample_unit->size); | ||
| 3069 | + } | ||
| 2579 | 3070 | ||
| 2580 | return ret; | 3071 | return ret; |
| 2581 | } | 3072 | } |
| 2582 | 3073 | ||
| 2583 | -int SrsTsCache::do_cache_audio(SrsAvcAacCodec* codec, SrsCodecSample* sample) | 3074 | +int SrsTsCache::do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample) |
| 2584 | { | 3075 | { |
| 2585 | int ret = ERROR_SUCCESS; | 3076 | int ret = ERROR_SUCCESS; |
| 2586 | 3077 | ||
| @@ -2654,14 +3145,14 @@ int SrsTsCache::do_cache_audio(SrsAvcAacCodec* codec, SrsCodecSample* sample) | @@ -2654,14 +3145,14 @@ int SrsTsCache::do_cache_audio(SrsAvcAacCodec* codec, SrsCodecSample* sample) | ||
| 2654 | adts_header[5] |= 0x1f; | 3145 | adts_header[5] |= 0x1f; |
| 2655 | 3146 | ||
| 2656 | // copy to audio buffer | 3147 | // copy to audio buffer |
| 2657 | - ab->append((const char*)adts_header, sizeof(adts_header)); | ||
| 2658 | - ab->append(sample_unit->bytes, sample_unit->size); | 3148 | + audio->payload->append((const char*)adts_header, sizeof(adts_header)); |
| 3149 | + audio->payload->append(sample_unit->bytes, sample_unit->size); | ||
| 2659 | } | 3150 | } |
| 2660 | 3151 | ||
| 2661 | return ret; | 3152 | return ret; |
| 2662 | } | 3153 | } |
| 2663 | 3154 | ||
| 2664 | -int SrsTsCache::do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample) | 3155 | +int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample) |
| 2665 | { | 3156 | { |
| 2666 | int ret = ERROR_SUCCESS; | 3157 | int ret = ERROR_SUCCESS; |
| 2667 | 3158 | ||
| @@ -2720,7 +3211,7 @@ int SrsTsCache::do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample) | @@ -2720,7 +3211,7 @@ int SrsTsCache::do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample) | ||
| 2720 | 3211 | ||
| 2721 | if (nal_unit_type == 1 || nal_unit_type == 5 || nal_unit_type == 6) { | 3212 | if (nal_unit_type == 1 || nal_unit_type == 5 || nal_unit_type == 6) { |
| 2722 | // for type 6, append a aud with type 9. | 3213 | // for type 6, append a aud with type 9. |
| 2723 | - vb->append((const char*)aud_nal, sizeof(aud_nal)); | 3214 | + video->payload->append((const char*)aud_nal, sizeof(aud_nal)); |
| 2724 | aud_sent = true; | 3215 | aud_sent = true; |
| 2725 | } | 3216 | } |
| 2726 | } | 3217 | } |
| @@ -2733,15 +3224,15 @@ int SrsTsCache::do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample) | @@ -2733,15 +3224,15 @@ int SrsTsCache::do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample) | ||
| 2733 | // @see: ngx_rtmp_hls_append_sps_pps | 3224 | // @see: ngx_rtmp_hls_append_sps_pps |
| 2734 | if (codec->sequenceParameterSetLength > 0) { | 3225 | if (codec->sequenceParameterSetLength > 0) { |
| 2735 | // AnnexB prefix, for sps always 4 bytes header | 3226 | // AnnexB prefix, for sps always 4 bytes header |
| 2736 | - vb->append((const char*)aud_nal, 4); | 3227 | + video->payload->append((const char*)aud_nal, 4); |
| 2737 | // sps | 3228 | // sps |
| 2738 | - vb->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength); | 3229 | + video->payload->append(codec->sequenceParameterSetNALUnit, codec->sequenceParameterSetLength); |
| 2739 | } | 3230 | } |
| 2740 | if (codec->pictureParameterSetLength > 0) { | 3231 | if (codec->pictureParameterSetLength > 0) { |
| 2741 | // AnnexB prefix, for pps always 4 bytes header | 3232 | // AnnexB prefix, for pps always 4 bytes header |
| 2742 | - vb->append((const char*)aud_nal, 4); | 3233 | + video->payload->append((const char*)aud_nal, 4); |
| 2743 | // pps | 3234 | // pps |
| 2744 | - vb->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength); | 3235 | + video->payload->append(codec->pictureParameterSetNALUnit, codec->pictureParameterSetLength); |
| 2745 | } | 3236 | } |
| 2746 | } | 3237 | } |
| 2747 | 3238 | ||
| @@ -2761,13 +3252,13 @@ int SrsTsCache::do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample) | @@ -2761,13 +3252,13 @@ int SrsTsCache::do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample) | ||
| 2761 | u_int8_t* end = p + 3; | 3252 | u_int8_t* end = p + 3; |
| 2762 | 3253 | ||
| 2763 | // first AnnexB prefix is long (4 bytes) | 3254 | // first AnnexB prefix is long (4 bytes) |
| 2764 | - if (vb->length() == 0) { | 3255 | + if (video->payload->length() == 0) { |
| 2765 | p = aud_nal; | 3256 | p = aud_nal; |
| 2766 | } | 3257 | } |
| 2767 | - vb->append((const char*)p, end - p); | 3258 | + video->payload->append((const char*)p, end - p); |
| 2768 | 3259 | ||
| 2769 | // sample data | 3260 | // sample data |
| 2770 | - vb->append(sample_unit->bytes, sample_unit->size); | 3261 | + video->payload->append(sample_unit->bytes, sample_unit->size); |
| 2771 | } | 3262 | } |
| 2772 | 3263 | ||
| 2773 | return ret; | 3264 | return ret; |
| @@ -2858,7 +3349,7 @@ int SrsTsEncoder::write_audio(int64_t timestamp, char* data, int size) | @@ -2858,7 +3349,7 @@ int SrsTsEncoder::write_audio(int64_t timestamp, char* data, int size) | ||
| 2858 | } | 3349 | } |
| 2859 | 3350 | ||
| 2860 | // flush if buffer exceed max size. | 3351 | // flush if buffer exceed max size. |
| 2861 | - if (cache->ab->length() > SRS_AUTO_HLS_AUDIO_CACHE_SIZE) { | 3352 | + if (cache->audio->payload->length() > SRS_AUTO_HLS_AUDIO_CACHE_SIZE) { |
| 2862 | return flush_video(); | 3353 | return flush_video(); |
| 2863 | } | 3354 | } |
| 2864 | 3355 | ||
| @@ -2866,7 +3357,7 @@ int SrsTsEncoder::write_audio(int64_t timestamp, char* data, int size) | @@ -2866,7 +3357,7 @@ int SrsTsEncoder::write_audio(int64_t timestamp, char* data, int size) | ||
| 2866 | // in ms, audio delay to flush the audios. | 3357 | // in ms, audio delay to flush the audios. |
| 2867 | int64_t audio_delay = SRS_CONF_DEFAULT_AAC_DELAY; | 3358 | int64_t audio_delay = SRS_CONF_DEFAULT_AAC_DELAY; |
| 2868 | // flush if audio delay exceed | 3359 | // flush if audio delay exceed |
| 2869 | - if (dts - cache->audio_buffer_start_pts > audio_delay * 90) { | 3360 | + if (dts - cache->audio->start_pts > audio_delay * 90) { |
| 2870 | return flush_audio(); | 3361 | return flush_audio(); |
| 2871 | } | 3362 | } |
| 2872 | 3363 | ||
| @@ -2913,12 +3404,12 @@ int SrsTsEncoder::flush_audio() | @@ -2913,12 +3404,12 @@ int SrsTsEncoder::flush_audio() | ||
| 2913 | { | 3404 | { |
| 2914 | int ret = ERROR_SUCCESS; | 3405 | int ret = ERROR_SUCCESS; |
| 2915 | 3406 | ||
| 2916 | - if ((ret = muxer->write_audio(cache->af, cache->ab)) != ERROR_SUCCESS) { | 3407 | + if ((ret = muxer->write_audio(cache->audio)) != ERROR_SUCCESS) { |
| 2917 | return ret; | 3408 | return ret; |
| 2918 | } | 3409 | } |
| 2919 | 3410 | ||
| 2920 | - // write success, clear and free the buffer | ||
| 2921 | - cache->ab->erase(cache->ab->length()); | 3411 | + // write success, clear and free the ts message. |
| 3412 | + srs_freep(cache->audio); | ||
| 2922 | 3413 | ||
| 2923 | return ret; | 3414 | return ret; |
| 2924 | } | 3415 | } |
| @@ -2927,12 +3418,12 @@ int SrsTsEncoder::flush_video() | @@ -2927,12 +3418,12 @@ int SrsTsEncoder::flush_video() | ||
| 2927 | { | 3418 | { |
| 2928 | int ret = ERROR_SUCCESS; | 3419 | int ret = ERROR_SUCCESS; |
| 2929 | 3420 | ||
| 2930 | - if ((ret = muxer->write_video(cache->vf, cache->vb)) != ERROR_SUCCESS) { | 3421 | + if ((ret = muxer->write_video(cache->video)) != ERROR_SUCCESS) { |
| 2931 | return ret; | 3422 | return ret; |
| 2932 | } | 3423 | } |
| 2933 | 3424 | ||
| 2934 | - // write success, clear and free the buffer | ||
| 2935 | - cache->vb->erase(cache->vb->length()); | 3425 | + // write success, clear and free the ts message. |
| 3426 | + srs_freep(cache->video); | ||
| 2936 | 3427 | ||
| 2937 | return ret; | 3428 | return ret; |
| 2938 | } | 3429 | } |
| @@ -185,6 +185,8 @@ struct SrsTsChannel | @@ -185,6 +185,8 @@ struct SrsTsChannel | ||
| 185 | SrsTsPidApply apply; | 185 | SrsTsPidApply apply; |
| 186 | SrsTsStream stream; | 186 | SrsTsStream stream; |
| 187 | SrsTsMessage* msg; | 187 | SrsTsMessage* msg; |
| 188 | + // for encoder. | ||
| 189 | + u_int8_t continuity_counter; | ||
| 188 | 190 | ||
| 189 | SrsTsChannel(); | 191 | SrsTsChannel(); |
| 190 | virtual ~SrsTsChannel(); | 192 | virtual ~SrsTsChannel(); |
| @@ -209,13 +211,17 @@ enum SrsTsPESStreamId | @@ -209,13 +211,17 @@ enum SrsTsPESStreamId | ||
| 209 | // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC | 211 | // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC |
| 210 | // 14496-3 audio stream number x xxxx | 212 | // 14496-3 audio stream number x xxxx |
| 211 | // ((sid >> 5) & 0x07) == SrsTsPESStreamIdAudio | 213 | // ((sid >> 5) & 0x07) == SrsTsPESStreamIdAudio |
| 212 | - SrsTsPESStreamIdAudio = 0x06, // 0b110 | 214 | + // @remark, use SrsTsPESStreamIdAudioCommon as actually audio, SrsTsPESStreamIdAudio to check whether audio. |
| 215 | + SrsTsPESStreamIdAudioChecker = 0x06, // 0b110 | ||
| 216 | + SrsTsPESStreamIdAudioCommon = 0xc0, | ||
| 213 | 217 | ||
| 214 | // 1110 xxxx | 218 | // 1110 xxxx |
| 215 | // ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC | 219 | // ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC |
| 216 | // 14496-2 video stream number xxxx | 220 | // 14496-2 video stream number xxxx |
| 217 | // ((stream_id >> 4) & 0x0f) == SrsTsPESStreamIdVideo | 221 | // ((stream_id >> 4) & 0x0f) == SrsTsPESStreamIdVideo |
| 218 | - SrsTsPESStreamIdVideo = 0x0e, // 0b1110 | 222 | + // @remark, use SrsTsPESStreamIdVideoCommon as actually video, SrsTsPESStreamIdVideo to check whether video. |
| 223 | + SrsTsPESStreamIdVideoChecker = 0x0e, // 0b1110 | ||
| 224 | + SrsTsPESStreamIdVideoCommon = 0xe0, | ||
| 219 | 225 | ||
| 220 | // ECM_stream | 226 | // ECM_stream |
| 221 | SrsTsPESStreamIdEcmStream = 0xf0, // 0b11110000 | 227 | SrsTsPESStreamIdEcmStream = 0xf0, // 0b11110000 |
| @@ -253,9 +259,21 @@ enum SrsTsPESStreamId | @@ -253,9 +259,21 @@ enum SrsTsPESStreamId | ||
| 253 | class SrsTsMessage | 259 | class SrsTsMessage |
| 254 | { | 260 | { |
| 255 | public: | 261 | public: |
| 262 | + // decoder only, | ||
| 263 | + // the ts messgae does not use them, | ||
| 264 | + // for user to get the channel and packet. | ||
| 256 | SrsTsChannel* channel; | 265 | SrsTsChannel* channel; |
| 257 | SrsTsPacket* packet; | 266 | SrsTsPacket* packet; |
| 258 | public: | 267 | public: |
| 268 | + // the audio cache buffer start pts, to flush audio if full. | ||
| 269 | + // @remark the pts is not the adjust one, it's the orignal pts. | ||
| 270 | + int64_t start_pts; | ||
| 271 | + // whether this message with pcr info, | ||
| 272 | + // generally, the video IDR(I frame, the keyframe of h.264) carray the pcr info. | ||
| 273 | + bool write_pcr; | ||
| 274 | + // whether got discontinuity ts, for example, sequence header changed. | ||
| 275 | + bool discontinuity; | ||
| 276 | +public: | ||
| 259 | // the timestamp in 90khz | 277 | // the timestamp in 90khz |
| 260 | int64_t dts; | 278 | int64_t dts; |
| 261 | int64_t pts; | 279 | int64_t pts; |
| @@ -269,8 +287,9 @@ public: | @@ -269,8 +287,9 @@ public: | ||
| 269 | // the payload bytes. | 287 | // the payload bytes. |
| 270 | SrsSimpleBuffer* payload; | 288 | SrsSimpleBuffer* payload; |
| 271 | public: | 289 | public: |
| 272 | - SrsTsMessage(SrsTsChannel* c, SrsTsPacket* p); | 290 | + SrsTsMessage(SrsTsChannel* c = NULL, SrsTsPacket* p = NULL); |
| 273 | virtual ~SrsTsMessage(); | 291 | virtual ~SrsTsMessage(); |
| 292 | +// decoder | ||
| 274 | public: | 293 | public: |
| 275 | /** | 294 | /** |
| 276 | * dumps all bytes in stream to ts message. | 295 | * dumps all bytes in stream to ts message. |
| @@ -361,15 +380,14 @@ public: | @@ -361,15 +380,14 @@ public: | ||
| 361 | public: | 380 | public: |
| 362 | /** | 381 | /** |
| 363 | * write the PES packet, the video/audio stream. | 382 | * write the PES packet, the video/audio stream. |
| 364 | - * @param frame the video/audio frame info. | ||
| 365 | - * @param payload the video/audio payload bytes. | 383 | + * @param msg the video/audio msg to write to ts. |
| 366 | * @param vc the video codec, write the PAT/PMT table when changed. | 384 | * @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. | 385 | * @param ac the audio codec, write the PAT/PMT table when changed. |
| 368 | */ | 386 | */ |
| 369 | - virtual int encode(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload, SrsCodecVideo vc, SrsCodecAudio ac); | 387 | + virtual int encode(SrsFileWriter* writer, SrsTsMessage* msg, SrsCodecVideo vc, SrsCodecAudio ac); |
| 370 | private: | 388 | private: |
| 371 | - virtual int encode_pat_pmt(SrsFileWriter* writer, SrsCodecVideo vcodec, SrsCodecAudio acodec); | ||
| 372 | - virtual int encode_pes(SrsFileWriter* writer, SrsMpegtsFrame* frame, SrsSimpleBuffer* payload); | 389 | + virtual int encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStream vs, int16_t apid, SrsTsStream as); |
| 390 | + virtual int encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, int16_t pid, SrsTsStream sid); | ||
| 373 | }; | 391 | }; |
| 374 | 392 | ||
| 375 | /** | 393 | /** |
| @@ -478,9 +496,22 @@ public: | @@ -478,9 +496,22 @@ public: | ||
| 478 | public: | 496 | public: |
| 479 | virtual int size(); | 497 | virtual int size(); |
| 480 | virtual int encode(SrsStream* stream); | 498 | virtual int encode(SrsStream* stream); |
| 499 | + virtual void padding(int nb_stuffings); | ||
| 481 | public: | 500 | 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); | 501 | + static SrsTsPacket* create_pat(SrsTsContext* context, |
| 502 | + int16_t pmt_number, int16_t pmt_pid | ||
| 503 | + ); | ||
| 504 | + static SrsTsPacket* create_pmt(SrsTsContext* context, | ||
| 505 | + int16_t pmt_number, int16_t pmt_pid, int16_t vpid, SrsTsStream vs, | ||
| 506 | + int16_t apid, SrsTsStream as | ||
| 507 | + ); | ||
| 508 | + static SrsTsPacket* create_pes_first(SrsTsContext* context, | ||
| 509 | + int16_t pid, SrsTsPESStreamId sid, u_int8_t continuity_counter, bool discontinuity, | ||
| 510 | + int64_t pcr, int64_t dts, int64_t pts, int size | ||
| 511 | + ); | ||
| 512 | + static SrsTsPacket* create_pes_continue(SrsTsContext* context, | ||
| 513 | + int16_t pid, SrsTsPESStreamId sid, u_int8_t continuity_counter | ||
| 514 | + ); | ||
| 484 | }; | 515 | }; |
| 485 | 516 | ||
| 486 | /** | 517 | /** |
| @@ -627,7 +658,10 @@ public: | @@ -627,7 +658,10 @@ public: | ||
| 627 | * the last bit of the program_clock_reference_base at the input of the system target decoder. | 658 | * the last bit of the program_clock_reference_base at the input of the system target decoder. |
| 628 | */ | 659 | */ |
| 629 | int64_t program_clock_reference_base; //33bits | 660 | int64_t program_clock_reference_base; //33bits |
| 630 | - //6bits reserved. | 661 | + /** |
| 662 | + * 6bits reserved, must be '1' | ||
| 663 | + */ | ||
| 664 | + int8_t const1_value0; // 6bits | ||
| 631 | int16_t program_clock_reference_extension; //9bits | 665 | int16_t program_clock_reference_extension; //9bits |
| 632 | 666 | ||
| 633 | // if OPCR_flag, 6B | 667 | // if OPCR_flag, 6B |
| @@ -646,7 +680,10 @@ public: | @@ -646,7 +680,10 @@ public: | ||
| 646 | * in the original single program Transport Stream. | 680 | * in the original single program Transport Stream. |
| 647 | */ | 681 | */ |
| 648 | int64_t original_program_clock_reference_base; //33bits | 682 | int64_t original_program_clock_reference_base; //33bits |
| 649 | - //6bits reserved. | 683 | + /** |
| 684 | + * 6bits reserved, must be '1' | ||
| 685 | + */ | ||
| 686 | + int8_t const1_value2; // 6bits | ||
| 650 | int16_t original_program_clock_reference_extension; //9bits | 687 | int16_t original_program_clock_reference_extension; //9bits |
| 651 | 688 | ||
| 652 | // if splicing_point_flag, 1B | 689 | // if splicing_point_flag, 1B |
| @@ -711,7 +748,10 @@ public: | @@ -711,7 +748,10 @@ public: | ||
| 711 | * constraints indicated by the splice_type value. | 748 | * constraints indicated by the splice_type value. |
| 712 | */ | 749 | */ |
| 713 | int8_t seamless_splice_flag; //1bit | 750 | int8_t seamless_splice_flag; //1bit |
| 714 | - //5bits reserved | 751 | + /** |
| 752 | + * reserved 5bits, must be '1' | ||
| 753 | + */ | ||
| 754 | + int8_t const1_value1; //5bits | ||
| 715 | // if ltw_flag, 2B | 755 | // if ltw_flag, 2B |
| 716 | /** | 756 | /** |
| 717 | * (legal time window_valid_flag) - This is a 1-bit field which when set to '1' indicates that the value of the | 757 | * (legal time window_valid_flag) - This is a 1-bit field which when set to '1' indicates that the value of the |
| @@ -856,6 +896,7 @@ public: | @@ -856,6 +896,7 @@ public: | ||
| 856 | * elementary stream type as defined in Table 2-18. In Transport Streams, the elementary stream type is specified in the | 896 | * elementary stream type as defined in Table 2-18. In Transport Streams, the elementary stream type is specified in the |
| 857 | * Program Specific Information as specified in 2.4.4. | 897 | * Program Specific Information as specified in 2.4.4. |
| 858 | */ | 898 | */ |
| 899 | + // @see SrsTsPESStreamId, value can be SrsTsPESStreamIdAudioCommon or SrsTsPESStreamIdVideoCommon. | ||
| 859 | u_int8_t stream_id; //8bits | 900 | u_int8_t stream_id; //8bits |
| 860 | // 2B | 901 | // 2B |
| 861 | /** | 902 | /** |
| @@ -866,7 +907,10 @@ public: | @@ -866,7 +907,10 @@ public: | ||
| 866 | u_int16_t PES_packet_length; //16bits | 907 | u_int16_t PES_packet_length; //16bits |
| 867 | 908 | ||
| 868 | // 1B | 909 | // 1B |
| 869 | - // 2bits const '10' | 910 | + /** |
| 911 | + * 2bits const '10' | ||
| 912 | + */ | ||
| 913 | + int8_t const2bits; //2bits | ||
| 870 | /** | 914 | /** |
| 871 | * The 2-bit PES_scrambling_control field indicates the scrambling mode of the PES packet | 915 | * The 2-bit PES_scrambling_control field indicates the scrambling mode of the PES packet |
| 872 | * payload. When scrambling is performed at the PES level, the PES packet header, including the optional fields when | 916 | * payload. When scrambling is performed at the PES level, the PES packet header, including the optional fields when |
| @@ -1068,7 +1112,10 @@ public: | @@ -1068,7 +1112,10 @@ public: | ||
| 1068 | * PES header. | 1112 | * PES header. |
| 1069 | */ | 1113 | */ |
| 1070 | int8_t P_STD_buffer_flag; //1bit | 1114 | int8_t P_STD_buffer_flag; //1bit |
| 1071 | - // reserved 3bits | 1115 | + /** |
| 1116 | + * reverved value, must be '1' | ||
| 1117 | + */ | ||
| 1118 | + int8_t const1_value0; //3bits | ||
| 1072 | /** | 1119 | /** |
| 1073 | * A 1-bit field which when set to '1' indicates the presence of the PES_extension_field_length | 1120 | * A 1-bit field which when set to '1' indicates the presence of the PES_extension_field_length |
| 1074 | * field and associated fields. When set to a value of '0' this indicates that the PES_extension_field_length field and any | 1121 | * field and associated fields. When set to a value of '0' this indicates that the PES_extension_field_length field and any |
| @@ -1179,6 +1226,7 @@ public: | @@ -1179,6 +1226,7 @@ public: | ||
| 1179 | virtual int encode(SrsStream* stream); | 1226 | virtual int encode(SrsStream* stream); |
| 1180 | private: | 1227 | private: |
| 1181 | virtual int decode_33bits_dts_pts(SrsStream* stream, int64_t* pv); | 1228 | virtual int decode_33bits_dts_pts(SrsStream* stream, int64_t* pv); |
| 1229 | + virtual int encode_33bits_dts_pts(SrsStream* stream, u_int8_t fb, int64_t v); | ||
| 1182 | }; | 1230 | }; |
| 1183 | 1231 | ||
| 1184 | /** | 1232 | /** |
| @@ -1516,14 +1564,12 @@ public: | @@ -1516,14 +1564,12 @@ public: | ||
| 1516 | virtual int update_acodec(SrsCodecAudio ac); | 1564 | virtual int update_acodec(SrsCodecAudio ac); |
| 1517 | /** | 1565 | /** |
| 1518 | * write an audio frame to ts, | 1566 | * write an audio frame to ts, |
| 1519 | - * @remark write PSI first when not write yet. | ||
| 1520 | */ | 1567 | */ |
| 1521 | - virtual int write_audio(SrsMpegtsFrame* af, SrsSimpleBuffer* ab); | 1568 | + virtual int write_audio(SrsTsMessage* audio); |
| 1522 | /** | 1569 | /** |
| 1523 | * write a video frame to ts, | 1570 | * write a video frame to ts, |
| 1524 | - * @remark write PSI first when not write yet. | ||
| 1525 | */ | 1571 | */ |
| 1526 | - virtual int write_video(SrsMpegtsFrame* vf, SrsSimpleBuffer* vb); | 1572 | + virtual int write_video(SrsTsMessage* video); |
| 1527 | /** | 1573 | /** |
| 1528 | * close the writer. | 1574 | * close the writer. |
| 1529 | */ | 1575 | */ |
| @@ -1531,38 +1577,6 @@ public: | @@ -1531,38 +1577,6 @@ public: | ||
| 1531 | }; | 1577 | }; |
| 1532 | 1578 | ||
| 1533 | /** | 1579 | /** |
| 1534 | -* jitter correct for audio, | ||
| 1535 | -* the sample rate 44100/32000 will lost precise, | ||
| 1536 | -* when mp4/ts(tbn=90000) covert to flv/rtmp(1000), | ||
| 1537 | -* so the Hls on ipad or iphone will corrupt, | ||
| 1538 | -* @see nginx-rtmp: est_pts | ||
| 1539 | -*/ | ||
| 1540 | -class SrsTsAacJitter | ||
| 1541 | -{ | ||
| 1542 | -private: | ||
| 1543 | - int64_t base_pts; | ||
| 1544 | - int64_t nb_samples; | ||
| 1545 | - int sync_ms; | ||
| 1546 | -public: | ||
| 1547 | - SrsTsAacJitter(); | ||
| 1548 | - virtual ~SrsTsAacJitter(); | ||
| 1549 | - /** | ||
| 1550 | - * when buffer start, calc the "correct" pts for ts, | ||
| 1551 | - * @param flv_pts, the flv pts calc from flv header timestamp, | ||
| 1552 | - * @param sample_rate, the sample rate in format(flv/RTMP packet header). | ||
| 1553 | - * @param aac_sample_rate, the sample rate in codec(sequence header). | ||
| 1554 | - * @return the calc correct pts. | ||
| 1555 | - */ | ||
| 1556 | - virtual int64_t on_buffer_start(int64_t flv_pts, int sample_rate, int aac_sample_rate); | ||
| 1557 | - /** | ||
| 1558 | - * when buffer continue, muxer donot write to file, | ||
| 1559 | - * the audio buffer continue grow and donot need a pts, | ||
| 1560 | - * for the ts audio PES packet only has one pts at the first time. | ||
| 1561 | - */ | ||
| 1562 | - virtual void on_buffer_continue(); | ||
| 1563 | -}; | ||
| 1564 | - | ||
| 1565 | -/** | ||
| 1566 | * ts stream cache, | 1580 | * ts stream cache, |
| 1567 | * use to cache ts stream. | 1581 | * use to cache ts stream. |
| 1568 | * | 1582 | * |
| @@ -1575,18 +1589,9 @@ public: | @@ -1575,18 +1589,9 @@ public: | ||
| 1575 | class SrsTsCache | 1589 | class SrsTsCache |
| 1576 | { | 1590 | { |
| 1577 | public: | 1591 | public: |
| 1578 | - // current frame and buffer | ||
| 1579 | - SrsMpegtsFrame* af; | ||
| 1580 | - SrsSimpleBuffer* ab; | ||
| 1581 | - SrsMpegtsFrame* vf; | ||
| 1582 | - SrsSimpleBuffer* vb; | ||
| 1583 | -public: | ||
| 1584 | - // the audio cache buffer start pts, to flush audio if full. | ||
| 1585 | - // @remark the pts is not the adjust one, it's the orignal pts. | ||
| 1586 | - int64_t audio_buffer_start_pts; | ||
| 1587 | -protected: | ||
| 1588 | - // time jitter for aac | ||
| 1589 | - SrsTsAacJitter* aac_jitter; | 1592 | + // current ts message. |
| 1593 | + SrsTsMessage* audio; | ||
| 1594 | + SrsTsMessage* video; | ||
| 1590 | public: | 1595 | public: |
| 1591 | SrsTsCache(); | 1596 | SrsTsCache(); |
| 1592 | virtual ~SrsTsCache(); | 1597 | virtual ~SrsTsCache(); |
| @@ -1594,14 +1599,15 @@ public: | @@ -1594,14 +1599,15 @@ public: | ||
| 1594 | /** | 1599 | /** |
| 1595 | * write audio to cache | 1600 | * write audio to cache |
| 1596 | */ | 1601 | */ |
| 1597 | - virtual int cache_audio(SrsAvcAacCodec* codec, int64_t pts, SrsCodecSample* sample); | 1602 | + virtual int cache_audio(SrsAvcAacCodec* codec, int64_t dts, SrsCodecSample* sample); |
| 1598 | /** | 1603 | /** |
| 1599 | * write video to muxer. | 1604 | * write video to muxer. |
| 1600 | */ | 1605 | */ |
| 1601 | virtual int cache_video(SrsAvcAacCodec* codec, int64_t dts, SrsCodecSample* sample); | 1606 | virtual int cache_video(SrsAvcAacCodec* codec, int64_t dts, SrsCodecSample* sample); |
| 1602 | private: | 1607 | private: |
| 1603 | - virtual int do_cache_audio(SrsAvcAacCodec* codec, SrsCodecSample* sample); | ||
| 1604 | - virtual int do_cache_video(SrsAvcAacCodec* codec, SrsCodecSample* sample); | 1608 | + virtual int do_cache_mp3(SrsAvcAacCodec* codec, SrsCodecSample* sample); |
| 1609 | + virtual int do_cache_aac(SrsAvcAacCodec* codec, SrsCodecSample* sample); | ||
| 1610 | + virtual int do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample); | ||
| 1605 | }; | 1611 | }; |
| 1606 | 1612 | ||
| 1607 | /** | 1613 | /** |
-
请 注册 或 登录 后发表评论