NXWidgets  1.19
cmediaplayer.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/nxwm/include/cmediaplayer.hxx
3  *
4  * Copyright (C) 2013 Ken Pettit. All rights reserved.
5  * Copyright (C) 2014 Gregory Nutt. All rights reserved.
6  * Author: Ken Pettit <pettitkd@gmail.com>
7  * Gregory Nutt <gnutt@nuttx.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
20  * me be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  ****************************************************************************/
37 
38 #ifndef __INCLUDE_CMEDIAPLAYER_HXX
39 #define __INCLUDE_CMEDIAPLAYER_HXX
40 
41 /****************************************************************************
42  * Included Files
43  ****************************************************************************/
44 
45 #include <nuttx/config.h>
46 
47 #include <sys/types.h>
48 #include <nuttx/nx/nxtk.h>
49 #include <nuttx/nx/nxterm.h>
50 
51 #include "cnxfont.hxx"
52 #include "cimage.hxx"
53 #include "cstickyimage.hxx"
54 #include "clabel.hxx"
55 #include "clistbox.hxx"
56 #include "clistboxdataitem.hxx"
58 
59 #include "iapplication.hxx"
60 #include "capplicationwindow.hxx"
61 #include "ctaskbar.hxx"
62 
63 /****************************************************************************
64  * Pre-Processor Definitions
65  ****************************************************************************/
66 
67 #define NXWM_MEDIAPLAYER_NROWS 6
68 #define NXWM_MEDIAPLAYER_NCOLUMNS 6
69 
70 /****************************************************************************
71  * Implementation Classes
72  ****************************************************************************/
73 
74 #if defined(__cplusplus)
75 
76 namespace NxWM
77 {
78  /**
79  * This class implements the CMediaPlayer application.
80  */
81 
82  class CMediaPlayer : public IApplication,
83  private IApplicationCallback,
85  {
86  private:
87  /**
88  * This enumeration identifies the state of the media player
89  *
90  * State Transition Table
91  * ---------+----------+----------+----------+----------+----------+----------+
92  * | FILE | FILE | | | FAST | |
93  * STATE | SELECTED |DESELECTED| PLAY | PAUSE | FORWARD | REWIND |
94  * ---------+----------+----------+----------+----------+----------+----------+
95  * STOPPED | STAGED | X | X | X | X | X |
96  * STAGED | STAGED | STOPPED | PLAYING | X | X | X |
97  * PLAYING | X | X | X | PAUSED |FFORWARD2 | REWIND2 |
98  * PAUSED | STAGED | STOPPED | PLAYING | X |FFORWARD1 | REWIND1 |
99  * FFORWARD1| X | X | PAUSED | X |FFORWARD1 | REWIND1 |
100  * REWIND1 | X | X | PAUSED | X |FFORWARD1 | REWIND1 |
101  * FFORWARD2| X | X | X | PLAYING | PLAYING | REWIND1 |
102  * REWIND2 | X | X | X | PLAYING |FFORWARD1 | PLAYING |
103  * ---------+----------+----------+----------+----------+----------+----------+
104  *
105  * Configuration Dependencies. States in the above state transition table may
106  * not be supported if any of the following features are excluded from the
107  * configuration:
108  *
109  * CONFIG_AUDIO_EXCLUDE_STOP
110  * CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
111  * CONFIG_AUDIO_EXCLUDE_VOLUME
112  * CONFIG_AUDIO_EXCLUDE_FFORWARD
113  * CONFIG_AUDIO_EXCLUDE_REWIND
114  */
115 
117  {
118  MPLAYER_STOPPED = 0, /**< No media file has been selected */
119  MPLAYER_STAGED, /**< Media file selected, not playing */
120  MPLAYER_PLAYING, /**< Playing a media file */
121  MPLAYER_PAUSED, /**< Playing a media file but paused */
122  MPLAYER_FFORWARD, /**< Fast forwarding through a media file */
123  MPLAYER_FREWIND, /**< Rewinding a media file */
124  };
125 
126  /**
127  * Describes the state of an image touch. Some image touch cannot be
128  * processed until the image contact is lost. This enumeration arms and
129  * manages those cases.
130  */
131 
133  {
134  PENDING_NONE = 0, /**< Nothing is pending */
135  PENDING_PLAY_RELEASE, /**< Expect play image to be released */
136  PENDING_PAUSE_RELEASE /**< Expect pause image to be released */
137  };
138 
139  /**
140  * The structure defines a pending operation.
141  */
142 
144  {
145  int64_t value; /**< Accumulated value */
146  uint8_t operation; /**< Identifies the operations */
147  };
148 
149  /**
150  * Media player state data.
151  */
152 
153  FAR struct nxplayer_s *m_player; /**< NxPlayer handle */
154  enum EMediaPlayerState m_state; /**< Media player current state */
155  enum EMediaPlayerState m_prevState; /**< Media player previous state */
156  enum EPendingRelease m_pending; /**< Pending image release event */
157  NXWidgets::CNxString m_filePath; /**< The full path to the selected file */
158  int m_fileIndex; /**< Last selected text box selection */
159  bool m_fileReady; /**< True: Ready to play */
160 #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
161  uint8_t m_level; /**< Current volume level, range 0-100 */
162 #endif
163 #if !defined(CONFIG_AUDIO_EXCLUDE_FFORWARD) || !defined(CONFIG_AUDIO_EXCLUDE_REWIND)
164  uint8_t m_subSample; /**< Current FFFORWARD subsampling index */
165 #endif
166 
167  /**
168  * Media player geometry.
169  */
170 
171  struct nxgl_size_s m_windowSize; /**< The size of the media player window */
172 
173  /**
174  * Cached constructor parameters.
175  */
176 
177  CTaskbar *m_taskbar; /**< Reference to the "parent" taskbar */
178  CApplicationWindow *m_window; /**< Reference to the application window */
179 
180  /**
181  * Widgets
182  */
183 
184  NXWidgets::CListBox *m_listbox; /**< List box containing media files selections */
185  NXWidgets::CNxFont *m_font; /**< The font used in the media player */
186  NXWidgets::CImage *m_play; /**< Play control */
187  NXWidgets::CImage *m_pause; /**< Pause control */
188  NXWidgets::CStickyImage *m_rewind; /**< Rewind control */
189  NXWidgets::CStickyImage *m_fforward; /**< Fast forward control */
191 #if !defined(CONFIG_AUDIO_EXCLUDE_FFORWARD) || !defined(CONFIG_AUDIO_EXCLUDE_REWIND)
192  NXWidgets::CLabel *m_speed; /**< FForward/rewind speed */
193 #endif
194 
195  /**
196  * Bitmaps
197  *
198  * These are retained only so that they can be released when the media
199  * is closed player
200  */
201 
202  NXWidgets::CRlePaletteBitmap *m_playBitmap; /**< Bitmap for the play control */
203  NXWidgets::CRlePaletteBitmap *m_pauseBitmap; /**< Bitmap for the pause control */
204  NXWidgets::CRlePaletteBitmap *m_rewindBitmap; /**< Bitmap for the rewind control */
205  NXWidgets::CRlePaletteBitmap *m_fforwardBitmap; /**< Bitmap for the fast forward control */
206  NXWidgets::CRlePaletteBitmap *m_volumeBitmap; /**< Volume control grip bitmap */
207 
208  /**
209  * Get the full media file path and make ready for playing. Called
210  * after a file has been selected from the list box.
211  */
212 
213  bool getMediaFile(const NXWidgets::CListBoxDataItem *item);
214 
215  /**
216  * Get the full media file path and make ready for playing. Called
217  * after a file has been selected from the list box.
218  */
219 
220  bool openMediaFile(const NXWidgets::CListBoxDataItem *item);
221 
222  /**
223  * Stop playing the current file. Called when a new media file is selected,
224  * when a media file is de-selected, or when destroying the media player
225  * instance.
226  */
227 
228  void stopPlaying(void);
229 
230  /**
231  * Select the geometry of the media player given the current window size.
232  * Only called as part of construction.
233  */
234 
235  inline void setGeometry(void);
236 
237  /**
238  * Load media files into the list box.
239  */
240 
241  inline bool showMediaFiles(FAR const char *mediaPath);
242 
243 #ifdef CONFIG_NXPLAYER_INCLUDE_PREFERRED_DEVICE
244  /**
245  * Set the preferred audio device for playback
246  */
247 
248  inline bool setDevice(FAR const char *devPath);
249 #endif
250 
251  /**
252  * Configure the NxPlayer.
253  */
254 
255  inline bool configureNxPlayer(void);
256 
257  /**
258  * Create the Media Player controls. Only start as part of the application
259  * start method.
260  */
261 
262  inline bool createPlayer(void);
263 
264  /**
265  * Called when the window minimize image is pressed.
266  */
267 
268  void minimize(void);
269 
270  /**
271  * Called when the window minimize close is pressed.
272  */
273 
274  void close(void);
275 
276  /**
277  * Redraw all widgets. Called from redraw() and also on any state
278  * change.
279  *
280  * @param state The new state to enter.
281  */
282 
283  void redrawWidgets(void);
284 
285  /**
286  * Transition to a new media player state.
287  *
288  * @param state The new state to enter.
289  */
290 
291  void setMediaPlayerState(enum EMediaPlayerState state);
292 
293 #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
294  /**
295  * Set the new volume level based on the position of the volume slider.
296  */
297 
298  void setVolumeLevel(void);
299 #endif
300 
301  /**
302  * Check if a new file has been selected (or de-selected) in the list box
303  */
304 
305  inline void checkFileSelection(void);
306 
307 #if !defined(CONFIG_AUDIO_EXCLUDE_FFORWARD) || !defined(CONFIG_AUDIO_EXCLUDE_REWIND)
308  /**
309  * Update fast forward/rewind speed indicator. Called on each state
310  * change and after each change in the speed of motion.
311  */
312 
313  void updateMotionIndicator(void);
314 #endif
315 
316  /**
317  * Handle a widget action event. For this application, that means image
318  * pre-release events.
319  *
320  * @param e The event data.
321  */
322 
324 
325  /**
326  * Handle a widget release event. Only the play and pause image release
327  * are of interest.
328  */
329 
331 
332  /**
333  * Handle a widget release event when the widget WAS dragged outside of
334  * its original bounding box. Only the play and pause image release
335  * are of interest.
336  */
337 
339 
340  /**
341  * Handle value changes. This will get events when there is a change in the
342  * volume level or a file is selected or deselected.
343  */
344 
346 
347  public:
348  /**
349  * CMediaPlayer constructor
350  *
351  * @param window. The application window
352  *
353  * @param taskbar. A pointer to the parent task bar instance
354  * @param window. The window to be used by this application.
355  */
356 
357  CMediaPlayer(CTaskbar *taskbar, CApplicationWindow *window);
358 
359  /**
360  * CMediaPlayer destructor
361  */
362 
363  ~CMediaPlayer(void);
364 
365  /**
366  * Each implementation of IApplication must provide a method to recover
367  * the contained CApplicationWindow instance.
368  */
369 
370  IApplicationWindow *getWindow(void) const;
371 
372  /**
373  * Get the icon associated with the application
374  *
375  * @return An instance if IBitmap that may be used to rend the
376  * application's icon. This is an new IBitmap instance that must
377  * be deleted by the caller when it is no long needed.
378  */
379 
381 
382  /**
383  * Get the name string associated with the application
384  *
385  * @return A copy if CNxString that contains the name of the application.
386  */
387 
389 
390  /**
391  * Start the application (perhaps in the minimized state).
392  *
393  * @return True if the application was successfully started.
394  */
395 
396  bool run(void);
397 
398  /**
399  * Stop the application.
400  */
401 
402  void stop(void);
403 
404  /**
405  * Destroy the application and free all of its resources. This method
406  * will initiate blocking of messages from the NX server. The server
407  * will flush the window message queue and reply with the blocked
408  * message. When the block message is received by CWindowMessenger,
409  * it will send the destroy message to the start window task which
410  * will, finally, safely delete the application.
411  */
412 
413  void destroy(void);
414 
415  /**
416  * The application window is hidden (either it is minimized or it is
417  * maximized, but not at the top of the hierarchy
418  */
419 
420  void hide(void);
421 
422  /**
423  * Redraw the entire window. The application has been maximized or
424  * otherwise moved to the top of the hierarchy. This method is call from
425  * CTaskbar when the application window must be displayed
426  */
427 
428  void redraw(void);
429 
430  /**
431  * Report of this is a "normal" window or a full screen window. The
432  * primary purpose of this method is so that window manager will know
433  * whether or not it show draw the task bar.
434  *
435  * @return True if this is a full screen window.
436  */
437 
438  bool isFullScreen(void) const;
439  };
440 
442  {
443  private:
444  CTaskbar *m_taskbar; /**< The taskbar */
445 
446  public:
447  /**
448  * CMediaPlayerFactory Constructor
449  *
450  * @param taskbar. The taskbar instance used to terminate calibration
451  */
452 
453  CMediaPlayerFactory(CTaskbar *taskbar);
454 
455  /**
456  * CMediaPlayerFactory Destructor
457  */
458 
459  inline ~CMediaPlayerFactory(void) { }
460 
461  /**
462  * Create a new instance of an CMediaPlayer (as IApplication).
463  */
464 
465  IApplication *create(void);
466 
467  /**
468  * Get the icon associated with the application
469  *
470  * @return An instance if IBitmap that may be used to rend the
471  * application's icon. This is an new IBitmap instance that must
472  * be deleted by the caller when it is no long needed.
473  */
474 
476  };
477 }
478 #endif // __cplusplus
479 
480 #endif // __INCLUDE_CMEDIAPLAYER_HXX
bool getMediaFile(const NXWidgets::CListBoxDataItem *item)
NXWidgets::CStickyImage * m_rewind
void updateMotionIndicator(void)
NXWidgets::CRlePaletteBitmap * m_volumeBitmap
bool createPlayer(void)
enum EMediaPlayerState m_state
void handleReleaseOutsideEvent(const NXWidgets::CWidgetEventArgs &e)
void checkFileSelection(void)
NXWidgets::CListBox * m_listbox
CMediaPlayer(CTaskbar *taskbar, CApplicationWindow *window)
NXWidgets::CImage * m_play
bool setDevice(FAR const char *devPath)
bool isFullScreen(void) const
void handleActionEvent(const NXWidgets::CWidgetEventArgs &e)
bool showMediaFiles(FAR const char *mediaPath)
NXWidgets::CRlePaletteBitmap * m_rewindBitmap
NXWidgets::CStickyImage * m_fforward
void setVolumeLevel(void)
NXWidgets::CRlePaletteBitmap * m_fforwardBitmap
NXWidgets::CNxFont * m_font
void handleReleaseEvent(const NXWidgets::CWidgetEventArgs &e)
NXWidgets::CImage * m_pause
IApplicationWindow * getWindow(void) const
enum EPendingRelease m_pending
void setMediaPlayerState(enum EMediaPlayerState state)
void stopPlaying(void)
FAR struct nxplayer_s * m_player
bool configureNxPlayer(void)
NXWidgets::CLabel * m_speed
NXWidgets::CGlyphSliderHorizontal * m_volume
enum EMediaPlayerState m_prevState
NXWidgets::CRlePaletteBitmap * m_pauseBitmap
bool openMediaFile(const NXWidgets::CListBoxDataItem *item)
NXWidgets::IBitmap * getIcon(void)
NXWidgets::CRlePaletteBitmap * m_playBitmap
struct nxgl_size_s m_windowSize
NXWidgets::CNxString m_filePath
NXWidgets::CNxString getName(void)
void setGeometry(void)
CApplicationWindow * m_window
void handleValueChangeEvent(const NXWidgets::CWidgetEventArgs &e)
void redrawWidgets(void)