Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/avmedia/source/framework/soundhandler.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include "soundhandler.hxx"
21
22
#include <comphelper/sequenceashashmap.hxx>
23
#include <unotools/mediadescriptor.hxx>
24
25
#include <com/sun/star/io/XInputStream.hpp>
26
#include <com/sun/star/frame/DispatchResultState.hpp>
27
28
#include <avmedia/mediawindow.hxx>
29
#include <cppuhelper/factory.hxx>
30
#include <cppuhelper/supportsservice.hxx>
31
32
namespace avmedia{
33
34
35
//  XServiceInfo
36
OUString SAL_CALL SoundHandler::getImplementationName()
37
0
{
38
0
    return u"com.sun.star.comp.framework.SoundHandler"_ustr;
39
0
}
40
41
// XServiceInfo
42
sal_Bool SAL_CALL SoundHandler::supportsService( const OUString& sServiceName )
43
0
{
44
0
    return cppu::supportsService(this, sServiceName);
45
0
}
46
47
// XServiceInfo
48
css::uno::Sequence< OUString > SAL_CALL SoundHandler::getSupportedServiceNames()
49
0
{
50
0
    return { u"com.sun.star.frame.ContentHandler"_ustr };
51
0
}
52
53
/*-************************************************************************************************************
54
    @short      standard ctor
55
    @descr      These initialize a new instance of this class with needed information for work.
56
57
    @seealso    using at owner
58
59
    @param      "xFactory", reference to service manager for creation of new services
60
    @onerror    Show an assertion and do nothing else.
61
    @threadsafe yes
62
*//*-*************************************************************************************************************/
63
SoundHandler::SoundHandler()
64
        // Init member
65
0
    :   m_bError        ( false    )
66
0
    ,   m_aUpdateIdle   ( "avmedia SoundHandler Update" )
67
0
{
68
0
    m_aUpdateIdle.SetInvokeHandler(LINK(this, SoundHandler, implts_PlayerNotify));
69
0
}
Unexecuted instantiation: avmedia::SoundHandler::SoundHandler()
Unexecuted instantiation: avmedia::SoundHandler::SoundHandler()
70
71
/*-************************************************************************************************************
72
    @short      standard dtor
73
*//*-*************************************************************************************************************/
74
SoundHandler::~SoundHandler()
75
0
{
76
0
    if (m_xListener.is())
77
0
    {
78
0
        css::frame::DispatchResultEvent aEvent;
79
0
        aEvent.State = css::frame::DispatchResultState::FAILURE;
80
0
        m_xListener->dispatchFinished(aEvent);
81
0
        m_xListener.clear();
82
0
    }
83
0
}
84
85
/*-************************************************************************************************************
86
    @interface  css::frame::XDispatch
87
88
    @short      try to load audio file
89
    @descr      This method try to load given audio file by URL and play it. We use vcl/Sound class to do that.
90
                Playing of sound is asynchronous every time.
91
92
    @attention  We must hold us alive by ourself ... because we use async. vcl sound player ... but playing is started
93
                in async interface call "dispatch()" too. And caller forget us immediately. But then our uno ref count
94
                will decreased to 0 and will die. The only solution is to use own reference to our implementation.
95
                But we do it for really started jobs only and release it during call back of vcl.
96
97
    @seealso    class vcl/Sound
98
    @seealso    method implts_PlayerNotify()
99
100
    @param      "aURL"      , URL to dispatch.
101
    @param      "lArguments", list of optional arguments.
102
    @onerror    We do nothing.
103
    @threadsafe yes
104
*//*-*************************************************************************************************************/
105
void SAL_CALL SoundHandler::dispatchWithNotification(const css::util::URL&                                             aURL      ,
106
                                                     const css::uno::Sequence< css::beans::PropertyValue >&            lDescriptor,
107
                                                     const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
108
0
{
109
    // SAFE {
110
0
    const std::unique_lock aLock(m_aMutex);
111
112
0
    comphelper::SequenceAsHashMap aDescriptor(lDescriptor);
113
114
0
    {
115
    //close streams otherwise on windows we can't reopen the file in the
116
    //media player when we pass the url to directx as it'll already be open
117
0
    css::uno::Reference< css::io::XInputStream > xInputStream =
118
0
        aDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_INPUTSTREAM,
119
0
        css::uno::Reference< css::io::XInputStream >());
120
0
    if (xInputStream.is()) xInputStream->closeInput();
121
0
    }
122
123
    // If player currently used for other dispatch() requests ...
124
    // cancel it by calling stop()!
125
0
    m_aUpdateIdle.Stop();
126
0
    if (m_xPlayer.is())
127
0
    {
128
0
        if (m_xPlayer->isPlaying())
129
0
            m_xPlayer->stop();
130
0
        m_xPlayer.clear();
131
0
    }
132
133
    // Try to initialize player.
134
0
    m_xListener = xListener;
135
0
    try
136
0
    {
137
0
        m_bError = false;
138
0
        m_xPlayer.set( avmedia::MediaWindow::createPlayer( aURL.Complete, aDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_REFERRER, OUString()) ), css::uno::UNO_SET_THROW );
139
        // OK- we can start async playing ...
140
        // Count this request and initialize self-holder against dying by uno ref count ...
141
0
        m_xSelfHold.set(getXWeak());
142
0
        m_xPlayer->start();
143
0
        m_aUpdateIdle.SetPriority( TaskPriority::HIGH_IDLE );
144
0
        m_aUpdateIdle.Start();
145
0
    }
146
0
    catch( css::uno::Exception& )
147
0
    {
148
0
        m_bError = true;
149
0
        m_xPlayer.clear();
150
0
    }
151
152
    // } SAFE
153
0
}
154
155
void SAL_CALL SoundHandler::dispatch( const css::util::URL&                                  aURL       ,
156
                                      const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
157
0
{
158
0
    dispatchWithNotification(aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
159
0
}
160
161
/*-************************************************************************************************************
162
    @interface  css::document::XExtendedFilterDetection
163
164
    @short      try to detect file (given as argument included in "lDescriptor")
165
    @descr      We try to detect, if given file could be handled by this class and is a well known one.
166
                If it is - we return right internal type name - otherwise we return nothing!
167
                So call can search for another detect service and ask him too.
168
169
    @attention  a) We don't need any mutex here ... because we don't use any member!
170
                b) Don't use internal player instance "m_pPlayer" to detect given sound file!
171
                   It's not necessary to do that ... and we can use temp. variable to do the same.
172
                   This way is easy - we don't must synchronize it with currently played sounds!
173
                   Another reason to do so ... We are a listener on our internal ma_Player object.
174
                   If you would call "IsSoundFile()" on this instance, he would call us back and
175
                   we make some unnecessary things ...
176
    @param      "lDescriptor", description of file to detect
177
    @return     Internal type name which match this file ... or nothing if it is unknown.
178
179
    @onerror    We return nothing.
180
    @threadsafe yes
181
*//*-*************************************************************************************************************/
182
OUString SAL_CALL SoundHandler::detect( css::uno::Sequence< css::beans::PropertyValue >& lDescriptor )
183
0
{
184
    // Our default is "nothing". So we can return it, if detection failed or file type is really unknown.
185
0
    OUString sTypeName;
186
187
    // Analyze given descriptor to find filename or input stream or ...
188
0
    comphelper::SequenceAsHashMap aDescriptor(lDescriptor);
189
0
    OUString                      sURL       = aDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_URL, OUString());
190
0
    OUString                      sReferer   = aDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_REFERRER, OUString());
191
192
0
    if (
193
0
        !sURL.isEmpty() &&
194
0
        (avmedia::MediaWindow::isMediaURL(sURL, sReferer))
195
0
       )
196
0
    {
197
        // If the file type is supported depends on the OS, so...
198
        // I think we can the following ones:
199
        //  a) look for given extension of url to map our type decision HARD CODED!!!
200
        //  b) return preferred type every time... it's easy :-)
201
0
        sTypeName = u"wav_Wave_Audio_File"_ustr;
202
0
        aDescriptor[utl::MediaDescriptor::PROP_TYPENAME] <<= sTypeName;
203
0
        aDescriptor >> lDescriptor;
204
0
    }
205
206
    // Return our decision.
207
0
    return sTypeName;
208
0
}
209
210
/*-************************************************************************************************************
211
    @short      call back of sound player
212
    @descr      Our player call us back to give us some information.
213
                We use this information to callback our might existing listener.
214
215
    @seealso    method dispatchWithNotification()
216
    @return     0 every time... it doesn't matter for us.
217
    @threadsafe yes
218
*//*-*************************************************************************************************************/
219
IMPL_LINK_NOARG(SoundHandler, implts_PlayerNotify, Timer *, void)
220
0
{
221
    // SAFE {
222
0
    std::unique_lock aLock(m_aMutex);
223
224
0
    if (m_xPlayer.is() && m_xPlayer->isPlaying() && m_xPlayer->getMediaTime() < m_xPlayer->getDuration())
225
0
    {
226
0
        m_aUpdateIdle.Start();
227
0
        return;
228
0
    }
229
0
    m_xPlayer.clear();
230
231
    // We use m_xSelfHold to let us die ... but we must live till real finishing of this method too!!!
232
    // So we SHOULD use another "self-holder" temp. to provide that ...
233
0
    css::uno::Reference< css::uno::XInterface > xOperationHold = m_xSelfHold;
234
0
    m_xSelfHold.clear();
235
236
    // notify might existing listener
237
    // And forget this listener!
238
    // Because the corresponding dispatch was finished.
239
0
    if (m_xListener.is())
240
0
    {
241
0
        css::frame::DispatchResultEvent aEvent;
242
0
        if (!m_bError)
243
0
            aEvent.State = css::frame::DispatchResultState::SUCCESS;
244
0
        else
245
0
            aEvent.State = css::frame::DispatchResultState::FAILURE;
246
0
        m_xListener->dispatchFinished(aEvent);
247
0
        m_xListener.clear();
248
0
    }
249
250
    // } SAFE
251
    //release aLock before end of method at which point xOperationHold goes out of scope and pThis dies
252
0
    aLock.unlock();
253
0
}
254
255
} // namespace framework
256
257
258
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
259
com_sun_star_comp_framework_SoundHandler_get_implementation(css::uno::XComponentContext*,
260
                                                            css::uno::Sequence<css::uno::Any> const &)
261
0
{
262
0
    return cppu::acquire(new avmedia::SoundHandler);
263
0
}
264
265
266
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */