c2a517dac360462aa34c300da1db3f01c1a7b5a7
[platform/core/api/audio-io.git] / src / cpp / CAudioOutput.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
18 #include "CAudioIODef.h"
19 #include <sched.h>
20
21 using namespace std;
22 using namespace tizen_media_audio;
23
24 /**
25  * class CAudioOutput
26  */
27 CAudioOutput::CAudioOutput(CAudioInfo& info) :
28     CAudioIO(info),
29     __mIsUsedSyncWrite(false),
30     __mIsInit(false) {
31 }
32
33 CAudioOutput::CAudioOutput(
34         unsigned int            sampleRate,
35         CAudioInfo::EChannel    channel,
36         CAudioInfo::ESampleType sampleType,
37         CAudioInfo::EAudioType  audioType) :
38     __mIsUsedSyncWrite(false),
39     __mIsInit(false) {
40     mAudioInfo = CAudioInfo(sampleRate, channel, sampleType, audioType, -1);
41 }
42
43 CAudioOutput::~CAudioOutput() {
44 }
45
46 void CAudioOutput::onStream(CPulseAudioClient* pClient, size_t length) {
47     assert(pClient);
48
49     /*
50      * Does not call CAudioIO::onStream() for synchronization
51      * if a user is using write()
52      */
53     if (__mIsUsedSyncWrite == true) {
54 #ifdef _AUDIO_IO_DEBUG_TIMING_
55         AUDIO_IO_LOGD("Sync Write Mode! - signal! - pClient:[%p], length:[%d]", pClient, length);
56 #endif
57         internalSignal();
58         return;
59     }
60
61     /*
62      * Accrues callback function
63      */
64 #ifdef _AUDIO_IO_DEBUG_TIMING_
65     AUDIO_IO_LOGD("pClient:[%p], length:[%d]", pClient, length);
66 #endif
67     CAudioIO::onStream(pClient, length);
68 }
69
70 void CAudioOutput::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focus_type_e focus_type,
71                                mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info) {
72     assert(pHandler);
73     AUDIO_IO_LOGD("[pHandler:0x%x], [focus_type:%d], [state:%d], [reason_for_change:%s], [additional_info:%s]",
74                    pHandler, focus_type, state, reason_for_change, additional_info);
75     CAudioIO::onInterrupt(pHandler, id, focus_type, state, reason_for_change, additional_info);
76 }
77
78 void CAudioOutput::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
79     assert(pHandler);
80     AUDIO_IO_LOGD("[pHandler:0x%x], [signal:%d], [value:%d]", pHandler, signal, value);
81     CAudioIO::onSignal(pHandler, signal, value);
82 }
83
84 void CAudioOutput::__setInit(bool flag) {
85     __mIsInit = flag;
86 }
87
88 bool CAudioOutput::__IsInit() {
89     return (CAudioIO::isInit() == true && __mIsInit == true);
90 }
91
92 bool CAudioOutput::__IsReady() {
93     return CAudioIO::IsReady();
94 }
95
96 void CAudioOutput::initialize() throw(CAudioError) {
97     if (__IsInit() == true) {
98         return;
99     }
100
101     try {
102         CAudioIO::initialize();
103
104         // Create ASM Handler
105         mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_PLAYBACK, mAudioInfo, this);
106         if (mpAudioSessionHandler == NULL) {
107             THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY,
108                             "Failed to allocate CAudioSessionHandler object");
109         }
110
111         // Initialize ASM Handler
112         mpAudioSessionHandler->initialize();
113
114         __setInit(true);
115         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
116     } catch (CAudioError err) {
117         finalize();
118         throw err;
119     }
120 }
121
122 void CAudioOutput::finalize() {
123     if (__IsInit() == false) {
124         AUDIO_IO_LOGD("Did not initialize");
125         return;
126     }
127
128     SAFE_FINALIZE(mpAudioSessionHandler);
129     SAFE_DELETE(mpAudioSessionHandler);
130
131     CAudioIO::finalize();
132
133     __setInit(false);
134 }
135
136 void CAudioOutput::prepare() throw(CAudioError) {
137     if (__IsInit() == false) {
138         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput");
139     }
140
141     if (__IsReady() == true) {
142         AUDIO_IO_LOGD("Already prepared CAudioOutput");
143         return;
144     }
145
146     try {
147         internalLock();
148
149         // Check to invalid AudioType
150         CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
151         if (audioType < CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX) {
152             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
153                                    "The audioType is invalid [type:%d]", static_cast<int>(audioType));
154         }
155
156         if (mpAudioSessionHandler->getId() < 0) {  // Did not registerSound()
157             if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) {
158                 // Register ASM Listener
159                 AUDIO_IO_LOGD("Register ASM Listener");
160                 mpAudioSessionHandler->registerSound();
161             }
162         }
163
164         // Init StreamSpec
165         AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_OUTPUT_DEFAULT");
166         CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT;
167         CPulseStreamSpec spec(streamSpec, mAudioInfo);
168
169         // Create PulseAudio Handler
170         mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK, spec, this);
171         if (mpPulseAudioClient == NULL) {
172             THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY,
173                             "Failed to allocate CPulseAudioClient object");
174         }
175
176         // Initialize PulseAudio Handler
177         mpPulseAudioClient->initialize();
178
179         if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
180             mpAudioSessionHandler->updatePlaying();
181
182         internalUnlock();
183
184         CAudioIO::prepare();
185     } catch (CAudioError e) {
186         internalUnlock();
187         throw e;
188     }
189 }
190
191 void CAudioOutput::unprepare() throw(CAudioError) {
192     if (__IsInit() == false) {
193         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
194                         "Did not initialize CAudioOutput");
195     }
196
197     if (__IsReady() == false) {
198         AUDIO_IO_LOGD("Already unprepared");
199         return;
200     }
201
202     try {
203         CAudioIO::unprepare();
204
205         internalLock();
206
207         SAFE_FINALIZE(mpPulseAudioClient);
208         SAFE_DELETE(mpPulseAudioClient);
209
210         if (mpAudioSessionHandler->getId() >= 0) {
211             if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
212                 mpAudioSessionHandler->updateStop();
213
214             if (mpAudioSessionHandler->isSkipSession() == false)
215                 mpAudioSessionHandler->unregisterSound();
216         }
217
218         internalUnlock();
219
220         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
221     } catch (CAudioError e) {
222         internalUnlock();
223         throw e;
224     }
225 }
226
227 void CAudioOutput::pause() throw(CAudioError) {
228     if (__IsInit() == false || __IsReady() == false) {
229         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
230                         "Did not initialize or prepare CAudioOutput");
231     }
232
233     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) {
234         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
235                         "Can't pause if not in Running state");
236     }
237
238     if (mpPulseAudioClient->isInThread() == true) {
239         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread");
240     }
241
242     try {
243         CAudioIO::pause();
244
245         internalLock();
246
247         /* Updates ASM to STOP */
248         if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
249             mpAudioSessionHandler->updateStop();
250
251         internalUnlock();
252
253         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
254     } catch (CAudioError e) {
255         internalUnlock();
256         throw e;
257     }
258 }
259
260 void CAudioOutput::resume() throw(CAudioError) {
261     if (__IsInit() == false || __IsReady() == false) {
262         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
263                         "Did not initialize or prepare CAudioOutput");
264     }
265
266     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED) {
267         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
268                         "Can't resume if not in Paused state");
269     }
270
271     if (mpPulseAudioClient->isInThread() == true) {
272         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread");
273     }
274
275     try {
276         internalLock();
277
278         if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
279             mpAudioSessionHandler->updatePlaying();
280
281         internalUnlock();
282
283         CAudioIO::resume();
284
285         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
286     } catch (CAudioError e) {
287         internalUnlock();
288         throw e;
289     }
290 }
291
292 void CAudioOutput::drain() throw(CAudioError) {
293     if (__IsInit() == false || __IsReady() == false) {
294         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
295                         "Did not initialize or prepare CAudioOutput");
296     }
297
298     try {
299         CAudioIO::drain();
300     } catch (CAudioError e) {
301         throw e;
302     }
303 }
304
305 void CAudioOutput::flush() throw(CAudioError) {
306     if (__IsInit() == false || __IsReady() == false) {
307         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
308                         "Did not initialize or prepare CAudioOutput");
309     }
310
311     try {
312         CAudioIO::flush();
313     } catch (CAudioError e) {
314         throw e;
315     }
316 }
317
318 int CAudioOutput::getBufferSize() throw(CAudioError) {
319     if (__IsInit() == false) {
320         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
321                         "Did not initialize or prepare CAudioOutput");
322     }
323
324     /* FIXME : return calculated size here to satisfy backward compatibility */
325     return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
326 }
327
328 size_t CAudioOutput::write(const void* buffer, size_t length) throw(CAudioError) {
329     if (__IsInit() == false || __IsReady() == false) {
330         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
331                         "Did not initialize or prepare CAudioOutput");
332     }
333
334     if (buffer == NULL) {
335         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
336                                "Parameters are invalid - buffer:%p, length:%zu", buffer, length);
337     }
338     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) {
339         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
340                         "Can't write if not in Running state");
341     }
342
343     /* When write() is called in PulseAudio callback, bypass a pcm data to CPulseAudioClient (For Asynchronous) */
344     if (mpPulseAudioClient->isInThread() == true) {
345         int ret = mpPulseAudioClient->write(buffer, length);
346         if (ret < 0) {
347             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
348                                    "The written result is invalid ret:%d", ret);
349         }
350
351 #ifdef _AUDIO_IO_DEBUG_TIMING_
352         AUDIO_IO_LOGD("CPulseAudioClient->write(buffer:%p, length:%d)", buffer, length);
353 #endif
354
355         return length;
356     }
357
358     try {
359         /* For synchronization */
360         internalLock();
361
362         // If another thread did call unprepare, do not write
363         if (mpPulseAudioClient == NULL)
364             THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
365                             "Did not initialize CPulseAudioClient");
366
367         // Sets synchronous flag
368         __mIsUsedSyncWrite = true;
369
370         size_t lengthIter = length;
371
372         while (lengthIter > 0) {
373             size_t l;
374
375             while ((l = mpPulseAudioClient->getWritableSize()) == 0) {
376 #ifdef _AUDIO_IO_DEBUG_TIMING_
377                 AUDIO_IO_LOGD("writableSize is [%d].. wait", l);
378 #endif
379                 internalWait();
380             }
381
382             if (l > lengthIter) {
383                 l = lengthIter;
384             }
385
386 #ifdef _AUDIO_IO_DEBUG_TIMING_
387             AUDIO_IO_LOGD("CPulseAudioClient->write(buffer:%p, length:%d)", buffer, l);
388 #endif
389
390             int ret = mpPulseAudioClient->write(buffer, l);
391             if (ret < 0) {
392                 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
393                                        "The written result is invalid ret:%d", ret);
394             }
395
396             buffer = static_cast<const uint8_t*>(buffer) + l;
397             lengthIter -= l;
398         }  // End of while (length > 0)
399
400         __mIsUsedSyncWrite = false;
401         internalUnlock();
402         sched_yield();
403     } catch (CAudioError e) {
404         __mIsUsedSyncWrite = false;
405         internalUnlock();
406         throw e;
407     }
408
409     return length;
410 }