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