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