代码之家  ›  专栏  ›  技术社区  ›  nilphilus

确定浏览器支持的DRM系统

  •  10
  • nilphilus  · 技术社区  · 9 年前

    我试图找出如何确定哪个DRM系统浏览器正在使用。事实上,只有chrome说它在使用com.widevine。其中IE&Safari(Win)在“requestMediaKeySystemAccess”上抛出错误,firefox甚至没有尝试说它使用了“com.adobe”。访问'=]

     function isKeySystemSupported(keySystem) {
    
        var dfd = Q.defer();
        console.log('check: ', keySystem);
        navigator.requestMediaKeySystemAccess(keySystem, [{contentType: 'video/webm; codecs="vp9"'}]).then(function() {
            dfd.resolve(true);
        }, function() { dfd.resolve(false); } );
    
        return dfd.promise;
    }
    

    是否有任何解决方案,如Modernizer或类似的解决方案来获得我应该使用的密钥系统?

    3 回复  |  直到 9 年前
        1
  •  13
  •   deepelement    6 年前

    有几个网站提供这种支票,比如 dash-player.com/browser-capabilities/ 在仔细研究了它是如何完成的之后,可以使用类似的方法:

    // EME Check
    var keySystems = {
      widevine: ['com.widevine.alpha'],
      playready: ['com.microsoft.playready', 'com.youtube.playready'],
      clearkey: ['webkit-org.w3.clearkey', 'org.w3.clearkey'],
      primetime: ['com.adobe.primetime', 'com.adobe.access'],
      fairplay: ['com.apple.fairplay']
    };
    var keySystemsCount = (function () {
      var count = 0;
      for (keysys in keySystems) {
        if (keySystems.hasOwnProperty(keysys)) {
          count += keySystems[keysys].length;
        }
      }
      return count;
    })();
    
    var testVideoElement = document.createElement('video');
    var supportedSystems = [];
    var unsupportedSystems = [];
    
    var supportsEncryptedMediaExtension = function () {
      if (!testVideoElement.mediaKeys) {
        if (window.navigator.requestMediaKeySystemAccess) {
          if (typeof window.navigator.requestMediaKeySystemAccess === 'function') {
            console.log('found default EME');
            hasEME = true;
            var isKeySystemSupported = function (keySystem) {
              var config = [{initDataTypes: ['cenc']}];
              if (window.navigator.requestMediaKeySystemAccess) {
                window.navigator.requestMediaKeySystemAccess(keySystem, config).then(function (keySystemAccess) {
                  supportedSystems.push(keySystem);
                }).catch(function () {
                  unsupportedSystems.push(keySystem);
                });
              }
            };
            var keysys, dummy, i;
            for (keysys in keySystems) {
              if (keySystems.hasOwnProperty(keysys)) {
                for (dummy in keySystems[keysys]) {
                  isKeySystemSupported(keySystems[keysys][dummy]);
                }
              }
            }
          }
        } else if (window.MSMediaKeys) {
          if (typeof window.MSMediaKeys === 'function') {
            console.log('found MS-EME');
            hasEME = true;
            var keysys, dummy, i;
            for (keysys in keySystems) {
              if (keySystems.hasOwnProperty(keysys)) {
                for (dummy in keySystems[keysys]) {
                  if (MSMediaKeys.isTypeSupported(keySystems[keysys][dummy])) {
                    supportedSystems.push(keySystems[keysys][dummy]);
                  } else {
                    unsupportedSystems.push(keySystems[keysys][dummy]);
                  }
                }
              }
            }
          }
        } else if (testVideoElement.webkitGenerateKeyRequest) {
          if (typeof testVideoElement.webkitGenerateKeyRequest === 'function') {
            console.log('found WebKit EME');
            hasEME = true;
            var keysys, dummy, i;
            for (keysys in keySystems) {
              if (keySystems.hasOwnProperty(keysys)) {
                for (dummy in keySystems[keysys]) {
                  if (testVideoElement.canPlayType('video/mp4', keySystems[keysys][dummy])) {
                    supportedSystems.push(keySystems[keysys][dummy]);
                  } else {
                    unsupportedSystems.push(keySystems[keysys][dummy]);
                  }
                }
              }
            }
          }
        } else {
          console.log('no supported EME implementation found');
          hasEME = false;
        }
      }
    }

    只需运行 支持加密媒体扩展() 支持的系统 将填充所需信息。

    请注意 config 对象应扩展为包括特定媒体的特定编解码器声明。仅仅检测关键系统是不够的,因为编解码器支持有时取决于Guest OS依赖性。

    var config = [{
         "initDataTypes": ["cenc"],
         "audioCapabilities": [{
              "contentType": "audio/mp4;codecs=\"mp4a.40.2\""
         }],
         "videoCapabilities": [{
              "contentType": "video/mp4;codecs=\"avc1.42E01E\""
         }]
    }];
    
        2
  •  5
  •   Jason Kim    7 年前

    除了这里列出的信息之外,我想提到的是,在Chrome中,您是否正在使用 https 是否会影响 navigator.requestMediaKeySystemAccess 作用

    在可能正在运行的开发环境中 http , 导航器.requestMediaKeySystemAccess 将返回 undefined 对于 而相同的代码将在 火狐 .

    在您的prod环境中 https(https) , 导航器.requestMediaKeySystemAccess 将返回一个函数 二者都 在Chrome和Firefox中。

        3
  •  4
  •   Whome    3 年前

    我不得不付出 videoCapabilities 标志来实现这一点。

    function testEME() {
      // https://shaka-player-demo.appspot.com/support.html
      var keySysConfig = [{
        "initDataTypes": ["cenc"]
          //,"persistentState": "required"  // don't use or MacSafari "not supported"
          //,"persistentState": "required", "distinctiveIdentifier": "required"
          //,"audioCapabilities": [{
          //  "contentType": "audio/mp4;codecs=\"mp4a.40.2\""
          //}]
          ,"videoCapabilities": [{
        "contentType": "video/mp4;codecs=\"avc1.4D401E\"" // avc1.42E01E = ConstrainedLevel3, 4D401E=MainLevel3
        //,"robustness": "3000"
          }]
      }];           
            
      var keySystems = {
        playready: ['com.microsoft.playready.recommendation', 'com.microsoft.playready'
          , 'com.microsoft.playready.hardware', 'com.youtube.playready'],
        clearkey: ['webkit-org.w3.clearkey', 'org.w3.clearkey'],
        widevine: ['com.widevine.alpha'],
        primetime: ['com.adobe.primetime', 'com.adobe.access'],
        fairplay: ['com.apple.fairplay','com.apple.fps'
          , 'com.apple.fps.1_0', 'com.apple.fps.2_0', 'com.apple.fps.3_0']
      };
                
      for(keyArr in keySystems) {
        for(forItemIdx in keySystems[keyArr]) {
          let keySys = keySystems[keyArr][forItemIdx];
          try {
             navigator.requestMediaKeySystemAccess(keySys, keySysConfig).
             then(function(mediaKeySystemAccess) {
             //let mkConfig = mediaKeySystemAccess.getConfiguration();
             //let sFlags = "persistentState="+mkConfig.persistentState 
             //  + ", distinctiveIdentifier="+mkConfig.distinctiveIdentifier; 
            console.log(keySys + " supported"); //+" ("+sFlags+")");
          }).catch(function(ex) {
             console.log(keySys+" not supported (" + ex.name+" "+ex.message+")." );
          });
          } catch (ex) {
            console.log(keySys+" not supported (" + ex.name+" "+ex.message+").." );
          }
        }
      }
    }