PlayerEngineCore.cs 8.22 KB
Newer Older
1 2 3
/***************************************************************************
 *  PlayerEngineCore.cs
 *
4 5
 *  Copyright (C) 2006 Novell, Inc.
 *  Written by Aaron Bockover <aaron@abock.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 ****************************************************************************/

/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),  
 *  to deal in the Software without restriction, including without limitation  
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,  
 *  and/or sell copies of the Software, and to permit persons to whom the  
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in 
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 *  DEALINGS IN THE SOFTWARE.
 */
 
using System;
30
using System.IO;
31
using System.Collections;
32
using System.Reflection;
33 34 35 36 37 38 39 40
using Mono.Unix;

using Banshee.MediaEngine;

namespace Banshee.Base
{
    public static class PlayerEngineCore
    {
41 42 43 44 45 46 47 48 49 50 51
        private const string RootEngineDir = ConfigureDefines.InstallDir + "Banshee.MediaEngine/";   
        
        private static ArrayList engines = new ArrayList();
        private static PlayerEngine active_engine;
        private static PlayerEngine default_engine;
        private static PlayerEngine pending_engine;

        public static event PlayerEngineEventHandler EventChanged;
        public static event PlayerEngineStateHandler StateChanged;

        private static void InstantiateEngines()
52
        {
53
            DirectoryInfo info = new DirectoryInfo(RootEngineDir);
54
            
55 56
            if(!info.Exists) {
                throw new IOException("Directory " + RootEngineDir + " does not exist");
57 58
            }
            
59 60 61 62
            string preferred_id = null;
            try {
                preferred_id = (string)Globals.Configuration.Get(GConfKeys.PlayerEngine);
            } catch {
63
            }
64 65 66 67 68 69 70 71 72 73 74 75 76
            
            foreach(DirectoryInfo sub_info in info.GetDirectories()) {
                DirectoryInfo directory_info = new DirectoryInfo(sub_info.FullName + "/");
                if(!directory_info.Exists) {
                    continue;
                }
                
                foreach(FileInfo file_info in directory_info.GetFiles("*.dll")) {
                    Assembly assembly = Assembly.LoadFrom(file_info.FullName);
                    foreach(Type type in assembly.GetTypes()) {
                        if(!type.IsSubclassOf(typeof(PlayerEngine))) {
                            continue;
                        }
77

Aaron Bockover's avatar
Aaron Bockover committed
78 79 80 81
                        try {
                            PlayerEngine engine = (PlayerEngine)Activator.CreateInstance(type);
                            engine.StateChanged += OnEngineStateChanged;
                            engine.EventChanged += OnEngineEventChanged;
82

Aaron Bockover's avatar
Aaron Bockover committed
83 84 85
                            if(engine.Id == preferred_id) {
                                default_engine = engine;
                            }
86

Aaron Bockover's avatar
Aaron Bockover committed
87 88 89 90
                            engines.Add(engine);
                        } catch(Exception e) {
                            LogCore.Instance.PushError("Could not load a PlayerEngine", e.ToString());
                        }
91 92
                    }
                }
93
            }
94 95 96
            
            if(default_engine == null && engines.Count > 0) {
                default_engine = engines[0] as PlayerEngine;
97 98
            }
            
99
            active_engine = default_engine;
100 101
            
            LogCore.Instance.PushDebug(Catalog.GetString("Default player engine"), active_engine.Name);
102 103
        }
        
104
        public static void Initialize()
105
        {
106 107 108 109
            try {
                InstantiateEngines();
                if(default_engine == null || active_engine == null || engines == null || engines.Count == 0) {
                    throw new ApplicationException("No player engines found");
110
                }
111 112 113 114
            } catch(Exception e) {
                Console.Error.WriteLine("Cannot load any PlayerEngine:\n\n{0}\n", e);
                System.Environment.Exit(1);
            }   
115
        }
116 117

        public static void Dispose()
118
        {
119 120
            foreach(PlayerEngine engine in engines) {
                engine.Dispose();
121
            }
122 123 124
        }

        private static void OnEngineStateChanged(object o, PlayerEngineStateArgs args)
125
        {
126
            if(o != active_engine) {
127 128 129
                return;
            }
            
130 131 132 133
            PlayerEngineStateHandler handler = StateChanged;
            if(handler != null) {
                handler(o, args);
            }
134
        }
135 136 137 138 139

        private static void OnEngineEventChanged(object o, PlayerEngineEventArgs args)
        {
            if(o != active_engine) {
                return;
140 141
            }
            
142 143 144
            PlayerEngineEventHandler handler = EventChanged;
            if(handler != null) {
                handler(o, args);
145 146 147
            }
        }
        
148 149
        public static void Open(TrackInfo track)
        {
Aaron Bockover's avatar
Aaron Bockover committed
150
            CheckPending();   
151 152 153 154 155
            active_engine.Open(track);
        }
        
        public static void Open(Uri uri)
        {
Aaron Bockover's avatar
Aaron Bockover committed
156
            CheckPending();
157 158 159 160 161
            active_engine.Open(uri);
        }
        
        public static void OpenPlay(TrackInfo track)
        {
162 163 164 165
            if(!track.CanPlay) {
                return;
            }
        
Aaron Bockover's avatar
Aaron Bockover committed
166
            CheckPending();
167 168 169 170 171 172 173
            active_engine.Open(track);
            active_engine.Play();
        }
        
        public static void Close()
        {
            active_engine.Reset();
174
            active_engine.Close();
175 176
        }
        
177 178 179 180 181 182 183 184 185
        public static void Play()
        {
            active_engine.Play();
        }
        
        public static void Pause()
        {
            active_engine.Pause();
        }
Aaron Bockover's avatar
Aaron Bockover committed
186 187 188 189 190 191 192 193 194 195 196 197

        private static void CheckPending()
        {
            if(pending_engine != null && pending_engine != active_engine) {
                if(active_engine.CurrentState == PlayerEngineState.Idle) {
                    active_engine.Close();
                }
                
                active_engine = pending_engine;
                pending_engine = null;
            } 
        }
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    
        public static TrackInfo CurrentTrack {
            get { return active_engine.CurrentTrack; }
        }
        
        public static Uri CurrentUri {
            get { return active_engine.CurrentUri; }
        }
        
        public static PlayerEngineState CurrentState {
            get { return active_engine.CurrentState; }
        }
        
        public static PlayerEngineState LastState {
            get { return active_engine.LastState; }
        }
        
        public static ushort Volume {
            get { return active_engine.Volume; }
            set { active_engine.Volume = value; }
        }
        
        public static uint Position {
            get { return active_engine.Position; }
            set { active_engine.Position = value; }
        }
        
Aaron Bockover's avatar
Aaron Bockover committed
225 226 227 228
        public static bool CanSeek {
            get { return active_engine.CanSeek; }
        }
        
229 230 231 232 233 234 235 236 237 238 239 240 241 242
        public static uint Length {
            get { return active_engine.Length; }
        }
    
        public static PlayerEngine ActiveEngine {
            get { return active_engine; }
            set { pending_engine = value; }
        }
        
        public static PlayerEngine DefaultEngine {
            get { return default_engine; }
            set { 
                default_engine = value;
                Globals.Configuration.Set(GConfKeys.PlayerEngine, value.Id);
243 244
            }
        }
245 246 247 248
        
        public static IEnumerable Engines {
            get { return engines; }
        }
249 250
    }
}