2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #include "btif_avrcp_audio_track.h"
20 #include <media/AudioTrack.h>
21 #include <utils/StrongPointer.h>
23 #include "osi/include/log.h"
25 using namespace android;
28 android::sp<android::AudioTrack> track;
29 } BtifAvrcpAudioTrack;
31 //#define DUMP_PCM_DATA TRUE
32 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
33 FILE *outputPcmSampleFile;
34 char outputFilename[50] = "/data/misc/bluedroid/output_sample.pcm";
37 void *BtifAvrcpAudioTrackCreate(int trackFreq, int channelType)
39 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btCreateTrack freq %d channel %d ",
40 __func__, trackFreq, channelType);
42 sp<android::AudioTrack> track =
43 new android::AudioTrack(AUDIO_STREAM_MUSIC, trackFreq, AUDIO_FORMAT_PCM_16_BIT,
44 channelType, (int)0, (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST,
45 NULL, NULL, 0, 0, android::AudioTrack::TRANSFER_SYNC);
46 assert(track != NULL);
48 BtifAvrcpAudioTrack *trackHolder = new BtifAvrcpAudioTrack;
50 trackHolder->track = track;
52 if (trackHolder->track->initCheck() != 0)
57 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
58 outputPcmSampleFile = fopen(outputFilename, "ab");
60 trackHolder->track->setVolume(1, 1);
61 return (void *)trackHolder;
64 void BtifAvrcpAudioTrackStart(void *handle)
66 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
68 assert(trackHolder->track != NULL);
69 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
70 trackHolder->track->start();
73 void BtifAvrcpAudioTrackStop(void *handle)
75 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
76 if (trackHolder != NULL && trackHolder->track != NULL) {
77 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
78 trackHolder->track->stop();
82 void BtifAvrcpAudioTrackDelete(void *handle)
84 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
85 if (trackHolder != NULL && trackHolder->track != NULL) {
86 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
90 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
91 if (outputPcmSampleFile)
93 fclose(outputPcmSampleFile);
95 outputPcmSampleFile = NULL;
99 void BtifAvrcpAudioTrackPause(void *handle)
101 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
102 if (trackHolder != NULL && trackHolder->track != NULL) {
103 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
104 trackHolder->track->pause();
105 trackHolder->track->flush();
109 int BtifAvrcpAudioTrackWriteData(void *handle, void *audioBuffer, int bufferlen)
111 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
113 assert(trackHolder->track != NULL);
115 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
116 if (outputPcmSampleFile)
118 fwrite ((audioBuffer), 1, (size_t)bufferlen, outputPcmSampleFile);
121 retval = trackHolder->track->write(audioBuffer, (size_t)bufferlen);
122 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btWriteData len = %d ret = %d",
123 __func__, bufferlen, retval);