Index: pjproject/trunk/pjsip-apps/src/samples/pjsua2_demo.cpp
===================================================================
--- pjproject/trunk/pjsip-apps/src/samples/pjsua2_demo.cpp	(revision 5985)
+++ pjproject/trunk/pjsip-apps/src/samples/pjsua2_demo.cpp	(revision 5986)
@@ -31,4 +31,5 @@
 private:
     MyAccount *myAcc;
+    AudioMediaPlayer *wav_player;
 
 public:
@@ -36,5 +37,12 @@
     : Call(acc, call_id)
     {
+    	wav_player = NULL;
         myAcc = (MyAccount *)&acc;
+    }
+    
+    ~MyCall()
+    {
+    	if (wav_player)
+    	    delete wav_player;
     }
     
@@ -42,4 +50,5 @@
     virtual void onCallTransferRequest(OnCallTransferRequestParam &prm);
     virtual void onCallReplaced(OnCallReplacedParam &prm);
+    virtual void onCallMediaState(OnCallMediaStateParam &prm);
 };
 
@@ -115,4 +124,42 @@
 }
 
+void MyCall::onCallMediaState(OnCallMediaStateParam &prm)
+{
+    PJ_UNUSED_ARG(prm);
+
+    unsigned i;
+    CallInfo ci = getInfo();
+    AudioMedia aud_med;
+    AudioMedia& play_dev_med =
+    	Endpoint::instance().audDevManager().getPlaybackDevMedia();
+
+    try {
+    	// Get the first audio media
+    	aud_med = getAudioMedia(-1);
+    } catch(...) {
+	std::cout << "Failed to get audio media" << std::endl;
+	return;
+    }
+
+    if (!wav_player) {
+    	wav_player = new AudioMediaPlayer();
+   	try {
+   	    wav_player->createPlayer(
+   	    	"../../../../tests/pjsua/wavs/input.16.wav", 0);
+   	} catch (...) {
+	    std::cout << "Failed opening wav file" << std::endl;
+	    delete wav_player;
+	    wav_player = NULL;
+    	}
+    }
+
+    // This will connect the wav file to the call audio media
+    if (wav_player)
+    	wav_player->startTransmit(aud_med);
+
+    // And this will connect the call audio media to the sound device/speaker
+    aud_med.startTransmit(play_dev_med);
+}
+
 void MyCall::onCallTransferRequest(OnCallTransferRequestParam &prm)
 {
@@ -164,5 +211,5 @@
     
     // Hangup all calls
-    pj_thread_sleep(8000);
+    pj_thread_sleep(4000);
     ep.hangupAllCalls();
     pj_thread_sleep(4000);
Index: pjproject/trunk/pjsip/src/pjsua2/call.cpp
===================================================================
--- pjproject/trunk/pjsip/src/pjsua2/call.cpp	(revision 5985)
+++ pjproject/trunk/pjsip/src/pjsua2/call.cpp	(revision 5986)
@@ -861,6 +861,8 @@
 	    /* Clear medias. */
 	    for (mi = 0; mi < medias.size(); mi++) {
-		if (medias[mi])
+		if (medias[mi]) {
+		    Endpoint::instance().mediaRemove((AudioMedia&)*medias[mi]);
 		    delete medias[mi];
+		}
 	    }
 	    medias.clear();	
@@ -923,6 +925,8 @@
         /* Clear medias. */
         for (mi = 0; mi < medias.size(); mi++) {
-            if (medias[mi])
+            if (medias[mi]) {
+		Endpoint::instance().mediaRemove((AudioMedia &)*medias[mi]);
                 delete medias[mi];
+            }
         }
         medias.clear();
Index: pjproject/trunk/pjsip/src/pjsua2/endpoint.cpp
===================================================================
--- pjproject/trunk/pjsip/src/pjsua2/endpoint.cpp	(revision 5985)
+++ pjproject/trunk/pjsip/src/pjsua2/endpoint.cpp	(revision 5986)
@@ -503,9 +503,4 @@
 	delete pendingJobs.front();
 	pendingJobs.pop_front();
-    }
-
-    while(mediaList.size() > 0) {
-	AudioMedia *cur_media = mediaList[0];
-	delete cur_media; /* this will remove itself from the list */
     }
 
@@ -1788,4 +1783,9 @@
     }
 
+    while(mediaList.size() > 0) {
+	AudioMedia *cur_media = mediaList[0];
+	delete cur_media; /* this will remove itself from the list */
+    }
+
     if (mediaListMutex) {
     	pj_mutex_destroy(mediaListMutex);
@@ -2089,10 +2089,10 @@
     pj_mutex_lock(mediaListMutex);
 
-    if (mediaExists(media)) {
-	pj_mutex_unlock(mediaListMutex);
-	return;
-    }
-
-    mediaList.push_back(&media);
+    AudioMediaVector::iterator it = std::find(mediaList.begin(),
+					      mediaList.end(),
+					      &media);
+
+    if (it == mediaList.end())
+    	mediaList.push_back(&media);
     pj_mutex_unlock(mediaListMutex);
 }
Index: pjproject/trunk/pjsip/src/pjsua2/media.cpp
===================================================================
--- pjproject/trunk/pjsip/src/pjsua2/media.cpp	(revision 5985)
+++ pjproject/trunk/pjsip/src/pjsua2/media.cpp	(revision 5986)
@@ -132,7 +132,5 @@
 {
     /* Check if media already added to Conf bridge. */
-    pj_assert(!Endpoint::instance().mediaExists(*this));
-
-    if (port != NULL) {
+    if (!Endpoint::instance().mediaExists(*this) && port != NULL) {
 	pj_assert(id == PJSUA_INVALID_ID);
 
@@ -664,4 +662,5 @@
     /* Avoid removing this port (conf port id=0) from conference */
     this->id = PJSUA_INVALID_ID;
+    unregisterMediaPort();
 }
 
