1 /*
2 * Copyright (C) 2011 Ericsson AB. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of Ericsson nor the names of its contributors
16 * may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33
34 #include "platform/mediastream/MediaStreamCenter.h"
35
36 #include "platform/mediastream/MediaStreamDescriptor.h"
37 #include "platform/mediastream/MediaStreamTrackSourcesRequest.h"
38 #include "platform/mediastream/MediaStreamWebAudioSource.h"
39 #include "public/platform/Platform.h"
40 #include "public/platform/WebAudioSourceProvider.h"
41 #include "public/platform/WebMediaStream.h"
42 #include "public/platform/WebMediaStreamCenter.h"
43 #include "public/platform/WebMediaStreamTrack.h"
44 #include "public/platform/WebMediaStreamTrackSourcesRequest.h"
45 #include "wtf/Assertions.h"
46 #include "wtf/MainThread.h"
47 #include "wtf/PassOwnPtr.h"
48
49 namespace blink {
50
instance()51 MediaStreamCenter& MediaStreamCenter::instance()
52 {
53 ASSERT(isMainThread());
54 DEFINE_STATIC_LOCAL(MediaStreamCenter, center, ());
55 return center;
56 }
57
MediaStreamCenter()58 MediaStreamCenter::MediaStreamCenter()
59 : m_private(adoptPtr(blink::Platform::current()->createMediaStreamCenter(this)))
60 {
61 }
62
~MediaStreamCenter()63 MediaStreamCenter::~MediaStreamCenter()
64 {
65 }
66
getMediaStreamTrackSources(MediaStreamTrackSourcesRequest * request)67 bool MediaStreamCenter::getMediaStreamTrackSources(MediaStreamTrackSourcesRequest* request)
68 {
69 return m_private && m_private->getMediaStreamTrackSources(request);
70 }
71
didSetMediaStreamTrackEnabled(MediaStreamComponent * component)72 void MediaStreamCenter::didSetMediaStreamTrackEnabled(MediaStreamComponent* component)
73 {
74 if (m_private) {
75 if (component->enabled()) {
76 m_private->didEnableMediaStreamTrack(component);
77 } else {
78 m_private->didDisableMediaStreamTrack(component);
79 }
80 }
81 }
82
didAddMediaStreamTrack(MediaStreamDescriptor * stream,MediaStreamComponent * component)83 bool MediaStreamCenter::didAddMediaStreamTrack(MediaStreamDescriptor* stream, MediaStreamComponent* component)
84 {
85 return m_private && m_private->didAddMediaStreamTrack(stream, component);
86 }
87
didRemoveMediaStreamTrack(MediaStreamDescriptor * stream,MediaStreamComponent * component)88 bool MediaStreamCenter::didRemoveMediaStreamTrack(MediaStreamDescriptor* stream, MediaStreamComponent* component)
89 {
90 return m_private && m_private->didRemoveMediaStreamTrack(stream, component);
91 }
92
didStopLocalMediaStream(MediaStreamDescriptor * stream)93 void MediaStreamCenter::didStopLocalMediaStream(MediaStreamDescriptor* stream)
94 {
95 if (m_private)
96 m_private->didStopLocalMediaStream(stream);
97 }
98
didStopMediaStreamTrack(MediaStreamComponent * track)99 bool MediaStreamCenter::didStopMediaStreamTrack(MediaStreamComponent* track)
100 {
101 return m_private && m_private->didStopMediaStreamTrack(track);
102 }
103
didCreateMediaStreamAndTracks(MediaStreamDescriptor * stream)104 void MediaStreamCenter::didCreateMediaStreamAndTracks(MediaStreamDescriptor* stream)
105 {
106 if (!m_private)
107 return;
108
109 for (size_t i = 0; i < stream->numberOfAudioComponents(); ++i)
110 didCreateMediaStreamTrack(stream->audioComponent(i));
111
112 for (size_t i = 0; i < stream->numberOfVideoComponents(); ++i)
113 didCreateMediaStreamTrack(stream->videoComponent(i));
114
115 blink::WebMediaStream webStream(stream);
116 m_private->didCreateMediaStream(webStream);
117 }
118
didCreateMediaStream(MediaStreamDescriptor * stream)119 void MediaStreamCenter::didCreateMediaStream(MediaStreamDescriptor* stream)
120 {
121 if (m_private) {
122 blink::WebMediaStream WebMediaStream(stream);
123 m_private->didCreateMediaStream(WebMediaStream);
124 }
125 }
126
didCreateMediaStreamTrack(MediaStreamComponent * track)127 void MediaStreamCenter::didCreateMediaStreamTrack(MediaStreamComponent* track)
128 {
129 if (m_private)
130 m_private->didCreateMediaStreamTrack(track);
131 }
132
createWebAudioSourceFromMediaStreamTrack(MediaStreamComponent * track)133 PassOwnPtr<AudioSourceProvider> MediaStreamCenter::createWebAudioSourceFromMediaStreamTrack(MediaStreamComponent* track)
134 {
135 ASSERT_UNUSED(track, track);
136 #if ENABLE(WEB_AUDIO)
137 if (m_private)
138 return MediaStreamWebAudioSource::create(adoptPtr(m_private->createWebAudioSourceFromMediaStreamTrack(track)));
139 #endif
140
141 return nullptr;
142 }
143
stopLocalMediaStream(const blink::WebMediaStream & webStream)144 void MediaStreamCenter::stopLocalMediaStream(const blink::WebMediaStream& webStream)
145 {
146 MediaStreamDescriptor* stream = webStream;
147 MediaStreamDescriptorClient* client = stream->client();
148 if (client)
149 client->streamEnded();
150 else
151 stream->setEnded();
152 }
153
154 } // namespace blink
155