1 /*
2 **
3 ** Copyright 2008, The Android Open Source Project
4 **
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
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
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.
16 */
17
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "AudioStream"
20 #include <utils/Log.h>
21
22 #include "android_audio_stream.h"
23
24 #include <sys/prctl.h>
25 #include <sys/resource.h>
26 #include <utils/threads.h>
27 #include <media/AudioTrack.h>
28
29 using namespace android;
30
31 // buffer to convert 8-bit samples to 16-bit (in 16-bit samples)
32 static uint32 kConversionBufferSize = 4096;
33
34 /*
35 / Packet Video Audio MIO component
36 /
37 / This implementation routes audio to a stream interface
38 */
AndroidAudioStream()39 OSCL_EXPORT_REF AndroidAudioStream::AndroidAudioStream() :
40 AndroidAudioMIO("AndroidAudioStream"),
41 iActiveTiming(NULL), mClockUpdated(false)
42 {
43 // create active timing object
44 LOGV("constructor");
45 OsclMemAllocator alloc;
46 OsclAny*ptr=alloc.allocate(sizeof(AndroidAudioMIOActiveTimingSupport));
47 if (ptr) {
48 iActiveTiming = new(ptr)AndroidAudioMIOActiveTimingSupport(0, 0);
49 }
50 }
51
~AndroidAudioStream()52 OSCL_EXPORT_REF AndroidAudioStream::~AndroidAudioStream()
53 {
54 LOGV("destructor");
55 // cleanup active timing object
56 if (iActiveTiming) {
57 iActiveTiming->~AndroidAudioMIOActiveTimingSupport();
58 OsclMemAllocator alloc;
59 alloc.deallocate(iActiveTiming);
60 }
61 }
62
QueryInterface(const PVUuid & aUuid,PVInterface * & aInterfacePtr,const OsclAny * aContext)63 PVMFCommandId AndroidAudioStream::QueryInterface(const PVUuid& aUuid, PVInterface*& aInterfacePtr, const OsclAny* aContext)
64 {
65 // check for active timing extension
66 if (iActiveTiming && (aUuid == PvmiClockExtensionInterfaceUuid)) {
67 PvmiClockExtensionInterface* myInterface = OSCL_STATIC_CAST(PvmiClockExtensionInterface*,iActiveTiming);
68 aInterfacePtr = OSCL_STATIC_CAST(PVInterface*, myInterface);
69 return QueueCmdResponse(PVMFSuccess, aContext);
70 }
71
72 // pass to base class
73 else return AndroidAudioMIO::QueryInterface(aUuid, aInterfacePtr, aContext);
74 }
75
QueryUUID(const PvmfMimeString & aMimeType,Oscl_Vector<PVUuid,OsclMemAllocator> & aUuids,bool aExactUuidsOnly,const OsclAny * aContext)76 PVMFCommandId AndroidAudioStream::QueryUUID(const PvmfMimeString& aMimeType,
77 Oscl_Vector<PVUuid, OsclMemAllocator>& aUuids,
78 bool aExactUuidsOnly, const OsclAny* aContext)
79 {
80 aUuids.push_back(PVMI_CAPABILITY_AND_CONFIG_PVUUID);
81 if (iActiveTiming) {
82 PVUuid uuid;
83 iActiveTiming->queryUuid(uuid);
84 aUuids.push_back(uuid);
85 }
86 return QueueCmdResponse(PVMFSuccess, aContext);
87 }
88
setParametersSync(PvmiMIOSession aSession,PvmiKvp * aParameters,int num_elements,PvmiKvp * & aRet_kvp)89 void AndroidAudioStream::setParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters,
90 int num_elements, PvmiKvp * & aRet_kvp)
91 {
92 AndroidAudioMIO::setParametersSync(aSession, aParameters, num_elements, aRet_kvp);
93
94 // initialize audio sink when we have enough information
95 if (iAudioSamplingRateValid && iAudioNumChannelsValid && iAudioFormat != PVMF_MIME_FORMAT_UNKNOWN) {
96 mAudioSink->open(iAudioSamplingRate, iAudioNumChannels, ((iAudioFormat==PVMF_MIME_PCM8)?AudioSystem::PCM_8_BIT:AudioSystem::PCM_16_BIT));
97
98 // reset flags for next time
99 iAudioSamplingRateValid = false;
100 iAudioNumChannelsValid = false;
101 iAudioFormat = PVMF_MIME_FORMAT_UNKNOWN;
102
103 // signal observer
104 iAudioThreadCreatedAndMIOConfigured = true;
105 if(iObserver){
106 LOGV("event PVMFMIOConfigurationComplete to peer");
107 iObserver->ReportInfoEvent(PVMFMIOConfigurationComplete);
108 }
109 }
110 }
111
writeAudioBuffer(uint8 * aData,uint32 aDataLen,PVMFCommandId cmdId,OsclAny * aContext,PVMFTimestamp aTimestamp)112 void AndroidAudioStream::writeAudioBuffer(uint8* aData, uint32 aDataLen, PVMFCommandId cmdId, OsclAny* aContext, PVMFTimestamp aTimestamp)
113 {
114 mAudioSink->write(aData, aDataLen);
115 sendResponse(cmdId, aContext, aTimestamp);
116 }
117
118