michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* michael@0: * Copyright (c) 2014 The Linux Foundation. All rights reserved. michael@0: * Copyright (C) 2008 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef AUDIOOUTPUT_H_ michael@0: #define AUDIOOUTPUT_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "AudioSink.h" michael@0: michael@0: #define LOG_TAG "AudioOffloadPlayer" michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * Stripped version of Android KK MediaPlayerService::AudioOutput class michael@0: * Android MediaPlayer uses AudioOutput as a wrapper to handle michael@0: * Android::AudioTrack michael@0: * Similarly to ease handling offloaded tracks, part of AudioOutput is used here michael@0: */ michael@0: class AudioOutput : public AudioSink michael@0: { michael@0: typedef android::Mutex Mutex; michael@0: typedef android::String8 String8; michael@0: typedef android::status_t status_t; michael@0: typedef android::AudioTrack AudioTrack; michael@0: michael@0: class CallbackData; michael@0: michael@0: public: michael@0: AudioOutput(int aSessionId, int aUid); michael@0: virtual ~AudioOutput(); michael@0: michael@0: virtual ssize_t FrameSize() const; michael@0: virtual status_t GetPosition(uint32_t* aPosition) const; michael@0: virtual status_t SetVolume(float aVolume) const; michael@0: virtual status_t SetParameters(const String8& aKeyValuePairs); michael@0: michael@0: // Creates an offloaded audio track with the given parameters michael@0: // TODO: Try to recycle audio tracks instead of creating new audio tracks michael@0: // every time michael@0: virtual status_t Open(uint32_t aSampleRate, michael@0: int aChannelCount, michael@0: audio_channel_mask_t aChannelMask, michael@0: audio_format_t aFormat, michael@0: AudioCallback aCb, michael@0: void* aCookie, michael@0: audio_output_flags_t aFlags = AUDIO_OUTPUT_FLAG_NONE, michael@0: const audio_offload_info_t* aOffloadInfo = nullptr); michael@0: michael@0: virtual status_t Start(); michael@0: virtual void Stop(); michael@0: virtual void Flush(); michael@0: virtual void Pause(); michael@0: virtual void Close(); michael@0: michael@0: private: michael@0: static void CallbackWrapper(int aEvent, void* aMe, void* aInfo); michael@0: michael@0: android::sp mTrack; michael@0: void* mCallbackCookie; michael@0: AudioCallback mCallback; michael@0: CallbackData* mCallbackData; michael@0: michael@0: // Uid of the current process, need to create audio track michael@0: int mUid; michael@0: michael@0: // Session id given by Android::AudioSystem and used to create audio track michael@0: int mSessionId; michael@0: michael@0: // CallbackData is what is passed to the AudioTrack as the "user" data. michael@0: // We need to be able to target this to a different Output on the fly, michael@0: // so we can't use the Output itself for this. michael@0: class CallbackData michael@0: { michael@0: public: michael@0: CallbackData(AudioOutput* aCookie) michael@0: { michael@0: mData = aCookie; michael@0: } michael@0: AudioOutput* GetOutput() { return mData;} michael@0: void SetOutput(AudioOutput* aNewcookie) { mData = aNewcookie; } michael@0: // Lock/Unlock are used by the callback before accessing the payload of michael@0: // this object michael@0: void Lock() { mLock.lock(); } michael@0: void Unlock() { mLock.unlock(); } michael@0: private: michael@0: AudioOutput* mData; michael@0: mutable Mutex mLock; michael@0: DISALLOW_EVIL_CONSTRUCTORS(CallbackData); michael@0: }; michael@0: }; // AudioOutput michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* AUDIOOUTPUT_H_ */