Skip to content

Commit a69b72c

Browse files
author
xufuji456
committed
Feature: change to c++ mutex
1 parent 81de600 commit a69b72c

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

‎Live/src/main/cpp/VideoStream.cpp‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1313
VideoStream::~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

2625
int 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(&param, "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(&param, "baseline");
7675
if (ret < 0) {
77-
goto end;
76+
return ret;
7877
}
7978
//open encoder
8079
videoCodec = x264_encoder_open(&param);
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

9692
void 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

141135
void 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;

‎Live/src/main/cpp/VideoStream.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define VIDEOSTREAM_H
44

55
#include <inttypes.h>
6-
#include <pthread.h>
6+
#include <mutex>
77
#include "rtmp/rtmp.h"
88
#include "include/x264/x264.h"
99

@@ -22,7 +22,7 @@ class VideoStream {
2222
void setVideoCallback(VideoCallback videoCallback);
2323

2424
private:
25-
pthread_mutex_t mutex{};
25+
std::mutex m_mutex;
2626

2727
int m_frameLen;
2828
x264_t *videoCodec = 0;

0 commit comments

Comments
 (0)