MDL-38158 core_media: Convert media players to new plugin type

AMOS BEGIN
  MOV [siteyoutube,core_media],[pluginname,media_youtube]
  MOV [siteyoutube_desc,core_media],[pluginname_help,media_youtube]
  MOV [sitevimeo,core_media],[pluginname,media_vimeo]
  MOV [sitevimeo_desc,core_media],[pluginname_help,media_vimeo]
  MOV [html5audio,core_media],[pluginname,media_html5audio]
  MOV [html5audio_desc,core_media],[pluginname_help,media_html5audio]
  MOV [html5video,core_media],[pluginname,media_html5video]
  MOV [html5video_desc,core_media],[pluginname_help,media_html5video]
  MOV [flashanimation,core_media],[pluginname,media_swf]
  MOV [flashanimation_desc,core_media],[pluginname_help,media_swf]
AMOS END
This commit is contained in:
Marina Glancy 2016-09-15 15:52:41 +08:00
parent 3c73b26c4b
commit fab11235d8
76 changed files with 3524 additions and 4406 deletions

View file

@ -0,0 +1,150 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Main class for plugin 'media_html5video'
*
* @package media_html5video
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Player that creates HTML5 <video> tag.
*
* @package media_html5video
* @copyright 2016 Marina Glancy
* @author 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class media_html5video_plugin extends core_media_player_native {
public function embed($urls, $name, $width, $height, $options) {
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)) {
// We already had media tag, do nothing here.
return $options[core_media_manager::OPTION_ORIGINAL_TEXT];
}
// Special handling to make videos play on Android devices pre 2.3.
// Note: I tested and 2.3.3 (in emulator) works without, is 533.1 webkit.
$oldandroid = core_useragent::is_webkit_android() &&
!core_useragent::check_webkit_android_version('533.1');
// Build array of source tags.
$sources = array();
foreach ($urls as $url) {
$mimetype = core_media_manager::instance()->get_mimetype($url);
$source = html_writer::empty_tag('source', array('src' => $url, 'type' => $mimetype));
if ($mimetype === 'video/mp4') {
if ($oldandroid) {
// Old Android fails if you specify the type param.
$source = html_writer::empty_tag('source', array('src' => $url));
}
// Better add m4v as first source, it might be a bit more
// compatible with problematic browsers.
array_unshift($sources, $source);
} else {
$sources[] = $source;
}
}
$sources = implode("\n", $sources);
$title = $this->get_name($name, $urls);
// Escape title but prevent double escaping.
$title = s(preg_replace(['/&amp;/', '/&gt;/', '/&lt;/'], ['&', '>', '<'], $title));
self::pick_video_size($width, $height);
if (!$height) {
// Let browser choose height automatically.
$size = "width=\"$width\"";
} else {
$size = "width=\"$width\" height=\"$height\"";
}
$sillyscript = '';
$idtag = '';
if ($oldandroid) {
// Old Android does not support 'controls' option.
$id = 'core_media_html5v_' . md5(time() . '_' . rand());
$idtag = 'id="' . $id . '"';
$sillyscript = <<<OET
<script type="text/javascript">
document.getElementById('$id').addEventListener('click', function() {
this.play();
}, false);
</script>
OET;
}
$fallback = core_media_player::PLACEHOLDER;
return <<<OET
<span class="mediaplugin mediaplugin_html5video">
<video $idtag controls="true" $size preload="metadata" title="$title">
$sources
$fallback
</video>
$sillyscript
</span>
OET;
}
public function get_supported_extensions() {
return file_get_typegroup('extension', 'html_video');
}
public function list_supported_urls(array $urls, array $options = array()) {
$extensions = $this->get_supported_extensions();
$result = array();
foreach ($urls as $url) {
$ext = core_media_manager::instance()->get_extension($url);
if (in_array('.' . $ext, $extensions) && core_useragent::supports_html5($ext)) {
// Unfortunately html5 video does not handle fallback properly.
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=10975
// That means we need to do browser detect and not use html5 on
// browsers which do not support the given type, otherwise users
// will not even see the fallback link.
$result[] = $url;
}
}
return $result;
}
/**
* Utility function that sets width and height to defaults if not specified
* as a parameter to the function (will be specified either if, (a) the calling
* code passed it, or (b) the URL included it).
* @param int $width Width passed to function (updated with final value)
* @param int $height Height passed to function (updated with final value)
*/
protected static function pick_video_size(&$width, &$height) {
global $CFG;
if (!$width) {
$width = $CFG->media_default_width;
}
}
/**
* Default rank
* @return int
*/
public function get_rank() {
return 50;
}
}

