2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <FMediaToneTypes.h>
19 #include <FBaseSysLog.h>
20 #include "FMedia_TonePlayerImpl.h"
21 #include "FMedia_ToneGenerator.h"
22 #include "FMedia_TonePlayerEvent.h"
23 #include "FMedia_TonePlayerEventArg.h"
24 #include "FMedia_AudioManagerConvert.h"
26 using namespace Tizen::Base;
27 using namespace Tizen::Base::Collection;
28 using namespace Tizen::Base::Runtime;
30 namespace Tizen { namespace Media
33 std::unique_ptr<Tizen::Base::Runtime::Mutex> _TonePlayerImpl::__pMutex;
34 bool _TonePlayerImpl::__isInstanceMutexInitialized = false;
35 int _TonePlayerImpl::__instanceCount = 0;
37 #define MAX_TONE_PLAYER_VOLUME 100
38 #define TONE_PLAYER_FEED_BUFFER_SIZE 1024
40 _TonePlayerImpl::_TonePlayerImpl(void)
41 : __pTonePlayerEventListener(null)
42 , __endOftoneReached(false)
43 , __tonePlayerOptimalBufferSize(0)
45 , __audioStreamtype(AUDIO_STREAM_TYPE_MEDIA)
46 , __interruptFlag(false)
49 __tempRepeatCount = 0;
51 __volume = DEFAULT_TONE_PLAYER_VOLUME;
53 __tonePlayerState = TONE_PLAYER_STATE_IDLE;
55 __tobePlayedBuffCount = 0;
57 __endOftoneReached = false;
58 __endOftoneReachedArr[0] = false;
59 __endOftoneReachedArr[1] = false;
60 __audioOutHandle = null;
62 __sampleNumberArr[0] = 0;
63 __sampleNumberArr[1] = 0;
65 __tempRepeatCountArr[0] = 0;
66 __tempRepeatCountArr[1] = 0;
68 __toneListIndexArr[0] = 0;
69 __toneListIndexArr[1] = 0;
75 _TonePlayerImpl::~_TonePlayerImpl(void)
77 if ((__tonePlayerState == TONE_PLAYER_STATE_PLAYING) ||
78 (__tonePlayerState == TONE_PLAYER_STATE_STOPPED) ||
79 (__tonePlayerState == TONE_PLAYER_STATE_PAUSED) ||
80 (__tonePlayerState == TONE_PLAYER_STATE_OPENED))
85 else if (__tonePlayerState == TONE_PLAYER_STATE_ERROR)
89 int ret = AUDIO_IO_ERROR_NONE;
90 ret = audio_out_unset_interrupted_cb(__audioOutHandle);
91 SysTryLog(NID_MEDIA, ret == AUDIO_IO_ERROR_NONE, "Failed to perform audio_out_unset_interrupted_cb operation with %x", ret);
93 ret = audio_out_destroy(__audioOutHandle);
94 __audioOutHandle = null;
95 SysTryLog(NID_MEDIA, ret == AUDIO_IO_ERROR_NONE, "Failed to perform audio_out_destroy operation with %0x%x", ret);
99 if (__pToneList.get())
101 __pToneList->RemoveAll(true);
104 if (__tonePlayerState != TONE_PLAYER_STATE_IDLE)
110 __tonePlayerState = TONE_PLAYER_STATE_IDLE;
114 _TonePlayerImpl::InitInstanceMutex(void)
116 result r = E_SUCCESS;
117 __pMutex.reset(new (std::nothrow) Tizen::Base::Runtime::Mutex);
118 SysTryReturn(NID_MEDIA, __pMutex.get() != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] no memory to create instance Mutex");
119 r = __pMutex->Create("FMEDIA_TONEPLAYER");
120 SysTryReturn(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Mutex::Create failed.");
121 __isInstanceMutexInitialized = true;
125 _TonePlayerImpl::GetInstance(TonePlayer* pTonePlayer)
127 if (pTonePlayer != null)
129 return pTonePlayer->__pTonePlayerImpl;
135 const _TonePlayerImpl*
136 _TonePlayerImpl::GetInstance(const TonePlayer* pTonePlayer)
138 if (pTonePlayer != null)
140 return pTonePlayer->__pTonePlayerImpl;
147 _TonePlayerImpl::Construct(const Tizen::Media::ITonePlayerEventListener& listener, const Tizen::Media::TonePlayer& tonePlayerInstance)
149 result r = E_SUCCESS;
150 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
151 if (!__isInstanceMutexInitialized)
153 pthread_once(&onceBlock, InitInstanceMutex);
155 SysTryCatch(NID_MEDIA, __isInstanceMutexInitialized == true, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] memory allocation for Instance Mutex failed");
157 SysTryCatch(NID_MEDIA, __tonePlayerState == TONE_PLAYER_STATE_IDLE ,
158 r = E_INVALID_STATE, E_INVALID_STATE, " [E_INVALID_STATE] TonePlayer state is in an invalid state.");
159 SysTryCatch(NID_MEDIA, __instanceCount <= MAX_TONE_PLAYER_OUT_CAPACITY, r = E_RESOURCE_UNAVAILABLE, E_RESOURCE_UNAVAILABLE,
160 "[E_RESOURCE_UNAVAILABLE] TonePlayer Exceeds the number of Instance available");
162 //Create TonePlayerEvent
163 __pTonePlayerEvent.reset(new (std::nothrow) _TonePlayerEvent);
164 SysTryCatch(NID_MEDIA, __pTonePlayerEvent.get(), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, " [E_OUT_OF_MEMORY] Memory allocation failed.");
165 r = __pTonePlayerEvent->Construct(tonePlayerInstance);
166 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct _TonePlayerEvent.", GetErrorMessage(r));
167 r = __pTonePlayerEvent->AddListener(listener);
168 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
170 __pTonePlayerWorker.reset(new (std::nothrow) _TonePlayerWorker);
171 SysTryCatch(NID_MEDIA, __pTonePlayerWorker.get(), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, " [E_OUT_OF_MEMORY] Memory allocation failed.");
172 __pTonePlayerWorker->Initialize(this);
179 __tonePlayerState = TONE_PLAYER_STATE_INITIALIZED;
186 _TonePlayerImpl::SetAudioStreamType(AudioStreamType type)
188 SysTryReturnResult(NID_MEDIA, (__tonePlayerState == TONE_PLAYER_STATE_INITIALIZED ) || (__tonePlayerState == TONE_PLAYER_STATE_CLOSED ),
189 E_INVALID_STATE, "TonePlayer instance is in an invalid state. State is %d", __tonePlayerState);
191 SysTryReturnResult(NID_MEDIA, (type != AUDIO_STREAM_TYPE_NO_SOUND )
192 , E_INVALID_ARG, "Invalid argument is used. AudioStream type = %d", type);
194 __audioStreamtype = type;
199 _TonePlayerImpl::Open(const Tizen::Base::Collection::IList& toneList, int repeatCount)
201 result r = E_SUCCESS;
202 int firstFrequency = 0;
203 int secondFrequency = 0;
205 int toneListCount = 0;
206 const Tone* pToneTemp = null;
208 int ret = AUDIO_IO_ERROR_NONE;
210 SysTryReturnResult(NID_MEDIA, (__tonePlayerState == TONE_PLAYER_STATE_INITIALIZED ) || (__tonePlayerState == TONE_PLAYER_STATE_CLOSED ),
211 E_INVALID_STATE, "TonePlayer instance is in an invalid state. State is %d", __tonePlayerState);
213 SysTryReturnResult(NID_MEDIA, (repeatCount >= 1), E_OUT_OF_RANGE, "repeatCount is out of range should be greater than or equal to 1");
215 toneListCount = toneList.GetCount();
217 SysTryReturnResult(NID_MEDIA, (toneListCount >= 1), E_OBJ_NOT_FOUND, "Tone instance is not available.");
219 if (__pToneList.get())
221 __pToneList->RemoveAll(true);
223 __pToneList.reset(new (std::nothrow) Tizen::Base::Collection::ArrayList);
224 SysTryReturnResult(NID_MEDIA, __pToneList.get() != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
226 r = __pToneList->Construct();
227 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct __pToneList..", GetErrorMessage(r));
229 for (int i = 0; i < toneListCount; i++)
231 pToneTemp = dynamic_cast<const Tone*>(toneList.GetAt(i));
232 SysTryCatch(NID_MEDIA, (pToneTemp != null), r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Tone instance is not available");
234 r = pToneTemp->GetFrequency(firstFrequency, secondFrequency);
235 SysTryCatch(NID_MEDIA, ((firstFrequency >= MIN_FREQUENCY) && (firstFrequency <= MAX_FREQUENCY)
236 && (secondFrequency >= MIN_FREQUENCY) && (secondFrequency <= MAX_FREQUENCY)), r = E_OUT_OF_RANGE , E_OUT_OF_RANGE,
237 "[E_OUT_OF_RANGE] firstFrequency and secondFrequency are out of range. firstFrequency = %d secondFrequency = %d.", firstFrequency, secondFrequency);
239 duration = pToneTemp->GetDuration();
241 // Adding parameters checker
243 SysTryCatch(NID_MEDIA, (duration >= MIN_DURATION), r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Duration is out of range - %d should be greater than or equal to %d", duration, MIN_DURATION);
245 pTone = new (std::nothrow) Tone(firstFrequency, secondFrequency, duration);
246 SysTryCatch(NID_MEDIA, pTone, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(r));
248 r = __pToneList->Add(*pTone);
253 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
256 __repeatCount = repeatCount;
258 //Initilaize audio_out Device for raw audio out playback
259 ret = audio_out_create(SAMPLING_RATE, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, _AudioManagerConvert::ConvertAudioStreamType2SoundType(__audioStreamtype), &__audioOutHandle);
260 r = MapExceptionToResult(ret);
261 SysTryCatch(NID_MEDIA, r != E_INVALID_STATE, , r, "[%s] The device has moved to invalid state as it has run out of system resources: 0x%x", GetErrorMessage(r), ret);
262 SysTryCatch(NID_MEDIA, r == E_SUCCESS , , r, "[%s] Failed to perform audio_out_create operation with 0x%x", GetErrorMessage(r), ret);
264 ret = audio_out_set_interrupted_cb(__audioOutHandle, AudioIoInterrupted, this);
265 r = MapExceptionToResult(ret);
266 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to perform audio_out_set_interrupted_cb operation with 0x%x", GetErrorMessage(r), ret);
268 ret = audio_out_get_buffer_size(__audioOutHandle, &__tonePlayerOptimalBufferSize);
269 if (ret == AUDIO_IO_ERROR_NONE)
271 SysLog(NID_MEDIA, "Optimal Buffer size is %d", __tonePlayerOptimalBufferSize);
275 r = MapExceptionToResult(ret);
276 SysLogException(NID_MEDIA, r, "[%s] Failed to perform audio_out_get_buffer_size operation with 0x%x",GetErrorMessage(r), ret);
279 __tonePlayerOptimalBufferSize = TONE_PLAYER_FEED_BUFFER_SIZE;
281 __pToneBuffer.reset(new (std::nothrow) ByteBuffer);
282 SysTryCatch(NID_MEDIA, __pToneBuffer.get(), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
283 r = __pToneBuffer->Construct(__tonePlayerOptimalBufferSize);
284 SysTryCatch(NID_MEDIA, (r == E_SUCCESS), , r, "[%s] Failed to construct __pToneBuffer.", GetErrorMessage(r));
286 __tonePlayerState = TONE_PLAYER_STATE_OPENED;
289 //Clean Up audio-out Device
290 if (__audioOutHandle)
292 ret = audio_out_unset_interrupted_cb(__audioOutHandle);
293 r = MapExceptionToResult(ret);
294 SysTryReturnResult(NID_MEDIA, r == E_SUCCESS, r, "Failed to perform audio_out_unset_interrupted_cb operation with %x", ret);
296 ret = audio_out_destroy(__audioOutHandle);
297 __audioOutHandle = null;
298 r = MapExceptionToResult(ret);
299 SysTryReturnResult(NID_MEDIA, r == E_SUCCESS, r, "Failed to perform audio_out_destroy operation with %0x%x", ret);
303 if (__pToneList.get())
305 __pToneList->RemoveAll(true);
311 _TonePlayerImpl::Open(const Tizen::Media::Tone& tone, int repeatCount)
313 result r = E_SUCCESS;
314 int firstFrequency = 0;
315 int secondFrequency = 0;
317 int ret = AUDIO_IO_ERROR_NONE;
319 SysTryReturnResult(NID_MEDIA, (__tonePlayerState == TONE_PLAYER_STATE_INITIALIZED ) || (__tonePlayerState == TONE_PLAYER_STATE_CLOSED ), E_INVALID_STATE, "TonePlayer is in invalid state");
321 SysTryReturnResult(NID_MEDIA, (repeatCount >= 1), E_OUT_OF_RANGE, "repeatCount is out of range. repeatCount = %d.", repeatCount);
323 r = tone.GetFrequency(firstFrequency, secondFrequency);
324 SysTryReturnResult(NID_MEDIA, ((firstFrequency >= MIN_FREQUENCY) && (firstFrequency <= MAX_FREQUENCY)
325 && (secondFrequency >= MIN_FREQUENCY) && (secondFrequency <= MAX_FREQUENCY)) , E_OUT_OF_RANGE,
326 "firstFrequency and secondFrequency are out of range. firstFrequency = %d secondFrequency = %d.", firstFrequency, secondFrequency);
328 duration = tone.GetDuration();
330 // Adding parameters checker
331 SysTryCatch(NID_MEDIA, (duration >= MIN_DURATION), r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Duration is out of range");
334 __pTone.reset(new (std::nothrow) Tone(firstFrequency, secondFrequency, duration));
335 SysTryReturnResult(NID_MEDIA, __pTone.get() != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
337 __repeatCount = repeatCount;
338 //Initlaize audio-out Device for raw audio out playback
339 ret = audio_out_create(SAMPLING_RATE, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, _AudioManagerConvert::ConvertAudioStreamType2SoundType(__audioStreamtype), &__audioOutHandle);
340 SysTryCatch(NID_MEDIA, r != E_INVALID_STATE, , r, "[%s] The device has moved to invalid state as it has run out of system resources: 0x%x", GetErrorMessage(r), ret);
341 r = MapExceptionToResult(ret);
342 SysTryCatch(NID_MEDIA, r == E_SUCCESS , , r, "[%s] Failed to perform audio_out_create operation with 0x%x", GetErrorMessage(r), ret);
344 ret = audio_out_set_interrupted_cb(__audioOutHandle, AudioIoInterrupted, this);
345 r = MapExceptionToResult(ret);
346 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to perform audio_out_set_interrupted_cb operation with 0x%x", GetErrorMessage(r), ret);
348 ret = audio_out_get_buffer_size(__audioOutHandle, &__tonePlayerOptimalBufferSize);
349 if (ret == AUDIO_IO_ERROR_NONE)
351 SysLog(NID_MEDIA, "Optimal Buffer size is %d", __tonePlayerOptimalBufferSize);
355 r = MapExceptionToResult(ret);
356 SysLogException(NID_MEDIA, r, "[%s] Failed to perform audio_out_get_buffer_size operation with 0x%x", GetErrorMessage(r), ret);
359 __tonePlayerOptimalBufferSize = TONE_PLAYER_FEED_BUFFER_SIZE;
361 __pToneBuffer.reset(new (std::nothrow) ByteBuffer);
362 SysTryCatch(NID_MEDIA, __pToneBuffer.get(), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
363 r = __pToneBuffer->Construct(__tonePlayerOptimalBufferSize);
364 SysTryCatch(NID_MEDIA, (r == E_SUCCESS), , r, "[%s] Failed to construct __pToneBuffer.", GetErrorMessage(r));
366 __tonePlayerState = TONE_PLAYER_STATE_OPENED;
370 if (__audioOutHandle)
372 ret = audio_out_unset_interrupted_cb(__audioOutHandle);
373 r = MapExceptionToResult(ret);
374 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to perform audio_out_unset_interrupted_cb operation with %x", GetErrorMessage(r), ret);
376 ret = audio_out_destroy(__audioOutHandle);
377 r = MapExceptionToResult(ret);
378 SysTryReturnResult(NID_MEDIA, r == E_SUCCESS, r, "Failed to perform audio_out_destroy operation with 0x%x", ret);
380 __audioOutHandle = null;
387 _TonePlayerImpl::Play(void)
389 result r = E_SUCCESS;
390 int ret = AUDIO_IO_ERROR_NONE;
391 SysTryReturnResult(NID_MEDIA, (__tonePlayerState == TONE_PLAYER_STATE_PAUSED ) ||
392 (__tonePlayerState == TONE_PLAYER_STATE_OPENED) ||
393 (__tonePlayerState == TONE_PLAYER_STATE_STOPPED ), E_INVALID_STATE, "TonePlayer is in an invalid state - %d", __tonePlayerState);
395 if ((__tonePlayerState == TONE_PLAYER_STATE_OPENED ) || (__tonePlayerState == TONE_PLAYER_STATE_STOPPED ))
397 __tempRepeatCount = __repeatCount;
398 __tempRepeatCountArr[0] = __tempRepeatCount;
399 __tempRepeatCountArr[1] = __tempRepeatCount;
401 __tobePlayedBuffCount = 0;
404 __sampleNumberArr[0] = __sampleNumber;
405 __sampleNumberArr[1] = __sampleNumber;
407 __endOftoneReached = false;
408 __endOftoneReachedArr[0] = __endOftoneReached;
409 __endOftoneReachedArr[1] = __endOftoneReached;
411 if (__pToneList.get())
413 __toneListIndex = __pToneList->GetCount();
414 __toneListIndexArr[0] = __toneListIndex;
415 __toneListIndexArr[1] = __toneListIndex;
419 ret = audio_out_prepare(__audioOutHandle);
420 r = MapExceptionToResult(ret);
421 SysTryReturnResult(NID_MEDIA, r != E_DEVICE_BUSY, r, "TonePlayer cannot Start, higher priority task at work.");
422 SysTryCatch(NID_MEDIA, r == E_SUCCESS , , r, "[%s] Failed to perform audio_out_prepare operation with 0x%x", GetErrorMessage(r), ret);
424 __pWorkerThread.reset(new (std::nothrow) Thread);
425 SysTryCatch(NID_MEDIA, __pWorkerThread.get(), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
426 r = __pWorkerThread->Construct(*(__pTonePlayerWorker.get()));
427 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct __pWorkerThread.", GetErrorMessage(r));
428 r = __pWorkerThread->Start();
429 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
432 else if ((__tonePlayerState == TONE_PLAYER_STATE_PAUSED ))
434 __tempRepeatCount = __tempRepeatCountArr[0];
435 __sampleNumber = __sampleNumberArr[0];
436 __endOftoneReached = __endOftoneReachedArr[0];
438 if (__pToneList.get())
440 __toneListIndex = __toneListIndexArr[0];
444 ret = audio_out_prepare(__audioOutHandle);
445 r = MapExceptionToResult(ret);
446 SysTryReturnResult(NID_MEDIA, r != E_DEVICE_BUSY, r, "TonePlayer cannot Start, higher priority task at work.");
447 SysTryCatch(NID_MEDIA, r == E_SUCCESS , , r, "[%s] Failed to perform audio_out_prepare operation with 0x%x", GetErrorMessage(r), ret);
449 __pWorkerThread.reset(new (std::nothrow) Thread);
450 SysTryCatch(NID_MEDIA, __pWorkerThread.get(), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
451 r = __pWorkerThread->Construct(*(__pTonePlayerWorker.get()));
452 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Failed to construct __pWorkerThread..", GetErrorMessage(r));
453 r = __pWorkerThread->Start();
454 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
457 __tonePlayerState = TONE_PLAYER_STATE_PLAYING;
461 __tonePlayerState = TONE_PLAYER_STATE_ERROR;
467 _TonePlayerImpl::Stop(void)
469 SysTryReturnResult(NID_MEDIA, (__tonePlayerState == TONE_PLAYER_STATE_PAUSED ) ||
470 (__tonePlayerState == TONE_PLAYER_STATE_PLAYING ), E_INVALID_STATE, "TonePlayer is in an invalid state");
471 result r = E_SUCCESS;
472 int ret = AUDIO_IO_ERROR_NONE;
474 if (__pWorkerThread.get())
476 __pWorkerThread->Join();
477 __pWorkerThread.reset(null);
480 if (__tonePlayerState == TONE_PLAYER_STATE_PLAYING)
482 ret = audio_out_unprepare(__audioOutHandle);
483 r = MapExceptionToResult(ret);
484 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform audio_out_unprepare operation with 0x%x", GetErrorMessage(r), ret);
486 __tonePlayerState = TONE_PLAYER_STATE_STOPPED;
492 _TonePlayerImpl::Pause(void)
494 result r = E_SUCCESS;
495 int ret = AUDIO_IO_ERROR_NONE;
496 SysTryReturnResult(NID_MEDIA, (__tonePlayerState == TONE_PLAYER_STATE_PLAYING ), E_INVALID_STATE,
497 "TonePlayer instance is in an invalid state. State is %d", __tonePlayerState);
500 if (__pWorkerThread.get())
502 __pWorkerThread->Join();
503 __pWorkerThread.reset(null);
506 ret = audio_out_unprepare(__audioOutHandle);
507 r = MapExceptionToResult(ret);
508 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform audio_out_unprepare operation with 0x%x", GetErrorMessage(r), ret);
510 __tonePlayerState = TONE_PLAYER_STATE_PAUSED;
516 _TonePlayerImpl::Close(void)
518 result r = E_SUCCESS;
519 SysTryReturnResult(NID_MEDIA, (__tonePlayerState == TONE_PLAYER_STATE_OPENED ) || (__tonePlayerState == TONE_PLAYER_STATE_STOPPED ) ||
520 (__tonePlayerState == TONE_PLAYER_STATE_END_OF_TONE ), E_INVALID_STATE, "TonePlayer is in an invalid state");
522 if (__tonePlayerState == TONE_PLAYER_STATE_END_OF_TONE)
525 if (__pWorkerThread.get())
527 SysLog(NID_MEDIA, "Join is starting.");
528 __pWorkerThread->Join();
531 __tonePlayerState = TONE_PLAYER_STATE_CLOSED;
533 if (__audioOutHandle)
535 int ret = AUDIO_IO_ERROR_NONE;
536 ret = audio_out_unset_interrupted_cb(__audioOutHandle);
537 r = MapExceptionToResult(ret);
538 SysTryLog(NID_MEDIA, r == E_SUCCESS,"[%s] Failed to perform audio_out_unset_interrupted_cb operation with %x", GetErrorMessage(r), ret);
540 ret = audio_out_destroy(__audioOutHandle);
541 __audioOutHandle = null;
542 r = MapExceptionToResult(ret);
543 SysTryLog(NID_MEDIA, r == E_SUCCESS , "[%s] Failed to perform audio_out_destroy operation with %0x%x", GetErrorMessage(r), ret);
546 if (__pToneList.get() != null)
548 __pToneList->RemoveAll(true);
554 _TonePlayerImpl::SetVolume(int& volume)
556 result r = E_SUCCESS;
557 SysTryReturnResult(NID_MEDIA, (0 <= volume) && (volume <= MAX_TONE_PLAYER_VOLUME), E_OUT_OF_RANGE,
558 "volume is out of range. volume = %d", volume);
564 _TonePlayerImpl::GetVolume(void) const
566 SetLastResult(E_SUCCESS);
572 _TonePlayerImpl::GetState(void) const
574 SetLastResult(E_SUCCESS);
575 return __tonePlayerState;
579 _TonePlayerImpl::Feed(void)
582 unsigned char* pData = NULL;
583 result r = E_SUCCESS;
584 int ret = AUDIO_IO_ERROR_NONE;
585 bool isSuccesful = true;
590 __pToneBuffer->Clear();
591 if (!GetBuffer(*__pToneBuffer.get()))
593 __tonePlayerState = TONE_PLAYER_STATE_END_OF_TONE;
595 SysTryReturnVoidResult(NID_MEDIA, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
600 buffer_size = __pToneBuffer->GetLimit();
601 pData = (unsigned char*) __pToneBuffer->GetPointer();
602 SysTryCatch(NID_MEDIA, pData, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. Data Sent to TonePlayer is null");
605 ret = audio_out_write(__audioOutHandle, (void*) pData, buffer_size);
606 if (ret != buffer_size)
609 SysLog(NID_MEDIA, "Audio Device could only put %d data to device", ret);
624 _TonePlayerImpl::AudioIoInterrupted(audio_io_interrupted_code_e code, void *pUserData)
626 SysTryReturn(NID_MEDIA, pUserData, , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. pUserData is null.");
627 _TonePlayerImpl *pTonePlayerImpl = (_TonePlayerImpl *)pUserData;
631 case AUDIO_IO_INTERRUPTED_COMPLETED:
632 if (pTonePlayerImpl->__tonePlayerState == TONE_PLAYER_STATE_PAUSED || pTonePlayerImpl->__tonePlayerState == TONE_PLAYER_STATE_STOPPED)
634 if (!(pTonePlayerImpl->__interruptFlag))
636 pTonePlayerImpl->SendReleased();
638 pTonePlayerImpl->__interruptFlag = false;
641 case AUDIO_IO_INTERRUPTED_BY_CALL:
642 pTonePlayerImpl->__interruptFlag = true;
643 //Intentional Fall through
644 case AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG:
645 //Intentional Fall through
646 case AUDIO_IO_INTERRUPTED_BY_MEDIA:
647 //Intentional Fall through
648 case AUDIO_IO_INTERRUPTED_BY_RESOURCE_CONFLICT:
649 if (pTonePlayerImpl->__tonePlayerState == TONE_PLAYER_STATE_PLAYING)
651 pTonePlayerImpl->SendAudioFocusChanged();
654 case AUDIO_IO_INTERRUPTED_BY_ALARM:
655 //Intentional Fall through
656 case AUDIO_IO_INTERRUPTED_BY_EMERGENCY:
657 //Intentional Fall through
658 case AUDIO_IO_INTERRUPTED_BY_RESUMABLE_MEDIA:
659 if (pTonePlayerImpl->__tonePlayerState == TONE_PLAYER_STATE_PLAYING)
661 pTonePlayerImpl->SendInterrupted();
665 SysLog(NID_MEDIA, "Audio I/O Interrupt is not set.");
671 _TonePlayerImpl::GetBuffer(Tizen::Base::ByteBuffer& toneBuffer)
673 int bufferTobeFilledSize = __tonePlayerOptimalBufferSize;
674 int firstFrequency = 0;
675 int secondFrequency = 0;
676 long startSample = 0;
681 __sampleNumberArr[0] = __sampleNumberArr[1];
682 __sampleNumberArr[1] = __sampleNumber;
684 __tempRepeatCountArr[0] = __tempRepeatCountArr[1];
685 __tempRepeatCountArr[1] = __tempRepeatCount;
687 __toneListIndexArr[0] = __toneListIndexArr[1];
688 __toneListIndexArr[1] = __toneListIndex;
690 __endOftoneReachedArr[0] = __endOftoneReachedArr[1];
691 __endOftoneReachedArr[1] = __endOftoneReached;
693 // If already end of tone is reached generate silent buffer and return
694 if (__endOftoneReached)
696 bufferTobeFilledSize = __tonePlayerOptimalBufferSize - toneBuffer.GetPosition(); //buffer size to be filled is total size - already filled buffer size
699 _ToneGenerator::GenerateTone(toneBuffer, firstFrequency, secondFrequency, 0, bufferTobeFilledSize / _BYTES_PER_SAMPLE, __volume); //fill silent buffer
705 // Get the details of the buffer to be generated
706 if (!GetNextToneBufferDetails(bufferTobeFilledSize, firstFrequency, secondFrequency, startSample, endSample))
708 __endOftoneReached = true;
709 _ToneGenerator::GenerateTone(toneBuffer, firstFrequency, secondFrequency, startSample, endSample, __volume);
710 bufferTobeFilledSize = __tonePlayerOptimalBufferSize - toneBuffer.GetPosition();
713 _ToneGenerator::GenerateTone(toneBuffer, firstFrequency, secondFrequency, 0, bufferTobeFilledSize / _BYTES_PER_SAMPLE, __volume); //fill silent buffer
716 _ToneGenerator::GenerateTone(toneBuffer, firstFrequency, secondFrequency, startSample, endSample, __volume);
717 bufferTobeFilledSize = __tonePlayerOptimalBufferSize - toneBuffer.GetPosition();
719 // keep trying till the buffer gets filled
720 if (0 >= bufferTobeFilledSize)
730 _TonePlayerImpl::GetNextToneBufferDetails(int bufferTobeFilledSize, int& firstFrequency, int& secondFrequency, long& startSample, long& endSample)
732 bufferTobeFilledSize /= _BYTES_PER_SAMPLE;
734 // This case is for construct with just Tone
735 if (__pToneList.get() == null)
737 __pTone->GetFrequency(firstFrequency, secondFrequency);
738 __duration = __pTone->GetDuration();
740 if ((__sampleNumber + bufferTobeFilledSize) <= (int) (SAMPLING_RATE / 1000.0 * __duration)) //(SAMPLING_RATE/1000.0*__duration)) gives the total samples for the duration
742 startSample = __sampleNumber;
743 __sampleNumber += bufferTobeFilledSize; // if current sample number is less than total sample return samples to be generated
744 endSample = __sampleNumber;
749 startSample = __sampleNumber;
750 endSample = (int) (SAMPLING_RATE / 1000.0 * __duration);
753 if (0 >= __tempRepeatCount)
763 else // Construct with Tone List
765 Tone* pTone = dynamic_cast<Tone*>(__pToneList->GetAt(__pToneList->GetCount() - __toneListIndex));
769 SysLog(NID_MEDIA, "pTone is null");
773 pTone->GetFrequency(firstFrequency, secondFrequency);
774 __duration = pTone->GetDuration();
776 if ((__sampleNumber + bufferTobeFilledSize) <= (int) (SAMPLING_RATE / 1000.0 * __duration))
778 startSample = __sampleNumber;
779 __sampleNumber += bufferTobeFilledSize;
780 endSample = __sampleNumber;
785 startSample = __sampleNumber;
786 endSample = (int) (SAMPLING_RATE / 1000.0 * __duration);
789 if (0 >= __toneListIndex)
792 if (0 >= __tempRepeatCount)
798 __toneListIndex = __pToneList->GetCount();
812 _TonePlayerImpl::SendEndOfTone(void)
814 result r = E_SUCCESS;
815 int ret = AUDIO_IO_ERROR_NONE;
817 ret = audio_out_unprepare(__audioOutHandle);
818 r = MapExceptionToResult(ret);
819 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Failed to perform audio_out_unprepare operation with 0x%x", GetErrorMessage(r), ret);
821 _TonePlayerEventArg* pTonePlayerEventArg = null;
822 pTonePlayerEventArg = new (std::nothrow) _TonePlayerEventArg;
823 SysTryReturnResult(NID_MEDIA, pTonePlayerEventArg, E_OUT_OF_MEMORY, "Memory allocation failed.");
824 pTonePlayerEventArg->SetEventType(_TONEPLAYER_EVENT_END_OF_PLAY_REACHED);
825 r = __pTonePlayerEvent->FireAsync(*pTonePlayerEventArg);
826 SysTryCatch(NID_MEDIA, r == E_SUCCESS, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
829 delete pTonePlayerEventArg;
830 pTonePlayerEventArg = null;
836 _TonePlayerImpl::SendInterrupted(void)
838 result r = E_SUCCESS;
839 _TonePlayerEventArg* pTonePlayerEventArg = null;
840 pTonePlayerEventArg = new (std::nothrow) _TonePlayerEventArg;
841 SysTryReturnResult(NID_MEDIA, pTonePlayerEventArg, E_OUT_OF_MEMORY, "Memory allocation failed.");
843 pTonePlayerEventArg->SetEventType(_TONEPLAYER_EVENT_INTERRUPTED);
844 r = __pTonePlayerEvent->FireAsync(*pTonePlayerEventArg);
845 SysTryCatch(NID_MEDIA, r == E_SUCCESS, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
848 delete pTonePlayerEventArg;
849 pTonePlayerEventArg = null;
855 _TonePlayerImpl::SendAudioFocusChanged(void)
857 result r = E_SUCCESS;
858 _TonePlayerEventArg* pTonePlayerEventArg = null;
859 pTonePlayerEventArg = new (std::nothrow) _TonePlayerEventArg;
860 SysTryReturnResult(NID_MEDIA, pTonePlayerEventArg, E_OUT_OF_MEMORY, "Memory allocation failed.");
862 pTonePlayerEventArg->SetEventType(_TONEPLAYER_EVENT_AUDIO_FOCUS_CHAGNED);
863 r = __pTonePlayerEvent->FireAsync(*pTonePlayerEventArg);
864 SysTryCatch(NID_MEDIA, r == E_SUCCESS, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
867 delete pTonePlayerEventArg;
868 pTonePlayerEventArg = null;
874 _TonePlayerImpl::SendReleased(void)
876 result r = E_SUCCESS;
877 _TonePlayerEventArg* pTonePlayerEventArg = null;
878 pTonePlayerEventArg = new (std::nothrow) _TonePlayerEventArg;
879 SysTryReturnResult(NID_MEDIA, pTonePlayerEventArg, E_OUT_OF_MEMORY, "Memory allocation failed.");
880 pTonePlayerEventArg->SetEventType(_TONEPLAYER_EVENT_RELEASED);
881 r = __pTonePlayerEvent->FireAsync(*pTonePlayerEventArg);
882 SysTryCatch(NID_MEDIA, r == E_SUCCESS, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
885 delete pTonePlayerEventArg;
886 pTonePlayerEventArg = null;
892 _TonePlayerImpl::SendError(result reason)
894 result r = E_SUCCESS;
895 __tonePlayerState = TONE_PLAYER_STATE_ERROR;
896 _TonePlayerErrorArg* pTonePlayerErrorArg = NULL;
898 pTonePlayerErrorArg = new (std::nothrow) _TonePlayerErrorArg;
899 if (pTonePlayerErrorArg == null)
904 pTonePlayerErrorArg->SetEventType(_TONEPLAYER_EVENT_ERROR);
905 pTonePlayerErrorArg->SetError(reason);
906 r = __pTonePlayerEvent->FireAsync(*pTonePlayerErrorArg);
914 if (pTonePlayerErrorArg != null)
916 delete pTonePlayerErrorArg;
922 _TonePlayerImpl::MapExceptionToResult(int reason)
924 //All the fall through are intentional
928 case AUDIO_IO_ERROR_NONE:
932 case AUDIO_IO_ERROR_OUT_OF_MEMORY:
933 return E_OUT_OF_MEMORY;
936 case AUDIO_IO_ERROR_INVALID_PARAMETER:
937 return E_INVALID_ARG;
940 case AUDIO_IO_ERROR_INVALID_OPERATION:
941 return E_INVALID_OPERATION;
944 case AUDIO_IO_ERROR_DEVICE_NOT_OPENED:
945 return E_INVALID_STATE;
948 case AUDIO_IO_ERROR_INVALID_BUFFER:
949 return E_INVALID_ARG;
952 case AUDIO_IO_ERROR_DEVICE_NOT_CLOSED:
953 return E_INVALID_STATE;
956 case AUDIO_IO_ERROR_SOUND_POLICY:
957 return E_DEVICE_BUSY;