winlin

for #250, merge avc to codec. use queue to dequeue.

@@ -366,7 +366,7 @@ ModuleLibIncs=(${SRS_OBJS_DIR}) @@ -366,7 +366,7 @@ ModuleLibIncs=(${SRS_OBJS_DIR})
366 MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream" 366 MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_stream"
367 "srs_kernel_utility" "srs_kernel_flv" "srs_kernel_codec" "srs_kernel_file" 367 "srs_kernel_utility" "srs_kernel_flv" "srs_kernel_codec" "srs_kernel_file"
368 "srs_kernel_consts" "srs_kernel_aac" "srs_kernel_mp3" "srs_kernel_ts" 368 "srs_kernel_consts" "srs_kernel_aac" "srs_kernel_mp3" "srs_kernel_ts"
369 - "srs_kernel_avc" "srs_kernel_buffer") 369 + "srs_kernel_buffer")
370 KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh 370 KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh
371 KERNEL_OBJS="${MODULE_OBJS[@]}" 371 KERNEL_OBJS="${MODULE_OBJS[@]}"
372 # 372 #
@@ -20,8 +20,6 @@ file @@ -20,8 +20,6 @@ file
20 kernel readonly separator, 20 kernel readonly separator,
21 ../../src/kernel/srs_kernel_aac.hpp, 21 ../../src/kernel/srs_kernel_aac.hpp,
22 ../../src/kernel/srs_kernel_aac.cpp, 22 ../../src/kernel/srs_kernel_aac.cpp,
23 - ../../src/kernel/srs_kernel_avc.hpp,  
24 - ../../src/kernel/srs_kernel_avc.cpp,  
25 ../../src/kernel/srs_kernel_buffer.hpp, 23 ../../src/kernel/srs_kernel_buffer.hpp,
26 ../../src/kernel/srs_kernel_buffer.cpp, 24 ../../src/kernel/srs_kernel_buffer.cpp,
27 ../../src/kernel/srs_kernel_codec.hpp, 25 ../../src/kernel/srs_kernel_codec.hpp,
@@ -47,7 +47,7 @@ using namespace std; @@ -47,7 +47,7 @@ using namespace std;
47 #include <srs_rtmp_sdk.hpp> 47 #include <srs_rtmp_sdk.hpp>
48 #include <srs_app_pithy_print.hpp> 48 #include <srs_app_pithy_print.hpp>
49 #include <srs_kernel_utility.hpp> 49 #include <srs_kernel_utility.hpp>
50 -#include <srs_kernel_avc.hpp> 50 +#include <srs_kernel_codec.hpp>
51 #include <srs_kernel_file.hpp> 51 #include <srs_kernel_file.hpp>
52 #include <srs_rtmp_buffer.hpp> 52 #include <srs_rtmp_buffer.hpp>
53 #include <srs_kernel_ts.hpp> 53 #include <srs_kernel_ts.hpp>
@@ -57,6 +57,68 @@ ISrsUdpHandler::~ISrsUdpHandler() @@ -57,6 +57,68 @@ ISrsUdpHandler::~ISrsUdpHandler()
57 { 57 {
58 } 58 }
59 59
  60 +SrsMpegtsQueue::SrsMpegtsQueue()
  61 +{
  62 + nb_audios = nb_videos = 0;
  63 +}
  64 +
  65 +SrsMpegtsQueue::~SrsMpegtsQueue()
  66 +{
  67 + std::map<int64_t, SrsSharedPtrMessage*>::iterator it;
  68 + for (it = msgs.begin(); it != msgs.end(); ++it) {
  69 + SrsSharedPtrMessage* msg = it->second;
  70 + srs_freep(msg);
  71 + }
  72 + msgs.clear();
  73 +}
  74 +
  75 +int SrsMpegtsQueue::push(SrsSharedPtrMessage* msg)
  76 +{
  77 + int ret = ERROR_SUCCESS;
  78 +
  79 + if (msgs.find(msg->timestamp) != msgs.end()) {
  80 + srs_warn("mpegts: free the msg for dts exists, dts=%"PRId64, msg->timestamp);
  81 + srs_freep(msg);
  82 + return ret;
  83 + }
  84 +
  85 + if (msg->is_audio()) {
  86 + nb_audios++;
  87 + }
  88 +
  89 + if (msg->is_video()) {
  90 + nb_videos++;
  91 + }
  92 +
  93 + msgs[msg->timestamp] = msg;
  94 +
  95 + return ret;
  96 +}
  97 +
  98 +SrsSharedPtrMessage* SrsMpegtsQueue::dequeue()
  99 +{
  100 + // got 2+ videos and audios, ok to dequeue.
  101 + bool av_ok = nb_videos >= 2 && nb_audios >= 2;
  102 + // 100 videos about 30s, while 300 audios about 30s
  103 + bool av_overflow = nb_videos > 100 || nb_audios > 300;
  104 +
  105 + if (av_ok || av_overflow) {
  106 + std::map<int64_t, SrsSharedPtrMessage*>::iterator it = msgs.begin();
  107 + SrsSharedPtrMessage* msg = it->second;
  108 + msgs.erase(it);
  109 +
  110 + if (msg->is_audio()) {
  111 + nb_audios--;
  112 + }
  113 +
  114 + if (msg->is_video()) {
  115 + nb_videos--;
  116 + }
  117 + }
  118 +
  119 + return NULL;
  120 +}
  121 +
60 SrsMpegtsOverUdp::SrsMpegtsOverUdp(SrsConfDirective* c) 122 SrsMpegtsOverUdp::SrsMpegtsOverUdp(SrsConfDirective* c)
61 { 123 {
62 stream = new SrsStream(); 124 stream = new SrsStream();
@@ -72,6 +134,7 @@ SrsMpegtsOverUdp::SrsMpegtsOverUdp(SrsConfDirective* c) @@ -72,6 +134,7 @@ SrsMpegtsOverUdp::SrsMpegtsOverUdp(SrsConfDirective* c)
72 h264_sps_changed = false; 134 h264_sps_changed = false;
73 h264_pps_changed = false; 135 h264_pps_changed = false;
74 h264_sps_pps_sent = false; 136 h264_sps_pps_sent = false;
  137 + queue = new SrsMpegtsQueue();
75 } 138 }
76 139
77 SrsMpegtsOverUdp::~SrsMpegtsOverUdp() 140 SrsMpegtsOverUdp::~SrsMpegtsOverUdp()
@@ -82,6 +145,7 @@ SrsMpegtsOverUdp::~SrsMpegtsOverUdp() @@ -82,6 +145,7 @@ SrsMpegtsOverUdp::~SrsMpegtsOverUdp()
82 srs_freep(stream); 145 srs_freep(stream);
83 srs_freep(context); 146 srs_freep(context);
84 srs_freep(avc); 147 srs_freep(avc);
  148 + srs_freep(queue);
85 } 149 }
86 150
87 int SrsMpegtsOverUdp::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf) 151 int SrsMpegtsOverUdp::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
@@ -280,11 +344,14 @@ int SrsMpegtsOverUdp::on_ts_video(SrsTsMessage* msg, SrsStream* avs) @@ -280,11 +344,14 @@ int SrsMpegtsOverUdp::on_ts_video(SrsTsMessage* msg, SrsStream* avs)
280 344
281 // it may be return error, but we must process all packets. 345 // it may be return error, but we must process all packets.
282 if ((ret = write_h264_raw_frame(frame, frame_size, dts, pts)) != ERROR_SUCCESS) { 346 if ((ret = write_h264_raw_frame(frame, frame_size, dts, pts)) != ERROR_SUCCESS) {
283 - if (ret = ERROR_H264_DROP_BEFORE_SPS_PPS) { 347 + if (ret == ERROR_H264_DROP_BEFORE_SPS_PPS) {
284 continue; 348 continue;
285 } 349 }
286 return ret; 350 return ret;
287 } 351 }
  352 +
  353 + // for video, drop others with same pts/dts.
  354 + break;
288 } 355 }
289 356
290 return ret; 357 return ret;
@@ -399,15 +466,28 @@ int SrsMpegtsOverUdp::rtmp_write_packet(char type, u_int32_t timestamp, char* da @@ -399,15 +466,28 @@ int SrsMpegtsOverUdp::rtmp_write_packet(char type, u_int32_t timestamp, char* da
399 SrsSharedPtrMessage* msg = NULL; 466 SrsSharedPtrMessage* msg = NULL;
400 467
401 if ((ret = srs_rtmp_create_msg(type, timestamp, data, size, stream_id, &msg)) != ERROR_SUCCESS) { 468 if ((ret = srs_rtmp_create_msg(type, timestamp, data, size, stream_id, &msg)) != ERROR_SUCCESS) {
  469 + srs_error("mpegts: create shared ptr msg failed. ret=%d", ret);
402 return ret; 470 return ret;
403 } 471 }
404 -  
405 srs_assert(msg); 472 srs_assert(msg);
406 473
  474 + // push msg to queue.
  475 + if ((ret = queue->push(msg)) != ERROR_SUCCESS) {
  476 + srs_error("mpegts: push msg to queue failed. ret=%d", ret);
  477 + return ret;
  478 + }
  479 +
  480 + // for all ready msg, dequeue and send out.
  481 + for (;;) {
  482 + if ((msg = queue->dequeue()) == NULL) {
  483 + break;
  484 + }
  485 +
407 // send out encoded msg. 486 // send out encoded msg.
408 if ((ret = client->send_and_free_message(msg, stream_id)) != ERROR_SUCCESS) { 487 if ((ret = client->send_and_free_message(msg, stream_id)) != ERROR_SUCCESS) {
409 return ret; 488 return ret;
410 } 489 }
  490 + }
411 491
412 return ret; 492 return ret;
413 } 493 }
@@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 34
35 class sockaddr_in; 35 class sockaddr_in;
36 #include <string> 36 #include <string>
  37 +#include <map>
37 38
38 class SrsStream; 39 class SrsStream;
39 class SrsTsContext; 40 class SrsTsContext;
@@ -43,6 +44,7 @@ class SrsRtmpClient; @@ -43,6 +44,7 @@ class SrsRtmpClient;
43 class SrsStSocket; 44 class SrsStSocket;
44 class SrsRequest; 45 class SrsRequest;
45 class SrsRawH264Stream; 46 class SrsRawH264Stream;
  47 +class SrsSharedPtrMessage;