View file

@ -0,0 +1,26 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for plugin 'media_html5video'
*
* @package media_html5video
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'HTML 5 video';
$string['pluginname_help'] = 'Video files played by browser native video player. (Format support depends on browser.)';

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 B

View file

@ -0,0 +1,131 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test classes for handling embedded media.
*
* @package media_html5video
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Test script for media embedding.
*
* @package media_html5video
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class media_html5video_testcase extends advanced_testcase {
/**
* Pre-test setup. Preserves $CFG.
*/
public function setUp() {
parent::setUp();
// Reset $CFG and $SERVER.
$this->resetAfterTest();
// Consistent initial setup: all players disabled.
\core\plugininfo\media::set_enabled_plugins('html5video');
// Pretend to be using Firefox browser (must support ogg for tests to work).
core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 ');
}
/**
* Test that plugin is returned as enabled media plugin.
*/
public function test_is_installed() {
$sortorder = \core\plugininfo\media::get_enabled_plugins();
$this->assertEquals(['html5video' => 'html5video'], $sortorder);
}
/**
* Test method get_supported_extensions()
*/
public function test_supported_extensions() {
$nativeextensions = file_get_typegroup('extension', 'html_video');
// Make sure that the list of extensions from the setting is exactly the same as html_video group.
$player = new media_html5video_plugin();
$this->assertEmpty(array_diff($player->get_supported_extensions(), $nativeextensions));
$this->assertEmpty(array_diff($nativeextensions, $player->get_supported_extensions()));
}
/**
* Test embedding without media filter (for example for displaying file resorce).
*/
public function test_embed_url() {
global $CFG;
$url = new moodle_url('http://example.org/1.webm');
$manager = core_media_manager::instance();
$embedoptions = array(
core_media_manager::OPTION_TRUSTED => true,
core_media_manager::OPTION_BLOCK => true,
);
$this->assertTrue($manager->can_embed_url($url, $embedoptions));
$content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
$this->assertRegExp('~mediaplugin_html5video~', $content);
$this->assertRegExp('~</video>~', $content);
$this->assertRegExp('~title="Test &amp; file"~', $content);
$this->assertRegExp('~width="' . $CFG->media_default_width . '"~', $content);
$this->assertNotRegExp('~height=~', $content); // Allow to set automatic height.
// Repeat sending the specific size to the manager.
$content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions);
$this->assertRegExp('~width="123" height="50"~', $content);
}
/**
* Test that mediaplugin filter replaces a link to the supported file with media tag.
*
* filter_mediaplugin is enabled by default.
*/
public function test_embed_link() {
global $CFG;
$url = new moodle_url('http://example.org/some_filename.mp4');
$text = html_writer::link($url, 'Watch this one');
$content = format_text($text, FORMAT_HTML);
$this->assertRegExp('~mediaplugin_html5video~', $content);
$this->assertRegExp('~</video>~', $content);
$this->assertRegExp('~title="Watch this one"~', $content);
$this->assertNotRegExp('~<track\b~i', $content);
$this->assertRegExp('~width="' . $CFG->media_default_width . '"~', $content);
}
/**
* Test that mediaplugin filter does not work on <video> tags.
*/
public function test_embed_media() {
$url = new moodle_url('http://example.org/some_filename.mp4');
$trackurl = new moodle_url('http://example.org/some_filename.vtt');
$text = '<video controls="true"><source src="'.$url.'"/><source src="somethinginvalid"/>' .
'<track src="'.$trackurl.'">Unsupported text</video>';
$content = format_text($text, FORMAT_HTML);
$this->assertNotRegExp('~mediaplugin_html5video~', $content);
$this->assertEquals(clean_text($text, FORMAT_HTML), $content);
}
}

View file

@ -0,0 +1,29 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version details
*
* @package media_html5video
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016052300; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2016051900; // Requires this Moodle version
$plugin->component = 'media_html5video'; // Full name of the plugin (used for diagnostics).