Bug 1222564 - Save audio volume to database r=alwu
authorSotaro Ikeda <sotaro.ikeda.g@gmail.com>
Tue, 10 Nov 2015 04:28:03 -0800
changeset 271928 7ae90a04b0fd9ed59c96fb60f1b8da0ddb0cbf93
parent 271927 ed4a12fe9c82cddc27b7eed4e5c27063dc2ef98c
child 271929 a675fc80caa9e744573d199d5d52cd84ab75ac7e
push id67796
push usersikeda@mozilla.com
push dateTue, 10 Nov 2015 12:28:17 +0000
treeherdermozilla-inbound@7ae90a04b0fd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersalwu
bugs1222564
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1222564 - Save audio volume to database r=alwu
dom/system/gonk/AudioManager.cpp
dom/system/gonk/AudioManager.h
--- a/dom/system/gonk/AudioManager.cpp
+++ b/dom/system/gonk/AudioManager.cpp
@@ -1032,16 +1032,17 @@ AudioManager::SetStreamVolumeIndex(int32
     rv = mStreamStates[AUDIO_STREAM_FM]->
       SetVolumeIndex(aIndex, AUDIO_DEVICE_OUT_FM);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 #endif
 
+  MaybeUpdateVolumeSettingToDatabase();
   return NS_OK;
 }
 
 nsresult
 AudioManager::GetStreamVolumeIndex(int32_t aStream, uint32_t *aIndex)
 {
   if (!aIndex) {
     return NS_ERROR_INVALID_ARG;
@@ -1147,35 +1148,29 @@ AudioManager::MaybeUpdateVolumeSettingTo
       continue;
     }
     // Get volume index of active device.
     volume = streamState->GetVolumeIndex();
     value.setInt32(volume);
     lock->Set(gVolumeData[idx].mChannelName, value, nullptr, nullptr);
   }
 
-  // Clear device changed flags
-  for (uint32_t idx = 0; idx < MOZ_ARRAY_LENGTH(gVolumeData); ++idx) {
-    int32_t  streamType = gVolumeData[idx].mStreamType;
-    mStreamStates[streamType]->ClearDevicesChanged();
-  }
-
-  if (!mAudioOutProfileUpdated) {
-    return;
-  }
-
   // For reducing the code dependency, Gaia doesn't need to know the
   // profile volume, it only need to care about different volume categories.
   // However, we need to send the setting volume to the permanent database,
   // so that we can store the volume setting even if the phone reboots.
 
   for (uint32_t idx = 0; idx < MOZ_ARRAY_LENGTH(gVolumeData); ++idx) {
     int32_t  streamType = gVolumeData[idx].mStreamType;
     VolumeStreamState* streamState = mStreamStates[streamType].get();
 
+    if(!streamState->IsVolumeIndexesChanged()) {
+        continue;
+    }
+
     if (mAudioOutProfileUpdated & (1 << DEVICE_PRIMARY)) {
       volume = streamState->GetVolumeIndex(AUDIO_DEVICE_OUT_SPEAKER);
       value.setInt32(volume);
       lock->Set(AppendProfileToVolumeSetting(
                   gVolumeData[idx].mChannelName,
                   DEVICE_PRIMARY).get(),
                   value, nullptr, nullptr);
     }
@@ -1192,16 +1187,22 @@ AudioManager::MaybeUpdateVolumeSettingTo
       value.setInt32(volume);
       lock->Set(AppendProfileToVolumeSetting(
                   gVolumeData[idx].mChannelName,
                   DEVICE_BLUETOOTH).get(),
                   value, nullptr, nullptr);
     }
   }
 
+  // Clear changed flags
+  for (uint32_t idx = 0; idx < MOZ_ARRAY_LENGTH(gVolumeData); ++idx) {
+    int32_t  streamType = gVolumeData[idx].mStreamType;
+    mStreamStates[streamType]->ClearDevicesChanged();
+    mStreamStates[streamType]->ClearVolumeIndexesChanged();
+  }
   // Clear mAudioOutProfileUpdated
   mAudioOutProfileUpdated = 0;
 }
 
 void
 AudioManager::InitVolumeForProfile(AudioOutputProfiles aProfile,
                                    int32_t aStreamType,
                                    uint32_t aIndex)
