J has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/78235


Change subject: Parse Ogg Opus files and enable playback if supported
......................................................................

Parse Ogg Opus files and enable playback if supported

Change-Id: I887fb13f9f1569da4a3a47ff6746f221aee9d4c3
---
M MwEmbedModules/EmbedPlayer/resources/mw.EmbedTypes.js
M TimedMediaHandler.php
M handlers/OggHandler/PEAR/File_Ogg/File/Ogg.php
A handlers/OggHandler/PEAR/File_Ogg/File/Ogg/Opus.php
4 files changed, 163 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TimedMediaHandler 
refs/changes/35/78235/1

diff --git a/MwEmbedModules/EmbedPlayer/resources/mw.EmbedTypes.js 
b/MwEmbedModules/EmbedPlayer/resources/mw.EmbedTypes.js
index c6bb988..5f1a6fc 100644
--- a/MwEmbedModules/EmbedPlayer/resources/mw.EmbedTypes.js
+++ b/MwEmbedModules/EmbedPlayer/resources/mw.EmbedTypes.js
@@ -40,6 +40,10 @@
        'audio/ogg; codecs="vorbis"',
        'application/ogg'
 ], 'Native' );
+// Native html5 players
+var opusNativePlayer = new mw.MediaPlayer( 'opusNative', [
+       'audio/ogg; codecs="opus"',
+], 'Native' );
 var h264NativePlayer = new mw.MediaPlayer( 'h264Native', [
        'video/h264',
        'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
@@ -233,6 +237,11 @@
                                        } else if ( this.supportedMimeType( 
'video/ogg' ) ) {
                                                this.mediaPlayers.addPlayer( 
oggNativePlayer );
                                        }
+
+                                       // Test for opus
+                                       if ( dummyvid.canPlayType( 'audio/ogg; 
codecs="opus"' ).replace(/maybe/, '') ) {
+                                               this.mediaPlayers.addPlayer( 
opusNativePlayer );
+                                       }
                                }
                        } catch ( e ) {
                                mw.log( 'could not run canPlayType ' + e );
diff --git a/TimedMediaHandler.php b/TimedMediaHandler.php
index a3e93b2..a4194f5 100644
--- a/TimedMediaHandler.php
+++ b/TimedMediaHandler.php
@@ -61,7 +61,7 @@
 
 // Set the supported ogg codecs:
 $wgMediaVideoTypes = array( 'Theora', 'VP8' );
-$wgMediaAudioTypes = array( 'Vorbis', 'Speex', 'FLAC' );
+$wgMediaAudioTypes = array( 'Vorbis', 'Speex', 'FLAC', 'Opus' );
 
 // Default skin for mwEmbed player
 $wgVideoPlayerSkinModule = 'mw.PlayerSkinKskin';
diff --git a/handlers/OggHandler/PEAR/File_Ogg/File/Ogg.php 
b/handlers/OggHandler/PEAR/File_Ogg/File/Ogg.php
index 1f556d6..15f4165 100644
--- a/handlers/OggHandler/PEAR/File_Ogg/File/Ogg.php
+++ b/handlers/OggHandler/PEAR/File_Ogg/File/Ogg.php
@@ -47,6 +47,10 @@
  * @access  public
  */
 define("OGG_STREAM_FLAC",       4);
+/**
+ * @access  public
+ */
+define("OGG_STREAM_OPUS",       5);
 
 /**
  * Capture pattern to determine if a file is an Ogg physical stream.
@@ -86,6 +90,11 @@
  */
 define("OGG_STREAM_CAPTURE_THEORA", "theora");
 /**
+ * Capture pattern for an Ogg Opus logical stream.
+ * @access  private
+ */
+define("OGG_STREAM_CAPTURE_OPUS",  "OpusHead");
+/**
  * Error thrown if the file location passed is nonexistant or unreadable.
  *
  * @access  private
@@ -118,6 +127,7 @@
 require_once("File/Ogg/Speex.php");
 require_once("File/Ogg/Theora.php");
 require_once("File/Ogg/Vorbis.php");
+require_once("File/Ogg/Opus.php");
 
 
 /**
@@ -477,6 +487,9 @@
             } elseif (preg_match("/" . OGG_STREAM_CAPTURE_THEORA . "/", 
$pattern)) {
                 $this->_streamList[$stream_serial]['stream_type'] = 
OGG_STREAM_THEORA;
                 $stream = new File_Ogg_Theora($stream_serial, $streamData, 
$this->_filePointer);
+            } elseif (preg_match("/" . OGG_STREAM_CAPTURE_OPUS . "/", 
$pattern)) {
+                $this->_streamList[$stream_serial]['stream_type'] = 
OGG_STREAM_OPUS;
+                $stream = new File_Ogg_Opus($stream_serial, $streamData, 
$this->_filePointer);
             } else {
                 $streamData['stream_type'] = "unknown";
                 $stream = false;
diff --git a/handlers/OggHandler/PEAR/File_Ogg/File/Ogg/Opus.php 
b/handlers/OggHandler/PEAR/File_Ogg/File/Ogg/Opus.php
new file mode 100644
index 0000000..d700ec6
--- /dev/null
+++ b/handlers/OggHandler/PEAR/File_Ogg/File/Ogg/Opus.php
@@ -0,0 +1,140 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// 
+----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams                        
 |
+// | Copyright (c) 2005-2007                                                   
 |
+// | David Grant <[email protected]>                                          
 |
+// | Tim Starling <[email protected]>                                    
 |
+// 
+----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or             
 |
+// | modify it under the terms of the GNU Lesser General Public                
 |
+// | License as published by the Free Software Foundation; either              
 |
+// | version 2.1 of the License, or (at your option) any later version.        
 |
+// |                                                                           
 |
+// | This library 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         
 |
+// | Lesser General Public License for more details.                           
 |
+// |                                                                           
 |
+// | You should have received a copy of the GNU Lesser General Public          
 |
+// | License along with this library; if not, write to the Free Software       
 |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
USA |
+// 
+----------------------------------------------------------------------------+
+
+require_once('File/Ogg/Media.php');
+
+define( 'OGG_OPUS_COMMENTS_PAGE_OFFSET', 2 );
+
+/**
+ * @author      David Grant <[email protected]>, Tim Starling 
<[email protected]>
+ * @category    File
+ * @copyright   David Grant <[email protected]>, Tim Starling 
<[email protected]>
+ * @license     http://www.gnu.org/copyleft/lesser.html GNU LGPL
+ * @link        http://pear.php.net/package/File_Ogg
+ * @link        http://www.opus-codec.org/
+ * @package     File_Ogg
+ * @version     CVS: $Id: Opus.php,v 1.10 2005/11/16 20:43:27 djg Exp $
+ */
+class File_Ogg_Opus extends File_Ogg_Media
+{
+    /**
+     * @access  private
+     */
+    function __construct($streamSerial, $streamData, $filePointer)
+    {
+        parent::__construct($streamSerial, $streamData, $filePointer);
+        $this->_decodeHeader();
+        //$this->_decodeCommentsHeader();
+
+        $endSec =  $this->getSecondsFromGranulePos( $this->_lastGranulePos );
+           $startSec = $this->getSecondsFromGranulePos( 
$this->_firstGranulePos );
+
+               //make sure the offset is worth taking into account oggz_chop 
related hack
+           if( $startSec > 1){
+            $this->_streamLength = $endSec - $startSec;
+            $this->_startOffset = $startSec;
+           }else{
+            $this->_streamLength = $endSec;
+           }
+        $this->_avgBitrate = $this->_streamLength ? ($this->_streamSize * 8) / 
$this->_streamLength : 0;
+    }
+
+       function getSecondsFromGranulePos( $granulePos ){
+               return (( '0x' . substr( $granulePos, 0, 8 ) ) * pow(2, 32)
+            + ( '0x' . substr( $granulePos, 8, 8 ) )
+            - $this->_header['pre_skip'])
+            / 48000;
+    }
+
+    /**
+     * Get a short string describing the type of the stream
+     * @return string
+     */
+    function getType()
+    {
+        return 'Opus';
+    }
+
+    /**
+     * Decode the stream header
+     * @access  private
+     */
+    function _decodeHeader()
+    {
+        fseek($this->_filePointer, 
$this->_streamData['pages'][0]['body_offset'], SEEK_SET);
+        // The first 8 characters should be "OpusHead".
+        if (fread($this->_filePointer, 8) != 'OpusHead')
+            throw new PEAR_Exception("Stream is undecodable due to a malformed 
header.", OGG_ERROR_UNDECODABLE);
+
+        $this->_header = File_Ogg::_readLittleEndian($this->_filePointer, 
array(
+            'opus_version'          => 8,
+            'nb_channels'           => 8,
+            'pre_skip'              => 16,
+            'audio_sample_rate'     => 32,
+            'output_gain'           => 16,
+            'channel_mapping'       => 8,
+        ));
+    }
+
+    /**
+     * Get an associative array containing header information about the stream
+     * @access  public
+     * @return  array
+     */
+    function getHeader() {
+        return $this->_header;
+    }
+
+    /**
+     * Number of channels used in this stream
+     *
+     * This function returns the number of channels used in this stream.  This
+     * can range from 1 to 255, but will likely be 2 (stereo) or 1 (mono).
+     *
+     * @access  public
+     * @return  int
+     * @see     File_Ogg_Vorbis::isMono()
+     * @see     File_Ogg_Vorbis::isStereo()
+     * @see     File_Ogg_Vorbis::isQuadrophonic()
+     */
+    function getChannels()
+    {
+        return ($this->header['nb_channels']);
+    }
+
+    function getSampleRate()
+    {
+        return 48000;
+    }
+
+    /**
+     * Decode the comments header
+     * @access private
+     */
+    function _decodeCommentsHeader()
+    {
+        $this->_decodeCommonHeader('OpusTags', OGG_OPUS_COMMENTS_PAGE_OFFSET);
+        $this->_decodeBareCommentsHeader();
+    }
+}
+?>

-- 
To view, visit https://gerrit.wikimedia.org/r/78235
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I887fb13f9f1569da4a3a47ff6746f221aee9d4c3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TimedMediaHandler
Gerrit-Branch: master
Gerrit-Owner: J <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to