• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 #define LOG_TAG "MonoPipeReader"
18 //#define LOG_NDEBUG 0
19 
20 #include <cutils/compiler.h>
21 #include <utils/Log.h>
22 #include <media/nbaio/MonoPipeReader.h>
23 
24 namespace android {
25 
MonoPipeReader(MonoPipe * pipe)26 MonoPipeReader::MonoPipeReader(MonoPipe* pipe) :
27         NBAIO_Source(pipe->mFormat),
28         mPipe(pipe)
29 {
30 }
31 
~MonoPipeReader()32 MonoPipeReader::~MonoPipeReader()
33 {
34 }
35 
availableToRead()36 ssize_t MonoPipeReader::availableToRead()
37 {
38     if (CC_UNLIKELY(!mNegotiated)) {
39         return NEGOTIATE;
40     }
41     ssize_t ret = android_atomic_acquire_load(&mPipe->mRear) - mPipe->mFront;
42     ALOG_ASSERT((0 <= ret) && (ret <= mMaxFrames));
43     return ret;
44 }
45 
read(void * buffer,size_t count,int64_t readPTS)46 ssize_t MonoPipeReader::read(void *buffer, size_t count, int64_t readPTS)
47 {
48     // Compute the "next read PTS" and cache it.  Callers of read pass a read
49     // PTS indicating the local time for which they are requesting data along
50     // with a count (which is the number of audio frames they are going to
51     // ultimately pass to the next stage of the pipeline).  Offsetting readPTS
52     // by the duration of count will give us the readPTS which will be passed to
53     // us next time, assuming they system continues to operate in steady state
54     // with no discontinuities.  We stash this value so it can be used by the
55     // MonoPipe writer to imlement getNextWriteTimestamp.
56     int64_t nextReadPTS;
57     nextReadPTS = mPipe->offsetTimestampByAudioFrames(readPTS, count);
58 
59     // count == 0 is unlikely and not worth checking for explicitly; will be handled automatically
60     ssize_t red = availableToRead();
61     if (CC_UNLIKELY(red <= 0)) {
62         // Uh-oh, looks like we are underflowing.  Update the next read PTS and
63         // get out.
64         mPipe->updateFrontAndNRPTS(mPipe->mFront, nextReadPTS);
65         return red;
66     }
67     if (CC_LIKELY((size_t) red > count)) {
68         red = count;
69     }
70     size_t front = mPipe->mFront & (mPipe->mMaxFrames - 1);
71     size_t part1 = mPipe->mMaxFrames - front;
72     if (part1 > (size_t) red) {
73         part1 = red;
74     }
75     if (CC_LIKELY(part1 > 0)) {
76         memcpy(buffer, (char *) mPipe->mBuffer + (front << mBitShift), part1 << mBitShift);
77         if (CC_UNLIKELY(front + part1 == mPipe->mMaxFrames)) {
78             size_t part2 = red - part1;
79             if (CC_LIKELY(part2 > 0)) {
80                 memcpy((char *) buffer + (part1 << mBitShift), mPipe->mBuffer, part2 << mBitShift);
81             }
82         }
83         mPipe->updateFrontAndNRPTS(red + mPipe->mFront, nextReadPTS);
84         mFramesRead += red;
85     }
86     return red;
87 }
88 
onTimestamp(const AudioTimestamp & timestamp)89 void MonoPipeReader::onTimestamp(const AudioTimestamp& timestamp)
90 {
91     mPipe->mTimestampMutator.push(timestamp);
92 }
93 
94 }   // namespace android
95