]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/audioqueue.c
vlc.desktop: add missing --started-from-file option (fixes #8839)
[vlc] / modules / audio_output / audioqueue.c
index 2510e4cdc8847ed8aee8ce832df1c7d5c69d3917..c945a85bc7084d81cccc6d31eb9545ed093cc78b 100644 (file)
@@ -1,59 +1,56 @@
 /*****************************************************************************
- * audioqueue.c : AudioQueue audio output plugin for vlc
- *****************************************************************************
- * Copyright (C) 2010-2013 VLC authors and VideoLAN
+ * Copyright (C) 2000-2013 VLC authors and VideoLAN
  * $Id$
  *
- * Authors: Romain Goyet <romain.goyet@likid.org>
- *          Felix Paul Kühne <fkuehne@videolan.org>
+ * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
+ *          Rémi Denis-Courmont
+ *          Rafaël Carré <funman at videolan dot org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-/*****************************************************************************
- * Preamble
- *****************************************************************************/
+#pragma mark includes
+
 #ifdef HAVE_CONFIG_H
 # import "config.h"
 #endif
-
 #import <vlc_common.h>
 #import <vlc_plugin.h>
 #import <vlc_aout.h>
-#import <AudioToolBox/AudioQueue.h>
+#import <AudioToolbox/AudioQueue.h>
+#import <TargetConditionals.h>
+#if TARGET_OS_IPHONE
+#import <AudioToolbox/AudioSession.h>
+#else
+#define AudioSessionSetActive(x)
+#endif
+
+#pragma mark -
+#pragma mark private declarations
 
-/*****************************************************************************
- * aout_sys_t: private audio output method descriptor
- *****************************************************************************
- * This structure is part of the audio output thread descriptor.
- * It describes the AudioQueue specific properties of an output thread.
- *****************************************************************************/
 struct aout_sys_t
 {
-    AudioQueueRef           audioQueue;
-    AudioQueueTimelineRef   outTimeline;
+    AudioQueueRef           audioQueueRef;
+    AudioQueueTimelineRef   timelineRef;
+
+    bool                    b_started;
 
-    int                     i_rate;
     mtime_t                 i_played_length;
-    bool                    b_stopped;
+    int                     i_rate;
     float                   f_volume;
 };
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
 static int  Open                     (vlc_object_t *);
 static void Close                    (vlc_object_t *);
 static void Play                     (audio_output_t *, block_t *);
@@ -61,14 +58,9 @@ static void Pause                    (audio_output_t *p_aout, bool pause, mtime_
 static void Flush                    (audio_output_t *p_aout, bool wait);
 static int  TimeGet                  (audio_output_t *aout, mtime_t *);
 static void UnusedAudioQueueCallback (void *, AudioQueueRef, AudioQueueBufferRef);
-
 static int Start(audio_output_t *, audio_sample_format_t *);
 static void Stop(audio_output_t *);
 static int VolumeSet(audio_output_t *, float );
-
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
 vlc_module_begin ()
 set_shortname("AudioQueue")
 set_description(N_("AudioQueue (iOS / Mac OS) audio output"))
@@ -79,9 +71,8 @@ add_shortcut("audioqueue")
 set_callbacks(Open, Close)
 vlc_module_end ()
 
-/*****************************************************************************
- * Module management
- *****************************************************************************/
+#pragma mark -
+#pragma mark initialization
 
 static int Open(vlc_object_t *obj)
 {
@@ -120,15 +111,11 @@ static int VolumeSet(audio_output_t * p_aout, float volume)
     p_sys->f_volume = volume;
 
     /* Set volume for output unit */
-    ostatus = AudioQueueSetParameter(p_sys->audioQueue, kAudioQueueParam_Volume, volume * volume * volume);
+    ostatus = AudioQueueSetParameter(p_sys->audioQueueRef, kAudioQueueParam_Volume, volume * volume * volume);
 
     return ostatus;
 }
 
-/*****************************************************************************
- * Start: open the audio device
- *****************************************************************************/
-
 static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
 {
     aout_sys_t *p_sys = p_aout->sys;
@@ -152,130 +139,144 @@ static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
                                  NULL,                        // RunLoop
                                  kCFRunLoopCommonModes,       // RunLoop mode
                                  0,                           // Flags ; must be zero (per documentation)...
-                                 &(p_sys->audioQueue));       // Output
-
-    msg_Dbg(p_aout, "New AudioQueue output created (status = %li)", error);
+                                 &(p_sys->audioQueueRef));    // Output
+    msg_Dbg(p_aout, "New AudioQueue instance created (status = %li)", error);
     if (error != noErr)
         return VLC_EGENERIC;
-
     fmt->i_format = VLC_CODEC_FL32;
     fmt->i_physical_channels = AOUT_CHANS_STEREO;
     aout_FormatPrepare(fmt);
-
-    p_aout->sys->b_stopped = false;
     p_aout->sys->i_rate = fmt->i_rate;
 
     // start queue
-    error = AudioQueueStart(p_sys->audioQueue, NULL);
+    error = AudioQueueStart(p_sys->audioQueueRef, NULL);
     msg_Dbg(p_aout, "Starting AudioQueue (status = %li)", error);
 
     // start timeline for synchro
-    error = AudioQueueCreateTimeline(p_sys->audioQueue, &p_sys->outTimeline);
+    error = AudioQueueCreateTimeline(p_sys->audioQueueRef, &p_sys->timelineRef);
     msg_Dbg(p_aout, "AudioQueue Timeline started (status = %li)", error);
-
     if (error != noErr)
         return VLC_EGENERIC;
 
+#if TARGET_OS_IPHONE
+    // start audio session so playback continues if mute switch is on
+    AudioSessionInitialize (NULL,
+                            kCFRunLoopCommonModes,
+                            NULL,
+                            NULL);
+
+       // Set audio session to mediaplayback
+       UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
+       AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory),&sessionCategory);
+       AudioSessionSetActive(true);
+#endif
+
+    p_aout->sys->b_started = true;
+
     p_aout->time_get = TimeGet;
     p_aout->play = Play;
     p_aout->pause = Pause;
     p_aout->flush = Flush;
