/**
  * @dataProvider user_agents_providers
  */
 public function test_useragent_edge($useragent, $tests)
 {
     // Setup the core_useragent instance.
     core_useragent::instance(true, $useragent);
     // Edge Tests.
     if (isset($tests['is_edge']) && $tests['is_edge']) {
         $this->assertTrue(core_useragent::is_edge());
     } else {
         $this->assertFalse(core_useragent::is_edge());
     }
     $versions = array('12' => false);
     if (isset($tests['check_edge_version'])) {
         // The test provider has overwritten some of the above checks.
         // Must use the '+' operator, because array_merge will incorrectly rewrite the array keys for integer-based indexes.
         $versions = $tests['check_edge_version'] + $versions;
     }
     foreach ($versions as $version => $result) {
         $this->assertEquals($result, core_useragent::check_edge_version($version), "Version incorrectly determined for Edge version '{$version}'");
     }
 }
Exemple #2
0
 public function list_supported_urls(array $urls, array $options = array())
 {
     $extensions = $this->get_supported_extensions();
     $result = array();
     foreach ($urls as $url) {
         $ext = core_media::get_extension($url);
         if (in_array($ext, $extensions)) {
             if ($ext === 'ogg' || $ext === 'oga') {
                 // Formats .ogg and .oga are not supported in IE, Edge, or Safari.
                 if (core_useragent::is_ie() || core_useragent::is_edge() || core_useragent::is_safari()) {
                     continue;
                 }
             } else {
                 // Formats .aac, .mp3, and .m4a are not supported in Opera.
                 if (core_useragent::is_opera()) {
                     continue;
                 }
                 // Formats .mp3 and .m4a were not reliably supported in Firefox before 27.
                 // https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats
                 // has the details. .aac is still not supported.
                 if (core_useragent::is_firefox() && ($ext === 'aac' || !core_useragent::check_firefox_version(27))) {
                     continue;
                 }
             }
             // Old Android versions (pre 2.3.3) 'support' audio tag but no codecs.
             if (core_useragent::is_webkit_android() && !core_useragent::is_webkit_android('533.1')) {
                 continue;
             }
             $result[] = $url;
         }
     }
     return $result;
 }
Exemple #3
0
 /**
  * Generates code required to embed the player.
  *
  * @param moodle_url[] $urls
  * @param string $name
  * @param int $width
  * @param int $height
  * @param array $options
  * @return string
  */
 public function embed($urls, $name, $width, $height, $options)
 {
     global $CFG;
     require_once $CFG->libdir . '/filelib.php';
     $sources = array();
     $mediamanager = core_media_manager::instance();
     $datasetup = [];
     $text = null;
     $isaudio = null;
     $hastracks = false;
     $hasposter = false;
     if (array_key_exists(core_media_manager::OPTION_ORIGINAL_TEXT, $options) && preg_match('/^<(video|audio)\\b/i', $options[core_media_manager::OPTION_ORIGINAL_TEXT], $matches)) {
         // Original text already had media tag - get some data from it.
         $text = $options[core_media_manager::OPTION_ORIGINAL_TEXT];
         $isaudio = strtolower($matches[1]) === 'audio';
         $hastracks = preg_match('/<track\\b/i', $text);
         $hasposter = self::get_attribute($text, 'poster') !== null;
     }
     // Currently Flash in VideoJS does not support responsive layout. If Flash is enabled try to guess
     // if HTML5 player will be engaged for the user and then set it to responsive.
     $responsive = get_config('media_videojs', 'useflash') && !$this->youtube ? null : true;
     // Build list of source tags.
     foreach ($urls as $url) {
         $extension = $mediamanager->get_extension($url);
         $mimetype = $mediamanager->get_mimetype($url);
         if ($mimetype === 'video/quicktime' && (core_useragent::is_chrome() || core_useragent::is_edge())) {
             // Fix for VideoJS/Chrome bug https://github.com/videojs/video.js/issues/423 .
             $mimetype = 'video/mp4';
         }
         $source = html_writer::empty_tag('source', array('src' => $url, 'type' => $mimetype));
         $sources[] = $source;
         if ($isaudio === null) {
             $isaudio = in_array('.' . $extension, file_get_typegroup('extension', 'audio'));
         }
         if ($responsive === null) {
             $responsive = core_useragent::supports_html5($extension);
         }
     }
     $sources = implode("\n", $sources);
     // Find the title, prevent double escaping.
     $title = $this->get_name($name, $urls);
     $title = preg_replace(['/&amp;/', '/&gt;/', '/&lt;/'], ['&', '>', '<'], $title);
     // Ensure JS is loaded. This will also load language strings and populate $this->language with the current language.
     $this->load_amd_module();
     if ($this->youtube) {
         $this->load_amd_module('Youtube');
         $datasetup[] = '"techOrder": ["youtube"]';
         $datasetup[] = '"sources": [{"type": "video/youtube", "src":"' . $urls[0] . '"}]';
         $sources = '';
         // Do not specify <source> tags - it may confuse browser.
         $isaudio = false;
         // Just in case.
     }
     // Add a language.
     if ($this->language) {
         $datasetup[] = '"language": "' . $this->language . '"';
     }
     // Set responsive option.
     if ($responsive) {
         $datasetup[] = '"fluid": true';
     }
     if ($isaudio && !$hastracks) {
         // We don't need a full screen toggle for the audios (except when tracks are present).
         $datasetup[] = '"controlBar": {"fullscreenToggle": false}';
     }
     if ($isaudio && !$height && !$hastracks && !$hasposter) {
         // Hide poster area for audios without tracks or poster.
         // See discussion on https://github.com/videojs/video.js/issues/2777 .
         // Maybe TODO: if there are only chapter tracks we still don't need poster area.
         $datasetup[] = '"aspectRatio": "1:0"';
     }
     // Attributes for the video/audio tag.
     static $playercounter = 1;
     $attributes = ['data-setup' => '{' . join(', ', $datasetup) . '}', 'id' => 'id_videojs_' . $playercounter++, 'class' => get_config('media_videojs', $isaudio ? 'audiocssclass' : 'videocssclass')];
     if (!$responsive) {
         // Note we ignore limitsize setting if not responsive.
         parent::pick_video_size($width, $height);
         $attributes += ['width' => $width] + ($height ? ['height' => $height] : []);
     }
     if ($text !== null) {
         // Original text already had media tag - add necessary attributes and replace sources
         // with the supported URLs only.
         if (($class = self::get_attribute($text, 'class')) !== null) {
             $attributes['class'] .= ' ' . $class;
         }
         $text = self::remove_attributes($text, ['id', 'width', 'height', 'class']);
         if (self::get_attribute($text, 'title') === null) {
             $attributes['title'] = $title;
         }
         $text = self::add_attributes($text, $attributes);
         $text = self::replace_sources($text, $sources);
     } else {
         // Create <video> or <audio> tag with necessary attributes and all sources.
         // We don't want fallback to another player because list_supported_urls() is already smart.
         // Otherwise we could end up with nested <audio> or <video> tags. Fallback to link only.
         $attributes += ['preload' => 'auto', 'controls' => 'true', 'title' => $title];
         $text = html_writer::tag($isaudio ? 'audio' : 'video', $sources . self::LINKPLACEHOLDER, $attributes);
     }
     // Limit the width of the video if width is specified.
     // We do not do it in the width attributes of the video because it does not work well
     // together with responsive behavior.
     if ($responsive) {
         self::pick_video_size($width, $height);
         if ($width) {
             $text = html_writer::div($text, null, ['style' => 'max-width:' . $width . 'px;']);
         }
     }
     return html_writer::div($text, 'mediaplugin mediaplugin_videojs');
 }