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