Home | History | Annotate | Download | only in spdif
      1 /*
      2  * Copyright 2014, 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 "AudioSPDIF"
     18 
     19 #include <string.h>
     20 #include <assert.h>
     21 
     22 #include <utils/Log.h>
     23 #include <audio_utils/spdif/FrameScanner.h>
     24 
     25 namespace android {
     26 
     27 FrameScanner::FrameScanner(int dataType,
     28             const uint8_t *syncBytes,
     29             uint32_t syncLength,
     30             uint32_t headerLength)
     31  : mBytesSkipped(0)
     32  , mSyncBytes(syncBytes)
     33  , mSyncLength(syncLength)
     34  , mHeaderLength(headerLength)
     35  , mCursor(0)
     36  , mFormatDumpCount(0)
     37  , mSampleRate(0)
     38  , mRateMultiplier(1)
     39  , mFrameSizeBytes(0)
     40  , mDataType(dataType)
     41  , mDataTypeInfo(0)
     42 {
     43 }
     44 
     45 FrameScanner::~FrameScanner()
     46 {
     47 }
     48 
     49 // State machine that scans for headers in a byte stream.
     50 // @return true if we have detected a complete and valid header.
     51 bool FrameScanner::scan(uint8_t byte)
     52 {
     53     bool result = false;
     54     ALOGV("FrameScanner: byte = 0x%02X, mCursor = %d", byte, mCursor);
     55     assert(mCursor < sizeof(mHeaderBuffer));
     56     if (mCursor < mSyncLength) {
     57         // match sync word
     58         if (byte == mSyncBytes[mCursor]) {
     59             mHeaderBuffer[mCursor++] = byte;
     60         } else {
     61             mBytesSkipped += 1; // skip unsynchronized data
     62             mCursor = 0;
     63         }
     64     } else if (mCursor < mHeaderLength) {
     65         // gather header for parsing
     66         mHeaderBuffer[mCursor++] = byte;
     67         if (mCursor >= mHeaderLength) {
     68             if (parseHeader()) {
     69                 result = true;
     70             } else {
     71                 ALOGE("FrameScanner: ERROR - parseHeader() failed.");
     72             }
     73             mCursor = 0;
     74         }
     75     }
     76     return result;
     77 }
     78 
     79 }  // namespace android
     80