OSDN Git Service

Merge "Add Connect and Disconnect methods to LowEnergyClient"
[android-x86/system-bt.git] / btif / src / btif_avrcp_audio_track.cpp
1 /*
2  * Copyright (C) 2015 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 //#define LOG_NDEBUG 0
18 #include "btif_avrcp_audio_track.h"
19
20 #include <media/AudioTrack.h>
21 #include <utils/StrongPointer.h>
22
23 #include "osi/include/log.h"
24
25 using namespace android;
26
27 typedef struct {
28     android::sp<android::AudioTrack> track;
29 } BtifAvrcpAudioTrack;
30
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";
35 #endif
36
37 void *BtifAvrcpAudioTrackCreate(int trackFreq, int channelType)
38 {
39     LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btCreateTrack freq %d  channel %d ",
40                      __func__, trackFreq, channelType);
41     int ret = -1;
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);
47
48     BtifAvrcpAudioTrack *trackHolder = new BtifAvrcpAudioTrack;
49     assert(trackHolder);
50     trackHolder->track = track;
51
52     if (trackHolder->track->initCheck() != 0)
53     {
54         return nullptr;
55     }
56
57 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
58     outputPcmSampleFile = fopen(outputFilename, "ab");
59 #endif
60     trackHolder->track->setVolume(1, 1);
61     return (void *)trackHolder;
62 }
63
64 void BtifAvrcpAudioTrackStart(void *handle)
65 {
66     BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
67     assert(trackHolder);
68     assert(trackHolder->track != NULL);
69     LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
70     trackHolder->track->start();
71 }
72
73 void BtifAvrcpAudioTrackStop(void *handle)
74 {
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();
79     }
80 }
81
82 void BtifAvrcpAudioTrackDelete(void *handle)
83 {
84     BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
85     if (trackHolder != NULL && trackHolder->track != NULL) {
86         LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
87         delete trackHolder;
88     }
89
90 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
91     if (outputPcmSampleFile)
92     {
93         fclose(outputPcmSampleFile);
94     }
95     outputPcmSampleFile = NULL;
96 #endif
97 }
98
99 void BtifAvrcpAudioTrackPause(void *handle)
100 {
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();
106     }
107 }
108
109 int BtifAvrcpAudioTrackWriteData(void *handle, void *audioBuffer, int bufferlen)
110 {
111     BtifAvrcpAudioTrack *trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
112     assert(trackHolder);
113     assert(trackHolder->track != NULL);
114     int retval = -1;
115 #if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
116     if (outputPcmSampleFile)
117     {
118         fwrite ((audioBuffer), 1, (size_t)bufferlen, outputPcmSampleFile);
119     }
120 #endif
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);
124     return retval;
125 }