WMF: each media player now has its own volume.
authorYoann Lopes <yoann.lopes@digia.com>
Tue, 4 Mar 2014 16:22:47 +0000 (17:22 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 7 Mar 2014 14:12:13 +0000 (15:12 +0100)
Instead of setting the volume on the audio session, which is shared by
all QMediaPlayers, we now set the volume on the media player's own audio
stream. This results in all QMediaPlayers correctly having independent
volumes.

[ChangeLog][QtMultimedia][Windows] QMediaPlayer::setVolume() does not
affect the volume of other QMediaPlayers anymore.

Task-number: QTBUG-30317
Change-Id: I8ea8ec47fc86127da01dc5c8247fb6f72c834630
Reviewed-by: Wouter Huysentruit <wouter_huysentruit@hotmail.com>
Reviewed-by: Andy Nichols <andy.nichols@digia.com>
src/plugins/wmf/player/mfplayersession.cpp
src/plugins/wmf/player/mfplayersession.h

index 183f023..6995806 100644 (file)
@@ -1325,8 +1325,10 @@ void MFPlayerSession::setVolume(int volume)
     if (m_volume == volume)
         return;
     m_volume = volume;
-    if (m_volumeControl)
-        m_volumeControl->SetMasterVolume(m_volume * 0.01f);
+
+    if (!m_muted)
+        setVolumeInternal(volume);
+
     emit volumeChanged(m_volume);
 }
 
@@ -1340,11 +1342,26 @@ void MFPlayerSession::setMuted(bool muted)
     if (m_muted == muted)
         return;
     m_muted = muted;
-    if (m_volumeControl)
-        m_volumeControl->SetMute(BOOL(m_muted));
+
+    setVolumeInternal(muted ? 0 : m_volume);
+
     emit mutedChanged(m_muted);
 }
 
+void MFPlayerSession::setVolumeInternal(int volume)
+{
+    if (m_volumeControl) {
+        quint32 channelCount = 0;
+        if (!SUCCEEDED(m_volumeControl->GetChannelCount(&channelCount))
+                || channelCount == 0)
+            return;
+
+        float scaled = volume * 0.01f;
+        for (quint32 i = 0; i < channelCount; ++i)
+            m_volumeControl->SetChannelVolume(i, scaled);
+    }
+}
+
 int MFPlayerSession::bufferStatus()
 {
     if (!m_netsourceStatistics)
@@ -1570,10 +1587,8 @@ void MFPlayerSession::handleSessionEvent(IMFMediaEvent *sessionEvent)
                 }
             }
 
-            if (SUCCEEDED(MFGetService(m_session, MR_POLICY_VOLUME_SERVICE, IID_PPV_ARGS(&m_volumeControl)))) {
-                m_volumeControl->SetMasterVolume(m_volume * 0.01f);
-                m_volumeControl->SetMute(m_muted);
-            }
+            if (SUCCEEDED(MFGetService(m_session, MR_STREAM_VOLUME_SERVICE, IID_PPV_ARGS(&m_volumeControl))))
+                setVolumeInternal(m_muted ? 0 : m_volume);
 
             DWORD dwCharacteristics = 0;
             m_sourceResolver->mediaSource()->GetCharacteristics(&dwCharacteristics);
@@ -1619,25 +1634,6 @@ void MFPlayerSession::handleSessionEvent(IMFMediaEvent *sessionEvent)
         break;
     case MEEndOfPresentationSegment:
         break;
-    case MEAudioSessionVolumeChanged:
-        if (m_volumeControl) {
-            float currentVolume = 1;
-            if (SUCCEEDED(m_volumeControl->GetMasterVolume(&currentVolume))) {
-                int scaledVolume = currentVolume * 100;
-                if (scaledVolume != m_volume) {
-                    m_volume = scaledVolume;
-                    emit volumeChanged(scaledVolume);
-                }
-            }
-            BOOL currentMuted = FALSE;
-            if (SUCCEEDED(m_volumeControl->GetMute(&currentMuted))) {
-                if (currentMuted != BOOL(m_muted)) {
-                    m_muted = bool(currentMuted);
-                    emit mutedChanged(m_muted);
-                }
-            }
-        }
-        break;
     case MESessionTopologyStatus: {
             UINT32 status;
             if (SUCCEEDED(sessionEvent->GetUINT32(MF_EVENT_TOPOLOGY_STATUS, &status))) {
index e7f8dcf..3ba43ce 100644 (file)
@@ -152,7 +152,7 @@ private:
     IMFPresentationClock *m_presentationClock;
     IMFRateControl *m_rateControl;
     IMFRateSupport *m_rateSupport;
-    IMFSimpleAudioVolume *m_volumeControl;
+    IMFAudioStreamVolume *m_volumeControl;
     IPropertyStore *m_netsourceStatistics;
     PROPVARIANT m_varStart;
     UINT64 m_duration;
@@ -218,6 +218,8 @@ private:
     int m_volume;
     bool m_muted;
 
+    void setVolumeInternal(int volume);
+
     void createSession();
     void setupPlaybackTopology(IMFMediaSource *source, IMFPresentationDescriptor *sourcePD);
     IMFTopologyNode* addSourceNode(IMFTopology* topology, IMFMediaSource* source,