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);
41 sp<android::AudioTrack> track =
42 new android::AudioTrack(AUDIO_STREAM_MUSIC, trackFreq, AUDIO_FORMAT_PCM_16_BIT,
43 channelType, (size_t) 0 /*frameCount*/,
44 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST,
45 NULL /*callback_t*/, NULL /*void* user*/, 0 /*notificationFrames*/,
46 AUDIO_SESSION_ALLOCATE, android::AudioTrack::TRANSFER_SYNC);
47 assert(track != NULL);
49 BtifAvrcpAudioTrack *trackHolder = new BtifAvrcpAudioTrack;
50 assert(trackHolder != NULL);
51 trackHolder->track = track;
53 if (trackHolder->track->initCheck() != 0)
58 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
59 outputPcmSampleFile = fopen(outputFilename, "ab");
61 trackHolder->track->setVolume(1, 1);
62 return (void *)trackHolder;
65 void BtifAvrcpAudioTrackStart(void *handle)
67 assert(handle != NULL);
68 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
69 assert(trackHolder != NULL);
70 assert(trackHolder->track != NULL);
71 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
72 trackHolder->track->start();
75 void BtifAvrcpAudioTrackStop(void *handle)
78 LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__);
81 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
82 if (trackHolder != NULL && trackHolder->track != NULL) {
83 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
84 trackHolder->track->stop();
88 void BtifAvrcpAudioTrackDelete(void *handle)
91 LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__);
94 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
95 if (trackHolder != NULL && trackHolder->track != NULL) {
96 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
100 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
101 if (outputPcmSampleFile)
103 fclose(outputPcmSampleFile);
105 outputPcmSampleFile = NULL;
109 void BtifAvrcpAudioTrackPause(void *handle)
111 if (handle == NULL) {
112 LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__);
115 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
116 if (trackHolder != NULL && trackHolder->track != NULL) {
117 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
118 trackHolder->track->pause();
119 trackHolder->track->flush();
123 void BtifAvrcpSetAudioTrackGain(void *handle, float gain)
125 if (handle == NULL) {
126 LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__);
129 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
130 if (trackHolder != NULL && trackHolder->track != NULL) {
131 LOG_VERBOSE(LOG_TAG, "%s set gain %f", __func__, gain);
132 trackHolder->track->setVolume(gain);
136 int BtifAvrcpAudioTrackWriteData(void *handle, void *audioBuffer, int bufferlen)
138 BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
139 assert(trackHolder != NULL);
140 assert(trackHolder->track != NULL);
142 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
143 if (outputPcmSampleFile)
145 fwrite ((audioBuffer), 1, (size_t)bufferlen, outputPcmSampleFile);
148 retval = trackHolder->track->write(audioBuffer, (size_t)bufferlen);
149 LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btWriteData len = %d ret = %d",
150 __func__, bufferlen, retval);