]> git.vomp.tv Git - vompclient.git/blob - audioplayer.h
Code cleaning
[vompclient.git] / audioplayer.h
1 /*
2     Copyright 2004-2006 Chris Tallon, Andreas Vogel
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #ifndef AUDIOPLAYER_H
22 #define AUDIOPLAYER_H
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifndef WIN32
27 #include <sys/time.h>
28 #endif
29 #include <time.h>
30
31 #include "audio.h"
32 #include "remote.h"
33 #include "vdr.h"
34 #include "callback.h"
35 #include "message.h"
36 #include "messagequeue.h"
37 #include "thread.h"
38 #include "afeed.h"
39 #include "timerreceiver.h"
40
41 #ifdef WIN32
42 #include "threadwin.h"
43 #else
44 #include "threadp.h"
45 #endif
46
47
48
49 class Boxx;
50 class DemuxerAudio;
51
52
53 class AudioPlayer : public Thread_TYPE, public Callback, public TimerReceiver
54 {
55   public:
56     //the instance method to create the
57     //player instance - only call thie for
58     //the first time in the main thread (no lock!)
59     //later you can change the view the player is connect with
60                 //a view that "goes" should call getInstance(NULL,false) to detach
61                 //the last media view should shutdown the player
62     static AudioPlayer * getInstance(Boxx * frontend, bool create=true);
63     //start the player thread
64     void run();
65
66     //is the player still running?
67     bool isPlayerRunning();
68
69     void shutdown();
70
71     //each of the commands works as a request
72     //only after getSequence returned the same sequence as those commands this is
73     //handled by the player and getError is valid
74     int play(const char * filename);
75     //stop the player without shutting it down
76     int stop();
77     int pause();
78     int unpause();
79                 int fastForward();
80                 int fastBackward();
81                 int jumpToPercent(double percent);
82                 int skipForward(int sec);
83                 int skipBackward(int sec);
84
85                 //info functions for frontend
86                 //delete provided String afterwards
87                 //get the played title from ID3 Tag  - or NULL
88                 char * getTitle();
89                 //get info
90                 //multi line String containing ID3 infos
91                 char * getID3Info();
92
93     //wait for a particular sequence to be handled
94     //timeout in s, returnes current sequence, -1 on timeout
95     int waitForSequence(int timeout, int sequence);
96     int getSequence();
97
98                 //info functions
99
100                 //get current position in s
101                 ULONG getCurrentTimes();
102                 //get song len in s
103                 ULONG getSonglen();
104                 //current bitrate
105                 int getCurrentBitrate();
106                 
107     
108     virtual void call(void * caller);
109
110     UCHAR getState() ;
111
112     const static UCHAR S_PLAY = 1;
113     const static UCHAR S_PAUSE = 2;
114     const static UCHAR S_POSITION = 3;
115                 //player finished a song - no reset, next will follow
116     const static UCHAR S_DONE=5;
117     const static UCHAR S_STOP = 6;
118     const static UCHAR S_ERROR = 8;
119     const static UCHAR S_FF = 9;
120     const static UCHAR S_BACK = 10;
121
122     //message parameters for frontend messages
123     const static ULONG CONNECTION_LOST=1;
124     const static ULONG STREAM_END=2;
125     const static ULONG STREAM_ERR=3;
126                 const static ULONG STATUS_CHANGE=4; //some info has been changed
127                 const static ULONG NEW_SONG=5; //some info has been changed
128                 const static ULONG SHORT_UPDATE=6; //timer info update
129
130     virtual void timercall(int reference);
131
132   protected:
133     void threadMethod();
134     void threadPostStopCleanup();
135
136   private:
137                 //to guess lengthes if the demux does not know
138                 const static ULONG DEFAULT_BITRATE=128000;
139     AudioPlayer(Boxx *frontend);
140     virtual ~AudioPlayer();
141     static AudioPlayer * instance;
142     bool playerRunnig;
143     Audio* audio;
144     VDR* vdr;
145     Log* logger;
146
147     DemuxerAudio *demuxer;
148     AFeed afeed;
149
150     //feeder control
151     const static int FEEDER_START=1;
152     const static int FEEDER_STOP=2;
153     const static int FEEDER_PAUSE=3;
154     const static int FEEDER_UNPAUSE=4;
155     void controlFeeder(int action) ;
156     int feederState;
157
158     //synchronized get/set methods for states
159     int setRequestedState(UCHAR st);
160     void setState(UCHAR st);
161
162     //to be called from within the thread
163     UCHAR checkState();
164
165                 //variables used by the thread
166                 UINT thisWrite;
167           UINT thisRead;
168     bool running;
169
170     UCHAR *threadBuffer;
171     UCHAR state;
172     UCHAR requestState;
173     ULONG streampos;
174     ULONG lenInBytes;
175                 ULONG bytesWritten;
176                 ULONG requestedStreampos;
177
178                 int skipfactor;
179     //the buffer len in bytes
180     const static int BUFLEN=50*1024;
181     Boxx *frontend;
182     //requested sequence
183     int requestedSequence;
184     //handled sequence
185     int sequence;
186     //startplay sequence
187     int currentPlaySequence;
188     //sequence that is changed for each new filename
189     int playSequence;
190
191     char * filename;
192     int openFile();
193     void handleVDRerror();
194
195     void sendFrontendMessage(ULONG para);
196
197     void waitTimed(int ms);
198
199
200 };
201
202 #endif
203