/** * Test for core_media_renderer split_alternatives. */ public function test_split_alternatives() { // Single URL - identical moodle_url. $mp4 = 'http://example.org/test.mp4'; $result = core_media::split_alternatives($mp4, $w, $h); $this->assertEquals($mp4, $result[0]->out(false)); // Width and height weren't specified. $this->assertEquals(0, $w); $this->assertEquals(0, $h); // Two URLs - identical moodle_urls. $webm = 'http://example.org/test.webm'; $result = core_media::split_alternatives("{$mp4}#{$webm}", $w, $h); $this->assertEquals($mp4, $result[0]->out(false)); $this->assertEquals($webm, $result[1]->out(false)); // Two URLs plus dimensions. $size = 'd=400x280'; $result = core_media::split_alternatives("{$mp4}#{$webm}#{$size}", $w, $h); $this->assertEquals($mp4, $result[0]->out(false)); $this->assertEquals($webm, $result[1]->out(false)); $this->assertEquals(400, $w); $this->assertEquals(280, $h); // Two URLs plus legacy dimensions (use last one). $result = core_media::split_alternatives("{$mp4}?d=1x1#{$webm}?{$size}", $w, $h); $this->assertEquals($mp4, $result[0]->out(false)); $this->assertEquals($webm, $result[1]->out(false)); $this->assertEquals(400, $w); $this->assertEquals(280, $h); }
public function embed($urls, $name, $width, $height, $options) { // If link is turned off, return empty. if (!empty($options[core_media::OPTION_NO_LINK])) { return ''; } // Build up link content. $output = ''; foreach ($urls as $url) { $title = core_media::get_filename($url); $printlink = html_writer::link($url, $title, array('class' => 'mediafallbacklink')); if ($output) { // Where there are multiple available formats, there are fallback links // for all formats, separated by /. $output .= ' / '; } $output .= $printlink; } return $output; }
/** * Given a list of URLs, returns a reduced array containing only those URLs * which are supported by this player. (Empty if none.) * @param array $urls Array of moodle_url * @param array $options Options (same as will be passed to embed) * @return array Array of supported moodle_url */ public function list_supported_urls(array $urls, array $options = array()) { $extensions = $this->get_supported_extensions(); $result = array(); foreach ($urls as $url) { // If RTMP support is disabled, skip the URL. if (!get_config('filter_jwplayer', 'supportrtmp') && $url->get_scheme() === 'rtmp') { continue; } // If RTMP support is enabled, URL is supported. if (get_config('filter_jwplayer', 'supportrtmp') && $url->get_scheme() === 'rtmp') { $result[] = $url; continue; } if (in_array(core_media::get_extension($url), $extensions)) { // URL is matching one of enabled extensions. $result[] = $url; } } return $result; }
/** * Given a list of URLs, returns a reduced array containing only those URLs * which are supported by this player. * * This media player only supports local urls. * * @param array $urls Array of moodle_url * @param array $options Options (same as will be passed to embed) * @return array Array of supported moodle_url */ public function list_supported_urls(array $urls, array $options = array()) { $extensions = $this->get_supported_extensions(); $result = array(); foreach ($urls as $url) { try { $url->out_as_local_url(); } catch (coding_exception $e) { continue; } if (in_array(core_media::get_extension($url), $extensions)) { $result[] = $url; } } return $result; }
/** * Replace link with embedded content, if supported. * * @param array $matches * @return string */ private function callback(array $matches) { global $CFG, $PAGE; // Check if we ignore it. if (preg_match('/class="[^"]*nomediaplugin/i', $matches[0])) { return $matches[0]; } // Get name. $name = trim($matches[2]); if (empty($name) or strpos($name, 'http') === 0) { $name = ''; // Use default name. } // Split provided URL into alternatives. $urls = core_media::split_alternatives($matches[1], $width, $height); $options = array(); // Allow SWF (or not). if ($this->trusted) { $options[core_media::OPTION_TRUSTED] = true; } // We could test whether embed is possible using can_embed, but to save // time, let's just embed it with the 'fallback to blank' option which // does most of the same stuff anyhow. $options[core_media::OPTION_FALLBACK_TO_BLANK] = true; // NOTE: Options are not passed through from filter because the 'embed' // code does not recognise filter options (it's a different kind of // option-space) as it can be used in non-filter situations. $result = $this->mediarenderer->embed_alternatives($urls, $name, $width, $height, $options); // If something was embedded, return it, otherwise return original. if ($result !== '') { return $result; } else { return $matches[0]; } }
/** * Callback function for preg_replace_callback. * * @param array $matches Match array with 1 being the url and 2 being the innerText * @return string The url, either transformed or as is */ private function callback(array $matches) { // Guard against runtime errors try { // Get name $name = trim($matches[2]); if (empty($name) or strpos($name, 'http') === 0) { $name = ''; // Use default name } // Split provided URL into alternatives $urls = core_media::split_alternatives($matches[1], $width, $height); $result = $this->mediarenderer->embed_alternatives($urls, $name, $width, $height); // If something was embedded, return it, otherwise return original if ($result !== '') { return $result; } else { return $matches[0]; } } catch (Exception $e) { error_log('filter_viewerjs encountered an exception: ' . $e->getMessage(), 0); return $matches[0]; } }