AudioNodeOutput.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2010, Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifndef AudioNodeOutput_h
  25. #define AudioNodeOutput_h
  26. #include "AudioBus.h"
  27. #include "AudioNode.h"
  28. #include "AudioParam.h"
  29. #include <wtf/HashSet.h>
  30. #include <wtf/RefPtr.h>
  31. #include <wtf/Vector.h>
  32. namespace WebCore {
  33. class AudioContext;
  34. class AudioNodeInput;
  35. // AudioNodeOutput represents a single output for an AudioNode.
  36. // It may be connected to one or more AudioNodeInputs.
  37. class AudioNodeOutput {
  38. public:
  39. // It's OK to pass 0 for numberOfChannels in which case setNumberOfChannels() must be called later on.
  40. AudioNodeOutput(AudioNode*, unsigned numberOfChannels);
  41. // Can be called from any thread.
  42. AudioNode* node() const { return m_node; }
  43. AudioContext* context() { return m_node->context(); }
  44. // Causes our AudioNode to process if it hasn't already for this render quantum.
  45. // It returns the bus containing the processed audio for this output, returning inPlaceBus if in-place processing was possible.
  46. // Called from context's audio thread.
  47. AudioBus* pull(AudioBus* inPlaceBus, size_t framesToProcess);
  48. // bus() will contain the rendered audio after pull() is called for each rendering time quantum.
  49. // Called from context's audio thread.
  50. AudioBus* bus() const;
  51. // renderingFanOutCount() is the number of AudioNodeInputs that we're connected to during rendering.
  52. // Unlike fanOutCount() it will not change during the course of a render quantum.
  53. unsigned renderingFanOutCount() const;
  54. // renderingParamFanOutCount() is the number of AudioParams that we're connected to during rendering.
  55. // Unlike paramFanOutCount() it will not change during the course of a render quantum.
  56. unsigned renderingParamFanOutCount() const;
  57. // Must be called with the context's graph lock.
  58. void disconnectAll();
  59. void setNumberOfChannels(unsigned);
  60. unsigned numberOfChannels() const { return m_numberOfChannels; }
  61. bool isChannelCountKnown() const { return numberOfChannels() > 0; }
  62. bool isConnected() { return fanOutCount() > 0 || paramFanOutCount() > 0; }
  63. // Disable/Enable happens when there are still JavaScript references to a node, but it has otherwise "finished" its work.
  64. // For example, when a note has finished playing. It is kept around, because it may be played again at a later time.
  65. // They must be called with the context's graph lock.
  66. void disable();
  67. void enable();
  68. // updateRenderingState() is called in the audio thread at the start or end of the render quantum to handle any recent changes to the graph state.
  69. // It must be called with the context's graph lock.
  70. void updateRenderingState();
  71. private:
  72. AudioNode* m_node;
  73. friend class AudioNodeInput;
  74. friend class AudioParam;
  75. // These are called from AudioNodeInput.
  76. // They must be called with the context's graph lock.
  77. void addInput(AudioNodeInput*);
  78. void removeInput(AudioNodeInput*);
  79. void addParam(AudioParam*);
  80. void removeParam(AudioParam*);
  81. // fanOutCount() is the number of AudioNodeInputs that we're connected to.
  82. // This method should not be called in audio thread rendering code, instead renderingFanOutCount() should be used.
  83. // It must be called with the context's graph lock.
  84. unsigned fanOutCount();
  85. // Similar to fanOutCount(), paramFanOutCount() is the number of AudioParams that we're connected to.
  86. // This method should not be called in audio thread rendering code, instead renderingParamFanOutCount() should be used.
  87. // It must be called with the context's graph lock.
  88. unsigned paramFanOutCount();
  89. // Must be called with the context's graph lock.
  90. void disconnectAllInputs();
  91. void disconnectAllParams();
  92. // updateInternalBus() updates m_internalBus appropriately for the number of channels.
  93. // It is called in the constructor or in the audio thread with the context's graph lock.
  94. void updateInternalBus();
  95. // Announce to any nodes we're connected to that we changed our channel count for its input.
  96. // It must be called in the audio thread with the context's graph lock.
  97. void propagateChannelCount();
  98. // updateNumberOfChannels() is called in the audio thread at the start or end of the render quantum to pick up channel changes.
  99. // It must be called with the context's graph lock.
  100. void updateNumberOfChannels();
  101. // m_numberOfChannels will only be changed in the audio thread.
  102. // The main thread sets m_desiredNumberOfChannels which will later get picked up in the audio thread in updateNumberOfChannels().
  103. unsigned m_numberOfChannels;
  104. unsigned m_desiredNumberOfChannels;
  105. // m_internalBus and m_inPlaceBus must only be changed in the audio thread with the context's graph lock (or constructor).
  106. RefPtr<AudioBus> m_internalBus;
  107. RefPtr<AudioBus> m_inPlaceBus;
  108. // If m_isInPlace is true, use m_inPlaceBus as the valid AudioBus; If false, use the default m_internalBus.
  109. bool m_isInPlace;
  110. HashSet<AudioNodeInput*> m_inputs;
  111. typedef HashSet<AudioNodeInput*>::iterator InputsIterator;
  112. bool m_isEnabled;
  113. // For the purposes of rendering, keeps track of the number of inputs and AudioParams we're connected to.
  114. // These value should only be changed at the very start or end of the rendering quantum.
  115. unsigned m_renderingFanOutCount;
  116. unsigned m_renderingParamFanOutCount;
  117. HashSet<RefPtr<AudioParam> > m_params;
  118. typedef HashSet<RefPtr<AudioParam> >::iterator ParamsIterator;
  119. };
  120. } // namespace WebCore
  121. #endif // AudioNodeOutput_h