Imported Upstream version 3.2.2
[debian/gnuradio] / gr-audio-osx / src / audio_osx_source.cc
index 27097f106cf807f473bd333cdb9b053000340e52..e82e8ad21515e128a8ea778ff7b0860912ca0036 100644 (file)
@@ -114,7 +114,7 @@ audio_osx_source::audio_osx_source (int sample_rate,
   d_max_sample_count = max_sample_count;
 
 #if _OSX_AU_DEBUG_
-  fprintf (stderr, "source(): max # samples = %ld", d_max_sample_count);
+  fprintf (stderr, "source(): max # samples = %ld\n", d_max_sample_count);
 #endif
 
   OSStatus err = noErr;
@@ -248,7 +248,7 @@ audio_osx_source::audio_osx_source (int sample_rate,
                      "audio_osx_source::audio_osx_source");
 
 #if _OSX_AU_DEBUG_
-  fprintf (stderr, "---- Device Stream Format ----\n" );
+  fprintf (stderr, "\n---- Device Stream Format ----\n" );
   PrintStreamDesc (&asbd_device);
 #endif
 
@@ -264,7 +264,7 @@ audio_osx_source::audio_osx_source (int sample_rate,
                      "audio_osx_source::audio_osx_source");
 
 #if _OSX_AU_DEBUG_
-  fprintf (stderr, "---- Client Stream Format ----\n");
+  fprintf (stderr, "\n---- Client Stream Format ----\n");
   PrintStreamDesc (&asbd_client);
 #endif
 
@@ -423,15 +423,11 @@ audio_osx_source::audio_osx_source (int sample_rate,
 
 // create the stuff to regulate I/O
 
-  d_internal = new mld_mutex ();
-  if (d_internal == NULL)
-    CheckErrorAndThrow (errno, "new mld_mutex (internal)",
-                       "audio_osx_source::audio_osx_source");
-
   d_cond_data = new mld_condition ();
   if (d_cond_data == NULL)
     CheckErrorAndThrow (errno, "new mld_condition (data)",
                        "audio_osx_source::audio_osx_source");
+  d_internal = d_cond_data->mutex ();
 
 // initialize the AU for input
 
@@ -580,7 +576,6 @@ audio_osx_source::~audio_osx_source ()
   d_buffers = 0;
 
 // close and delete the control stuff
-  delete d_internal;
   delete d_cond_data;
 }
 
@@ -630,47 +625,55 @@ audio_osx_source::check_topology (int ninputs, int noutputs)
 }
 
 int
