|
| 1 | +// |
| 2 | +// Created by 张涛 on 2020/4/26. |
| 3 | +// |
| 4 | + |
| 5 | +#include "ExtensionVideoFilter.hpp" |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +namespace agora { |
| 9 | + namespace extension { |
| 10 | + |
| 11 | + ExtensionVideoFilter::ExtensionVideoFilter(agora_refptr<YUVImageProcessor> processor):threadPool_(1) { |
| 12 | + YUVProcessor = processor; |
| 13 | + } |
| 14 | + |
| 15 | + ExtensionVideoFilter::~ExtensionVideoFilter() { |
| 16 | + YUVProcessor->releaseOpenGL(); |
| 17 | + } |
| 18 | + |
| 19 | + //Set the process mode between the SDK and the video plug-in |
| 20 | + //If set ProcessMode to Sync mode, Agora SDK and video extension will pass data through adaptVideoFrame |
| 21 | + //If set ProcessMode to Async mode, Agora SDK and video extension will pass data through pendVideoFrame and deliverVideoFrame |
| 22 | + //If set independent_thread to false, all callbacks sent by Agora SDK are performed on the internal video processing thread |
| 23 | + //If set independent_thread to true, all callbacks sent by Agora SDK are performed on a separate thread |
| 24 | + void ExtensionVideoFilter::getProcessMode(ProcessMode& mode, bool& independent_thread) { |
| 25 | + mode = ProcessMode::kSync; |
| 26 | + independent_thread = false; |
| 27 | + mode_ = mode; |
| 28 | + } |
| 29 | + |
| 30 | + //Set the type and format of the video data to be processed |
| 31 | + void ExtensionVideoFilter::getVideoFormatWanted(rtc::VideoFrameData::Type& type, |
| 32 | + rtc::RawPixelBuffer::Format& format) { |
| 33 | + type = rtc::VideoFrameData::Type::kRawPixels; |
| 34 | + format = rtc::RawPixelBuffer::Format::kI420; |
| 35 | + } |
| 36 | + |
| 37 | + int ExtensionVideoFilter::start(agora::agora_refptr<Control> control) { |
| 38 | + printf("ExtensionVideoFilter::start\n"); |
| 39 | + if (!YUVProcessor) { |
| 40 | + return -1; |
| 41 | + } |
| 42 | + if (control) { |
| 43 | + control_ = control; |
| 44 | + YUVProcessor->setExtensionControl(control); |
| 45 | + } |
| 46 | + if (mode_ == ProcessMode::kAsync){ |
| 47 | + invoker_id = threadPool_.RegisterInvoker("thread_videofilter"); |
| 48 | + auto res = threadPool_.PostTaskWithRes(invoker_id, [yuvProcessor=YUVProcessor] { |
| 49 | + return yuvProcessor->initOpenGL(); |
| 50 | + }); |
| 51 | + isInitOpenGL = res.get(); |
| 52 | + } else { |
| 53 | + isInitOpenGL = YUVProcessor->initOpenGL(); |
| 54 | + } |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + int ExtensionVideoFilter::stop() { |
| 59 | + printf("ExtensionVideoFilter::stop\n"); |
| 60 | + if (YUVProcessor) { |
| 61 | + YUVProcessor->releaseOpenGL(); |
| 62 | + isInitOpenGL = false; |
| 63 | + } |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + rtc::IExtensionVideoFilter::ProcessResult ExtensionVideoFilter::pendVideoFrame(agora::agora_refptr<rtc::IVideoFrame> frame) { |
| 68 | + if (!frame || !isInitOpenGL) { |
| 69 | + return kBypass; |
| 70 | + } |
| 71 | + |
| 72 | + bool isAsyncMode = (mode_ == ProcessMode::kAsync); |
| 73 | + if (isAsyncMode && YUVProcessor && control_ && invoker_id >= 0) { |
| 74 | + threadPool_.PostTask(invoker_id, [videoFrame=frame, processor=YUVProcessor, control=control_] { |
| 75 | + rtc::VideoFrameData srcData; |
| 76 | + videoFrame->getVideoFrameData(srcData); |
| 77 | + processor->processFrame(srcData); |
| 78 | + // In asynchronous mode (mode is set to Async), |
| 79 | + // the plug-in needs to call this method to return the processed video frame to the SDK. |
| 80 | + control->deliverVideoFrame(videoFrame); |
| 81 | + }); |
| 82 | + return kSuccess; |
| 83 | + } |
| 84 | + return kBypass; |
| 85 | + } |
| 86 | + |
| 87 | + rtc::IExtensionVideoFilter::ProcessResult ExtensionVideoFilter::adaptVideoFrame(agora::agora_refptr<rtc::IVideoFrame> src, |
| 88 | + agora::agora_refptr<rtc::IVideoFrame>& dst) { |
| 89 | + if (!isInitOpenGL) { |
| 90 | + return kBypass; |
| 91 | + } |
| 92 | + bool isSyncMode = (mode_ == ProcessMode::kSync); |
| 93 | + if (isSyncMode && YUVProcessor) { |
| 94 | + rtc::VideoFrameData srcData; |
| 95 | + src->getVideoFrameData(srcData); |
| 96 | + YUVProcessor->processFrame(srcData); |
| 97 | + dst = src; |
| 98 | + return kSuccess; |
| 99 | + } |
| 100 | + return kBypass; |
| 101 | + } |
| 102 | + |
| 103 | + // When the app developer calls RtcEngine.setExtensionProperty, |
| 104 | + // Agora SDK will call this method to set video plug-in properties |
| 105 | + int ExtensionVideoFilter::setProperty(const char *key, const void *buf, |
| 106 | + size_t buf_size) { |
| 107 | + printf("setProperty %s %s\n", key, buf); |
| 108 | + std::string stringParameter((char*)buf); |
| 109 | + YUVProcessor->setParameters(stringParameter); |
| 110 | + return 0; |
| 111 | + } |
| 112 | + |
| 113 | + // When the app developer calls getExtensionProperty, |
| 114 | + // Agora SDK will call this method to get the properties of the video plug-in |
| 115 | + int ExtensionVideoFilter::getProperty(const char *key, void *buf, size_t buf_size) { |
| 116 | + return -1; |
| 117 | + } |
| 118 | + |
| 119 | + void ExtensionVideoFilter::setEnabled(bool enable) { |
| 120 | + } |
| 121 | + |
| 122 | + bool ExtensionVideoFilter::isEnabled() { |
| 123 | + return true; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments