Index: /pjproject/trunk/pjmedia/src/pjmedia-audiodev/bb10_dev.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-audiodev/bb10_dev.c	(revision 4339)
+++ /pjproject/trunk/pjmedia/src/pjmedia-audiodev/bb10_dev.c	(revision 4340)
@@ -123,4 +123,5 @@
 
     /* Playback */
+    unsigned int pb_ctrl_audio_manager_handle;
     snd_pcm_t		*pb_pcm;
     unsigned int pb_audio_manager_handle;
@@ -169,6 +170,4 @@
     pjmedia_aud_dev_info *adi;
     int pb_result, ca_result;
-    int card = -1;
-    int dev = 0;
     unsigned int handle;
     snd_pcm_t *pcm_handle;
@@ -184,15 +183,10 @@
                                                      &pcm_handle,
                                                      &handle,
-                                                     "/dev/snd/voicep",
+                                                     (char*)"voice",
                                                      SND_PCM_OPEN_PLAYBACK))
                                                      >= 0)
     {
-    	if ((pb_result = snd_pcm_plugin_set_disable (pcm_handle, PLUGIN_DISABLE_MMAP)) < 0) {
-            TRACE_((THIS_FILE, "snd_pcm_plugin_set_disable ret = %d", pb_result));
-    	}else{
-            TRACE_((THIS_FILE, "Try to open the device for playback - success"));
-    	}
-	    snd_pcm_close (pcm_handle);
-	    audio_manager_free_handle(handle);
+	snd_pcm_close (pcm_handle);
+	audio_manager_free_handle(handle);
     } else {
         TRACE_((THIS_FILE, "Try to open the device for playback - failure"));
@@ -202,15 +196,10 @@
                                                      &pcm_handle,
                                                      &handle,
-                                                     "/dev/snd/voicec",
+                                                     (char*)"voice",
                                                      SND_PCM_OPEN_CAPTURE))
                                                      >= 0)
     {
-        if ((ca_result = snd_pcm_plugin_set_disable (pcm_handle, PLUGIN_DISABLE_MMAP)) < 0) {
-            TRACE_((THIS_FILE, "snd_pcm_plugin_set_disable ret = %d", ca_result));
-        }else{
-            TRACE_((THIS_FILE, "Try to open the device for capture - success"));        
-        }
-        snd_pcm_close (pcm_handle);
-	    audio_manager_free_handle(handle);
+	snd_pcm_close (pcm_handle);
+	audio_manager_free_handle(handle);
 
     } else {
@@ -397,8 +386,14 @@
         snd_pcm_close(stream->pb_pcm);
         stream->pb_pcm = NULL;
-        if(stream->pb_audio_manager_handle != 0){
+
+        if (stream->pb_audio_manager_handle != 0) {
     	    audio_manager_free_handle(stream->pb_audio_manager_handle);
     	    stream->pb_audio_manager_handle = 0;
         }
+
+        if (stream->pb_ctrl_audio_manager_handle != 0) {
+    	    audio_manager_free_handle(stream->pb_ctrl_audio_manager_handle);
+    	    stream->pb_ctrl_audio_manager_handle = 0;
+        }
     }
 }
@@ -416,5 +411,6 @@
         snd_pcm_close(stream->ca_pcm);
         stream->ca_pcm = NULL;
