@@ -7,11 +7,10 @@ VideoStream::VideoStream():m_frameLen(0),
77 videoCodec(nullptr ),
88 pic_in(nullptr ),
99 videoCallback(nullptr ) {
10- pthread_mutex_init (&mutex, nullptr );
10+
1111}
1212
1313VideoStream::~VideoStream () {
14- pthread_mutex_destroy (&mutex);
1514 if (videoCodec) {
1615 x264_encoder_close (videoCodec);
1716 videoCodec = nullptr ;
@@ -24,7 +23,7 @@ VideoStream::~VideoStream() {
2423}
2524
2625int VideoStream::setVideoEncInfo (int width, int height, int fps, int bitrate) {
27- pthread_mutex_lock (& mutex);
26+ std::lock_guard<std:: mutex> l (m_mutex );
2827 m_frameLen = width * height;
2928 if (videoCodec) {
3029 x264_encoder_close (videoCodec);
@@ -40,7 +39,7 @@ int VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) {
4039 x264_param_t param;
4140 int ret = x264_param_default_preset (¶m, " ultrafast" , " zerolatency" );
4241 if (ret < 0 ) {
43- goto end ;
42+ return ret ;
4443 }
4544 param.i_level_idc = 32 ;
4645 // input format
@@ -74,18 +73,15 @@ int VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) {
7473
7574 ret = x264_param_apply_profile (¶m, " baseline" );
7675 if (ret < 0 ) {
77- goto end ;
76+ return ret ;
7877 }
7978 // open encoder
8079 videoCodec = x264_encoder_open (¶m);
8180 if (!videoCodec) {
82- ret = -1 ;
83- goto end;
81+ return -1 ;
8482 }
8583 pic_in = new x264_picture_t ();
8684 x264_picture_alloc (pic_in, X264_CSP_I420 , width, height);
87- end:
88- pthread_mutex_unlock (&mutex);
8985 return ret;
9086}
9187
@@ -94,8 +90,7 @@ void VideoStream::setVideoCallback(VideoCallback callback) {
9490}
9591
9692void VideoStream::encodeVideo (int8_t *data, int camera_type) {
97- pthread_mutex_lock (&mutex);
98-
93+ std::lock_guard<std::mutex> l (m_mutex);
9994 if (!pic_in)
10095 return ;
10196
@@ -135,24 +130,24 @@ void VideoStream::encodeVideo(int8_t *data, int camera_type) {
135130 sendFrame (pp_nal[i].i_type , pp_nal[i].p_payload , pp_nal[i].i_payload );
136131 }
137132 }
138- pthread_mutex_unlock (&mutex);
139133}
140134
141135void VideoStream::sendSpsPps (uint8_t *sps, uint8_t *pps, int sps_len, int pps_len) {
142136 int bodySize = 13 + sps_len + 3 + pps_len;
143137 auto *packet = new RTMPPacket ();
144138 RTMPPacket_Alloc (packet, bodySize);
145139 int i = 0 ;
146- // start code
140+ // type
147141 packet->m_body [i++] = 0x17 ;
148- // type
149142 packet->m_body [i++] = 0x00 ;
143+ // timestamp
150144 packet->m_body [i++] = 0x00 ;
151145 packet->m_body [i++] = 0x00 ;
152146 packet->m_body [i++] = 0x00 ;
153147
154148 // version
155149 packet->m_body [i++] = 0x01 ;
150+ // profile
156151 packet->m_body [i++] = sps[1 ];
157152 packet->m_body [i++] = sps[2 ];
158153 packet->m_body [i++] = sps[3 ];
@@ -196,11 +191,11 @@ void VideoStream::sendFrame(int type, uint8_t *payload, int i_payload) {
196191 auto *packet = new RTMPPacket ();
197192 RTMPPacket_Alloc (packet, bodySize);
198193
199- packet->m_body [0 ] = 0x27 ;
194+ packet->m_body [0 ] = 0x27 ; // 2:None key frame 7:AVC
200195 if (type == NAL_SLICE_IDR ) {
201- packet->m_body [0 ] = 0x17 ;
196+ packet->m_body [0 ] = 0x17 ; // 1:Key frame 7:AVC
202197 }
203- // packet type
198+ // AVC NALU
204199 packet->m_body [1 ] = 0x01 ;
205200 // timestamp
206201 packet->m_body [2 ] = 0x00 ;
0 commit comments