Home | History | Annotate | Download | only in legacy
      1 /*
      2  * Copyright 2016 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 #ifndef LEGACY_AUDIO_STREAM_LEGACY_H
     18 #define LEGACY_AUDIO_STREAM_LEGACY_H
     19 
     20 #include <media/AudioTimestamp.h>
     21 #include <media/AudioSystem.h>
     22 
     23 #include <aaudio/AAudio.h>
     24 
     25 #include "AudioStream.h"
     26 #include "AAudioLegacy.h"
     27 #include "utility/FixedBlockAdapter.h"
     28 
     29 namespace aaudio {
     30 
     31 
     32 typedef void (*aaudio_legacy_callback_t)(int event, void* user, void *info);
     33 
     34 enum {
     35     /**
     36      * Request that the callback function should fill the data buffer of an output stream,
     37      * or process the data of an input stream.
     38      * The address parameter passed to the callback function will point to a data buffer.
     39      * For an input stream, the data is read-only.
     40      * The value1 parameter will be the number of frames.
     41      * The value2 parameter is reserved and will be set to zero.
     42      * The callback should return AAUDIO_CALLBACK_RESULT_CONTINUE or AAUDIO_CALLBACK_RESULT_STOP.
     43      */
     44             AAUDIO_CALLBACK_OPERATION_PROCESS_DATA,
     45 
     46     /**
     47      * Inform the callback function that the stream was disconnected.
     48      * The address parameter passed to the callback function will be NULL.
     49      * The value1 will be an error code or AAUDIO_OK.
     50      * The value2 parameter is reserved and will be set to zero.
     51      * The callback return value will be ignored.
     52      */
     53             AAUDIO_CALLBACK_OPERATION_DISCONNECTED,
     54 };
     55 typedef int32_t aaudio_callback_operation_t;
     56 
     57 
     58 class AudioStreamLegacy : public AudioStream, public FixedBlockProcessor {
     59 public:
     60     AudioStreamLegacy();
     61 
     62     virtual ~AudioStreamLegacy();
     63 
     64     aaudio_legacy_callback_t getLegacyCallback();
     65 
     66     // This is public so it can be called from the C callback function.
     67     // This is called from the AudioTrack/AudioRecord client.
     68     virtual void processCallback(int event, void *info) = 0;
     69 
     70     void processCallbackCommon(aaudio_callback_operation_t opcode, void *info);
     71 
     72     // Implement FixedBlockProcessor
     73     int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override;
     74 
     75     virtual int64_t incrementClientFrameCounter(int32_t frames)  = 0;
     76 
     77 protected:
     78 
     79     class StreamDeviceCallback : public android::AudioSystem::AudioDeviceCallback
     80     {
     81     public:
     82 
     83         StreamDeviceCallback(AudioStreamLegacy *parent) : mParent(parent) {}
     84         virtual ~StreamDeviceCallback() {}
     85 
     86         virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo __unused,
     87                                          audio_port_handle_t deviceId) {
     88             if (mParent != nullptr) {
     89                 mParent->onAudioDeviceUpdate(deviceId);
     90             }
     91         }
     92 
     93         AudioStreamLegacy *mParent;
     94     };
     95 
     96     aaudio_result_t getBestTimestamp(clockid_t clockId,
     97                                      int64_t *framePosition,
     98                                      int64_t *timeNanoseconds,
     99                                      android::ExtendedTimestamp *extendedTimestamp);
    100 
    101     void onAudioDeviceUpdate(audio_port_handle_t deviceId);
    102 
    103     void onStart() { mCallbackEnabled.store(true); }
    104     void onStop() { mCallbackEnabled.store(false); }
    105 
    106     FixedBlockAdapter         *mBlockAdapter = nullptr;
    107     aaudio_wrapping_frames_t   mPositionWhenStarting = 0;
    108     int32_t                    mCallbackBufferSize = 0;
    109     const android::sp<StreamDeviceCallback>   mDeviceCallback;
    110 };
    111 
    112 } /* namespace aaudio */
    113 
    114 #endif //LEGACY_AUDIO_STREAM_LEGACY_H
    115