MediaElementAudioSourceNode.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2011, 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. #include "config.h"
  25. #if ENABLE(WEB_AUDIO) && ENABLE(VIDEO)
  26. #include "MediaElementAudioSourceNode.h"
  27. #include "AudioContext.h"
  28. #include "AudioNodeOutput.h"
  29. #include "Logging.h"
  30. #include "MediaPlayer.h"
  31. #include <wtf/Locker.h>
  32. // These are somewhat arbitrary limits, but we need to do some kind of sanity-checking.
  33. const unsigned minSampleRate = 8000;
  34. const unsigned maxSampleRate = 192000;
  35. namespace WebCore {
  36. PassRefPtr<MediaElementAudioSourceNode> MediaElementAudioSourceNode::create(AudioContext* context, HTMLMediaElement* mediaElement)
  37. {
  38. return adoptRef(new MediaElementAudioSourceNode(context, mediaElement));
  39. }
  40. MediaElementAudioSourceNode::MediaElementAudioSourceNode(AudioContext* context, HTMLMediaElement* mediaElement)
  41. : AudioNode(context, context->sampleRate())
  42. , m_mediaElement(mediaElement)
  43. , m_sourceNumberOfChannels(0)
  44. , m_sourceSampleRate(0)
  45. {
  46. // Default to stereo. This could change depending on what the media element .src is set to.
  47. addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
  48. setNodeType(NodeTypeMediaElementAudioSource);
  49. initialize();
  50. }
  51. MediaElementAudioSourceNode::~MediaElementAudioSourceNode()
  52. {
  53. m_mediaElement->setAudioSourceNode(0);
  54. uninitialize();
  55. }
  56. void MediaElementAudioSourceNode::setFormat(size_t numberOfChannels, float sourceSampleRate)
  57. {
  58. if (numberOfChannels != m_sourceNumberOfChannels || sourceSampleRate != m_sourceSampleRate) {
  59. if (!numberOfChannels || numberOfChannels > AudioContext::maxNumberOfChannels() || sourceSampleRate < minSampleRate || sourceSampleRate > maxSampleRate) {
  60. // process() will generate silence for these uninitialized values.
  61. LOG(Media, "MediaElementAudioSourceNode::setFormat(%u, %f) - unhandled format change", static_cast<unsigned>(numberOfChannels), sourceSampleRate);
  62. m_sourceNumberOfChannels = 0;
  63. m_sourceSampleRate = 0;
  64. return;
  65. }
  66. m_sourceNumberOfChannels = numberOfChannels;
  67. m_sourceSampleRate = sourceSampleRate;
  68. // Synchronize with process().
  69. Locker<MediaElementAudioSourceNode> locker(*this);
  70. if (sourceSampleRate != sampleRate()) {
  71. double scaleFactor = sourceSampleRate / sampleRate();
  72. m_multiChannelResampler = adoptPtr(new MultiChannelResampler(scaleFactor, numberOfChannels));
  73. } else {
  74. // Bypass resampling.
  75. m_multiChannelResampler.clear();
  76. }
  77. {
  78. // The context must be locked when changing the number of output channels.
  79. AudioContext::AutoLocker contextLocker(context());
  80. // Do any necesssary re-configuration to the output's number of channels.
  81. output(0)->setNumberOfChannels(numberOfChannels);
  82. }
  83. }
  84. }
  85. void MediaElementAudioSourceNode::process(size_t numberOfFrames)
  86. {
  87. AudioBus* outputBus = output(0)->bus();
  88. if (!mediaElement() || !m_sourceNumberOfChannels || !m_sourceSampleRate) {
  89. outputBus->zero();
  90. return;
  91. }
  92. // Use a tryLock() to avoid contention in the real-time audio thread.
  93. // If we fail to acquire the lock then the HTMLMediaElement must be in the middle of
  94. // reconfiguring its playback engine, so we output silence in this case.
  95. MutexTryLocker tryLocker(m_processLock);
  96. if (tryLocker.locked()) {
  97. if (AudioSourceProvider* provider = mediaElement()->audioSourceProvider()) {
  98. if (m_multiChannelResampler.get()) {
  99. ASSERT(m_sourceSampleRate != sampleRate());
  100. m_multiChannelResampler->process(provider, outputBus, numberOfFrames);
  101. } else {
  102. // Bypass the resampler completely if the source is at the context's sample-rate.
  103. ASSERT(m_sourceSampleRate == sampleRate());
  104. provider->provideInput(outputBus, numberOfFrames);
  105. }
  106. } else {
  107. // Either this port doesn't yet support HTMLMediaElement audio stream access,
  108. // or the stream is not yet available.
  109. outputBus->zero();
  110. }
  111. } else {
  112. // We failed to acquire the lock.
  113. outputBus->zero();
  114. }
  115. }
  116. void MediaElementAudioSourceNode::reset()
  117. {
  118. }
  119. void MediaElementAudioSourceNode::lock()
  120. {
  121. ref();
  122. m_processLock.lock();
  123. }
  124. void MediaElementAudioSourceNode::unlock()
  125. {
  126. m_processLock.unlock();
  127. deref();
  128. }
  129. } // namespace WebCore
  130. #endif // ENABLE(WEB_AUDIO)