|
1 | 1 | # Video extension plugins
|
2 |
| -## es.upv.paella.rtmpPlayer |
| 2 | +## es.upv.paella.mpegDashPlayer |
| 3 | + |
| 4 | +This plugin adds support for MPEG-Dash sources. To create a video extension plugin, you must to |
| 5 | +implement two classes: |
| 6 | + |
| 7 | +- The player class: extend a paella.VideoElementBase class or subclass. |
| 8 | +- The factory class: Create a class insode paella.videoFactories with two functions: |
| 9 | + - isStreamCompatible(streamData): Returns true if the video player can playback the source specified in streamData |
| 10 | + - getVideoObject(id,streamData,rect): This is the factory method that will create the video player instance. |
| 11 | + |
| 12 | +You can add third party dependencies to the "deps" folder, inside the plugin folder. In this case, the plugin |
| 13 | +includes a reference to the MPEG-Dash reference player: |
| 14 | + |
| 15 | + es.upv.paella.mpegDashPlayer |
| 16 | + deps |
| 17 | + dash.all.js |
| 18 | + mpeg-dash-player.js |
| 19 | + |
| 20 | + |
| 21 | +## Create the player class |
| 22 | + |
| 23 | +The MpegDashVideo plugin extends paella.Html5Video because MPEG-Dash uses a standard HTML video element. The |
| 24 | +class constructor receives the stream data as parameter |
| 25 | + |
| 26 | + Class ("paella.MpegDashVideo", paella.Html5Video,{ |
| 27 | + _posterFrame:null, |
| 28 | + _player:null, |
| 29 | + |
| 30 | + initialize:function(id,stream,left,top,width,height) { |
| 31 | + this.parent(id,stream,left,top,width,height); |
| 32 | + var This = this; |
| 33 | + }, |
| 34 | + |
| 35 | + |
| 36 | +## Load dependencies using require.js |
| 37 | + |
| 38 | + _loadDeps:function() { |
| 39 | + var defer = $.Deferred(); |
| 40 | + if (!window.$paella_mpd) { |
| 41 | + require(['resources/deps/dash.all.js'],function() { |
| 42 | + window.$paella_mpd = true; |
| 43 | + defer.resolve(); |
| 44 | + }); |
| 45 | + } |
| 46 | + else { |
| 47 | + defer.resolve(window.$paella_mpd); |
| 48 | + } |
| 49 | + return defer; |
| 50 | + }, |
| 51 | + |
| 52 | +## Load video |
| 53 | + |
| 54 | + load:function() { |
| 55 | + var This = this; |
| 56 | + var defer = $.Deferred(); |
| 57 | + var source = this._stream.sources.mpd; |
| 58 | + if (source && source.length>0) { |
| 59 | + source = source[0]; |
| 60 | + this._loadDeps() |
| 61 | + .then(function() { |
| 62 | + var context = new Dash.di.DashContext(); |
| 63 | + player = new MediaPlayer(context); |
| 64 | + dashContext = context; |
| 65 | + player.startup(); |
| 66 | + player.debug.setLogToBrowserConsole(false); |
| 67 | + player.attachView(This.video); |
| 68 | + player.setAutoPlay(false); |
| 69 | + player.setAutoSwitchQuality(false); |
| 70 | + This._player = player; |
| 71 | + |
| 72 | + player.addEventListener(MediaPlayer.events.STREAM_INITIALIZED,function(a,b) { |
| 73 | + bitrates = player.getBitrateInfoListFor("video"); |
| 74 | + This._deferredAction(function() { |
| 75 | + defer.resolve(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + player.attachSource(source.src); |
| 80 | + }); |
| 81 | + } |
| 82 | + else { |
| 83 | + defer.reject(new Error("Invalid source")); |
| 84 | + } |
| 85 | + |
| 86 | + return defer; |
| 87 | + }, |
| 88 | + |
| 89 | +## getQualities(): |
| 90 | + |
| 91 | +This function is used to get the available qualities. |
| 92 | + |
| 93 | + getQualities:function() { |
| 94 | + var This = this; |
| 95 | + var defer = $.Deferred(); |
| 96 | + this._deferredAction(function() { |
| 97 | + if (!This._qualities) { |
| 98 | + This._qualities = []; |
| 99 | + This._player |
| 100 | + .getBitrateInfoListFor("video") |
| 101 | + |
| 102 | + .sort(function(a,b) { |
| 103 | + return a.bitrate - b.bitrate; |
| 104 | + }) |
| 105 | + |
| 106 | + .forEach(function(item,index) { |
| 107 | + This._qualities.push(This._getQualityObject(item,index,bitrates)); |
| 108 | + }); |
| 109 | + } |
| 110 | + defer.resolve(This._qualities); |
| 111 | + }); |
| 112 | + return defer; |
| 113 | + }, |
| 114 | + |
| 115 | +## Get and set the current video quality |
| 116 | + |
| 117 | + setQuality:function(index) { |
| 118 | + var defer = $.Deferred(); |
| 119 | + var This = this; |
| 120 | + |
| 121 | + var currentQuality = this._player.getQualityFor("video"); |
| 122 | + if (index!=currentQuality) { |
| 123 | + this._player.removeEventListener(MediaPlayer.events.METRIC_CHANGED); |
| 124 | + this._player.addEventListener(MediaPlayer.events.METRIC_CHANGED,function(a,b) { |
| 125 | + if(a.type=="metricchanged" && a.data.stream=="video") { |
| 126 | + if (currentQuality!=This._player.getQualityFor("video")) { |
| 127 | + currentQuality = This._player.getQualityFor("video"); |
| 128 | + defer.resolve(); |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | + This._player.setQualityFor("video",index); |
| 133 | + } |
| 134 | + else { |
| 135 | + defer.resolve(); |
| 136 | + } |
| 137 | + |
| 138 | + return defer; |
| 139 | + }, |
| 140 | + |
| 141 | + getCurrentQuality:function() { |
| 142 | + var defer = $.Deferred(); |
| 143 | + var index = this._player.getQualityFor("video"); |
| 144 | + defer.resolve(this._getQualityObject(this._qualities[index],index,this._player.getBitrateInfoListFor("video"))); |
| 145 | + return defer; |
| 146 | + }, |
| 147 | + |
| 148 | + |
| 149 | +## Factory class |
| 150 | + |
| 151 | + Class ("paella.videoFactories.MpegDashVideoFactory", { |
| 152 | + isStreamCompatible:function(streamData) { |
| 153 | + try { |
| 154 | + for (var key in streamData.sources) { |
| 155 | + if (key=='mpd') return true; |
| 156 | + } |
| 157 | + } |
| 158 | + catch (e) {} |
| 159 | + return false; |
| 160 | + }, |
| 161 | + |
| 162 | + getVideoObject:function(id, streamData, rect) { |
| 163 | + return new paella.MpegDashVideo(id, streamData, rect.x, rect.y, rect.w, rect.h); |
| 164 | + } |
| 165 | + }); |
| 166 | + |
0 commit comments