1 /******************************************************************************* 2 * Goggles Audio Player Library * 3 ******************************************************************************** 4 * Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved * 5 * --- * 6 * This program 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 3 of the License, or * 9 * (at your option) any later version. * 10 * * 11 * This program 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 this program. If not, see http://www.gnu.org/licenses. * 18 ********************************************************************************/ 19 #ifndef MEM_PACKET_H 20 #define MEM_PACKET_H 21 22 #include "ap_buffer.h" 23 #include "ap_event.h" 24 #include "ap_signal.h" 25 #include "ap_format.h" 26 27 namespace ap { 28 29 class Packet; 30 31 /* 32 class Stream { 33 FXint id; 34 FXlong length; 35 FXlong position; 36 }; 37 */ 38 39 class PacketPool { 40 protected: 41 FXLFQueueOf<Packet> packets; 42 Semaphore semaphore; 43 public: 44 /// Constructor 45 PacketPool(); 46 47 /// Initialize pool 48 FXbool init(FXival sz,FXival n); 49 50 /// free pool 51 void free(); 52 53 /// Block until packet is available or signal is set 54 Packet * wait(const Signal &); 55 56 /// Put event back into pool 57 void push(Packet*); 58 59 /// Destructor 60 ~PacketPool(); 61 }; 62 63 64 class Packet : public Event, public MemoryBuffer { 65 friend class PacketPool; 66 protected: 67 PacketPool* pool; 68 public: 69 AudioFormat af; 70 FXuchar flags; 71 FXlong stream_position; 72 FXlong stream_length; 73 protected: 74 Packet(PacketPool*,FXival sz); 75 virtual ~Packet(); 76 public: 77 virtual void unref(); 78 79 void reset(); 80 full()81 FXbool full() const { return (af.framesize() > space()); } 82 numFrames()83 FXint numFrames() const { return size() / af.framesize(); } 84 availableFrames()85 FXint availableFrames() const { return space() / af.framesize(); } 86 wroteFrames(FXint nframes)87 void wroteFrames(FXint nframes) { wroteBytes(nframes*af.framesize()); } 88 appendFrames(const FXuchar * buf,FXival nframes)89 void appendFrames(const FXuchar * buf,FXival nframes) { append(buf,af.framesize()*nframes); } 90 copyFrames(FXuchar * & buf,FXint & nframes)91 FXint copyFrames(FXuchar *& buf, FXint & nframes) { 92 FXint n = FXMIN(nframes,availableFrames()); 93 if (n) { 94 const FXint nbytes = af.framesize()*n; 95 append(buf,nbytes); 96 nframes-=n; 97 buf+=nbytes; 98 } 99 return n; 100 } 101 102 103 }; 104 105 } 106 107 #endif 108