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 "core/platform/mediastream/MediaStreamDescriptor.h"
35
36#include "platform/UUID.h"
37
38namespace WebCore {
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 String& id, const MediaStreamComponentVector& audioComponents, const MediaStreamComponentVector& videoComponents)
46{
47    return adoptRef(new MediaStreamDescriptor(id, audioComponents, videoComponents));
48}
49
50void MediaStreamDescriptor::addComponent(PassRefPtr<MediaStreamComponent> component)
51{
52    switch (component->source()->type()) {
53    case MediaStreamSource::TypeAudio:
54        if (m_audioComponents.find(component) == kNotFound)
55            m_audioComponents.append(component);
56        break;
57    case MediaStreamSource::TypeVideo:
58        if (m_videoComponents.find(component) == kNotFound)
59            m_videoComponents.append(component);
60        break;
61    }
62}
63
64void MediaStreamDescriptor::removeComponent(PassRefPtr<MediaStreamComponent> component)
65{
66    size_t pos = kNotFound;
67    switch (component->source()->type()) {
68    case MediaStreamSource::TypeAudio:
69        pos = m_audioComponents.find(component);
70        if (pos != kNotFound)
71            m_audioComponents.remove(pos);
72        break;
73    case MediaStreamSource::TypeVideo:
74        pos = m_videoComponents.find(component);
75        if (pos != kNotFound)
76            m_videoComponents.remove(pos);
77        break;
78    }
79}
80
81void MediaStreamDescriptor::addRemoteTrack(MediaStreamComponent* component)
82{
83    if (m_client)
84        m_client->addRemoteTrack(component);
85    else
86        addComponent(component);
87}
88
89void MediaStreamDescriptor::removeRemoteTrack(MediaStreamComponent* component)
90{
91    if (m_client)
92        m_client->removeRemoteTrack(component);
93    else
94        removeComponent(component);
95}
96
97MediaStreamDescriptor::MediaStreamDescriptor(const String& id, const MediaStreamSourceVector& audioSources, const MediaStreamSourceVector& videoSources)
98    : m_client(0)
99    , m_id(id)
100    , m_ended(false)
101{
102    ASSERT(m_id.length());
103    for (size_t i = 0; i < audioSources.size(); i++)
104        m_audioComponents.append(MediaStreamComponent::create(this, audioSources[i]));
105
106    for (size_t i = 0; i < videoSources.size(); i++)
107        m_videoComponents.append(MediaStreamComponent::create(this, videoSources[i]));
108}
109
110MediaStreamDescriptor::MediaStreamDescriptor(const String& id, const MediaStreamComponentVector& audioComponents, const MediaStreamComponentVector& videoComponents)
111    : m_client(0)
112    , m_id(id)
113    , m_ended(false)
114{
115    ASSERT(m_id.length());
116    for (MediaStreamComponentVector::const_iterator iter = audioComponents.begin(); iter != audioComponents.end(); ++iter) {
117        (*iter)->setStream(this);
118        m_audioComponents.append((*iter));
119    }
120    for (MediaStreamComponentVector::const_iterator iter = videoComponents.begin(); iter != videoComponents.end(); ++iter) {
121        (*iter)->setStream(this);
122        m_videoComponents.append((*iter));
123    }
124}
125
126MediaStreamDescriptor::~MediaStreamDescriptor()
127{
128    for (MediaStreamComponentVector::iterator iter = m_audioComponents.begin(); iter != m_audioComponents.end(); ++iter)
129        (*iter)->setStream(0);
130
131    for (MediaStreamComponentVector::iterator iter = m_videoComponents.begin(); iter != m_videoComponents.end(); ++iter)
132        (*iter)->setStream(0);
133}
134
135} // namespace WebCore
136
137