-
     return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * Stop: close the audio device
- *****************************************************************************/
-
 static void Stop(audio_output_t *p_aout)
 {
-    p_aout->sys->b_stopped = true;
-    p_aout->sys->i_played_length = 0;
-
-    AudioQueueDisposeTimeline(p_aout->sys->audioQueue, p_aout->sys->outTimeline);
-    AudioQueueStop(p_aout->sys->audioQueue, true);
-    AudioQueueDispose(p_aout->sys->audioQueue, true);
+    AudioSessionSetActive(false);
 
+    p_aout->sys->i_played_length = 0;
+    AudioQueueDisposeTimeline(p_aout->sys->audioQueueRef, p_aout->sys->timelineRef);
+    AudioQueueStop(p_aout->sys->audioQueueRef, true);
+    AudioQueueDispose(p_aout->sys->audioQueueRef, true);
     msg_Dbg(p_aout, "audioqueue stopped and disposed");
 }
 
-/*****************************************************************************
- * actual playback
- *****************************************************************************/
+#pragma mark -
+#pragma mark actual playback
 
 static void Play(audio_output_t *p_aout, block_t *p_block)
 {
     AudioQueueBufferRef inBuffer = NULL;
     OSStatus status;
 
-    status = AudioQueueAllocateBuffer(p_aout->sys->audioQueue, p_block->i_buffer, &inBuffer);
-    if (status != noErr) {
-        msg_Err(p_aout, "buffer alloction failed (%li)", status);
-        return;
-    }
+    status = AudioQueueAllocateBuffer(p_aout->sys->audioQueueRef, p_block->i_buffer, &inBuffer);
+    if (status == noErr) {
+        memcpy(inBuffer->mAudioData, p_block->p_buffer, p_block->i_buffer);
+        inBuffer->mAudioDataByteSize = p_block->i_buffer;
 
-    memcpy(inBuffer->mAudioData, p_block->p_buffer, p_block->i_buffer);
-    inBuffer->mAudioDataByteSize = p_block->i_buffer;
-
-    status = AudioQueueEnqueueBuffer(p_aout->sys->audioQueue, inBuffer, 0, NULL);
-    if (status == noErr)
-        p_aout->sys->i_played_length += p_block->i_length;
-    else
-        msg_Err(p_aout, "enqueuing buffer failed (%li)", status);
+        status = AudioQueueEnqueueBuffer(p_aout->sys->audioQueueRef, inBuffer, 0, NULL);
+        if (status == noErr)
+            p_aout->sys->i_played_length += p_block->i_length;
+        else
+            msg_Err(p_aout, "enqueuing buffer failed (%li)", status);
+    } else
+            msg_Err(p_aout, "buffer alloction failed (%li)", status);
 
     block_Release(p_block);
 }
 
 void UnusedAudioQueueCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
     /* this function does nothing, but needs to be here to make the AudioQueue API happy.
-     * without a callback, it will refuse to create an AudioQueue instance. */
+     * additionally, we clean-up after empty buffers */
     VLC_UNUSED(inUserData);