46 48
47 #include <srs_app_st.hpp> 49 #include <srs_app_st.hpp>
48 #include <srs_kernel_ts.hpp> 50 #include <srs_kernel_ts.hpp>
@@ -69,6 +71,26 @@ public: @@ -69,6 +71,26 @@ public:
69 }; 71 };
70 72
71 /** 73 /**
  74 +* the queue for mpegts over udp to send packets.
  75 +* for the aac in mpegts contains many flv packets in a pes packet,
  76 +* we must recalc the timestamp.
  77 +*/
  78 +class SrsMpegtsQueue
  79 +{
  80 +private:
  81 + // key: dts, value: msg.
  82 + std::map<int64_t, SrsSharedPtrMessage*> msgs;
  83 + int nb_audios;
  84 + int nb_videos;
  85 +public:
  86 + SrsMpegtsQueue();
  87 + virtual ~SrsMpegtsQueue();
  88 +public:
  89 + virtual int push(SrsSharedPtrMessage* msg);
  90 + virtual SrsSharedPtrMessage* dequeue();
  91 +};
  92 +
  93 +/**
72 * the mpegts over udp stream caster. 94 * the mpegts over udp stream caster.
73 */ 95 */
74 class SrsMpegtsOverUdp : virtual public ISrsTsHandler 96 class SrsMpegtsOverUdp : virtual public ISrsTsHandler
@@ -92,6 +114,7 @@ private: @@ -92,6 +114,7 @@ private:
92 std::string h264_pps; 114 std::string h264_pps;
93 bool h264_pps_changed; 115 bool h264_pps_changed;
94 bool h264_sps_pps_sent; 116 bool h264_sps_pps_sent;
  117 + SrsMpegtsQueue* queue;
