summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/qt7/medianode.mm
blob: 00f8340ded59cb28ffb188247740f59dc08cd327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*  This file is part of the KDE project.

    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).

    This library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2.1 or 3 of the License.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "medianode.h"
#include "audiograph.h"
#include "audionode.h"
#include "backendheader.h"

#include "mediaobject.h"
#include "audiooutput.h"
#include "quicktimevideoplayer.h"

QT_BEGIN_NAMESPACE

namespace Phonon
{
namespace QT7
{

MediaNode::MediaNode(NodeDescription description, QObject *parent)
    : QObject(parent), m_audioGraph(0), m_audioNode(0), m_description(description), m_owningMediaObject(0)
{
}

MediaNode::MediaNode(NodeDescription description, AudioNode *audioPart, QObject *parent)
    : QObject(parent), m_audioGraph(0), m_audioNode(audioPart), m_description(description)
{
}

void MediaNode::setAudioNode(AudioNode *audioPart)
{
    if (m_audioNode)
        delete m_audioNode;
    m_audioNode = audioPart;
}

MediaNode::~MediaNode()
{
   delete m_audioNode;
   qDeleteAll(m_audioSinkList);
}

AudioConnection *MediaNode::getAudioConnectionToSink(MediaNode *sink)
{
    AudioConnection *connection = 0;
    for (int i=0; i<m_audioSinkList.size(); ++i){
        if (m_audioSinkList[i]->isBetween(this, sink)){
            connection = m_audioSinkList[i];
            break;
        }
    }
    return connection;
}

bool MediaNode::connectToSink(MediaNode *sink)
{
    if ((m_description & AudioSource) && (sink->m_description & AudioSink)){
        // Check that they don't belong to different graphs. If they do, but
        // sink is not connected to any source, accept it:
        if (m_owningMediaObject && sink->m_owningMediaObject
            && m_owningMediaObject != sink->m_owningMediaObject
            && !sink->m_audioSourceList.isEmpty()){
            return false;
        }

        // Check that the connection doesn't already exists:
        AudioConnection *connection = getAudioConnectionToSink(sink);
        if (connection)
            return true;

        // Check that there are awailable input/output busses:
        int inputBus = sink->availableAudioInputBus();
        int outputBus = availableAudioOutputBus();
        if (inputBus >= sink->m_audioNode->m_maxInputBusses || outputBus >= m_audioNode->m_maxOutputBusses)
            return false;

        // All OK. Create connection:
        connection = new AudioConnection(this, outputBus, sink, inputBus);
        m_audioSinkList << connection;
        sink->m_audioSourceList << connection;

        if (m_audioNode->m_audioGraph)
            m_audioNode->m_audioGraph->connectLate(connection);

        MediaNodeEvent event1(MediaNodeEvent::AudioSinkAdded, connection);
        notify(&event1, false);
        MediaNodeEvent event2(MediaNodeEvent::AudioSourceAdded, connection);
        sink->notify(&event2, false);
        return true;
    }

    if ((m_description & VideoSource) && (sink->m_description & VideoSink)){
        // Check that the connection doesn't already exists:
        if (m_videoSinkList.contains(sink))
            return true;

        m_videoSinkList << sink;
        MediaNodeEvent event1(MediaNodeEvent::VideoSinkAdded, sink);
        notify(&event1, false);
        MediaNodeEvent event2(MediaNodeEvent::VideoSourceAdded, this);
        sink->notify(&event2, false);
        return true;
    }

    return false;
}

bool MediaNode::disconnectToSink(MediaNode *sink)
{
    if ((m_description & AudioSource) && (sink->m_description & AudioSink)){
        AudioConnection *connection = getAudioConnectionToSink(sink);
        if (!connection)
            return false;

        m_audioSinkList.removeOne(connection);
        sink->m_audioSourceList.removeOne(connection);

        if (m_audioNode->m_audioGraph)
            m_audioNode->m_audioGraph->disconnectLate(connection);

        MediaNodeEvent event1(MediaNodeEvent::AudioSinkRemoved, connection);
        notify(&event1, false);
        MediaNodeEvent event2(MediaNodeEvent::AudioSourceRemoved, connection);
        sink->notify(&event2, false);
        
        delete connection;
        return true;
    }

    if ((m_description & VideoSource) && (sink->m_description & VideoSink)){
        m_videoSinkList.removeOne(sink);

        MediaNodeEvent event1(MediaNodeEvent::VideoSinkRemoved, sink);
        notify(&event1, false);
        MediaNodeEvent event2(MediaNodeEvent::VideoSourceRemoved, this);
        sink->notify(&event2, false);
        return true;
    }

    return false;
}

int MediaNode::availableAudioInputBus()
{
    // Scan through all the connection <u>in</u> to this
    // node, and find an awailable index:
    int index = -1;
    bool available = false;

    while (!available){
        ++index;
        available = true;
        for (int i=0; i<m_audioSourceList.size(); ++i){
            if (m_audioSourceList[i]->m_sinkInputBus == index){
                available = false;
                break;
            }
        }
    }
    return index;
}

int MediaNode::availableAudioOutputBus()
{
    // Scan through all the connection <u>out</u> from this
    // node, and find an awailable index:
    int bus = -1;
    bool available = false;

    while (!available){
        ++bus;
        available = true;
        for (int i=0; i<m_audioSinkList.size(); ++i){
            if (m_audioSinkList[i]->m_sourceOutputBus == bus){
                available = false;
                break;
            }
        }
    }
    return bus;
}

void MediaNode::notify(const MediaNodeEvent *event, bool propagate)
{
    // Let subclass handle the event first:
    mediaNodeEvent(event);

    switch(event->type()){
    case MediaNodeEvent::AudioGraphAboutToBeDeleted:
        if (m_audioNode){
            foreach(AudioConnection *connection, m_audioSinkList)
                connection->invalidate();
        }
        break;
    case MediaNodeEvent::NewAudioGraph:
        m_audioGraph = static_cast<AudioGraph *>(event->data());
        break;
    case MediaNodeEvent::AudioSinkAdded:
    case MediaNodeEvent::VideoSinkAdded:
        if (m_owningMediaObject){
            MediaNodeEvent e1(MediaNodeEvent::SetMediaObject, m_owningMediaObject);
            sendEventToSinks(&e1);
            QRect videoRect = m_owningMediaObject->videoPlayer()->videoRect();
            MediaNodeEvent e2(MediaNodeEvent::VideoFrameSizeChanged, &videoRect);
            sendEventToSinks(&e2);
        }
        break;
    case MediaNodeEvent::SetMediaObject:
        m_owningMediaObject = static_cast<MediaObject *>(event->data());
        break;
    default:
        break;
    }

    // Inform the audio node as well:
    if (m_audioNode)
        m_audioNode->notify(event);

    // And perhaps the sinks:
    if (propagate)
        sendEventToSinks(event);
}

void MediaNode::sendEventToSinks(const MediaNodeEvent *event)
{
    for (int i=0; i<m_audioSinkList.size(); ++i)
        m_audioSinkList[i]->m_sink->notify(event);
    for (int i=0; i<m_videoSinkList.size(); ++i)
        m_videoSinkList[i]->notify(event);
}

void MediaNode::updateVideo(VideoFrame &frame){
    for (int i=0; i<m_videoSinkList.size(); ++i)
        m_videoSinkList[i]->updateVideo(frame);
}

void MediaNode::mediaNodeEvent(const MediaNodeEvent */*event*/)
{
   // Override if needed.
}

}} //namespace Phonon::QT7

QT_END_NAMESPACE

#include "moc_medianode.cpp"