@@ -1290,16 +1291,17 @@ AudioManager::SelectDeviceFromDevices(ui
   return device;
 }
 AudioManager::VolumeStreamState::VolumeStreamState(AudioManager& aManager,
                                                    int32_t aStreamType)
   : mManager(aManager)
   , mStreamType(aStreamType)
   , mLastDevices(0)
   , mIsDevicesChanged(true)
+  , mIsVolumeIndexesChanged(true)
 {
   InitStreamVolume();
 }
 
 bool
 AudioManager::VolumeStreamState::IsDevicesChanged(bool aFromCache)
 {
   uint32_t devices = mManager.GetDevicesForStream(mStreamType, aFromCache);
@@ -1311,16 +1313,28 @@ AudioManager::VolumeStreamState::IsDevic
 }
 
 void
 AudioManager::VolumeStreamState::ClearDevicesChanged()
 {
   mIsDevicesChanged = false;
 }
 
+bool
+AudioManager::VolumeStreamState::IsVolumeIndexesChanged()
+{
+  return mIsVolumeIndexesChanged;
+}
+
+void
+AudioManager::VolumeStreamState::ClearVolumeIndexesChanged()
+{
+  mIsVolumeIndexesChanged = false;
+}
+
 void
 AudioManager::VolumeStreamState::InitStreamVolume()
 {
   AudioSystem::initStreamVolume(static_cast<audio_stream_type_t>(mStreamType),
                                 0,
                                 GetMaxIndex());
 }
 
@@ -1482,26 +1496,28 @@ nsresult
 AudioManager::VolumeStreamState::SetVolumeIndex(uint32_t aIndex,
                                                 uint32_t aDevice,
                                                 bool aUpdateCache)
 {
   status_t rv;
 #if ANDROID_VERSION >= 17
   if (aUpdateCache) {
     mVolumeIndexes.Put(aDevice, aIndex);
+    mIsVolumeIndexesChanged = true;
   }
 
   rv = AudioSystem::setStreamVolumeIndex(
          static_cast<audio_stream_type_t>(mStreamType),
          aIndex,
          aDevice);
   return rv ? NS_ERROR_FAILURE : NS_OK;
 #else
   if (aUpdateCache) {
     mVolumeIndexes.Put(AUDIO_DEVICE_OUT_DEFAULT, aIndex);
+    mIsVolumeIndexesChanged = true;
   }
   rv = AudioSystem::setStreamVolumeIndex(
          static_cast<audio_stream_type_t>(mStreamType),
          aIndex);
   return rv ? NS_ERROR_FAILURE : NS_OK;
 #endif
 }
 
--- a/dom/system/gonk/AudioManager.h
+++ b/dom/system/gonk/AudioManager.h
@@ -83,16 +83,18 @@ public:
       return mStreamType;
     }
     bool IsDevicesChanged(bool aFromCache = true);
     void ClearDevicesChanged();
     uint32_t GetLastDevices()
     {
       return mLastDevices;
     }
+    bool IsVolumeIndexesChanged();
+    void ClearVolumeIndexesChanged();
     void InitStreamVolume();
     uint32_t GetMaxIndex();
     uint32_t GetDefaultIndex();
     uint32_t GetVolumeIndex();
     uint32_t GetVolumeIndex(uint32_t aDevice);
     void ClearCurrentVolumeUpdated();
     // Set volume index to all active devices.
     // Active devices are chosen by android AudioPolicyManager.
@@ -106,16 +108,17 @@ public:
     nsresult SetVolumeIndex(uint32_t aIndex, uint32_t aDevice, bool aUpdateCache = true);
     // Restore volume index to all devices. Called when AudioFlinger is restarted.
     void RestoreVolumeIndexToAllDevices();
   private:
     AudioManager& mManager;
     const int32_t mStreamType;
     uint32_t mLastDevices;
     bool mIsDevicesChanged;
+    bool mIsVolumeIndexesChanged;
     nsDataHashtable<nsUint32HashKey, uint32_t> mVolumeIndexes;
   };
 
 protected:
   int32_t mPhoneState;
 
   bool mIsVolumeInited;