1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| FakeVideoCapturer::FakeVideoCapturer(jobject video_capturer) : running_(false), video_capturer(video_capturer), is_screen_cast(false), ticker(webrtc::EventTimerWrapper::Create()), thread(FakeVideoCapturer::Run, this, "FakeVideoCapturer") { JNIEnv *env = ATTACH_CURRENT_THREAD_IF_NEEDED(); video_capture_class = env->GetObjectClass(video_capturer); get_width_method = env->GetMethodID(video_capture_class, "getWidth", "()I"); get_height_method = env->GetMethodID(video_capture_class, "getHeight", "()I"); get_fps_method = env->GetMethodID(video_capture_class, "getFps", "()I"); capture_method = env->GetMethodID(video_capture_class, "capture", "()Lpackage/name/of/rtc4j/model/VideoFrame;"); width = env->CallIntMethod(video_capturer, get_width_method); previous_width = width; height = env->CallIntMethod(video_capturer, get_height_method); previous_height = height; fps = env->CallIntMethod(video_capturer, get_fps_method); static const cricket::VideoFormat formats[] = { {width, height, cricket::VideoFormat::FpsToInterval(fps), cricket::FOURCC_I420} }; SetSupportedFormats({&formats[0], &formats[arraysize(formats)]}); RTC_CHECK(ticker->StartTimer(true, rtc::kNumMillisecsPerSec / fps)); thread.Start(); thread.SetPriority(rtc::kHighPriority); decompress_handle = tjInitDecompress(); WEBRTC_LOG("Create fake video capturer, " + std::to_string(width) + ", " + std::to_string(height), INFO); }
FakeVideoCapturer::~FakeVideoCapturer() { thread.Stop(); SignalDestroyed(this); JNIEnv *env = ATTACH_CURRENT_THREAD_IF_NEEDED(); if (video_capture_class != nullptr) { env->DeleteLocalRef(video_capture_class); video_capture_class = nullptr; } if (decompress_handle) { if (tjDestroy(decompress_handle) != 0) { WEBRTC_LOG("Release decompress handle failed, reason is: " + std::string(tjGetErrorStr2(decompress_handle)), ERROR); } } WEBRTC_LOG("Free fake video capturer", INFO); }
bool FakeVideoCapturer::Run(void *obj) { static_cast<FakeVideoCapturer *>(obj)->CaptureFrame(); return true; }
void FakeVideoCapturer::CaptureFrame() { { rtc::CritScope cs(&lock_); if (running_) { int64_t t0 = rtc::TimeMicros(); JNIEnv *env = ATTACH_CURRENT_THREAD_IF_NEEDED(); jobject java_video_frame = env->CallObjectMethod(video_capturer, capture_method); if (java_video_frame == nullptr) { rtc::scoped_refptr<webrtc::I420Buffer> buffer = webrtc::I420Buffer::Create(previous_width, previous_height); webrtc::I420Buffer::SetBlack(buffer); OnFrame(webrtc::VideoFrame(buffer, (webrtc::VideoRotation) previous_rotation, t0), previous_width, previous_height); return; } jobject java_data_buffer = env->CallObjectMethod(java_video_frame, GET_VIDEO_FRAME_BUFFER_GETTER_METHOD()); auto data_buffer = (unsigned char *) env->GetDirectBufferAddress(java_data_buffer); auto length = (unsigned long) env->CallIntMethod(java_video_frame, GET_VIDEO_FRAME_LENGTH_GETTER_METHOD()); int rotation = env->CallIntMethod(java_video_frame, GET_VIDEO_FRAME_ROTATION_GETTER_METHOD()); int width; int height; tjDecompressHeader(decompress_handle, data_buffer, length, &width, &height); previous_width = width; previous_height = height; previous_rotation = rotation; rtc::scoped_refptr<webrtc::I420Buffer> buffer = webrtc::I420Buffer::Create(width, height, width % 32 == 0 ? width : width / 32 * 32 + 32, (width / 2) % 32 == 0 ? (width / 2) : (width / 2) / 32 * 32 + 32, (width / 2) % 32 == 0 ? (width / 2) : (width / 2) / 32 * 32 + 32); uint8_t *planes[] = {buffer->MutableDataY(), buffer->MutableDataU(), buffer->MutableDataV()}; int strides[] = {buffer->StrideY(), buffer->StrideU(), buffer->StrideV()}; tjDecompressToYUVPlanes(decompress_handle, data_buffer, length, planes, width, strides, height, TJFLAG_FASTDCT | TJFLAG_NOREALLOC); env->DeleteLocalRef(java_data_buffer); env->DeleteLocalRef(java_video_frame); OnFrame(webrtc::VideoFrame(buffer, (webrtc::VideoRotation) rotation, t0), width, height); } } ticker->Wait(WEBRTC_EVENT_INFINITE); }
cricket::CaptureState FakeVideoCapturer::Start( const cricket::VideoFormat &format) { running_ = true; SetCaptureState(cricket::CS_RUNNING); WEBRTC_LOG("Start fake video capturing", INFO); return cricket::CS_RUNNING; }
void FakeVideoCapturer::Stop() { running_ = false; SetCaptureState(cricket::CS_STOPPED); WEBRTC_LOG("Stop fake video capturing", INFO); }
bool FakeVideoCapturer::GetPreferredFourccs(std::vector<uint32_t> *fourccs) { fourccs->push_back(cricket::FOURCC_I420); return true; }
void FakeVideoCapturer::AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame> *sink, const rtc::VideoSinkWants &wants) { cricket::VideoCapturer::AddOrUpdateSink(sink, wants); }
void FakeVideoCapturer::RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame> *sink) { cricket::VideoCapturer::RemoveSink(sink); }
|