OSDN Git Service

RIO-8910: Fix for MP3 parser returning incorrect value for bitrate key
[android-x86/external-opencore.git] / android / android_audio_stream.cpp
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 */
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     {
49         iActiveTiming = new(ptr)AndroidAudioMIOActiveTimingSupport(0, 0);
50     }
51 }
52
53 OSCL_EXPORT_REF AndroidAudioStream::~AndroidAudioStream()
54 {
55     LOGV("destructor");
56     // cleanup active timing object
57     if (iActiveTiming)
58     {
59         iActiveTiming->~AndroidAudioMIOActiveTimingSupport();
60         OsclMemAllocator alloc;
61         alloc.deallocate(iActiveTiming);
62     }
63 }
64
65 PVMFCommandId AndroidAudioStream::QueryInterface(const PVUuid& aUuid, PVInterface*& aInterfacePtr, const OsclAny* aContext)
66 {
67     // check for active timing extension
68     if (iActiveTiming && (aUuid == PvmiClockExtensionInterfaceUuid))
69     {
70         PvmiClockExtensionInterface* myInterface = OSCL_STATIC_CAST(PvmiClockExtensionInterface*, iActiveTiming);
71         aInterfacePtr = OSCL_STATIC_CAST(PVInterface*, myInterface);
72         return QueueCmdResponse(PVMFSuccess, aContext);
73     }
74
75     // pass to base class
76     else return AndroidAudioMIO::QueryInterface(aUuid, aInterfacePtr, aContext);
77 }
78
79 void AndroidAudioStream::setParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters,
80         int num_elements, PvmiKvp * & aRet_kvp)
81 {
82     AndroidAudioMIO::setParametersSync(aSession, aParameters, num_elements, aRet_kvp);
83
84     // initialize audio sink when we have enough information
85     if (iAudioSamplingRateValid && iAudioNumChannelsValid && iAudioFormat != PVMF_MIME_FORMAT_UNKNOWN)
86     {
87         mAudioSink->open(iAudioSamplingRate, iAudioNumChannels, ((iAudioFormat == PVMF_MIME_PCM8) ? AudioSystem::PCM_8_BIT : AudioSystem::PCM_16_BIT));
88
89         // reset flags for next time
90         iAudioSamplingRateValid = false;
91         iAudioNumChannelsValid  = false;
92         iAudioFormat = PVMF_MIME_FORMAT_UNKNOWN;
93     }
94 }
95
96 void AndroidAudioStream::writeAudioBuffer(uint8* aData, uint32 aDataLen, PVMFCommandId cmdId, OsclAny* aContext, PVMFTimestamp aTimestamp)
97 {
98     mAudioSink->write(aData, aDataLen);
99     sendResponse(cmdId, aContext, aTimestamp);
100 }
101