| | 1 | ## common/gst.py |
| | 2 | ## |
| | 3 | ## Contributors for this file: |
| | 4 | ## - Sami Haahtinen <ressu@ressukka.net> |
| | 5 | ## |
| | 6 | ## Copyright (C) 2006 Sami Haahtinen <ressu@ressukka.net> |
| | 7 | ## |
| | 8 | ## This program is free software; you can redistribute it and/or modify |
| | 9 | ## it under the terms of the GNU General Public License as published |
| | 10 | ## by the Free Software Foundation; version 2 only. |
| | 11 | ## |
| | 12 | ## This program is distributed in the hope that it will be useful, |
| | 13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | 15 | ## GNU General Public License for more details. |
| | 16 | |
| | 17 | import gajim |
| | 18 | import os |
| | 19 | |
| | 20 | class GajimPlayer: |
| | 21 | player = None |
| | 22 | |
| | 23 | def __init__(self): |
| | 24 | # For now, lets default to GStreamer, winsound, and then try External player |
| | 25 | # FIXME: This really needs elegance! |
| | 26 | if not gajim.config.get('sounds_on'): |
| | 27 | raise NoSound |
| | 28 | |
| | 29 | try: |
| | 30 | self.player = GajimGstPlayer() |
| | 31 | except self.PlayerUnavailable: |
| | 32 | try: |
| | 33 | self.player = GajimWin32Player() |
| | 34 | except self.PlayerUnavailable: |
| | 35 | try: |
| | 36 | self.player = GajimExtPlayer() |
| | 37 | except self.PlayerUnavailable: |
| | 38 | self.player = None # Make sure that player is still set to None |
| | 39 | raise NoSound |
| | 40 | |
| | 41 | class NoSound(Exception): |
| | 42 | '''Notifies the system that we should be silent''' |
| | 43 | |
| | 44 | class BeepOnly(Exception): |
| | 45 | '''Notifies the system that we should just beep''' |
| | 46 | |
| | 47 | class PlayerUnavailable(Exception): |
| | 48 | '''Notifies that the player is unavailable''' |
| | 49 | |
| | 50 | def event_to_soundfile(self, event): |
| | 51 | '''Returns the full file path for sound file of an event''' |
| | 52 | # Check if we should play sound at all |
| | 53 | if self.player is None: |
| | 54 | raise NoSound |
| | 55 | |
| | 56 | # Get soundevent |
| | 57 | path_to_soundfile = gajim.config.get_per('soundevents', event, 'path') |
| | 58 | |
| | 59 | if path_to_soundfile is None or not os.path.exists(path_to_soundfile): |
| | 60 | raise NoSound |
| | 61 | |
| | 62 | return path_to_soundfile |
| | 63 | |
| | 64 | def play(self, soundfile): |
| | 65 | if soundfile == 'beep': |
| | 66 | beep() |
| | 67 | |
| | 68 | if self.player is None: |
| | 69 | return |
| | 70 | |
| | 71 | self.player.play_sound(soundfile) |
| | 72 | |
| | 73 | def beep(self): |
| | 74 | '''Play a single beep''' |
| | 75 | # FIXME: There must be a better way |
| | 76 | print "\a" |
| | 77 | |
| | 78 | # FIXME: This class hasn't gotten any testing! |
| | 79 | class GajimWin32Player: |
| | 80 | '''User native Win32 layer to play sounds''' |
| | 81 | |
| | 82 | def __init__(self): |
| | 83 | try: |
| | 84 | import winsound |
| | 85 | except ImportError: |
| | 86 | # gst module is not available... |
| | 87 | raise GajimPlayer.PlayerUnavailable, "Winsound library not available" |
| | 88 | |
| | 89 | def play_sound(self,soundfile): |
| | 90 | try: |
| | 91 | winsound.PlaySound(soundfile, winsound.SND_FILENAME|winsound.SND_ASYNC) |
| | 92 | except: |
| | 93 | pass |
| | 94 | |
| | 95 | class GajimExtPlayer: |
| | 96 | '''Use external application to play the sounds''' |
| | 97 | |
| | 98 | def __init__(self): |
| | 99 | self.player = gajim.config.get('soundplayer') |
| | 100 | if self.player == '': |
| | 101 | raise GajimPlayer.PlayerUnavailable, "External player not set" |
| | 102 | # FIXME: We should check if the player exists! |
| | 103 | |
| | 104 | def play_sound(self,soundfile): |
| | 105 | soundfile = soundfile.replace('"', '\\"') |
| | 106 | command = self.player + ' "' + soundfile + '" &' |
| | 107 | os.system(command) |
| | 108 | |
| | 109 | class GajimGstPlayer: |
| | 110 | '''Player that plays sound events through the GStreamer framework''' |
| | 111 | |
| | 112 | def __init__(self): |
| | 113 | try: |
| | 114 | import gst |
| | 115 | except ImportError: |
| | 116 | # gst module is not available... |
| | 117 | raise GajimPlayer.PlayerUnavailable, "Python Gstreamer modules not available" |
| | 118 | self.gst = gst |
| | 119 | self.GstBin = self.gst.parse_launch("playbin") |
| | 120 | |
| | 121 | # Create a sink for video, if someone decides to use a video file ;) |
| | 122 | fakesink = self.gst.element_factory_make("fakesink") |
| | 123 | self.GstBin.set_property("video-sink", fakesink) |
| | 124 | |
| | 125 | def play_sound(self,soundfile): |
| | 126 | self.GstBin.set_state(self.gst.STATE_NULL) |
| | 127 | |
| | 128 | # since playbin takes input in URI format, add file:// prefix |
| | 129 | sounduri = "file://" + os.path.abspath(soundfile) |
| | 130 | self.GstBin.set_property('uri', sounduri) |
| | 131 | |
| | 132 | self.GstBin.set_state(self.gst.STATE_PLAYING) |
| | 133 | |
| | 134 | def play_event(event): |
| | 135 | global Player |
| | 136 | try: |
| | 137 | try: |
| | 138 | soundfile = Player.event_to_soundfile(event) |
| | 139 | except NameError: |
| | 140 | Player = GajimPlayer() |
| | 141 | soundfile = Player.event_to_soundfile(event) |
| | 142 | except GajimPlayer.NoSound: |
| | 143 | # No sound. |
| | 144 | return |
| | 145 | |
| | 146 | # Ok, we should be clear to play the sound |
| | 147 | Player.play(soundfile) |