1 /* 2 * Copyright (C) 2022 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 17 #pragma once 18 19 #ifndef SL_PREFETCHEVENT_NONE // This is defined in slesl_allinclusive, which isn't guarded 20 #include "sles_allinclusive.h" 21 #endif 22 23 #include "media/AudioTrack.h" 24 25 void audioPlayer_dispatch_headAtEnd_lockPlay(CAudioPlayer*, bool, bool); 26 27 void audioTrack_handleUnderrun_lockPlay(CAudioPlayer* ap); 28 void audioTrack_handleMarker_lockPlay(CAudioPlayer* ap); 29 void audioTrack_handleNewPos_lockPlay(CAudioPlayer* ap); 30 size_t audioTrack_handleMoreData_lockPlay(CAudioPlayer* ap, 31 const android::AudioTrack::Buffer& buffer); 32 //-------------------------------------------------------------------------------------------------- 33 namespace android { 34 class AudioTrackCallback : public AudioTrack::IAudioTrackCallback { 35 public: AudioTrackCallback(CAudioPlayer * player)36 AudioTrackCallback(CAudioPlayer * player) : mAp(player) {} 37 onMoreData(const AudioTrack::Buffer & buffer)38 size_t onMoreData(const AudioTrack::Buffer& buffer) override { 39 if (!android::CallbackProtector::enterCbIfOk(mAp->mCallbackProtector)) { 40 // it is not safe to enter the callback (the track is about to go away) 41 return buffer.size(); // duplicate existing behavior 42 } 43 size_t bytesCopied = audioTrack_handleMoreData_lockPlay(mAp, buffer); 44 mAp->mCallbackProtector->exitCb(); 45 return bytesCopied; 46 } 47 onUnderrun()48 void onUnderrun() override { 49 if (!android::CallbackProtector::enterCbIfOk(mAp->mCallbackProtector)) { 50 // it is not safe to enter the callback (the track is about to go away) 51 return; 52 } 53 audioTrack_handleUnderrun_lockPlay(mAp); 54 mAp->mCallbackProtector->exitCb(); 55 } 56 onLoopEnd(int32_t loopsRemaining)57 void onLoopEnd([[maybe_unused]] int32_t loopsRemaining) override { 58 SL_LOGE("Encountered loop end for CAudioPlayer %p", mAp); 59 } onMarker(uint32_t markerPosition)60 void onMarker([[maybe_unused]] uint32_t markerPosition) override { 61 if (!android::CallbackProtector::enterCbIfOk(mAp->mCallbackProtector)) { 62 // it is not safe to enter the callback (the track is about to go away) 63 return; 64 } 65 audioTrack_handleMarker_lockPlay(mAp); 66 mAp->mCallbackProtector->exitCb(); 67 } 68 onNewPos(uint32_t newPos)69 void onNewPos([[maybe_unused]] uint32_t newPos) override { 70 if (!android::CallbackProtector::enterCbIfOk(mAp->mCallbackProtector)) { 71 // it is not safe to enter the callback (the track is about to go away) 72 return; 73 } 74 audioTrack_handleNewPos_lockPlay(mAp); 75 mAp->mCallbackProtector->exitCb(); 76 } onBufferEnd()77 void onBufferEnd() override { 78 SL_LOGE("Encountered buffer end for CAudioPlayer %p", mAp); 79 } 80 // Ignore onNewIAudioTrack()81 void onNewIAudioTrack() override {} onStreamEnd()82 void onStreamEnd() override { 83 SL_LOGE("Encountered buffer end for CAudioPlayer %p", mAp); 84 } onNewTimestamp(AudioTimestamp timestamp)85 void onNewTimestamp([[maybe_unused]] AudioTimestamp timestamp) { 86 SL_LOGE("Encountered write more data for CAudioPlayer %p", mAp); 87 } onCanWriteMoreData(const AudioTrack::Buffer & buffer)88 size_t onCanWriteMoreData([[maybe_unused]] const AudioTrack::Buffer& buffer) { 89 SL_LOGE("Encountered write more data for CAudioPlayer %p", mAp); 90 return 0; 91 } 92 93 private: 94 AudioTrackCallback(const AudioTrackCallback&) = delete; 95 AudioTrackCallback& operator=(const AudioTrackCallback&) = delete; 96 CAudioPlayer* const mAp; 97 }; 98 } // namespace android 99