-        if(stream->ca_audio_manager_handle != 0){
+
+        if (stream->ca_audio_manager_handle != 0) {
     	    audio_manager_free_handle(stream->ca_audio_manager_handle);
     	    stream->ca_audio_manager_handle = 0;
@@ -458,5 +454,5 @@
         close_play_pcm(stream);
         TRACE_((THIS_FILE, "pb_thread_func failed prepare = %d", result));
-	return PJ_SUCCESS;
+        return PJ_SUCCESS;
     }
 
@@ -471,4 +467,5 @@
         frame.bit_info = 0;
 
+        /* Read the audio from pjmedia */
         result = stream->pb_cb (user_data, &frame);
         if (result != PJ_SUCCESS || stream->quit)
@@ -481,26 +478,35 @@
         result = snd_pcm_plugin_write(stream->pb_pcm,buf,size);
         if (result != size || result < 0) {
-            snd_pcm_channel_status_t status;
+	    /* either the write to output device has failed or not the
+	     * full amount of bytes have been written. This usually happens
+	     * when audio routing is being changed by another thread
+	     * Use a status variable for reading the error
+	     */
+	    snd_pcm_channel_status_t status;
+            status.channel = SND_PCM_CHANNEL_PLAYBACK;
             if (snd_pcm_plugin_status (stream->pb_pcm, &status) < 0) {
-                PJ_LOG(4,(THIS_FILE,
+                /* Call has failed nothing we can do except log and
+                 * continue */
+            	PJ_LOG(4,(THIS_FILE,
                 	  "underrun: playback channel status error"));
-                continue;
-            }
-
-            if (status.status == SND_PCM_STATUS_READY ||
-        	status.status == SND_PCM_STATUS_UNDERRUN)
-            {
-                if (snd_pcm_plugin_prepare (stream->pb_pcm,
-                                            SND_PCM_CHANNEL_PLAYBACK) < 0)
-                {
-                    PJ_LOG(4,(THIS_FILE,
-                    	      "underrun: playback channel prepare error"));
-                    continue;
-                }
-            }
-
-            TRACE_((THIS_FILE, "pb_thread_func failed write = %d", result));
+            } else {
+            	/* The status of the error has been read
+            	 * RIM say these are expected so we can "re-prepare" the stream
+            	 */
+            	PJ_LOG(4,(THIS_FILE,"PLAY thread ERROR status = %d",
+            		  status.status));
+		if (status.status == SND_PCM_STATUS_READY ||
+		    status.status == SND_PCM_STATUS_UNDERRUN ||
+		    status.status == SND_PCM_STATUS_ERROR ) 
+		{
+		    if (snd_pcm_plugin_prepare (stream->pb_pcm,
+						SND_PCM_CHANNEL_PLAYBACK) < 0)
+		    {
+			PJ_LOG(4,(THIS_FILE,
+				  "underrun: playback channel prepare error"));
+		    }
+		}
+	    }
         }
-
 	tstamp.u64 += nframes;
     }
@@ -553,5 +559,5 @@
         close_capture_pcm(stream);
         TRACE_((THIS_FILE, "ca_thread_func failed prepare = %d", result));
-	return PJ_SUCCESS;
+        return PJ_SUCCESS;
     }
 
@@ -561,22 +567,39 @@
         pj_bzero (buf, size);
 
+        /* read the input device */
         result = snd_pcm_plugin_read(stream->ca_pcm, buf,size);
         if(result <0 || result != size) {
+            /* We expect result to be size (640)
+             * It's not so we have to read the status error and "prepare"
+             * the channel. This usually happens when output audio routing
+             * has been changed by another thread.
+             * We won't "continue", instead just do what we can and leave
+             * the end of the loop to write what's in the buffer. Not entirely
+             * correct but saves a potential underrun in PJMEDIA
+             */
+            PJ_LOG (4,(THIS_FILE,
+        	       "snd_pcm_plugin_read ERROR read = %d required = %d",
+        	       result,size));
             snd_pcm_channel_status_t status;
-            if (snd_pcm_plugin_status (stream->ca_pcm, &status) < 0) {
-                PJ_LOG (4,(THIS_FILE, "overrun: capture channel status "
-                                      "error"));
-                continue;
-            }
-
-            if (status.status == SND_PCM_STATUS_READY ||
-        	status.status == SND_PCM_STATUS_OVERRUN) {
-                if (snd_pcm_plugin_prepare (stream->ca_pcm,
-                                            SND_PCM_CHANNEL_CAPTURE) < 0)
-                {
-                    PJ_LOG (4,(THIS_FILE, "overrun: capture channel prepare  "
-                                          "error"));
-                    continue;
-                }
+            status.channel = SND_PCM_CHANNEL_CAPTURE;
+            if ((result = snd_pcm_plugin_status (stream->ca_pcm, &status)) < 0)
+            {
+        	/* Should not fail but all we can do is continue */
+        	PJ_LOG(4,(THIS_FILE, "capture: snd_pcm_plugin_status ret = %d",
+        		  result));
+            } else {
+        	/* RIM say these are the errors that we should "prepare"
+        	 * after */
+        	if (status.status == SND_PCM_STATUS_READY ||
+        		status.status == SND_PCM_STATUS_OVERRUN ||
+        		status.status == SND_PCM_STATUS_ERROR)
+        	{
+        	    if (snd_pcm_plugin_prepare (stream->ca_pcm,
+        	                                SND_PCM_CHANNEL_CAPTURE) < 0)
+        	    {
+        		PJ_LOG (4,(THIS_FILE,
+        			   "overrun: capture channel prepare  error"));
+        	    }
+        	}
             }
         }
@@ -585,4 +608,5 @@
             break;
 
+        /* Write the capture audio data to PJMEDIA */
         frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
         frame.buf = (void *) buf;
@@ -605,10 +629,60 @@
 }
 
+/* Audio routing, speaker/headset */
+static pj_status_t bb10_initialize_playback_ctrl(struct bb10_stream *stream,
+                                                 bool speaker)
+{
+    /* Although the play and capture have audio manager handles, audio routing
+     * requires a separate handle
+     */
+    int ret = PJ_SUCCESS;
+
+    if (stream->pb_ctrl_audio_manager_handle == 0) {
+	/* lazy init an audio manager handle */
+	ret = audio_manager_get_handle(AUDIO_TYPE_VIDEO_CHAT, 0, false,
+	                               &stream->pb_ctrl_audio_manager_handle);
+	if (ret != 0) {
+	    TRACE_((THIS_FILE, "audio_manager_get_handle ret = %d",ret));
+	    return PJMEDIA_EAUD_SYSERR;
+	}
+    }
+
+    /* Set for either speaker or earpiece */
+    if (speaker) {
+	ret = audio_manager_set_handle_type(
+		stream->pb_ctrl_audio_manager_handle,
+		AUDIO_TYPE_VIDEO_CHAT,
+		AUDIO_DEVICE_SPEAKER,
+		AUDIO_DEVICE_DEFAULT);
+    } else {
+	ret = audio_manager_set_handle_type(
+		stream->pb_ctrl_audio_manager_handle,
+		AUDIO_TYPE_VIDEO_CHAT,
+		AUDIO_DEVICE_HANDSET,
+		AUDIO_DEVICE_DEFAULT);
+    }
+
+    if (ret == 0) {
+	/* RIM recommend this call */
+	ret = audio_manager_set_handle_routing_conditions(
+		stream->pb_ctrl_audio_manager_handle,
+		SETTINGS_RESET_ON_DEVICE_CONNECTION);
+	if (ret != 0) {
+	    TRACE_((THIS_FILE,
+		    "audio_manager_set_handle_routing_conditions ret = %d",
+		    ret));
+	    return PJMEDIA_EAUD_SYSERR;
+	}
+    } else {
+	TRACE_((THIS_FILE, "audio_manager_set_handle_type ret = %d", ret));
+	return PJMEDIA_EAUD_SYSERR;
+    }
+
+    return PJ_SUCCESS;
+}
 
 static pj_status_t bb10_open_playback (struct bb10_stream *stream,
                                        const pjmedia_aud_param *param)
 {
-    int card = -1;
-    int dev = 0;
     int ret = 0;
     snd_pcm_channel_info_t pi;
@@ -623,41 +697,44 @@
     }
 
-    if ((ret = audio_manager_snd_pcm_open_name(AUDIO_TYPE_VIDEO_CHAT,
-                                               &stream->pb_pcm, &stream->pb_audio_manager_handle,
-                                               "/dev/snd/voicep",
-                                               SND_PCM_OPEN_PLAYBACK)) < 0)
+    /* Use the bb10 audio manager API to open as opposed to QNX core audio
+     * Echo cancellation built in
+     */
+    if ((ret = audio_manager_snd_pcm_open_name(
+	    AUDIO_TYPE_VIDEO_CHAT,
+            &stream->pb_pcm, &stream->pb_audio_manager_handle,
+            (char*)"voice",
+            SND_PCM_OPEN_PLAYBACK)) < 0)
     {
         TRACE_((THIS_FILE, "audio_manager_snd_pcm_open_name ret = %d", ret));
         return PJMEDIA_EAUD_SYSERR;
     }
