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