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