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