-    ret = audio_manager_set_handle_type(stream->pb_audio_manager_handle, AUDIO_TYPE_VIDEO_CHAT, AUDIO_DEVICE_HANDSET , AUDIO_DEVICE_HANDSET);
-    if (ret==0) {
-          ret = audio_manager_set_handle_routing_conditions(stream->pb_audio_manager_handle, SETTINGS_RESET_ON_DEVICE_CONNECTION);
-          if(ret != 0){
-              TRACE_((THIS_FILE, "audio_manager_set_handle_routing_conditions ret = %d", ret));
-              return PJMEDIA_EAUD_SYSERR;
-          }
-    }else{
-    	TRACE_((THIS_FILE, "audio_manager_set_handle_type ret = %d", ret));
-        return PJMEDIA_EAUD_SYSERR;
-    }
-
-    if ((ret = snd_pcm_plugin_set_disable (stream->pb_pcm, PLUGIN_DISABLE_MMAP)) < 0) {
-    	TRACE_((THIS_FILE, "snd_pcm_plugin_set_disable ret = %d", ret));
-        return PJMEDIA_EAUD_SYSERR;
-    }
+
+    /* Required call from January 2013 gold OS release */
+    if ((ret = snd_pcm_plugin_set_disable(stream->pb_pcm,
+                                          PLUGIN_DISABLE_MMAP)) < 0)
+    {
+	TRACE_((THIS_FILE, "snd_pcm_plugin_set_disable ret = %d", ret));
+	return PJMEDIA_EAUD_SYSERR;
+    }
+
+    /* Required call from January 2013 gold OS release */
+    if ((ret = snd_pcm_plugin_set_enable(stream->pb_pcm,
+                                         PLUGIN_ROUTING)) < 0)
+    {
+	TRACE_((THIS_FILE, "snd_pcm_plugin_set_enable ret = %d", ret));
+	return PJMEDIA_EAUD_SYSERR;
+    }
+
     /* TODO PJ_ZERO */
     memset (&pi, 0, sizeof (pi));
     pi.channel = SND_PCM_CHANNEL_PLAYBACK;
     if ((ret = snd_pcm_plugin_info (stream->pb_pcm, &pi)) < 0) {
-        TRACE_((THIS_FILE, "snd_pcm_plugin_info ret = %d", ret));
-        return PJMEDIA_EAUD_SYSERR;
+	TRACE_((THIS_FILE, "snd_pcm_plugin_info ret = %d", ret));
+	return PJMEDIA_EAUD_SYSERR;
     }
 
     memset (&pp, 0, sizeof (pp));
 
-    /* Request VoIP compatible capabilities
-     * On simulator frag_size is always negotiated to 170
-     */
+    /* Request VoIP compatible capabilities */
     pp.mode = SND_PCM_MODE_BLOCK;
     pp.channel = SND_PCM_CHANNEL_PLAYBACK;
@@ -666,6 +743,6 @@
     /* HARD CODE for the time being PJMEDIA expects 640 for 16khz */
     pp.buf.block.frag_size = PREFERRED_FRAME_SIZE*2;
-    /* Increasing this internal buffer count delays write failure in the loop */
-    pp.buf.block.frags_max = 4;
+    /* RIM recommends maximum of 3 */
+    pp.buf.block.frags_max = 3;
     pp.buf.block.frags_min = 1;
     pp.format.interleave = 1;
@@ -715,5 +792,5 @@
     TRACE_((THIS_FILE, "bb10_open_playback: pb_frames = %d clock = %d",
                        stream->pb_frames, param->clock_rate));
-
+    
     return PJ_SUCCESS;
 }
@@ -725,6 +802,4 @@
     unsigned int rate;
     unsigned long tmp_buf_size;
-    int card = -1;
-    int dev = 0;
     int frame_size;
     snd_pcm_channel_info_t pi;
@@ -736,15 +811,25 @@
         return PJMEDIA_EAUD_INVDEV;
 
-    if ((ret = audio_manager_snd_pcm_open_name(AUDIO_TYPE_VIDEO_CHAT,
-                                               &stream->ca_pcm,
-                                               &stream->ca_audio_manager_handle,
-                                               "/dev/snd/voicec",
-                                               SND_PCM_OPEN_CAPTURE)) < 0)
+    if ((ret=audio_manager_snd_pcm_open_name(AUDIO_TYPE_VIDEO_CHAT,
+                                             &stream->ca_pcm,
+                                             &stream->ca_audio_manager_handle,
+                                             (char*)"voice",
+                                             SND_PCM_OPEN_CAPTURE)) < 0)
     {
 	TRACE_((THIS_FILE, "audio_manager_snd_pcm_open_name ret = %d", ret));
 	return PJMEDIA_EAUD_SYSERR;
     }
