2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / audio-alsa.h
blobfae4c7c0de96a5571d7fc3ed37081eaf3514b38a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * audio-alsa.h:
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2008 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
13 #if INCLUDE_ALSA
15 #ifndef __AUDIO_ALSA_H__
16 #define __AUDIO_ALSA_H__
18 #include <pthread.h>
19 #include <poll.h>
20 #include <asoundlib.h>
22 #include "audio.h"
23 #include "mutex.h"
25 class AlsaPlayer;
27 class AlsaSource : public AudioSource {
28 AlsaPlayer *player;
29 snd_pcm_t *pcm;
30 snd_pcm_uframes_t period_size;
31 snd_pcm_uframes_t buffer_size;
33 Mutex mutex;
35 bool initialized;
36 bool mmap;
37 bool started;
38 bool drop_pending;
40 bool SetupHW ();
41 bool PreparePcm (snd_pcm_sframes_t *avail);
43 // Underrun recovery
44 // Handles EPIPE and ESTRPIPE, prints an error if recovery failed
45 // or if err isn't any of the above values.
46 bool XrunRecovery (int err);
48 bool WriteRW ();
49 bool WriteMmap ();
51 void Drain ();
53 bool InitializeAlsa ();
54 void CloseAlsa ();
56 protected:
57 virtual ~AlsaSource ();
59 virtual void Played ();
60 virtual void Paused ();
61 virtual void Stopped ();
62 virtual void StateChanged (AudioState old_state);
63 virtual guint64 GetDelayInternal ();
65 virtual bool InitializeInternal ();
66 virtual void CloseInternal ();
68 public:
69 pollfd *udfs;
70 int ndfs;
72 AlsaSource (AlsaPlayer *player, MediaPlayer *mplayer, AudioStream *stream);
74 // Pushes data onto the pcm device if the
75 // device can accept more data, and if the
76 // there is data availabe. The node has to
77 // be locked during playback.
78 // Returns false if nothing has been played.
79 bool WriteAlsa ();
81 bool IsDropPending () { return drop_pending; }
82 void DropAlsa ();
85 class AlsaPlayer : public AudioPlayer {
86 // The audio thread
87 pthread_t *audio_thread;
88 bool shutdown; // set to true to exit the audio thread.
90 // A list of all the file descriptors in all the
91 // audio nodes. We need to poll on changes in any of the
92 // descriptors, so we create a big list with all of them
93 // and poll on that.
94 pollfd *udfs;
95 int ndfs;
97 // We also need to be able to wake up from the poll
98 // whenever we want to, so we create a pipe which we
99 // poll on. This is always the first file descriptor
100 // in udfs.
101 int fds [2];
103 // If UpdatePollList must be called before polling.
104 bool update_poll_pending;
106 // The audio loop which is executed
107 // on the audio thread.
108 void Loop ();
109 static void *Loop (void *data);
111 void WakeUp (); // Wakes up the audio thread.
113 protected:
114 virtual ~AlsaPlayer ();
116 virtual void AddInternal (AudioSource *node);
117 virtual void RemoveInternal (AudioSource *node);
118 virtual void PrepareShutdownInternal ();
119 virtual void FinishShutdownInternal ();
120 virtual bool Initialize ();
121 virtual AudioSource *CreateNode (MediaPlayer *mplayer, AudioStream *stream);
123 public:
124 AlsaPlayer ();
126 void UpdatePollList ();
128 static bool IsInstalled ();
131 #endif /* __AUDI_ALSA_H__ */
133 #endif /* INCLUDE_ALSA */