The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | // Proxy for media player implementations |
| 19 | |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #define LOG_TAG "MediaPlayerService" |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 26 | #include <sys/time.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <dirent.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <string.h> |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 31 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <cutils/atomic.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 33 | #include <cutils/properties.h> // for property_get |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 34 | |
| 35 | #include <utils/misc.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 37 | #include <binder/IPCThreadState.h> |
| 38 | #include <binder/IServiceManager.h> |
| 39 | #include <binder/MemoryHeapBase.h> |
| 40 | #include <binder/MemoryBase.h> |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 41 | #include <gui/SurfaceTextureClient.h> |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 42 | #include <utils/Errors.h> // for status_t |
| 43 | #include <utils/String8.h> |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 44 | #include <utils/SystemClock.h> |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 45 | #include <utils/Vector.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | #include <media/MediaPlayerInterface.h> |
| 48 | #include <media/mediarecorder.h> |
| 49 | #include <media/MediaMetadataRetrieverInterface.h> |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 50 | #include <media/Metadata.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | #include <media/AudioTrack.h> |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 52 | #include <media/MemoryLeakTrackUtil.h> |
Eric Laurent | 9cb839a | 2011-09-27 09:48:56 -0700 | [diff] [blame] | 53 | #include <media/stagefright/MediaErrors.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 55 | #include <system/audio.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 56 | |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 57 | #include <private/android_filesystem_config.h> |
| 58 | |
James Dong | 559bf28 | 2012-03-28 10:29:14 -0700 | [diff] [blame] | 59 | #include "ActivityManager.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | #include "MediaRecorderClient.h" |
| 61 | #include "MediaPlayerService.h" |
| 62 | #include "MetadataRetrieverClient.h" |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 63 | #include "MediaPlayerFactory.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | |
| 65 | #include "MidiFile.h" |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 66 | #include "TestPlayerStub.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 67 | #include "StagefrightPlayer.h" |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 68 | #include "nuplayer/NuPlayerDriver.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 69 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 70 | #include <OMX.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 71 | |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 72 | #include "Crypto.h" |
Andreas Huber | 35213f1 | 2012-08-29 11:41:50 -0700 | [diff] [blame] | 73 | #include "RemoteDisplay.h" |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 74 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 75 | namespace { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 76 | using android::media::Metadata; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 77 | using android::status_t; |
| 78 | using android::OK; |
| 79 | using android::BAD_VALUE; |
| 80 | using android::NOT_ENOUGH_DATA; |
| 81 | using android::Parcel; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 82 | |
| 83 | // Max number of entries in the filter. |
| 84 | const int kMaxFilterSize = 64; // I pulled that out of thin air. |
| 85 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 86 | // FIXME: Move all the metadata related function in the Metadata.cpp |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 87 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 88 | |
| 89 | // Unmarshall a filter from a Parcel. |
| 90 | // Filter format in a parcel: |
| 91 | // |
| 92 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 93 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 94 | // | number of entries (n) | |
| 95 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 96 | // | metadata type 1 | |
| 97 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 98 | // | metadata type 2 | |
| 99 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 100 | // .... |
| 101 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 102 | // | metadata type n | |
| 103 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 104 | // |
| 105 | // @param p Parcel that should start with a filter. |
| 106 | // @param[out] filter On exit contains the list of metadata type to be |
| 107 | // filtered. |
| 108 | // @param[out] status On exit contains the status code to be returned. |
| 109 | // @return true if the parcel starts with a valid filter. |
| 110 | bool unmarshallFilter(const Parcel& p, |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 111 | Metadata::Filter *filter, |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 112 | status_t *status) |
| 113 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 114 | int32_t val; |
| 115 | if (p.readInt32(&val) != OK) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 116 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 117 | ALOGE("Failed to read filter's length"); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 118 | *status = NOT_ENOUGH_DATA; |
| 119 | return false; |
| 120 | } |
| 121 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 122 | if( val > kMaxFilterSize || val < 0) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 123 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 124 | ALOGE("Invalid filter len %d", val); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 125 | *status = BAD_VALUE; |
| 126 | return false; |
| 127 | } |
| 128 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 129 | const size_t num = val; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 130 | |
| 131 | filter->clear(); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 132 | filter->setCapacity(num); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 133 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 134 | size_t size = num * sizeof(Metadata::Type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 135 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 136 | |
| 137 | if (p.dataAvail() < size) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 138 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 139 | ALOGE("Filter too short expected %d but got %d", size, p.dataAvail()); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 140 | *status = NOT_ENOUGH_DATA; |
| 141 | return false; |
| 142 | } |
| 143 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 144 | const Metadata::Type *data = |
| 145 | static_cast<const Metadata::Type*>(p.readInplace(size)); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 146 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 147 | if (NULL == data) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 148 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 149 | ALOGE("Filter had no data"); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 150 | *status = BAD_VALUE; |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | // TODO: The stl impl of vector would be more efficient here |
| 155 | // because it degenerates into a memcpy on pod types. Try to |
| 156 | // replace later or use stl::set. |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 157 | for (size_t i = 0; i < num; ++i) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 158 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 159 | filter->add(*data); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 160 | ++data; |
| 161 | } |
| 162 | *status = OK; |
| 163 | return true; |
| 164 | } |
| 165 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 166 | // @param filter Of metadata type. |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 167 | // @param val To be searched. |
| 168 | // @return true if a match was found. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 169 | bool findMetadata(const Metadata::Filter& filter, const int32_t val) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 170 | { |
| 171 | // Deal with empty and ANY right away |
| 172 | if (filter.isEmpty()) return false; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 173 | if (filter[0] == Metadata::kAny) return true; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 174 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 175 | return filter.indexOf(val) >= 0; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | } // anonymous namespace |
| 179 | |
| 180 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | namespace android { |
| 182 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 183 | static bool checkPermission(const char* permissionString) { |
| 184 | #ifndef HAVE_ANDROID_OS |
| 185 | return true; |
| 186 | #endif |
| 187 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 188 | bool ok = checkCallingPermission(String16(permissionString)); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 189 | if (!ok) ALOGE("Request requires %s", permissionString); |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 190 | return ok; |
| 191 | } |
| 192 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4; |
| 195 | /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false; |
| 196 | |
| 197 | void MediaPlayerService::instantiate() { |
| 198 | defaultServiceManager()->addService( |
| 199 | String16("media.player"), new MediaPlayerService()); |
| 200 | } |
| 201 | |
| 202 | MediaPlayerService::MediaPlayerService() |
| 203 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 204 | ALOGV("MediaPlayerService created"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | mNextConnId = 1; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 206 | |
| 207 | mBatteryAudio.refCount = 0; |
| 208 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 209 | mBatteryAudio.deviceOn[i] = 0; |
| 210 | mBatteryAudio.lastTime[i] = 0; |
| 211 | mBatteryAudio.totalTime[i] = 0; |
| 212 | } |
| 213 | // speaker is on by default |
| 214 | mBatteryAudio.deviceOn[SPEAKER] = 1; |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 215 | |
| 216 | MediaPlayerFactory::registerBuiltinFactories(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | MediaPlayerService::~MediaPlayerService() |
| 220 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 221 | ALOGV("MediaPlayerService destroyed"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid) |
| 225 | { |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 226 | sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid); |
| 227 | wp<MediaRecorderClient> w = recorder; |
| 228 | Mutex::Autolock lock(mLock); |
| 229 | mMediaRecorderClients.add(w); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 230 | ALOGV("Create new media recorder client from pid %d", pid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | return recorder; |
| 232 | } |
| 233 | |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 234 | void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client) |
| 235 | { |
| 236 | Mutex::Autolock lock(mLock); |
| 237 | mMediaRecorderClients.remove(client); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 238 | ALOGV("Delete media recorder client"); |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 239 | } |
| 240 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid) |
| 242 | { |
| 243 | sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 244 | ALOGV("Create new media retriever from pid %d", pid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | return retriever; |
| 246 | } |
| 247 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client, |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 249 | int audioSessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | { |
| 251 | int32_t connId = android_atomic_inc(&mNextConnId); |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 252 | |
| 253 | sp<Client> c = new Client( |
| 254 | this, pid, connId, client, audioSessionId, |
| 255 | IPCThreadState::self()->getCallingUid()); |
| 256 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 257 | ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid, |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 258 | IPCThreadState::self()->getCallingUid()); |
| 259 | |
| 260 | wp<Client> w = c; |
| 261 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | Mutex::Autolock lock(mLock); |
| 263 | mClients.add(w); |
| 264 | } |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 265 | return c; |
| 266 | } |
| 267 | |
Andreas Huber | 318ad9c | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 268 | sp<IOMX> MediaPlayerService::getOMX() { |
| 269 | Mutex::Autolock autoLock(mLock); |
| 270 | |
| 271 | if (mOMX.get() == NULL) { |
| 272 | mOMX = new OMX; |
| 273 | } |
| 274 | |
| 275 | return mOMX; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 278 | sp<ICrypto> MediaPlayerService::makeCrypto() { |
Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 279 | return new Crypto; |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Andreas Huber | 35213f1 | 2012-08-29 11:41:50 -0700 | [diff] [blame] | 282 | status_t MediaPlayerService::enableRemoteDisplay(bool enable) { |
| 283 | Mutex::Autolock autoLock(mLock); |
| 284 | |
| 285 | if (enable && mRemoteDisplay == NULL) { |
| 286 | mRemoteDisplay = new RemoteDisplay; |
| 287 | |
| 288 | status_t err = mRemoteDisplay->start(); |
| 289 | |
| 290 | if (err != OK) { |
| 291 | mRemoteDisplay.clear(); |
| 292 | return err; |
| 293 | } |
| 294 | |
| 295 | return OK; |
| 296 | } else if (!enable && mRemoteDisplay != NULL) { |
| 297 | mRemoteDisplay->stop(); |
| 298 | mRemoteDisplay.clear(); |
| 299 | } |
| 300 | |
| 301 | return OK; |
| 302 | } |
| 303 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const |
| 305 | { |
| 306 | const size_t SIZE = 256; |
| 307 | char buffer[SIZE]; |
| 308 | String8 result; |
| 309 | |
| 310 | result.append(" AudioCache\n"); |
| 311 | if (mHeap != 0) { |
| 312 | snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n", |
| 313 | mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice()); |
| 314 | result.append(buffer); |
| 315 | } |
| 316 | snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n", |
| 317 | mMsecsPerFrame, mChannelCount, mFormat, mFrameCount); |
| 318 | result.append(buffer); |
| 319 | snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n", |
| 320 | mSampleRate, mSize, mError, mCommandComplete?"true":"false"); |
| 321 | result.append(buffer); |
| 322 | ::write(fd, result.string(), result.size()); |
| 323 | return NO_ERROR; |
| 324 | } |
| 325 | |
| 326 | status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const |
| 327 | { |
| 328 | const size_t SIZE = 256; |
| 329 | char buffer[SIZE]; |
| 330 | String8 result; |
| 331 | |
| 332 | result.append(" AudioOutput\n"); |
| 333 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", |
| 334 | mStreamType, mLeftVolume, mRightVolume); |
| 335 | result.append(buffer); |
| 336 | snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n", |
Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 337 | mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | result.append(buffer); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 339 | snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n", |
| 340 | mAuxEffectId, mSendLevel); |
| 341 | result.append(buffer); |
| 342 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | ::write(fd, result.string(), result.size()); |
| 344 | if (mTrack != 0) { |
| 345 | mTrack->dump(fd, args); |
| 346 | } |
| 347 | return NO_ERROR; |
| 348 | } |
| 349 | |
| 350 | status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const |
| 351 | { |
| 352 | const size_t SIZE = 256; |
| 353 | char buffer[SIZE]; |
| 354 | String8 result; |
| 355 | result.append(" Client\n"); |
| 356 | snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n", |
| 357 | mPid, mConnId, mStatus, mLoop?"true": "false"); |
| 358 | result.append(buffer); |
| 359 | write(fd, result.string(), result.size()); |
Andreas Huber | a0b1d4b | 2011-06-07 15:52:25 -0700 | [diff] [blame] | 360 | if (mPlayer != NULL) { |
| 361 | mPlayer->dump(fd, args); |
| 362 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | if (mAudioOutput != 0) { |
| 364 | mAudioOutput->dump(fd, args); |
| 365 | } |
| 366 | write(fd, "\n", 1); |
| 367 | return NO_ERROR; |
| 368 | } |
| 369 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | status_t MediaPlayerService::dump(int fd, const Vector<String16>& args) |
| 371 | { |
| 372 | const size_t SIZE = 256; |
| 373 | char buffer[SIZE]; |
| 374 | String8 result; |
| 375 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 376 | snprintf(buffer, SIZE, "Permission Denial: " |
| 377 | "can't dump MediaPlayerService from pid=%d, uid=%d\n", |
| 378 | IPCThreadState::self()->getCallingPid(), |
| 379 | IPCThreadState::self()->getCallingUid()); |
| 380 | result.append(buffer); |
| 381 | } else { |
| 382 | Mutex::Autolock lock(mLock); |
| 383 | for (int i = 0, n = mClients.size(); i < n; ++i) { |
| 384 | sp<Client> c = mClients[i].promote(); |
| 385 | if (c != 0) c->dump(fd, args); |
| 386 | } |
James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 387 | if (mMediaRecorderClients.size() == 0) { |
| 388 | result.append(" No media recorder client\n\n"); |
| 389 | } else { |
| 390 | for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) { |
| 391 | sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote(); |
James Dong | e579e28 | 2011-10-18 22:29:20 -0700 | [diff] [blame] | 392 | if (c != 0) { |
| 393 | snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid); |
| 394 | result.append(buffer); |
| 395 | write(fd, result.string(), result.size()); |
| 396 | result = "\n"; |
| 397 | c->dump(fd, args); |
| 398 | } |
James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 399 | } |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 400 | } |
| 401 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | result.append(" Files opened and/or mapped:\n"); |
Glenn Kasten | 0512ab5 | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 403 | snprintf(buffer, SIZE, "/proc/%d/maps", gettid()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | FILE *f = fopen(buffer, "r"); |
| 405 | if (f) { |
| 406 | while (!feof(f)) { |
| 407 | fgets(buffer, SIZE, f); |
Marco Nelissen | 73ac1ee | 2012-05-07 15:36:32 -0700 | [diff] [blame] | 408 | if (strstr(buffer, " /storage/") || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | strstr(buffer, " /system/sounds/") || |
Dave Sparks | 02fa834 | 2010-09-27 16:55:18 -0700 | [diff] [blame] | 410 | strstr(buffer, " /data/") || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | strstr(buffer, " /system/media/")) { |
| 412 | result.append(" "); |
| 413 | result.append(buffer); |
| 414 | } |
| 415 | } |
| 416 | fclose(f); |
| 417 | } else { |
| 418 | result.append("couldn't open "); |
| 419 | result.append(buffer); |
| 420 | result.append("\n"); |
| 421 | } |
| 422 | |
Glenn Kasten | 0512ab5 | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 423 | snprintf(buffer, SIZE, "/proc/%d/fd", gettid()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | DIR *d = opendir(buffer); |
| 425 | if (d) { |
| 426 | struct dirent *ent; |
| 427 | while((ent = readdir(d)) != NULL) { |
| 428 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
Glenn Kasten | 0512ab5 | 2011-05-04 17:58:57 -0700 | [diff] [blame] | 429 | snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | struct stat s; |
| 431 | if (lstat(buffer, &s) == 0) { |
| 432 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 433 | char linkto[256]; |
| 434 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 435 | if(len > 0) { |
| 436 | if(len > 255) { |
| 437 | linkto[252] = '.'; |
| 438 | linkto[253] = '.'; |
| 439 | linkto[254] = '.'; |
| 440 | linkto[255] = 0; |
| 441 | } else { |
| 442 | linkto[len] = 0; |
| 443 | } |
Marco Nelissen | 73ac1ee | 2012-05-07 15:36:32 -0700 | [diff] [blame] | 444 | if (strstr(linkto, "/storage/") == linkto || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | strstr(linkto, "/system/sounds/") == linkto || |
Dave Sparks | 02fa834 | 2010-09-27 16:55:18 -0700 | [diff] [blame] | 446 | strstr(linkto, "/data/") == linkto || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | strstr(linkto, "/system/media/") == linkto) { |
| 448 | result.append(" "); |
| 449 | result.append(buffer); |
| 450 | result.append(" -> "); |
| 451 | result.append(linkto); |
| 452 | result.append("\n"); |
| 453 | } |
| 454 | } |
| 455 | } else { |
| 456 | result.append(" unexpected type for "); |
| 457 | result.append(buffer); |
| 458 | result.append("\n"); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | closedir(d); |
| 464 | } else { |
| 465 | result.append("couldn't open "); |
| 466 | result.append(buffer); |
| 467 | result.append("\n"); |
| 468 | } |
| 469 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | bool dumpMem = false; |
| 471 | for (size_t i = 0; i < args.size(); i++) { |
| 472 | if (args[i] == String16("-m")) { |
| 473 | dumpMem = true; |
| 474 | } |
| 475 | } |
| 476 | if (dumpMem) { |
James Dong | 8635b7b | 2011-03-14 17:01:38 -0700 | [diff] [blame] | 477 | dumpMemoryAddresses(fd); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | } |
| 480 | write(fd, result.string(), result.size()); |
| 481 | return NO_ERROR; |
| 482 | } |
| 483 | |
| 484 | void MediaPlayerService::removeClient(wp<Client> client) |
| 485 | { |
| 486 | Mutex::Autolock lock(mLock); |
| 487 | mClients.remove(client); |
| 488 | } |
| 489 | |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 490 | MediaPlayerService::Client::Client( |
| 491 | const sp<MediaPlayerService>& service, pid_t pid, |
| 492 | int32_t connId, const sp<IMediaPlayerClient>& client, |
| 493 | int audioSessionId, uid_t uid) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 495 | ALOGV("Client(%d) constructor", connId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | mPid = pid; |
| 497 | mConnId = connId; |
| 498 | mService = service; |
| 499 | mClient = client; |
| 500 | mLoop = false; |
| 501 | mStatus = NO_INIT; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 502 | mAudioSessionId = audioSessionId; |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 503 | mUID = uid; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 504 | mRetransmitEndpointValid = false; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 505 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 507 | ALOGD("create Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | mAntagonizer = new Antagonizer(notify, this); |
| 509 | #endif |
| 510 | } |
| 511 | |
| 512 | MediaPlayerService::Client::~Client() |
| 513 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 514 | ALOGV("Client(%d) destructor pid = %d", mConnId, mPid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | mAudioOutput.clear(); |
| 516 | wp<Client> client(this); |
| 517 | disconnect(); |
| 518 | mService->removeClient(client); |
| 519 | } |
| 520 | |
| 521 | void MediaPlayerService::Client::disconnect() |
| 522 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 523 | ALOGV("disconnect(%d) from pid %d", mConnId, mPid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | // grab local reference and clear main reference to prevent future |
| 525 | // access to object |
| 526 | sp<MediaPlayerBase> p; |
| 527 | { |
| 528 | Mutex::Autolock l(mLock); |
| 529 | p = mPlayer; |
| 530 | } |
Dave Sparks | 795fa58 | 2009-03-24 17:57:12 -0700 | [diff] [blame] | 531 | mClient.clear(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 532 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | mPlayer.clear(); |
| 534 | |
| 535 | // clear the notification to prevent callbacks to dead client |
| 536 | // and reset the player. We assume the player will serialize |
| 537 | // access to itself if necessary. |
| 538 | if (p != 0) { |
| 539 | p->setNotifyCallback(0, 0); |
| 540 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 541 | ALOGD("kill Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | mAntagonizer->kill(); |
| 543 | #endif |
| 544 | p->reset(); |
| 545 | } |
| 546 | |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 547 | disconnectNativeWindow(); |
| 548 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | IPCThreadState::self()->flushCommands(); |
| 550 | } |
| 551 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType) |
| 553 | { |
| 554 | // determine if we have the right player type |
| 555 | sp<MediaPlayerBase> p = mPlayer; |
| 556 | if ((p != NULL) && (p->playerType() != playerType)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 557 | ALOGV("delete player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | p.clear(); |
| 559 | } |
| 560 | if (p == NULL) { |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 561 | p = MediaPlayerFactory::createPlayer(playerType, this, notify); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 563 | |
Jason Simmons | db29e52 | 2011-08-12 13:46:55 -0700 | [diff] [blame] | 564 | if (p != NULL) { |
| 565 | p->setUID(mUID); |
| 566 | } |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 567 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | return p; |
| 569 | } |
| 570 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 571 | sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre( |
| 572 | player_type playerType) |
| 573 | { |
| 574 | ALOGV("player type = %d", playerType); |
| 575 | |
| 576 | // create the right type of player |
| 577 | sp<MediaPlayerBase> p = createPlayer(playerType); |
| 578 | if (p == NULL) { |
| 579 | return p; |
| 580 | } |
| 581 | |
| 582 | if (!p->hardwareOutput()) { |
| 583 | mAudioOutput = new AudioOutput(mAudioSessionId); |
| 584 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 585 | } |
| 586 | |
| 587 | return p; |
| 588 | } |
| 589 | |
| 590 | void MediaPlayerService::Client::setDataSource_post( |
| 591 | const sp<MediaPlayerBase>& p, |
| 592 | status_t status) |
| 593 | { |
| 594 | ALOGV(" setDataSource"); |
| 595 | mStatus = status; |
| 596 | if (mStatus != OK) { |
| 597 | ALOGE(" error: %d", mStatus); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | // Set the re-transmission endpoint if one was chosen. |
| 602 | if (mRetransmitEndpointValid) { |
| 603 | mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint); |
| 604 | if (mStatus != NO_ERROR) { |
| 605 | ALOGE("setRetransmitEndpoint error: %d", mStatus); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (mStatus == OK) { |
| 610 | mPlayer = p; |
| 611 | } |
| 612 | } |
| 613 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 614 | status_t MediaPlayerService::Client::setDataSource( |
| 615 | const char *url, const KeyedVector<String8, String8> *headers) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 617 | ALOGV("setDataSource(%s)", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | if (url == NULL) |
| 619 | return UNKNOWN_ERROR; |
| 620 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 621 | if ((strncmp(url, "http://", 7) == 0) || |
| 622 | (strncmp(url, "https://", 8) == 0) || |
| 623 | (strncmp(url, "rtsp://", 7) == 0)) { |
| 624 | if (!checkPermission("android.permission.INTERNET")) { |
| 625 | return PERMISSION_DENIED; |
| 626 | } |
| 627 | } |
| 628 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | if (strncmp(url, "content://", 10) == 0) { |
| 630 | // get a filedescriptor for the content Uri and |
| 631 | // pass it to the setDataSource(fd) method |
| 632 | |
| 633 | String16 url16(url); |
| 634 | int fd = android::openContentProviderFile(url16); |
| 635 | if (fd < 0) |
| 636 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 637 | ALOGE("Couldn't open fd for %s", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | return UNKNOWN_ERROR; |
| 639 | } |
| 640 | setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus |
| 641 | close(fd); |
| 642 | return mStatus; |
| 643 | } else { |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 644 | player_type playerType = MediaPlayerFactory::getPlayerType(this, url); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 645 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 646 | if (p == NULL) { |
| 647 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | } |
| 649 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 650 | setDataSource_post(p, p->setDataSource(url, headers)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | return mStatus; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length) |
| 656 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 657 | ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | struct stat sb; |
| 659 | int ret = fstat(fd, &sb); |
| 660 | if (ret != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 661 | ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | return UNKNOWN_ERROR; |
| 663 | } |
| 664 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 665 | ALOGV("st_dev = %llu", sb.st_dev); |
| 666 | ALOGV("st_mode = %u", sb.st_mode); |
| 667 | ALOGV("st_uid = %lu", sb.st_uid); |
| 668 | ALOGV("st_gid = %lu", sb.st_gid); |
| 669 | ALOGV("st_size = %llu", sb.st_size); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | |
| 671 | if (offset >= sb.st_size) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 672 | ALOGE("offset error"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | ::close(fd); |
| 674 | return UNKNOWN_ERROR; |
| 675 | } |
| 676 | if (offset + length > sb.st_size) { |
| 677 | length = sb.st_size - offset; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 678 | ALOGV("calculated length = %lld", length); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | } |
| 680 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 681 | player_type playerType = MediaPlayerFactory::getPlayerType(this, |
| 682 | fd, |
| 683 | offset, |
| 684 | length); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 685 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 686 | if (p == NULL) { |
| 687 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // now set data source |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 691 | setDataSource_post(p, p->setDataSource(fd, offset, length)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | return mStatus; |
| 693 | } |
| 694 | |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 695 | status_t MediaPlayerService::Client::setDataSource( |
| 696 | const sp<IStreamSource> &source) { |
| 697 | // create the right type of player |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 698 | player_type playerType = MediaPlayerFactory::getPlayerType(this, source); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 699 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 700 | if (p == NULL) { |
| 701 | return NO_INIT; |
| 702 | } |
| 703 | |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 704 | // now set data source |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 705 | setDataSource_post(p, p->setDataSource(source)); |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 706 | return mStatus; |
| 707 | } |
| 708 | |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 709 | void MediaPlayerService::Client::disconnectNativeWindow() { |
| 710 | if (mConnectedWindow != NULL) { |
| 711 | status_t err = native_window_api_disconnect(mConnectedWindow.get(), |
| 712 | NATIVE_WINDOW_API_MEDIA); |
| 713 | |
| 714 | if (err != OK) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 715 | ALOGW("native_window_api_disconnect returned an error: %s (%d)", |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 716 | strerror(-err), err); |
| 717 | } |
| 718 | } |
| 719 | mConnectedWindow.clear(); |
| 720 | } |
| 721 | |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 722 | status_t MediaPlayerService::Client::setVideoSurfaceTexture( |
| 723 | const sp<ISurfaceTexture>& surfaceTexture) |
| 724 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 725 | ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get()); |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 726 | sp<MediaPlayerBase> p = getPlayer(); |
| 727 | if (p == 0) return UNKNOWN_ERROR; |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 728 | |
| 729 | sp<IBinder> binder(surfaceTexture == NULL ? NULL : |
| 730 | surfaceTexture->asBinder()); |
| 731 | if (mConnectedWindowBinder == binder) { |
| 732 | return OK; |
| 733 | } |
| 734 | |
| 735 | sp<ANativeWindow> anw; |
| 736 | if (surfaceTexture != NULL) { |
| 737 | anw = new SurfaceTextureClient(surfaceTexture); |
| 738 | status_t err = native_window_api_connect(anw.get(), |
| 739 | NATIVE_WINDOW_API_MEDIA); |
| 740 | |
| 741 | if (err != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 742 | ALOGE("setVideoSurfaceTexture failed: %d", err); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 743 | // Note that we must do the reset before disconnecting from the ANW. |
| 744 | // Otherwise queue/dequeue calls could be made on the disconnected |
| 745 | // ANW, which may result in errors. |
| 746 | reset(); |
| 747 | |
| 748 | disconnectNativeWindow(); |
| 749 | |
| 750 | return err; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // Note that we must set the player's new SurfaceTexture before |
| 755 | // disconnecting the old one. Otherwise queue/dequeue calls could be made |
| 756 | // on the disconnected ANW, which may result in errors. |
| 757 | status_t err = p->setVideoSurfaceTexture(surfaceTexture); |
| 758 | |
| 759 | disconnectNativeWindow(); |
| 760 | |
| 761 | mConnectedWindow = anw; |
| 762 | |
| 763 | if (err == OK) { |
| 764 | mConnectedWindowBinder = binder; |
| 765 | } else { |
| 766 | disconnectNativeWindow(); |
| 767 | } |
| 768 | |
| 769 | return err; |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 772 | status_t MediaPlayerService::Client::invoke(const Parcel& request, |
| 773 | Parcel *reply) |
| 774 | { |
| 775 | sp<MediaPlayerBase> p = getPlayer(); |
| 776 | if (p == NULL) return UNKNOWN_ERROR; |
| 777 | return p->invoke(request, reply); |
| 778 | } |
| 779 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 780 | // This call doesn't need to access the native player. |
| 781 | status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter) |
| 782 | { |
| 783 | status_t status; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 784 | media::Metadata::Filter allow, drop; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 785 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 786 | if (unmarshallFilter(filter, &allow, &status) && |
| 787 | unmarshallFilter(filter, &drop, &status)) { |
| 788 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 789 | |
| 790 | mMetadataAllow = allow; |
| 791 | mMetadataDrop = drop; |
| 792 | } |
| 793 | return status; |
| 794 | } |
| 795 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 796 | status_t MediaPlayerService::Client::getMetadata( |
| 797 | bool update_only, bool apply_filter, Parcel *reply) |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 798 | { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 799 | sp<MediaPlayerBase> player = getPlayer(); |
| 800 | if (player == 0) return UNKNOWN_ERROR; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 801 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 802 | status_t status; |
| 803 | // Placeholder for the return code, updated by the caller. |
| 804 | reply->writeInt32(-1); |
| 805 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 806 | media::Metadata::Filter ids; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 807 | |
| 808 | // We don't block notifications while we fetch the data. We clear |
| 809 | // mMetadataUpdated first so we don't lose notifications happening |
| 810 | // during the rest of this call. |
| 811 | { |
| 812 | Mutex::Autolock lock(mLock); |
| 813 | if (update_only) { |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 814 | ids = mMetadataUpdated; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 815 | } |
| 816 | mMetadataUpdated.clear(); |
| 817 | } |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 818 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 819 | media::Metadata metadata(reply); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 820 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 821 | metadata.appendHeader(); |
| 822 | status = player->getMetadata(ids, reply); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 823 | |
| 824 | if (status != OK) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 825 | metadata.resetParcel(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 826 | ALOGE("getMetadata failed %d", status); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 827 | return status; |
| 828 | } |
| 829 | |
| 830 | // FIXME: Implement filtering on the result. Not critical since |
| 831 | // filtering takes place on the update notifications already. This |
| 832 | // would be when all the metadata are fetch and a filter is set. |
| 833 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 834 | // Everything is fine, update the metadata length. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 835 | metadata.updateLength(); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 836 | return OK; |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 837 | } |
| 838 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | status_t MediaPlayerService::Client::prepareAsync() |
| 840 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 841 | ALOGV("[%d] prepareAsync", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | sp<MediaPlayerBase> p = getPlayer(); |
| 843 | if (p == 0) return UNKNOWN_ERROR; |
| 844 | status_t ret = p->prepareAsync(); |
| 845 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 846 | ALOGD("start Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | if (ret == NO_ERROR) mAntagonizer->start(); |
| 848 | #endif |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | status_t MediaPlayerService::Client::start() |
| 853 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 854 | ALOGV("[%d] start", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | sp<MediaPlayerBase> p = getPlayer(); |
| 856 | if (p == 0) return UNKNOWN_ERROR; |
| 857 | p->setLooping(mLoop); |
| 858 | return p->start(); |
| 859 | } |
| 860 | |
| 861 | status_t MediaPlayerService::Client::stop() |
| 862 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 863 | ALOGV("[%d] stop", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | sp<MediaPlayerBase> p = getPlayer(); |
| 865 | if (p == 0) return UNKNOWN_ERROR; |
| 866 | return p->stop(); |
| 867 | } |
| 868 | |
| 869 | status_t MediaPlayerService::Client::pause() |
| 870 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 871 | ALOGV("[%d] pause", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | sp<MediaPlayerBase> p = getPlayer(); |
| 873 | if (p == 0) return UNKNOWN_ERROR; |
| 874 | return p->pause(); |
| 875 | } |
| 876 | |
| 877 | status_t MediaPlayerService::Client::isPlaying(bool* state) |
| 878 | { |
| 879 | *state = false; |
| 880 | sp<MediaPlayerBase> p = getPlayer(); |
| 881 | if (p == 0) return UNKNOWN_ERROR; |
| 882 | *state = p->isPlaying(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 883 | ALOGV("[%d] isPlaying: %d", mConnId, *state); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | return NO_ERROR; |
| 885 | } |
| 886 | |
| 887 | status_t MediaPlayerService::Client::getCurrentPosition(int *msec) |
| 888 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 889 | ALOGV("getCurrentPosition"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | sp<MediaPlayerBase> p = getPlayer(); |
| 891 | if (p == 0) return UNKNOWN_ERROR; |
| 892 | status_t ret = p->getCurrentPosition(msec); |
| 893 | if (ret == NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 894 | ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 896 | ALOGE("getCurrentPosition returned %d", ret); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | } |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | status_t MediaPlayerService::Client::getDuration(int *msec) |
| 902 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 903 | ALOGV("getDuration"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | sp<MediaPlayerBase> p = getPlayer(); |
| 905 | if (p == 0) return UNKNOWN_ERROR; |
| 906 | status_t ret = p->getDuration(msec); |
| 907 | if (ret == NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 908 | ALOGV("[%d] getDuration = %d", mConnId, *msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 910 | ALOGE("getDuration returned %d", ret); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | } |
| 912 | return ret; |
| 913 | } |
| 914 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 915 | status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) { |
| 916 | ALOGV("setNextPlayer"); |
| 917 | Mutex::Autolock l(mLock); |
| 918 | sp<Client> c = static_cast<Client*>(player.get()); |
| 919 | mNextClient = c; |
| 920 | if (mAudioOutput != NULL && c != NULL) { |
| 921 | mAudioOutput->setNextOutput(c->mAudioOutput); |
| 922 | } else { |
| 923 | ALOGE("no current audio output"); |
| 924 | } |
| 925 | return OK; |
| 926 | } |
| 927 | |
| 928 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | status_t MediaPlayerService::Client::seekTo(int msec) |
| 930 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 931 | ALOGV("[%d] seekTo(%d)", mConnId, msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | sp<MediaPlayerBase> p = getPlayer(); |
| 933 | if (p == 0) return UNKNOWN_ERROR; |
| 934 | return p->seekTo(msec); |
| 935 | } |
| 936 | |
| 937 | status_t MediaPlayerService::Client::reset() |
| 938 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 939 | ALOGV("[%d] reset", mConnId); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 940 | mRetransmitEndpointValid = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | sp<MediaPlayerBase> p = getPlayer(); |
| 942 | if (p == 0) return UNKNOWN_ERROR; |
| 943 | return p->reset(); |
| 944 | } |
| 945 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 946 | status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 948 | ALOGV("[%d] setAudioStreamType(%d)", mConnId, type); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | // TODO: for hardware output, call player instead |
| 950 | Mutex::Autolock l(mLock); |
| 951 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); |
| 952 | return NO_ERROR; |
| 953 | } |
| 954 | |
| 955 | status_t MediaPlayerService::Client::setLooping(int loop) |
| 956 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 957 | ALOGV("[%d] setLooping(%d)", mConnId, loop); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | mLoop = loop; |
| 959 | sp<MediaPlayerBase> p = getPlayer(); |
| 960 | if (p != 0) return p->setLooping(loop); |
| 961 | return NO_ERROR; |
| 962 | } |
| 963 | |
| 964 | status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume) |
| 965 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 966 | ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); |
John Grossman | 761defc | 2012-02-09 15:09:05 -0800 | [diff] [blame] | 967 | |
| 968 | // for hardware output, call player instead |
| 969 | sp<MediaPlayerBase> p = getPlayer(); |
| 970 | { |
| 971 | Mutex::Autolock l(mLock); |
| 972 | if (p != 0 && p->hardwareOutput()) { |
| 973 | MediaPlayerHWInterface* hwp = |
| 974 | reinterpret_cast<MediaPlayerHWInterface*>(p.get()); |
| 975 | return hwp->setVolume(leftVolume, rightVolume); |
| 976 | } else { |
| 977 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); |
| 978 | return NO_ERROR; |
| 979 | } |
| 980 | } |
| 981 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | return NO_ERROR; |
| 983 | } |
| 984 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 985 | status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level) |
| 986 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 987 | ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 988 | Mutex::Autolock l(mLock); |
| 989 | if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level); |
| 990 | return NO_ERROR; |
| 991 | } |
| 992 | |
| 993 | status_t MediaPlayerService::Client::attachAuxEffect(int effectId) |
| 994 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 995 | ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 996 | Mutex::Autolock l(mLock); |
| 997 | if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId); |
| 998 | return NO_ERROR; |
| 999 | } |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1000 | |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1001 | status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1002 | ALOGV("[%d] setParameter(%d)", mConnId, key); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1003 | sp<MediaPlayerBase> p = getPlayer(); |
| 1004 | if (p == 0) return UNKNOWN_ERROR; |
| 1005 | return p->setParameter(key, request); |
| 1006 | } |
| 1007 | |
| 1008 | status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1009 | ALOGV("[%d] getParameter(%d)", mConnId, key); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1010 | sp<MediaPlayerBase> p = getPlayer(); |
| 1011 | if (p == 0) return UNKNOWN_ERROR; |
| 1012 | return p->getParameter(key, reply); |
| 1013 | } |
| 1014 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1015 | status_t MediaPlayerService::Client::setRetransmitEndpoint( |
| 1016 | const struct sockaddr_in* endpoint) { |
| 1017 | |
| 1018 | if (NULL != endpoint) { |
| 1019 | uint32_t a = ntohl(endpoint->sin_addr.s_addr); |
| 1020 | uint16_t p = ntohs(endpoint->sin_port); |
| 1021 | ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId, |
| 1022 | (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p); |
| 1023 | } else { |
| 1024 | ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId); |
| 1025 | } |
| 1026 | |
| 1027 | sp<MediaPlayerBase> p = getPlayer(); |
| 1028 | |
| 1029 | // Right now, the only valid time to set a retransmit endpoint is before |
| 1030 | // player selection has been made (since the presence or absence of a |
| 1031 | // retransmit endpoint is going to determine which player is selected during |
| 1032 | // setDataSource). |
| 1033 | if (p != 0) return INVALID_OPERATION; |
| 1034 | |
| 1035 | if (NULL != endpoint) { |
| 1036 | mRetransmitEndpoint = *endpoint; |
| 1037 | mRetransmitEndpointValid = true; |
| 1038 | } else { |
| 1039 | mRetransmitEndpointValid = false; |
| 1040 | } |
| 1041 | |
| 1042 | return NO_ERROR; |
| 1043 | } |
| 1044 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1045 | status_t MediaPlayerService::Client::getRetransmitEndpoint( |
| 1046 | struct sockaddr_in* endpoint) |
| 1047 | { |
| 1048 | if (NULL == endpoint) |
| 1049 | return BAD_VALUE; |
| 1050 | |
| 1051 | sp<MediaPlayerBase> p = getPlayer(); |
| 1052 | |
| 1053 | if (p != NULL) |
| 1054 | return p->getRetransmitEndpoint(endpoint); |
| 1055 | |
| 1056 | if (!mRetransmitEndpointValid) |
| 1057 | return NO_INIT; |
| 1058 | |
| 1059 | *endpoint = mRetransmitEndpoint; |
| 1060 | |
| 1061 | return NO_ERROR; |
| 1062 | } |
| 1063 | |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 1064 | void MediaPlayerService::Client::notify( |
| 1065 | void* cookie, int msg, int ext1, int ext2, const Parcel *obj) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | { |
| 1067 | Client* client = static_cast<Client*>(cookie); |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1068 | if (client == NULL) { |
| 1069 | return; |
| 1070 | } |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1071 | |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1072 | sp<IMediaPlayerClient> c; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1073 | { |
| 1074 | Mutex::Autolock l(client->mLock); |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1075 | c = client->mClient; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1076 | if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) { |
John Grossman | cb0b755 | 2012-08-23 17:47:31 -0700 | [diff] [blame] | 1077 | if (client->mAudioOutput != NULL) |
| 1078 | client->mAudioOutput->switchToNextOutput(); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1079 | client->mNextClient->start(); |
| 1080 | client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj); |
| 1081 | } |
| 1082 | } |
| 1083 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1084 | if (MEDIA_INFO == msg && |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1085 | MEDIA_INFO_METADATA_UPDATE == ext1) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1086 | const media::Metadata::Type metadata_type = ext2; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1087 | |
| 1088 | if(client->shouldDropMetadata(metadata_type)) { |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | // Update the list of metadata that have changed. getMetadata |
| 1093 | // also access mMetadataUpdated and clears it. |
| 1094 | client->addNewMetadataUpdate(metadata_type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1095 | } |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (c != NULL) { |
| 1098 | ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2); |
| 1099 | c->notify(msg, ext1, ext2, obj); |
| 1100 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1103 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1104 | bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1105 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1106 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1107 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1108 | if (findMetadata(mMetadataDrop, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1109 | return true; |
| 1110 | } |
| 1111 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1112 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1113 | return false; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1114 | } else { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1115 | return true; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1119 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1120 | void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1121 | Mutex::Autolock lock(mLock); |
| 1122 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1123 | mMetadataUpdated.add(metadata_type); |
| 1124 | } |
| 1125 | } |
| 1126 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | #if CALLBACK_ANTAGONIZER |
| 1128 | const int Antagonizer::interval = 10000; // 10 msecs |
| 1129 | |
| 1130 | Antagonizer::Antagonizer(notify_callback_f cb, void* client) : |
| 1131 | mExit(false), mActive(false), mClient(client), mCb(cb) |
| 1132 | { |
| 1133 | createThread(callbackThread, this); |
| 1134 | } |
| 1135 | |
| 1136 | void Antagonizer::kill() |
| 1137 | { |
| 1138 | Mutex::Autolock _l(mLock); |
| 1139 | mActive = false; |
| 1140 | mExit = true; |
| 1141 | mCondition.wait(mLock); |
| 1142 | } |
| 1143 | |
| 1144 | int Antagonizer::callbackThread(void* user) |
| 1145 | { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1146 | ALOGD("Antagonizer started"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); |
| 1148 | while (!p->mExit) { |
| 1149 | if (p->mActive) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1150 | ALOGV("send event"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | p->mCb(p->mClient, 0, 0, 0); |
| 1152 | } |
| 1153 | usleep(interval); |
| 1154 | } |
| 1155 | Mutex::Autolock _l(p->mLock); |
| 1156 | p->mCondition.signal(); |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1157 | ALOGD("Antagonizer stopped"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
| 1160 | #endif |
| 1161 | |
| 1162 | static size_t kDefaultHeapSize = 1024 * 1024; // 1MB |
| 1163 | |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 1164 | sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1166 | ALOGV("decode(%s)", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | sp<MemoryBase> mem; |
| 1168 | sp<MediaPlayerBase> player; |
| 1169 | |
| 1170 | // Protect our precious, precious DRMd ringtones by only allowing |
| 1171 | // decoding of http, but not filesystem paths or content Uris. |
| 1172 | // If the application wants to decode those, it should open a |
| 1173 | // filedescriptor for them and use that. |
| 1174 | if (url != NULL && strncmp(url, "http://", 7) != 0) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1175 | ALOGD("Can't decode %s by path, use filedescriptor instead", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | return mem; |
| 1177 | } |
| 1178 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1179 | player_type playerType = |
| 1180 | MediaPlayerFactory::getPlayerType(NULL /* client */, url); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1181 | ALOGV("player type = %d", playerType); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | |
| 1183 | // create the right type of player |
| 1184 | sp<AudioCache> cache = new AudioCache(url); |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1185 | player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | if (player == NULL) goto Exit; |
| 1187 | if (player->hardwareOutput()) goto Exit; |
| 1188 | |
| 1189 | static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache); |
| 1190 | |
| 1191 | // set data source |
| 1192 | if (player->setDataSource(url) != NO_ERROR) goto Exit; |
| 1193 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1194 | ALOGV("prepare"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | player->prepareAsync(); |
| 1196 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1197 | ALOGV("wait for prepare"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1199 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1200 | ALOGV("start"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | player->start(); |
| 1202 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1203 | ALOGV("wait for playback complete"); |
Eric Laurent | 9cb839a | 2011-09-27 09:48:56 -0700 | [diff] [blame] | 1204 | cache->wait(); |
| 1205 | // in case of error, return what was successfully decoded. |
| 1206 | if (cache->size() == 0) { |
| 1207 | goto Exit; |
| 1208 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | |
| 1210 | mem = new MemoryBase(cache->getHeap(), 0, cache->size()); |
| 1211 | *pSampleRate = cache->sampleRate(); |
| 1212 | *pNumChannels = cache->channelCount(); |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 1213 | *pFormat = cache->format(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1214 | ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | |
| 1216 | Exit: |
| 1217 | if (player != 0) player->reset(); |
| 1218 | return mem; |
| 1219 | } |
| 1220 | |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 1221 | sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1223 | ALOGV("decode(%d, %lld, %lld)", fd, offset, length); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | sp<MemoryBase> mem; |
| 1225 | sp<MediaPlayerBase> player; |
| 1226 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1227 | player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */, |
| 1228 | fd, |
| 1229 | offset, |
| 1230 | length); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1231 | ALOGV("player type = %d", playerType); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | |
| 1233 | // create the right type of player |
| 1234 | sp<AudioCache> cache = new AudioCache("decode_fd"); |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1235 | player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | if (player == NULL) goto Exit; |
| 1237 | if (player->hardwareOutput()) goto Exit; |
| 1238 | |
| 1239 | static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache); |
| 1240 | |
| 1241 | // set data source |
| 1242 | if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit; |
| 1243 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1244 | ALOGV("prepare"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | player->prepareAsync(); |
| 1246 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1247 | ALOGV("wait for prepare"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1249 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1250 | ALOGV("start"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | player->start(); |
| 1252 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1253 | ALOGV("wait for playback complete"); |
Eric Laurent | 9cb839a | 2011-09-27 09:48:56 -0700 | [diff] [blame] | 1254 | cache->wait(); |
| 1255 | // in case of error, return what was successfully decoded. |
| 1256 | if (cache->size() == 0) { |
| 1257 | goto Exit; |
| 1258 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | |
| 1260 | mem = new MemoryBase(cache->getHeap(), 0, cache->size()); |
| 1261 | *pSampleRate = cache->sampleRate(); |
| 1262 | *pNumChannels = cache->channelCount(); |
| 1263 | *pFormat = cache->format(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1264 | ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | |
| 1266 | Exit: |
| 1267 | if (player != 0) player->reset(); |
| 1268 | ::close(fd); |
| 1269 | return mem; |
| 1270 | } |
| 1271 | |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1272 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | #undef LOG_TAG |
| 1274 | #define LOG_TAG "AudioSink" |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1275 | MediaPlayerService::AudioOutput::AudioOutput(int sessionId) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1276 | : mCallback(NULL), |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1277 | mCallbackCookie(NULL), |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1278 | mCallbackData(NULL), |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1279 | mBytesWritten(0), |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1280 | mSessionId(sessionId), |
| 1281 | mFlags(AUDIO_OUTPUT_FLAG_NONE) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1282 | ALOGV("AudioOutput(%d)", sessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | mTrack = 0; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1284 | mRecycledTrack = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1285 | mStreamType = AUDIO_STREAM_MUSIC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | mLeftVolume = 1.0; |
| 1287 | mRightVolume = 1.0; |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1288 | mPlaybackRatePermille = 1000; |
| 1289 | mSampleRateHz = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | mMsecsPerFrame = 0; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1291 | mAuxEffectId = 0; |
| 1292 | mSendLevel = 0.0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | setMinBufferCount(); |
| 1294 | } |
| 1295 | |
| 1296 | MediaPlayerService::AudioOutput::~AudioOutput() |
| 1297 | { |
| 1298 | close(); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1299 | delete mRecycledTrack; |
| 1300 | delete mCallbackData; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void MediaPlayerService::AudioOutput::setMinBufferCount() |
| 1304 | { |
| 1305 | char value[PROPERTY_VALUE_MAX]; |
| 1306 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 1307 | mIsOnEmulator = true; |
| 1308 | mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | bool MediaPlayerService::AudioOutput::isOnEmulator() |
| 1313 | { |
| 1314 | setMinBufferCount(); |
| 1315 | return mIsOnEmulator; |
| 1316 | } |
| 1317 | |
| 1318 | int MediaPlayerService::AudioOutput::getMinBufferCount() |
| 1319 | { |
| 1320 | setMinBufferCount(); |
| 1321 | return mMinBufferCount; |
| 1322 | } |
| 1323 | |
| 1324 | ssize_t MediaPlayerService::AudioOutput::bufferSize() const |
| 1325 | { |
| 1326 | if (mTrack == 0) return NO_INIT; |
| 1327 | return mTrack->frameCount() * frameSize(); |
| 1328 | } |
| 1329 | |
| 1330 | ssize_t MediaPlayerService::AudioOutput::frameCount() const |
| 1331 | { |
| 1332 | if (mTrack == 0) return NO_INIT; |
| 1333 | return mTrack->frameCount(); |
| 1334 | } |
| 1335 | |
| 1336 | ssize_t MediaPlayerService::AudioOutput::channelCount() const |
| 1337 | { |
| 1338 | if (mTrack == 0) return NO_INIT; |
| 1339 | return mTrack->channelCount(); |
| 1340 | } |
| 1341 | |
| 1342 | ssize_t MediaPlayerService::AudioOutput::frameSize() const |
| 1343 | { |
| 1344 | if (mTrack == 0) return NO_INIT; |
| 1345 | return mTrack->frameSize(); |
| 1346 | } |
| 1347 | |
| 1348 | uint32_t MediaPlayerService::AudioOutput::latency () const |
| 1349 | { |
Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 1350 | if (mTrack == 0) return 0; |
| 1351 | return mTrack->latency(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | float MediaPlayerService::AudioOutput::msecsPerFrame() const |
| 1355 | { |
| 1356 | return mMsecsPerFrame; |
| 1357 | } |
| 1358 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1359 | status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1360 | { |
| 1361 | if (mTrack == 0) return NO_INIT; |
| 1362 | return mTrack->getPosition(position); |
| 1363 | } |
| 1364 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1365 | status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const |
| 1366 | { |
| 1367 | if (mTrack == 0) return NO_INIT; |
| 1368 | *frameswritten = mBytesWritten / frameSize(); |
| 1369 | return OK; |
| 1370 | } |
| 1371 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1372 | status_t MediaPlayerService::AudioOutput::open( |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1373 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, |
| 1374 | audio_format_t format, int bufferCount, |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1375 | AudioCallback cb, void *cookie, |
| 1376 | audio_output_flags_t flags) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1378 | mCallback = cb; |
| 1379 | mCallbackCookie = cookie; |
| 1380 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | // Check argument "bufferCount" against the mininum buffer count |
| 1382 | if (bufferCount < mMinBufferCount) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1383 | ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | bufferCount = mMinBufferCount; |
| 1385 | |
| 1386 | } |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1387 | ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask, |
| 1388 | format, bufferCount, mSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | int afSampleRate; |
| 1390 | int afFrameCount; |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1391 | uint32_t frameCount; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | |
| 1393 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| 1394 | return NO_INIT; |
| 1395 | } |
| 1396 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| 1397 | return NO_INIT; |
| 1398 | } |
| 1399 | |
| 1400 | frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1401 | |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1402 | if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { |
Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1403 | channelMask = audio_channel_out_mask_from_count(channelCount); |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1404 | if (0 == channelMask) { |
| 1405 | ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount); |
| 1406 | return NO_INIT; |
| 1407 | } |
| 1408 | } |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1409 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1410 | AudioTrack *t; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1411 | CallbackData *newcbd = NULL; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1412 | if (mCallback != NULL) { |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1413 | newcbd = new CallbackData(this); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1414 | t = new AudioTrack( |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1415 | mStreamType, |
| 1416 | sampleRate, |
| 1417 | format, |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1418 | channelMask, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1419 | frameCount, |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1420 | flags, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1421 | CallbackWrapper, |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1422 | newcbd, |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 1423 | 0, // notification frames |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1424 | mSessionId); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1425 | } else { |
| 1426 | t = new AudioTrack( |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1427 | mStreamType, |
| 1428 | sampleRate, |
| 1429 | format, |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1430 | channelMask, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1431 | frameCount, |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1432 | flags, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1433 | NULL, |
| 1434 | NULL, |
| 1435 | 0, |
| 1436 | mSessionId); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1440 | ALOGE("Unable to create audio track"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | delete t; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1442 | delete newcbd; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | return NO_INIT; |
| 1444 | } |
| 1445 | |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1446 | |
| 1447 | if (mRecycledTrack) { |
| 1448 | // check if the existing track can be reused as-is, or if a new track needs to be created. |
| 1449 | |
| 1450 | bool reuse = true; |
| 1451 | if ((mCallbackData == NULL && mCallback != NULL) || |
| 1452 | (mCallbackData != NULL && mCallback == NULL)) { |
| 1453 | // recycled track uses callbacks but the caller wants to use writes, or vice versa |
| 1454 | ALOGV("can't chain callback and write"); |
| 1455 | reuse = false; |
| 1456 | } else if ((mRecycledTrack->getSampleRate() != sampleRate) || |
| 1457 | (mRecycledTrack->channelCount() != channelCount) || |
| 1458 | (mRecycledTrack->frameCount() != t->frameCount())) { |
| 1459 | ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames", |
| 1460 | mRecycledTrack->getSampleRate(), sampleRate, |
| 1461 | mRecycledTrack->channelCount(), channelCount, |
| 1462 | mRecycledTrack->frameCount(), t->frameCount()); |
| 1463 | reuse = false; |
| 1464 | } else if (flags != mFlags) { |
| 1465 | ALOGV("output flags differ %08x/%08x", flags, mFlags); |
| 1466 | reuse = false; |
| 1467 | } |
| 1468 | if (reuse) { |
| 1469 | ALOGV("chaining to next output"); |
| 1470 | close(); |
| 1471 | mTrack = mRecycledTrack; |
| 1472 | mRecycledTrack = NULL; |
| 1473 | if (mCallbackData != NULL) { |
| 1474 | mCallbackData->setOutput(this); |
| 1475 | } |
| 1476 | delete t; |
| 1477 | delete newcbd; |
| 1478 | return OK; |
| 1479 | } |
| 1480 | |
| 1481 | // if we're not going to reuse the track, unblock and flush it |
| 1482 | if (mCallbackData != NULL) { |
| 1483 | mCallbackData->setOutput(NULL); |
| 1484 | mCallbackData->endTrackSwitch(); |
| 1485 | } |
| 1486 | mRecycledTrack->flush(); |
| 1487 | delete mRecycledTrack; |
| 1488 | mRecycledTrack = NULL; |
| 1489 | delete mCallbackData; |
| 1490 | mCallbackData = NULL; |
| 1491 | close(); |
| 1492 | } |
| 1493 | |
| 1494 | mCallbackData = newcbd; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1495 | ALOGV("setVolume"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | t->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1497 | |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1498 | mSampleRateHz = sampleRate; |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1499 | mFlags = flags; |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1500 | mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate; |
Marco Nelissen | 9944860 | 2012-04-02 12:16:49 -0700 | [diff] [blame] | 1501 | uint32_t pos; |
| 1502 | if (t->getPosition(&pos) == OK) { |
| 1503 | mBytesWritten = uint64_t(pos) * t->frameSize(); |
| 1504 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | mTrack = t; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1506 | |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1507 | status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000); |
| 1508 | if (res != NO_ERROR) { |
| 1509 | return res; |
| 1510 | } |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1511 | t->setAuxEffectSendLevel(mSendLevel); |
| 1512 | return t->attachAuxEffect(mAuxEffectId);; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | void MediaPlayerService::AudioOutput::start() |
| 1516 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1517 | ALOGV("start"); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1518 | if (mCallbackData != NULL) { |
| 1519 | mCallbackData->endTrackSwitch(); |
| 1520 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | if (mTrack) { |
| 1522 | mTrack->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1523 | mTrack->setAuxEffectSendLevel(mSendLevel); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | mTrack->start(); |
| 1525 | } |
| 1526 | } |
| 1527 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1528 | void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) { |
| 1529 | mNextOutput = nextOutput; |
| 1530 | } |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1531 | |
| 1532 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1533 | void MediaPlayerService::AudioOutput::switchToNextOutput() { |
| 1534 | ALOGV("switchToNextOutput"); |
| 1535 | if (mNextOutput != NULL) { |
| 1536 | if (mCallbackData != NULL) { |
| 1537 | mCallbackData->beginTrackSwitch(); |
| 1538 | } |
| 1539 | delete mNextOutput->mCallbackData; |
| 1540 | mNextOutput->mCallbackData = mCallbackData; |
| 1541 | mCallbackData = NULL; |
| 1542 | mNextOutput->mRecycledTrack = mTrack; |
| 1543 | mTrack = NULL; |
| 1544 | mNextOutput->mSampleRateHz = mSampleRateHz; |
| 1545 | mNextOutput->mMsecsPerFrame = mMsecsPerFrame; |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1546 | mNextOutput->mBytesWritten = mBytesWritten; |
Marco Nelissen | d791e09 | 2012-06-11 17:00:59 -0700 | [diff] [blame] | 1547 | mNextOutput->mFlags = mFlags; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size) |
| 1552 | { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1553 | LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback."); |
| 1554 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1555 | //ALOGV("write(%p, %u)", buffer, size); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1556 | if (mTrack) { |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1557 | ssize_t ret = mTrack->write(buffer, size); |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1558 | mBytesWritten += ret; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1559 | return ret; |
| 1560 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1561 | return NO_INIT; |
| 1562 | } |
| 1563 | |
| 1564 | void MediaPlayerService::AudioOutput::stop() |
| 1565 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1566 | ALOGV("stop"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | if (mTrack) mTrack->stop(); |
| 1568 | } |
| 1569 | |
| 1570 | void MediaPlayerService::AudioOutput::flush() |
| 1571 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1572 | ALOGV("flush"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | if (mTrack) mTrack->flush(); |
| 1574 | } |
| 1575 | |
| 1576 | void MediaPlayerService::AudioOutput::pause() |
| 1577 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1578 | ALOGV("pause"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | if (mTrack) mTrack->pause(); |
| 1580 | } |
| 1581 | |
| 1582 | void MediaPlayerService::AudioOutput::close() |
| 1583 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1584 | ALOGV("close"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | delete mTrack; |
| 1586 | mTrack = 0; |
| 1587 | } |
| 1588 | |
| 1589 | void MediaPlayerService::AudioOutput::setVolume(float left, float right) |
| 1590 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1591 | ALOGV("setVolume(%f, %f)", left, right); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | mLeftVolume = left; |
| 1593 | mRightVolume = right; |
| 1594 | if (mTrack) { |
| 1595 | mTrack->setVolume(left, right); |
| 1596 | } |
| 1597 | } |
| 1598 | |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1599 | status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille) |
| 1600 | { |
| 1601 | ALOGV("setPlaybackRatePermille(%d)", ratePermille); |
| 1602 | status_t res = NO_ERROR; |
| 1603 | if (mTrack) { |
| 1604 | res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000); |
| 1605 | } else { |
| 1606 | res = NO_INIT; |
| 1607 | } |
| 1608 | mPlaybackRatePermille = ratePermille; |
| 1609 | if (mSampleRateHz != 0) { |
| 1610 | mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz; |
| 1611 | } |
| 1612 | return res; |
| 1613 | } |
| 1614 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1615 | status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level) |
| 1616 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1617 | ALOGV("setAuxEffectSendLevel(%f)", level); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1618 | mSendLevel = level; |
| 1619 | if (mTrack) { |
| 1620 | return mTrack->setAuxEffectSendLevel(level); |
| 1621 | } |
| 1622 | return NO_ERROR; |
| 1623 | } |
| 1624 | |
| 1625 | status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId) |
| 1626 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1627 | ALOGV("attachAuxEffect(%d)", effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1628 | mAuxEffectId = effectId; |
| 1629 | if (mTrack) { |
| 1630 | return mTrack->attachAuxEffect(effectId); |
| 1631 | } |
| 1632 | return NO_ERROR; |
| 1633 | } |
| 1634 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1635 | // static |
| 1636 | void MediaPlayerService::AudioOutput::CallbackWrapper( |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 1637 | int event, void *cookie, void *info) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1638 | //ALOGV("callbackwrapper"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1639 | if (event != AudioTrack::EVENT_MORE_DATA) { |
| 1640 | return; |
| 1641 | } |
| 1642 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1643 | CallbackData *data = (CallbackData*)cookie; |
| 1644 | data->lock(); |
| 1645 | AudioOutput *me = data->getOutput(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1646 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1647 | if (me == NULL) { |
| 1648 | // no output set, likely because the track was scheduled to be reused |
| 1649 | // by another player, but the format turned out to be incompatible. |
| 1650 | data->unlock(); |
| 1651 | buffer->size = 0; |
| 1652 | return; |
| 1653 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1654 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1655 | size_t actualSize = (*me->mCallback)( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1656 | me, buffer->raw, buffer->size, me->mCallbackCookie); |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1657 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1658 | if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) { |
Andreas Huber | 51c1e0e | 2011-04-04 11:43:40 -0700 | [diff] [blame] | 1659 | // We've reached EOS but the audio track is not stopped yet, |
| 1660 | // keep playing silence. |
Andreas Huber | 2e8ffaf | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 1661 | |
Andreas Huber | 51c1e0e | 2011-04-04 11:43:40 -0700 | [diff] [blame] | 1662 | memset(buffer->raw, 0, buffer->size); |
| 1663 | actualSize = buffer->size; |
| 1664 | } |
| 1665 | |
| 1666 | buffer->size = actualSize; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1667 | data->unlock(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1670 | int MediaPlayerService::AudioOutput::getSessionId() const |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 1671 | { |
| 1672 | return mSessionId; |
| 1673 | } |
| 1674 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1675 | #undef LOG_TAG |
| 1676 | #define LOG_TAG "AudioCache" |
| 1677 | MediaPlayerService::AudioCache::AudioCache(const char* name) : |
| 1678 | mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0), |
| 1679 | mError(NO_ERROR), mCommandComplete(false) |
| 1680 | { |
| 1681 | // create ashmem heap |
| 1682 | mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name); |
| 1683 | } |
| 1684 | |
| 1685 | uint32_t MediaPlayerService::AudioCache::latency () const |
| 1686 | { |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | float MediaPlayerService::AudioCache::msecsPerFrame() const |
| 1691 | { |
| 1692 | return mMsecsPerFrame; |
| 1693 | } |
| 1694 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1695 | status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1696 | { |
| 1697 | if (position == 0) return BAD_VALUE; |
| 1698 | *position = mSize; |
| 1699 | return NO_ERROR; |
| 1700 | } |
| 1701 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1702 | status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const |
| 1703 | { |
| 1704 | if (written == 0) return BAD_VALUE; |
| 1705 | *written = mSize; |
| 1706 | return NO_ERROR; |
| 1707 | } |
| 1708 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1709 | //////////////////////////////////////////////////////////////////////////////// |
| 1710 | |
| 1711 | struct CallbackThread : public Thread { |
| 1712 | CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink, |
| 1713 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 1714 | void *cookie); |
| 1715 | |
| 1716 | protected: |
| 1717 | virtual ~CallbackThread(); |
| 1718 | |
| 1719 | virtual bool threadLoop(); |
| 1720 | |
| 1721 | private: |
| 1722 | wp<MediaPlayerBase::AudioSink> mSink; |
| 1723 | MediaPlayerBase::AudioSink::AudioCallback mCallback; |
| 1724 | void *mCookie; |
| 1725 | void *mBuffer; |
| 1726 | size_t mBufferSize; |
| 1727 | |
| 1728 | CallbackThread(const CallbackThread &); |
| 1729 | CallbackThread &operator=(const CallbackThread &); |
| 1730 | }; |
| 1731 | |
| 1732 | CallbackThread::CallbackThread( |
| 1733 | const wp<MediaPlayerBase::AudioSink> &sink, |
| 1734 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 1735 | void *cookie) |
| 1736 | : mSink(sink), |
| 1737 | mCallback(cb), |
| 1738 | mCookie(cookie), |
| 1739 | mBuffer(NULL), |
| 1740 | mBufferSize(0) { |
| 1741 | } |
| 1742 | |
| 1743 | CallbackThread::~CallbackThread() { |
| 1744 | if (mBuffer) { |
| 1745 | free(mBuffer); |
| 1746 | mBuffer = NULL; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | bool CallbackThread::threadLoop() { |
| 1751 | sp<MediaPlayerBase::AudioSink> sink = mSink.promote(); |
| 1752 | if (sink == NULL) { |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
| 1756 | if (mBuffer == NULL) { |
| 1757 | mBufferSize = sink->bufferSize(); |
| 1758 | mBuffer = malloc(mBufferSize); |
| 1759 | } |
| 1760 | |
| 1761 | size_t actualSize = |
| 1762 | (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie); |
| 1763 | |
| 1764 | if (actualSize > 0) { |
| 1765 | sink->write(mBuffer, actualSize); |
| 1766 | } |
| 1767 | |
| 1768 | return true; |
| 1769 | } |
| 1770 | |
| 1771 | //////////////////////////////////////////////////////////////////////////////// |
| 1772 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1773 | status_t MediaPlayerService::AudioCache::open( |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1774 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, |
| 1775 | audio_format_t format, int bufferCount, |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1776 | AudioCallback cb, void *cookie, audio_output_flags_t flags) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1777 | { |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1778 | ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount); |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1779 | if (mHeap->getHeapID() < 0) { |
| 1780 | return NO_INIT; |
| 1781 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1782 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | mSampleRate = sampleRate; |
| 1784 | mChannelCount = (uint16_t)channelCount; |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 1785 | mFormat = format; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | mMsecsPerFrame = 1.e3 / (float) sampleRate; |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1787 | |
| 1788 | if (cb != NULL) { |
| 1789 | mCallbackThread = new CallbackThread(this, cb, cookie); |
| 1790 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | return NO_ERROR; |
| 1792 | } |
| 1793 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1794 | void MediaPlayerService::AudioCache::start() { |
| 1795 | if (mCallbackThread != NULL) { |
| 1796 | mCallbackThread->run("AudioCache callback"); |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | void MediaPlayerService::AudioCache::stop() { |
| 1801 | if (mCallbackThread != NULL) { |
| 1802 | mCallbackThread->requestExitAndWait(); |
| 1803 | } |
| 1804 | } |
| 1805 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1806 | ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size) |
| 1807 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1808 | ALOGV("write(%p, %u)", buffer, size); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | if ((buffer == 0) || (size == 0)) return size; |
| 1810 | |
| 1811 | uint8_t* p = static_cast<uint8_t*>(mHeap->getBase()); |
| 1812 | if (p == NULL) return NO_INIT; |
| 1813 | p += mSize; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1814 | ALOGV("memcpy(%p, %p, %u)", p, buffer, size); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1815 | if (mSize + size > mHeap->getSize()) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1816 | ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1817 | size = mHeap->getSize() - mSize; |
| 1818 | } |
| 1819 | memcpy(p, buffer, size); |
| 1820 | mSize += size; |
| 1821 | return size; |
| 1822 | } |
| 1823 | |
| 1824 | // call with lock held |
| 1825 | status_t MediaPlayerService::AudioCache::wait() |
| 1826 | { |
| 1827 | Mutex::Autolock lock(mLock); |
Dave Sparks | 4bbc0ba | 2010-03-01 19:29:58 -0800 | [diff] [blame] | 1828 | while (!mCommandComplete) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | mSignal.wait(mLock); |
| 1830 | } |
| 1831 | mCommandComplete = false; |
| 1832 | |
| 1833 | if (mError == NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1834 | ALOGV("wait - success"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1835 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1836 | ALOGV("wait - error"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | } |
| 1838 | return mError; |
| 1839 | } |
| 1840 | |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 1841 | void MediaPlayerService::AudioCache::notify( |
| 1842 | void* cookie, int msg, int ext1, int ext2, const Parcel *obj) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1843 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1844 | ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1845 | AudioCache* p = static_cast<AudioCache*>(cookie); |
| 1846 | |
| 1847 | // ignore buffering messages |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1848 | switch (msg) |
| 1849 | { |
| 1850 | case MEDIA_ERROR: |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1851 | ALOGE("Error %d, %d occurred", ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1852 | p->mError = ext1; |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1853 | break; |
| 1854 | case MEDIA_PREPARED: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1855 | ALOGV("prepared"); |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1856 | break; |
| 1857 | case MEDIA_PLAYBACK_COMPLETE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1858 | ALOGV("playback complete"); |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1859 | break; |
| 1860 | default: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1861 | ALOGV("ignored"); |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1862 | return; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | // wake up thread |
Dave Sparks | fe4c6f0 | 2010-03-02 12:56:37 -0800 | [diff] [blame] | 1866 | Mutex::Autolock lock(p->mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | p->mCommandComplete = true; |
| 1868 | p->mSignal.signal(); |
| 1869 | } |
| 1870 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1871 | int MediaPlayerService::AudioCache::getSessionId() const |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 1872 | { |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1876 | void MediaPlayerService::addBatteryData(uint32_t params) |
| 1877 | { |
| 1878 | Mutex::Autolock lock(mLock); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1879 | |
| 1880 | int32_t time = systemTime() / 1000000L; |
| 1881 | |
| 1882 | // change audio output devices. This notification comes from AudioFlinger |
| 1883 | if ((params & kBatteryDataSpeakerOn) |
| 1884 | || (params & kBatteryDataOtherAudioDeviceOn)) { |
| 1885 | |
| 1886 | int deviceOn[NUM_AUDIO_DEVICES]; |
| 1887 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1888 | deviceOn[i] = 0; |
| 1889 | } |
| 1890 | |
| 1891 | if ((params & kBatteryDataSpeakerOn) |
| 1892 | && (params & kBatteryDataOtherAudioDeviceOn)) { |
| 1893 | deviceOn[SPEAKER_AND_OTHER] = 1; |
| 1894 | } else if (params & kBatteryDataSpeakerOn) { |
| 1895 | deviceOn[SPEAKER] = 1; |
| 1896 | } else { |
| 1897 | deviceOn[OTHER_AUDIO_DEVICE] = 1; |
| 1898 | } |
| 1899 | |
| 1900 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1901 | if (mBatteryAudio.deviceOn[i] != deviceOn[i]){ |
| 1902 | |
| 1903 | if (mBatteryAudio.refCount > 0) { // if playing audio |
| 1904 | if (!deviceOn[i]) { |
| 1905 | mBatteryAudio.lastTime[i] += time; |
| 1906 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 1907 | mBatteryAudio.lastTime[i] = 0; |
| 1908 | } else { |
| 1909 | mBatteryAudio.lastTime[i] = 0 - time; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | mBatteryAudio.deviceOn[i] = deviceOn[i]; |
| 1914 | } |
| 1915 | } |
| 1916 | return; |
| 1917 | } |
| 1918 | |
| 1919 | // an sudio stream is started |
| 1920 | if (params & kBatteryDataAudioFlingerStart) { |
| 1921 | // record the start time only if currently no other audio |
| 1922 | // is being played |
| 1923 | if (mBatteryAudio.refCount == 0) { |
| 1924 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1925 | if (mBatteryAudio.deviceOn[i]) { |
| 1926 | mBatteryAudio.lastTime[i] -= time; |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | mBatteryAudio.refCount ++; |
| 1932 | return; |
| 1933 | |
| 1934 | } else if (params & kBatteryDataAudioFlingerStop) { |
| 1935 | if (mBatteryAudio.refCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1936 | ALOGW("Battery track warning: refCount is <= 0"); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1937 | return; |
| 1938 | } |
| 1939 | |
| 1940 | // record the stop time only if currently this is the only |
| 1941 | // audio being played |
| 1942 | if (mBatteryAudio.refCount == 1) { |
| 1943 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1944 | if (mBatteryAudio.deviceOn[i]) { |
| 1945 | mBatteryAudio.lastTime[i] += time; |
| 1946 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 1947 | mBatteryAudio.lastTime[i] = 0; |
| 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | mBatteryAudio.refCount --; |
| 1953 | return; |
| 1954 | } |
| 1955 | |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1956 | int uid = IPCThreadState::self()->getCallingUid(); |
| 1957 | if (uid == AID_MEDIA) { |
| 1958 | return; |
| 1959 | } |
| 1960 | int index = mBatteryData.indexOfKey(uid); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1961 | |
| 1962 | if (index < 0) { // create a new entry for this UID |
| 1963 | BatteryUsageInfo info; |
| 1964 | info.audioTotalTime = 0; |
| 1965 | info.videoTotalTime = 0; |
| 1966 | info.audioLastTime = 0; |
| 1967 | info.videoLastTime = 0; |
| 1968 | info.refCount = 0; |
| 1969 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1970 | if (mBatteryData.add(uid, info) == NO_MEMORY) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1971 | ALOGE("Battery track error: no memory for new app"); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1972 | return; |
| 1973 | } |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | BatteryUsageInfo &info = mBatteryData.editValueFor(uid); |
| 1977 | |
| 1978 | if (params & kBatteryDataCodecStarted) { |
| 1979 | if (params & kBatteryDataTrackAudio) { |
| 1980 | info.audioLastTime -= time; |
| 1981 | info.refCount ++; |
| 1982 | } |
| 1983 | if (params & kBatteryDataTrackVideo) { |
| 1984 | info.videoLastTime -= time; |
| 1985 | info.refCount ++; |
| 1986 | } |
| 1987 | } else { |
| 1988 | if (info.refCount == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1989 | ALOGW("Battery track warning: refCount is already 0"); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1990 | return; |
| 1991 | } else if (info.refCount < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1992 | ALOGE("Battery track error: refCount < 0"); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1993 | mBatteryData.removeItem(uid); |
| 1994 | return; |
| 1995 | } |
| 1996 | |
| 1997 | if (params & kBatteryDataTrackAudio) { |
| 1998 | info.audioLastTime += time; |
| 1999 | info.refCount --; |
| 2000 | } |
| 2001 | if (params & kBatteryDataTrackVideo) { |
| 2002 | info.videoLastTime += time; |
| 2003 | info.refCount --; |
| 2004 | } |
| 2005 | |
| 2006 | // no stream is being played by this UID |
| 2007 | if (info.refCount == 0) { |
| 2008 | info.audioTotalTime += info.audioLastTime; |
| 2009 | info.audioLastTime = 0; |
| 2010 | info.videoTotalTime += info.videoLastTime; |
| 2011 | info.videoLastTime = 0; |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | status_t MediaPlayerService::pullBatteryData(Parcel* reply) { |
| 2017 | Mutex::Autolock lock(mLock); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2018 | |
| 2019 | // audio output devices usage |
| 2020 | int32_t time = systemTime() / 1000000L; //in ms |
| 2021 | int32_t totalTime; |
| 2022 | |
| 2023 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2024 | totalTime = mBatteryAudio.totalTime[i]; |
| 2025 | |
| 2026 | if (mBatteryAudio.deviceOn[i] |
| 2027 | && (mBatteryAudio.lastTime[i] != 0)) { |
| 2028 | int32_t tmpTime = mBatteryAudio.lastTime[i] + time; |
| 2029 | totalTime += tmpTime; |
| 2030 | } |
| 2031 | |
| 2032 | reply->writeInt32(totalTime); |
| 2033 | // reset the total time |
| 2034 | mBatteryAudio.totalTime[i] = 0; |
| 2035 | } |
| 2036 | |
| 2037 | // codec usage |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2038 | BatteryUsageInfo info; |
| 2039 | int size = mBatteryData.size(); |
| 2040 | |
| 2041 | reply->writeInt32(size); |
| 2042 | int i = 0; |
| 2043 | |
| 2044 | while (i < size) { |
| 2045 | info = mBatteryData.valueAt(i); |
| 2046 | |
| 2047 | reply->writeInt32(mBatteryData.keyAt(i)); //UID |
| 2048 | reply->writeInt32(info.audioTotalTime); |
| 2049 | reply->writeInt32(info.videoTotalTime); |
| 2050 | |
| 2051 | info.audioTotalTime = 0; |
| 2052 | info.videoTotalTime = 0; |
| 2053 | |
| 2054 | // remove the UID entry where no stream is being played |
| 2055 | if (info.refCount <= 0) { |
| 2056 | mBatteryData.removeItemsAt(i); |
| 2057 | size --; |
| 2058 | i --; |
| 2059 | } |
| 2060 | i++; |
| 2061 | } |
| 2062 | return NO_ERROR; |
| 2063 | } |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 2064 | } // namespace android |