aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/neomedia/notify/AudioNotifierServiceImpl.java
blob: 71a5b2eb1f8202fcc7ede505ae6714c6dac849d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.neomedia.notify;

import java.net.*;
import java.util.*;
import java.beans.*;

import net.java.sip.communicator.impl.neomedia.*;
import net.java.sip.communicator.impl.neomedia.device.*;
import net.java.sip.communicator.service.audionotifier.*;

/**
 * The implementation of the AudioNotifierService.
 *
 * @author Yana Stamcheva
 */
public class AudioNotifierServiceImpl
    implements AudioNotifierService,
               PropertyChangeListener
{
    /**
     * Map of differents audio clips.
     */
    private static final Map<String, SCAudioClipImpl> audioClips =
        new HashMap<String, SCAudioClipImpl>();

    /**
     * If the sound is currently disabled.
     */
    private boolean isMute;

    /**
     * Device config to look for notify device.
     */
    private DeviceConfiguration deviceConfiguration;

    /**
     * Creates audio notify service.
     * @param deviceConfiguration the device configuration.
     */
    public AudioNotifierServiceImpl(DeviceConfiguration deviceConfiguration)
    {
        this.deviceConfiguration = deviceConfiguration;
        deviceConfiguration.addPropertyChangeListener(this);
    }

    /**
     * Creates an SCAudioClip from the given URI and adds it to the list of
     * available audio-s.
     *
     * @param uri the path where the audio file could be found
     * @return a newly created <tt>SCAudioClip</tt> from <tt>uri</tt>
     */
    public SCAudioClipImpl createAudio(String uri)
    {
        SCAudioClipImpl audioClip;

        synchronized (audioClips)
        {
            if(audioClips.containsKey(uri))
            {
                audioClip = audioClips.get(uri);
            }
            else
            {
                URL url =
                    NeomediaActivator.getResources().getSoundURLForPath(uri);

                if (url == null)
                {
                    // Not found by the class loader. Perhaps it's a local file.
                    try
                    {
                        url = new URL(uri);
                    }
                    catch (MalformedURLException e)
                    {
                        //logger.error("The given uri could not be parsed.", e);
                        return null;
                    }
                }

                try
                {
                    if(getDeviceConfiguration().getAudioSystem().equals(
                        DeviceConfiguration.AUDIO_SYSTEM_JAVASOUND))
                    {
                        audioClip = new JMFAudioClipImpl(url, this);
                    }
                    else if(getDeviceConfiguration().getAudioSystem().equals(
                        DeviceConfiguration.AUDIO_SYSTEM_PORTAUDIO))
                    {
                        audioClip = new PortAudioClipImpl(url, this);
                    }
                    else
                        return null;
                }
                catch (Throwable e)
                {
                    // Cannot create audio to play
                    return null;
                }

                audioClips.put(uri, audioClip);
            }
        }

        return audioClip;
    }

    /**
     * Removes the given audio from the list of available audio clips.
     *
     * @param audioClip the audio to destroy
     */
    public void destroyAudio(SCAudioClip audioClip)
    {
        synchronized (audioClips)
        {
            audioClips.remove(audioClip);
        }
    }

    /**
     * Enables or disables the sound in the application. If FALSE, we try to
     * restore all looping sounds if any.
     *
     * @param isMute when TRUE disables the sound, otherwise enables the sound.
     */
    public void setMute(boolean isMute)
    {
        this.isMute = isMute;

        for (SCAudioClipImpl audioClip : audioClips.values())
        {
            if (isMute)
            {
                audioClip.internalStop();
            }
            else if (audioClip.isLooping())
            {
                audioClip.playInLoop(audioClip.getLoopInterval());
            }
        }
    }

    /**
     * Returns TRUE if the sound is currently disabled, FALSE otherwise.
     * @return TRUE if the sound is currently disabled, FALSE otherwise
     */
    public boolean isMute()
    {
        return isMute;
    }

    /**
     * The device configuration.
     *
     * @return the deviceConfiguration
     */
    public DeviceConfiguration getDeviceConfiguration()
    {
        return deviceConfiguration;
    }

    /**
     * Listens for changes in notify device
     * @param evt the event that notify device has changed.
     */
    public void propertyChange(PropertyChangeEvent evt)
    {
        if(evt.getPropertyName().equals(
                    DeviceConfiguration.AUDIO_NOTIFY_DEVICE))
        {
            audioClips.clear();
        }
    }
}