95 public: 118 public:
96 SrsMpegtsOverUdp(SrsConfDirective* c); 119 SrsMpegtsOverUdp(SrsConfDirective* c);
97 virtual ~SrsMpegtsOverUdp(); 120 virtual ~SrsMpegtsOverUdp();
@@ -40,7 +40,7 @@ using namespace std; @@ -40,7 +40,7 @@ using namespace std;
40 #include <srs_kernel_stream.hpp> 40 #include <srs_kernel_stream.hpp>
41 #include <srs_app_edge.hpp> 41 #include <srs_app_edge.hpp>
42 #include <srs_kernel_utility.hpp> 42 #include <srs_kernel_utility.hpp>
43 -#include <srs_kernel_avc.hpp> 43 +#include <srs_kernel_codec.hpp>
44 #include <srs_rtmp_msg_array.hpp> 44 #include <srs_rtmp_msg_array.hpp>
45 45
46 #define CONST_MAX_JITTER_MS 500 46 #define CONST_MAX_JITTER_MS 500
1 -/*  
2 -The MIT License (MIT)  
3 -  
4 -Copyright (c) 2013-2015 winlin  
5 -  
6 -Permission is hereby granted, free of charge, to any person obtaining a copy of  
7 -this software and associated documentation files (the "Software"), to deal in  
8 -the Software without restriction, including without limitation the rights to  
9 -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of  
10 -the Software, and to permit persons to whom the Software is furnished to do so,  
11 -subject to the following conditions:  
12 -  
13 -The above copyright notice and this permission notice shall be included in all  
14 -copies or substantial portions of the Software.  
15 -  
16 -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
17 -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS  
18 -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR  
19 -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER  
20 -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN  
21 -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
22 -*/  
23 -  
24 -#include <srs_kernel_avc.hpp>  
25 -  
26 -#include <srs_kernel_error.hpp>  
27 -#include <srs_kernel_log.hpp>  
28 -#include <srs_kernel_stream.hpp>  
29 -#include <srs_kernel_utility.hpp>  
30 -#include <srs_kernel_buffer.hpp>  
31 -#include <srs_kernel_file.hpp>  
32 -  
33 -using namespace std;  
34 -  
35 -SrsCodecSampleUnit::SrsCodecSampleUnit()  
36 -{  
37 - size = 0;  
38 - bytes = NULL;  
39 -}  
40 -  
41 -SrsCodecSampleUnit::~SrsCodecSampleUnit()  
42 -{  
43 -}  
44 -  
45 -SrsCodecSample::SrsCodecSample()  
46 -{  
47 - clear();  
48 -}  
49 -  
50 -SrsCodecSample::~SrsCodecSample()  
51 -{  
52 -}  
53 -  
54 -void SrsCodecSample::clear()  
55 -{  
56 - is_video = false;  
57 - nb_sample_units = 0;  
58 -  
59 - cts = 0;  
60 - frame_type = SrsCodecVideoAVCFrameReserved;  
61 - avc_packet_type = SrsCodecVideoAVCTypeReserved;  
62 -  
63 - acodec = SrsCodecAudioReserved1;  
64 - sound_rate = SrsCodecAudioSampleRateReserved;  
65 - sound_size = SrsCodecAudioSampleSizeReserved;  
66 - sound_type = SrsCodecAudioSoundTypeReserved;  
67 - aac_packet_type = SrsCodecAudioTypeReserved;  
68 -}  
69 -  
70 -int SrsCodecSample::add_sample_unit(char* bytes, int size)  
71 -{  
72 - int ret = ERROR_SUCCESS;  
73 -  
74 - if (nb_sample_units >= __SRS_SRS_MAX_CODEC_SAMPLE) {  
75 - ret = ERROR_HLS_DECODE_ERROR;  
76 - srs_error("hls decode samples error, "  
77 - "exceed the max count: %d, ret=%d", __SRS_SRS_MAX_CODEC_SAMPLE, ret);  
78 - return ret;  
79 - }  
80 -  
81 - SrsCodecSampleUnit* sample_unit = &sample_units[nb_sample_units++];  
82 - sample_unit->bytes = bytes;  
83 - sample_unit->size = size;  
84 -  
85 - return ret;  
86 -}  
87 -  
88 -SrsAvcAacCodec::SrsAvcAacCodec()  
89 -{  
90 - width = 0;  
91 - height = 0;  
92 - duration = 0;  
93 - NAL_unit_length = 0;  
94 - frame_rate = 0;  
95 -  
96 - video_data_rate = 0;  
97 - video_codec_id = 0;  
98 -  
99 - audio_data_rate = 0;  
100 - audio_codec_id = 0;  
101 -  
102 - avc_profile = 0;  
103 - avc_level = 0;  
104 - aac_profile = 0;  
105 - aac_sample_rate = __SRS_AAC_SAMPLE_RATE_UNSET; // sample rate ignored  
106 - aac_channels = 0;  
107 - avc_extra_size = 0;  
108 - avc_extra_data = NULL;  
109 - aac_extra_size = 0;  
110 - aac_extra_data = NULL;  
111 -  
112 - sequenceParameterSetLength = 0;  
113 - sequenceParameterSetNALUnit = NULL;  
114 - pictureParameterSetLength = 0;  
115 - pictureParameterSetNALUnit = NULL;  
116 -  
117 - stream = new SrsStream();  
118 -}  
119 -  
120 -SrsAvcAacCodec::~SrsAvcAacCodec()  
121 -{  
122 - srs_freep(avc_extra_data);  
123 - srs_freep(aac_extra_data);  
124 -  
125 - srs_freep(stream);  
126 - srs_freep(sequenceParameterSetNALUnit);  
127 - srs_freep(pictureParameterSetNALUnit);  
128 -}  
129 -  
130 -int SrsAvcAacCodec::audio_aac_demux(char* data, int size, SrsCodecSample* sample)  
131 -{  
132 - int ret = ERROR_SUCCESS;  
133 -  
134 - sample->is_video = false;  
135 -  
136 - if (!data || size <= 0) {  
137 - srs_trace("no audio present, ignore it.");  
138 - return ret;  
139 - }  
140 -  
141 - if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) {  
142 - return ret;  
143 - }  
144 -  
145 - // audio decode  
146 - if (!stream->require(1)) {  
147 - ret = ERROR_HLS_DECODE_ERROR;  
148 - srs_error("audio codec decode sound_format failed. ret=%d", ret);  
149 - return ret;  
150 - }  
151 -  
152 - // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76  
153 - int8_t sound_format = stream->read_1bytes();  
154 -  
155 - int8_t sound_type = sound_format & 0x01;  
156 - int8_t sound_size = (sound_format >> 1) & 0x01;  
157 - int8_t sound_rate = (sound_format >> 2) & 0x03;  
158 - sound_format = (sound_format >> 4) & 0x0f;  
159 -  
160 - audio_codec_id = sound_format;  
161 - sample->acodec = (SrsCodecAudio)audio_codec_id;  
162 -  
163 - sample->sound_type = (SrsCodecAudioSoundType)sound_type;  
164 - sample->sound_rate = (SrsCodecAudioSampleRate)sound_rate;  
165 - sample->sound_size = (SrsCodecAudioSampleSize)sound_size;  
166 -  
167 - // we support h.264+mp3 for hls.  
168 - if (audio_codec_id == SrsCodecAudioMP3) {  
169 - return ERROR_HLS_TRY_MP3;  
170 - }  
171 -  
172 - // only support aac  
173 - if (audio_codec_id != SrsCodecAudioAAC) {  
174 - ret = ERROR_HLS_DECODE_ERROR;  
175 - srs_error("audio codec only support mp3/aac codec. actual=%d, ret=%d", audio_codec_id, ret);  
176 - return ret;  
177 - }  
178 -  
179 - if (!stream->require(1)) {  
180 - ret = ERROR_HLS_DECODE_ERROR;  
181 - srs_error("audio codec decode aac_packet_type failed. ret=%d", ret);  
182 - return ret;  
183 - }  
184 -  
185 - int8_t aac_packet_type = stream->read_1bytes();  
186 - sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;  
187 -  
188 - if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {  
189 - // AudioSpecificConfig  
190 - // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.  
191 - aac_extra_size = stream->size() - stream->pos();  
192 - if (aac_extra_size > 0) {  
193 - srs_freep(aac_extra_data);  
194 - aac_extra_data = new char[aac_extra_size];  
195 - memcpy(aac_extra_data, stream->data() + stream->pos(), aac_extra_size);  
196 - }  
197 -  
198 - // only need to decode the first 2bytes:  
199 - // audioObjectType, aac_profile, 5bits.  
200 - // samplingFrequencyIndex, aac_sample_rate, 4bits.  
201 - // channelConfiguration, aac_channels, 4bits  
202 - if (!stream->require(2)) {  
203 - ret = ERROR_HLS_DECODE_ERROR;  
204 - srs_error("audio codec decode aac sequence header failed. ret=%d", ret);  
205 - return ret;  
206 - }  
207 - aac_profile = stream->read_1bytes();  
208 - aac_sample_rate = stream->read_1bytes();  
209 -  
210 - aac_channels = (aac_sample_rate >> 3) & 0x0f;  
211 - aac_sample_rate = ((aac_profile << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);  
212 - aac_profile = (aac_profile >> 3) & 0x1f;  
213 -  
214 - if (aac_profile == 0 || aac_profile == 0x1f) {  
215 - ret = ERROR_HLS_DECODE_ERROR;  
216 - srs_error("audio codec decode aac sequence header failed, "  
217 - "adts object=%d invalid. ret=%d", aac_profile, ret);  
218 - return ret;  
219 - }  
220 -  
221 - // TODO: FIXME: to support aac he/he-v2, see: ngx_rtmp_codec_parse_aac_header  
222 - // @see: https://github.com/winlinvip/nginx-rtmp-module/commit/3a5f9eea78fc8d11e8be922aea9ac349b9dcbfc2  
223 - //  
224 - // donot force to LC, @see: https://github.com/winlinvip/simple-rtmp-server/issues/81  
225 - // the source will print the sequence header info.  
226 - //if (aac_profile > 3) {  
227 - // Mark all extended profiles as LC  
228 - // to make Android as happy as possible.  
229 - // @see: ngx_rtmp_hls_parse_aac_header  
230 - //aac_profile = 1;  
231 - //}  
232 - } else if (aac_packet_type == SrsCodecAudioTypeRawData) {  
233 - // ensure the sequence header demuxed  
234 - if (aac_extra_size <= 0 || !aac_extra_data) {  
235 - ret = ERROR_HLS_DECODE_ERROR;  
236 - srs_error("audio codec decode aac failed, sequence header not found. ret=%d", ret);  
237 - return ret;  
238 - }  
239 -  
240 - // Raw AAC frame data in UI8 []  
241 - // 6.3 Raw Data, aac-iso-13818-7.pdf, page 28  
242 - if ((ret = sample->add_sample_unit(stream->data() + stream->pos(), stream->size() - stream->pos())) != ERROR_SUCCESS) {  
243 - srs_error("audio codec add sample failed. ret=%d", ret);  
244 - return ret;  
245 - }  
246 - } else {  
247 - // ignored.  
248 - }  
249 -  
250 - // reset the sample rate by sequence header  
251 - if (aac_sample_rate != __SRS_AAC_SAMPLE_RATE_UNSET) {  
252 - static int aac_sample_rates[] = {  
253 - 96000, 88200, 64000, 48000,  
254 - 44100, 32000, 24000, 22050,  
255 - 16000, 12000, 11025, 8000,  
256 - 7350, 0, 0, 0  
257 - };  
258 - switch (aac_sample_rates[aac_sample_rate]) {  
259 - case 11025:  
260 - sample->sound_rate = SrsCodecAudioSampleRate11025;  
261 - break;  
262 - case 22050:  
263 - sample->sound_rate = SrsCodecAudioSampleRate22050;  
264 - break;  
265 - case 44100:  
266 - sample->sound_rate = SrsCodecAudioSampleRate44100;  
267 - break;  
268 - default:  
269 - break;  
270 - };  
271 - }  
272 -  
273 - srs_info("audio decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d",  
274 - sound_type, audio_codec_id, sound_size, sound_rate, sound_format, size);  
275 -  
276 - return ret;  
277 -}  
278 -  
279 -int SrsAvcAacCodec::audio_mp3_demux(char* data, int size, SrsCodecSample* sample)  
280 -{  
281 - int ret = ERROR_SUCCESS;  
282 -  
283 - // we always decode aac then mp3.  
284 - srs_assert(sample->acodec == SrsCodecAudioMP3);  
285 -  
286 - // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76  
287 - if (!data || size <= 1) {  
288 - srs_trace("no mp3 audio present, ignore it.");  
289 - return ret;  
290 - }  
291 -  
292 - // mp3 payload.  
293 - if ((ret = sample->add_sample_unit(data + 1, size - 1)) != ERROR_SUCCESS) {  
294 - srs_error("audio codec add mp3 sample failed. ret=%d", ret);  
295 - return ret;  
296 - }  
297 -  
298 - srs_info("audio decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d",  
299 - sample->sound_type, audio_codec_id, sample->sound_size, sample->sound_rate, sample->acodec, size);  
300 -  
301 - return ret;  
302 -}  
303 -  
304 -int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample)  
305 -{  
306 - int ret = ERROR_SUCCESS;  
307 -  
308 - sample->is_video = true;  
309 -  
310 - if (!data || size <= 0) {  
311 - srs_trace("no video present, ignore it.");  
312 - return ret;  
313 - }  
314 -  
315 - if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) {  
316 - return ret;  
317 - }  
318 -  
319 - // video decode  
320 - if (!stream->require(1)) {  
321 - ret = ERROR_HLS_DECODE_ERROR;  
322 - srs_error("video codec decode frame_type failed. ret=%d", ret);  
323 - return ret;  
324 - }  
325 -  
326 - // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78  
327 - int8_t frame_type = stream->read_1bytes();  
328 - int8_t codec_id = frame_type & 0x0f;  
329 - frame_type = (frame_type >> 4) & 0x0f;  
330 -  
331 - sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;  
332 -  
333 - // ignore info frame without error,  
334 - // @see https://github.com/winlinvip/simple-rtmp-server/issues/288#issuecomment-69863909  
335 - if (sample->frame_type == SrsCodecVideoAVCFrameVideoInfoFrame) {  
336 - srs_warn("video codec igone the info frame, ret=%d", ret);  
337 - return ret;  
338 - }  
339 -  
340 - // only support h.264/avc  
341 - if (codec_id != SrsCodecVideoAVC) {  
342 - ret = ERROR_HLS_DECODE_ERROR;  
343 - srs_error("video codec only support video h.264/avc codec. actual=%d, ret=%d", codec_id, ret);  
344 - return ret;  
345 - }  
346 - video_codec_id = codec_id;  
347 -  
348 - if (!stream->require(4)) {  
349 - ret = ERROR_HLS_DECODE_ERROR;  
350 - srs_error("video codec decode avc_packet_type failed. ret=%d", ret);  
351 - return ret;  
352 - }  
353 - int8_t avc_packet_type = stream->read_1bytes();  
354 - int32_t composition_time = stream->read_3bytes();  
355 -  
356 - // pts = dts + cts.  
357 - sample->cts = composition_time;  
358 - sample->avc_packet_type = (SrsCodecVideoAVCType)avc_packet_type;  
359 -  
360 - if (avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {  
361 - if ((ret = avc_demux_sps_pps(stream)) != ERROR_SUCCESS) {  
362 - return ret;  
363 - }  
364 - } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){  
365 - // ensure the sequence header demuxed  
366 - if (avc_extra_size <= 0 || !avc_extra_data) {  
367 - ret = ERROR_HLS_DECODE_ERROR;  
368 - srs_error("avc decode failed, sequence header not found. ret=%d", ret);  
369 - return ret;  
370 - }  
371 -  
372 - // One or more NALUs (Full frames are required)  
373 - // try "AnnexB" from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.  
374 - if ((ret = avc_demux_annexb_format(stream, sample)) != ERROR_SUCCESS) {  
375 - // stop try when system error.  
376 - if (ret != ERROR_HLS_AVC_TRY_OTHERS) {  
377 - srs_error("avc demux for annexb failed. ret=%d", ret);  
378 - return ret;  
379 - }  
380 -  
381 - // try "ISO Base Media File Format" from H.264-AVC-ISO_IEC_14496-15.pdf, page 20  
382 - if ((ret = avc_demux_ibmf_format(stream, sample)) != ERROR_SUCCESS) {  
383 - return ret;  
384 - }  
385 - }  
386 - } else {  
387 - // ignored.  
388 - }  
389 -  
390 - srs_info("video decoded, type=%d, codec=%d, avc=%d, time=%d, size=%d",  
391 - frame_type, video_codec_id, avc_packet_type, composition_time, size);  
392 -  
393 - return ret;  
394 -}  
395 -  
396 -int SrsAvcAacCodec::avc_demux_sps_pps(SrsStream* stream)  
397 -{  
398 - int ret = ERROR_SUCCESS;  
399 -  
400 - // AVCDecoderConfigurationRecord  
401 - // 5.2.4.1.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16  
402 - avc_extra_size = stream->size() - stream->pos();  
403 - if (avc_extra_size > 0) {  
404 - srs_freep(avc_extra_data);  
405 - avc_extra_data = new char[avc_extra_size];  
406 - memcpy(avc_extra_data, stream->data() + stream->pos(), avc_extra_size);  
407 - }  
408 -  
409 - if (!stream->require(6)) {  
410 - ret = ERROR_HLS_DECODE_ERROR;  
411 - srs_error("avc decode sequenc header failed. ret=%d", ret);  
412 - return ret;  
413 - }  
414 - //int8_t configurationVersion = stream->read_1bytes();  
415 - stream->read_1bytes();  
416 - //int8_t AVCProfileIndication = stream->read_1bytes();  
417 - avc_profile = stream->read_1bytes();  
418 - //int8_t profile_compatibility = stream->read_1bytes();  
419 - stream->read_1bytes();  
420 - //int8_t AVCLevelIndication = stream->read_1bytes();  
421 - avc_level = stream->read_1bytes();  
422 -  
423 - // parse the NALU size.  
424 - int8_t lengthSizeMinusOne = stream->read_1bytes();  
425 - lengthSizeMinusOne &= 0x03;  
426 - NAL_unit_length = lengthSizeMinusOne;  
427 -  
428 - // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16  
429 - // 5.2.4.1 AVC decoder configuration record  
430 - // 5.2.4.1.2 Semantics  
431 - // The value of this field shall be one of 0, 1, or 3 corresponding to a  
432 - // length encoded with 1, 2, or 4 bytes, respectively.  
433 - if (NAL_unit_length == 2) {  
434 - ret = ERROR_HLS_DECODE_ERROR;  
435 - srs_error("sps lengthSizeMinusOne should never be 2. ret=%d", ret);  
436 - return ret;  
437 - }  
438 -  
439 - // 1 sps  
440 - if (!stream->require(1)) {  
441 - ret = ERROR_HLS_DECODE_ERROR;  
442 - srs_error("avc decode sequenc header sps failed. ret=%d", ret);  
443 - return ret;  
444 - }  
445 - int8_t numOfSequenceParameterSets = stream->read_1bytes();  
446 - numOfSequenceParameterSets &= 0x1f;  
447 - if (numOfSequenceParameterSets != 1) {  
448 - ret = ERROR_HLS_DECODE_ERROR;  
449 - srs_error("avc decode sequenc header sps failed. ret=%d", ret);  
450 - return ret;  
451 - }  
452 - if (!stream->require(2)) {  
453 - ret = ERROR_HLS_DECODE_ERROR;  
454 - srs_error("avc decode sequenc header sps size failed. ret=%d", ret);  
455 - return ret;  
456 - }  
457 - sequenceParameterSetLength = stream->read_2bytes();  
458 - if (!stream->require(sequenceParameterSetLength)) {  
459 - ret = ERROR_HLS_DECODE_ERROR;  
460 - srs_error("avc decode sequenc header sps data failed. ret=%d", ret);  
461 - return ret;  
462 - }  
463 - if (sequenceParameterSetLength > 0) {  
464 - srs_freep(sequenceParameterSetNALUnit);  
465 - sequenceParameterSetNALUnit = new char[sequenceParameterSetLength];  
466 - memcpy(sequenceParameterSetNALUnit, stream->data() + stream->pos(), sequenceParameterSetLength);  
467 - stream->skip(sequenceParameterSetLength);  
468 - }  
469 - // 1 pps  
470 - if (!stream->require(1)) {  
471 - ret = ERROR_HLS_DECODE_ERROR;  
472 - srs_error("avc decode sequenc header pps failed. ret=%d", ret);  
473 - return ret;  
474 - }  
475 - int8_t numOfPictureParameterSets = stream->read_1bytes();  
476 - numOfPictureParameterSets &= 0x1f;  
477 - if (numOfPictureParameterSets != 1) {  
478 - ret = ERROR_HLS_DECODE_ERROR;  
479 - srs_error("avc decode sequenc header pps failed. ret=%d", ret);  
480 - return ret;  
481 - }  
482 - if (!stream->require(2)) {  
483 - ret = ERROR_HLS_DECODE_ERROR;  
484 - srs_error("avc decode sequenc header pps size failed. ret=%d", ret);  
485 - return ret;  
486 - }  
487 - pictureParameterSetLength = stream->read_2bytes();  
488 - if (!stream->require(pictureParameterSetLength)) {  
489 - ret = ERROR_HLS_DECODE_ERROR;  
490 - srs_error("avc decode sequenc header pps data failed. ret=%d", ret);  
491 - return ret;  
492 - }  
493 - if (pictureParameterSetLength > 0) {  
494 - srs_freep(pictureParameterSetNALUnit);  
495 - pictureParameterSetNALUnit = new char[pictureParameterSetLength];  
496 - memcpy(pictureParameterSetNALUnit, stream->data() + stream->pos(), pictureParameterSetLength);  
497 - stream->skip(pictureParameterSetLength);  
498 - }  
499 -  
500 - return ret;  
501 -}  
502 -  
503 -int SrsAvcAacCodec::avc_demux_annexb_format(SrsStream* stream, SrsCodecSample* sample)  
504 -{  
505 - int ret = ERROR_SUCCESS;  
506 -  
507 - // not annexb, try others  
508 - if (!srs_avc_startswith_annexb(stream, NULL)) {  
509 - return ERROR_HLS_AVC_TRY_OTHERS;  
510 - }  
511 -  
512 - // AnnexB  
513 - // B.1.1 Byte stream NAL unit syntax,  
514 - // H.264-AVC-ISO_IEC_14496-10.pdf, page 211.  
515 - while (!stream->empty()) {  
516 - // find start code  
517 - int nb_start_code = 0;  
518 - if (!srs_avc_startswith_annexb(stream, &nb_start_code)) {  
519 - return ret;  
520 - }  
521 -  
522 - // skip the start code.  
523 - if (nb_start_code > 0) {  
524 - stream->skip(nb_start_code);  
525 - }  
526 -  
527 - // the NALU start bytes.  
528 - char* p = stream->data() + stream->pos();  
529 -  
530 - // get the last matched NALU  
531 - while (!stream->empty()) {  
532 - if (srs_avc_startswith_annexb(stream, NULL)) {  
533 - break;  
534 - }  
535 -  
536 - stream->skip(1);  
537 - }  
538 -  
539 - char* pp = stream->data() + stream->pos();  
540 -  
541 - // skip the empty.  
542 - if (pp - p <= 0) {  
543 - continue;  
544 - }  
545 -  
546 - // got the NALU.  
547 - if ((ret = sample->add_sample_unit(p, pp - p)) != ERROR_SUCCESS) {  
548 - srs_error("annexb add video sample failed. ret=%d", ret);  
549 - return ret;  
550 - }  
551 - }  
552 -  
553 - return ret;  
554 -}  
555 -  
556 -int SrsAvcAacCodec::avc_demux_ibmf_format(SrsStream* stream, SrsCodecSample* sample)  
557 -{  
558 - int ret = ERROR_SUCCESS;  
559 -  
560 - int PictureLength = stream->size() - stream->pos();  
561 -  
562 - // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16  
563 - // 5.2.4.1 AVC decoder configuration record  
564 - // 5.2.4.1.2 Semantics  
565 - // The value of this field shall be one of 0, 1, or 3 corresponding to a  
566 - // length encoded with 1, 2, or 4 bytes, respectively.  
567 - srs_assert(NAL_unit_length != 2);  
568 -  
569 - // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20  
570 - for (int i = 0; i < PictureLength;) {  
571 - // unsigned int((NAL_unit_length+1)*8) NALUnitLength;  
572 - if (!stream->require(NAL_unit_length + 1)) {  
573 - ret = ERROR_HLS_DECODE_ERROR;  
574 - srs_error("avc decode NALU size failed. ret=%d", ret);  
575 - return ret;  
576 - }  
577 - int32_t NALUnitLength = 0;  
578 - if (NAL_unit_length == 3) {  
579 - NALUnitLength = stream->read_4bytes();  
580 - } else if (NAL_unit_length == 1) {  
581 - NALUnitLength = stream->read_2bytes();  
582 - } else {  
583 - NALUnitLength = stream->read_1bytes();  
584 - }  
585 -  
586 - // maybe stream is invalid format.  
587 - // see: https://github.com/winlinvip/simple-rtmp-server/issues/183  
588 - if (NALUnitLength < 0) {  
589 - ret = ERROR_HLS_DECODE_ERROR;  
590 - srs_error("maybe stream is AnnexB format. ret=%d", ret);  
591 - return ret;  
592 - }  
593 -  
594 - // NALUnit  
595 - if (!stream->require(NALUnitLength)) {  
596 - ret = ERROR_HLS_DECODE_ERROR;  
597 - srs_error("avc decode NALU data failed. ret=%d", ret);  
598 - return ret;  
599 - }  
600 - // 7.3.1 NAL unit syntax, H.264-AVC-ISO_IEC_14496-10.pdf, page 44.  
601 - if ((ret = sample->add_sample_unit(stream->data() + stream->pos(), NALUnitLength)) != ERROR_SUCCESS) {  
602 - srs_error("avc add video sample failed. ret=%d", ret);  
603 - return ret;  
604 - }  
605 - stream->skip(NALUnitLength);  
606 -  
607 - i += NAL_unit_length + 1 + NALUnitLength;  
608 - }  
609 -  
610 - return ret;  
611 -}  
612 -  
1 -/*  
2 -The MIT License (MIT)  
3 -  
4 -Copyright (c) 2013-2015 winlin  
5 -  
6 -Permission is hereby granted, free of charge, to any person obtaining a copy of  
7 -this software and associated documentation files (the "Software"), to deal in  
8 -the Software without restriction, including without limitation the rights to  
9 -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of  
10 -the Software, and to permit persons to whom the Software is furnished to do so,  
11 -subject to the following conditions:  
12 -  
13 -The above copyright notice and this permission notice shall be included in all  
14 -copies or substantial portions of the Software.  
15 -  
16 -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
17 -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS  
18 -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR  
19 -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER  
20 -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN  
21 -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
22 -*/  
23 -  
24 -#ifndef SRS_KERNEL_AVC_HPP  
25 -#define SRS_KERNEL_AVC_HPP  
26 -  
27 -/*  
28 -#include <srs_kernel_avc.hpp>  
29 -*/  
30 -  
31 -#include <srs_core.hpp>  
32 -  
33 -#include <string>  
34 -  
35 -#include <srs_kernel_codec.hpp>  
36 -  
37 -class SrsStream;  
38 -class SrsMpegtsFrame;  
39 -class SrsSimpleBuffer;  
40 -class SrsAvcAacCodec;  
41 -class SrsCodecSample;  
42 -class SrsFileWriter;  
43 -  
44 -/**  
45 -* the public data, event HLS disable, others can use it.  
46 -*/  
47 -/**  
48 -* the flv sample rate map  
49 -*/  
50 -extern int flv_sample_rates[];  
51 -  
52 -/**  
53 -* the aac sample rate map  
54 -*/  
55 -extern int aac_sample_rates[];  
56 -  
57 -#define __SRS_SRS_MAX_CODEC_SAMPLE 128  
58 -#define __SRS_AAC_SAMPLE_RATE_UNSET 15  
59 -  
60 -// in ms, for HLS aac flush the audio  
61 -#define SRS_CONF_DEFAULT_AAC_DELAY 100  
62 -  
63 -// max PES packets size to flush the video.  
64 -#define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 1024 * 1024  
65 -  
66 -/**  
67 -* the FLV/RTMP supported audio sample size.  
68 -* Size of each audio sample. This parameter only pertains to  
69 -* uncompressed formats. Compressed formats always decode  
70 -* to 16 bits internally.  
71 -* 0 = 8-bit samples  
72 -* 1 = 16-bit samples  
73 -*/  
74 -enum SrsCodecAudioSampleSize  
75 -{  
76 - // set to the max value to reserved, for array map.  
77 - SrsCodecAudioSampleSizeReserved = 2,  
78 -  
79 - SrsCodecAudioSampleSize8bit = 0,  
80 - SrsCodecAudioSampleSize16bit = 1,  
81 -};  
82 -  
83 -/**  
84 -* the FLV/RTMP supported audio sound type/channel.  
85 -* Mono or stereo sound  
86 -* 0 = Mono sound  
87 -* 1 = Stereo sound  
88 -*/  
89 -enum SrsCodecAudioSoundType  
90 -{  
91 - // set to the max value to reserved, for array map.  
92 - SrsCodecAudioSoundTypeReserved = 2,  
93 -  
94 - SrsCodecAudioSoundTypeMono = 0,  
95 - SrsCodecAudioSoundTypeStereo = 1,  
96 -};  
97 -  
98 -/**  
99 -* the codec sample unit.  
100 -* for h.264 video packet, a NALU is a sample unit.  
101 -* for aac raw audio packet, a NALU is the entire aac raw data.  
102 -* for sequence header, it's not a sample unit.  
103 -*/  
104 -class SrsCodecSampleUnit  
105 -{  
106 -public:  
107 - /**  
108 - * the sample bytes is directly ptr to packet bytes,  
109 - * user should never use it when packet destroyed.  
110 - */  
111 - int size;  
112 - char* bytes;  
113 -public:  
114 - SrsCodecSampleUnit();  
115 - virtual ~SrsCodecSampleUnit();  
116 -};  
117 -  
118 -/**  
119 -* the samples in the flv audio/video packet.  
120 -* the sample used to analysis a video/audio packet,  
121 -* split the h.264 NALUs to buffers, or aac raw data to a buffer,  
122 -* and decode the video/audio specified infos.  
123 -*  
124 -* the sample unit:  
125 -* a video packet codec in h.264 contains many NALUs, each is a sample unit.  
126 -* a audio packet codec in aac is a sample unit.  
127 -* @remark, the video/audio sequence header is not sample unit,  
128 -* all sequence header stores as extra data,  
129 -* @see SrsAvcAacCodec.avc_extra_data and SrsAvcAacCodec.aac_extra_data  
130 -* @remark, user must clear all samples before decode a new video/audio packet.  
131 -*/  
132 -class SrsCodecSample  
133 -{  
134 -public:  
135 - /**  
136 - * each audio/video raw data packet will dumps to one or multiple buffers,  
137 - * the buffers will write to hls and clear to reset.  
138 - * generally, aac audio packet corresponding to one buffer,  
139 - * where avc/h264 video packet may contains multiple buffer.  
140 - */  
141 - int nb_sample_units;  
142 - SrsCodecSampleUnit sample_units[__SRS_SRS_MAX_CODEC_SAMPLE];  
143 -public:  
144 - /**  
145 - * whether the sample is video sample which demux from video packet.  
146 - */  
147 - bool is_video;  
148 - /**  
149 - * CompositionTime, video_file_format_spec_v10_1.pdf, page 78.  
150 - * cts = pts - dts, where dts = flvheader->timestamp.  
151 - */  
152 - int32_t cts;  
153 -public:  
154 - // video specified  
155 - SrsCodecVideoAVCFrame frame_type;  
156 - SrsCodecVideoAVCType avc_packet_type;  
157 -public:  
158 - // audio specified  
159 - SrsCodecAudio acodec;  
160 - // audio aac specified.  
161 - SrsCodecAudioSampleRate sound_rate;  
162 - SrsCodecAudioSampleSize sound_size;  
163 - SrsCodecAudioSoundType sound_type;  
164 - SrsCodecAudioType aac_packet_type;  
165 -public:  
166 - SrsCodecSample();  
167 - virtual ~SrsCodecSample();  
168 -public:  
169 - /**  
170 - * clear all samples.  
171 - * the sample units never copy the bytes, it directly use the ptr,  
172 - * so when video/audio packet is destroyed, the sample must be clear.  
173 - * in a word, user must clear sample before demux it.  
174 - * @remark demux sample use SrsAvcAacCodec.audio_aac_demux or video_avc_demux.  
175 - */  
176 - void clear();  
177 - /**  
178 - * add the a sample unit, it's a h.264 NALU or aac raw data.  
179 - * the sample unit directly use the ptr of packet bytes,  
180 - * so user must never use sample unit when packet is destroyed.  
181 - * in a word, user must clear sample before demux it.  
182 - */  
183 - int add_sample_unit(char* bytes, int size);  
184 -};  
185 -  
186 -/**  
187 -* the h264/avc and aac codec, for media stream.  
188 -*  
189 -* to demux the FLV/RTMP video/audio packet to sample,  
190 -* add each NALUs of h.264 as a sample unit to sample,  
191 -* while the entire aac raw data as a sample unit.  
192 -*  
193 -* for sequence header,  
194 -* demux it and save it in the avc_extra_data and aac_extra_data,  
195 -*  
196 -* for the codec info, such as audio sample rate,  
197 -* decode from FLV/RTMP header, then use codec info in sequence  
198 -* header to override it.  
199 -*/  
200 -class SrsAvcAacCodec  
201 -{  
202 -private:  
203 - SrsStream* stream;  
204 -public:  
205 - /**  
206 - * metadata specified  
207 - */  
208 - int duration;  
209 - int width;  
210 - int height;  
211 - int frame_rate;  
212 - // @see: SrsCodecVideo  
213 - int video_codec_id;  
214 - int video_data_rate; // in bps  
215 - // @see: SrsCod ecAudioType  
216 - int audio_codec_id;  
217 - int audio_data_rate; // in bps  
218 -public:  
219 - /**  
220 - * video specified  
221 - */  
222 - // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.  
223 - u_int8_t avc_profile;  
224 - // level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.  
225 - u_int8_t avc_level;  
226 - // lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16  
227 - int8_t NAL_unit_length;  
228 - u_int16_t sequenceParameterSetLength;  
229 - char* sequenceParameterSetNALUnit;  
230 - u_int16_t pictureParameterSetLength;  
231 - char* pictureParameterSetNALUnit;  
232 -public:  
233 - /**  
234 - * audio specified  
235 - * audioObjectType, in 1.6.2.1 AudioSpecificConfig, page 33,  
236 - * 1.5.1.1 Audio object type definition, page 23,  
237 - * in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf.  
238 - */  
239 - u_int8_t aac_profile;  
240 - /**  
241 - * samplingFrequencyIndex  
242 - */  
243 - u_int8_t aac_sample_rate;  
244 - /**  
245 - * channelConfiguration  
246 - */  
247 - u_int8_t aac_channels;  
248 -public:  
249 - /**  
250 - * the avc extra data, the AVC sequence header,  
251 - * without the flv codec header,  
252 - * @see: ffmpeg, AVCodecContext::extradata  
253 - */  
254 - int avc_extra_size;  
255 - char* avc_extra_data;  
256 - /**  
257 - * the aac extra data, the AAC sequence header,  
258 - * without the flv codec header,  
259 - * @see: ffmpeg, AVCodecContext::extradata  
260 - */  
261 - int aac_extra_size;  
262 - char* aac_extra_data;  
263 -public:  
264 - SrsAvcAacCodec();  
265 - virtual ~SrsAvcAacCodec();  
266 -// the following function used for hls to build the sample and codec.  
267 -public:  
268 - /**  
269 - * demux the audio packet in aac codec.  
270 - * the packet mux in FLV/RTMP format defined in flv specification.  
271 - * demux the audio speicified data(sound_format, sound_size, ...) to sample.  
272 - * demux the aac specified data(aac_profile, ...) to codec from sequence header.  
273 - * demux the aac raw to sample units.  
274 - */  
275 - virtual int audio_aac_demux(char* data, int size, SrsCodecSample* sample);  
276 - virtual int audio_mp3_demux(char* data, int size, SrsCodecSample* sample);  
277 - /**  
278 - * demux the video packet in h.264 codec.  
279 - * the packet mux in FLV/RTMP format defined in flv specification.  
280 - * demux the video specified data(frame_type, codec_id, ...) to sample.  
281 - * demux the h.264 sepcified data(avc_profile, ...) to codec from sequence header.  
282 - * demux the h.264 NALUs to sampe units.  
283 - */  
284 - virtual int video_avc_demux(char* data, int size, SrsCodecSample* sample);  
285 -private:  
286 - /**  
287 - * when avc packet type is SrsCodecVideoAVCTypeSequenceHeader,  
288 - * decode the sps and pps.  
289 - */  
290 - virtual int avc_demux_sps_pps(SrsStream* stream);  
291 - /**  
292 - * demux the avc NALU in "AnnexB"  
293 - * from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.  
294 - */  
295 - virtual int avc_demux_annexb_format(SrsStream* stream, SrsCodecSample* sample);  
296 - /**  
297 - * demux the avc NALU in "ISO Base Media File Format"  
298 - * from H.264-AVC-ISO_IEC_14496-15.pdf, page 20  
299 - */  
300 - virtual int avc_demux_ibmf_format(SrsStream* stream, SrsCodecSample* sample);  
301 -};  
302 -  
303 -#endif  
304 -  
@@ -25,6 +25,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,6 +25,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 25
26 #include <string.h> 26 #include <string.h>
27 #include <stdlib.h> 27 #include <stdlib.h>
  28 +using namespace std;
  29 +
  30 +#include <srs_kernel_error.hpp>
  31 +#include <srs_kernel_log.hpp>
  32 +#include <srs_kernel_stream.hpp>
  33 +#include <srs_kernel_utility.hpp>
