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