1/*
2 * Copyright (C) 2011 Ericsson AB. All rights reserved.
3 * Copyright (C) 2013 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 are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this 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/MediaStreamDescriptor.h"
35
36#include "platform/UUID.h"
37
38namespace blink {
39
40PassRefPtr<MediaStreamDescriptor> MediaStreamDescriptor::create(const MediaStreamSourceVector& audioSources, const MediaStreamSourceVector& videoSources)
41{
42    return adoptRef(new MediaStreamDescriptor(createCanonicalUUIDString(), audioSources, videoSources));
43}
44
45PassRefPtr<MediaStreamDescriptor> MediaStreamDescriptor::create(const MediaStreamComponentVector& audioComponents, const MediaStreamComponentVector& videoComponents)
46{
47    return adoptRef(new MediaStreamDescriptor(createCanonicalUUIDString(), audioComponents, videoComponents));
48}
49
50PassRefPtr<MediaStreamDescriptor> MediaStreamDescriptor::create(const String& id, const MediaStreamComponentVector& audioComponents, const MediaStreamComponentVector& videoComponents)
51{
52    return adoptRef(new MediaStreamDescriptor(id, audioComponents, videoComponents));
53}
54
55void MediaStreamDescriptor::addComponent(PassRefPtr<MediaStreamComponent> component)
56{
57    switch (component->source()->type()) {
58    case MediaStreamSource::TypeAudio:
59        if (m_audioComponents.find(component) == kNotFound)
60            m_audioComponents.append(component);
61        break;
62    case MediaStreamSource::TypeVideo:
63        if (m_videoComponents.find(component) == kNotFound)
64            m_videoComponents.append(component);
65        break;
66    }
67}
68
69void MediaStreamDescriptor::removeComponent(PassRefPtr<MediaStreamComponent> component)
70{
71    size_t pos = kNotFound;
72    switch (component->source()->type()) {
73    case MediaStreamSource::TypeAudio:
74        pos = m_audioComponents.find(component);
75        if (pos != kNotFound)
76            m_audioComponents.remove(pos);
77        break;
78    case MediaStreamSource::TypeVideo:
79        pos = m_videoComponents.find(component);
80        if (pos != kNotFound)
81            m_videoComponents.remove(pos);
82        break;
83    }
84}
85
86void MediaStreamDescriptor::addRemoteTrack(MediaStreamComponent* component)
87{
88    if (m_client)
89        m_client->addRemoteTrack(component);
90    else
91        addComponent(component);
92}
93
94void MediaStreamDescriptor::removeRemoteTrack(MediaStreamComponent* component)
95{
96    if (m_client)
97        m_client->removeRemoteTrack(component);
98    else
99        removeComponent(component);
100}
101
102MediaStreamDescriptor::MediaStreamDescriptor(const String& id, const MediaStreamSourceVector& audioSources, const MediaStreamSourceVector& videoSources)
103    : m_client(0)
104    , m_id(id)
105    , m_ended(false)
106{
107    ASSERT(m_id.length());
108    for (size_t i = 0; i < audioSources.size(); i++)
109        m_audioComponents.append(MediaStreamComponent::create(audioSources[i]));
110
111    for (size_t i = 0; i < videoSources.size(); i++)
112        m_videoComponents.append(MediaStreamComponent::create(videoSources[i]));
113}
114
115MediaStreamDescriptor::MediaStreamDescriptor(const String& id, const MediaStreamComponentVector& audioComponents, const MediaStreamComponentVector& videoComponents)
116    : m_client(0)
117    , m_id(id)
118    , m_ended(false)
119{
120    ASSERT(m_id.length());
121    for (MediaStreamComponentVector::const_iterator iter = audioComponents.begin(); iter != audioComponents.end(); ++iter)
122        m_audioComponents.append((*iter));
123    for (MediaStreamComponentVector::const_iterator iter = videoComponents.begin(); iter != videoComponents.end(); ++iter)
124        m_videoComponents.append((*iter));
125}
126
127} // namespace blink
128
129