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