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