]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/sound_sdl.cpp
Fix for probably rare but potential refcount leak.
[wxWidgets.git] / src / unix / sound_sdl.cpp
index eb1e5637668a36c8ce50e6adf8637fb20174f37c..12edf71953ac54c616977e63087865122ae84fce 100644 (file)
@@ -5,7 +5,7 @@
 // Modified by:
 // Created:     2004/01/31
 // RCS-ID:      $Id$
-// Copyright:   (c) 2004, Vaclav Slavik
+// Copyright:   (c) 2004, Open Source Applications Foundation
 // Licence:    wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
@@ -18,7 +18,7 @@
 #pragma hdrstop
 #endif
 
-#if wxUSE_WAVE && wxUSE_LIBSDL
+#if wxUSE_SOUND && wxUSE_LIBSDL
 
 #include <SDL.h>
 
@@ -56,8 +56,7 @@ END_DECLARE_EVENT_TYPES()
     DECLARE_EVENT_TABLE_ENTRY(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, \
                               -1,                       \
                               -1,                       \
-                              (wxObjectEventFunction)   \
-                              (wxSoundBackendSDLNotificationFunction)& func, \
+                              (wxObjectEventFunction)  wxStaticCastEvent( wxSoundBackendSDLNotificationFunction, & func ), \
                               (wxObject *) NULL ),
 
 IMPLEMENT_DYNAMIC_CLASS(wxSoundBackendSDLNotification, wxEvtHandler)
@@ -75,20 +74,26 @@ class wxSoundBackendSDL : public wxSoundBackend
 public:
     wxSoundBackendSDL() 
         : m_initialized(false), m_playing(false), m_audioOpen(false),
-          m_evtHandler(NULL) {}
+          m_data(NULL), m_evtHandler(NULL) {}
     virtual ~wxSoundBackendSDL();
     
     wxString GetName() const { return _T("Simple DirectMedia Layer"); }
     int GetPriority() const { return 9; }
     bool IsAvailable() const;
     bool HasNativeAsyncPlayback() const { return true; }
-    bool Play(wxSoundData *data, unsigned flags);
+    bool Play(wxSoundData *data, unsigned flags,
+              volatile wxSoundPlaybackStatus *status);
 
     void FillAudioBuffer(Uint8 *stream, int len);
+    void FinishedPlayback();
+    
     void Stop();
     bool IsPlaying() const { return m_playing; }
     
 private:
+    bool OpenAudio();
+    void CloseAudio();
+    
     bool                        m_initialized;
     bool                        m_playing, m_audioOpen;
     // playback information:
@@ -110,7 +115,7 @@ private:
     {
         wxLogTrace(_T("sound"),
                    _T("received playback status change notification"));
-        m_backend->Stop();
+        m_backend->FinishedPlayback();
     }
     wxSoundBackendSDL *m_backend;
 
@@ -124,6 +129,7 @@ END_EVENT_TABLE()
 wxSoundBackendSDL::~wxSoundBackendSDL()
 {
     Stop();
+    CloseAudio();
     delete m_evtHandler;
 }
 
@@ -187,8 +193,62 @@ void wxSoundBackendSDL::FillAudioBuffer(Uint8 *stream, int len)
     }
 }
 
-bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
+void wxSoundBackendSDL::FinishedPlayback()
+{
+    if (!m_playing)
+        Stop();
+}
+
+bool wxSoundBackendSDL::OpenAudio()
 {
+    if (!m_audioOpen)
+    {
+        if (!m_evtHandler)
+            m_evtHandler = new wxSoundBackendSDLEvtHandler(this);
+        
+        m_spec.silence = 0;
+        m_spec.samples = 4096;
+        m_spec.size = 0;
+        m_spec.callback = wx_sdl_audio_callback;
+        m_spec.userdata = (void*)this;
+                
+        wxLogTrace(_T("sound"), _T("opening SDL audio..."));
+        if (SDL_OpenAudio(&m_spec, NULL) >= 0)
+        {
+#if wxUSE_LOG_DEBUG
+            char driver[256];
+            SDL_AudioDriverName(driver, 256);                    
+            wxLogTrace(_T("sound"), _T("opened audio, driver '%s'"),
+                       wxString(driver, wxConvLocal).c_str());
+#endif
+            m_audioOpen = true;
+            return true;
+        }
+        else
+        {
+            wxString err(SDL_GetError(), wxConvLocal);
+            wxLogError(_("Couldn't open audio: %s"), err.c_str());
+            return false;
+        }
+    }
+    return true;
+}
+
+void wxSoundBackendSDL::CloseAudio()
+{
+    if (m_audioOpen)
+    {
+        SDL_CloseAudio();
+        wxLogTrace(_T("sound"), _T("closed audio"));
+        m_audioOpen = false;
+    }
+}
+
+bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags,
+                             volatile wxSoundPlaybackStatus *WXUNUSED(status))
+{
+    Stop();
+    
     int format;
     if (data->m_bitsPerSample == 8)
         format = AUDIO_U8;
@@ -196,18 +256,10 @@ bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
         format = AUDIO_S16LSB;
     else
         return false;
-    
-    SDL_LockAudio();
-
-    if (!m_evtHandler)
-        m_evtHandler = new wxSoundBackendSDLEvtHandler(this);
-
-    data->IncRef();
 
     bool needsOpen = true;
     if (m_audioOpen)
     {
-        wxLogTrace(_T("sound"), _T("another sound playing, will be stopped"));
         if (format == m_spec.format &&
             m_spec.freq == (int)data->m_samplingRate &&
             m_spec.channels == data->m_channels)
@@ -216,62 +268,35 @@ bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
         }
         else
         {
-            SDL_CloseAudio();
-            m_audioOpen = false;
-            wxLogTrace(_T("sound"), _T("closed audio"));
+            CloseAudio();
         }
-        m_data->DecRef();
     }
