1 /* 2 * Copyright (c) 2002, Jerome Duval (jerome.duval@free.fr) 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef MULTI_AUDIO_NODE_H 6 #define MULTI_AUDIO_NODE_H 7 8 9 #include <BufferConsumer.h> 10 #include <BufferProducer.h> 11 #include <Controllable.h> 12 #include <MediaAddOn.h> 13 #include <MediaDefs.h> 14 #include <MediaEventLooper.h> 15 #include <MediaNode.h> 16 #include <Message.h> 17 #include <TimeSource.h> 18 19 #include "hmulti_audio.h" 20 #include "MultiAudioDevice.h" 21 22 class BDiscreteParameter; 23 class BParameterGroup; 24 25 class node_input; 26 class node_output; 27 28 29 class MultiAudioNode : public BBufferConsumer, public BBufferProducer, 30 public BTimeSource, public BMediaEventLooper, public BControllable { 31 protected: 32 virtual ~MultiAudioNode(); 33 34 public: 35 MultiAudioNode(BMediaAddOn* addon, const char* name, 36 MultiAudioDevice* device, int32 internalID, 37 BMessage* config); 38 39 virtual status_t InitCheck() const; 40 41 static void GetFlavor(flavor_info* info, int32 id); 42 static void GetFormat(media_format* outFormat); 43 44 status_t GetConfigurationFor(BMessage* message); 45 46 // BMediaNode methods 47 virtual BMediaAddOn* AddOn(int32* internalID) const; 48 virtual status_t HandleMessage(int32 message, const void* data, 49 size_t size); 50 51 protected: 52 virtual void Preroll(); 53 virtual void NodeRegistered(); 54 virtual status_t RequestCompleted(const media_request_info& info); 55 virtual void SetTimeSource(BTimeSource* timeSource); 56 57 // BBufferConsumer methods 58 59 virtual status_t AcceptFormat(const media_destination& dest, 60 media_format* format); 61 virtual status_t GetNextInput(int32* cookie, media_input* input); 62 virtual void DisposeInputCookie(int32 cookie); 63 virtual void BufferReceived(BBuffer* buffer); 64 virtual void ProducerDataStatus(const media_destination& forWhom, 65 int32 status, bigtime_t atPerformanceTime); 66 virtual status_t GetLatencyFor(const media_destination& forWhom, 67 bigtime_t* latency, media_node_id* timeSource); 68 virtual status_t Connected(const media_source& producer, 69 const media_destination& where, 70 const media_format& withFormat, 71 media_input* input); 72 virtual void Disconnected(const media_source& producer, 73 const media_destination& where); 74 virtual status_t FormatChanged(const media_source& producer, 75 const media_destination& consumer, 76 int32 changeTag, const media_format& format); 77 78 virtual status_t SeekTagRequested( 79 const media_destination& destination, 80 bigtime_t targetTime, uint32 flags, 81 media_seek_tag* _seekTag, bigtime_t* _taggedTime, 82 uint32* _flags); 83 84 // BBufferProducer methods 85 86 virtual status_t FormatSuggestionRequested(media_type type, 87 int32 quality, media_format* format); 88 89 virtual status_t FormatProposal(const media_source& output, 90 media_format* format); 91 92 virtual status_t FormatChangeRequested(const media_source& source, 93 const media_destination& destination, 94 media_format* io_format, int32* _deprecated); 95 virtual status_t GetNextOutput(int32* cookie, 96 media_output* out_output); 97 virtual status_t DisposeOutputCookie(int32 cookie); 98 99 virtual status_t SetBufferGroup(const media_source& for_source, 100 BBufferGroup* group); 101 102 virtual status_t PrepareToConnect(const media_source& what, 103 const media_destination& where, 104 media_format* format, media_source* source, 105 char* name); 106 107 virtual void Connect(status_t error, const media_source& source, 108 const media_destination& destination, 109 const media_format& format, char* name); 110 virtual void Disconnect(const media_source& what, 111 const media_destination& where); 112 113 virtual void LateNoticeReceived(const media_source& what, 114 bigtime_t howMuch, bigtime_t performanceTime); 115 116 virtual void EnableOutput(const media_source& what, bool enabled, 117 int32* _deprecated); 118 virtual void AdditionalBufferRequested(const media_source& source, 119 media_buffer_id previousBuffer, 120 bigtime_t previousTime, 121 const media_seek_tag* previousTag); 122 123 // BMediaEventLooper methods 124 virtual void HandleEvent(const media_timed_event* event, 125 bigtime_t lateness, bool realTimeEvent = false); 126 127 // BTimeSource methods 128 virtual void SetRunMode(run_mode mode); 129 virtual status_t TimeSourceOp(const time_source_op_info& op, 130 void *_reserved); 131 132 // BControllable methods 133 virtual status_t GetParameterValue(int32 id, bigtime_t* lastChange, 134 void* value, size_t* size); 135 virtual void SetParameterValue(int32 id, bigtime_t when, 136 const void* value, size_t size); 137 virtual BParameterWeb* MakeParameterWeb(); 138 139 private: 140 // private unimplemented 141 MultiAudioNode(const MultiAudioNode& clone); 142 MultiAudioNode& operator=(const MultiAudioNode& clone); 143 144 status_t _HandleStart(const media_timed_event* event, 145 bigtime_t lateness, bool realTimeEvent = false); 146 status_t _HandleSeek(const media_timed_event* event, 147 bigtime_t lateness, bool realTimeEvent = false); 148 status_t _HandleWarp(const media_timed_event* event, 149 bigtime_t lateness, bool realTimeEvent = false); 150 status_t _HandleStop(const media_timed_event* event, 151 bigtime_t lateness, bool realTimeEvent = false); 152 status_t _HandleBuffer(const media_timed_event* event, 153 bigtime_t lateness, bool realTimeEvent = false); 154 status_t _HandleDataStatus(const media_timed_event* event, 155 bigtime_t lateness, bool realTimeEvent = false); 156 status_t _HandleParameter(const media_timed_event* event, 157 bigtime_t lateness, bool realTimeEvent = false); 158 159 char* _PlaybackBuffer(int32 cycle, int32 channel) 160 { return fDevice->BufferList().playback_buffers 161 [cycle][channel].base; } 162 uint32 _PlaybackStride(int32 cycle, int32 channel) 163 { return fDevice->BufferList().playback_buffers 164 [cycle][channel].stride; } 165 166 void _WriteZeros(node_input& input, uint32 bufferCycle); 167 void _FillWithZeros(node_input& input); 168 void _FillNextBuffer(node_input& channel, 169 BBuffer* buffer); 170 171 static int32 _run_thread_(void* data); 172 int32 _RunThread(); 173 status_t _StartThread(); 174 status_t _StopThread(); 175 176 void _AllocateBuffers(node_output& channel); 177 BBuffer* _FillNextBuffer(multi_buffer_info& info, 178 node_output& channel); 179 void _UpdateTimeSource(multi_buffer_info& info, 180 multi_buffer_info& oldInfo, node_input& input); 181 182 node_output* _FindOutput(media_source source); 183 node_input* _FindInput(media_destination destination); 184 node_input* _FindInput(int32 destinationId); 185 186 const char* _GetControlName(multi_mix_control& control); 187 void _ProcessGroup(BParameterGroup* group, int32 index, 188 int32& numParameters); 189 void _ProcessMux(BDiscreteParameter* parameter, 190 int32 index); 191 192 private: 193 status_t fInitStatus; 194 195 BMediaAddOn* fAddOn; 196 int32 fId; 197 198 BList fInputs; 199 200 bigtime_t fLatency; 201 BList fOutputs; 202 media_format fPreferredFormat; 203 204 bigtime_t fInternalLatency; 205 // this is computed from the real (negotiated) chunk size and bit rate, 206 // not the defaults that are in the parameters 207 bigtime_t fBufferPeriod; 208 209 sem_id fBufferFreeSem; 210 thread_id fThread; 211 MultiAudioDevice* fDevice; 212 bool fTimeSourceStarted; 213 BParameterWeb* fWeb; 214 BMessage fConfig; 215 }; 216 217 #endif // MULTI_AUDIO_NODE_H 218