display_speaker_controller.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/audio/display_speaker_controller.h"
  5. #include "ash/shell.h"
  6. #include "chromeos/ash/components/audio/cras_audio_handler.h"
  7. #include "third_party/cros_system_api/dbus/service_constants.h"
  8. #include "ui/display/manager/display_manager.h"
  9. #include "ui/display/manager/managed_display_info.h"
  10. #include "ui/display/util/display_util.h"
  11. inline cras::DisplayRotation ToCRASDisplayRotation(
  12. display::Display::Rotation rotation) {
  13. switch (rotation) {
  14. case display::Display::ROTATE_0:
  15. return cras::DisplayRotation::ROTATE_0;
  16. case display::Display::ROTATE_90:
  17. return cras::DisplayRotation::ROTATE_90;
  18. case display::Display::ROTATE_180:
  19. return cras::DisplayRotation::ROTATE_180;
  20. case display::Display::ROTATE_270:
  21. return cras::DisplayRotation::ROTATE_270;
  22. };
  23. }
  24. namespace ash {
  25. DisplaySpeakerController::DisplaySpeakerController() {
  26. chromeos::PowerManagerClient::Get()->AddObserver(this);
  27. }
  28. DisplaySpeakerController::~DisplaySpeakerController() {
  29. chromeos::PowerManagerClient::Get()->RemoveObserver(this);
  30. }
  31. void DisplaySpeakerController::OnDisplayAdded(
  32. const display::Display& new_display) {
  33. if (!new_display.IsInternal())
  34. return;
  35. UpdateInternalSpeakerForDisplayRotation();
  36. // This event will be triggered when the lid of the device is opened to exit
  37. // the docked mode, we should always start or re-start HDMI re-discovering
  38. // grace period right after this event.
  39. CrasAudioHandler::Get()->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
  40. }
  41. void DisplaySpeakerController::OnDisplayRemoved(
  42. const display::Display& old_display) {
  43. if (!old_display.IsInternal())
  44. return;
  45. UpdateInternalSpeakerForDisplayRotation();
  46. // This event will be triggered when the lid of the device is closed to enter
  47. // the docked mode, we should always start or re-start HDMI re-discovering
  48. // grace period right after this event.
  49. CrasAudioHandler::Get()->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
  50. }
  51. void DisplaySpeakerController::OnDisplayMetricsChanged(
  52. const display::Display& display,
  53. uint32_t changed_metrics) {
  54. if (!display.IsInternal())
  55. return;
  56. if (changed_metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION) {
  57. UpdateInternalSpeakerForDisplayRotation();
  58. }
  59. // The event could be triggered multiple times during the HDMI display
  60. // transition, we don't need to restart HDMI re-discovering grace period
  61. // it is already started earlier.
  62. CrasAudioHandler::Get()->SetActiveHDMIOutoutRediscoveringIfNecessary(false);
  63. }
  64. void DisplaySpeakerController::SuspendDone(base::TimeDelta sleep_duration) {
  65. // This event is triggered when the device resumes after earlier suspension,
  66. // we should always start or re-start HDMI re-discovering
  67. // grace period right after this event.
  68. CrasAudioHandler::Get()->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
  69. }
  70. void DisplaySpeakerController::UpdateInternalSpeakerForDisplayRotation() {
  71. // Swap left/right channel only if it is in Yoga mode.
  72. bool swap = false;
  73. if (display::HasInternalDisplay()) {
  74. const display::ManagedDisplayInfo& display_info =
  75. Shell::Get()->display_manager()->GetDisplayInfo(
  76. display::Display::InternalDisplayId());
  77. display::Display::Rotation rotation = display_info.GetActiveRotation();
  78. if (rotation == display::Display::ROTATE_180)
  79. swap = true;
  80. CrasAudioHandler::Get()->SetDisplayRotation(
  81. ToCRASDisplayRotation(rotation));
  82. }
  83. CrasAudioHandler::Get()->SwapInternalSpeakerLeftRightChannel(swap);
  84. }
  85. } // namespace ash