cec: added VolumeUp()/cec_volume_up(), VolumeDown()/cec_volume_down(), MuteAudio...
[deb_libcec.git] / src / lib / devices / CECAudioSystem.cpp
index d0ab39ebf15781ebbc6841cf5642d8372d0a926a..4a4f11fbe2434a898df529176772f57749f5c0f3 100644 (file)
  */
 
 #include "CECAudioSystem.h"
+#include "../CECProcessor.h"
+#include "../implementations/CECCommandHandler.h"
 
 using namespace CEC;
 
 CCECAudioSystem::CCECAudioSystem(CCECProcessor *processor, cec_logical_address address, uint16_t iPhysicalAddress /* = 0 */) :
-    CCECBusDevice(processor, address, iPhysicalAddress)
+    CCECBusDevice(processor, address, iPhysicalAddress),
+    m_systemAudioStatus(CEC_SYSTEM_AUDIO_STATUS_ON),
+    m_audioStatus(CEC_AUDIO_MUTE_STATUS_MASK)
 {
-  m_type          = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
-  m_strDeviceName = "Audio";
+  m_type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
+}
+
+bool CCECAudioSystem::SetAudioStatus(uint8_t status)
+{
+  if (m_audioStatus != status)
+  {
+    CStdString strLog;
+    strLog.Format(">> %s (%X): audio status changed from %2x to %2x", GetLogicalAddressName(), m_iLogicalAddress, m_audioStatus, status);
+    AddLog(CEC_LOG_DEBUG, strLog.c_str());
+
+    m_audioStatus = status;
+    return true;
+  }
+
+  return false;
+}
+
+bool CCECAudioSystem::SetSystemAudioMode(const cec_system_audio_status mode)
+{
+  if (m_systemAudioStatus != mode)
+  {
+    CStdString strLog;
+    strLog.Format(">> %s (%X): system audio mode changed from %s to %s", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(m_systemAudioStatus), CCECCommandHandler::ToString(mode));
+    AddLog(CEC_LOG_DEBUG, strLog.c_str());
+
+    m_systemAudioStatus = mode;
+    return true;
+  }
+
+  return false;
+}
+
+bool CCECAudioSystem::SetSystemAudioMode(const cec_command &command)
+{
+  SetSystemAudioMode((command.parameters.size == 0) ?
+    CEC_SYSTEM_AUDIO_STATUS_OFF :
+  CEC_SYSTEM_AUDIO_STATUS_ON);
+
+  return TransmitAudioStatus(command.initiator);
+}
+
+bool CCECAudioSystem::TransmitAudioStatus(cec_logical_address dest)
+{
+  CStdString strLog;
+  strLog.Format("<< %x -> %x: audio status '%2x'", m_iLogicalAddress, dest, m_audioStatus);
+  AddLog(CEC_LOG_NOTICE, strLog);
+
+  cec_command command;
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_AUDIO_STATUS);
+  command.parameters.PushBack(m_audioStatus);
+
+  return m_processor->Transmit(command);
+}
+
+bool CCECAudioSystem::TransmitSystemAudioModeStatus(cec_logical_address dest)
+{
+  CStdString strLog;
+  strLog.Format("<< %x -> %x: system audio mode '%s'", m_iLogicalAddress, dest, CCECCommandHandler::ToString(m_systemAudioStatus));
+  AddLog(CEC_LOG_NOTICE, strLog);
+
+  cec_command command;
+  cec_command::Format(command, m_iLogicalAddress, dest, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
+  command.parameters.PushBack((uint8_t) m_systemAudioStatus);
+
+  return m_processor->Transmit(command);
+}
+
+uint8_t CCECAudioSystem::VolumeUp(void)
+{
+  return SendKey(CEC_USER_CONTROL_CODE_VOLUME_UP);
+}
+
+uint8_t CCECAudioSystem::VolumeDown(void)
+{
+  return SendKey(CEC_USER_CONTROL_CODE_VOLUME_DOWN);
+}
+
+uint8_t CCECAudioSystem::MuteAudio(void)
+{
+  return SendKey(CEC_USER_CONTROL_CODE_MUTE);
+}
+
+uint8_t CCECAudioSystem::SendKey(cec_user_control_code key)
+{
+  {
+    CLockObject lock(&m_transmitMutex);
+    cec_command command;
+    cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_PRESSED);
+    command.parameters.PushBack(key);
+    m_processor->Transmit(command);
+
+    cec_command::Format(command, m_processor->GetLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_USER_CONTROL_RELEASE);
+    if (m_processor->Transmit(command))
+      m_condition.Wait(&m_transmitMutex, 1000);
+  }
+
+  CLockObject lock(&m_mutex);
+  return m_audioStatus;
 }