This source file includes following definitions.
- parseOptions
- create
- m_errorCallback
- audio
- video
- audioConstraints
- videoConstraints
- ownerDocument
- start
- succeed
- failPermissionDenied
- failConstraint
- failUASpecific
- contextDestroyed
#include "config.h"
#include "modules/mediastream/UserMediaRequest.h"
#include "bindings/v8/Dictionary.h"
#include "bindings/v8/ExceptionMessages.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/SpaceSplitString.h"
#include "modules/mediastream/MediaConstraintsImpl.h"
#include "modules/mediastream/MediaStream.h"
#include "modules/mediastream/UserMediaController.h"
#include "platform/mediastream/MediaStreamCenter.h"
#include "platform/mediastream/MediaStreamDescriptor.h"
namespace WebCore {
static blink::WebMediaConstraints parseOptions(const Dictionary& options, const String& mediaType, ExceptionState& exceptionState)
{
blink::WebMediaConstraints constraints;
Dictionary constraintsDictionary;
bool ok = options.get(mediaType, constraintsDictionary);
if (ok && !constraintsDictionary.isUndefinedOrNull())
constraints = MediaConstraintsImpl::create(constraintsDictionary, exceptionState);
else {
bool mediaRequested = false;
options.get(mediaType, mediaRequested);
if (mediaRequested)
constraints = MediaConstraintsImpl::create();
}
return constraints;
}
PassRefPtr<UserMediaRequest> UserMediaRequest::create(ExecutionContext* context, UserMediaController* controller, const Dictionary& options, PassOwnPtr<NavigatorUserMediaSuccessCallback> successCallback, PassOwnPtr<NavigatorUserMediaErrorCallback> errorCallback, ExceptionState& exceptionState)
{
blink::WebMediaConstraints audio = parseOptions(options, "audio", exceptionState);
if (exceptionState.hadException())
return nullptr;
blink::WebMediaConstraints video = parseOptions(options, "video", exceptionState);
if (exceptionState.hadException())
return nullptr;
if (audio.isNull() && video.isNull()) {
exceptionState.throwDOMException(SyntaxError, "At least one of audio and video must be requested");
return nullptr;
}
return adoptRef(new UserMediaRequest(context, controller, audio, video, successCallback, errorCallback));
}
UserMediaRequest::UserMediaRequest(ExecutionContext* context, UserMediaController* controller, blink::WebMediaConstraints audio, blink::WebMediaConstraints video, PassOwnPtr<NavigatorUserMediaSuccessCallback> successCallback, PassOwnPtr<NavigatorUserMediaErrorCallback> errorCallback)
: ContextLifecycleObserver(context)
, m_audio(audio)
, m_video(video)
, m_controller(controller)
, m_successCallback(successCallback)
, m_errorCallback(errorCallback)
{
}
UserMediaRequest::~UserMediaRequest()
{
}
bool UserMediaRequest::audio() const
{
return !m_audio.isNull();
}
bool UserMediaRequest::video() const
{
return !m_video.isNull();
}
blink::WebMediaConstraints UserMediaRequest::audioConstraints() const
{
return m_audio;
}
blink::WebMediaConstraints UserMediaRequest::videoConstraints() const
{
return m_video;
}
Document* UserMediaRequest::ownerDocument()
{
if (ExecutionContext* context = executionContext()) {
return toDocument(context);
}
return 0;
}
void UserMediaRequest::start()
{
if (m_controller)
m_controller->requestUserMedia(this);
}
void UserMediaRequest::succeed(PassRefPtr<MediaStreamDescriptor> streamDescriptor)
{
if (!executionContext())
return;
RefPtr<MediaStream> stream = MediaStream::create(executionContext(), streamDescriptor);
MediaStreamTrackVector audioTracks = stream->getAudioTracks();
for (MediaStreamTrackVector::iterator iter = audioTracks.begin(); iter != audioTracks.end(); ++iter) {
(*iter)->component()->source()->setConstraints(m_audio);
}
MediaStreamTrackVector videoTracks = stream->getVideoTracks();
for (MediaStreamTrackVector::iterator iter = videoTracks.begin(); iter != videoTracks.end(); ++iter) {
(*iter)->component()->source()->setConstraints(m_video);
}
m_successCallback->handleEvent(stream.get());
}
void UserMediaRequest::failPermissionDenied(const String& message)
{
if (!executionContext())
return;
RefPtr<NavigatorUserMediaError> error = NavigatorUserMediaError::create(NavigatorUserMediaError::NamePermissionDenied, message, String());
m_errorCallback->handleEvent(error.get());
}
void UserMediaRequest::failConstraint(const String& constraintName, const String& message)
{
ASSERT(!constraintName.isEmpty());
if (!executionContext())
return;
RefPtr<NavigatorUserMediaError> error = NavigatorUserMediaError::create(NavigatorUserMediaError::NameConstraintNotSatisfied, message, constraintName);
m_errorCallback->handleEvent(error.get());
}
void UserMediaRequest::failUASpecific(const String& name, const String& message, const String& constraintName)
{
ASSERT(!name.isEmpty());
if (!executionContext())
return;
RefPtr<NavigatorUserMediaError> error = NavigatorUserMediaError::create(name, message, constraintName);
m_errorCallback->handleEvent(error.get());
}
void UserMediaRequest::contextDestroyed()
{
RefPtr<UserMediaRequest> protect(this);
if (m_controller) {
m_controller->cancelUserMediaRequest(this);
m_controller = 0;
}
ContextLifecycleObserver::contextDestroyed();
}
}