1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modules/audio_output_devices/HTMLMediaElementAudioOutputDevice.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
#include "bindings/core/v8/ScriptState.h"
#include "core/dom/DOMException.h"
#include "core/dom/ExecutionContext.h"
#include "modules/audio_output_devices/AudioOutputDeviceClient.h"
#include "modules/audio_output_devices/SetSinkIdCallbacks.h"
#include "public/platform/WebSecurityOrigin.h"
#include "wtf/PtrUtil.h"
#include <memory>
namespace blink {
namespace {
class SetSinkIdResolver : public ScriptPromiseResolver {
WTF_MAKE_NONCOPYABLE(SetSinkIdResolver);
public:
static SetSinkIdResolver* create(ScriptState*,
HTMLMediaElement&,
const String& sinkId);
~SetSinkIdResolver() override = default;
void startAsync();
DECLARE_VIRTUAL_TRACE();
private:
SetSinkIdResolver(ScriptState*, HTMLMediaElement&, const String& sinkId);
void timerFired(TimerBase*);
Member<HTMLMediaElement> m_element;
String m_sinkId;
Timer<SetSinkIdResolver> m_timer;
};
SetSinkIdResolver* SetSinkIdResolver::create(ScriptState* scriptState,
HTMLMediaElement& element,
const String& sinkId) {
SetSinkIdResolver* resolver =
new SetSinkIdResolver(scriptState, element, sinkId);
resolver->suspendIfNeeded();
resolver->keepAliveWhilePending();
return resolver;
}
SetSinkIdResolver::SetSinkIdResolver(ScriptState* scriptState,
HTMLMediaElement& element,
const String& sinkId)
: ScriptPromiseResolver(scriptState),
m_element(element),
m_sinkId(sinkId),
m_timer(this, &SetSinkIdResolver::timerFired) {}
void SetSinkIdResolver::startAsync() {
m_timer.startOneShot(0, BLINK_FROM_HERE);
}
void SetSinkIdResolver::timerFired(TimerBase* timer) {
ExecutionContext* context = getExecutionContext();
ASSERT(context && context->isDocument());
std::unique_ptr<SetSinkIdCallbacks> callbacks =
WTF::wrapUnique(new SetSinkIdCallbacks(this, *m_element, m_sinkId));
WebMediaPlayer* webMediaPlayer = m_element->webMediaPlayer();
if (webMediaPlayer) {
// Using release() to transfer ownership because |webMediaPlayer| is a
// platform object that takes raw pointers.
webMediaPlayer->setSinkId(m_sinkId,
WebSecurityOrigin(context->getSecurityOrigin()),
callbacks.release());
} else {
if (AudioOutputDeviceClient* client =
AudioOutputDeviceClient::from(context)) {
client->checkIfAudioSinkExistsAndIsAuthorized(context, m_sinkId,
std::move(callbacks));
} else {
// The context has been detached. Impossible to get a security origin to
// check.
DCHECK(context->isContextDestroyed());
reject(DOMException::create(
SecurityError,
"Impossible to authorize device for detached context"));
}
}
}
DEFINE_TRACE(SetSinkIdResolver) {
visitor->trace(m_element);
ScriptPromiseResolver::trace(visitor);
}
} // namespace
HTMLMediaElementAudioOutputDevice::HTMLMediaElementAudioOutputDevice()
: m_sinkId("") {}
String HTMLMediaElementAudioOutputDevice::sinkId(HTMLMediaElement& element) {
HTMLMediaElementAudioOutputDevice& aodElement =
HTMLMediaElementAudioOutputDevice::from(element);
return aodElement.m_sinkId;
}
void HTMLMediaElementAudioOutputDevice::setSinkId(const String& sinkId) {
m_sinkId = sinkId;
}
ScriptPromise HTMLMediaElementAudioOutputDevice::setSinkId(
ScriptState* scriptState,
HTMLMediaElement& element,
const String& sinkId) {
SetSinkIdResolver* resolver =
SetSinkIdResolver::create(scriptState, element, sinkId);
ScriptPromise promise = resolver->promise();
if (sinkId == HTMLMediaElementAudioOutputDevice::sinkId(element))
resolver->resolve();
else
resolver->startAsync();
return promise;
}
const char* HTMLMediaElementAudioOutputDevice::supplementName() {
return "HTMLMediaElementAudioOutputDevice";
}
HTMLMediaElementAudioOutputDevice& HTMLMediaElementAudioOutputDevice::from(
HTMLMediaElement& element) {
HTMLMediaElementAudioOutputDevice* supplement =
static_cast<HTMLMediaElementAudioOutputDevice*>(
Supplement<HTMLMediaElement>::from(element, supplementName()));
if (!supplement) {
supplement = new HTMLMediaElementAudioOutputDevice();
provideTo(element, supplementName(), supplement);
}
return *supplement;
}
DEFINE_TRACE(HTMLMediaElementAudioOutputDevice) {
Supplement<HTMLMediaElement>::trace(visitor);
}
} // namespace blink
|