10

We can request a media stream to a screen or windows via navigator.mediaDevices.getDisplayMedia(). However, this immediately prompts the user to decide which kind of capturing to use. I need to check if the browser/platform even support screen capturing.

Of course, it is possible to check for 'getDisplayMedia' in navigator.mediaDevices, but this just tells us if the API is supported by the browser. In particular, on FF and Chrome on Android, the API is defined and I can call getDisplayMedia(), but it always immediately returns a NotAllowedError error (which is to be expected: according to caniuse, the mobile browsers do not yet support getDisplayMedia.)

Next, I tried checking navigator.mediaDevices.getSupportedConstraints(). However, my mobile FF returns the exact same object as my desktop FF. In particular, navigator.mediaDevices.getSupportedConstraints().mediaSource is true in both cases. Finally, the data returned by navigator.mediaDevices.enumerateDevices() does not help me either. I only get a device and group ID which I cannot interpret in any way (right?).

Is it possible to detect whether or not screen capture via getDisplayMedia is supported beforehand?

(Note: this Q&A seems fairly similar, but is about getUserMedia and is already quite old)

Lukas Kalbertodt
  • 55,166
  • 13
  • 173
  • 236
  • Are you using HTTPS? – str Nov 13 '19 at 18:00
  • @str Yes, I am. Also note that this question is not about "I can't use `getDisplayMedia` on mobile". That is fine for me. I just want to check *whether or not* it is supported. – Lukas Kalbertodt Nov 13 '19 at 18:01
  • Doesn't getDisplayMedia return a promise? – aksappy Nov 13 '19 at 18:30
  • @aksappy Yes. Why? I don't see how that would help me. Either it resolves immediately to an error (on mobile) or the user get prompted by the browser (desktop, if supported). – Lukas Kalbertodt Nov 13 '19 at 18:34
  • Well, that was more of a conversation starter :-) Have you looked at MediaRecorder? It solves your use case with a isTypeSupported method. – aksappy Nov 13 '19 at 19:05
  • @aksappy Ah ok, sorry, didn't understand that :D The `isTypeSupported` method can only check MIME types, right? So that wouldn't really help me detect whether or not `getDisplayMedia` is supported. Or am I missing something? – Lukas Kalbertodt Nov 13 '19 at 19:14

1 Answers1

6

Unfortunately, there's no direct way to feature-detect whether getDisplayMedia will work on those browsers.

All you can do today is browser-sniff the UA string to detect you're not on mobile, where support is lacking.¹

I've filed an issue on the spec based on your question, to see if getDisplayMedia is better left undefined when unsupported.


1. caniuse claims Opera Mobile has support, but this appears not so when I test it.

jib
  • 34,243
  • 11
  • 80
  • 138
  • 1
    Please upvote this chromium bug report to get this fixed https://bugs.chromium.org/p/chromium/issues/detail?id=1038244#c2 – jpodwys Jan 08 '20 at 21:04