xref: /aosp_15_r20/frameworks/av/media/libnbaio/LibsndfileSource.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker  *
4*ec779b8eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker  *
8*ec779b8eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker  *
10*ec779b8eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker  * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker  */
16*ec779b8eSAndroid Build Coastguard Worker 
17*ec779b8eSAndroid Build Coastguard Worker #define LOG_TAG "LibsndfileSource"
18*ec779b8eSAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
19*ec779b8eSAndroid Build Coastguard Worker 
20*ec779b8eSAndroid Build Coastguard Worker #include <utils/Errors.h>
21*ec779b8eSAndroid Build Coastguard Worker #include <utils/Log.h>
22*ec779b8eSAndroid Build Coastguard Worker #include <media/nbaio/LibsndfileSource.h>
23*ec779b8eSAndroid Build Coastguard Worker 
24*ec779b8eSAndroid Build Coastguard Worker namespace android {
25*ec779b8eSAndroid Build Coastguard Worker 
LibsndfileSource(SNDFILE * sndfile,const SF_INFO & sfinfo,bool loop)26*ec779b8eSAndroid Build Coastguard Worker LibsndfileSource::LibsndfileSource(SNDFILE *sndfile, const SF_INFO &sfinfo, bool loop) :
27*ec779b8eSAndroid Build Coastguard Worker     NBAIO_Source(Format_from_SR_C(sfinfo.samplerate, sfinfo.channels)),
28*ec779b8eSAndroid Build Coastguard Worker     mSndfile(sndfile),
29*ec779b8eSAndroid Build Coastguard Worker     mEstimatedFramesUntilEOF(sfinfo.frames),
30*ec779b8eSAndroid Build Coastguard Worker     mLooping(loop && sfinfo.seekable),
31*ec779b8eSAndroid Build Coastguard Worker     mReadAnyFramesThisLoopCycle(false)
32*ec779b8eSAndroid Build Coastguard Worker {
33*ec779b8eSAndroid Build Coastguard Worker }
34*ec779b8eSAndroid Build Coastguard Worker 
~LibsndfileSource()35*ec779b8eSAndroid Build Coastguard Worker LibsndfileSource::~LibsndfileSource()
36*ec779b8eSAndroid Build Coastguard Worker {
37*ec779b8eSAndroid Build Coastguard Worker     // do not close mSndfile; we don't own it
38*ec779b8eSAndroid Build Coastguard Worker }
39*ec779b8eSAndroid Build Coastguard Worker 
availableToRead()40*ec779b8eSAndroid Build Coastguard Worker ssize_t LibsndfileSource::availableToRead()
41*ec779b8eSAndroid Build Coastguard Worker {
42*ec779b8eSAndroid Build Coastguard Worker     // after we reach the presumed EOF, report infinity just in case there's actually more
43*ec779b8eSAndroid Build Coastguard Worker     return !mLooping && mEstimatedFramesUntilEOF > 0 ? mEstimatedFramesUntilEOF : SSIZE_MAX;
44*ec779b8eSAndroid Build Coastguard Worker }
45*ec779b8eSAndroid Build Coastguard Worker 
read(void * buffer,size_t count)46*ec779b8eSAndroid Build Coastguard Worker ssize_t LibsndfileSource::read(void *buffer, size_t count)
47*ec779b8eSAndroid Build Coastguard Worker {
48*ec779b8eSAndroid Build Coastguard Worker     if (!mNegotiated) {
49*ec779b8eSAndroid Build Coastguard Worker         return (ssize_t) NEGOTIATE;
50*ec779b8eSAndroid Build Coastguard Worker     }
51*ec779b8eSAndroid Build Coastguard Worker     if (mSndfile == NULL) {
52*ec779b8eSAndroid Build Coastguard Worker         return (ssize_t) NO_INIT;
53*ec779b8eSAndroid Build Coastguard Worker     }
54*ec779b8eSAndroid Build Coastguard Worker     sf_count_t actual = sf_readf_short(mSndfile, (short *) buffer, (sf_count_t) count);
55*ec779b8eSAndroid Build Coastguard Worker     // Detect EOF by zero frames read, not by mFramesUntilEOF as it could be inaccurate
56*ec779b8eSAndroid Build Coastguard Worker     if (actual == 0) {
57*ec779b8eSAndroid Build Coastguard Worker         if (mLooping) {
58*ec779b8eSAndroid Build Coastguard Worker             if (mReadAnyFramesThisLoopCycle) {
59*ec779b8eSAndroid Build Coastguard Worker                 (void) sf_seek(mSndfile, (sf_count_t) 0, SEEK_SET);
60*ec779b8eSAndroid Build Coastguard Worker                 mReadAnyFramesThisLoopCycle = false;
61*ec779b8eSAndroid Build Coastguard Worker             } else {
62*ec779b8eSAndroid Build Coastguard Worker                 // We didn't read any frames during the current loop cycle, so disable
63*ec779b8eSAndroid Build Coastguard Worker                 // further looping to prevent the caller from busy waiting at read().
64*ec779b8eSAndroid Build Coastguard Worker                 // This is especially important when looping an empty file.
65*ec779b8eSAndroid Build Coastguard Worker                 mLooping = false;
66*ec779b8eSAndroid Build Coastguard Worker             }
67*ec779b8eSAndroid Build Coastguard Worker         }
68*ec779b8eSAndroid Build Coastguard Worker     } else {
69*ec779b8eSAndroid Build Coastguard Worker         mFramesRead += actual;
70*ec779b8eSAndroid Build Coastguard Worker         if (actual >= mEstimatedFramesUntilEOF) {
71*ec779b8eSAndroid Build Coastguard Worker             mEstimatedFramesUntilEOF = 0;
72*ec779b8eSAndroid Build Coastguard Worker         } else {
73*ec779b8eSAndroid Build Coastguard Worker             mEstimatedFramesUntilEOF -= actual;
74*ec779b8eSAndroid Build Coastguard Worker         }
75*ec779b8eSAndroid Build Coastguard Worker         mReadAnyFramesThisLoopCycle = true;
76*ec779b8eSAndroid Build Coastguard Worker     }
77*ec779b8eSAndroid Build Coastguard Worker     return actual;
78*ec779b8eSAndroid Build Coastguard Worker }
79*ec779b8eSAndroid Build Coastguard Worker 
80*ec779b8eSAndroid Build Coastguard Worker }   // namespace android
81