Skip to content

Commit c459d0a

Browse files
committed
compat for Camera1 and Camera2
1 parent 30110d1 commit c459d0a

4 files changed

Lines changed: 41 additions & 69 deletions

File tree

‎Live/src/main/cpp/RtmpPusher.cpp‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void releasePackets(RTMPPacket *&packet) {
7070
if (packet) {
7171
RTMPPacket_Free(packet);
7272
delete packet;
73-
packet = 0;
73+
packet = nullptr;
7474
}
7575
}
7676

@@ -173,26 +173,23 @@ RTMP_PUSHER_FUNC(void, native_1start, jstring path_) {
173173
env->ReleaseStringUTFChars(path_, path);
174174
}
175175

176-
RTMP_PUSHER_FUNC(void, native_1pushVideo, jbyteArray data_) {
176+
RTMP_PUSHER_FUNC(void, native_1pushVideo, jbyteArray yuv, jbyteArray y, jbyteArray u, jbyteArray v) {
177177
if (!videoStream || !readyPushing) {
178178
return;
179179
}
180-
jbyte *data = env->GetByteArrayElements(data_, nullptr);
181-
videoStream->encodeData(data);
182-
env->ReleaseByteArrayElements(data_, data, 0);
183-
}
184-
185-
RTMP_PUSHER_FUNC(void, native_1pushVideoNew, jbyteArray y, jbyteArray u, jbyteArray v) {
186-
if (!videoStream || !readyPushing) {
187-
return;
180+
if (yuv) {
181+
jbyte *yuv_plane = env->GetByteArrayElements(yuv, JNI_FALSE);
182+
videoStream->encodeVideo(yuv_plane, nullptr, nullptr, nullptr);
183+
env->ReleaseByteArrayElements(yuv, yuv_plane, 0);
184+
} else if (y && u && v) {
185+
jbyte *y_plane = env->GetByteArrayElements(y, JNI_FALSE);
186+
jbyte *u_plane = env->GetByteArrayElements(u, JNI_FALSE);
187+
jbyte *v_plane = env->GetByteArrayElements(v, JNI_FALSE);
188+
videoStream->encodeVideo(nullptr, y_plane, u_plane, v_plane);
189+
env->ReleaseByteArrayElements(y, y_plane, 0);
190+
env->ReleaseByteArrayElements(u, u_plane, 0);
191+
env->ReleaseByteArrayElements(v, v_plane, 0);
188192
}
189-
jbyte *y_plane = env->GetByteArrayElements(y, nullptr);
190-
jbyte *u_plane = env->GetByteArrayElements(u, nullptr);
191-
jbyte *v_plane = env->GetByteArrayElements(v, nullptr);
192-
videoStream->encodeDataNew(y_plane, u_plane, v_plane);
193-
env->ReleaseByteArrayElements(y, y_plane, 0);
194-
env->ReleaseByteArrayElements(u, u_plane, 0);
195-
env->ReleaseByteArrayElements(v, v_plane, 0);
196193
}
197194

198195
RTMP_PUSHER_FUNC(void, native_1setAudioCodecInfo, jint sampleRateInHz, jint channels) {

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

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
#include "VideoStream.h"
55
#include "PushInterface.h"
66

7-
VideoStream::VideoStream() {
7+
VideoStream::VideoStream():yLen(0),
8+
mBitrate(0),
9+
videoCodec(nullptr),
10+
pic_in(nullptr),
11+
videoCallback(nullptr) {
812
pthread_mutex_init(&mutex, nullptr);
913
}
1014

@@ -22,9 +26,6 @@ VideoStream::~VideoStream() {
2226

2327
void VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) {
2428
pthread_mutex_lock(&mutex);
25-
mWidth = width;
26-
mHeight = height;
27-
mFps = fps;
2829
mBitrate = bitrate;
2930
yLen = width * height;
3031
if (videoCodec) {
@@ -81,44 +82,24 @@ void VideoStream::setVideoCallback(VideoCallback callback) {
8182
this->videoCallback = callback;
8283
}
8384

84-
void VideoStream::encodeData(int8_t *data) {
85+
void VideoStream::encodeVideo(int8_t *data, int8_t *y_plane, int8_t *u_plane, int8_t *v_plane) {
8586
pthread_mutex_lock(&mutex);
86-
//y
87-
memcpy(pic_in->img.plane[0], data, yLen);
88-
for (int i = 0; i < yLen/4; ++i) {
89-
//uv
90-
*(pic_in->img.plane[1] + i) = *(data + yLen + i * 2 + 1);
91-
*(pic_in->img.plane[2] + i) = *(data + yLen + i * 2);
92-
}
9387

94-
x264_nal_t *pp_nal;
95-
int pi_nal;
96-
x264_picture_t pic_out;
97-
x264_encoder_encode(videoCodec, &pp_nal, &pi_nal, pic_in, &pic_out);
98-
int pps_len, sps_len = 0;
99-
uint8_t sps[100];
100-
uint8_t pps[100];
101-
for (int i = 0; i < pi_nal; ++i) {
102-
if (pp_nal[i].i_type == NAL_SPS) {
103-
sps_len = pp_nal[i].i_payload - 4;
104-
memcpy(sps, pp_nal[i].p_payload + 4, static_cast<size_t>(sps_len));
105-
} else if (pp_nal[i].i_type == NAL_PPS) {
106-
pps_len = pp_nal[i].i_payload - 4;
107-
memcpy(pps, pp_nal[i].p_payload + 4, static_cast<size_t>(pps_len));
108-
sendSpsPps(sps, pps, sps_len, pps_len);
109-
} else {
110-
sendFrame(pp_nal[i].i_type, pp_nal[i].p_payload, pp_nal[i].i_payload);
88+
if (data) {
89+
//y
90+
memcpy(pic_in->img.plane[0], data, yLen);
91+
//uv
92+
for (int i = 0; i < yLen/4; ++i) {
93+
*(pic_in->img.plane[1] + i) = *(data + yLen + i * 2 + 1);
94+
*(pic_in->img.plane[2] + i) = *(data + yLen + i * 2);
11195
}
96+
} else if (y_plane && u_plane && v_plane) {
97+
memcpy(pic_in->img.plane[0], y_plane, (size_t) yLen);
98+
memcpy(pic_in->img.plane[1], u_plane, (size_t) yLen / 4);
99+
memcpy(pic_in->img.plane[2], v_plane, (size_t) yLen / 4);
100+
} else {
101+
return;
112102
}
113-
pthread_mutex_unlock(&mutex);
114-
}
115-
116-
void VideoStream::encodeDataNew(int8_t *y_plane, int8_t *u_plane, int8_t *v_plane) {
117-
pthread_mutex_lock(&mutex);
118-
119-
memcpy(pic_in->img.plane[0], y_plane, (size_t) yLen);
120-
memcpy(pic_in->img.plane[1], u_plane, (size_t) yLen / 4);
121-
memcpy(pic_in->img.plane[2], v_plane, (size_t) yLen / 4);
122103

123104
x264_nal_t *pp_nal;
124105
int pi_nal;

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,17 @@ class VideoStream {
1717

1818
void setVideoEncInfo(int width, int height, int fps, int bitrate);
1919

20-
void encodeData(int8_t *data);
21-
22-
void encodeDataNew(int8_t *y_plane, int8_t *u_plane, int8_t *v_plane);
20+
void encodeVideo(int8_t *data, int8_t *y_plane, int8_t *u_plane, int8_t *v_plane);
2321

2422
void setVideoCallback(VideoCallback videoCallback);
2523

2624
private:
27-
pthread_mutex_t mutex;
28-
int mWidth;
29-
int mHeight;
30-
int mFps;
25+
pthread_mutex_t mutex{};
26+
27+
int yLen;
3128
int mBitrate;
3229
x264_t *videoCodec = 0;
3330
x264_picture_t *pic_in = 0;
34-
35-
int yLen;
3631
VideoCallback videoCallback;
3732

3833
void sendSpsPps(uint8_t *sps, uint8_t *pps, int sps_len, int pps_len);

‎Live/src/main/java/com/frank/live/LivePusherNew.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ public void pushAudio(byte[] data) {
150150
}
151151

152152
public void pushVideo(byte[] data) {
153-
native_pushVideo(data);
153+
native_pushVideo(data, null, null, null);
154154
}
155155

156156
public void pushVideo(byte[] y, byte[] u, byte[] v) {
157-
native_pushVideoNew(y, u, v);
157+
native_pushVideo(null, y, u, v);
158158
}
159159

160160
private native void native_init();
@@ -169,9 +169,8 @@ public void pushVideo(byte[] y, byte[] u, byte[] v) {
169169

170170
private native void native_pushAudio(byte[] data);
171171

172-
private native void native_pushVideo(byte[] data);
173-
174-
private native void native_pushVideoNew(byte[] y, byte[] u, byte[] v);
172+
// compat for Camera1 and Camera2
173+
private native void native_pushVideo(byte[] yuv, byte[] y, byte[] u, byte[] v);
175174

176175
private native void native_stop();
177176

0 commit comments

Comments
 (0)