28 34
29 SrsFlvCodec::SrsFlvCodec() 35 SrsFlvCodec::SrsFlvCodec()
30 { 36 {
@@ -111,3 +117,581 @@ bool SrsFlvCodec::audio_is_aac(char* data, int size) @@ -111,3 +117,581 @@ bool SrsFlvCodec::audio_is_aac(char* data, int size)
111 return sound_format == SrsCodecAudioAAC; 117 return sound_format == SrsCodecAudioAAC;
112 } 118 }
113 119
  120 +SrsCodecSampleUnit::SrsCodecSampleUnit()
  121 +{
  122 + size = 0;
  123 + bytes = NULL;
  124 +}
  125 +
  126 +SrsCodecSampleUnit::~SrsCodecSampleUnit()
  127 +{
  128 +}
  129 +
  130 +SrsCodecSample::SrsCodecSample()
  131 +{
  132 + clear();
  133 +}
  134 +
  135 +SrsCodecSample::~SrsCodecSample()
  136 +{
  137 +}
  138 +
  139 +void SrsCodecSample::clear()
  140 +{
  141 + is_video = false;
  142 + nb_sample_units = 0;
  143 +
  144 + cts = 0;
  145 + frame_type = SrsCodecVideoAVCFrameReserved;
  146 + avc_packet_type = SrsCodecVideoAVCTypeReserved;
  147 +
  148 + acodec = SrsCodecAudioReserved1;
  149 + sound_rate = SrsCodecAudioSampleRateReserved;
  150 + sound_size = SrsCodecAudioSampleSizeReserved;
  151 + sound_type = SrsCodecAudioSoundTypeReserved;
  152 + aac_packet_type = SrsCodecAudioTypeReserved;
  153 +}
  154 +
  155 +int SrsCodecSample::add_sample_unit(char* bytes, int size)
  156 +{
  157 + int ret = ERROR_SUCCESS;
  158 +
  159 + if (nb_sample_units >= __SRS_SRS_MAX_CODEC_SAMPLE) {
  160 + ret = ERROR_HLS_DECODE_ERROR;
  161 + srs_error("hls decode samples error, "
  162 + "exceed the max count: %d, ret=%d", __SRS_SRS_MAX_CODEC_SAMPLE, ret);
  163 + return ret;
  164 + }
  165 +
  166 + SrsCodecSampleUnit* sample_unit = &sample_units[nb_sample_units++];
  167 + sample_unit->bytes = bytes;
  168 + sample_unit->size = size;
  169 +
  170 + return ret;
  171 +}
  172 +
  173 +SrsAvcAacCodec::SrsAvcAacCodec()
  174 +{
  175 + width = 0;
  176 + height = 0;
  177 + duration = 0;
  178 + NAL_unit_length = 0;
  179 + frame_rate = 0;
  180 +
  181 + video_data_rate = 0;
  182 + video_codec_id = 0;
  183 +
  184 + audio_data_rate = 0;
  185 + audio_codec_id = 0;
  186 +
  187 + avc_profile = 0;
  188 + avc_level = 0;
  189 + aac_profile = 0;
  190 + aac_sample_rate = __SRS_AAC_SAMPLE_RATE_UNSET; // sample rate ignored
  191 + aac_channels = 0;
  192 + avc_extra_size = 0;
  193 + avc_extra_data = NULL;
  194 + aac_extra_size = 0;
  195 + aac_extra_data = NULL;
  196 +
  197 + sequenceParameterSetLength = 0;
  198 + sequenceParameterSetNALUnit = NULL;
  199 + pictureParameterSetLength = 0;
  200 + pictureParameterSetNALUnit = NULL;
  201 +
  202 + stream = new SrsStream();
  203 +}
  204 +
  205 +SrsAvcAacCodec::~SrsAvcAacCodec()
  206 +{
  207 + srs_freep(avc_extra_data);
  208 + srs_freep(aac_extra_data);
  209 +
  210 + srs_freep(stream);
  211 + srs_freep(sequenceParameterSetNALUnit);
  212 + srs_freep(pictureParameterSetNALUnit);
  213 +}
  214 +
  215 +int SrsAvcAacCodec::audio_aac_demux(char* data, int size, SrsCodecSample* sample)
  216 +{
  217 + int ret = ERROR_SUCCESS;
  218 +
  219 + sample->is_video = false;
  220 +
  221 + if (!data || size <= 0) {
  222 + srs_trace("no audio present, ignore it.");
  223 + return ret;
  224 + }
  225 +
  226 + if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) {
  227 + return ret;
  228 + }
  229 +
  230 + // audio decode
  231 + if (!stream->require(1)) {
  232 + ret = ERROR_HLS_DECODE_ERROR;
  233 + srs_error("audio codec decode sound_format failed. ret=%d", ret);
  234 + return ret;
  235 + }
  236 +
  237 + // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
  238 + int8_t sound_format = stream->read_1bytes();
  239 +
  240 + int8_t sound_type = sound_format & 0x01;
  241 + int8_t sound_size = (sound_format >> 1) & 0x01;
  242 + int8_t sound_rate = (sound_format >> 2) & 0x03;
  243 + sound_format = (sound_format >> 4) & 0x0f;
  244 +
  245 + audio_codec_id = sound_format;
  246 + sample->acodec = (SrsCodecAudio)audio_codec_id;
  247 +
  248 + sample->sound_type = (SrsCodecAudioSoundType)sound_type;
  249 + sample->sound_rate = (SrsCodecAudioSampleRate)sound_rate;
  250 + sample->sound_size = (SrsCodecAudioSampleSize)sound_size;
  251 +
  252 + // we support h.264+mp3 for hls.
  253 + if (audio_codec_id == SrsCodecAudioMP3) {
  254 + return ERROR_HLS_TRY_MP3;
  255 + }
  256 +
  257 + // only support aac
  258 + if (audio_codec_id != SrsCodecAudioAAC) {
  259 + ret = ERROR_HLS_DECODE_ERROR;
  260 + srs_error("audio codec only support mp3/aac codec. actual=%d, ret=%d", audio_codec_id, ret);
  261 + return ret;
  262 + }
  263 +
  264 + if (!stream->require(1)) {
  265 + ret = ERROR_HLS_DECODE_ERROR;
  266 + srs_error("audio codec decode aac_packet_type failed. ret=%d", ret);
  267 + return ret;
  268 + }
  269 +
  270 + int8_t aac_packet_type = stream->read_1bytes();
  271 + sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;
  272 +
  273 + if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
  274 + // AudioSpecificConfig
  275 + // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.
  276 + aac_extra_size = stream->size() - stream->pos();
  277 + if (aac_extra_size > 0) {
  278 + srs_freep(aac_extra_data);
  279 + aac_extra_data = new char[aac_extra_size];
  280 + memcpy(aac_extra_data, stream->data() + stream->pos(), aac_extra_size);
  281 + }
  282 +
  283 + // only need to decode the first 2bytes:
  284 + // audioObjectType, aac_profile, 5bits.
  285 + // samplingFrequencyIndex, aac_sample_rate, 4bits.
  286 + // channelConfiguration, aac_channels, 4bits
  287 + if (!stream->require(2)) {
  288 + ret = ERROR_HLS_DECODE_ERROR;
  289 + srs_error("audio codec decode aac sequence header failed. ret=%d", ret);
  290 + return ret;
  291 + }
  292 + aac_profile = stream->read_1bytes();
  293 + aac_sample_rate = stream->read_1bytes();
  294 +
  295 + aac_channels = (aac_sample_rate >> 3) & 0x0f;
  296 + aac_sample_rate = ((aac_profile << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);
  297 + aac_profile = (aac_profile >> 3) & 0x1f;
  298 +
  299 + if (aac_profile == 0 || aac_profile == 0x1f) {
  300 + ret = ERROR_HLS_DECODE_ERROR;
  301 + srs_error("audio codec decode aac sequence header failed, "
  302 + "adts object=%d invalid. ret=%d", aac_profile, ret);
  303 + return ret;
  304 + }
  305 +
  306 + // TODO: FIXME: to support aac he/he-v2, see: ngx_rtmp_codec_parse_aac_header
  307 + // @see: https://github.com/winlinvip/nginx-rtmp-module/commit/3a5f9eea78fc8d11e8be922aea9ac349b9dcbfc2
  308 + //
  309 + // donot force to LC, @see: https://github.com/winlinvip/simple-rtmp-server/issues/81
  310 + // the source will print the sequence header info.
  311 + //if (aac_profile > 3) {
  312 + // Mark all extended profiles as LC
  313 + // to make Android as happy as possible.
  314 + // @see: ngx_rtmp_hls_parse_aac_header
  315 + //aac_profile = 1;
  316 + //}
  317 + } else if (aac_packet_type == SrsCodecAudioTypeRawData) {
  318 + // ensure the sequence header demuxed
  319 + if (aac_extra_size <= 0 || !aac_extra_data) {
  320 + ret = ERROR_HLS_DECODE_ERROR;
  321 + srs_error("audio codec decode aac failed, sequence header not found. ret=%d", ret);
  322 + return ret;
  323 + }
  324 +
  325 + // Raw AAC frame data in UI8 []
  326 + // 6.3 Raw Data, aac-iso-13818-7.pdf, page 28
  327 + if ((ret = sample->add_sample_unit(stream->data() + stream->pos(), stream->size() - stream->pos())) != ERROR_SUCCESS) {
  328 + srs_error("audio codec add sample failed. ret=%d", ret);
  329 + return ret;
  330 + }
  331 + } else {
  332 + // ignored.
  333 + }
  334 +
  335 + // reset the sample rate by sequence header
  336 + if (aac_sample_rate != __SRS_AAC_SAMPLE_RATE_UNSET) {
  337 + static int aac_sample_rates[] = {
  338 + 96000, 88200, 64000, 48000,
  339 + 44100, 32000, 24000, 22050,
  340 + 16000, 12000, 11025, 8000,
  341 + 7350, 0, 0, 0
  342 + };
  343 + switch (aac_sample_rates[aac_sample_rate]) {
  344 + case 11025:
  345 + sample->sound_rate = SrsCodecAudioSampleRate11025;
  346 + break;
  347 + case 22050:
  348 + sample->sound_rate = SrsCodecAudioSampleRate22050;
  349 + break;
  350 + case 44100:
  351 + sample->sound_rate = SrsCodecAudioSampleRate44100;
  352 + break;
  353 + default:
  354 + break;
  355 + };
  356 + }
  357 +
  358 + srs_info("audio decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d",
  359 + sound_type, audio_codec_id, sound_size, sound_rate, sound_format, size);
  360 +
  361 + return ret;
  362 +}
  363 +
  364 +int SrsAvcAacCodec::audio_mp3_demux(char* data, int size, SrsCodecSample* sample)
  365 +{
  366 + int ret = ERROR_SUCCESS;
  367 +
  368 + // we always decode aac then mp3.
  369 + srs_assert(sample->acodec == SrsCodecAudioMP3);
  370 +
  371 + // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
  372 + if (!data || size <= 1) {
  373 + srs_trace("no mp3 audio present, ignore it.");
  374 + return ret;
  375 + }
  376 +
  377 + // mp3 payload.
  378 + if ((ret = sample->add_sample_unit(data + 1, size - 1)) != ERROR_SUCCESS) {
  379 + srs_error("audio codec add mp3 sample failed. ret=%d", ret);
  380 + return ret;
  381 + }
  382 +
  383 + srs_info("audio decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d",
  384 + sample->sound_type, audio_codec_id, sample->sound_size, sample->sound_rate, sample->acodec, size);
  385 +
  386 + return ret;
  387 +}
  388 +
  389 +int SrsAvcAacCodec::video_avc_demux(char* data, int size, SrsCodecSample* sample)
  390 +{
  391 + int ret = ERROR_SUCCESS;
  392 +
  393 + sample->is_video = true;
  394 +
  395 + if (!data || size <= 0) {
  396 + srs_trace("no video present, ignore it.");
  397 + return ret;
  398 + }
  399 +
  400 + if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) {
  401 + return ret;
  402 + }
  403 +
  404 + // video decode
  405 + if (!stream->require(1)) {
  406 + ret = ERROR_HLS_DECODE_ERROR;
  407 + srs_error("video codec decode frame_type failed. ret=%d", ret);
  408 + return ret;
  409 + }
  410 +
  411 + // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78
  412 + int8_t frame_type = stream->read_1bytes();
  413 + int8_t codec_id = frame_type & 0x0f;
  414 + frame_type = (frame_type >> 4) & 0x0f;
  415 +
  416 + sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;
  417 +
  418 + // ignore info frame without error,
  419 + // @see https://github.com/winlinvip/simple-rtmp-server/issues/288#issuecomment-69863909
  420 + if (sample->frame_type == SrsCodecVideoAVCFrameVideoInfoFrame) {
  421 + srs_warn("video codec igone the info frame, ret=%d", ret);
  422 + return ret;
  423 + }
  424 +
  425 + // only support h.264/avc
  426 + if (codec_id != SrsCodecVideoAVC) {
  427 + ret = ERROR_HLS_DECODE_ERROR;
  428 + srs_error("video codec only support video h.264/avc codec. actual=%d, ret=%d", codec_id, ret);
  429 + return ret;
  430 + }
  431 + video_codec_id = codec_id;
  432 +
  433 + if (!stream->require(4)) {
  434 + ret = ERROR_HLS_DECODE_ERROR;
  435 + srs_error("video codec decode avc_packet_type failed. ret=%d", ret);
  436 + return ret;
  437 + }
  438 + int8_t avc_packet_type = stream->read_1bytes();
  439 + int32_t composition_time = stream->read_3bytes();
  440 +
  441 + // pts = dts + cts.
  442 + sample->cts = composition_time;
  443 + sample->avc_packet_type = (SrsCodecVideoAVCType)avc_packet_type;
  444 +
  445 + if (avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {
  446 + if ((ret = avc_demux_sps_pps(stream)) != ERROR_SUCCESS) {
  447 + return ret;
  448 + }
  449 + } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){
  450 + // ensure the sequence header demuxed
  451 + if (avc_extra_size <= 0 || !avc_extra_data) {
  452 + ret = ERROR_HLS_DECODE_ERROR;
  453 + srs_error("avc decode failed, sequence header not found. ret=%d", ret);
  454 + return ret;
  455 + }
  456 +
  457 + // One or more NALUs (Full frames are required)
  458 + // try "AnnexB" from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
  459 + if ((ret = avc_demux_annexb_format(stream, sample)) != ERROR_SUCCESS) {
  460 + // stop try when system error.
  461 + if (ret != ERROR_HLS_AVC_TRY_OTHERS) {
  462 + srs_error("avc demux for annexb failed. ret=%d", ret);
  463 + return ret;
  464 + }
  465 +
  466 + // try "ISO Base Media File Format" from H.264-AVC-ISO_IEC_14496-15.pdf, page 20
  467 + if ((ret = avc_demux_ibmf_format(stream, sample)) != ERROR_SUCCESS) {
  468 + return ret;
  469 + }
  470 + }
  471 + } else {
  472 + // ignored.
  473 + }
  474 +
  475 + srs_info("video decoded, type=%d, codec=%d, avc=%d, time=%d, size=%d",
  476 + frame_type, video_codec_id, avc_packet_type, composition_time, size);
  477 +
  478 + return ret;
  479 +}
  480 +
  481 +int SrsAvcAacCodec::avc_demux_sps_pps(SrsStream* stream)
  482 +{
  483 + int ret = ERROR_SUCCESS;
  484 +
  485 + // AVCDecoderConfigurationRecord
  486 + // 5.2.4.1.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  487 + avc_extra_size = stream->size() - stream->pos();
  488 + if (avc_extra_size > 0) {
  489 + srs_freep(avc_extra_data);
  490 + avc_extra_data = new char[avc_extra_size];
  491 + memcpy(avc_extra_data, stream->data() + stream->pos(), avc_extra_size);
  492 + }
  493 +
  494 + if (!stream->require(6)) {
  495 + ret = ERROR_HLS_DECODE_ERROR;
  496 + srs_error("avc decode sequenc header failed. ret=%d", ret);
  497 + return ret;
  498 + }
  499 + //int8_t configurationVersion = stream->read_1bytes();
  500 + stream->read_1bytes();
  501 + //int8_t AVCProfileIndication = stream->read_1bytes();
  502 + avc_profile = stream->read_1bytes();
  503 + //int8_t profile_compatibility = stream->read_1bytes();
  504 + stream->read_1bytes();
  505 + //int8_t AVCLevelIndication = stream->read_1bytes();
  506 + avc_level = stream->read_1bytes();
  507 +
  508 + // parse the NALU size.
  509 + int8_t lengthSizeMinusOne = stream->read_1bytes();
  510 + lengthSizeMinusOne &= 0x03;
  511 + NAL_unit_length = lengthSizeMinusOne;
  512 +
  513 + // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  514 + // 5.2.4.1 AVC decoder configuration record
  515 + // 5.2.4.1.2 Semantics
  516 + // The value of this field shall be one of 0, 1, or 3 corresponding to a
  517 + // length encoded with 1, 2, or 4 bytes, respectively.
  518 + if (NAL_unit_length == 2) {
  519 + ret = ERROR_HLS_DECODE_ERROR;
  520 + srs_error("sps lengthSizeMinusOne should never be 2. ret=%d", ret);
  521 + return ret;
  522 + }
  523 +
  524 + // 1 sps
  525 + if (!stream->require(1)) {
  526 + ret = ERROR_HLS_DECODE_ERROR;
  527 + srs_error("avc decode sequenc header sps failed. ret=%d", ret);
  528 + return ret;
  529 + }
  530 + int8_t numOfSequenceParameterSets = stream->read_1bytes();
  531 + numOfSequenceParameterSets &= 0x1f;
  532 + if (numOfSequenceParameterSets != 1) {
  533 + ret = ERROR_HLS_DECODE_ERROR;
  534 + srs_error("avc decode sequenc header sps failed. ret=%d", ret);
  535 + return ret;
  536 + }
  537 + if (!stream->require(2)) {
  538 + ret = ERROR_HLS_DECODE_ERROR;
  539 + srs_error("avc decode sequenc header sps size failed. ret=%d", ret);
  540 + return ret;
  541 + }
  542 + sequenceParameterSetLength = stream->read_2bytes();
  543 + if (!stream->require(sequenceParameterSetLength)) {
  544 + ret = ERROR_HLS_DECODE_ERROR;
  545 + srs_error("avc decode sequenc header sps data failed. ret=%d", ret);
  546 + return ret;
  547 + }
  548 + if (sequenceParameterSetLength > 0) {
  549 + srs_freep(sequenceParameterSetNALUnit);
  550 + sequenceParameterSetNALUnit = new char[sequenceParameterSetLength];
  551 + memcpy(sequenceParameterSetNALUnit, stream->data() + stream->pos(), sequenceParameterSetLength);
  552 + stream->skip(sequenceParameterSetLength);
  553 + }
  554 + // 1 pps
  555 + if (!stream->require(1)) {
  556 + ret = ERROR_HLS_DECODE_ERROR;
  557 + srs_error("avc decode sequenc header pps failed. ret=%d", ret);
  558 + return ret;
  559 + }
  560 + int8_t numOfPictureParameterSets = stream->read_1bytes();
  561 + numOfPictureParameterSets &= 0x1f;
  562 + if (numOfPictureParameterSets != 1) {
  563 + ret = ERROR_HLS_DECODE_ERROR;
  564 + srs_error("avc decode sequenc header pps failed. ret=%d", ret);
  565 + return ret;
  566 + }
  567 + if (!stream->require(2)) {
  568 + ret = ERROR_HLS_DECODE_ERROR;
  569 + srs_error("avc decode sequenc header pps size failed. ret=%d", ret);
  570 + return ret;
  571 + }
  572 + pictureParameterSetLength = stream->read_2bytes();
  573 + if (!stream->require(pictureParameterSetLength)) {
  574 + ret = ERROR_HLS_DECODE_ERROR;
  575 + srs_error("avc decode sequenc header pps data failed. ret=%d", ret);
  576 + return ret;
  577 + }
  578 + if (pictureParameterSetLength > 0) {
  579 + srs_freep(pictureParameterSetNALUnit);
  580 + pictureParameterSetNALUnit = new char[pictureParameterSetLength];
  581 + memcpy(pictureParameterSetNALUnit, stream->data() + stream->pos(), pictureParameterSetLength);
  582 + stream->skip(pictureParameterSetLength);
  583 + }
  584 +
  585 + return ret;
  586 +}
  587 +
  588 +int SrsAvcAacCodec::avc_demux_annexb_format(SrsStream* stream, SrsCodecSample* sample)
  589 +{
  590 + int ret = ERROR_SUCCESS;
  591 +
  592 + // not annexb, try others
  593 + if (!srs_avc_startswith_annexb(stream, NULL)) {
  594 + return ERROR_HLS_AVC_TRY_OTHERS;
  595 + }
  596 +
  597 + // AnnexB
  598 + // B.1.1 Byte stream NAL unit syntax,
  599 + // H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
  600 + while (!stream->empty()) {
  601 + // find start code
  602 + int nb_start_code = 0;
  603 + if (!srs_avc_startswith_annexb(stream, &nb_start_code)) {
  604 + return ret;
  605 + }
  606 +
  607 + // skip the start code.
  608 + if (nb_start_code > 0) {
  609 + stream->skip(nb_start_code);
  610 + }
  611 +
  612 + // the NALU start bytes.
  613 + char* p = stream->data() + stream->pos();
  614 +
  615 + // get the last matched NALU
  616 + while (!stream->empty()) {
  617 + if (srs_avc_startswith_annexb(stream, NULL)) {
  618 + break;
  619 + }
  620 +
  621 + stream->skip(1);
  622 + }
  623 +
  624 + char* pp = stream->data() + stream->pos();
  625 +
  626 + // skip the empty.
  627 + if (pp - p <= 0) {
  628 + continue;
  629 + }
  630 +
  631 + // got the NALU.
  632 + if ((ret = sample->add_sample_unit(p, pp - p)) != ERROR_SUCCESS) {
  633 + srs_error("annexb add video sample failed. ret=%d", ret);
  634 + return ret;
  635 + }
  636 + }
  637 +
  638 + return ret;
  639 +}
  640 +
  641 +int SrsAvcAacCodec::avc_demux_ibmf_format(SrsStream* stream, SrsCodecSample* sample)
  642 +{
  643 + int ret = ERROR_SUCCESS;
  644 +
  645 + int PictureLength = stream->size() - stream->pos();
  646 +
  647 + // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  648 + // 5.2.4.1 AVC decoder configuration record
  649 + // 5.2.4.1.2 Semantics
  650 + // The value of this field shall be one of 0, 1, or 3 corresponding to a
  651 + // length encoded with 1, 2, or 4 bytes, respectively.
  652 + srs_assert(NAL_unit_length != 2);
  653 +
  654 + // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20
  655 + for (int i = 0; i < PictureLength;) {
  656 + // unsigned int((NAL_unit_length+1)*8) NALUnitLength;
  657 + if (!stream->require(NAL_unit_length + 1)) {
  658 + ret = ERROR_HLS_DECODE_ERROR;
  659 + srs_error("avc decode NALU size failed. ret=%d", ret);
  660 + return ret;
  661 + }
  662 + int32_t NALUnitLength = 0;
  663 + if (NAL_unit_length == 3) {
  664 + NALUnitLength = stream->read_4bytes();
  665 + } else if (NAL_unit_length == 1) {
  666 + NALUnitLength = stream->read_2bytes();
  667 + } else {
  668 + NALUnitLength = stream->read_1bytes();
  669 + }
  670 +
  671 + // maybe stream is invalid format.
  672 + // see: https://github.com/winlinvip/simple-rtmp-server/issues/183
  673 + if (NALUnitLength < 0) {
  674 + ret = ERROR_HLS_DECODE_ERROR;
  675 + srs_error("maybe stream is AnnexB format. ret=%d", ret);
  676 + return ret;
  677 + }
  678 +
  679 + // NALUnit
  680 + if (!stream->require(NALUnitLength)) {
  681 + ret = ERROR_HLS_DECODE_ERROR;
  682 + srs_error("avc decode NALU data failed. ret=%d", ret);
  683 + return ret;
  684 + }
  685 + // 7.3.1 NAL unit syntax, H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  686 + if ((ret = sample->add_sample_unit(stream->data() + stream->pos(), NALUnitLength)) != ERROR_SUCCESS) {
  687 + srs_error("avc add video sample failed. ret=%d", ret);
  688 + return ret;
  689 + }
  690 + stream->skip(NALUnitLength);
  691 +
  692 + i += NAL_unit_length + 1 + NALUnitLength;
  693 + }
  694 +
  695 + return ret;
  696 +}
  697 +