-   
-    m_playing = true;
-    m_data = data;
-    m_pos = 0;
-    m_loop = (flags & wxSOUND_LOOP);
-    wxLogTrace(_T("sound"), _T("prepared sound for playback"));
-
-    bool status = true;
-
+    
     if (needsOpen)
     {
         m_spec.format = format;
         m_spec.freq = data->m_samplingRate;
         m_spec.channels = data->m_channels;
-        m_spec.silence = 0;
-        m_spec.samples = 4096;
-        m_spec.size = 0;
-        m_spec.callback = wx_sdl_audio_callback;
-        m_spec.userdata = (void*)this;
-                
-        wxLogTrace(_T("sound"), _T("opening SDL audio..."));
-        status = (SDL_OpenAudio(&m_spec, NULL) >= 0);
-        if (status)
-        {
-#if wxUSE_LOG_DEBUG
-            char driver[256];
-            SDL_AudioDriverName(driver, 256);                    
-            wxLogTrace(_T("sound"), _T("opened audio, driver '%s'"),
-                       wxString(driver, wxConvLocal).c_str());
-#endif
-            m_audioOpen = true;
-            SDL_PauseAudio(0);
-        }
-        else
-        {
-            wxString err(SDL_GetError(), wxConvLocal);
-            wxLogError(_("Couldn't open audio: %s"), err.c_str());
-        }
+        if (!OpenAudio())
+            return false;
     }
-
+    
+    SDL_LockAudio();
+    wxLogTrace(_T("sound"), _T("playing new sound"));
+    m_playing = true;
+    m_pos = 0;
+    m_loop = (flags & wxSOUND_LOOP);
+    m_data = data;
+    data->IncRef();
     SDL_UnlockAudio();
 
-    if (!status)
-        return false;
+    SDL_PauseAudio(0);
 
     // wait until playback finishes if called in sync mode:
     if (!(flags & wxSOUND_ASYNC))
     {
         wxLogTrace(_T("sound"), _T("waiting for sample to finish"));
-        while (m_playing)
+        while (m_playing && m_data == data)
         {
 #if wxUSE_THREADS
             // give the playback thread a chance to add event to pending
@@ -279,7 +304,7 @@ bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
             if (wxThread::IsMain())
                 wxMutexGuiLeave();
 #endif
-            wxUsleep(10);
+            wxMilliSleep(10);
 #if wxUSE_THREADS
             if (wxThread::IsMain())
                 wxMutexGuiEnter();
@@ -294,15 +319,13 @@ bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
 void wxSoundBackendSDL::Stop()
 {
     SDL_LockAudio();
-
-    if (m_audioOpen)
+    SDL_PauseAudio(1);
+    m_playing = false;
+    if (m_data)
     {
-        SDL_CloseAudio();
-        m_audioOpen = false;
-        wxLogTrace(_T("sound"), _T("closed audio"));
         m_data->DecRef();
+        m_data = NULL;
     }
-
     SDL_UnlockAudio();
 }
 
@@ -311,4 +334,4 @@ extern "C" wxSoundBackend *wxCreateSoundBackendSDL()
     return new wxSoundBackendSDL();
 }
 
-#endif // wxUSE_WAVE && wxUSE_LIBSDL
+#endif // wxUSE_SOUND && wxUSE_LIBSDL