1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/omx/AudioOutput.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,242 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* 1.7 + * Copyright (c) 2014 The Linux Foundation. All rights reserved. 1.8 + * Copyright (C) 2008 The Android Open Source Project 1.9 + * 1.10 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.11 + * you may not use this file except in compliance with the License. 1.12 + * You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, software 1.17 + * distributed under the License is distributed on an "AS IS" BASIS, 1.18 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.19 + * See the License for the specific language governing permissions and 1.20 + * limitations under the License. 1.21 + */ 1.22 + 1.23 +#include <stagefright/foundation/ADebug.h> 1.24 +#include "AudioOutput.h" 1.25 + 1.26 +namespace mozilla { 1.27 + 1.28 +#ifdef PR_LOGGING 1.29 +extern PRLogModuleInfo* gAudioOffloadPlayerLog; 1.30 +#define AUDIO_OFFLOAD_LOG(type, msg) \ 1.31 + PR_LOG(gAudioOffloadPlayerLog, type, msg) 1.32 +#else 1.33 +#define AUDIO_OFFLOAD_LOG(type, msg) 1.34 +#endif 1.35 + 1.36 +using namespace android; 1.37 + 1.38 +AudioOutput::AudioOutput(int aSessionId, int aUid) : 1.39 + mCallback(nullptr), 1.40 + mCallbackCookie(nullptr), 1.41 + mCallbackData(nullptr), 1.42 + mSessionId(aSessionId), 1.43 + mUid(aUid) 1.44 +{ 1.45 +#ifdef PR_LOGGING 1.46 + if (!gAudioOffloadPlayerLog) { 1.47 + gAudioOffloadPlayerLog = PR_NewLogModule("AudioOffloadPlayer"); 1.48 + } 1.49 +#endif 1.50 +} 1.51 + 1.52 +AudioOutput::~AudioOutput() 1.53 +{ 1.54 + Close(); 1.55 +} 1.56 + 1.57 +ssize_t AudioOutput::FrameSize() const 1.58 +{ 1.59 + if (!mTrack.get()) { 1.60 + return NO_INIT; 1.61 + } 1.62 + return mTrack->frameSize(); 1.63 +} 1.64 + 1.65 +status_t AudioOutput::GetPosition(uint32_t *aPosition) const 1.66 +{ 1.67 + if (!mTrack.get()) { 1.68 + return NO_INIT; 1.69 + } 1.70 + return mTrack->getPosition(aPosition); 1.71 +} 1.72 + 1.73 +status_t AudioOutput::SetVolume(float aVolume) const 1.74 +{ 1.75 + if (!mTrack.get()) { 1.76 + return NO_INIT; 1.77 + } 1.78 + return mTrack->setVolume(aVolume); 1.79 +} 1.80 + 1.81 +status_t AudioOutput::SetParameters(const String8& aKeyValuePairs) 1.82 +{ 1.83 + if (!mTrack.get()) { 1.84 + return NO_INIT; 1.85 + } 1.86 + return mTrack->setParameters(aKeyValuePairs); 1.87 +} 1.88 + 1.89 +status_t AudioOutput::Open(uint32_t aSampleRate, 1.90 + int aChannelCount, 1.91 + audio_channel_mask_t aChannelMask, 1.92 + audio_format_t aFormat, 1.93 + AudioCallback aCb, 1.94 + void* aCookie, 1.95 + audio_output_flags_t aFlags, 1.96 + const audio_offload_info_t *aOffloadInfo) 1.97 +{ 1.98 + mCallback = aCb; 1.99 + mCallbackCookie = aCookie; 1.100 + 1.101 + if (((aFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || !aCb || 1.102 + !aOffloadInfo) { 1.103 + return BAD_VALUE; 1.104 + } 1.105 + 1.106 + AUDIO_OFFLOAD_LOG(PR_LOG_DEBUG, ("open(%u, %d, 0x%x, 0x%x, %d 0x%x)", 1.107 + aSampleRate, aChannelCount, aChannelMask, aFormat, mSessionId, aFlags)); 1.108 + 1.109 + if (aChannelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { 1.110 + aChannelMask = audio_channel_out_mask_from_count(aChannelCount); 1.111 + if (0 == aChannelMask) { 1.112 + AUDIO_OFFLOAD_LOG(PR_LOG_ERROR, ("open() error, can\'t derive mask for" 1.113 + " %d audio channels", aChannelCount)); 1.114 + return NO_INIT; 1.115 + } 1.116 + } 1.117 + 1.118 + sp<AudioTrack> t; 1.119 + CallbackData* newcbd = new CallbackData(this); 1.120 + 1.121 + t = new AudioTrack( 1.122 + AUDIO_STREAM_MUSIC, 1.123 + aSampleRate, 1.124 + aFormat, 1.125 + aChannelMask, 1.126 + 0, // Offloaded tracks will get frame count from AudioFlinger 1.127 + aFlags, 1.128 + CallbackWrapper, 1.129 + newcbd, 1.130 + 0, // notification frames 1.131 + mSessionId, 1.132 + AudioTrack::TRANSFER_CALLBACK, 1.133 + aOffloadInfo, 1.134 + mUid); 1.135 + 1.136 + if ((!t.get()) || (t->initCheck() != NO_ERROR)) { 1.137 + AUDIO_OFFLOAD_LOG(PR_LOG_ERROR, ("Unable to create audio track")); 1.138 + delete newcbd; 1.139 + return NO_INIT; 1.140 + } 1.141 + 1.142 + mCallbackData = newcbd; 1.143 + t->setVolume(1.0); 1.144 + 1.145 + mTrack = t; 1.146 + return NO_ERROR; 1.147 +} 1.148 + 1.149 +status_t AudioOutput::Start() 1.150 +{ 1.151 + AUDIO_OFFLOAD_LOG(PR_LOG_DEBUG, ("%s", __PRETTY_FUNCTION__)); 1.152 + if (!mTrack.get()) { 1.153 + return NO_INIT; 1.154 + } 1.155 + mTrack->setVolume(1.0); 1.156 + return mTrack->start(); 1.157 +} 1.158 + 1.159 +void AudioOutput::Stop() 1.160 +{ 1.161 + AUDIO_OFFLOAD_LOG(PR_LOG_DEBUG, ("%s", __PRETTY_FUNCTION__)); 1.162 + if (mTrack.get()) { 1.163 + mTrack->stop(); 1.164 + } 1.165 +} 1.166 + 1.167 +void AudioOutput::Flush() 1.168 +{ 1.169 + if (mTrack.get()) { 1.170 + mTrack->flush(); 1.171 + } 1.172 +} 1.173 + 1.174 +void AudioOutput::Pause() 1.175 +{ 1.176 + if (mTrack.get()) { 1.177 + mTrack->pause(); 1.178 + } 1.179 +} 1.180 + 1.181 +void AudioOutput::Close() 1.182 +{ 1.183 + AUDIO_OFFLOAD_LOG(PR_LOG_DEBUG, ("%s", __PRETTY_FUNCTION__)); 1.184 + mTrack.clear(); 1.185 + 1.186 + delete mCallbackData; 1.187 + mCallbackData = nullptr; 1.188 +} 1.189 + 1.190 +// static 1.191 +void AudioOutput::CallbackWrapper(int aEvent, void* aCookie, void* aInfo) 1.192 +{ 1.193 + CallbackData* data = (CallbackData*) aCookie; 1.194 + data->Lock(); 1.195 + AudioOutput* me = data->GetOutput(); 1.196 + AudioTrack::Buffer* buffer = (AudioTrack::Buffer*) aInfo; 1.197 + if (!me) { 1.198 + // no output set, likely because the track was scheduled to be reused 1.199 + // by another player, but the format turned out to be incompatible. 1.200 + data->Unlock(); 1.201 + if (buffer) { 1.202 + buffer->size = 0; 1.203 + } 1.204 + return; 1.205 + } 1.206 + 1.207 + switch(aEvent) { 1.208 + 1.209 + case AudioTrack::EVENT_MORE_DATA: { 1.210 + 1.211 + size_t actualSize = (*me->mCallback)(me, buffer->raw, buffer->size, 1.212 + me->mCallbackCookie, CB_EVENT_FILL_BUFFER); 1.213 + 1.214 + if (actualSize == 0 && buffer->size > 0) { 1.215 + // We've reached EOS but the audio track is not stopped yet, 1.216 + // keep playing silence. 1.217 + memset(buffer->raw, 0, buffer->size); 1.218 + actualSize = buffer->size; 1.219 + } 1.220 + 1.221 + buffer->size = actualSize; 1.222 + } break; 1.223 + 1.224 + case AudioTrack::EVENT_STREAM_END: 1.225 + AUDIO_OFFLOAD_LOG(PR_LOG_DEBUG, ("Callback wrapper: EVENT_STREAM_END")); 1.226 + (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */, 1.227 + me->mCallbackCookie, CB_EVENT_STREAM_END); 1.228 + break; 1.229 + 1.230 + case AudioTrack::EVENT_NEW_IAUDIOTRACK : 1.231 + AUDIO_OFFLOAD_LOG(PR_LOG_DEBUG, ("Callback wrapper: EVENT_TEAR_DOWN")); 1.232 + (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */, 1.233 + me->mCallbackCookie, CB_EVENT_TEAR_DOWN); 1.234 + break; 1.235 + 1.236 + default: 1.237 + AUDIO_OFFLOAD_LOG(PR_LOG_DEBUG, ("received unknown event type: %d in" 1.238 + " Callback wrapper!", aEvent)); 1.239 + break; 1.240 + } 1.241 + 1.242 + data->Unlock(); 1.243 +} 1.244 + 1.245 +} // namespace mozilla