@@ -30,6 +30,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,6 +30,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
  33 +#include <string>
  34 +
  35 +class SrsStream;
  36 +
33 // AACPacketType IF SoundFormat == 10 UI8 37 // AACPacketType IF SoundFormat == 10 UI8
34 // The following values are defined: 38 // The following values are defined:
35 // 0 = AAC sequence header 39 // 0 = AAC sequence header
@@ -212,4 +216,263 @@ public: @@ -212,4 +216,263 @@ public:
212 static bool audio_is_aac(char* data, int size); 216 static bool audio_is_aac(char* data, int size);
213 }; 217 };
214 218
  219 +/**
  220 +* the public data, event HLS disable, others can use it.
  221 +*/
  222 +/**
  223 +* the flv sample rate map
  224 +*/
  225 +extern int flv_sample_rates[];
  226 +
  227 +/**
  228 +* the aac sample rate map
  229 +*/
  230 +extern int aac_sample_rates[];
  231 +
  232 +#define __SRS_SRS_MAX_CODEC_SAMPLE 128
  233 +#define __SRS_AAC_SAMPLE_RATE_UNSET 15
  234 +
  235 +// in ms, for HLS aac flush the audio
  236 +#define SRS_CONF_DEFAULT_AAC_DELAY 100
  237 +
  238 +// max PES packets size to flush the video.
  239 +#define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 1024 * 1024
  240 +
  241 +/**
  242 +* the FLV/RTMP supported audio sample size.
  243 +* Size of each audio sample. This parameter only pertains to
  244 +* uncompressed formats. Compressed formats always decode
  245 +* to 16 bits internally.
  246 +* 0 = 8-bit samples
  247 +* 1 = 16-bit samples
  248 +*/
  249 +enum SrsCodecAudioSampleSize
  250 +{
  251 + // set to the max value to reserved, for array map.
  252 + SrsCodecAudioSampleSizeReserved = 2,
  253 +
  254 + SrsCodecAudioSampleSize8bit = 0,
  255 + SrsCodecAudioSampleSize16bit = 1,
  256 +};
  257 +
  258 +/**
  259 +* the FLV/RTMP supported audio sound type/channel.
  260 +* Mono or stereo sound
  261 +* 0 = Mono sound
  262 +* 1 = Stereo sound
  263 +*/
  264 +enum SrsCodecAudioSoundType
  265 +{
  266 + // set to the max value to reserved, for array map.
  267 + SrsCodecAudioSoundTypeReserved = 2,
  268 +
  269 + SrsCodecAudioSoundTypeMono = 0,
  270 + SrsCodecAudioSoundTypeStereo = 1,
  271 +};
  272 +
  273 +/**
  274 +* the codec sample unit.
  275 +* for h.264 video packet, a NALU is a sample unit.
  276 +* for aac raw audio packet, a NALU is the entire aac raw data.
  277 +* for sequence header, it's not a sample unit.
  278 +*/
  279 +class SrsCodecSampleUnit
  280 +{
  281 +public:
  282 + /**
  283 + * the sample bytes is directly ptr to packet bytes,
  284 + * user should never use it when packet destroyed.
  285 + */
  286 + int size;
  287 + char* bytes;
  288 +public:
  289 + SrsCodecSampleUnit();
  290 + virtual ~SrsCodecSampleUnit();
  291 +};
  292 +
  293 +/**
  294 +* the samples in the flv audio/video packet.
  295 +* the sample used to analysis a video/audio packet,
  296 +* split the h.264 NALUs to buffers, or aac raw data to a buffer,
  297 +* and decode the video/audio specified infos.
  298 +*
  299 +* the sample unit:
  300 +* a video packet codec in h.264 contains many NALUs, each is a sample unit.
  301 +* a audio packet codec in aac is a sample unit.
  302 +* @remark, the video/audio sequence header is not sample unit,
  303 +* all sequence header stores as extra data,
  304 +* @see SrsAvcAacCodec.avc_extra_data and SrsAvcAacCodec.aac_extra_data
  305 +* @remark, user must clear all samples before decode a new video/audio packet.
  306 +*/
  307 +class SrsCodecSample
  308 +{
  309 +public:
  310 + /**
  311 + * each audio/video raw data packet will dumps to one or multiple buffers,
  312 + * the buffers will write to hls and clear to reset.
  313 + * generally, aac audio packet corresponding to one buffer,
  314 + * where avc/h264 video packet may contains multiple buffer.
  315 + */
  316 + int nb_sample_units;
  317 + SrsCodecSampleUnit sample_units[__SRS_SRS_MAX_CODEC_SAMPLE];
  318 +public:
  319 + /**
  320 + * whether the sample is video sample which demux from video packet.
  321 + */
  322 + bool is_video;
  323 + /**
  324 + * CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
  325 + * cts = pts - dts, where dts = flvheader->timestamp.
  326 + */
  327 + int32_t cts;
  328 +public:
  329 + // video specified
  330 + SrsCodecVideoAVCFrame frame_type;
  331 + SrsCodecVideoAVCType avc_packet_type;
  332 +public:
  333 + // audio specified
  334 + SrsCodecAudio acodec;
  335 + // audio aac specified.
  336 + SrsCodecAudioSampleRate sound_rate;
  337 + SrsCodecAudioSampleSize sound_size;
  338 + SrsCodecAudioSoundType sound_type;
  339 + SrsCodecAudioType aac_packet_type;
  340 +public:
  341 + SrsCodecSample();
  342 + virtual ~SrsCodecSample();
  343 +public:
  344 + /**
  345 + * clear all samples.
  346 + * the sample units never copy the bytes, it directly use the ptr,
  347 + * so when video/audio packet is destroyed, the sample must be clear.
  348 + * in a word, user must clear sample before demux it.
  349 + * @remark demux sample use SrsAvcAacCodec.audio_aac_demux or video_avc_demux.
  350 + */
  351 + void clear();
  352 + /**
  353 + * add the a sample unit, it's a h.264 NALU or aac raw data.
  354 + * the sample unit directly use the ptr of packet bytes,
  355 + * so user must never use sample unit when packet is destroyed.
  356 + * in a word, user must clear sample before demux it.
  357 + */
  358 + int add_sample_unit(char* bytes, int size);
  359 +};
  360 +
  361 +/**
  362 +* the h264/avc and aac codec, for media stream.
  363 +*
  364 +* to demux the FLV/RTMP video/audio packet to sample,
  365 +* add each NALUs of h.264 as a sample unit to sample,
  366 +* while the entire aac raw data as a sample unit.
  367 +*
  368 +* for sequence header,
  369 +* demux it and save it in the avc_extra_data and aac_extra_data,
  370 +*
  371 +* for the codec info, such as audio sample rate,
  372 +* decode from FLV/RTMP header, then use codec info in sequence
  373 +* header to override it.
  374 +*/
  375 +class SrsAvcAacCodec
  376 +{
  377 +private:
  378 + SrsStream* stream;
  379 +public:
  380 + /**
  381 + * metadata specified
  382 + */
  383 + int duration;
  384 + int width;
  385 + int height;
  386 + int frame_rate;
  387 + // @see: SrsCodecVideo
  388 + int video_codec_id;
  389 + int video_data_rate; // in bps
  390 + // @see: SrsCod ecAudioType
  391 + int audio_codec_id;
  392 + int audio_data_rate; // in bps
  393 +public:
  394 + /**
  395 + * video specified
  396 + */
  397 + // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
  398 + u_int8_t avc_profile;
  399 + // level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
  400 + u_int8_t avc_level;
  401 + // lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  402 + int8_t NAL_unit_length;
  403 + u_int16_t sequenceParameterSetLength;
  404 + char* sequenceParameterSetNALUnit;
  405 + u_int16_t pictureParameterSetLength;
  406 + char* pictureParameterSetNALUnit;
  407 +public:
  408 + /**
  409 + * audio specified
  410 + * audioObjectType, in 1.6.2.1 AudioSpecificConfig, page 33,
  411 + * 1.5.1.1 Audio object type definition, page 23,
  412 + * in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf.
  413 + */
  414 + u_int8_t aac_profile;
  415 + /**
  416 + * samplingFrequencyIndex
  417 + */
  418 + u_int8_t aac_sample_rate;
  419 + /**
  420 + * channelConfiguration
  421 + */
  422 + u_int8_t aac_channels;
  423 +public:
  424 + /**
  425 + * the avc extra data, the AVC sequence header,
  426 + * without the flv codec header,
  427 + * @see: ffmpeg, AVCodecContext::extradata
  428 + */
  429 + int avc_extra_size;
  430 + char* avc_extra_data;
  431 + /**
  432 + * the aac extra data, the AAC sequence header,
  433 + * without the flv codec header,
  434 + * @see: ffmpeg, AVCodecContext::extradata
  435 + */
  436 + int aac_extra_size;
  437 + char* aac_extra_data;
  438 +public:
  439 + SrsAvcAacCodec();
  440 + virtual ~SrsAvcAacCodec();
  441 +// the following function used for hls to build the sample and codec.
  442 +public:
  443 + /**
  444 + * demux the audio packet in aac codec.
  445 + * the packet mux in FLV/RTMP format defined in flv specification.
  446 + * demux the audio speicified data(sound_format, sound_size, ...) to sample.
  447 + * demux the aac specified data(aac_profile, ...) to codec from sequence header.
  448 + * demux the aac raw to sample units.
  449 + */
  450 + virtual int audio_aac_demux(char* data, int size, SrsCodecSample* sample);
  451 + virtual int audio_mp3_demux(char* data, int size, SrsCodecSample* sample);
  452 + /**
  453 + * demux the video packet in h.264 codec.
  454 + * the packet mux in FLV/RTMP format defined in flv specification.
  455 + * demux the video specified data(frame_type, codec_id, ...) to sample.
  456 + * demux the h.264 sepcified data(avc_profile, ...) to codec from sequence header.
  457 + * demux the h.264 NALUs to sampe units.
  458 + */
  459 + virtual int video_avc_demux(char* data, int size, SrsCodecSample* sample);
  460 +private:
  461 + /**
  462 + * when avc packet type is SrsCodecVideoAVCTypeSequenceHeader,
  463 + * decode the sps and pps.
  464 + */
  465 + virtual int avc_demux_sps_pps(SrsStream* stream);
  466 + /**
  467 + * demux the avc NALU in "AnnexB"
  468 + * from H.264-AVC-ISO_IEC_14496-10.pdf, page 211.
  469 + */
  470 + virtual int avc_demux_annexb_format(SrsStream* stream, SrsCodecSample* sample);
  471 + /**
  472 + * demux the avc NALU in "ISO Base Media File Format"
  473 + * from H.264-AVC-ISO_IEC_14496-15.pdf, page 20
  474 + */
  475 + virtual int avc_demux_ibmf_format(SrsStream* stream, SrsCodecSample* sample);
  476 +};
  477 +
