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