1/*
2 * Copyright (C) 2013 Apple, 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer
12 *    in the documentation and/or other materials provided with the
13 *    distribution.
14 * 3. Neither the name of Ericsson nor the names of its contributors
15 *    may be used to endorse or promote products derived from this
16 *    software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#if ENABLE(MEDIA_STREAM)
34
35#include "MediaStreamCenterMac.h"
36
37#include "AVCaptureDeviceManager.h"
38#include "MediaStreamCreationClient.h"
39#include "MediaStreamPrivate.h"
40#include "MediaStreamTrackSourcesRequestClient.h"
41#include <wtf/MainThread.h>
42
43namespace WebCore {
44
45MediaStreamCenter& MediaStreamCenter::platformCenter()
46{
47    ASSERT(isMainThread());
48    DEPRECATED_DEFINE_STATIC_LOCAL(MediaStreamCenterMac, center, ());
49    return center;
50}
51
52MediaStreamCenterMac::MediaStreamCenterMac()
53{
54}
55
56MediaStreamCenterMac::~MediaStreamCenterMac()
57{
58}
59
60void MediaStreamCenterMac::validateRequestConstraints(PassRefPtr<MediaStreamCreationClient> prpQueryClient, PassRefPtr<MediaConstraints> audioConstraints, PassRefPtr<MediaConstraints> videoConstraints)
61{
62    RefPtr<MediaStreamCreationClient> client = prpQueryClient;
63
64    ASSERT(client);
65
66    if (audioConstraints) {
67        String invalidConstraint;
68        AVCaptureDeviceManager::shared().verifyConstraintsForMediaType(MediaStreamSource::Audio, audioConstraints.get(), invalidConstraint);
69        if (!invalidConstraint.isEmpty()) {
70            client->constraintsInvalid(invalidConstraint);
71            return;
72        }
73    }
74
75    if (videoConstraints) {
76        String invalidConstraint;
77        AVCaptureDeviceManager::shared().verifyConstraintsForMediaType(MediaStreamSource::Video, videoConstraints.get(), invalidConstraint);
78        if (!invalidConstraint.isEmpty()) {
79            client->constraintsInvalid(invalidConstraint);
80            return;
81        }
82    }
83
84    client->constraintsValidated();
85}
86
87void MediaStreamCenterMac::createMediaStream(PassRefPtr<MediaStreamCreationClient> prpQueryClient, PassRefPtr<MediaConstraints> audioConstraints, PassRefPtr<MediaConstraints> videoConstraints)
88{
89    RefPtr<MediaStreamCreationClient> client = prpQueryClient;
90
91    ASSERT(client);
92
93    Vector<RefPtr<MediaStreamSource>> audioSources;
94    Vector<RefPtr<MediaStreamSource>> videoSources;
95
96    if (audioConstraints) {
97        String invalidConstraint;
98        AVCaptureDeviceManager::shared().verifyConstraintsForMediaType(MediaStreamSource::Audio, audioConstraints.get(), invalidConstraint);
99        if (!invalidConstraint.isEmpty()) {
100            client->failedToCreateStreamWithConstraintsError(invalidConstraint);
101            return;
102        }
103
104        RefPtr<MediaStreamSource> audioSource = AVCaptureDeviceManager::shared().bestSourceForTypeAndConstraints(MediaStreamSource::Audio, audioConstraints.get());
105        ASSERT(audioSource);
106
107        audioSources.append(audioSource.release());
108    }
109
110    if (videoConstraints) {
111        String invalidConstraint;
112        AVCaptureDeviceManager::shared().verifyConstraintsForMediaType(MediaStreamSource::Video, videoConstraints.get(), invalidConstraint);
113        if (!invalidConstraint.isEmpty()) {
114            client->failedToCreateStreamWithConstraintsError(invalidConstraint);
115            return;
116        }
117
118        RefPtr<MediaStreamSource> videoSource = AVCaptureDeviceManager::shared().bestSourceForTypeAndConstraints(MediaStreamSource::Video, videoConstraints.get());
119        ASSERT(videoSource);
120
121        videoSources.append(videoSource.release());
122    }
123
124    client->didCreateStream(MediaStreamPrivate::create(audioSources, videoSources));
125}
126
127bool MediaStreamCenterMac::getMediaStreamTrackSources(PassRefPtr<MediaStreamTrackSourcesRequestClient> prpClient)
128{
129    RefPtr<MediaStreamTrackSourcesRequestClient> requestClient = prpClient;
130
131    Vector<RefPtr<TrackSourceInfo>> sources = AVCaptureDeviceManager::shared().getSourcesInfo(requestClient->requestOrigin());
132
133    requestClient->didCompleteRequest(sources);
134    return true;
135}
136
137} // namespace WebCore
138
139#endif // ENABLE(MEDIA_STREAM)
140