MediaStreamAudioSourceNode.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2012, 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(MEDIA_STREAM)
  26. #include "MediaStreamAudioSourceNode.h"
  27. #include "AudioContext.h"
  28. #include "AudioNodeOutput.h"
  29. #include "Logging.h"
  30. #include <wtf/Locker.h>
  31. namespace WebCore {
  32. PassRefPtr<MediaStreamAudioSourceNode> MediaStreamAudioSourceNode::create(AudioContext* context, MediaStream* mediaStream, AudioSourceProvider* audioSourceProvider)
  33. {
  34. return adoptRef(new MediaStreamAudioSourceNode(context, mediaStream, audioSourceProvider));
  35. }
  36. MediaStreamAudioSourceNode::MediaStreamAudioSourceNode(AudioContext* context, MediaStream* mediaStream, AudioSourceProvider* audioSourceProvider)
  37. : AudioNode(context, context->sampleRate())
  38. , m_mediaStream(mediaStream)
  39. , m_audioSourceProvider(audioSourceProvider)
  40. , m_sourceNumberOfChannels(0)
  41. {
  42. // Default to stereo. This could change depending on the format of the MediaStream's audio track.
  43. addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
  44. setNodeType(NodeTypeMediaStreamAudioSource);
  45. initialize();
  46. }
  47. MediaStreamAudioSourceNode::~MediaStreamAudioSourceNode()
  48. {
  49. uninitialize();
  50. }
  51. void MediaStreamAudioSourceNode::setFormat(size_t numberOfChannels, float sourceSampleRate)
  52. {
  53. if (numberOfChannels != m_sourceNumberOfChannels || sourceSampleRate != sampleRate()) {
  54. // The sample-rate must be equal to the context's sample-rate.
  55. if (!numberOfChannels || numberOfChannels > AudioContext::maxNumberOfChannels() || sourceSampleRate != sampleRate()) {
  56. // process() will generate silence for these uninitialized values.
  57. LOG(Media, "MediaStreamAudioSourceNode::setFormat(%u, %f) - unhandled format change", static_cast<unsigned>(numberOfChannels), sourceSampleRate);
  58. m_sourceNumberOfChannels = 0;
  59. return;
  60. }
  61. // Synchronize with process().
  62. MutexLocker locker(m_processLock);
  63. m_sourceNumberOfChannels = numberOfChannels;
  64. {
  65. // The context must be locked when changing the number of output channels.
  66. AudioContext::AutoLocker contextLocker(context());
  67. // Do any necesssary re-configuration to the output's number of channels.
  68. output(0)->setNumberOfChannels(numberOfChannels);
  69. }
  70. }
  71. }
  72. void MediaStreamAudioSourceNode::process(size_t numberOfFrames)
  73. {
  74. AudioBus* outputBus = output(0)->bus();
  75. if (!audioSourceProvider()) {
  76. outputBus->zero();
  77. return;
  78. }
  79. if (!mediaStream() || m_sourceNumberOfChannels != outputBus->numberOfChannels()) {
  80. outputBus->zero();
  81. return;
  82. }
  83. // Use a tryLock() to avoid contention in the real-time audio thread.
  84. // If we fail to acquire the lock then the MediaStream must be in the middle of
  85. // a format change, so we output silence in this case.
  86. MutexTryLocker tryLocker(m_processLock);
  87. if (tryLocker.locked())
  88. audioSourceProvider()->provideInput(outputBus, numberOfFrames);
  89. else {
  90. // We failed to acquire the lock.
  91. outputBus->zero();
  92. }
  93. }
  94. void MediaStreamAudioSourceNode::reset()
  95. {
  96. }
  97. } // namespace WebCore
  98. #endif // ENABLE(WEB_AUDIO) && ENABLE(MEDIA_STREAM)