| 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies). |
| 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 | * 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 in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 21 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "MediaStreamRegistry.h" |
| 28 | |
| 29 | #if ENABLE(MEDIA_STREAM) |
| 30 | |
| 31 | #include "MediaStream.h" |
| 32 | #include <wtf/MainThread.h> |
| 33 | #include <wtf/NeverDestroyed.h> |
| 34 | #include <wtf/URL.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | MediaStreamRegistry& MediaStreamRegistry::shared() |
| 39 | { |
| 40 | // Since WebWorkers cannot obtain MediaSource objects, we should be on the main thread. |
| 41 | ASSERT(isMainThread()); |
| 42 | static NeverDestroyed<MediaStreamRegistry> instance; |
| 43 | return instance; |
| 44 | } |
| 45 | |
| 46 | void MediaStreamRegistry::registerURL(SecurityOrigin*, const URL& url, URLRegistrable& stream) |
| 47 | { |
| 48 | ASSERT(&stream.registry() == this); |
| 49 | ASSERT(isMainThread()); |
| 50 | m_mediaStreams.set(url.string(), static_cast<MediaStream*>(&stream)); |
| 51 | } |
| 52 | |
| 53 | void MediaStreamRegistry::unregisterURL(const URL& url) |
| 54 | { |
| 55 | ASSERT(isMainThread()); |
| 56 | m_mediaStreams.remove(url.string()); |
| 57 | } |
| 58 | |
| 59 | URLRegistrable* MediaStreamRegistry::lookup(const String& url) const |
| 60 | { |
| 61 | ASSERT(isMainThread()); |
| 62 | return m_mediaStreams.get(url); |
| 63 | } |
| 64 | |
| 65 | MediaStream* MediaStreamRegistry::lookUp(const URL& url) const |
| 66 | { |
| 67 | return static_cast<MediaStream*>(lookup(url.string())); |
| 68 | } |
| 69 | |
| 70 | static Vector<MediaStream*>& mediaStreams() |
| 71 | { |
| 72 | static NeverDestroyed<Vector<MediaStream*>> streams; |
| 73 | return streams; |
| 74 | } |
| 75 | |
| 76 | void MediaStreamRegistry::registerStream(MediaStream& stream) |
| 77 | { |
| 78 | mediaStreams().append(&stream); |
| 79 | } |
| 80 | |
| 81 | void MediaStreamRegistry::unregisterStream(MediaStream& stream) |
| 82 | { |
| 83 | auto& allStreams = mediaStreams(); |
| 84 | size_t pos = allStreams.find(&stream); |
| 85 | if (pos != notFound) |
| 86 | allStreams.remove(pos); |
| 87 | } |
| 88 | |
| 89 | void MediaStreamRegistry::forEach(const WTF::Function<void(MediaStream&)>& callback) const |
| 90 | { |
| 91 | for (auto& stream : mediaStreams()) |
| 92 | callback(*stream); |
| 93 | } |
| 94 | |
| 95 | } // namespace WebCore |
| 96 | |
| 97 | #endif // ENABLE(MEDIA_STREAM) |
| 98 | |