215 #endif 478 #endif
@@ -35,7 +35,7 @@ using namespace std; @@ -35,7 +35,7 @@ using namespace std;
35 #include <srs_kernel_log.hpp> 35 #include <srs_kernel_log.hpp>
36 #include <srs_kernel_error.hpp> 36 #include <srs_kernel_error.hpp>
37 #include <srs_kernel_file.hpp> 37 #include <srs_kernel_file.hpp>
38 -#include <srs_kernel_avc.hpp> 38 +#include <srs_kernel_codec.hpp>
39 #include <srs_kernel_buffer.hpp> 39 #include <srs_kernel_buffer.hpp>
40 #include <srs_kernel_utility.hpp> 40 #include <srs_kernel_utility.hpp>
41 #include <srs_kernel_stream.hpp> 41 #include <srs_kernel_stream.hpp>
@@ -1520,7 +1520,6 @@ int srs_h264_write_raw_frames(srs_rtmp_t rtmp, @@ -1520,7 +1520,6 @@ int srs_h264_write_raw_frames(srs_rtmp_t rtmp,
1520 while (!context->h264_raw_stream.empty()) { 1520 while (!context->h264_raw_stream.empty()) {
1521 char* frame = NULL; 1521 char* frame = NULL;
1522 int frame_size = 0; 1522 int frame_size = 0;
1523 - bool got_sps_pps = false;  
1524 if ((ret = context->avc_raw.annexb_demux(&context->h264_raw_stream, &frame, &frame_size)) != ERROR_SUCCESS) { 1523 if ((ret = context->avc_raw.annexb_demux(&context->h264_raw_stream, &frame, &frame_size)) != ERROR_SUCCESS) {
1525 return ret; 1524 return ret;
1526 } 1525 }