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> |
| 26 | #include <dirent.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <string.h> |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <cutils/atomic.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> // for property_get |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 33 | |
| 34 | #include <utils/misc.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | #include <android_runtime/ActivityManager.h> |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 37 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 38 | #include <binder/IPCThreadState.h> |
| 39 | #include <binder/IServiceManager.h> |
| 40 | #include <binder/MemoryHeapBase.h> |
| 41 | #include <binder/MemoryBase.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 | #include <cutils/properties.h> |
| 47 | |
| 48 | #include <media/MediaPlayerInterface.h> |
| 49 | #include <media/mediarecorder.h> |
| 50 | #include <media/MediaMetadataRetrieverInterface.h> |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 51 | #include <media/Metadata.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | #include <media/AudioTrack.h> |
| 53 | |
| 54 | #include "MediaRecorderClient.h" |
| 55 | #include "MediaPlayerService.h" |
| 56 | #include "MetadataRetrieverClient.h" |
| 57 | |
| 58 | #include "MidiFile.h" |
| 59 | #include "VorbisPlayer.h" |
| 60 | #include <media/PVPlayer.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 61 | #include "TestPlayerStub.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 62 | #include "StagefrightPlayer.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 63 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 64 | #include <OMX.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 65 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | /* desktop Linux needs a little help with gettid() */ |
| 67 | #if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS) |
| 68 | #define __KERNEL__ |
| 69 | # include <linux/unistd.h> |
| 70 | #ifdef _syscall0 |
| 71 | _syscall0(pid_t,gettid) |
| 72 | #else |
| 73 | pid_t gettid() { return syscall(__NR_gettid);} |
| 74 | #endif |
| 75 | #undef __KERNEL__ |
| 76 | #endif |
| 77 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 78 | namespace { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 79 | using android::media::Metadata; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 80 | using android::status_t; |
| 81 | using android::OK; |
| 82 | using android::BAD_VALUE; |
| 83 | using android::NOT_ENOUGH_DATA; |
| 84 | using android::Parcel; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 85 | |
| 86 | // Max number of entries in the filter. |
| 87 | const int kMaxFilterSize = 64; // I pulled that out of thin air. |
| 88 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 89 | // FIXME: Move all the metadata related function in the Metadata.cpp |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 90 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 91 | |
| 92 | // Unmarshall a filter from a Parcel. |
| 93 | // Filter format in a parcel: |
| 94 | // |
| 95 | // 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 |
| 96 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 97 | // | number of entries (n) | |
| 98 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 99 | // | metadata type 1 | |
| 100 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 101 | // | metadata type 2 | |
| 102 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 103 | // .... |
| 104 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 105 | // | metadata type n | |
| 106 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 107 | // |
| 108 | // @param p Parcel that should start with a filter. |
| 109 | // @param[out] filter On exit contains the list of metadata type to be |
| 110 | // filtered. |
| 111 | // @param[out] status On exit contains the status code to be returned. |
| 112 | // @return true if the parcel starts with a valid filter. |
| 113 | bool unmarshallFilter(const Parcel& p, |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 114 | Metadata::Filter *filter, |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 115 | status_t *status) |
| 116 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 117 | int32_t val; |
| 118 | if (p.readInt32(&val) != OK) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 119 | { |
| 120 | LOGE("Failed to read filter's length"); |
| 121 | *status = NOT_ENOUGH_DATA; |
| 122 | return false; |
| 123 | } |
| 124 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 125 | if( val > kMaxFilterSize || val < 0) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 126 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 127 | LOGE("Invalid filter len %d", val); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 128 | *status = BAD_VALUE; |
| 129 | return false; |
| 130 | } |
| 131 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 132 | const size_t num = val; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 133 | |
| 134 | filter->clear(); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 135 | filter->setCapacity(num); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 136 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 137 | size_t size = num * sizeof(Metadata::Type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 138 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 139 | |
| 140 | if (p.dataAvail() < size) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 141 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 142 | LOGE("Filter too short expected %d but got %d", size, p.dataAvail()); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 143 | *status = NOT_ENOUGH_DATA; |
| 144 | return false; |
| 145 | } |
| 146 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 147 | const Metadata::Type *data = |
| 148 | static_cast<const Metadata::Type*>(p.readInplace(size)); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 149 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 150 | if (NULL == data) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 151 | { |
| 152 | LOGE("Filter had no data"); |
| 153 | *status = BAD_VALUE; |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | // TODO: The stl impl of vector would be more efficient here |
| 158 | // because it degenerates into a memcpy on pod types. Try to |
| 159 | // replace later or use stl::set. |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 160 | for (size_t i = 0; i < num; ++i) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 161 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 162 | filter->add(*data); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 163 | ++data; |
| 164 | } |
| 165 | *status = OK; |
| 166 | return true; |
| 167 | } |
| 168 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 169 | // @param filter Of metadata type. |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 170 | // @param val To be searched. |
| 171 | // @return true if a match was found. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 172 | bool findMetadata(const Metadata::Filter& filter, const int32_t val) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 173 | { |
| 174 | // Deal with empty and ANY right away |
| 175 | if (filter.isEmpty()) return false; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 176 | if (filter[0] == Metadata::kAny) return true; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 177 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 178 | return filter.indexOf(val) >= 0; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | } // anonymous namespace |
| 182 | |
| 183 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | namespace android { |
| 185 | |
| 186 | // TODO: Temp hack until we can register players |
| 187 | typedef struct { |
| 188 | const char *extension; |
| 189 | const player_type playertype; |
| 190 | } extmap; |
| 191 | extmap FILE_EXTS [] = { |
| 192 | {".mid", SONIVOX_PLAYER}, |
| 193 | {".midi", SONIVOX_PLAYER}, |
| 194 | {".smf", SONIVOX_PLAYER}, |
| 195 | {".xmf", SONIVOX_PLAYER}, |
| 196 | {".imy", SONIVOX_PLAYER}, |
| 197 | {".rtttl", SONIVOX_PLAYER}, |
| 198 | {".rtx", SONIVOX_PLAYER}, |
| 199 | {".ota", SONIVOX_PLAYER}, |
| 200 | {".ogg", VORBIS_PLAYER}, |
| 201 | {".oga", VORBIS_PLAYER}, |
| 202 | }; |
| 203 | |
| 204 | // 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] | 205 | /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4; |
| 206 | /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false; |
| 207 | |
| 208 | void MediaPlayerService::instantiate() { |
| 209 | defaultServiceManager()->addService( |
| 210 | String16("media.player"), new MediaPlayerService()); |
| 211 | } |
| 212 | |
| 213 | MediaPlayerService::MediaPlayerService() |
| 214 | { |
| 215 | LOGV("MediaPlayerService created"); |
| 216 | mNextConnId = 1; |
| 217 | } |
| 218 | |
| 219 | MediaPlayerService::~MediaPlayerService() |
| 220 | { |
| 221 | LOGV("MediaPlayerService destroyed"); |
| 222 | } |
| 223 | |
| 224 | sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid) |
| 225 | { |
Jean-Baptiste Queru | 6c5b210 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 226 | #ifndef NO_OPENCORE |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 227 | sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid); |
| 228 | wp<MediaRecorderClient> w = recorder; |
| 229 | Mutex::Autolock lock(mLock); |
| 230 | mMediaRecorderClients.add(w); |
Jean-Baptiste Queru | 6c5b210 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 231 | #else |
| 232 | sp<MediaRecorderClient> recorder = NULL; |
| 233 | #endif |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | LOGV("Create new media recorder client from pid %d", pid); |
| 235 | return recorder; |
| 236 | } |
| 237 | |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 238 | void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client) |
| 239 | { |
| 240 | Mutex::Autolock lock(mLock); |
| 241 | mMediaRecorderClients.remove(client); |
| 242 | LOGV("Delete media recorder client"); |
| 243 | } |
| 244 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid) |
| 246 | { |
| 247 | sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid); |
| 248 | LOGV("Create new media retriever from pid %d", pid); |
| 249 | return retriever; |
| 250 | } |
| 251 | |
| 252 | sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url) |
| 253 | { |
| 254 | int32_t connId = android_atomic_inc(&mNextConnId); |
| 255 | sp<Client> c = new Client(this, pid, connId, client); |
| 256 | LOGV("Create new client(%d) from pid %d, url=%s, connId=%d", connId, pid, url, connId); |
| 257 | if (NO_ERROR != c->setDataSource(url)) |
| 258 | { |
| 259 | c.clear(); |
| 260 | return c; |
| 261 | } |
| 262 | wp<Client> w = c; |
| 263 | Mutex::Autolock lock(mLock); |
| 264 | mClients.add(w); |
| 265 | return c; |
| 266 | } |
| 267 | |
| 268 | sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client, |
| 269 | int fd, int64_t offset, int64_t length) |
| 270 | { |
| 271 | int32_t connId = android_atomic_inc(&mNextConnId); |
| 272 | sp<Client> c = new Client(this, pid, connId, client); |
| 273 | LOGV("Create new client(%d) from pid %d, fd=%d, offset=%lld, length=%lld", |
| 274 | connId, pid, fd, offset, length); |
| 275 | if (NO_ERROR != c->setDataSource(fd, offset, length)) { |
| 276 | c.clear(); |
| 277 | } else { |
| 278 | wp<Client> w = c; |
| 279 | Mutex::Autolock lock(mLock); |
| 280 | mClients.add(w); |
| 281 | } |
| 282 | ::close(fd); |
| 283 | return c; |
| 284 | } |
| 285 | |
Andreas Huber | 318ad9c | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 286 | sp<IOMX> MediaPlayerService::getOMX() { |
| 287 | Mutex::Autolock autoLock(mLock); |
| 288 | |
| 289 | if (mOMX.get() == NULL) { |
| 290 | mOMX = new OMX; |
| 291 | } |
| 292 | |
| 293 | return mOMX; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 294 | } |
| 295 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const |
| 297 | { |
| 298 | const size_t SIZE = 256; |
| 299 | char buffer[SIZE]; |
| 300 | String8 result; |
| 301 | |
| 302 | result.append(" AudioCache\n"); |
| 303 | if (mHeap != 0) { |
| 304 | snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n", |
| 305 | mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice()); |
| 306 | result.append(buffer); |
| 307 | } |
| 308 | snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n", |
| 309 | mMsecsPerFrame, mChannelCount, mFormat, mFrameCount); |
| 310 | result.append(buffer); |
| 311 | snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n", |
| 312 | mSampleRate, mSize, mError, mCommandComplete?"true":"false"); |
| 313 | result.append(buffer); |
| 314 | ::write(fd, result.string(), result.size()); |
| 315 | return NO_ERROR; |
| 316 | } |
| 317 | |
| 318 | status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const |
| 319 | { |
| 320 | const size_t SIZE = 256; |
| 321 | char buffer[SIZE]; |
| 322 | String8 result; |
| 323 | |
| 324 | result.append(" AudioOutput\n"); |
| 325 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", |
| 326 | mStreamType, mLeftVolume, mRightVolume); |
| 327 | result.append(buffer); |
| 328 | snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n", |
| 329 | mMsecsPerFrame, mLatency); |
| 330 | result.append(buffer); |
| 331 | ::write(fd, result.string(), result.size()); |
| 332 | if (mTrack != 0) { |
| 333 | mTrack->dump(fd, args); |
| 334 | } |
| 335 | return NO_ERROR; |
| 336 | } |
| 337 | |
| 338 | status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const |
| 339 | { |
| 340 | const size_t SIZE = 256; |
| 341 | char buffer[SIZE]; |
| 342 | String8 result; |
| 343 | result.append(" Client\n"); |
| 344 | snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n", |
| 345 | mPid, mConnId, mStatus, mLoop?"true": "false"); |
| 346 | result.append(buffer); |
| 347 | write(fd, result.string(), result.size()); |
| 348 | if (mAudioOutput != 0) { |
| 349 | mAudioOutput->dump(fd, args); |
| 350 | } |
| 351 | write(fd, "\n", 1); |
| 352 | return NO_ERROR; |
| 353 | } |
| 354 | |
| 355 | static int myTid() { |
| 356 | #ifdef HAVE_GETTID |
| 357 | return gettid(); |
| 358 | #else |
| 359 | return getpid(); |
| 360 | #endif |
| 361 | } |
| 362 | |
| 363 | #if defined(__arm__) |
| 364 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, |
| 365 | size_t* infoSize, size_t* totalMemory, size_t* backtraceSize); |
| 366 | extern "C" void free_malloc_leak_info(uint8_t* info); |
| 367 | |
Andreas Huber | 1eea7f5 | 2009-10-27 15:50:04 -0700 | [diff] [blame] | 368 | // Use the String-class below instead of String8 to allocate all memory |
| 369 | // beforehand and not reenter the heap while we are examining it... |
| 370 | struct MyString8 { |
| 371 | static const size_t MAX_SIZE = 256 * 1024; |
| 372 | |
| 373 | MyString8() |
| 374 | : mPtr((char *)malloc(MAX_SIZE)) { |
| 375 | *mPtr = '\0'; |
| 376 | } |
| 377 | |
| 378 | ~MyString8() { |
| 379 | free(mPtr); |
| 380 | } |
| 381 | |
| 382 | void append(const char *s) { |
| 383 | strcat(mPtr, s); |
| 384 | } |
| 385 | |
| 386 | const char *string() const { |
| 387 | return mPtr; |
| 388 | } |
| 389 | |
| 390 | size_t size() const { |
| 391 | return strlen(mPtr); |
| 392 | } |
| 393 | |
| 394 | private: |
| 395 | char *mPtr; |
| 396 | |
| 397 | MyString8(const MyString8 &); |
| 398 | MyString8 &operator=(const MyString8 &); |
| 399 | }; |
| 400 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | void memStatus(int fd, const Vector<String16>& args) |
| 402 | { |
| 403 | const size_t SIZE = 256; |
| 404 | char buffer[SIZE]; |
Andreas Huber | 1eea7f5 | 2009-10-27 15:50:04 -0700 | [diff] [blame] | 405 | MyString8 result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | |
| 407 | typedef struct { |
| 408 | size_t size; |
| 409 | size_t dups; |
| 410 | intptr_t * backtrace; |
| 411 | } AllocEntry; |
| 412 | |
| 413 | uint8_t *info = NULL; |
| 414 | size_t overallSize = 0; |
| 415 | size_t infoSize = 0; |
| 416 | size_t totalMemory = 0; |
| 417 | size_t backtraceSize = 0; |
| 418 | |
| 419 | get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize); |
| 420 | if (info) { |
| 421 | uint8_t *ptr = info; |
| 422 | size_t count = overallSize / infoSize; |
| 423 | |
| 424 | snprintf(buffer, SIZE, " Allocation count %i\n", count); |
| 425 | result.append(buffer); |
| 426 | |
| 427 | AllocEntry * entries = new AllocEntry[count]; |
| 428 | |
| 429 | for (size_t i = 0; i < count; i++) { |
| 430 | // Each entry should be size_t, size_t, intptr_t[backtraceSize] |
| 431 | AllocEntry *e = &entries[i]; |
| 432 | |
| 433 | e->size = *reinterpret_cast<size_t *>(ptr); |
| 434 | ptr += sizeof(size_t); |
| 435 | |
| 436 | e->dups = *reinterpret_cast<size_t *>(ptr); |
| 437 | ptr += sizeof(size_t); |
| 438 | |
| 439 | e->backtrace = reinterpret_cast<intptr_t *>(ptr); |
| 440 | ptr += sizeof(intptr_t) * backtraceSize; |
| 441 | } |
| 442 | |
| 443 | // Now we need to sort the entries. They come sorted by size but |
| 444 | // not by stack trace which causes problems using diff. |
| 445 | bool moved; |
| 446 | do { |
| 447 | moved = false; |
| 448 | for (size_t i = 0; i < (count - 1); i++) { |
| 449 | AllocEntry *e1 = &entries[i]; |
| 450 | AllocEntry *e2 = &entries[i+1]; |
| 451 | |
| 452 | bool swap = e1->size < e2->size; |
| 453 | if (e1->size == e2->size) { |
| 454 | for(size_t j = 0; j < backtraceSize; j++) { |
| 455 | if (e1->backtrace[j] == e2->backtrace[j]) { |
| 456 | continue; |
| 457 | } |
| 458 | swap = e1->backtrace[j] < e2->backtrace[j]; |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | if (swap) { |
| 463 | AllocEntry t = entries[i]; |
| 464 | entries[i] = entries[i+1]; |
| 465 | entries[i+1] = t; |
| 466 | moved = true; |
| 467 | } |
| 468 | } |
| 469 | } while (moved); |
| 470 | |
| 471 | for (size_t i = 0; i < count; i++) { |
| 472 | AllocEntry *e = &entries[i]; |
| 473 | |
| 474 | snprintf(buffer, SIZE, "size %8i, dup %4i", e->size, e->dups); |
| 475 | result.append(buffer); |
| 476 | for (size_t ct = 0; (ct < backtraceSize) && e->backtrace[ct]; ct++) { |
| 477 | if (ct) { |
| 478 | result.append(", "); |
| 479 | } |
| 480 | snprintf(buffer, SIZE, "0x%08x", e->backtrace[ct]); |
| 481 | result.append(buffer); |
| 482 | } |
| 483 | result.append("\n"); |
| 484 | } |
| 485 | |
| 486 | delete[] entries; |
| 487 | free_malloc_leak_info(info); |
| 488 | } |
| 489 | |
| 490 | write(fd, result.string(), result.size()); |
| 491 | } |
| 492 | #endif |
| 493 | |
| 494 | status_t MediaPlayerService::dump(int fd, const Vector<String16>& args) |
| 495 | { |
| 496 | const size_t SIZE = 256; |
| 497 | char buffer[SIZE]; |
| 498 | String8 result; |
| 499 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 500 | snprintf(buffer, SIZE, "Permission Denial: " |
| 501 | "can't dump MediaPlayerService from pid=%d, uid=%d\n", |
| 502 | IPCThreadState::self()->getCallingPid(), |
| 503 | IPCThreadState::self()->getCallingUid()); |
| 504 | result.append(buffer); |
| 505 | } else { |
| 506 | Mutex::Autolock lock(mLock); |
| 507 | for (int i = 0, n = mClients.size(); i < n; ++i) { |
| 508 | sp<Client> c = mClients[i].promote(); |
| 509 | if (c != 0) c->dump(fd, args); |
| 510 | } |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 511 | for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) { |
| 512 | result.append(" MediaRecorderClient\n"); |
| 513 | sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote(); |
| 514 | snprintf(buffer, 255, " pid(%d)\n\n", c->mPid); |
| 515 | result.append(buffer); |
| 516 | } |
| 517 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | result.append(" Files opened and/or mapped:\n"); |
| 519 | snprintf(buffer, SIZE, "/proc/%d/maps", myTid()); |
| 520 | FILE *f = fopen(buffer, "r"); |
| 521 | if (f) { |
| 522 | while (!feof(f)) { |
| 523 | fgets(buffer, SIZE, f); |
| 524 | if (strstr(buffer, " /sdcard/") || |
| 525 | strstr(buffer, " /system/sounds/") || |
| 526 | strstr(buffer, " /system/media/")) { |
| 527 | result.append(" "); |
| 528 | result.append(buffer); |
| 529 | } |
| 530 | } |
| 531 | fclose(f); |
| 532 | } else { |
| 533 | result.append("couldn't open "); |
| 534 | result.append(buffer); |
| 535 | result.append("\n"); |
| 536 | } |
| 537 | |
| 538 | snprintf(buffer, SIZE, "/proc/%d/fd", myTid()); |
| 539 | DIR *d = opendir(buffer); |
| 540 | if (d) { |
| 541 | struct dirent *ent; |
| 542 | while((ent = readdir(d)) != NULL) { |
| 543 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
| 544 | snprintf(buffer, SIZE, "/proc/%d/fd/%s", myTid(), ent->d_name); |
| 545 | struct stat s; |
| 546 | if (lstat(buffer, &s) == 0) { |
| 547 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 548 | char linkto[256]; |
| 549 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 550 | if(len > 0) { |
| 551 | if(len > 255) { |
| 552 | linkto[252] = '.'; |
| 553 | linkto[253] = '.'; |
| 554 | linkto[254] = '.'; |
| 555 | linkto[255] = 0; |
| 556 | } else { |
| 557 | linkto[len] = 0; |
| 558 | } |
| 559 | if (strstr(linkto, "/sdcard/") == linkto || |
| 560 | strstr(linkto, "/system/sounds/") == linkto || |
| 561 | strstr(linkto, "/system/media/") == linkto) { |
| 562 | result.append(" "); |
| 563 | result.append(buffer); |
| 564 | result.append(" -> "); |
| 565 | result.append(linkto); |
| 566 | result.append("\n"); |
| 567 | } |
| 568 | } |
| 569 | } else { |
| 570 | result.append(" unexpected type for "); |
| 571 | result.append(buffer); |
| 572 | result.append("\n"); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | closedir(d); |
| 578 | } else { |
| 579 | result.append("couldn't open "); |
| 580 | result.append(buffer); |
| 581 | result.append("\n"); |
| 582 | } |
| 583 | |
| 584 | #if defined(__arm__) |
| 585 | bool dumpMem = false; |
| 586 | for (size_t i = 0; i < args.size(); i++) { |
| 587 | if (args[i] == String16("-m")) { |
| 588 | dumpMem = true; |
| 589 | } |
| 590 | } |
| 591 | if (dumpMem) { |
| 592 | memStatus(fd, args); |
| 593 | } |
| 594 | #endif |
| 595 | } |
| 596 | write(fd, result.string(), result.size()); |
| 597 | return NO_ERROR; |
| 598 | } |
| 599 | |
| 600 | void MediaPlayerService::removeClient(wp<Client> client) |
| 601 | { |
| 602 | Mutex::Autolock lock(mLock); |
| 603 | mClients.remove(client); |
| 604 | } |
| 605 | |
| 606 | MediaPlayerService::Client::Client(const sp<MediaPlayerService>& service, pid_t pid, |
| 607 | int32_t connId, const sp<IMediaPlayerClient>& client) |
| 608 | { |
| 609 | LOGV("Client(%d) constructor", connId); |
| 610 | mPid = pid; |
| 611 | mConnId = connId; |
| 612 | mService = service; |
| 613 | mClient = client; |
| 614 | mLoop = false; |
| 615 | mStatus = NO_INIT; |
| 616 | #if CALLBACK_ANTAGONIZER |
| 617 | LOGD("create Antagonizer"); |
| 618 | mAntagonizer = new Antagonizer(notify, this); |
| 619 | #endif |
| 620 | } |
| 621 | |
| 622 | MediaPlayerService::Client::~Client() |
| 623 | { |
| 624 | LOGV("Client(%d) destructor pid = %d", mConnId, mPid); |
| 625 | mAudioOutput.clear(); |
| 626 | wp<Client> client(this); |
| 627 | disconnect(); |
| 628 | mService->removeClient(client); |
| 629 | } |
| 630 | |
| 631 | void MediaPlayerService::Client::disconnect() |
| 632 | { |
| 633 | LOGV("disconnect(%d) from pid %d", mConnId, mPid); |
| 634 | // grab local reference and clear main reference to prevent future |
| 635 | // access to object |
| 636 | sp<MediaPlayerBase> p; |
| 637 | { |
| 638 | Mutex::Autolock l(mLock); |
| 639 | p = mPlayer; |
| 640 | } |
Dave Sparks | 795fa58 | 2009-03-24 17:57:12 -0700 | [diff] [blame] | 641 | mClient.clear(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 642 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | mPlayer.clear(); |
| 644 | |
| 645 | // clear the notification to prevent callbacks to dead client |
| 646 | // and reset the player. We assume the player will serialize |
| 647 | // access to itself if necessary. |
| 648 | if (p != 0) { |
| 649 | p->setNotifyCallback(0, 0); |
| 650 | #if CALLBACK_ANTAGONIZER |
| 651 | LOGD("kill Antagonizer"); |
| 652 | mAntagonizer->kill(); |
| 653 | #endif |
| 654 | p->reset(); |
| 655 | } |
| 656 | |
| 657 | IPCThreadState::self()->flushCommands(); |
| 658 | } |
| 659 | |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 660 | static player_type getDefaultPlayerType() { |
Andreas Huber | 8f0e4aa | 2009-09-11 09:54:52 -0700 | [diff] [blame] | 661 | #if BUILD_WITH_FULL_STAGEFRIGHT |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 662 | char value[PROPERTY_VALUE_MAX]; |
| 663 | if (property_get("media.stagefright.enable-player", value, NULL) |
| 664 | && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { |
| 665 | return STAGEFRIGHT_PLAYER; |
| 666 | } |
Andreas Huber | 8f0e4aa | 2009-09-11 09:54:52 -0700 | [diff] [blame] | 667 | #endif |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 668 | |
| 669 | return PV_PLAYER; |
| 670 | } |
| 671 | |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 672 | player_type getPlayerType(int fd, int64_t offset, int64_t length) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | { |
| 674 | char buf[20]; |
| 675 | lseek(fd, offset, SEEK_SET); |
| 676 | read(fd, buf, sizeof(buf)); |
| 677 | lseek(fd, offset, SEEK_SET); |
| 678 | |
| 679 | long ident = *((long*)buf); |
| 680 | |
| 681 | // Ogg vorbis? |
| 682 | if (ident == 0x5367674f) // 'OggS' |
| 683 | return VORBIS_PLAYER; |
| 684 | |
| 685 | // Some kind of MIDI? |
| 686 | EAS_DATA_HANDLE easdata; |
| 687 | if (EAS_Init(&easdata) == EAS_SUCCESS) { |
| 688 | EAS_FILE locator; |
| 689 | locator.path = NULL; |
| 690 | locator.fd = fd; |
| 691 | locator.offset = offset; |
| 692 | locator.length = length; |
| 693 | EAS_HANDLE eashandle; |
| 694 | if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) { |
| 695 | EAS_CloseFile(easdata, eashandle); |
| 696 | EAS_Shutdown(easdata); |
| 697 | return SONIVOX_PLAYER; |
| 698 | } |
| 699 | EAS_Shutdown(easdata); |
| 700 | } |
| 701 | |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 702 | return getDefaultPlayerType(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | } |
| 704 | |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 705 | player_type getPlayerType(const char* url) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | { |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 707 | if (TestPlayerStub::canBeUsed(url)) { |
| 708 | return TEST_PLAYER; |
| 709 | } |
| 710 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | // use MidiFile for MIDI extensions |
| 712 | int lenURL = strlen(url); |
| 713 | for (int i = 0; i < NELEM(FILE_EXTS); ++i) { |
| 714 | int len = strlen(FILE_EXTS[i].extension); |
| 715 | int start = lenURL - len; |
| 716 | if (start > 0) { |
| 717 | if (!strncmp(url + start, FILE_EXTS[i].extension, len)) { |
| 718 | return FILE_EXTS[i].playertype; |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
Andreas Huber | 47945ea | 2009-12-17 13:31:13 -0800 | [diff] [blame] | 723 | if (!strncasecmp(url, "http://", 7)) { |
Andreas Huber | f1350fd | 2010-01-04 17:27:37 -0800 | [diff] [blame] | 724 | char value[PROPERTY_VALUE_MAX]; |
| 725 | if (!property_get("media.stagefright.enable-http", value, NULL) |
| 726 | || (strcmp(value, "1") && strcasecmp(value, "true"))) { |
| 727 | // For now, we're going to use PV for http-based playback |
| 728 | // by default until we can clear up a few more issues. |
| 729 | return PV_PLAYER; |
| 730 | } |
Andreas Huber | 47945ea | 2009-12-17 13:31:13 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Andreas Huber | 47f59cf | 2009-08-07 09:30:32 -0700 | [diff] [blame] | 733 | return getDefaultPlayerType(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie, |
| 737 | notify_callback_f notifyFunc) |
| 738 | { |
| 739 | sp<MediaPlayerBase> p; |
| 740 | switch (playerType) { |
Jean-Baptiste Queru | 6c5b210 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 741 | #ifndef NO_OPENCORE |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | case PV_PLAYER: |
| 743 | LOGV(" create PVPlayer"); |
| 744 | p = new PVPlayer(); |
| 745 | break; |
Jean-Baptiste Queru | 6c5b210 | 2009-03-21 11:40:18 -0700 | [diff] [blame] | 746 | #endif |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | case SONIVOX_PLAYER: |
| 748 | LOGV(" create MidiFile"); |
| 749 | p = new MidiFile(); |
| 750 | break; |
| 751 | case VORBIS_PLAYER: |
| 752 | LOGV(" create VorbisPlayer"); |
| 753 | p = new VorbisPlayer(); |
| 754 | break; |
Andreas Huber | 8f0e4aa | 2009-09-11 09:54:52 -0700 | [diff] [blame] | 755 | #if BUILD_WITH_FULL_STAGEFRIGHT |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 756 | case STAGEFRIGHT_PLAYER: |
| 757 | LOGV(" create StagefrightPlayer"); |
| 758 | p = new StagefrightPlayer; |
| 759 | break; |
Andreas Huber | 8f0e4aa | 2009-09-11 09:54:52 -0700 | [diff] [blame] | 760 | #endif |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 761 | case TEST_PLAYER: |
| 762 | LOGV("Create Test Player stub"); |
| 763 | p = new TestPlayerStub(); |
| 764 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } |
| 766 | if (p != NULL) { |
| 767 | if (p->initCheck() == NO_ERROR) { |
| 768 | p->setNotifyCallback(cookie, notifyFunc); |
| 769 | } else { |
| 770 | p.clear(); |
| 771 | } |
| 772 | } |
| 773 | if (p == NULL) { |
| 774 | LOGE("Failed to create player object"); |
| 775 | } |
| 776 | return p; |
| 777 | } |
| 778 | |
| 779 | sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType) |
| 780 | { |
| 781 | // determine if we have the right player type |
| 782 | sp<MediaPlayerBase> p = mPlayer; |
| 783 | if ((p != NULL) && (p->playerType() != playerType)) { |
| 784 | LOGV("delete player"); |
| 785 | p.clear(); |
| 786 | } |
| 787 | if (p == NULL) { |
| 788 | p = android::createPlayer(playerType, this, notify); |
| 789 | } |
| 790 | return p; |
| 791 | } |
| 792 | |
| 793 | status_t MediaPlayerService::Client::setDataSource(const char *url) |
| 794 | { |
| 795 | LOGV("setDataSource(%s)", url); |
| 796 | if (url == NULL) |
| 797 | return UNKNOWN_ERROR; |
| 798 | |
| 799 | if (strncmp(url, "content://", 10) == 0) { |
| 800 | // get a filedescriptor for the content Uri and |
| 801 | // pass it to the setDataSource(fd) method |
| 802 | |
| 803 | String16 url16(url); |
| 804 | int fd = android::openContentProviderFile(url16); |
| 805 | if (fd < 0) |
| 806 | { |
| 807 | LOGE("Couldn't open fd for %s", url); |
| 808 | return UNKNOWN_ERROR; |
| 809 | } |
| 810 | setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus |
| 811 | close(fd); |
| 812 | return mStatus; |
| 813 | } else { |
| 814 | player_type playerType = getPlayerType(url); |
| 815 | LOGV("player type = %d", playerType); |
| 816 | |
| 817 | // create the right type of player |
| 818 | sp<MediaPlayerBase> p = createPlayer(playerType); |
| 819 | if (p == NULL) return NO_INIT; |
| 820 | |
| 821 | if (!p->hardwareOutput()) { |
| 822 | mAudioOutput = new AudioOutput(); |
| 823 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 824 | } |
| 825 | |
| 826 | // now set data source |
| 827 | LOGV(" setDataSource"); |
| 828 | mStatus = p->setDataSource(url); |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 829 | if (mStatus == NO_ERROR) { |
| 830 | mPlayer = p; |
| 831 | } else { |
| 832 | LOGE(" error: %d", mStatus); |
| 833 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | return mStatus; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length) |
| 839 | { |
| 840 | LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length); |
| 841 | struct stat sb; |
| 842 | int ret = fstat(fd, &sb); |
| 843 | if (ret != 0) { |
| 844 | LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
| 845 | return UNKNOWN_ERROR; |
| 846 | } |
| 847 | |
| 848 | LOGV("st_dev = %llu", sb.st_dev); |
| 849 | LOGV("st_mode = %u", sb.st_mode); |
| 850 | LOGV("st_uid = %lu", sb.st_uid); |
| 851 | LOGV("st_gid = %lu", sb.st_gid); |
| 852 | LOGV("st_size = %llu", sb.st_size); |
| 853 | |
| 854 | if (offset >= sb.st_size) { |
| 855 | LOGE("offset error"); |
| 856 | ::close(fd); |
| 857 | return UNKNOWN_ERROR; |
| 858 | } |
| 859 | if (offset + length > sb.st_size) { |
| 860 | length = sb.st_size - offset; |
| 861 | LOGV("calculated length = %lld", length); |
| 862 | } |
| 863 | |
| 864 | player_type playerType = getPlayerType(fd, offset, length); |
| 865 | LOGV("player type = %d", playerType); |
| 866 | |
| 867 | // create the right type of player |
| 868 | sp<MediaPlayerBase> p = createPlayer(playerType); |
| 869 | if (p == NULL) return NO_INIT; |
| 870 | |
| 871 | if (!p->hardwareOutput()) { |
| 872 | mAudioOutput = new AudioOutput(); |
| 873 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 874 | } |
| 875 | |
| 876 | // now set data source |
| 877 | mStatus = p->setDataSource(fd, offset, length); |
| 878 | if (mStatus == NO_ERROR) mPlayer = p; |
| 879 | return mStatus; |
| 880 | } |
| 881 | |
| 882 | status_t MediaPlayerService::Client::setVideoSurface(const sp<ISurface>& surface) |
| 883 | { |
| 884 | LOGV("[%d] setVideoSurface(%p)", mConnId, surface.get()); |
| 885 | sp<MediaPlayerBase> p = getPlayer(); |
| 886 | if (p == 0) return UNKNOWN_ERROR; |
| 887 | return p->setVideoSurface(surface); |
| 888 | } |
| 889 | |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 890 | status_t MediaPlayerService::Client::invoke(const Parcel& request, |
| 891 | Parcel *reply) |
| 892 | { |
| 893 | sp<MediaPlayerBase> p = getPlayer(); |
| 894 | if (p == NULL) return UNKNOWN_ERROR; |
| 895 | return p->invoke(request, reply); |
| 896 | } |
| 897 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 898 | // This call doesn't need to access the native player. |
| 899 | status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter) |
| 900 | { |
| 901 | status_t status; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 902 | media::Metadata::Filter allow, drop; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 903 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 904 | if (unmarshallFilter(filter, &allow, &status) && |
| 905 | unmarshallFilter(filter, &drop, &status)) { |
| 906 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 907 | |
| 908 | mMetadataAllow = allow; |
| 909 | mMetadataDrop = drop; |
| 910 | } |
| 911 | return status; |
| 912 | } |
| 913 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 914 | status_t MediaPlayerService::Client::getMetadata( |
| 915 | bool update_only, bool apply_filter, Parcel *reply) |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 916 | { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 917 | sp<MediaPlayerBase> player = getPlayer(); |
| 918 | if (player == 0) return UNKNOWN_ERROR; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 919 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 920 | status_t status; |
| 921 | // Placeholder for the return code, updated by the caller. |
| 922 | reply->writeInt32(-1); |
| 923 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 924 | media::Metadata::Filter ids; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 925 | |
| 926 | // We don't block notifications while we fetch the data. We clear |
| 927 | // mMetadataUpdated first so we don't lose notifications happening |
| 928 | // during the rest of this call. |
| 929 | { |
| 930 | Mutex::Autolock lock(mLock); |
| 931 | if (update_only) { |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 932 | ids = mMetadataUpdated; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 933 | } |
| 934 | mMetadataUpdated.clear(); |
| 935 | } |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 936 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 937 | media::Metadata metadata(reply); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 938 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 939 | metadata.appendHeader(); |
| 940 | status = player->getMetadata(ids, reply); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 941 | |
| 942 | if (status != OK) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 943 | metadata.resetParcel(); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 944 | LOGE("getMetadata failed %d", status); |
| 945 | return status; |
| 946 | } |
| 947 | |
| 948 | // FIXME: Implement filtering on the result. Not critical since |
| 949 | // filtering takes place on the update notifications already. This |
| 950 | // would be when all the metadata are fetch and a filter is set. |
| 951 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 952 | // Everything is fine, update the metadata length. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 953 | metadata.updateLength(); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 954 | return OK; |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 955 | } |
| 956 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | status_t MediaPlayerService::Client::prepareAsync() |
| 958 | { |
| 959 | LOGV("[%d] prepareAsync", mConnId); |
| 960 | sp<MediaPlayerBase> p = getPlayer(); |
| 961 | if (p == 0) return UNKNOWN_ERROR; |
| 962 | status_t ret = p->prepareAsync(); |
| 963 | #if CALLBACK_ANTAGONIZER |
| 964 | LOGD("start Antagonizer"); |
| 965 | if (ret == NO_ERROR) mAntagonizer->start(); |
| 966 | #endif |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | status_t MediaPlayerService::Client::start() |
| 971 | { |
| 972 | LOGV("[%d] start", mConnId); |
| 973 | sp<MediaPlayerBase> p = getPlayer(); |
| 974 | if (p == 0) return UNKNOWN_ERROR; |
| 975 | p->setLooping(mLoop); |
| 976 | return p->start(); |
| 977 | } |
| 978 | |
| 979 | status_t MediaPlayerService::Client::stop() |
| 980 | { |
| 981 | LOGV("[%d] stop", mConnId); |
| 982 | sp<MediaPlayerBase> p = getPlayer(); |
| 983 | if (p == 0) return UNKNOWN_ERROR; |
| 984 | return p->stop(); |
| 985 | } |
| 986 | |
| 987 | status_t MediaPlayerService::Client::pause() |
| 988 | { |
| 989 | LOGV("[%d] pause", mConnId); |
| 990 | sp<MediaPlayerBase> p = getPlayer(); |
| 991 | if (p == 0) return UNKNOWN_ERROR; |
| 992 | return p->pause(); |
| 993 | } |
| 994 | |
| 995 | status_t MediaPlayerService::Client::isPlaying(bool* state) |
| 996 | { |
| 997 | *state = false; |
| 998 | sp<MediaPlayerBase> p = getPlayer(); |
| 999 | if (p == 0) return UNKNOWN_ERROR; |
| 1000 | *state = p->isPlaying(); |
| 1001 | LOGV("[%d] isPlaying: %d", mConnId, *state); |
| 1002 | return NO_ERROR; |
| 1003 | } |
| 1004 | |
| 1005 | status_t MediaPlayerService::Client::getCurrentPosition(int *msec) |
| 1006 | { |
| 1007 | LOGV("getCurrentPosition"); |
| 1008 | sp<MediaPlayerBase> p = getPlayer(); |
| 1009 | if (p == 0) return UNKNOWN_ERROR; |
| 1010 | status_t ret = p->getCurrentPosition(msec); |
| 1011 | if (ret == NO_ERROR) { |
| 1012 | LOGV("[%d] getCurrentPosition = %d", mConnId, *msec); |
| 1013 | } else { |
| 1014 | LOGE("getCurrentPosition returned %d", ret); |
| 1015 | } |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | status_t MediaPlayerService::Client::getDuration(int *msec) |
| 1020 | { |
| 1021 | LOGV("getDuration"); |
| 1022 | sp<MediaPlayerBase> p = getPlayer(); |
| 1023 | if (p == 0) return UNKNOWN_ERROR; |
| 1024 | status_t ret = p->getDuration(msec); |
| 1025 | if (ret == NO_ERROR) { |
| 1026 | LOGV("[%d] getDuration = %d", mConnId, *msec); |
| 1027 | } else { |
| 1028 | LOGE("getDuration returned %d", ret); |
| 1029 | } |
| 1030 | return ret; |
| 1031 | } |
| 1032 | |
| 1033 | status_t MediaPlayerService::Client::seekTo(int msec) |
| 1034 | { |
| 1035 | LOGV("[%d] seekTo(%d)", mConnId, msec); |
| 1036 | sp<MediaPlayerBase> p = getPlayer(); |
| 1037 | if (p == 0) return UNKNOWN_ERROR; |
| 1038 | return p->seekTo(msec); |
| 1039 | } |
| 1040 | |
| 1041 | status_t MediaPlayerService::Client::reset() |
| 1042 | { |
| 1043 | LOGV("[%d] reset", mConnId); |
| 1044 | sp<MediaPlayerBase> p = getPlayer(); |
| 1045 | if (p == 0) return UNKNOWN_ERROR; |
| 1046 | return p->reset(); |
| 1047 | } |
| 1048 | |
| 1049 | status_t MediaPlayerService::Client::setAudioStreamType(int type) |
| 1050 | { |
| 1051 | LOGV("[%d] setAudioStreamType(%d)", mConnId, type); |
| 1052 | // TODO: for hardware output, call player instead |
| 1053 | Mutex::Autolock l(mLock); |
| 1054 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); |
| 1055 | return NO_ERROR; |
| 1056 | } |
| 1057 | |
| 1058 | status_t MediaPlayerService::Client::setLooping(int loop) |
| 1059 | { |
| 1060 | LOGV("[%d] setLooping(%d)", mConnId, loop); |
| 1061 | mLoop = loop; |
| 1062 | sp<MediaPlayerBase> p = getPlayer(); |
| 1063 | if (p != 0) return p->setLooping(loop); |
| 1064 | return NO_ERROR; |
| 1065 | } |
| 1066 | |
| 1067 | status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume) |
| 1068 | { |
| 1069 | LOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); |
| 1070 | // TODO: for hardware output, call player instead |
| 1071 | Mutex::Autolock l(mLock); |
| 1072 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); |
| 1073 | return NO_ERROR; |
| 1074 | } |
| 1075 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1076 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | void MediaPlayerService::Client::notify(void* cookie, int msg, int ext1, int ext2) |
| 1078 | { |
| 1079 | Client* client = static_cast<Client*>(cookie); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1080 | |
| 1081 | if (MEDIA_INFO == msg && |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1082 | MEDIA_INFO_METADATA_UPDATE == ext1) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1083 | const media::Metadata::Type metadata_type = ext2; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1084 | |
| 1085 | if(client->shouldDropMetadata(metadata_type)) { |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | // Update the list of metadata that have changed. getMetadata |
| 1090 | // also access mMetadataUpdated and clears it. |
| 1091 | client->addNewMetadataUpdate(metadata_type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1092 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | LOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2); |
| 1094 | client->mClient->notify(msg, ext1, ext2); |
| 1095 | } |
| 1096 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1097 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1098 | bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1099 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1100 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1101 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1102 | if (findMetadata(mMetadataDrop, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1103 | return true; |
| 1104 | } |
| 1105 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1106 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1107 | return false; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1108 | } else { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1109 | return true; |
| 1110 | } |
| 1111 | } |
| 1112 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1113 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1114 | void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1115 | Mutex::Autolock lock(mLock); |
| 1116 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1117 | mMetadataUpdated.add(metadata_type); |
| 1118 | } |
| 1119 | } |
| 1120 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | #if CALLBACK_ANTAGONIZER |
| 1122 | const int Antagonizer::interval = 10000; // 10 msecs |
| 1123 | |
| 1124 | Antagonizer::Antagonizer(notify_callback_f cb, void* client) : |
| 1125 | mExit(false), mActive(false), mClient(client), mCb(cb) |
| 1126 | { |
| 1127 | createThread(callbackThread, this); |
| 1128 | } |
| 1129 | |
| 1130 | void Antagonizer::kill() |
| 1131 | { |
| 1132 | Mutex::Autolock _l(mLock); |
| 1133 | mActive = false; |
| 1134 | mExit = true; |
| 1135 | mCondition.wait(mLock); |
| 1136 | } |
| 1137 | |
| 1138 | int Antagonizer::callbackThread(void* user) |
| 1139 | { |
| 1140 | LOGD("Antagonizer started"); |
| 1141 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); |
| 1142 | while (!p->mExit) { |
| 1143 | if (p->mActive) { |
| 1144 | LOGV("send event"); |
| 1145 | p->mCb(p->mClient, 0, 0, 0); |
| 1146 | } |
| 1147 | usleep(interval); |
| 1148 | } |
| 1149 | Mutex::Autolock _l(p->mLock); |
| 1150 | p->mCondition.signal(); |
| 1151 | LOGD("Antagonizer stopped"); |
| 1152 | return 0; |
| 1153 | } |
| 1154 | #endif |
| 1155 | |
| 1156 | static size_t kDefaultHeapSize = 1024 * 1024; // 1MB |
| 1157 | |
| 1158 | sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
| 1159 | { |
| 1160 | LOGV("decode(%s)", url); |
| 1161 | sp<MemoryBase> mem; |
| 1162 | sp<MediaPlayerBase> player; |
| 1163 | |
| 1164 | // Protect our precious, precious DRMd ringtones by only allowing |
| 1165 | // decoding of http, but not filesystem paths or content Uris. |
| 1166 | // If the application wants to decode those, it should open a |
| 1167 | // filedescriptor for them and use that. |
| 1168 | if (url != NULL && strncmp(url, "http://", 7) != 0) { |
| 1169 | LOGD("Can't decode %s by path, use filedescriptor instead", url); |
| 1170 | return mem; |
| 1171 | } |
| 1172 | |
| 1173 | player_type playerType = getPlayerType(url); |
| 1174 | LOGV("player type = %d", playerType); |
| 1175 | |
| 1176 | // create the right type of player |
| 1177 | sp<AudioCache> cache = new AudioCache(url); |
| 1178 | player = android::createPlayer(playerType, cache.get(), cache->notify); |
| 1179 | if (player == NULL) goto Exit; |
| 1180 | if (player->hardwareOutput()) goto Exit; |
| 1181 | |
| 1182 | static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache); |
| 1183 | |
| 1184 | // set data source |
| 1185 | if (player->setDataSource(url) != NO_ERROR) goto Exit; |
| 1186 | |
| 1187 | LOGV("prepare"); |
| 1188 | player->prepareAsync(); |
| 1189 | |
| 1190 | LOGV("wait for prepare"); |
| 1191 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1192 | |
| 1193 | LOGV("start"); |
| 1194 | player->start(); |
| 1195 | |
| 1196 | LOGV("wait for playback complete"); |
| 1197 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1198 | |
| 1199 | mem = new MemoryBase(cache->getHeap(), 0, cache->size()); |
| 1200 | *pSampleRate = cache->sampleRate(); |
| 1201 | *pNumChannels = cache->channelCount(); |
| 1202 | *pFormat = cache->format(); |
| 1203 | LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat); |
| 1204 | |
| 1205 | Exit: |
| 1206 | if (player != 0) player->reset(); |
| 1207 | return mem; |
| 1208 | } |
| 1209 | |
| 1210 | sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
| 1211 | { |
| 1212 | LOGV("decode(%d, %lld, %lld)", fd, offset, length); |
| 1213 | sp<MemoryBase> mem; |
| 1214 | sp<MediaPlayerBase> player; |
| 1215 | |
| 1216 | player_type playerType = getPlayerType(fd, offset, length); |
| 1217 | LOGV("player type = %d", playerType); |
| 1218 | |
| 1219 | // create the right type of player |
| 1220 | sp<AudioCache> cache = new AudioCache("decode_fd"); |
| 1221 | player = android::createPlayer(playerType, cache.get(), cache->notify); |
| 1222 | if (player == NULL) goto Exit; |
| 1223 | if (player->hardwareOutput()) goto Exit; |
| 1224 | |
| 1225 | static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache); |
| 1226 | |
| 1227 | // set data source |
| 1228 | if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit; |
| 1229 | |
| 1230 | LOGV("prepare"); |
| 1231 | player->prepareAsync(); |
| 1232 | |
| 1233 | LOGV("wait for prepare"); |
| 1234 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1235 | |
| 1236 | LOGV("start"); |
| 1237 | player->start(); |
| 1238 | |
| 1239 | LOGV("wait for playback complete"); |
| 1240 | if (cache->wait() != NO_ERROR) goto Exit; |
| 1241 | |
| 1242 | mem = new MemoryBase(cache->getHeap(), 0, cache->size()); |
| 1243 | *pSampleRate = cache->sampleRate(); |
| 1244 | *pNumChannels = cache->channelCount(); |
| 1245 | *pFormat = cache->format(); |
| 1246 | LOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat); |
| 1247 | |
| 1248 | Exit: |
| 1249 | if (player != 0) player->reset(); |
| 1250 | ::close(fd); |
| 1251 | return mem; |
| 1252 | } |
| 1253 | |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1254 | /* |
| 1255 | * Avert your eyes, ugly hack ahead. |
| 1256 | * The following is to support music visualizations. |
| 1257 | */ |
| 1258 | |
| 1259 | static const int NUMVIZBUF = 32; |
| 1260 | static const int VIZBUFFRAMES = 1024; |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1261 | static const int BUFTIMEMSEC = NUMVIZBUF * VIZBUFFRAMES * 1000 / 44100; |
| 1262 | static const int TOTALBUFTIMEMSEC = NUMVIZBUF * BUFTIMEMSEC; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1263 | |
| 1264 | static bool gotMem = false; |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1265 | static sp<MemoryHeapBase> heap; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1266 | static sp<MemoryBase> mem[NUMVIZBUF]; |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1267 | static uint64_t endTime; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1268 | static uint64_t lastReadTime; |
| 1269 | static uint64_t lastWriteTime; |
| 1270 | static int writeIdx = 0; |
| 1271 | |
| 1272 | static void allocVizBufs() { |
| 1273 | if (!gotMem) { |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1274 | heap = new MemoryHeapBase(NUMVIZBUF * VIZBUFFRAMES * 2, 0, "snooper"); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1275 | for (int i=0;i<NUMVIZBUF;i++) { |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1276 | mem[i] = new MemoryBase(heap, VIZBUFFRAMES * 2 * i, VIZBUFFRAMES * 2); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1277 | } |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1278 | endTime = 0; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1279 | gotMem = true; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | /* |
| 1285 | * Get a buffer of audio data that is about to be played. |
| 1286 | * We don't synchronize this because in practice the writer |
| 1287 | * is ahead of the reader, and even if we did happen to catch |
| 1288 | * a buffer while it's being written, it's just a visualization, |
| 1289 | * so no harm done. |
| 1290 | */ |
| 1291 | static sp<MemoryBase> getVizBuffer() { |
| 1292 | |
| 1293 | allocVizBufs(); |
| 1294 | |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1295 | lastReadTime = uptimeMillis(); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1296 | |
| 1297 | // if there is no recent buffer (yet), just return empty handed |
| 1298 | if (lastWriteTime + TOTALBUFTIMEMSEC < lastReadTime) { |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1299 | //LOGI("@@@@ no audio data to look at yet: %d + %d < %d", (int)lastWriteTime, TOTALBUFTIMEMSEC, (int)lastReadTime); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1300 | return NULL; |
| 1301 | } |
| 1302 | |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1303 | int timedelta = endTime - lastReadTime; |
| 1304 | if (timedelta < 0) timedelta = 0; |
| 1305 | int framedelta = timedelta * 44100 / 1000; |
| 1306 | int headIdx = (writeIdx - framedelta) / VIZBUFFRAMES - 1; |
| 1307 | while (headIdx < 0) { |
| 1308 | headIdx += NUMVIZBUF; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1309 | } |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1310 | return mem[headIdx]; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1313 | // Append the data to the vizualization buffer |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1314 | static void makeVizBuffers(const char *data, int len, uint64_t time) { |
| 1315 | |
| 1316 | allocVizBufs(); |
| 1317 | |
| 1318 | uint64_t startTime = time; |
| 1319 | const int frameSize = 4; // 16 bit stereo sample is 4 bytes |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1320 | int offset = writeIdx; |
| 1321 | int maxoff = heap->getSize() / 2; // in shorts |
| 1322 | short *base = (short*)heap->getBase(); |
| 1323 | short *src = (short*)data; |
| 1324 | while (len > 0) { |
| 1325 | |
| 1326 | // Degrade quality by mixing to mono and clearing the lowest 3 bits. |
| 1327 | // This should still be good enough for a visualization |
| 1328 | base[offset++] = ((int(src[0]) + int(src[1])) >> 1) & ~0x7; |
| 1329 | src += 2; |
| 1330 | len -= frameSize; |
| 1331 | if (offset >= maxoff) { |
| 1332 | offset = 0; |
| 1333 | } |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1334 | } |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1335 | writeIdx = offset; |
| 1336 | endTime = time + (len / frameSize) / 44; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1337 | //LOGI("@@@ stored buffers from %d to %d", uint32_t(startTime), uint32_t(time)); |
| 1338 | } |
| 1339 | |
| 1340 | sp<IMemory> MediaPlayerService::snoop() |
| 1341 | { |
| 1342 | sp<MemoryBase> mem = getVizBuffer(); |
| 1343 | return mem; |
| 1344 | } |
| 1345 | |
| 1346 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1347 | #undef LOG_TAG |
| 1348 | #define LOG_TAG "AudioSink" |
| 1349 | MediaPlayerService::AudioOutput::AudioOutput() |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1350 | : mCallback(NULL), |
| 1351 | mCallbackCookie(NULL) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | mTrack = 0; |
| 1353 | mStreamType = AudioSystem::MUSIC; |
| 1354 | mLeftVolume = 1.0; |
| 1355 | mRightVolume = 1.0; |
| 1356 | mLatency = 0; |
| 1357 | mMsecsPerFrame = 0; |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1358 | mNumFramesWritten = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | setMinBufferCount(); |
| 1360 | } |
| 1361 | |
| 1362 | MediaPlayerService::AudioOutput::~AudioOutput() |
| 1363 | { |
| 1364 | close(); |
| 1365 | } |
| 1366 | |
| 1367 | void MediaPlayerService::AudioOutput::setMinBufferCount() |
| 1368 | { |
| 1369 | char value[PROPERTY_VALUE_MAX]; |
| 1370 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 1371 | mIsOnEmulator = true; |
| 1372 | mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | bool MediaPlayerService::AudioOutput::isOnEmulator() |
| 1377 | { |
| 1378 | setMinBufferCount(); |
| 1379 | return mIsOnEmulator; |
| 1380 | } |
| 1381 | |
| 1382 | int MediaPlayerService::AudioOutput::getMinBufferCount() |
| 1383 | { |
| 1384 | setMinBufferCount(); |
| 1385 | return mMinBufferCount; |
| 1386 | } |
| 1387 | |
| 1388 | ssize_t MediaPlayerService::AudioOutput::bufferSize() const |
| 1389 | { |
| 1390 | if (mTrack == 0) return NO_INIT; |
| 1391 | return mTrack->frameCount() * frameSize(); |
| 1392 | } |
| 1393 | |
| 1394 | ssize_t MediaPlayerService::AudioOutput::frameCount() const |
| 1395 | { |
| 1396 | if (mTrack == 0) return NO_INIT; |
| 1397 | return mTrack->frameCount(); |
| 1398 | } |
| 1399 | |
| 1400 | ssize_t MediaPlayerService::AudioOutput::channelCount() const |
| 1401 | { |
| 1402 | if (mTrack == 0) return NO_INIT; |
| 1403 | return mTrack->channelCount(); |
| 1404 | } |
| 1405 | |
| 1406 | ssize_t MediaPlayerService::AudioOutput::frameSize() const |
| 1407 | { |
| 1408 | if (mTrack == 0) return NO_INIT; |
| 1409 | return mTrack->frameSize(); |
| 1410 | } |
| 1411 | |
| 1412 | uint32_t MediaPlayerService::AudioOutput::latency () const |
| 1413 | { |
| 1414 | return mLatency; |
| 1415 | } |
| 1416 | |
| 1417 | float MediaPlayerService::AudioOutput::msecsPerFrame() const |
| 1418 | { |
| 1419 | return mMsecsPerFrame; |
| 1420 | } |
| 1421 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1422 | status_t MediaPlayerService::AudioOutput::open( |
| 1423 | uint32_t sampleRate, int channelCount, int format, int bufferCount, |
| 1424 | AudioCallback cb, void *cookie) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1426 | mCallback = cb; |
| 1427 | mCallbackCookie = cookie; |
| 1428 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | // Check argument "bufferCount" against the mininum buffer count |
| 1430 | if (bufferCount < mMinBufferCount) { |
| 1431 | LOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount); |
| 1432 | bufferCount = mMinBufferCount; |
| 1433 | |
| 1434 | } |
| 1435 | LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount); |
| 1436 | if (mTrack) close(); |
| 1437 | int afSampleRate; |
| 1438 | int afFrameCount; |
| 1439 | int frameCount; |
| 1440 | |
| 1441 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| 1442 | return NO_INIT; |
| 1443 | } |
| 1444 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| 1445 | return NO_INIT; |
| 1446 | } |
| 1447 | |
| 1448 | frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1449 | |
| 1450 | AudioTrack *t; |
| 1451 | if (mCallback != NULL) { |
| 1452 | t = new AudioTrack( |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1453 | mStreamType, |
| 1454 | sampleRate, |
| 1455 | format, |
| 1456 | (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO, |
| 1457 | frameCount, |
| 1458 | 0 /* flags */, |
| 1459 | CallbackWrapper, |
| 1460 | this); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1461 | } else { |
| 1462 | t = new AudioTrack( |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1463 | mStreamType, |
| 1464 | sampleRate, |
| 1465 | format, |
| 1466 | (channelCount == 2) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO, |
| 1467 | frameCount); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
| 1471 | LOGE("Unable to create audio track"); |
| 1472 | delete t; |
| 1473 | return NO_INIT; |
| 1474 | } |
| 1475 | |
| 1476 | LOGV("setVolume"); |
| 1477 | t->setVolume(mLeftVolume, mRightVolume); |
| 1478 | mMsecsPerFrame = 1.e3 / (float) sampleRate; |
Dave Sparks | 1d711f6 | 2009-12-03 10:13:32 -0800 | [diff] [blame] | 1479 | mLatency = t->latency(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | mTrack = t; |
| 1481 | return NO_ERROR; |
| 1482 | } |
| 1483 | |
| 1484 | void MediaPlayerService::AudioOutput::start() |
| 1485 | { |
| 1486 | LOGV("start"); |
| 1487 | if (mTrack) { |
| 1488 | mTrack->setVolume(mLeftVolume, mRightVolume); |
| 1489 | mTrack->start(); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1490 | mTrack->getPosition(&mNumFramesWritten); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1494 | void MediaPlayerService::AudioOutput::snoopWrite(const void* buffer, size_t size) { |
| 1495 | // Only make visualization buffers if anyone recently requested visualization data |
| 1496 | uint64_t now = uptimeMillis(); |
| 1497 | if (lastReadTime + TOTALBUFTIMEMSEC >= now) { |
| 1498 | // Based on the current play counter, the number of frames written and |
| 1499 | // the current real time we can calculate the approximate real start |
| 1500 | // time of the buffer we're about to write. |
| 1501 | uint32_t pos; |
| 1502 | mTrack->getPosition(&pos); |
| 1503 | |
| 1504 | // we're writing ahead by this many frames: |
| 1505 | int ahead = mNumFramesWritten - pos; |
| 1506 | //LOGI("@@@ written: %d, playpos: %d, latency: %d", mNumFramesWritten, pos, mTrack->latency()); |
| 1507 | // which is this many milliseconds, assuming 44100 Hz: |
| 1508 | ahead /= 44; |
| 1509 | |
| 1510 | makeVizBuffers((const char*)buffer, size, now + ahead + mTrack->latency()); |
| 1511 | lastWriteTime = now; |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size) |
| 1517 | { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1518 | LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback."); |
| 1519 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | //LOGV("write(%p, %u)", buffer, size); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1521 | if (mTrack) { |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1522 | snoopWrite(buffer, size); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1523 | ssize_t ret = mTrack->write(buffer, size); |
| 1524 | mNumFramesWritten += ret / 4; // assume 16 bit stereo |
| 1525 | return ret; |
| 1526 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | return NO_INIT; |
| 1528 | } |
| 1529 | |
| 1530 | void MediaPlayerService::AudioOutput::stop() |
| 1531 | { |
| 1532 | LOGV("stop"); |
| 1533 | if (mTrack) mTrack->stop(); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1534 | lastWriteTime = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | void MediaPlayerService::AudioOutput::flush() |
| 1538 | { |
| 1539 | LOGV("flush"); |
| 1540 | if (mTrack) mTrack->flush(); |
| 1541 | } |
| 1542 | |
| 1543 | void MediaPlayerService::AudioOutput::pause() |
| 1544 | { |
| 1545 | LOGV("pause"); |
| 1546 | if (mTrack) mTrack->pause(); |
Marco Nelissen | 6741eb8 | 2009-11-02 13:52:11 -0800 | [diff] [blame] | 1547 | lastWriteTime = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | void MediaPlayerService::AudioOutput::close() |
| 1551 | { |
| 1552 | LOGV("close"); |
| 1553 | delete mTrack; |
| 1554 | mTrack = 0; |
| 1555 | } |
| 1556 | |
| 1557 | void MediaPlayerService::AudioOutput::setVolume(float left, float right) |
| 1558 | { |
| 1559 | LOGV("setVolume(%f, %f)", left, right); |
| 1560 | mLeftVolume = left; |
| 1561 | mRightVolume = right; |
| 1562 | if (mTrack) { |
| 1563 | mTrack->setVolume(left, right); |
| 1564 | } |
| 1565 | } |
| 1566 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1567 | // static |
| 1568 | void MediaPlayerService::AudioOutput::CallbackWrapper( |
| 1569 | int event, void *cookie, void *info) { |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1570 | //LOGV("callbackwrapper"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1571 | if (event != AudioTrack::EVENT_MORE_DATA) { |
| 1572 | return; |
| 1573 | } |
| 1574 | |
| 1575 | AudioOutput *me = (AudioOutput *)cookie; |
| 1576 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
| 1577 | |
| 1578 | (*me->mCallback)( |
| 1579 | me, buffer->raw, buffer->size, me->mCallbackCookie); |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1580 | me->snoopWrite(buffer->raw, buffer->size); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | #undef LOG_TAG |
| 1584 | #define LOG_TAG "AudioCache" |
| 1585 | MediaPlayerService::AudioCache::AudioCache(const char* name) : |
| 1586 | mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0), |
| 1587 | mError(NO_ERROR), mCommandComplete(false) |
| 1588 | { |
| 1589 | // create ashmem heap |
| 1590 | mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name); |
| 1591 | } |
| 1592 | |
| 1593 | uint32_t MediaPlayerService::AudioCache::latency () const |
| 1594 | { |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | float MediaPlayerService::AudioCache::msecsPerFrame() const |
| 1599 | { |
| 1600 | return mMsecsPerFrame; |
| 1601 | } |
| 1602 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1603 | status_t MediaPlayerService::AudioCache::open( |
| 1604 | uint32_t sampleRate, int channelCount, int format, int bufferCount, |
| 1605 | AudioCallback cb, void *cookie) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | { |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1607 | LOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1608 | if (cb != NULL) { |
| 1609 | return UNKNOWN_ERROR; // TODO: implement this. |
| 1610 | } |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1611 | if (mHeap->getHeapID() < 0) { |
| 1612 | return NO_INIT; |
| 1613 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1614 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | mSampleRate = sampleRate; |
| 1616 | mChannelCount = (uint16_t)channelCount; |
| 1617 | mFormat = (uint16_t)format; |
| 1618 | mMsecsPerFrame = 1.e3 / (float) sampleRate; |
| 1619 | return NO_ERROR; |
| 1620 | } |
| 1621 | |
| 1622 | ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size) |
| 1623 | { |
| 1624 | LOGV("write(%p, %u)", buffer, size); |
| 1625 | if ((buffer == 0) || (size == 0)) return size; |
| 1626 | |
| 1627 | uint8_t* p = static_cast<uint8_t*>(mHeap->getBase()); |
| 1628 | if (p == NULL) return NO_INIT; |
| 1629 | p += mSize; |
| 1630 | LOGV("memcpy(%p, %p, %u)", p, buffer, size); |
| 1631 | if (mSize + size > mHeap->getSize()) { |
| 1632 | LOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize()); |
| 1633 | size = mHeap->getSize() - mSize; |
| 1634 | } |
| 1635 | memcpy(p, buffer, size); |
| 1636 | mSize += size; |
| 1637 | return size; |
| 1638 | } |
| 1639 | |
| 1640 | // call with lock held |
| 1641 | status_t MediaPlayerService::AudioCache::wait() |
| 1642 | { |
| 1643 | Mutex::Autolock lock(mLock); |
| 1644 | if (!mCommandComplete) { |
| 1645 | mSignal.wait(mLock); |
| 1646 | } |
| 1647 | mCommandComplete = false; |
| 1648 | |
| 1649 | if (mError == NO_ERROR) { |
| 1650 | LOGV("wait - success"); |
| 1651 | } else { |
| 1652 | LOGV("wait - error"); |
| 1653 | } |
| 1654 | return mError; |
| 1655 | } |
| 1656 | |
| 1657 | void MediaPlayerService::AudioCache::notify(void* cookie, int msg, int ext1, int ext2) |
| 1658 | { |
| 1659 | LOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2); |
| 1660 | AudioCache* p = static_cast<AudioCache*>(cookie); |
| 1661 | |
| 1662 | // ignore buffering messages |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1663 | switch (msg) |
| 1664 | { |
| 1665 | case MEDIA_ERROR: |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | LOGE("Error %d, %d occurred", ext1, ext2); |
| 1667 | p->mError = ext1; |
Dave Sparks | 8eb8011 | 2009-12-09 20:20:26 -0800 | [diff] [blame] | 1668 | break; |
| 1669 | case MEDIA_PREPARED: |
| 1670 | LOGV("prepared"); |
| 1671 | break; |
| 1672 | case MEDIA_PLAYBACK_COMPLETE: |
| 1673 | LOGV("playback complete"); |
| 1674 | break; |
| 1675 | default: |
| 1676 | LOGV("ignored"); |
| 1677 | return; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // wake up thread |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | p->mCommandComplete = true; |
| 1682 | p->mSignal.signal(); |
| 1683 | } |
| 1684 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1685 | } // namespace android |