-    if ((ret = snd_pcm_plugin_set_disable (stream->ca_pcm, PLUGIN_DISABLE_MMAP)) < 0) {
+    /* Required call from January 2013 gold OS release */
+    if ((ret = snd_pcm_plugin_set_disable (stream->ca_pcm,
+                                           PLUGIN_DISABLE_MMAP)) < 0)
+    {
         TRACE_(("snd_pcm_plugin_set_disable failed: %d",ret));
+        return PJMEDIA_EAUD_SYSERR;
+    }
+    /* Required call from January 2013 gold OS release */
+    if ((ret = snd_pcm_plugin_set_enable(stream->ca_pcm,
+                                         PLUGIN_ROUTING)) < 0)
+    {
+        TRACE_(("snd_pcm_plugin_set_enable failed: %d",ret));
         return PJMEDIA_EAUD_SYSERR;
     }
@@ -770,6 +855,6 @@
     /* HARD CODE for the time being PJMEDIA expects 640 for 16khz */
     pp.buf.block.frag_size = PREFERRED_FRAME_SIZE*2;
-    /* Not applicable for capture hence -1 */
-    pp.buf.block.frags_max = -1;
+    /* From January 2013 gold OS release. RIM recommend these for capture */
+    pp.buf.block.frags_max = 1;
     pp.buf.block.frags_min = 1;
     pp.format.interleave = 1;
@@ -803,8 +888,5 @@
     }
 
-    /* frag_size should be 160 */
     frame_size = setup.buf.block.frag_size;
-
-    /* END BB10 init */
 
     /* Set clock rate */
@@ -883,4 +965,13 @@
     }
 
+    /* Part of the play functionality but the RIM/Truphone loopback sample
+     * initialializes after the play and capture
+     * "false" is default/earpiece for output
+     */
+    status = bb10_initialize_playback_ctrl(stream,false);
+    if (status != PJ_SUCCESS) {
+    	return PJMEDIA_EAUD_SYSERR;
+    }
+
     *p_strm = &stream->base;
     return PJ_SUCCESS;
@@ -888,5 +979,8 @@
 
 
-/* API: get running parameter */
+/* 
+ * API: get running parameter
+ * based on ALSA template
+ */
 static pj_status_t bb10_stream_get_param(pjmedia_aud_stream *s,
                                          pjmedia_aud_param *pi)
@@ -902,5 +996,8 @@
 
 
-/* API: get capability */
+/*
+ * API: get capability
+ * based on ALSA template 
+*/
 static pj_status_t bb10_stream_get_cap(pjmedia_aud_stream *s,
                                        pjmedia_aud_dev_cap cap,
@@ -917,4 +1014,5 @@
         *(unsigned*)pval = stream->param.input_latency_ms;
         return PJ_SUCCESS;
+
     } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY &&
                (stream->param.dir & PJMEDIA_DIR_PLAYBACK))
@@ -923,4 +1021,5 @@
         *(unsigned*)pval = stream->param.output_latency_ms;
         return PJ_SUCCESS;
+
     } else {
         return PJMEDIA_EAUD_INVCAP;
@@ -929,14 +1028,35 @@
 
 
-/* API: set capability */
+/*
+ * API: set capability
+ * Currently just supporting toggle between speaker and earpiece
+ */
 static pj_status_t bb10_stream_set_cap(pjmedia_aud_stream *strm,
                                        pjmedia_aud_dev_cap cap,
                                        const void *value)
 {
-    PJ_UNUSED_ARG(strm);
-    PJ_UNUSED_ARG(cap);
-    PJ_UNUSED_ARG(value);
-
-    return PJMEDIA_EAUD_INVCAP;
+    pj_status_t ret = PJ_SUCCESS;
+    struct bb10_stream *stream = (struct bb10_stream*)strm;
+
+    if (cap != PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE || value == NULL) {
+        TRACE_((THIS_FILE,"bb10_stream_set_cap() = PJMEDIA_EAUD_INVCAP"));
+        return PJMEDIA_EAUD_INVCAP; 
+
+    } else {
+    	pjmedia_aud_dev_route route = *((pjmedia_aud_dev_route*)value);
+        /* Use the initialization function which lazy-inits the
+         * handle for routing
+         */
+    	if (route == PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER) {
+            ret = bb10_initialize_playback_ctrl(stream,true);
+        } else {
+            ret = bb10_initialize_playback_ctrl(stream,false);        	
+        }
+    }
+
+    if (ret != PJ_SUCCESS) {
+        TRACE_((THIS_FILE,"bb10_stream_set_cap() = %d",ret));
+    }
+    return ret;
 }
 
