FastMixer.h revision c263ca0ad8b6bdf5b0693996bc5f2f5916e0cd49
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#ifndef ANDROID_AUDIO_FAST_MIXER_H
18#define ANDROID_AUDIO_FAST_MIXER_H
19
20#include <linux/futex.h>
21#include <sys/syscall.h>
22#include <utils/Debug.h>
23#include "FastThread.h"
24#include <utils/Thread.h>
25#include "StateQueue.h"
26#include "FastMixerState.h"
27#include "FastMixerDumpState.h"
28
29namespace android {
30
31class AudioMixer;
32
33typedef StateQueue<FastMixerState> FastMixerStateQueue;
34
35class FastMixer : public FastThread {
36
37public:
38            FastMixer();
39    virtual ~FastMixer();
40
41            FastMixerStateQueue* sq();
42
43private:
44            FastMixerStateQueue mSQ;
45
46    // callouts
47    virtual const FastThreadState *poll();
48    virtual void setLog(NBLog::Writer *logWriter);
49    virtual void onIdle();
50    virtual void onExit();
51    virtual bool isSubClassCommand(FastThreadState::Command command);
52    virtual void onStateChange();
53    virtual void onWork();
54
55    // FIXME these former local variables need comments and to be renamed to have "m" prefix
56    static const FastMixerState initial;
57    FastMixerState preIdle; // copy of state before we went into idle
58    long slopNs;        // accumulated time we've woken up too early (> 0) or too late (< 0)
59    int fastTrackNames[FastMixerState::kMaxFastTracks]; // handles used by mixer to identify tracks
60    int generations[FastMixerState::kMaxFastTracks];    // last observed mFastTracks[i].mGeneration
61    NBAIO_Sink *outputSink;
62    int outputSinkGen;
63    AudioMixer* mixer;
64
65    // mSinkBuffer audio format is stored in format.mFormat.
66    void* mSinkBuffer;                  // used for mixer output format translation
67                                        // if sink format is different than mixer output.
68    size_t mSinkBufferSize;
69    void* mMixerBuffer;                 // mixer output buffer.
70    size_t mMixerBufferSize;
71    audio_format_t mMixerBufferFormat;  // mixer output format: AUDIO_FORMAT_PCM_(16_BIT|FLOAT).
72
73    enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState;
74    NBAIO_Format format;
75    unsigned sampleRate;
76    int fastTracksGen;
77    FastMixerDumpState dummyDumpState;
78    uint32_t totalNativeFramesWritten;  // copied to dumpState->mFramesWritten
79
80    // next 2 fields are valid only when timestampStatus == NO_ERROR
81    AudioTimestamp timestamp;
82    uint32_t nativeFramesWrittenButNotPresented;
83
84};  // class FastMixer
85
86}   // namespace android
87
88#endif  // ANDROID_AUDIO_FAST_MIXER_H
89