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