summaryrefslogtreecommitdiffstats
path: root/msm8909/hal/audio_extn/a2dp.c
blob: 6b22b692016575987a525ccf054797608fdc941b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (c) 2015, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials provided
*       with the distribution.
*     * Neither the name of The Linux Foundation nor the names of its
*       contributors may be used to endorse or promote products derived
*       from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LOG_TAG "split_a2dp"
/*#define LOG_NDEBUG 0*/
#define LOG_NDDEBUG 0
#include <errno.h>
#include <cutils/log.h>

#include "audio_hw.h"
#include "platform.h"
#include "platform_api.h"
#include <stdlib.h>
#include <cutils/str_parms.h>
#include <hardware/audio.h>
#include <hardware/hardware.h>

#ifdef SPLIT_A2DP_ENABLED

struct a2dp_data{
    struct audio_stream_out *a2dp_stream;
    struct audio_hw_device *a2dp_device;
    bool a2dp_started;
    bool a2dp_suspended;
};

struct a2dp_data a2dp;

#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"

static int open_a2dp_output()
{
    hw_module_t *mod;
    int      format = AUDIO_FORMAT_PCM_16_BIT;
    int rc=0;
    uint32_t channels = AUDIO_CHANNEL_OUT_STEREO;
    uint32_t sampleRate = DEFAULT_OUTPUT_SAMPLING_RATE;
    struct audio_config config;

    ALOGV("open_a2dp_output");

    config.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
    config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
    config.format = AUDIO_FORMAT_PCM_16_BIT;

    if (a2dp.a2dp_device == NULL){
        rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, (const char*)"a2dp",
                                    (const hw_module_t**)&mod);
        if (rc != 0) {
            ALOGE("Could not get a2dp hardware module");
            return rc;
        }
        ALOGV("Opening A2DP device HAL for the first time");
        rc = audio_hw_device_open(mod, &a2dp.a2dp_device);
        if (rc != 0) {
            ALOGE("couldn't open a2dp audio hw device");
            return rc;
        }
    }

    rc = a2dp.a2dp_device->open_output_stream(a2dp.a2dp_device, 0,AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
                                    (audio_output_flags_t)AUDIO_OUTPUT_FLAG_NONE, &config, &a2dp.a2dp_stream, NULL);

    if( rc != 0 ) {
        ALOGE("Failed to open output stream for a2dp: status %d", rc);
    }

    a2dp.a2dp_suspended = false;
    return rc;
}

static int close_a2dp_output()
{

    ALOGV("close_a2dp_output");
    if(!a2dp.a2dp_device && !a2dp.a2dp_stream){
        ALOGE("No Active A2dp output found");
        return 0;
    }

    a2dp.a2dp_device->close_output_stream(a2dp.a2dp_device, a2dp.a2dp_stream);
    a2dp.a2dp_stream = NULL;
    a2dp.a2dp_started = false;
    a2dp.a2dp_suspended = true;

    return 0;
}

void audio_extn_a2dp_set_parameters(struct str_parms *parms)
{
     int ret, val;
     char value[32]={0};

     ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
                            sizeof(value));
     if( ret >= 0) {
         val = atoi(value);
         if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
             ALOGV("Received device connect request for A2DP");
             open_a2dp_output();
         }
     }

     ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
                         sizeof(value));

     if( ret >= 0) {
         val = atoi(value);
         if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
             ALOGV("Received device dis- connect request");
             close_a2dp_output();
         }
     }

     ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
     if (ret >= 0) {
         if (a2dp.a2dp_device && a2dp.a2dp_stream) {
             a2dp.a2dp_device->set_parameters(a2dp.a2dp_device, str_parms_to_str(parms));
             if (!strncmp(value,"true",sizeof(value))) {
                 a2dp.a2dp_suspended = true;
             } else {
                 a2dp.a2dp_suspended = false;
             }
         }
     }
}

void audio_extn_a2dp_start_playback()
{
    int ret = 0;
    char buf[20]={0};

    if (!a2dp.a2dp_started && a2dp.a2dp_device && a2dp.a2dp_stream) {

         snprintf(buf,sizeof(buf),"%s=true",AUDIO_PARAMETER_A2DP_STARTED);
        /* This call indicates BT HAL to start playback */
        ret =  a2dp.a2dp_device->set_parameters(a2dp.a2dp_device, buf);
        if (ret < 0 ) {
           ALOGE("BT controller start failed, retry on the next write");
           a2dp.a2dp_started = false;
        } else {
           a2dp.a2dp_started = true;
           ALOGV("Start playback successful to BT HAL");
        }
    }
}

void audio_extn_a2dp_stop_playback()
{
    int ret =0;
    char buf[20]={0};

    if ( a2dp.a2dp_started && a2dp.a2dp_device && a2dp.a2dp_stream) {

       snprintf(buf,sizeof(buf),"%s=false",AUDIO_PARAMETER_A2DP_STARTED);

        ret = a2dp.a2dp_device->set_parameters(a2dp.a2dp_device, buf);

        if (ret < 0)
            ALOGE("out_standby to BT HAL failed");
        else
            ALOGV("out_standby to BT HAL successful");

    }
    a2dp.a2dp_started = false;
    a2dp.a2dp_suspended = true;
}

void audio_extn_a2dp_init ()
{
  a2dp.a2dp_started = false;
  a2dp.a2dp_suspended = true;
  a2dp.a2dp_stream = NULL;
  a2dp.a2dp_device = NULL;
}
#endif // SPLIT_A2DP_ENABLED