-    VLC_UNUSED(inAQ);
-    VLC_UNUSED(inBuffer);
+    AudioQueueFreeBuffer(inAQ, inBuffer);
 }
 
 static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
 {
     VLC_UNUSED(date);
 
-    if (pause)
-        AudioQueuePause(p_aout->sys->audioQueue);
-    else
-        AudioQueueStart(p_aout->sys->audioQueue, NULL);
+    if (pause) {
+        AudioQueuePause(p_aout->sys->audioQueueRef);
+        AudioSessionSetActive(false);
+    } else {
+        AudioQueueStart(p_aout->sys->audioQueueRef, NULL);
+        AudioSessionSetActive(true);
+    }
 }
 
 static void Flush(audio_output_t *p_aout, bool wait)
 {
-    if (p_aout->sys->b_stopped || !p_aout->sys->audioQueue)
+    if (!p_aout->sys->audioQueueRef)
         return;
 
-    AudioQueueDisposeTimeline(p_aout->sys->audioQueue, p_aout->sys->outTimeline);
+    AudioQueueDisposeTimeline(p_aout->sys->audioQueueRef, p_aout->sys->timelineRef);
 
     if (wait)
-        AudioQueueStop(p_aout->sys->audioQueue, false);
+        AudioQueueStop(p_aout->sys->audioQueueRef, false);
     else
-        AudioQueueStop(p_aout->sys->audioQueue, true);
+        AudioQueueStop(p_aout->sys->audioQueueRef, true);
 
+    p_aout->sys->b_started = false;
     p_aout->sys->i_played_length = 0;
-    AudioQueueStart(p_aout->sys->audioQueue, NULL);
-    AudioQueueCreateTimeline(p_aout->sys->audioQueue, &p_aout->sys->outTimeline);
+    AudioQueueStart(p_aout->sys->audioQueueRef, NULL);
+    AudioQueueCreateTimeline(p_aout->sys->audioQueueRef, &p_aout->sys->timelineRef);
+    p_aout->sys->b_started = true;
 }
 
 static int TimeGet(audio_output_t *p_aout, mtime_t *restrict delay)
 {
     AudioTimeStamp outTimeStamp;
     Boolean b_discontinuity;
-    OSStatus status = AudioQueueGetCurrentTime(p_aout->sys->audioQueue, p_aout->sys->outTimeline, &outTimeStamp, &b_discontinuity);
+    OSStatus status = AudioQueueGetCurrentTime(p_aout->sys->audioQueueRef, p_aout->sys->timelineRef, &outTimeStamp, &b_discontinuity);
 
     if (status != noErr)
         return -1;
 
-    if (b_discontinuity)
+    bool b_started = p_aout->sys->b_started;
+
+    if (!b_started)
+        return -1;
+
+    if (b_discontinuity) {
         msg_Dbg(p_aout, "detected output discontinuity");
+        return -1;
+    }
 
     mtime_t i_pos = (mtime_t) outTimeStamp.mSampleTime * CLOCK_FREQ / p_aout->sys->i_rate;
-    *delay = p_aout->sys->i_played_length - i_pos;
-
-    return 0;
+    if (i_pos > 0) {
+        *delay = p_aout->sys->i_played_length - i_pos;
+        return 0;
+    } else
+        return -1;
 }