8000 Merge branch 'master' of https://github.com/flutter-webrtc/flutter-we… · samhblee/flutter-webrtc@f0fc9a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0fc9a1

Browse files
committed
2 parents 1c9ced2 + 79ec0aa commit f0fc9a1

File tree

5 files changed

+137
-45
lines changed

5 files changed

+137
-45
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/GetUserMediaImpl.java

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class GetUserMediaImpl {
8888
private static final String PERMISSION_AUDIO = Manifest.permission.RECORD_AUDIO;
8989
private static final String PERMISSION_VIDEO = Manifest.permission.CAMERA;
9090
private static final String PERMISSION_SCREEN = "android.permission.MediaProjection";
91-
private static int CAPTURE_PERMISSION_REQUEST_CODE = 1;
91+
private static final int CAPTURE_PERMISSION_REQUEST_CODE = 1;
9292
private static final String GRANT_RESULTS = "GRANT_RESULT";
9393
private static final String PERMISSIONS = "PERMISSION";
9494
private static final String PROJECTION_DATA = "PROJECTION_DATA";
@@ -103,8 +103,6 @@ class GetUserMediaImpl {
103103
private final Context applicationContext;
104104

105105
static final int minAPILevel = Build.VERSION_CODES.LOLLIPOP;
106-
private MediaProjectionManager mProjectionManager = null;
107-
private static MediaProjection sMediaProjection = null;
108106

109107
final AudioSamplesInterceptor inputSamplesInterceptor = new AudioSamplesInterceptor();
110108
private OutputAudioSamplesInterceptor outputSamplesInterceptor = null;
@@ -351,23 +349,6 @@ private AudioTrack getUserAudio(ConstraintsMap constraints) {
351349
void getUserMedia(
352350
final ConstraintsMap constraints, final Result result, final MediaStream mediaStream) {
353351

354-
// TODO: change getUserMedia constraints format to support new syntax
355-
// constraint format seems changed, and there is no mandatory any more.
356-
// and has a new syntax/attrs to specify resolution
357-
// should change `parseConstraints()` according
358-
// see: https://www.w3.org/TR/mediacapture-streams/#idl-def-MediaTrackConstraints
359-
360-
ConstraintsMap videoConstraintsMap = null;
361-
ConstraintsMap videoConstraintsMandatory = null;
362-
363-
if (constraints.getType("video") == ObjectType.Map) {
364-
videoConstraintsMap = constraints.getMap("video");
365-
if (videoConstraintsMap.hasKey("mandatory")
366-
&& videoConstraintsMap.getType("mandatory") == ObjectType.Map) {
367-
videoConstraintsMandatory = videoConstraintsMap.getMap("mandatory");
368-
}
369-
}
370-
371352
final ArrayList<String> requestPermissions = new ArrayList<>();
372353

373354
if (constraints.hasKey("audio")) {
@@ -441,18 +422,6 @@ public void invoke(Object... args) {
441422

442423
void getDisplayMedia(
443424
final ConstraintsMap constraints, final Result result, final MediaStream mediaStream) {
444-
ConstraintsMap videoConstraintsMap = null;
445-
ConstraintsMap videoConstraintsMandatory = null;
446-
447-
if (constraints.getType("video") == ObjectType.Map) {
448-
videoConstraintsMap = constraints.getMap("video");
449-
if (videoConstraintsMap.hasKey("mandatory")
450-
&& videoConstraintsMap.getType("mandatory") == ObjectType.Map) {
451-
videoConstraintsMandatory = videoConstraintsMap.getMap("mandatory");
452-
}
453-
}
454-
455-
final ConstraintsMap videoConstraintsMandatory2 = videoConstraintsMandatory;
456425

457426
screenRequestPremissions(
458427
new ResultReceiver(new Handler(Looper.getMainLooper())) {
@@ -640,6 +609,34 @@ private void getUserMedia(
640609

641610
private boolean isFacing = true;
642611

612+
/**
613+
* @return Returns the integer at the key, or the `ideal` property if it is a map.
614+
*/
615+
@Nullable
616+
private Integer getConstrainInt(@Nullable ConstraintsMap constraintsMap, String key) {
617+
if(constraintsMap == null){
618+
return null;
619+
}
620+
621+
if (constraintsMap.getType(key) == ObjectType.Number) {
622+
try {
623+
return constraintsMap.getInt(key);
624+
} catch (Exception e) {
625+
// Could be a double instead
626+
return (int) Math.round(constraintsMap.getDouble(key));
627+
}
628+
}
629+
630+
if (constraintsMap.getType(key) == ObjectType.Map) {
631+
ConstraintsMap innerMap = constraintsMap.getMap(key);
632+
if (constraintsMap.getType("ideal") == ObjectType.Number) {
633+
return innerMap.getInt("ideal");
634+
}
635+
}
636+
637+
return null;
638+
}
639+
643640
private VideoTrack getUserVideo(ConstraintsMap constraints) {
644641
ConstraintsMap videoConstraintsMap = null;
645642
ConstraintsMap videoConstraintsMandatory = null;
@@ -651,6 +648,7 @@ private VideoTrack getUserVideo(ConstraintsMap constraints) {
651648
}
652649
}
653650

651+
654652
Log.i(TAG, "getUserMedia(video): " + videoConstraintsMap);
655653

656654
// NOTE: to support Camera2, the device should:
@@ -688,16 +686,25 @@ private VideoTrack getUserVideo(ConstraintsMap constraints) {
688686
surfaceTextureHelper, applicationContext, videoSource.getCapturerObserver());
689687

690688
VideoCapturerInfo info = new VideoCapturerInfo();
691-
info.width =
692-
videoConstraintsMandatory != null && videoConstraintsMandatory.hasKey("minWidth")
689+
690+
Integer videoWidth = getConstrainInt(videoConstraintsMap, "width");
691+
info.width = videoWidth != null
692+
? videoWidth
693+
: videoConstraintsMandatory != null && videoConstraintsMandatory.hasKey("minWidth")
693694
? videoConstraintsMandatory.getInt("minWidth")
694695
: DEFAULT_WIDTH;
695-
info.height =
696-
videoConstraintsMandatory != null && videoConstraintsMandatory.hasKey("minHeight")
696+
697+
Integer videoHeight = getConstrainInt(videoConstraintsMap, "height");
698+
info.height = videoHeight != null
699+
? videoHeight
700+
: videoConstraintsMandatory != null && videoConstraintsMandatory.hasKey("minHeight")
697701
? videoConstraintsMandatory.getInt("minHeight")
698702
: DEFAULT_HEIGHT;
699-
info.fps =
700-
videoConstraintsMandatory != null && videoConstraintsMandatory.hasKey("minFrameRate")
703+
704+
Integer videoFrameRate = getConstrainInt(videoConstraintsMap, "frameRate");
705+
info.fps = videoFrameRate != null
706+
? videoFrameRate
707+
: videoConstraintsMandatory != null && videoConstraintsMandatory.hasKey("minFrameRate")
701708
? videoConstraintsMandatory.getInt("minFrameRate")
702709
: DEFAULT_FPS;
703710
info.capturer = videoCapturer;

android/src/main/java/com/cloudwebrtc/webrtc/utils/ConstraintsMap.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,11 @@ public ConstraintsArray getArray(String name){
128128
public ArrayList<Object> getListArray(String name){
129129
return (ArrayList<Object>) mMap.get(name);
130130
}
131+
132+
@Override
133+
public String toString() {
134+
return "ConstraintsMap{" +
135+
"mMap=" + mMap +
136+
'}';
137+
}
131138
}

android/src/main/java/com/cloudwebrtc/webrtc/utils/MediaConstraintsUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public class MediaConstraintsUtils {
2424
public static MediaConstraints parseMediaConstraints(ConstraintsMap constraints) {
2525
MediaConstraints mediaConstraints = new MediaConstraints();
2626

27+
// TODO: change getUserMedia constraints format to support new syntax
28+
// constraint format seems changed, and there is no mandatory any more.
29+
// and has a new syntax/attrs to specify resolution
30+
// should change `parseConstraints()` according
31+
// see: https://www.w3.org/TR/mediacapture-streams/#idl-def-MediaTrackConstraints
2732
if (constraints.hasKey("mandatory")
2833
&& constraints.getType("mandatory") == ObjectType.Map) {
2934
parseConstraints(constraints.getMap("mandatory"),

common/cpp/src/flutter_media_stream.cc

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,36 +133,64 @@ std::string getSourceIdConstraint(const EncodableMap& mediaConstraints) {
133133
return "";
134134
}
135135

136+
EncodableValue getConstrainInt(const EncodableMap& constraints, const std::string& key) {
137+
EncodableValue value;
138+
auto it = constraints.find(EncodableValue(key));
139+
if(it != constraints.end()) {
140+
if(TypeIs<int>(it->second)) {
141+
return it->second;
142+
}
143+
144+
if(TypeIs<EncodableMap>(it->second)) {
145+
EncodableMap innerMap = GetValue<EncodableMap>(it->second);
146+
auto it2 = innerMap.find(EncodableValue("ideal"));
147+
if(it2 != constraints.end() && TypeIs<int>(it2->second)){
148+
return it2->second;
149+
}
150+
}
151+
}
152+
153+
return EncodableValue();
154+
}
155+
136156
void FlutterMediaStream::GetUserVideo(const EncodableMap& constraints,
137157
scoped_refptr<RTCMediaStream> stream,
138158
EncodableMap& params) {
139159
EncodableMap video_constraints;
140160
EncodableMap video_mandatory;
141161
auto it = constraints.find(EncodableValue("video"));
142162
if (it != constraints.end() && TypeIs<EncodableMap>(it->second)) {
143-
EncodableMap video_map = GetValue<EncodableMap>(it->second);
144-
if (video_map.find(EncodableValue("mandatory")) != video_map.end()) {
163+
video_constraints = GetValue<EncodableMap>(it->second);
164+
if (video_constraints.find(EncodableValue("mandatory")) != video_constraints.end()) {
145165
video_mandatory =
146-
GetValue<EncodableMap>(video_map.find(EncodableValue("mandatory"))->second);
166+
GetValue<EncodableMap>(video_constraints.find(EncodableValue("mandatory"))->second);
147167
}
148168
}
149169

150170
std::string facing_mode = getFacingMode(video_constraints);
151171
//bool isFacing = facing_mode == "" || facing_mode != "environment";
152172
std::string sourceId = getSourceIdConstraint(video_constraints);
153173

154-
EncodableValue widthValue = findEncodableValue(video_mandatory, "minWidth");
174+
EncodableValue widthValue = getConstrainInt(video_constraints, "width");
175+
176+
if (widthValue == EncodableValue())
177+
widthValue = findEncodableValue(video_mandatory, "minWidth");
155178

156179
if (widthValue == EncodableValue())
157180
widthValue = findEncodableValue(video_mandatory, "width");
158181

159-
EncodableValue heightValue = findEncodableValue(video_mandatory, "minHeight");
182+
EncodableValue heightValue = getConstrainInt(video_constraints, "height");
183+
184+
if(heightValue == EncodableValue())
185+
heightValue = findEncodableValue(video_mandatory, "minHeight");
160186

161187
if (heightValue == EncodableValue())
162188
heightValue = findEncodableValue(video_mandatory, "height");
163189

190+
EncodableValue fpsValue = getConstrainInt(video_constraints, "frameRate");
164191

165-
EncodableValue fpsValue = findEncodableValue(video_mandatory, "minFrameRate");
192+
if(fpsValue == EncodableValue())
193+
fpsValue = findEncodableValue(video_mandatory, "minFrameRate");
166194

167195
if (fpsValue == EncodableValue())
168196
fpsValue = findEncodableValue(video_mandatory, "frameRate");
@@ -342,5 +370,7 @@ void FlutterMediaStream::MediaStreamTrackSwitchCamera(
342370

343371
void FlutterMediaStream::MediaStreamTrackDispose(
344372
const std::string& track_id,
345-
std::unique_ptr<MethodResult<EncodableValue>> result) {}
373+
std::unique_ptr<MethodResult<EncodableValue>> result) {
374+
result->Success();
375+
}
346376
} // namespace flutter_webrtc_plugin

common/darwin/Classes/FlutterRTCMediaStream.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,34 @@ - (void)getUserMedia:(NSDictionary *)constraints
195195
successCallback(mediaStream);
196196
}
197197

198+
- (int)getConstrainInt:(NSDictionary *)constraints
199+
forKey:(NSString *)key {
200+
201+
if (![constraints isKindOfClass:[NSDictionary class]]) {
202+
return 0;
203+
}
204+
205+
id constraint = constraints[key];
206+
if ([constraint isKindOfClass:[NSNumber class]]) {
207+
return [constraint intValue];
208+
} else if ([constraint isKindOfClass:[NSString class]]) {
209+
int possibleValue = [constraint intValue];
210+
if (possibleValue != 0) {
211+
return possibleValue;
212+
}
213+
} else if ([constraint isKindOfClass:[NSDictionary class]]) {
214+
id idealConstraint = constraint[@"ideal"];
215+
if([idealConstraint isKindOfClass: [NSString class]]) {
216+
int possibleValue = [idealConstraint intValue];
217+
if(possibleValue != 0){
218+
return possibleValue;
219+
}
220+
}
221+
}
222+
223+
return 0;
224+
}
225+
198226
/**
199227
* Initializes a new {@link RTCVideoTrack} which satisfies specific constraints,
200228
* adds it to a specific {@link RTCMediaStream}, and reports success to a
@@ -301,6 +329,21 @@ - (void)getUserVideo:(NSDictionary *)constraints
301329
}
302330
}
303331

332+
int possibleWidth = [self getConstrainInt:videoConstraints forKey:@"width"];
333+
if(possibleWidth != 0){
334+
self._targetWidth = possibleWidth;
335+
}
336+
337+
int possibleHeight = [self getConstrainInt:videoConstraints forKey:@"height"];
338+
if(possibleHeight != 0){
339+
self._targetHeight = possibleHeight;
340+
}
341+
342+
int possibleFps = [self getConstrainInt:videoConstraints forKey:@"frameRate"];
343+
if(possibleFps != 0){
344+
self._targetFps = possibleFps;
345+
}
346+
304347
if (videoDevice) {
305348
RTCVideoSource *videoSource = [self.peerConnectionFactory videoSource];
306349
if (self.videoCapturer) {

0 commit comments

Comments
 (0)
0