LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SoundPlayer.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
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 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef SOUNDPLAYER_H
23 #define SOUNDPLAYER_H
24 
25 #include "Common.h"
26 
27 #include <string>
28 #include <list>
29 #include <glib.h>
30 #include <pthread.h>
31 
32 #include "sptr.h"
33 
37 #define BOOST_NO_TYPEID
38 #define BOOST_NO_RTTI
39 
40 #include <media/LunaConnector.h>
41 #include <media/MediaClient.h>
42 #include <media/MediaPlayer.h>
43 
44 #include "Timer.h"
45 
46 using namespace media;
47 using namespace boost;
48 using namespace std;
49 
54 class SoundPlayer : public RefCounted, public LunaConnector {
55 
56  /*
57  * This listener merely forwards notifications to SoundPlayer object
58  * Can't use multiple inheritance, because it's refcounted via boost::shared_ptr
59  */
60  class SoundPlayerMediaPlayerChangeListener : public MediaPlayerChangeListener
61  {
62  public:
63  SoundPlayerMediaPlayerChangeListener(SoundPlayer * player) : m_soundPlayer(player) {}
64 
65  void disconnect() { m_soundPlayer = 0; }
66 
67  virtual void currentTimeChanged() { if (m_soundPlayer) m_soundPlayer->currentTimeChanged(); }
68  virtual void eosChanged() { if (m_soundPlayer) m_soundPlayer->eosChanged(); }
69  virtual void sourceChanged() { if (m_soundPlayer) m_soundPlayer->sourceChanged(); }
70  virtual void errorChanged() { if (m_soundPlayer) m_soundPlayer->errorChanged(); }
71  virtual void extendedErrorChanged() { if (m_soundPlayer) m_soundPlayer->extendedErrorChanged(); }
72 
73  private:
74  SoundPlayer * m_soundPlayer;
75  };
76 
77  enum EState
78  {
79  // Created, but nothing happened yet
80  eState_Init,
81 
82  // Waiting for connection
83  eState_Connecting,
84 
85  // Connected, waiting for a request
86  eState_Connected,
87 
88  // Play request sent, but not playing yet
89  eState_PlayPending,
90 
91  // Playing. Audio is flowing out...
92  eState_Playing,
93 
94  // Stop request issued. Waiting for completion
95  eState_Closing,
96 
97  // Done: we're all done successfully (you can try to reuse us)
98  eState_Finished,
99 
100  // Dead: we're finished (successful or not), and don't try to use us again!
101  eState_Dead
102  };
103 
104 public:
105 
109  SoundPlayer();
110  ~SoundPlayer();
111 
112  void play(const std::string& filePath, const std::string& streamClass, bool repeat, int duration);
113  void stop();
114 
115  bool dead() const { return m_state == eState_Dead; }
116 
117  static int m_numInstances; // to keep count of number of instantiations
118 
119 protected:
120 
125  virtual LSPalmService * connectToBus();
126  virtual void connected();
127 
132  void currentTimeChanged();
133  void eosChanged();
134  void sourceChanged();
135  void errorChanged();
136  void extendedErrorChanged();
137 
138 private:
142  bool healthCheck();
143  void checkForNextStep();
144  void setState(EState state);
145  const char * getStateName();
146  guint64 currentTime();
147  void startTimer();
148  string getURI();
149  void onError();
150 
151  LSHandle * getServiceHandle();
152 
153 private:
154  shared_ptr<MediaPlayer> m_player;
155  LSPalmService* m_serviceHandle;
156  string m_filePath;
157  string m_streamClass;
158  AudioStreamClass m_audioStreamClass;
159  bool m_repeat;
160  int m_retries;
161  float m_duration;
162  EState m_state;
163  guint64 m_lastPlayingTime;
164  int m_fakeBackupRingtoneCount;
165  boost::shared_ptr<MediaPlayerChangeListener> m_mediaPlayerChangeListener;
166 
167  Timer<SoundPlayer> m_timer;
168  int m_activityID;
169 
170  static int m_activityCount;
171 };
172 
173 #endif /* SOUNDPLAYER_H */