TrackPlayerBase.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <media/TrackPlayerBase.h>
  17. namespace android {
  18. using media::VolumeShaper;
  19. //--------------------------------------------------------------------------------------------------
  20. TrackPlayerBase::TrackPlayerBase() : PlayerBase(),
  21. mPlayerVolumeL(1.0f), mPlayerVolumeR(1.0f)
  22. {
  23. ALOGD("TrackPlayerBase::TrackPlayerBase()");
  24. }
  25. TrackPlayerBase::~TrackPlayerBase() {
  26. ALOGD("TrackPlayerBase::~TrackPlayerBase()");
  27. doDestroy();
  28. }
  29. void TrackPlayerBase::init(AudioTrack* pat, player_type_t playerType, audio_usage_t usage) {
  30. PlayerBase::init(playerType, usage);
  31. mAudioTrack = pat;
  32. }
  33. void TrackPlayerBase::destroy() {
  34. doDestroy();
  35. baseDestroy();
  36. }
  37. void TrackPlayerBase::doDestroy() {
  38. if (mAudioTrack != 0) {
  39. mAudioTrack->stop();
  40. // Note that there may still be another reference in post-unlock phase of SetPlayState
  41. mAudioTrack.clear();
  42. }
  43. }
  44. void TrackPlayerBase::setPlayerVolume(float vl, float vr) {
  45. {
  46. Mutex::Autolock _l(mSettingsLock);
  47. mPlayerVolumeL = vl;
  48. mPlayerVolumeR = vr;
  49. }
  50. doSetVolume();
  51. }
  52. //------------------------------------------------------------------------------
  53. // Implementation of IPlayer
  54. status_t TrackPlayerBase::playerStart() {
  55. status_t status = NO_INIT;
  56. if (mAudioTrack != 0) {
  57. status = mAudioTrack->start();
  58. }
  59. return status;
  60. }
  61. status_t TrackPlayerBase::playerPause() {
  62. status_t status = NO_INIT;
  63. if (mAudioTrack != 0) {
  64. mAudioTrack->pause();
  65. status = NO_ERROR;
  66. }
  67. return status;
  68. }
  69. status_t TrackPlayerBase::playerStop() {
  70. status_t status = NO_INIT;
  71. if (mAudioTrack != 0) {
  72. mAudioTrack->stop();
  73. status = NO_ERROR;
  74. }
  75. return status;
  76. }
  77. status_t TrackPlayerBase::playerSetVolume() {
  78. return doSetVolume();
  79. }
  80. status_t TrackPlayerBase::doSetVolume() {
  81. status_t status = NO_INIT;
  82. if (mAudioTrack != 0) {
  83. float tl = mPlayerVolumeL * mPanMultiplierL * mVolumeMultiplierL;
  84. float tr = mPlayerVolumeR * mPanMultiplierR * mVolumeMultiplierR;
  85. mAudioTrack->setVolume(tl, tr);
  86. status = NO_ERROR;
  87. }
  88. return status;
  89. }
  90. binder::Status TrackPlayerBase::applyVolumeShaper(
  91. const VolumeShaper::Configuration& configuration,
  92. const VolumeShaper::Operation& operation) {
  93. sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration);
  94. sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation);
  95. if (mAudioTrack != 0) {
  96. ALOGD("TrackPlayerBase::applyVolumeShaper() from IPlayer");
  97. VolumeShaper::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation);
  98. if (status < 0) { // a non-negative value is the volume shaper id.
  99. ALOGE("TrackPlayerBase::applyVolumeShaper() failed with status %d", status);
  100. }
  101. return binder::Status::fromStatusT(status);
  102. } else {
  103. ALOGD("TrackPlayerBase::applyVolumeShaper()"
  104. " no AudioTrack for volume control from IPlayer");
  105. return binder::Status::ok();
  106. }
  107. }
  108. } // namespace android