-audio_osx_source::work (int noutput_items,
-                       gr_vector_const_void_star &input_items,
-                       gr_vector_void_star &output_items)
+audio_osx_source::work
+(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
 {
-// acquire control to do processing here only
-  d_internal->wait ();
+  // acquire control to do processing here only
+  d_internal->lock ();
 
 #if _OSX_AU_DEBUG_
   fprintf (stderr, "work1: SC = %4ld, #OI = %4d, #Chan = %ld\n",
           d_queueSampleCount, noutput_items, output_items.size());
 #endif
 
-// ?: always block until there is something to output from the source
-//    or return anything that is available, even if it's less than desired?
+  // set the actual # of output items to the 'desired' amount then
+  // verify that data is available; if not enough data is available,
+  // either wait until it is (is "do_block" is true), return (0) is no
+  // data is available and "do_block" is false, or process the actual
+  // amount of available data.
 
   UInt32 actual_noutput_items = noutput_items;
 
   if (d_queueSampleCount < actual_noutput_items) {
     if (d_queueSampleCount == 0) {
-// no data; do_block decides what to do
+      // no data; do_block decides what to do
       if (d_do_block == true) {
        while (d_queueSampleCount == 0) {
-// release control so-as to allow data to be retrieved
-         d_internal->post ();
-// block until there is data to return
+         // release control so-as to allow data to be retrieved;
+         // block until there is data to return
          d_cond_data->wait ();
-// the condition's signal() was called; acquire control
-// to keep thread safe
-         d_internal->wait ();
+         // the condition's signal() was called; acquire control to
+         // keep thread safe
        }
       } else {
-// not enough data & not blocking; return nothing
+       // no data & not blocking; return nothing
+       // release control so-as to allow data to be retrieved
+       d_internal->unlock ();
        return (0);
       }
     }
+    // use the actual amount of available data
     actual_noutput_items = d_queueSampleCount;
   }
 
+  // number of channels
   int l_counter = (int) output_items.size();
 
-// get the items from the circular buffers
+  // copy the items from the circular buffer(s) to 'work's output buffers
+  // verify that the number copied out is as expected.
+
   while (--l_counter >= 0) {
     UInt32 t_n_output_items = actual_noutput_items;
     d_buffers[l_counter]->dequeue ((float*) output_items[l_counter],
@@ -684,6 +687,9 @@ audio_osx_source::work (int noutput_items,
     }
   }
 
+  // subtract the actual number of items removed from the buffer(s)
+  // from the local accounting of the number of available samples
+
   d_queueSampleCount -= actual_noutput_items;
 
 #if _OSX_AU_DEBUG_
@@ -691,21 +697,28 @@ audio_osx_source::work (int noutput_items,
           d_queueSampleCount, actual_noutput_items);
 #endif
 
-// release control to allow for other processing parts to run
-  d_internal->post ();
+  // release control to allow for other processing parts to run
+
+  d_internal->unlock ();
+
+#if _OSX_AU_DEBUG_
+  fprintf (stderr, "work3: Returning.\n");
+#endif
 
   return (actual_noutput_items);
 }
 
 OSStatus
-audio_osx_source::ConverterCallback (AudioConverterRef inAudioConverter,
-                                    UInt32* ioNumberDataPackets,
-                                    AudioBufferList* ioData,
-                                    AudioStreamPacketDescription** ioASPD,
-                                    void* inUserData)
+audio_osx_source::ConverterCallback
+(AudioConverterRef inAudioConverter,
+ UInt32* ioNumberDataPackets,
+ AudioBufferList* ioData,
+ AudioStreamPacketDescription** ioASPD,
+ void* inUserData)
 {
-// take current device buffers and copy them to the tail of the input buffers
-// the lead buffer is already there in the first d_leadSizeFrames slots
+  // take current device buffers and copy them to the tail of the
+  // input buffers the lead buffer is already there in the first
+  // d_leadSizeFrames slots
 
   audio_osx_source* This = static_cast<audio_osx_source*>(inUserData);
   AudioBufferList* l_inputABL = This->d_InputBuffer;
@@ -726,6 +739,10 @@ audio_osx_source::ConverterCallback (AudioConverterRef inAudioConverter,
     l_ioD_AB->mDataByteSize = totalInputBufferSizeBytes;
   }
 
+#if _OSX_AU_DEBUG_
+  fprintf (stderr, "cc2: Returning.\n");
+#endif
+
   return (noErr);
 }
 
@@ -740,7 +757,7 @@ audio_osx_source::AUInputCallback (void* inRefCon,
   OSStatus err = noErr;
   audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
 
-  This->d_internal->wait ();
+  This->d_internal->lock ();
 
 #if _OSX_AU_DEBUG_
   fprintf (stderr, "cb0: in#F = %4ld, inBN = %ld, SC = %4ld\n",
@@ -839,6 +856,11 @@ audio_osx_source::AUInputCallback (void* inRefCon,
 
   while (--l_counter >= 0) {
     float* inBuffer = (float*) This->d_OutputBuffer->mBuffers[l_counter].mData;
+
+#if _OSX_AU_DEBUG_
+  fprintf (stderr, "cb3: enqueuing audio data.\n");
+#endif
+
     int l_res = This->d_buffers[l_counter]->enqueue (inBuffer, ActualOutputFrames);
     if (l_res == -1)
       res = -1;
@@ -857,7 +879,7 @@ audio_osx_source::AUInputCallback (void* inRefCon,
   }
 
 #if _OSX_AU_DEBUG_
-  fprintf (stderr, "cb5: #OI = %4ld, #Cnt = %4ld, mSC = %ld, \n",
+  fprintf (stderr, "cb4: #OI = %4ld, #Cnt = %4ld, mSC = %ld, \n",
           ActualOutputFrames, This->d_queueSampleCount,
           This->d_max_sample_count);
 #endif
@@ -865,8 +887,16 @@ audio_osx_source::AUInputCallback (void* inRefCon,
 // signal that data is available, if appropraite
   This->d_cond_data->signal ();
 
+#if _OSX_AU_DEBUG_
+  fprintf (stderr, "cb5: releasing internal mutex.\n");
+#endif
+
 // release control to allow for other processing parts to run
-  This->d_internal->post ();
+  This->d_internal->unlock ();
+
+#if _OSX_AU_DEBUG_
+  fprintf (stderr, "cb6: returning.\n");
+#endif
 
   return (err);
 }