3 ** Copyright 2008, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "AudioStream"
20 #include <utils/Log.h>
22 #include "android_audio_stream.h"
24 #include <sys/prctl.h>
25 #include <sys/resource.h>
26 #include <utils/threads.h>
27 #include <media/AudioTrack.h>
29 using namespace android;
31 // buffer to convert 8-bit samples to 16-bit (in 16-bit samples)
32 static uint32 kConversionBufferSize = 4096;
35 / Packet Video Audio MIO component
37 / This implementation routes audio to a stream interface
39 OSCL_EXPORT_REF AndroidAudioStream::AndroidAudioStream() :
40 AndroidAudioMIO("AndroidAudioStream"),
41 iActiveTiming(NULL), mClockUpdated(false)
43 // create active timing object
45 OsclMemAllocator alloc;
46 OsclAny*ptr = alloc.allocate(sizeof(AndroidAudioMIOActiveTimingSupport));
49 iActiveTiming = new(ptr)AndroidAudioMIOActiveTimingSupport(0, 0);
53 OSCL_EXPORT_REF AndroidAudioStream::~AndroidAudioStream()
56 // cleanup active timing object
59 iActiveTiming->~AndroidAudioMIOActiveTimingSupport();
60 OsclMemAllocator alloc;
61 alloc.deallocate(iActiveTiming);
65 PVMFCommandId AndroidAudioStream::QueryInterface(const PVUuid& aUuid, PVInterface*& aInterfacePtr, const OsclAny* aContext)
67 // check for active timing extension
68 if (iActiveTiming && (aUuid == PvmiClockExtensionInterfaceUuid))
70 PvmiClockExtensionInterface* myInterface = OSCL_STATIC_CAST(PvmiClockExtensionInterface*, iActiveTiming);
71 aInterfacePtr = OSCL_STATIC_CAST(PVInterface*, myInterface);
72 return QueueCmdResponse(PVMFSuccess, aContext);
76 else return AndroidAudioMIO::QueryInterface(aUuid, aInterfacePtr, aContext);
79 void AndroidAudioStream::setParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters,
80 int num_elements, PvmiKvp * & aRet_kvp)
82 AndroidAudioMIO::setParametersSync(aSession, aParameters, num_elements, aRet_kvp);
84 // initialize audio sink when we have enough information
85 if (iAudioSamplingRateValid && iAudioNumChannelsValid && iAudioFormat != PVMF_MIME_FORMAT_UNKNOWN)
87 mAudioSink->open(iAudioSamplingRate, iAudioNumChannels, ((iAudioFormat == PVMF_MIME_PCM8) ? AudioSystem::PCM_8_BIT : AudioSystem::PCM_16_BIT));
89 // reset flags for next time
90 iAudioSamplingRateValid = false;
91 iAudioNumChannelsValid = false;
92 iAudioFormat = PVMF_MIME_FORMAT_UNKNOWN;
96 void AndroidAudioStream::writeAudioBuffer(uint8* aData, uint32 aDataLen, PVMFCommandId cmdId, OsclAny* aContext, PVMFTimestamp aTimestamp)
98 mAudioSink->write(aData, aDataLen);
99 sendResponse(cmdId, aContext, aTimestamp);