CAudioPlayer.c revision 83ac345e264c1e22b7a2f1a110b2fe92473394ec
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/** \file CAudioPlayer.c AudioPlayer class */
18
19#include "sles_allinclusive.h"
20
21
22/** \brief Hook called by Object::Realize when an audio player is realized */
23
24SLresult CAudioPlayer_Realize(void *self, SLboolean async)
25{
26    CAudioPlayer *thiz = (CAudioPlayer *) self;
27    SLresult result = SL_RESULT_SUCCESS;
28
29#ifdef ANDROID
30    result = android_audioPlayer_realize(thiz, async);
31#endif
32
33#ifdef USE_SNDFILE
34    result = SndFile_Realize(thiz);
35#endif
36
37    // At this point the channel count and sample rate might still be unknown,
38    // depending on the data source and the platform implementation.
39    // If they are unknown here, then they will be determined during prefetch.
40
41    return result;
42}
43
44
45/** \brief Hook called by Object::Resume when an audio player is resumed */
46
47SLresult CAudioPlayer_Resume(void *self, SLboolean async)
48{
49    return SL_RESULT_SUCCESS;
50}
51
52
53/** \brief Hook called by Object::Destroy when an audio player is destroyed */
54
55void CAudioPlayer_Destroy(void *self)
56{
57    CAudioPlayer *thiz = (CAudioPlayer *) self;
58#ifdef ANDROID
59    android_audioPlayer_destroy(thiz);
60#endif
61    freeDataLocatorFormat(&thiz->mDataSource);
62    freeDataLocatorFormat(&thiz->mDataSink);
63#ifdef USE_SNDFILE
64    SndFile_Destroy(thiz);
65#endif
66}
67
68
69/** \brief Hook called by Object::Destroy before an audio player is about to be destroyed */
70
71predestroy_t CAudioPlayer_PreDestroy(void *self)
72{
73    CAudioPlayer *thiz = (CAudioPlayer *) self;
74#ifdef ANDROID
75    android_audioPlayer_preDestroy(thiz);
76#endif
77
78#ifdef USE_OUTPUTMIXEXT
79    // Safe to proceed immediately if a track has not yet been assigned
80    Track *track = thiz->mTrack;
81    if (NULL == track) {
82        return predestroy_ok;
83    }
84    CAudioPlayer *audioPlayer = track->mAudioPlayer;
85    if (NULL == audioPlayer) {
86        return predestroy_ok;
87    }
88    assert(audioPlayer == thiz);
89    // Request the mixer thread to unlink this audio player's track
90    thiz->mDestroyRequested = true;
91    while (thiz->mDestroyRequested) {
92        object_cond_wait(self);
93    }
94    // Mixer thread has acknowledged the request
95#endif
96    return predestroy_ok;
97}
98
99
100/** \brief Given an audio player, return its data sink, which is guaranteed to be a non-NULL output
101 *  mix.  This function is used by effect send.
102 */
103
104COutputMix *CAudioPlayer_GetOutputMix(CAudioPlayer *audioPlayer)
105{
106    assert(NULL != audioPlayer);
107    assert(SL_DATALOCATOR_OUTPUTMIX == audioPlayer->mDataSink.mLocator.mLocatorType);
108    SLObjectItf outputMix = audioPlayer->mDataSink.mLocator.mOutputMix.outputMix;
109    assert(NULL != outputMix);
110    return (COutputMix *) outputMix;
111}
112