Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Main class for plugin 'media_html5video'
  19   *
  20   * @package   media_html5video
  21   * @copyright 2016 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Player that creates HTML5 <video> tag.
  29   *
  30   * @package   media_html5video
  31   * @copyright 2016 Marina Glancy
  32   * @author 2011 The Open University
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class media_html5video_plugin extends core_media_player_native {
  36      public function embed($urls, $name, $width, $height, $options) {
  37  
  38          if (array_key_exists(core_media_manager::OPTION_ORIGINAL_TEXT, $options) &&
  39              preg_match('/^<(video|audio)\b/i', $options[core_media_manager::OPTION_ORIGINAL_TEXT], $matches)) {
  40              // We already had media tag, do nothing here.
  41              return $options[core_media_manager::OPTION_ORIGINAL_TEXT];
  42          }
  43  
  44          // Special handling to make videos play on Android devices pre 2.3.
  45          // Note: I tested and 2.3.3 (in emulator) works without, is 533.1 webkit.
  46          $oldandroid = core_useragent::is_webkit_android() &&
  47              !core_useragent::check_webkit_android_version('533.1');
  48  
  49          // Build array of source tags.
  50          $sources = array();
  51          foreach ($urls as $url) {
  52              $mimetype = core_media_manager::instance()->get_mimetype($url);
  53              $source = html_writer::empty_tag('source', array('src' => $url, 'type' => $mimetype));
  54              if ($mimetype === 'video/mp4') {
  55                  if ($oldandroid) {
  56                      // Old Android fails if you specify the type param.
  57                      $source = html_writer::empty_tag('source', array('src' => $url));
  58                  }
  59  
  60                  // Better add m4v as first source, it might be a bit more
  61                  // compatible with problematic browsers.
  62                  array_unshift($sources, $source);
  63              } else {
  64                  $sources[] = $source;
  65              }
  66          }
  67  
  68          $sources = implode("\n", $sources);
  69          $title = $this->get_name($name, $urls);
  70          // Escape title but prevent double escaping.
  71          $title = s(preg_replace(['/&amp;/', '/&gt;/', '/&lt;/'], ['&', '>', '<'], $title));
  72  
  73          self::pick_video_size($width, $height);
  74          if (!$height) {
  75              // Let browser choose height automatically.
  76              $size = "width=\"$width\"";
  77          } else {
  78              $size = "width=\"$width\" height=\"$height\"";
  79          }
  80  
  81          $sillyscript = '';
  82          $idtag = '';
  83          if ($oldandroid) {
  84              // Old Android does not support 'controls' option.
  85              $id = 'core_media_html5v_' . md5(time() . '_' . rand());
  86              $idtag = 'id="' . $id . '"';
  87              $sillyscript = <<<OET
  88  <script type="text/javascript">
  89  document.getElementById('$id').addEventListener('click', function() {
  90      this.play();
  91  }, false);
  92  </script>
  93  OET;
  94          }
  95  
  96          // We don't want fallback to another player because list_supported_urls() is already smart.
  97          // Otherwise we could end up with nested <video> tags. Fallback to link only.
  98          $fallback = self::LINKPLACEHOLDER;
  99          return <<<OET
 100  <span class="mediaplugin mediaplugin_html5video">
 101  <video $idtag controls="true" $size preload="metadata" title="$title">
 102      $sources
 103      $fallback
 104  </video>
 105  $sillyscript
 106  </span>
 107  OET;
 108      }
 109  
 110      public function get_supported_extensions() {
 111          global $CFG;
 112          require_once($CFG->libdir . '/filelib.php');
 113          return file_get_typegroup('extension', 'html_video');
 114      }
 115  
 116      public function list_supported_urls(array $urls, array $options = array()) {
 117          $extensions = $this->get_supported_extensions();
 118          $result = array();
 119          foreach ($urls as $url) {
 120              $ext = core_media_manager::instance()->get_extension($url);
 121              if (in_array('.' . $ext, $extensions) && core_useragent::supports_html5($ext)) {
 122                  // Unfortunately html5 video does not handle fallback properly.
 123                  // https://www.w3.org/Bugs/Public/show_bug.cgi?id=10975
 124                  // That means we need to do browser detect and not use html5 on
 125                  // browsers which do not support the given type, otherwise users
 126                  // will not even see the fallback link.
 127                  $result[] = $url;
 128              }
 129          }
 130          return $result;
 131      }
 132  
 133      /**
 134       * Utility function that sets width and height to defaults if not specified
 135       * as a parameter to the function (will be specified either if, (a) the calling
 136       * code passed it, or (b) the URL included it).
 137       * @param int $width Width passed to function (updated with final value)
 138       * @param int $height Height passed to function (updated with final value)
 139       */
 140      protected static function pick_video_size(&$width, &$height) {
 141          global $CFG;
 142          if (!$width) {
 143              $width = $CFG->media_default_width;
 144          }
 145      }
 146  
 147      /**
 148       * Default rank
 149       * @return int
 150       */
 151      public function get_rank() {
 152          return 50;
 153      }
 154  }