dom/media/webaudio/MediaElementAudioSourceNode.cpp
author alwu <alwu@mozilla.com>
Fri, 11 Jan 2019 20:43:01 +0000
changeset 510635 c34e287f2f7ca98ad1945804c2a20f61249b36af
parent 509471 6a89226d8c15f0b8b9dbcb23f76f371fdefc289b
child 519769 f41cee9bf14931b453838d1bbcbda528e3b064e8
permissions -rw-r--r--
Bug 1513733 - part2 : try to start AudioContext when media element which is as a source for web audio starts r=cpearce,karlt If media element is used as a source for AudioContext, we would try to start AudioContext which was not allowed to start when media element starts playing. Differential Revision: https://phabricator.services.mozilla.com/D14593

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MediaElementAudioSourceNode.h"
#include "mozilla/dom/MediaElementAudioSourceNodeBinding.h"
#include "AudioDestinationNode.h"
#include "nsIScriptError.h"
#include "AudioNodeStream.h"

namespace mozilla {
namespace dom {

MediaElementAudioSourceNode::MediaElementAudioSourceNode(AudioContext* aContext)
    : MediaStreamAudioSourceNode(aContext) {}

/* static */ already_AddRefed<MediaElementAudioSourceNode>
MediaElementAudioSourceNode::Create(
    AudioContext& aAudioContext, const MediaElementAudioSourceOptions& aOptions,
    ErrorResult& aRv) {
  if (aAudioContext.IsOffline()) {
    aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
    return nullptr;
  }

  if (aAudioContext.CheckClosed(aRv)) {
    return nullptr;
  }

  RefPtr<MediaElementAudioSourceNode> node =
      new MediaElementAudioSourceNode(&aAudioContext);

  RefPtr<DOMMediaStream> stream = aOptions.mMediaElement->CaptureAudio(
      aRv, aAudioContext.Destination()->Stream()->Graph());
  if (aRv.Failed()) {
    return nullptr;
  }

  node->Init(stream, aRv);
  if (aRv.Failed()) {
    return nullptr;
  }

  node->ListenForAllowedToPlay(aOptions);
  return node.forget();
}

JSObject* MediaElementAudioSourceNode::WrapObject(
    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return MediaElementAudioSourceNode_Binding::Wrap(aCx, this, aGivenProto);
}

void MediaElementAudioSourceNode::ListenForAllowedToPlay(
    const MediaElementAudioSourceOptions& aOptions) {
  aOptions.mMediaElement->GetAllowedToPlayPromise()
      ->Then(
          AbstractMainThread(), __func__,
          // Capture by reference to bypass the mozilla-refcounted-inside-lambda
          // static analysis. We capture a non-owning reference so as to allow
          // cycle collection of the node. The reference is cleared via
          // DisconnectIfExists() from Destroy() when the node is collected.
          [& self = *this]() {
            self.Context()->StartBlockedAudioContextIfAllowed();
            self.mAllowedToPlayRequest.Complete();
          })
      ->Track(mAllowedToPlayRequest);
}

void MediaElementAudioSourceNode::Destroy() {
  mAllowedToPlayRequest.DisconnectIfExists();
  MediaStreamAudioSourceNode::Destroy();
}

}  // namespace dom
}  // namespace mozilla