Add version of so files
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_sound.c
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@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
22 /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 =======================================================================================*/
25 #include "mm_camcorder_internal.h"
26 #include "mm_camcorder_sound.h"
27
28 /*---------------------------------------------------------------------------------------
29 |    LOCAL VARIABLE DEFINITIONS for internal                                            |
30 ---------------------------------------------------------------------------------------*/
31 #define _MMCAMCORDER_SOLO_PLAY_WAIT_TIMEOUT (G_TIME_SPAN_SECOND * 3)
32
33
34 /*---------------------------------------------------------------------------------------
35 |    LOCAL FUNCTION PROTOTYPES:                                                         |
36 ---------------------------------------------------------------------------------------*/
37
38
39
40 gboolean _mmcamcorder_sound_init(MMHandleType handle)
41 {
42         int sound_enable = TRUE;
43         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
44         SOUND_INFO *info = NULL;
45
46         mmf_return_val_if_fail(hcamcorder, FALSE);
47
48         info = &(hcamcorder->snd_info);
49
50         g_mutex_lock(&info->open_mutex);
51
52         /* check sound state */
53         if (info->state > _MMCAMCORDER_SOUND_STATE_NONE) {
54                 MMCAM_LOG_WARNING("already initialized [%d]", info->state);
55                 g_mutex_unlock(&info->open_mutex);
56                 return TRUE;
57         }
58
59         /* check sound play enable */
60         mm_camcorder_get_attributes((MMHandleType)hcamcorder, NULL,
61                 MMCAM_CAPTURE_SOUND_ENABLE, &sound_enable,
62                 NULL);
63         MMCAM_LOG_INFO("Capture sound enable %d", sound_enable);
64         if (!sound_enable) {
65                 MMCAM_LOG_WARNING("capture sound disabled");
66                 g_mutex_unlock(&info->open_mutex);
67                 return FALSE;
68         }
69
70         /* check shutter sound policy and status of system */
71         if (hcamcorder->shutter_sound_policy == VCONFKEY_CAMERA_SHUTTER_SOUND_POLICY_OFF &&
72             hcamcorder->sub_context->info_image->sound_status == FALSE) {
73                 MMCAM_LOG_WARNING("skip sound init : Sound Policy OFF and Silent status");
74                 g_mutex_unlock(&info->open_mutex);
75                 return FALSE;
76         }
77
78         info->state = _MMCAMCORDER_SOUND_STATE_INIT;
79
80         MMCAM_LOG_INFO("init DONE");
81
82         g_mutex_unlock(&info->open_mutex);
83
84         return TRUE;
85 }
86
87
88 gboolean _mmcamcorder_sound_play(MMHandleType handle, const char *sample_name, gboolean sync_play)
89 {
90         int sound_enable = TRUE;
91         const char *volume_gain = NULL;
92
93         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
94         SOUND_INFO *info = NULL;
95
96         mmf_return_val_if_fail(hcamcorder && sample_name, FALSE);
97
98         /* check sound play enable */
99         mm_camcorder_get_attributes((MMHandleType)hcamcorder, NULL,
100                 MMCAM_CAPTURE_SOUND_ENABLE, &sound_enable,
101                 NULL);
102         MMCAM_LOG_INFO("Capture sound enable %d", sound_enable);
103         if (!sound_enable) {
104                 MMCAM_LOG_WARNING("capture sound disabled");
105                 return FALSE;
106         }
107
108         info = &(hcamcorder->snd_info);
109
110         g_mutex_lock(&info->open_mutex);
111
112         if (info->state < _MMCAMCORDER_SOUND_STATE_INIT) {
113                 MMCAM_LOG_INFO("not initialized state:[%d]", info->state);
114                 g_mutex_unlock(&info->open_mutex);
115                 return FALSE;
116         }
117
118         if (!strcmp(sample_name, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE02))
119                 volume_gain = "shutter2";
120         else if (!strcmp(sample_name, _MMCAMCORDER_SAMPLE_SOUND_NAME_REC_STOP))
121                 volume_gain = "camcording";
122
123         MMCAM_LOG_INFO("Play start - sample name [%s]", sample_name);
124
125         _mmcamcorder_send_sound_play_message(hcamcorder->gdbus_conn,
126                 hcamcorder->gdbus_info_sound, sample_name, "system", volume_gain, sync_play);
127
128         g_mutex_unlock(&info->open_mutex);
129
130         MMCAM_LOG_INFO("Done");
131
132         return TRUE;
133 }
134
135
136 gboolean _mmcamcorder_sound_finalize(MMHandleType handle)
137 {
138         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
139         SOUND_INFO *info = NULL;
140
141         mmf_return_val_if_fail(hcamcorder, FALSE);
142
143         info = &(hcamcorder->snd_info);
144
145         MMCAM_LOG_ERROR("START");
146
147         g_mutex_lock(&info->open_mutex);
148
149         if (info->state < _MMCAMCORDER_SOUND_STATE_INIT) {
150                 MMCAM_LOG_WARNING("not initialized");
151                 g_mutex_unlock(&info->open_mutex);
152                 return TRUE;
153         }
154
155         info->state = _MMCAMCORDER_SOUND_STATE_NONE;
156
157         MMCAM_LOG_ERROR("DONE");
158
159         g_mutex_unlock(&info->open_mutex);
160
161         return TRUE;
162 }
163
164
165 int _mmcamcorder_sound_solo_play(MMHandleType handle, const char *sample_name, gboolean sync_play)
166 {
167         int sound_enable = TRUE;
168         int current_state = MM_CAMCORDER_STATE_NONE;
169
170         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
171
172         mmf_return_val_if_fail(hcamcorder && sample_name, FALSE);
173
174         MMCAM_LOG_INFO("START : [%s]", sample_name);
175
176         _mmcamcorder_sound_solo_play_wait(handle);
177
178         current_state = _mmcamcorder_get_state(handle);
179         if (current_state < MM_CAMCORDER_STATE_PREPARE) {
180                 MMCAM_LOG_WARNING("invalid state %d", current_state);
181                 return FALSE;
182         }
183
184         /* check sound play enable */
185         mm_camcorder_get_attributes((MMHandleType)hcamcorder, NULL,
186                 MMCAM_CAPTURE_SOUND_ENABLE, &sound_enable,
187                 NULL);
188         MMCAM_LOG_INFO("Capture sound enable %d", sound_enable);
189         if (!sound_enable) {
190                 MMCAM_LOG_WARNING("capture sound disabled");
191                 return FALSE;
192         }
193
194         MMCAM_LOG_INFO("Play start - sample name [%s]", sample_name);
195
196         if (hcamcorder->shutter_sound_policy == VCONFKEY_CAMERA_SHUTTER_SOUND_POLICY_ON ||
197                 hcamcorder->sub_context->info_image->sound_status) {
198                 _mmcamcorder_send_sound_play_message(hcamcorder->gdbus_conn,
199                         hcamcorder->gdbus_info_solo_sound, sample_name, "system", "shutter1", sync_play);
200         } else {
201                 MMCAM_LOG_WARNING("skip shutter sound : sound policy %d, sound status %d",
202                         hcamcorder->shutter_sound_policy, hcamcorder->sub_context->info_image->sound_status);
203         }
204
205         MMCAM_LOG_INFO("Done");
206
207         return TRUE;
208 }
209
210
211 void _mmcamcorder_sound_solo_play_wait(MMHandleType handle)
212 {
213         gint64 end_time = 0;
214         mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
215         _MMCamcorderGDbusCbInfo *info = NULL;
216
217         mmf_return_if_fail(hcamcorder && hcamcorder->gdbus_info_solo_sound);
218
219         info = hcamcorder->gdbus_info_solo_sound;
220
221         end_time = g_get_monotonic_time() + _MMCAMCORDER_SOLO_PLAY_WAIT_TIMEOUT;
222
223         g_mutex_lock(&info->sync_mutex);
224
225         MMCAM_LOG_INFO("is_playing[%d]", info->is_playing);
226
227         while (info->is_playing) {
228                 MMCAM_LOG_INFO("Wait for signal");
229
230                 if (!g_cond_wait_until(&info->sync_cond, &info->sync_mutex, end_time)) {
231                         MMCAM_LOG_WARNING("sound play EOS timeout");
232                         break;
233                 }
234
235                 MMCAM_LOG_INFO("signal received, check again[is_playing:%d]", info->is_playing);
236         }
237
238         g_mutex_unlock(&info->sync_mutex);
239
240         MMCAM_LOG_INFO("DONE");
241
242         return;
243 }