Merge "Removed unused result variable from sound_codec_asm_callback" into tizen
[platform/core/multimedia/libmm-sound.git] / server / mm_sound_hal.c
1 /*
2  *  libmm-sound
3  *
4  *  Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  *  Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <mm_error.h>
26 #include <mm_debug.h>
27 #include <avsys-audio.h>
28
29 #include "mm_sound_hal.h"
30
31 #include "include/mm_sound_plugin_hal.h"
32 #include "include/mm_sound_mgr_pulse.h"
33
34 static MMSoundPluginType* g_hal_plugin = NULL;
35 static mmsound_hal_interface_t g_plugin_interface;
36
37 #define DEVICE_API_BLUETOOTH    "bluez"
38 #define DEVICE_API_ALSA "alsa"
39 #ifdef USE_PULSE_WFD /* Not enabled yet */
40 #define DEVICE_API_WFD  "wfd"
41 #endif
42
43 #define DEVICE_BUS_BLUETOOTH "bluetooth"
44 #define DEVICE_BUS_USB "usb"
45 #define DEVICE_BUS_BUILTIN "builtin"
46
47 int audio_hal_pulse_sink_route(int device)
48 {
49     debug_fenter();
50     if (g_plugin_interface.pulse_sink_route) {
51         return g_plugin_interface.pulse_sink_route(device);
52     }
53
54     debug_log("g_plugin_interface.pulse_sink_route is null!");
55     if (device & MM_SOUND_DEVICE_OUT_BT_A2DP) {
56         debug_log("BT A2DP is active, Set default sink to BLUEZ");
57         MMSoundMgrPulseSetDefaultSink (DEVICE_API_BLUETOOTH, DEVICE_BUS_BLUETOOTH);
58         return AUDIO_HAL_ROUTE_SUCCESS_AND_GOTOEND;
59     } else if (device & MM_SOUND_DEVICE_OUT_WFD) {
60 #ifdef USE_PULSE_WFD /* Not enabled yet */
61         debug_log("WFD is active, Set default sink to WFD");
62         MMSoundMgrPulseSetDefaultSink (DEVICE_API_WFD, DEVICE_BUS_BUILTIN);
63         return AUDIO_HAL_ROUTE_SUCCESS_AND_GOTOEND;
64 #endif
65     } else if (device & MM_SOUND_DEVICE_OUT_USB_AUDIO) {
66         debug_log("USB Audio is active, Set default sink to USB Audio");
67         MMSoundMgrPulseSetDefaultSink (DEVICE_API_ALSA, DEVICE_BUS_USB);
68         return AUDIO_HAL_ROUTE_SUCCESS_AND_GOTOEND;
69     }
70     debug_log("Set default sink to ALSA with BUILTIN");
71     MMSoundMgrPulseSetDefaultSink (DEVICE_API_ALSA, DEVICE_BUS_BUILTIN);
72     debug_fleave();
73     return AUDIO_HAL_STATE_SUCCESS;
74 }
75
76 int audio_hal_pulse_source_route(int device)
77 {
78     debug_fenter();
79     if (g_plugin_interface.pulse_source_route) {
80         return g_plugin_interface.pulse_source_route(device);
81     }
82     debug_log("g_plugin_interface.pulse_source_route is null!");
83     debug_fleave();
84     return AUDIO_HAL_STATE_SUCCESS;
85 }
86
87 EXPORT_API
88 int audio_hal_set_sound_path(int gain, int output, int input, int option)
89 {
90     int err = MM_ERROR_NONE;
91
92     debug_fenter();
93     if (g_plugin_interface.set_sound_path) {
94         err = g_plugin_interface.set_sound_path(gain, output, input, option);
95     }
96     else {
97         err = avsys_audio_set_path_ex(gain, output, input, option);
98     }
99     debug_fleave();
100     return err;
101 }
102
103 int audio_hal_init()
104 {
105     debug_fenter();
106     if (g_plugin_interface.init) {
107         return g_plugin_interface.init();
108     }
109     debug_fleave();
110     return AUDIO_HAL_STATE_SUCCESS;
111 }
112
113 int audio_hal_fini()
114 {
115     debug_fenter();
116     if (g_plugin_interface.fini) {
117         return g_plugin_interface.fini();
118     }
119     debug_fleave();
120     return AUDIO_HAL_STATE_SUCCESS;
121 }
122
123 int MMSoundMgrHALInit(const char *targetdir)
124 {
125     int err = MM_ERROR_NONE;
126
127     debug_fenter();
128     if (g_hal_plugin) {
129         debug_error("Please Check Init\n");
130         return MM_ERROR_SOUND_INTERNAL;
131     }
132     err = MMSoundPluginScan(targetdir, MM_SOUND_PLUGIN_TYPE_HAL, &g_hal_plugin);
133     debug_log("MMSoundPluginScan return %d", err);
134
135     if (g_hal_plugin) {
136         void* func = NULL;
137
138         err = MMSoundPluginGetSymbol(g_hal_plugin, HAL_GET_INTERFACE_FUNC_NAME, &func);
139         if (err  != MM_ERROR_NONE) {
140             debug_error("Get Symbol %s fail : %x\n", HAL_GET_INTERFACE_FUNC_NAME, err);
141             goto err_out_and_free;
142         }
143         err = MMSoundPlugHALCastGetInterface(func)(&g_plugin_interface);
144         if (err != AUDIO_HAL_STATE_SUCCESS) {
145             debug_error("Get interface fail : %x\n", err);
146             goto err_out_and_free;
147         }
148
149         debug_log("g_plugin_interface.pulse_sink_route=%p", g_plugin_interface.pulse_sink_route);
150         debug_log("g_plugin_interface.pulse_source_route=%p", g_plugin_interface.pulse_source_route);
151         debug_log("g_plugin_interface.set_sound_path=%p", g_plugin_interface.set_sound_path);
152         debug_log("g_plugin_interface.init=%p", g_plugin_interface.init);
153         debug_log("g_plugin_interface.fini=%p", g_plugin_interface.fini);
154
155         err = audio_hal_init();
156         if (err != AUDIO_HAL_STATE_SUCCESS) {
157             debug_error("audio_hal_init failed : %x\n", err);
158             goto err_out_and_free;
159         }
160     }
161
162     debug_fleave();
163     return AUDIO_HAL_STATE_SUCCESS;
164
165 err_out_and_free:
166     free(g_hal_plugin);
167     g_hal_plugin = NULL;
168     debug_fleave();
169     return AUDIO_HAL_STATE_ERROR_INTERNAL;
170 }
171
172 int MMSoundMgrHALFini(void)
173 {
174     int err = MM_ERROR_NONE;
175
176     debug_fenter();
177     err = audio_hal_fini();
178     if (err != AUDIO_HAL_STATE_SUCCESS) {
179         debug_error("audio_hal_fini failed");
180         return err;
181     }
182     debug_fleave();
183     return AUDIO_HAL_STATE_SUCCESS;
184 }