summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/qt7/audiograph.mm
blob: fa73138eb3b9320d609b00d56929b9838587373e (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*  This file is part of the KDE project.

    Copyright (C) 2012 Digia Plc 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 "audiograph.h"
#include "quicktimeaudioplayer.h"
#include "medianode.h"

QT_BEGIN_NAMESPACE

namespace Phonon
{
namespace QT7
{

AudioGraph::AudioGraph(MediaNode *root) : MediaNode(AudioGraphNode, 0, root), m_root(root)
{
    m_audioGraphRef = 0;
    m_initialized = false;
    m_startedLogically = false;
    m_graphCannotPlay = false;
    m_paused = false;
}

AudioGraph::~AudioGraph()
{
    deleteGraph();
}

void AudioGraph::startAllOverFromScratch()
{
    MediaNodeEvent event(MediaNodeEvent::AudioGraphAboutToBeDeleted, this);
    m_root->notify(&event);
    deleteGraph();
}

void AudioGraph::deleteGraph()
{
    if (m_audioGraphRef){
    	AUGraphStop(m_audioGraphRef);
	    AUGraphUninitialize(m_audioGraphRef);
        AUGraphClose(m_audioGraphRef);
        DisposeAUGraph(m_audioGraphRef);
        m_initialized = false;
        m_graphCannotPlay = false;
        DEBUG_AUDIO_GRAPH("Graph ref in" << int(this) << "is deleted")
    }
}

MediaNode *AudioGraph::root()
{
    return m_root;
}

AUGraph AudioGraph::audioGraphRef()
{
    return m_audioGraphRef;
}

void AudioGraph::setStatusCannotPlay()
{
    DEBUG_AUDIO_GRAPH("Graph" << int(this) << "received 'cannot play' request")
    if (!m_graphCannotPlay){
        stop();
        m_graphCannotPlay = true;
        MediaNodeEvent e(MediaNodeEvent::AudioGraphCannotPlay, this);
        m_root->notify(&e);
    }
}

void AudioGraph::rebuildGraph()
{
    DEBUG_AUDIO_GRAPH("Graph" << int(this) << "is rebuilding")
    startAllOverFromScratch();
    if (!openAndInit()){
        setStatusCannotPlay();
    } else { 
        tryStartGraph();
        m_graphCannotPlay = false;
    }   
}

bool AudioGraph::graphCannotPlay()
{
    return m_graphCannotPlay;
}

void AudioGraph::updateStreamSpecifications()
{
    if (!m_initialized){
        if (m_graphCannotPlay)
            rebuildGraph();
        return;
    }

    AudioConnection rootConnection(m_root);
    bool updateOk = updateStreamSpecificationRecursive(&rootConnection);
    if (!updateOk){
        DEBUG_AUDIO_GRAPH("Graph" << int(this) << "could not update stream specification. Rebuild.")
        rebuildGraph();
    }
}

bool AudioGraph::updateStreamSpecificationRecursive(AudioConnection *connection)
{
    bool updateOk = connection->updateStreamSpecification();
    if (!updateOk)
        return false;

    for (int i=0; i<connection->m_sink->m_audioSinkList.size(); ++i){
        if (!updateStreamSpecificationRecursive(connection->m_sink->m_audioSinkList[i]))
            return false;
    }
    return true;
}

bool AudioGraph::openAndInit()
{
	OSStatus err;
	err = NewAUGraph(&m_audioGraphRef);
    BACKEND_ASSERT3(err == noErr, "Could not create audio graph.", NORMAL_ERROR, false)

    MediaNodeEvent eventNew(MediaNodeEvent::NewAudioGraph, this);
    m_root->notify(&eventNew);

    AudioConnection rootConnection(m_root);
    createAndConnectAuNodesRecursive(&rootConnection);
	err = AUGraphOpen(m_audioGraphRef);
    BACKEND_ASSERT3(err == noErr, "Could not create audio graph.", NORMAL_ERROR, false)

    if (!createAudioUnitsRecursive(&rootConnection))
        return false;

	err = AUGraphInitialize(m_audioGraphRef);
    BACKEND_ASSERT3(err == noErr, "Could not initialize audio graph.", NORMAL_ERROR, false)

    m_initialized = true;
    MediaNodeEvent eventInit(MediaNodeEvent::AudioGraphInitialized, this);
    m_root->notify(&eventInit);
    return true;
}

void AudioGraph::createAndConnectAuNodesRecursive(AudioConnection *connection)
{
    connection->m_sink->m_audioNode->createAndConnectAUNodes();
    for (int i=0; i<connection->m_sink->m_audioSinkList.size(); ++i){
        AudioConnection *c = connection->m_sink->m_audioSinkList[i];
        createAndConnectAuNodesRecursive(c);
        bool ok = c->connect(this);
        BACKEND_ASSERT2(ok, "Could not connect an audio nodes pair in the audio graph.", NORMAL_ERROR)
    }
}

bool AudioGraph::createAudioUnitsRecursive(AudioConnection *connection)
{
    connection->m_sink->m_audioNode->createAudioUnits();
    bool ok = connection->updateStreamSpecification();
    if (!ok)
        return false;
    for (int i=0; i<connection->m_sink->m_audioSinkList.size(); ++i){
        if (!createAudioUnitsRecursive(connection->m_sink->m_audioSinkList[i]))
            return false;
    }
    return true;
}

void AudioGraph::tryStartGraph()
{
    // The graph will only start if the background AUGraph
    // is valid. Therefore we just try. If it fails, user
    // actions like connect etc. migh make the graph valid
    // at a later point.
    if (m_startedLogically && !isRunning()){
        OSStatus err = AUGraphStart(m_audioGraphRef);
        if (err == noErr)
            DEBUG_AUDIO_GRAPH("Graph" << int(this) << "started")
        else
            DEBUG_AUDIO_GRAPH("Graph" << int(this) << "could not start")
    }
}

bool AudioGraph::isRunning()
{
    Boolean running = false;
    AUGraphIsRunning(m_audioGraphRef, &running);
    return running;
}

void AudioGraph::setPaused(bool pause)
{
    // This function should only make
    // a difference if the graph is
    // running before pausing.
    if (pause){
        if (isRunning()){
            stop();
            m_paused = true;
        }
    } else if (m_paused){
        start();
        m_paused = false;
    }
}

void AudioGraph::connectLate(AudioConnection *connection)
{
    MediaNodeEvent event(MediaNodeEvent::NewAudioGraph, this);
    connection->m_sink->notify(&event);

    if (!m_initialized)
        return;

    DEBUG_AUDIO_GRAPH("Graph:" << int(this) << "create and connect audio sink after init:" << int(connection->m_sink->m_audioNode))
    AudioConnection startConnection(connection->m_source);
    createAndConnectAuNodesRecursive(&startConnection);
    
    if (!createAudioUnitsRecursive(&startConnection)){
        DEBUG_AUDIO_GRAPH("Graph" << int(this) << "could not update stream specification. Rebuild.")
        rebuildGraph();
    }  
}

void AudioGraph::disconnectLate(AudioConnection *connection)
{
    if (!m_initialized)
        return;

    DEBUG_AUDIO_GRAPH("Graph:" << int(this) << "disconnect audio sink after init:" << int(connection->m_sink->m_audioNode))

    if (!connection->disconnect(this)){
        DEBUG_AUDIO_GRAPH("Graph" << int(this) << "could not disconnect audio sink. Rebuild.")
        rebuildGraph();
    }
}

void AudioGraph::update()
{
    if (m_startedLogically){
        if (m_initialized){
            // Quick solution:
            AUGraphUpdate(m_audioGraphRef, 0);
            tryStartGraph();            
        } else
            rebuildGraph();
    }
}

int AudioGraph::nodeCount()
{
    if (!m_audioGraphRef)
        return 0;
    UInt32 count;
    AUGraphGetNodeCount(m_audioGraphRef, &count);
    return int(count);
}

void AudioGraph::prepare()
{
    if (!m_initialized)
        rebuildGraph();
}

void AudioGraph::start()
{
    // Start does not mean 'start to play
    // music'. It means 'prepare to receive
    // audio from the player units'.
    DEBUG_AUDIO_GRAPH("Graph" << int(this) << "asked to start (cannot play:" << m_graphCannotPlay << ")")
    m_startedLogically = true;
    
    if (m_graphCannotPlay)
        return;
        
    if (!m_initialized)
        rebuildGraph();

    if (!m_graphCannotPlay)
        tryStartGraph();
}

void AudioGraph::stop()
{
    DEBUG_AUDIO_GRAPH("Graph" << int(this) << "asked to stop")
    if (m_audioGraphRef)
	    AUGraphStop(m_audioGraphRef);
    m_startedLogically = false;
}

void AudioGraph::notify(const MediaNodeEvent *event, bool propagate)
{
    switch (event->type()){
        case MediaNodeEvent::StartConnectionChange:
            if (m_graphCannotPlay)
                startAllOverFromScratch();
            break;
        case MediaNodeEvent::EndConnectionChange:
            update();
            break;
       default:
            break;
    }
    m_root->notify(event, propagate);
}

}} // namespace Phonon::QT7

QT_END_NAMESPACE