CAF state bug fix
[platform/framework/native/media.git] / src / FMedia_TonePlayerImpl.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 //
19 // @file                        FMedia_TonePlayerImpl.h
20 // @brief                       This header file contains the declaration related to TonePlayer class
21 //
22 //
23
24 #ifndef _FMEDIA_INTERNAL_TONEPLAYER_IMPL_H_
25 #define _FMEDIA_INTERNAL_TONEPLAYER_IMPL_H_
26 #include <memory>
27 #include <audio_io.h>
28 #include <unique_ptr.h>
29 #include <FBaseColIList.h>
30 #include <FBaseByteBuffer.h>
31 #include <FBaseColArrayList.h>
32 #include <FBaseRtThread.h>
33 #include <FBaseObject.h>
34 #include <FBaseRtMutex.h>
35 #include <FBaseRtIRunnable.h>
36 #include <FMediaTone.h>
37 #include <FMediaAudioManagerTypes.h>
38
39 #define MAX_TONE_PLAYER_OUT_CAPACITY 16
40
41 namespace Tizen { namespace Media
42 {
43 class TonePlayer;
44 class ITonePlayerEventListener;
45 class _TonePlayerWorker;
46 class _TonePlayerEvent;
47
48 class _TonePlayerImpl
49         : public virtual Tizen::Base::Object
50 {
51 public:
52         _TonePlayerImpl(void);
53         virtual ~_TonePlayerImpl(void);
54
55         result Construct(const Tizen::Media::ITonePlayerEventListener& listener, const Tizen::Media::TonePlayer& tonePlayerInstance);
56         result Open(const Tizen::Base::Collection::IList& toneList, int repeatCount = 1);
57         result Open(const Tizen::Media::Tone& tone, int repeatCount = 1);
58         result Play(void);
59         result Stop(void);
60         result Pause(void);
61         result SetVolume(int& volume);
62         int GetVolume(void) const;
63         result Close(void);
64         TonePlayerState GetState(void) const;
65         result SetAudioStreamType(AudioStreamType type);
66         void Feed(void);
67         static void AudioIoInterrupted(audio_io_interrupted_code_e code, void *pUserData);
68 private:
69         _TonePlayerImpl(const _TonePlayerImpl& rhs);
70         _TonePlayerImpl& operator =(const _TonePlayerImpl& rhs);
71
72         bool GetBuffer(Tizen::Base::ByteBuffer& toneBuffer);
73         bool GetNextToneBufferDetails(int bufferTobeFilledSize, int& firstFrequency, int& secondFrequency, long& startSample, long& endSample);
74         result SendEndOfTone(void);
75         result SendInterrupted(void);
76         result SendReleased(void);
77         result SendAudioFocusChanged();
78         result SendError(result r);
79         result MapExceptionToResult(int reason);
80
81         //sicne 3.0
82         static _TonePlayerImpl* GetInstance(TonePlayer* pTonePlayer);
83         static const _TonePlayerImpl* GetInstance(const TonePlayer* pTonePlayer);
84         static void InitInstanceMutex(void);
85         static int __noOfInstances;
86         static int __instanceCount;
87         std::unique_ptr<Tizen::Base::ByteBuffer> __pToneBuffer;
88         std::unique_ptr<Tizen::Base::Collection::ArrayList> __pToneList;
89         std::unique_ptr<_TonePlayerEvent> __pTonePlayerEvent;
90         std::unique_ptr<Tizen::Base::Runtime::Thread> __pWorkerThread;
91         std::unique_ptr<_TonePlayerWorker> __pTonePlayerWorker;
92         std::unique_ptr<Tone> __pTone;
93         static std::unique_ptr<Tizen::Base::Runtime::Mutex> __pMutex;
94         static bool __isInstanceMutexInitialized;
95
96         Tizen::Media::ITonePlayerEventListener* __pTonePlayerEventListener;
97         int __repeatCount;
98         int __tempRepeatCount;
99         int __toneListIndex;
100         int __volume;
101         int __minBufferSize;
102         long __duration;
103         long __sampleNumber;
104         TonePlayerState __tonePlayerState;
105         int __tobePlayedBuffCount;
106         bool __endOftoneReached;
107
108         long __sampleNumberArr[2];
109         int __tempRepeatCountArr[2];
110         int __toneListIndexArr[2];
111         bool __endOftoneReachedArr[2];
112         audio_out_h __audioOutHandle;
113         int __tonePlayerOptimalBufferSize;
114         bool __stop;
115         AudioStreamType __audioStreamtype;
116         bool __interruptFlag;
117
118 };      //end of _TonePlayerImp
119
120
121 class _TonePlayerWorker
122         : public virtual Tizen::Base::Object
123         , public virtual Tizen::Base::Runtime::IRunnable
124 {
125 public:
126         _TonePlayerWorker(void)
127         {
128                 _pTonePlayerImpl = null;
129         }
130         void Initialize(_TonePlayerImpl* pTonePlayerImpl)
131         {
132                 _pTonePlayerImpl = pTonePlayerImpl;
133         }
134         virtual Tizen::Base::Object* Run(void)
135         {
136                 if (_pTonePlayerImpl)
137                 {
138                         _pTonePlayerImpl->Feed();
139                 }
140                 return this;
141         }
142 protected:
143         _TonePlayerImpl* _pTonePlayerImpl;
144 };      //end of _TonePlayerWorker
145
146 }}//Tizen::Media
147
148 #endif