拿到绘制好的buffer,并调用native绘制接口
void *base;
result = outBuffer->lock(android::GraphicBuffer::USAGE_SW_READ_OFTEN,
&base);
if (base == nullptr || result != 0) {
RTC_LOG(LS_INFO) << "Webrtc onFrameCallback base null";
return;
}
client_->onFrameCaptured(reinterpret_cast<uint8_t *>(base),outBuffer->getStride(),region);
outBuffer->unlock();
进行native绘制
void onFrameCaptured(uint8_t *data,int srcStride,Region &changeRegion);
void AndroidCallClient::onFrameCaptured(uint8_t *data, int srcStride, Region &changedRegion) {
//webrtc::MutexLock lock(&pc_mutex_);
ANativeWindow_Buffer outBuffer;
int surfaceWidth = mRight - mLeft;
int surfaceHeight = mBottom - mTop;
if (mDisplayWidth <= 0) {
return;
}
{
webrtc::MutexLock lock(&pc_mutex_);
if (!allowDrawPicture) {
return;
}
}
if (nativeWindow == nullptr) {
return;
}
ANativeWindow_setBuffersGeometry(nativeWindow, surfaceWidth, surfaceHeight,
WINDOW_FORMAT_RGBA_8888);
ANativeWindow_acquire(nativeWindow);
if (ANativeWindow_lock(nativeWindow, &outBuffer, nullptr) < 0) {
ANativeWindow_release(nativeWindow);
nativeWindow = nullptr;
return;
}
//draw picture
uint8_t *dst_data = static_cast<uint8_t *>(outBuffer.bits);
int srcLineSize = srcStride * 4;
int dstLineSize = outBuffer.stride * 4;
libyuv::ARGBScale(data,
srcLineSize,
mDisplayWidth,
mDisplayHeight,
dst_data,
dstLineSize,
surfaceWidth,
surfaceHeight,
libyuv::kFilterNone);
//draw red rect
size_t count = 0;
int alpha = 0xFF;
int red = 0xFF;
int green = 0x00;
int blue = 0x00;
int colorABGR = (alpha << 24) | (blue << 16) | (green << 8) | red;
int x, y, left, top, right, bottom;
const Rect *allRects = changedRegion.getArray(&count);
if (count > 0) {
float wRatio = surfaceWidth * 1.0 / mDisplayWidth;
float hRatio = surfaceHeight * 1.0 / mDisplayHeight;
RTC_LOG(LS_ERROR)
<< "rect count:" << count << ",wRatio:" << wRatio << ",hRatio:" << hRatio;
for (int i = 0; i < count; i++) {
Rect rect = allRects[i];
if ((rect.left < 0 || rect.right <= 0 || rect.top < 0 || rect.bottom <= 0)
|| (rect.right <= rect.left)
|| (rect.bottom <= rect.top)) {
RTC_LOG(LS_ERROR) << "invalid rect,ignore";
continue;
}
left = (int) (rect.left * wRatio);
top = (int) (rect.top * hRatio);
right = (int) (rect.right * wRatio);
bottom = (int) (rect.bottom * hRatio);
if (right >= surfaceWidth) right = surfaceWidth - 1;
if (bottom >= surfaceHeight) bottom = surfaceHeight - 1;
//draw RED rectangle
uint32_t *line = (uint32_t *) outBuffer.bits;
//move buffer's Y position to top
for (y = 0; y < top; y++) {
line = line + outBuffer.stride;
}
//top border
for (x = left; x < right; x++) {
line[x] = colorABGR;
}
for (y = top; y < bottom; y++) {
line[left] = colorABGR; //left border
line[right] = colorABGR; //right border
line = line + outBuffer.stride;
}
//bottom border
for (x = left; x < right; x++) {
line[x] = colorABGR;
}
}
}
if (ANativeWindow_unlockAndPost(nativeWindow) < 0) {
RTC_LOG(LS_ERROR) << "ANativeWindow_unlockAndPost failed";
}
}
release native window
void releaseNativeWindow(JNIEnv* env);
void AndroidCallClient::releaseNativeWindow(JNIEnv *env) {
webrtc::MutexLock lock(&pc_mutex_);
if (false) {
RTC_LOG(LS_WARNING) << "begin to download leak stack.";
leaktracer::MemoryTrace::GetInstance().stopAllMonitoring();
std::ofstream out;
out.open("/sdcard/leaktracer.log", std::ios_base::out);
if (out.is_open()) {
leaktracer::MemoryTrace::GetInstance().writeLeaks(out);
} else {
RTC_LOG(LS_WARNING) << "Failed to write to /sdcard/leaktracer.log.";
}
}
allowDrawPicture = false;
RTC_LOG(LS_INFO) << "releaseNativeWindow";
if (nativeWindow != nullptr) {
RTC_LOG(LS_INFO) << "ANativeWindow_release ";
ANativeWindow_release(nativeWindow);
nativeWindow = nullptr;
}
}
setsurface init native window
void AndroidCallClient::SetSurface(JNIEnv *env,
jobject surface,
jint format,
jint left,
jint top,
jint right,
jint bottom,
jint displayWidth,
jint displayHeight) {
RTC_LOG(LS_INFO)
<< "SetSurface:" << format << "," << left << "," << top << "," << right << "," << bottom
<< "," << displayWidth << "," << displayHeight;
{
webrtc::MutexLock lock(&pc_mutex_);
allowDrawPicture = false;
}
if ((right - left) > displayWidth / 2) {
RTC_LOG(LS_ERROR) << "surface is not determined,ignore";
return;
}
if (nativeWindow != nullptr) {
RTC_LOG(LS_INFO) << "ANativeWindow_release ";
ANativeWindow_release(nativeWindow);
nativeWindow = nullptr;
}
nativeWindow = ANativeWindow_fromSurface(env, surface);
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
mFormat = format;
mDisplayWidth = displayWidth;
mDisplayHeight = displayHeight;
{
webrtc::MutexLock lock(&pc_mutex_);
allowDrawPicture = true;
}
RTC_LOG(LS_INFO)
<< "set native Window:" << "width:" << (mRight - mLeft) << ",height:" << (mBottom - mTop);
}
释放资源
if (nativeWindow != nullptr) {
ANativeWindow_release(nativeWindow);
nativeWindow = nullptr;
}