Skip to content

Commit

Permalink
fix: mac render crash
Browse files Browse the repository at this point in the history
  • Loading branch information
LichKing-2234 committed Jun 3, 2021
1 parent 910d191 commit dee5ab9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/src/classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,18 @@ class AgoraImage {
int? y;

/// Width of the image on the broadcasting video.
int width;
int? width;

/// Height of the image on the broadcasting video.
int height;
int? height;

/// Constructs a [AgoraImage]
AgoraImage(
this.url,
this.width,
this.height, {
this.url, {
this.x,
this.y,
this.width,
this.height,
});

/// @nodoc
Expand Down
4 changes: 2 additions & 2 deletions lib/src/classes.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/src/rtc_device_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum _ApiTypeVideoDeviceManager {
kVDMStopVideoDeviceTest,
}

/// TODO(doc)
class RtcDeviceManager {
static const MethodChannel _audioMethodChannel =
MethodChannel('agora_rtc_audio_device_manager');
Expand Down
34 changes: 27 additions & 7 deletions lib/src/rtc_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2510,7 +2510,7 @@ class RtcEngine with RtcEngineInterface {

@override
Future<void> setScreenCaptureContentHint(VideoContentHint contentHint) {
if (kIsWeb || (Platform.isWindows || Platform.isMacOS)) {
if (!kIsWeb && (Platform.isWindows || Platform.isMacOS)) {
return _invokeMethod('callApi', {
'apiType': _ApiTypeEngine.kEngineSetScreenCaptureContentHint.index,
'params': jsonEncode({
Expand All @@ -2524,7 +2524,7 @@ class RtcEngine with RtcEngineInterface {
@override
Future<void> startScreenCaptureByDisplayId(int displayId,
Rectangle regionRect, ScreenCaptureParameters captureParams) {
if (kIsWeb || (Platform.isWindows || Platform.isMacOS)) {
if (!kIsWeb && (Platform.isWindows || Platform.isMacOS)) {
return _invokeMethod('callApi', {
'apiType': _ApiTypeEngine.kEngineStartScreenCaptureByDisplayId.index,
'params': jsonEncode({
Expand All @@ -2540,9 +2540,9 @@ class RtcEngine with RtcEngineInterface {
@override
Future<void> startScreenCaptureByScreenRect(Rectangle screenRect,
Rectangle regionRect, ScreenCaptureParameters captureParams) {
if (kIsWeb || (Platform.isWindows || Platform.isMacOS)) {
if (!kIsWeb && (Platform.isWindows || Platform.isMacOS)) {
return _invokeMethod('callApi', {
'apiType': _ApiTypeEngine.kEngineStartScreenCaptureByScreenRect.index,
'apiType': _ApiTypeEngine.kEngineStartScreenCapture.index,
'params': jsonEncode({
'screenRect': screenRect.toJson(),
'regionRect': regionRect.toJson(),
Expand All @@ -2556,7 +2556,7 @@ class RtcEngine with RtcEngineInterface {
@override
Future<void> startScreenCaptureByWindowId(int windowId, Rectangle regionRect,
ScreenCaptureParameters captureParams) {
if (kIsWeb || (Platform.isWindows || Platform.isMacOS)) {
if (!kIsWeb && (Platform.isWindows || Platform.isMacOS)) {
return _invokeMethod('callApi', {
'apiType': _ApiTypeEngine.kEngineStartScreenCaptureByWindowId.index,
'params': jsonEncode({
Expand All @@ -2583,7 +2583,7 @@ class RtcEngine with RtcEngineInterface {
@override
Future<void> updateScreenCaptureParameters(
ScreenCaptureParameters captureParams) {
if (kIsWeb || (Platform.isWindows || Platform.isMacOS)) {
if (!kIsWeb && (Platform.isWindows || Platform.isMacOS)) {
return _invokeMethod('callApi', {
'apiType': _ApiTypeEngine.kEngineUpdateScreenCaptureParameters.index,
'params': jsonEncode({
Expand All @@ -2596,7 +2596,7 @@ class RtcEngine with RtcEngineInterface {

@override
Future<void> updateScreenCaptureRegion(Rectangle regionRect) {
if (kIsWeb || (Platform.isWindows || Platform.isMacOS)) {
if (!kIsWeb && (Platform.isWindows || Platform.isMacOS)) {
return _invokeMethod('callApi', {
'apiType': _ApiTypeEngine.kEngineUpdateScreenCaptureRegion.index,
'params': jsonEncode({
Expand All @@ -2606,6 +2606,23 @@ class RtcEngine with RtcEngineInterface {
}
throw PlatformException(code: ErrorCode.NotSupported.toString());
}

@override
Future<void> startScreenCapture(
int? windowId, int captureFreq, Rect rect, int bitrate) {
if (kIsWeb || (Platform.isWindows || Platform.isMacOS)) {
return _invokeMethod('callApi', {
'apiType': _ApiTypeEngine.kEngineStartScreenCapture.index,
'params': jsonEncode({
'windowId': windowId,
'captureFreq': captureFreq,
'rect': rect.toJson(),
'bitrate': bitrate,
}),
});
}
throw PlatformException(code: ErrorCode.NotSupported.toString());
}
}

/// @nodoc
Expand Down Expand Up @@ -4619,4 +4636,7 @@ mixin RtcScreenSharingInterface {
Future<void> updateScreenCaptureRegion(Rectangle regionRect);

Future<void> stopScreenCapture();

Future<void> startScreenCapture(
int windowId, int captureFreq, Rect rect, int bitrate);
}
24 changes: 16 additions & 8 deletions macos/Classes/AgoraTextureViewFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ @interface TextureRenderer : NSObject <FlutterTexture>
@property(nonatomic, weak) NSObject<FlutterTextureRegistry> *textureRegistry;
@property(nonatomic, assign) int64_t textureId;
@property(nonatomic, strong) FlutterMethodChannel *channel;
@property(nonatomic) CVPixelBufferRef buffer;
@property(nonatomic) CVPixelBufferRef buffer_cache;
@property(nonatomic) CVPixelBufferRef buffer_temp;
@property(nonatomic) IrisRtcRenderer *renderer;
@property(nonatomic, strong) dispatch_semaphore_t lock;

- (instancetype)
initWithTextureRegistry:(NSObject<FlutterTextureRegistry> *)textureRegistry
Expand Down Expand Up @@ -48,10 +50,10 @@ void OnVideoFrameReceived(const IrisRtcVideoFrame &video_frame,
video_frame.y_buffer_length);
CVPixelBufferUnlockBaseAddress(buffer, 0);

CVPixelBufferRelease(renderer.buffer);
renderer.buffer = buffer;
CVBufferRetain(renderer.buffer);
CVPixelBufferRelease(buffer);
dispatch_semaphore_wait(renderer.lock, DISPATCH_TIME_FOREVER);
renderer.buffer_cache = buffer;
dispatch_semaphore_signal(renderer.lock);

[renderer.textureRegistry textureFrameAvailable:renderer.textureId];
}
}
Expand All @@ -73,14 +75,15 @@ @implementation TextureRenderer
self = [super init];
if (self) {
self.textureRegistry = textureRegistry;
self.renderer = renderer;
self.delegate = new ::RendererDelegate((__bridge void *)self);
self.textureId = [self.textureRegistry registerTexture:self];
self.channel = [FlutterMethodChannel
methodChannelWithName:
[NSString stringWithFormat:@"agora_rtc_engine/texture_render_%lld",
self.textureId]
binaryMessenger:messenger];
self.renderer = renderer;
self.lock = dispatch_semaphore_create(1);
self.delegate = new ::RendererDelegate((__bridge void *)self);
__weak typeof(self) weakSelf = self;
[self.channel setMethodCallHandler:^(FlutterMethodCall *_Nonnull call,
FlutterResult _Nonnull result) {
Expand Down Expand Up @@ -116,7 +119,12 @@ - (void)destroy {
}

- (CVPixelBufferRef)copyPixelBuffer {
return self.buffer;
dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
CVPixelBufferRelease(self.buffer_temp);
self.buffer_temp = self.buffer_cache;
CVBufferRetain(self.buffer_temp);
dispatch_semaphore_signal(self.lock);
return self.buffer_temp;
}

- (void)onTextureUnregistered:(NSObject<FlutterTexture> *)texture {
Expand Down

0 comments on commit dee5ab9

Please sign in to comment.