8000 [Dart] Fixed calculation error for aspectRatio in RTCVideoView. · Condelab/flutter-webrtc@80a7db1 · GitHub
[go: up one dir, main page]

10000
Skip to content

Commit 80a7db1

Browse files
committed
[Dart] Fixed calculation error for aspectRatio in RTCVideoView.
1 parent 11918fe commit 80a7db1

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

lib/rtc_video_view.dart

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,21 @@ enum RTCVideoViewObjectFit {
1010
RTCVideoViewObjectFitCover,
1111
}
1212

13-
typedef void VideoRotationChangeCallback(int textureId, int rotation);
14-
typedef void VideoSizeChangeCallback(
15-
int textureId, double width, double height);
13+
typedef void VideoAspectRatioCallback(double aspectRatio);
1614

1715
class RTCVideoRenderer {
1816
MethodChannel _channel = WebRTC.methodChannel();
1917
int _textureId;
2018
int _rotation = 0;
2119
double _width = 0.0, _height = 0.0;
2220
bool _mirror;
21+
double _aspectRatio = 1.0;
2322
MediaStream _srcObject;
2423
RTCVideoViewObjectFit _objectFit =
2524
RTCVideoViewObjectFit.RTCVideoViewObjectFitContain;
2625
StreamSubscription<dynamic> _eventSubscription;
27-
VideoSizeChangeCallback onVideoSizeChanged;
28-
VideoRotationChangeCallback onVideoRotationChanged;
29-
dynamic onFirstFrameRendered;
26+
27+
VideoAspectRatioCallback onAspectRatioChanged;
3028

3129
initialize() async {
3230
final Map<dynamic, dynamic> response =
@@ -45,6 +43,8 @@ class RTCVideoRenderer {
4543

4644
int get textureId => _textureId;
4745

46+
double get aspectRatio => _aspectRatio;
47+
4848
set mirror(bool mirror) {
4949
_mirror = mirror;
5050
}
@@ -77,83 +77,80 @@ class RTCVideoRenderer {
7777
switch (map['event']) {
7878
case 'didTextureChangeRotation':
7979
_rotation = map['rotation'];
80-
if (this.onVideoRotationChanged != null)
81-
this.onVideoRotationChanged(_textureId, _rotation);
80+
_updateAspectRatio();
8281
break;
8382
case 'didTextureChangeVideoSize':
84-
_width = map['width'];
85-
_height = map['height'];
86-
if (this.onVideoSizeChanged != null)
87-
this.onVideoSizeChanged(_textureId, _width, _height);
83+
_width = 0.0 + map['width'];
84+
_height = 0.0 + map['height'];
85+
_updateAspectRatio();
8886
break;
8987
case 'didFirstFrameRendered':
90-
if (this.onFirstFrameRendered != null) this.onFirstFrameRendered();
88+
_updateAspectRatio();
9189
break;
9290
}
9391
}
9492

93+
void _updateAspectRatio() {
94+
double aspectRatio = _aspectRatio;
95+
double textureWidth = 0.0,
96+
textureHeight = 0.0;
97+
if (_rotation == 90 || _rotation == 270) {
98+
textureWidth = min(_width, _height);
99+
textureHeight = max(_width, _height);
100+
if (_height != 0.0) {
101+
aspectRatio = _width / _height;
102+
}
103+
} else {
104+
textureWidth = max(_width, _height);
105+
textureHeight = min(_width, _height);
106+
if (_height != 0.0) {
107+
aspectRatio = _width / _height;
108+
}
109+
}
110+
if(this.onAspectRatioChanged != null && _aspectRatio != aspectRatio){
111+
this.onAspectRatioChanged(aspectRatio);
112+
}
113+
_aspectRatio = aspectRatio;
114+
}
115+
95116
void errorListener(Object obj) {
96117
final PlatformException e = obj;
97118
throw e;
98119
}
99120
}
100121

101122
class RTCVideoView extends StatefulWidget {
102-
final RTCVideoRenderer renderer;
123+
RTCVideoRenderer renderer;
103124
RTCVideoView(this.renderer);
104125
@override
105126
_RTCVideoViewState createState() => new _RTCVideoViewState(renderer);
106127
}
107128

108129
class _RTCVideoViewState extends State<RTCVideoView> {
109-
final RTCVideoRenderer renderer;
110-
var aspectRatio = 1.0;
130+
RTCVideoRenderer renderer;
131+
double _aspectRatio;
111132

112133
_RTCVideoViewState(this.renderer);
113134

114135
@override
115136
void initState() {
116137
super.initState();
117138
_setCallbacks();
139+
_aspectRatio = renderer.aspectRatio;
118140
}
119141

120142
@override
121143
void deactivate() {
122144
super.deactivate();
123-
renderer.onVideoRotationChanged = null;
124-
renderer.onVideoSizeChanged = null;
125-
renderer.onFirstFrameRendered = null;
145+
renderer.onAspectRatioChanged = null;
126146
}
127147

128148
void _setCallbacks() {
129-
renderer.onVideoRotationChanged = (int textureId, int rotation) {
149+
renderer.onAspectRatioChanged = (double aspectRatio ){
130150
setState(() {
131-
_updateContainerSize();
151+
_aspectRatio = aspectRatio;
132152
});
133153
};
134-
renderer.onVideoSizeChanged = (int textureId, double width, double height) {
135-
setState(() {
136-
_updateContainerSize();
137-
});
138-
};
139-
renderer.onFirstFrameRendered = () {
140-
setState(() {
141-
_updateContainerSize();
142-
});
143-
};
144-
}
145-
146-
void _updateContainerSize() {
147-
double textureWidth = 0.0, textureHeight = 0.0;
148-
if (renderer.rotation == 90 || renderer.rotation == 270) {
149-
textureWidth = min(renderer.width, renderer.height);
150-
textureHeight = max(renderer.width, renderer.height);
151-
aspectRatio = textureWidth / textureHeight;
152-
} else {
153-
textureWidth = max(renderer.width, renderer.height);
154-
textureHeight = min(renderer.width, renderer.height);
155-
aspectRatio = textureWidth / textureHeight;
156-
}
157154
}
158155

159156
@override
@@ -162,7 +159,7 @@ class _RTCVideoViewState extends State<RTCVideoView> {
162159
child: (this.renderer._textureId == null || this.renderer._srcObject == null)
163160
? new Container()
164161
: new AspectRatio(
165-
aspectRatio: aspectRatio,
162+
aspectRatio: _aspectRatio,
166163
child: new Texture(textureId: this.renderer._textureId)));
167164 388C
}
168165
}

0 commit comments

Comments
 (0)
0