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