Copiste  0.1
 All Classes Functions Variables Enumerations Friends Pages
streamplayer.h
1 /*
2  * This file is part of Copiste.
3  *
4  * Copiste is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Copiste is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Copiste. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #ifndef INCLUDEDSTREAMPLAYERH
20 #define INCLUDEDSTREAMPLAYERH
21 
22 #include <vlc/vlc.h>
23 
25 #include <QMutex>
26 
27 #include <boost/thread.hpp>
28 #include <boost/date_time.hpp>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <sstream>
32 #include <string>
33 
34 #include "features/spectrum.h"
35 
36 const int DEFAULT_AUDIO_CHUNK_SIZE = 1024;
37 const int VLC_DEFAULT_VOLUME = 80;
38 
39 using namespace std;
40 
41 class StreamPlayer;
42 
43 // Callbacks audio
44 void handleStream(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate,
45  unsigned int nb_samples, unsigned int bits_per_sample, unsigned int size, int64_t pts);
46 void prepareRender(void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size);
47 
61 {
62  public:
63  // Set up functions
64 
66  StreamPlayer(bool live = false, bool verbose = true);
68  ~StreamPlayer();
70  string url() { return mUrl; }
72  void setUrl(string url) { mUrl = url; }
74  void setOverlapping(float factor);
76  void setChunkSize(int size);
78  int chunkSize() { return mChunkSize; }
79 
81  void play();
83  libvlc_time_t playingTime();
85  libvlc_time_t totalTime();
87  void stop();
89  bool isLive() { return mLive; }
91  void setVolume(int vol);
93  int volume();
94 
95  // Computing functions : designed to be overloaded by the user
96 
98  virtual void sequenceStarts() { ; }
100  virtual void useBuffer() { ; }
102  virtual void sequenceEnds() { ; }
103 
104  // Handling functions
105 
110  static uint16_t* convert8to16(const uint8_t* source, int size);
115  static uint16_t* average(uint16_t* source, int size, int passes, int scale= 1);
117  static void reduce(uint16_t* source, uint16_t* dest, int size, int passes, int scale=1);
119  static void addOffset(uint16_t* source, uint16_t* dest, int size, int offset);
121  static int pow2(int n);
122 
124  void watch();
125 
127  inline uint16_t buffer(int i);
129  inline int bufferSize();
131  inline void fillBuffer(uint16_t value);
133  inline void flushBuffer();
134 
136  std::deque<uint16_t> mBuffer;
137  QMutex mLock;
138 
139  // Prerender callback
140  char* mAudioData;
141  unsigned int mAudioDataSize;
142  unsigned int mFrequency; // detected from VLC
143 
144  protected:
145  bool mVerbose;
146 
147  private:
148  // Parameters
149  string mUrl;
150  bool mPlaying;
151  bool mLive;
152  float mOverlapping;
153 
154  int mChunkSize;
155  int mFramesOverlap;
156 
157  // VLC
158  libvlc_instance_t *mVlcInstance;
159  libvlc_media_player_t *mMp;
160  libvlc_media_t *mMedia;
161 
162  boost::thread mWatchThread;
163  boost::mutex mPlayingLock;
164 };
165 
214 #endif