Adjust sound volume depending on whether a headset is connected or not
authorMohammed Sameer <msameer@foolab.org>
Mon, 4 Feb 2013 23:54:00 +0000 (01:54 +0200)
committerMohammed Sameer <msameer@foolab.org>
Mon, 4 Feb 2013 23:54:00 +0000 (01:54 +0200)
declarative/sounds.cpp
declarative/sounds.h

index fdf559f..af49bb8 100644 (file)
@@ -25,6 +25,7 @@
 #include <QWaitCondition>
 #include <QDBusServiceWatcher>
 #include <QDBusConnection>
+#include <contextsubscriber/contextproperty.h>
 
 #define CAMERA_IMAGE_START_SOUND_ID  "camera-image-start"
 #define CAMERA_IMAGE_END_SOUND_ID    "camera-image-end"
 #define CAMERA_FOCUS_SOUND_ID        "camera-focus"
 
 // Odd, volume has to be a char *
-#define CANBERRA_FULL_VOLUME "0.0"
-
-// TODO: if we are using headphones then sound volume might be loud. Detect and lower it.
+#define CANBERRA_FULL_VOLUME         "0.0"
+#define CANBERRA_HEADSET_VOLUME      "-24.0"
+#define AUDIO_ROUTE_PROPERTY         "/com/nokia/policy/audio_route"
+#define AUDIO_ROUTE_SPEAKERS         "ihf"
 
 Sounds::Sounds(QObject *parent) :
   QObject(parent),
@@ -53,6 +55,11 @@ Sounds::Sounds(QObject *parent) :
 
   // No idea why but canberra will not cache without that!!!
   setenv("CANBERRA_EVENT_LOOKUP", "1", 1);
+
+  m_audioRoute = new ContextProperty(AUDIO_ROUTE_PROPERTY, this);
+  QObject::connect(m_audioRoute, SIGNAL(valueChanged()), this, SLOT(audioConnectionChanged()));
+  m_audioRoute->waitForSubscription(true);
+  audioConnectionChanged();
 }
 
 Sounds::~Sounds() {
@@ -197,7 +204,7 @@ void Sounds::play(const char *id) {
   }
 
   int code = ca_context_play(m_ctx, 0,
-                            CA_PROP_CANBERRA_VOLUME, CANBERRA_FULL_VOLUME,
+                            CA_PROP_CANBERRA_VOLUME, m_volume.toAscii().constData(),
                             CA_PROP_EVENT_ID, id,
                             CA_PROP_MEDIA_ROLE, "camera-sound-effect",
                             NULL);
@@ -248,3 +255,11 @@ void Sounds::playAndBlock(const char *id) {
   ca_proplist_destroy(p);
   mutex.unlock();
 }
+
+void Sounds::audioConnectionChanged() {
+  if (m_audioRoute->value().toString() != AUDIO_ROUTE_SPEAKERS) {
+    m_volume = CANBERRA_HEADSET_VOLUME;
+  } else {
+    m_volume = CANBERRA_FULL_VOLUME;
+  }
+}
index 27dbae3..dd08973 100644 (file)
@@ -29,6 +29,7 @@
 
 class QtCamConfig;
 class QDBusServiceWatcher;
+class ContextProperty;
 
 class Sounds : public QObject, public DeclarativeQtCameraNotifications {
   Q_OBJECT
@@ -58,6 +59,7 @@ signals:
 private slots:
   void serviceOwnerChanged(const QString& serviceName, const QString& oldOwner,
                           const QString& newOwner);
+  void audioConnectionChanged();
 
 private:
   void cache(const QString& path, const char *id);
@@ -68,6 +70,8 @@ private:
   ca_context *m_ctx;
   QtCamConfig *m_conf;
   QDBusServiceWatcher *m_watcher;
+  ContextProperty *m_audioRoute;
+  QString m_volume;
 };
 
 #endif /* SOUNDS_H */