@@ -10,23 +10,21 @@ enum RTCVideoViewObjectFit {
10
10
RTCVideoViewObjectFitCover ,
11
11
}
12
12
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);
16
14
17
15
class RTCVideoRenderer {
18
16
MethodChannel _channel = WebRTC .methodChannel ();
19
17
int _textureId;
20
18
int _rotation = 0 ;
21
19
double _width = 0.0 , _height = 0.0 ;
22
20
bool _mirror;
21
+ double _aspectRatio = 1.0 ;
23
22
MediaStream _srcObject;
24
23
RTCVideoViewObjectFit _objectFit =
25
24
RTCVideoViewObjectFit .RTCVideoViewObjectFitContain ;
26
25
StreamSubscription <dynamic > _eventSubscription;
27
- VideoSizeChangeCallback onVideoSizeChanged;
28
- VideoRotationChangeCallback onVideoRotationChanged;
29
- dynamic onFirstFrameRendered;
26
+
27
+ VideoAspectRatioCallback onAspectRatioChanged;
30
28
31
29
initialize () async {
32
30
final Map <dynamic , dynamic > response =
@@ -45,6 +43,8 @@ class RTCVideoRenderer {
45
43
46
44
int get textureId => _textureId;
47
45
46
+ double get aspectRatio => _aspectRatio;
47
+
48
48
set mirror (bool mirror) {
49
49
_mirror = mirror;
50
50
}
@@ -77,83 +77,80 @@ class RTCVideoRenderer {
77
77
switch (map['event' ]) {
78
78
case 'didTextureChangeRotation' :
79
79
_rotation = map['rotation' ];
80
- if (this .onVideoRotationChanged != null )
81
- this .onVideoRotationChanged (_textureId, _rotation);
80
+ _updateAspectRatio ();
82
81
break ;
83
82
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 ();
88
86
break ;
89
87
case 'didFirstFrameRendered' :
90
- if ( this .onFirstFrameRendered != null ) this . onFirstFrameRendered ();
88
+ _updateAspectRatio ();
91
89
break ;
92
90
}
93
91
}
94
92
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
+
95
116
void errorListener (Object obj) {
96
117
final PlatformException e = obj;
97
118
throw e;
98
119
}
99
120
}
100
121
101
122
class RTCVideoView extends StatefulWidget {
102
- final RTCVideoRenderer renderer;
123
+ RTCVideoRenderer renderer;
103
124
RTCVideoView (this .renderer);
104
125
@override
105
126
_RTCVideoViewState createState () => new _RTCVideoViewState (renderer);
106
127
}
107
128
108
129
class _RTCVideoViewState extends State <RTCVideoView > {
109
- final RTCVideoRenderer renderer;
110
- var aspectRatio = 1.0 ;
130
+ RTCVideoRenderer renderer;
131
+ double _aspectRatio ;
111
132
112
133
_RTCVideoViewState (this .renderer);
113
134
114
135
@override
115
136
void initState () {
116
137
super .initState ();
117
138
_setCallbacks ();
139
+ _aspectRatio = renderer.aspectRatio;
118
140
}
119
141
120
142
@override
121
143
void deactivate () {
122
144
super.deactivate ();
123
- renderer.onVideoRotationChanged = null ;
124
- renderer.onVideoSizeChanged = null ;
125
- renderer.onFirstFrameRendered = null ;
145
+ renderer.onAspectRatioChanged = null ;
126
146
}
127
147
128
148
void _setCallbacks () {
129
- renderer.onVideoRotationChanged = (int textureId, int rotation) {
149
+ renderer.onAspectRatioChanged = (double aspectRatio ) {
130
150
setState (() {
131
- _updateContainerSize () ;
151
+ _aspectRatio = aspectRatio ;
132
152
});
133
153
};
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
- }
157
154
}
158
155
159
156
@override
@@ -162,7 +159,7 @@ class _RTCVideoViewState extends State<RTCVideoView> {
162
159
child: (this .renderer._textureId == null || this .renderer._srcObject == null )
163
160
? new Container ()
164
161
: new AspectRatio (
165
- aspectRatio: aspectRatio ,
162
+ aspectRatio: _aspectRatio ,
166
163
child: new Texture (textureId: this .renderer._textureId)));
167
164
388C
}
168
165
}
0 commit comments