The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | // Proxy for media player implementations |
| 19 | |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #define LOG_TAG "MediaPlayerService" |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 26 | #include <sys/time.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <dirent.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <string.h> |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 31 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <cutils/atomic.h> |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 33 | #include <cutils/properties.h> // for property_get |
Mathias Agopian | 6f74b0c | 2009-06-03 17:32:49 -0700 | [diff] [blame] | 34 | |
| 35 | #include <utils/misc.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 37 | #include <binder/IPCThreadState.h> |
| 38 | #include <binder/IServiceManager.h> |
| 39 | #include <binder/MemoryHeapBase.h> |
| 40 | #include <binder/MemoryBase.h> |
Mathias Agopian | b1e7cd1 | 2013-02-14 17:11:27 -0800 | [diff] [blame] | 41 | #include <gui/Surface.h> |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 42 | #include <utils/Errors.h> // for status_t |
| 43 | #include <utils/String8.h> |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 44 | #include <utils/SystemClock.h> |
Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 45 | #include <utils/Timers.h> |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 46 | #include <utils/Vector.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 48 | #include <media/AudioPolicyHelper.h> |
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> |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 58 | #include <media/stagefright/InterfaceUtils.h> |
Lajos Molnar | 1381d4b | 2014-08-07 15:18:35 -0700 | [diff] [blame] | 59 | #include <media/stagefright/MediaCodecList.h> |
Eric Laurent | 9cb839a | 2011-09-27 09:48:56 -0700 | [diff] [blame] | 60 | #include <media/stagefright/MediaErrors.h> |
Marco Nelissen | 83b0fd9 | 2015-09-16 13:48:07 -0700 | [diff] [blame] | 61 | #include <media/stagefright/Utils.h> |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 62 | #include <media/stagefright/foundation/ADebug.h> |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 63 | #include <media/stagefright/foundation/ALooperRoster.h> |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 64 | #include <media/stagefright/SurfaceUtils.h> |
Ruben Brunk | 99e6971 | 2015-05-26 17:25:07 -0700 | [diff] [blame] | 65 | #include <mediautils/BatteryNotifier.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 67 | #include <memunreachable/memunreachable.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 68 | #include <system/audio.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 69 | |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 70 | #include <private/android_filesystem_config.h> |
| 71 | |
James Dong | 559bf28 | 2012-03-28 10:29:14 -0700 | [diff] [blame] | 72 | #include "ActivityManager.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | #include "MediaRecorderClient.h" |
| 74 | #include "MediaPlayerService.h" |
| 75 | #include "MetadataRetrieverClient.h" |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 76 | #include "MediaPlayerFactory.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 78 | #include "TestPlayerStub.h" |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 79 | #include "nuplayer/NuPlayerDriver.h" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 80 | |
Andreas Huber | b7319a7 | 2013-05-29 14:20:52 -0700 | [diff] [blame] | 81 | #include "HTTPBase.h" |
Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 82 | |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 83 | static const int kDumpLockRetries = 50; |
| 84 | static const int kDumpLockSleepUs = 20000; |
| 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; |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 93 | using android::media::VolumeShaper; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 94 | |
| 95 | // Max number of entries in the filter. |
| 96 | const int kMaxFilterSize = 64; // I pulled that out of thin air. |
| 97 | |
Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 98 | const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup. |
| 99 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 100 | // FIXME: Move all the metadata related function in the Metadata.cpp |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 101 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 102 | |
| 103 | // Unmarshall a filter from a Parcel. |
| 104 | // Filter format in a parcel: |
| 105 | // |
| 106 | // 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 |
| 107 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 108 | // | number of entries (n) | |
| 109 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 110 | // | metadata type 1 | |
| 111 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 112 | // | metadata type 2 | |
| 113 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 114 | // .... |
| 115 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 116 | // | metadata type n | |
| 117 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 118 | // |
| 119 | // @param p Parcel that should start with a filter. |
| 120 | // @param[out] filter On exit contains the list of metadata type to be |
| 121 | // filtered. |
| 122 | // @param[out] status On exit contains the status code to be returned. |
| 123 | // @return true if the parcel starts with a valid filter. |
| 124 | bool unmarshallFilter(const Parcel& p, |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 125 | Metadata::Filter *filter, |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 126 | status_t *status) |
| 127 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 128 | int32_t val; |
| 129 | if (p.readInt32(&val) != OK) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 130 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 131 | ALOGE("Failed to read filter's length"); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 132 | *status = NOT_ENOUGH_DATA; |
| 133 | return false; |
| 134 | } |
| 135 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 136 | if( val > kMaxFilterSize || val < 0) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 137 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 138 | ALOGE("Invalid filter len %d", val); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 139 | *status = BAD_VALUE; |
| 140 | return false; |
| 141 | } |
| 142 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 143 | const size_t num = val; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 144 | |
| 145 | filter->clear(); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 146 | filter->setCapacity(num); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 147 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 148 | size_t size = num * sizeof(Metadata::Type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 149 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 150 | |
| 151 | if (p.dataAvail() < size) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 152 | { |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 153 | ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail()); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 154 | *status = NOT_ENOUGH_DATA; |
| 155 | return false; |
| 156 | } |
| 157 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 158 | const Metadata::Type *data = |
| 159 | static_cast<const Metadata::Type*>(p.readInplace(size)); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 160 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 161 | if (NULL == data) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 162 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 163 | ALOGE("Filter had no data"); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 164 | *status = BAD_VALUE; |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | // TODO: The stl impl of vector would be more efficient here |
| 169 | // because it degenerates into a memcpy on pod types. Try to |
| 170 | // replace later or use stl::set. |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 171 | for (size_t i = 0; i < num; ++i) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 172 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 173 | filter->add(*data); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 174 | ++data; |
| 175 | } |
| 176 | *status = OK; |
| 177 | return true; |
| 178 | } |
| 179 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 180 | // @param filter Of metadata type. |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 181 | // @param val To be searched. |
| 182 | // @return true if a match was found. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 183 | bool findMetadata(const Metadata::Filter& filter, const int32_t val) |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 184 | { |
| 185 | // Deal with empty and ANY right away |
| 186 | if (filter.isEmpty()) return false; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 187 | if (filter[0] == Metadata::kAny) return true; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 188 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 189 | return filter.indexOf(val) >= 0; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // anonymous namespace |
| 193 | |
| 194 | |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 195 | namespace { |
| 196 | using android::Parcel; |
| 197 | using android::String16; |
| 198 | |
| 199 | // marshalling tag indicating flattened utf16 tags |
| 200 | // keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java |
| 201 | const int32_t kAudioAttributesMarshallTagFlattenTags = 1; |
| 202 | |
| 203 | // Audio attributes format in a parcel: |
| 204 | // |
| 205 | // 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 |
| 206 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 207 | // | usage | |
| 208 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 209 | // | content_type | |
| 210 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Hyejin Kim | 4f418f9 | 2014-09-05 15:50:03 +0900 | [diff] [blame] | 211 | // | source | |
| 212 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 213 | // | flags | |
| 214 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 215 | // | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found |
| 216 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 217 | // | flattened tags in UTF16 | |
| 218 | // | ... | |
| 219 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 220 | // |
| 221 | // @param p Parcel that contains audio attributes. |
| 222 | // @param[out] attributes On exit points to an initialized audio_attributes_t structure |
| 223 | // @param[out] status On exit contains the status code to be returned. |
| 224 | void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes) |
| 225 | { |
| 226 | attributes->usage = (audio_usage_t) parcel.readInt32(); |
| 227 | attributes->content_type = (audio_content_type_t) parcel.readInt32(); |
Hyejin Kim | 4f418f9 | 2014-09-05 15:50:03 +0900 | [diff] [blame] | 228 | attributes->source = (audio_source_t) parcel.readInt32(); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 229 | attributes->flags = (audio_flags_mask_t) parcel.readInt32(); |
| 230 | const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags); |
| 231 | if (hasFlattenedTag) { |
| 232 | // the tags are UTF16, convert to UTF8 |
| 233 | String16 tags = parcel.readString16(); |
| 234 | ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size()); |
| 235 | if (realTagSize <= 0) { |
| 236 | strcpy(attributes->tags, ""); |
| 237 | } else { |
| 238 | // copy the flattened string into the attributes as the destination for the conversion: |
| 239 | // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it |
| 240 | size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ? |
| 241 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize; |
Sergio Giro | 1d3f427 | 2016-06-28 18:24:52 +0100 | [diff] [blame] | 242 | utf16_to_utf8(tags.string(), tagSize, attributes->tags, |
| 243 | sizeof(attributes->tags) / sizeof(attributes->tags[0])); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 244 | } |
| 245 | } else { |
| 246 | ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values"); |
| 247 | strcpy(attributes->tags, ""); |
| 248 | } |
| 249 | } |
| 250 | } // anonymous namespace |
| 251 | |
| 252 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | namespace android { |
| 254 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 255 | extern ALooperRoster gLooperRoster; |
| 256 | |
| 257 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 258 | static bool checkPermission(const char* permissionString) { |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 259 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 260 | bool ok = checkCallingPermission(String16(permissionString)); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 261 | if (!ok) ALOGE("Request requires %s", permissionString); |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 262 | return ok; |
| 263 | } |
| 264 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | // 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] | 266 | /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4; |
| 267 | /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false; |
| 268 | |
| 269 | void MediaPlayerService::instantiate() { |
| 270 | defaultServiceManager()->addService( |
| 271 | String16("media.player"), new MediaPlayerService()); |
| 272 | } |
| 273 | |
| 274 | MediaPlayerService::MediaPlayerService() |
| 275 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 276 | ALOGV("MediaPlayerService created"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | mNextConnId = 1; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 278 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 279 | MediaPlayerFactory::registerBuiltinFactories(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | MediaPlayerService::~MediaPlayerService() |
| 283 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 284 | ALOGV("MediaPlayerService destroyed"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 287 | sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | { |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 289 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 290 | sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName); |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 291 | wp<MediaRecorderClient> w = recorder; |
| 292 | Mutex::Autolock lock(mLock); |
| 293 | mMediaRecorderClients.add(w); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 294 | 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] | 295 | return recorder; |
| 296 | } |
| 297 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 298 | void MediaPlayerService::removeMediaRecorderClient(const wp<MediaRecorderClient>& client) |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 299 | { |
| 300 | Mutex::Autolock lock(mLock); |
| 301 | mMediaRecorderClients.remove(client); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 302 | ALOGV("Delete media recorder client"); |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 305 | sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | { |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 307 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 309 | ALOGV("Create new media retriever from pid %d", pid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | return retriever; |
| 311 | } |
| 312 | |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 313 | sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 314 | audio_session_t audioSessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | { |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 316 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | int32_t connId = android_atomic_inc(&mNextConnId); |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 318 | |
| 319 | sp<Client> c = new Client( |
| 320 | this, pid, connId, client, audioSessionId, |
| 321 | IPCThreadState::self()->getCallingUid()); |
| 322 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 323 | ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid, |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 324 | IPCThreadState::self()->getCallingUid()); |
| 325 | |
| 326 | wp<Client> w = c; |
| 327 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | Mutex::Autolock lock(mLock); |
| 329 | mClients.add(w); |
| 330 | } |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 331 | return c; |
| 332 | } |
| 333 | |
Lajos Molnar | 1381d4b | 2014-08-07 15:18:35 -0700 | [diff] [blame] | 334 | sp<IMediaCodecList> MediaPlayerService::getCodecList() const { |
| 335 | return MediaCodecList::getLocalInstance(); |
| 336 | } |
| 337 | |
Jeff Brown | 2013a54 | 2012-09-04 21:38:42 -0700 | [diff] [blame] | 338 | sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay( |
Chong Zhang | d0a98fa | 2017-09-14 17:43:10 -0700 | [diff] [blame] | 339 | const String16 &/*opPackageName*/, |
| 340 | const sp<IRemoteDisplayClient>& /*client*/, |
| 341 | const String8& /*iface*/) { |
| 342 | ALOGE("listenForRemoteDisplay is no longer supported!"); |
Jeff Brown | aba33d5 | 2012-09-07 17:38:58 -0700 | [diff] [blame] | 343 | |
Chong Zhang | d0a98fa | 2017-09-14 17:43:10 -0700 | [diff] [blame] | 344 | return NULL; |
Jeff Brown | 2013a54 | 2012-09-04 21:38:42 -0700 | [diff] [blame] | 345 | } |
| 346 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const |
| 348 | { |
| 349 | const size_t SIZE = 256; |
| 350 | char buffer[SIZE]; |
| 351 | String8 result; |
| 352 | |
| 353 | result.append(" AudioOutput\n"); |
| 354 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", |
| 355 | mStreamType, mLeftVolume, mRightVolume); |
| 356 | result.append(buffer); |
| 357 | snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n", |
Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 358 | mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | result.append(buffer); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 360 | snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n", |
| 361 | mAuxEffectId, mSendLevel); |
| 362 | result.append(buffer); |
| 363 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | ::write(fd, result.string(), result.size()); |
| 365 | if (mTrack != 0) { |
| 366 | mTrack->dump(fd, args); |
| 367 | } |
| 368 | return NO_ERROR; |
| 369 | } |
| 370 | |
Lajos Molnar | 6d339f1 | 2015-04-17 16:15:53 -0700 | [diff] [blame] | 371 | 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] | 372 | { |
| 373 | const size_t SIZE = 256; |
| 374 | char buffer[SIZE]; |
| 375 | String8 result; |
| 376 | result.append(" Client\n"); |
| 377 | snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n", |
| 378 | mPid, mConnId, mStatus, mLoop?"true": "false"); |
| 379 | result.append(buffer); |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 380 | |
| 381 | sp<MediaPlayerBase> p; |
| 382 | sp<AudioOutput> audioOutput; |
| 383 | bool locked = false; |
| 384 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 385 | if (mLock.tryLock() == NO_ERROR) { |
| 386 | locked = true; |
| 387 | break; |
| 388 | } |
| 389 | usleep(kDumpLockSleepUs); |
Andreas Huber | a0b1d4b | 2011-06-07 15:52:25 -0700 | [diff] [blame] | 390 | } |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 391 | |
| 392 | if (locked) { |
| 393 | p = mPlayer; |
| 394 | audioOutput = mAudioOutput; |
| 395 | mLock.unlock(); |
| 396 | } else { |
| 397 | result.append(" lock is taken, no dump from player and audio output\n"); |
| 398 | } |
| 399 | write(fd, result.string(), result.size()); |
| 400 | |
| 401 | if (p != NULL) { |
| 402 | p->dump(fd, args); |
| 403 | } |
| 404 | if (audioOutput != 0) { |
| 405 | audioOutput->dump(fd, args); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
| 407 | write(fd, "\n", 1); |
| 408 | return NO_ERROR; |
| 409 | } |
| 410 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 411 | /** |
| 412 | * The only arguments this understands right now are -c, -von and -voff, |
| 413 | * which are parsed by ALooperRoster::dump() |
| 414 | */ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | status_t MediaPlayerService::dump(int fd, const Vector<String16>& args) |
| 416 | { |
| 417 | const size_t SIZE = 256; |
| 418 | char buffer[SIZE]; |
| 419 | String8 result; |
Ravi Kumar Alamanda | 330c8e3 | 2014-12-22 10:05:29 +0530 | [diff] [blame] | 420 | SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction. |
| 421 | SortedVector< sp<MediaRecorderClient> > mediaRecorderClients; |
| 422 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 424 | snprintf(buffer, SIZE, "Permission Denial: " |
| 425 | "can't dump MediaPlayerService from pid=%d, uid=%d\n", |
| 426 | IPCThreadState::self()->getCallingPid(), |
| 427 | IPCThreadState::self()->getCallingUid()); |
| 428 | result.append(buffer); |
| 429 | } else { |
| 430 | Mutex::Autolock lock(mLock); |
| 431 | for (int i = 0, n = mClients.size(); i < n; ++i) { |
| 432 | sp<Client> c = mClients[i].promote(); |
| 433 | if (c != 0) c->dump(fd, args); |
Ravi Kumar Alamanda | 330c8e3 | 2014-12-22 10:05:29 +0530 | [diff] [blame] | 434 | clients.add(c); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 436 | if (mMediaRecorderClients.size() == 0) { |
| 437 | result.append(" No media recorder client\n\n"); |
| 438 | } else { |
| 439 | for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) { |
| 440 | sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote(); |
James Dong | e579e28 | 2011-10-18 22:29:20 -0700 | [diff] [blame] | 441 | if (c != 0) { |
| 442 | snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid); |
| 443 | result.append(buffer); |
| 444 | write(fd, result.string(), result.size()); |
| 445 | result = "\n"; |
| 446 | c->dump(fd, args); |
Ravi Kumar Alamanda | 330c8e3 | 2014-12-22 10:05:29 +0530 | [diff] [blame] | 447 | mediaRecorderClients.add(c); |
James Dong | e579e28 | 2011-10-18 22:29:20 -0700 | [diff] [blame] | 448 | } |
James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 449 | } |
Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 450 | } |
| 451 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | result.append(" Files opened and/or mapped:\n"); |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 453 | snprintf(buffer, SIZE, "/proc/%d/maps", getpid()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | FILE *f = fopen(buffer, "r"); |
| 455 | if (f) { |
| 456 | while (!feof(f)) { |
| 457 | fgets(buffer, SIZE, f); |
Marco Nelissen | 73ac1ee | 2012-05-07 15:36:32 -0700 | [diff] [blame] | 458 | if (strstr(buffer, " /storage/") || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | strstr(buffer, " /system/sounds/") || |
Dave Sparks | 02fa834 | 2010-09-27 16:55:18 -0700 | [diff] [blame] | 460 | strstr(buffer, " /data/") || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | strstr(buffer, " /system/media/")) { |
| 462 | result.append(" "); |
| 463 | result.append(buffer); |
| 464 | } |
| 465 | } |
| 466 | fclose(f); |
| 467 | } else { |
| 468 | result.append("couldn't open "); |
| 469 | result.append(buffer); |
| 470 | result.append("\n"); |
| 471 | } |
| 472 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 473 | snprintf(buffer, SIZE, "/proc/%d/fd", getpid()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | DIR *d = opendir(buffer); |
| 475 | if (d) { |
| 476 | struct dirent *ent; |
| 477 | while((ent = readdir(d)) != NULL) { |
| 478 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 479 | 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] | 480 | struct stat s; |
| 481 | if (lstat(buffer, &s) == 0) { |
| 482 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 483 | char linkto[256]; |
| 484 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 485 | if(len > 0) { |
| 486 | if(len > 255) { |
| 487 | linkto[252] = '.'; |
| 488 | linkto[253] = '.'; |
| 489 | linkto[254] = '.'; |
| 490 | linkto[255] = 0; |
| 491 | } else { |
| 492 | linkto[len] = 0; |
| 493 | } |
Marco Nelissen | 73ac1ee | 2012-05-07 15:36:32 -0700 | [diff] [blame] | 494 | if (strstr(linkto, "/storage/") == linkto || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | strstr(linkto, "/system/sounds/") == linkto || |
Dave Sparks | 02fa834 | 2010-09-27 16:55:18 -0700 | [diff] [blame] | 496 | strstr(linkto, "/data/") == linkto || |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | strstr(linkto, "/system/media/") == linkto) { |
| 498 | result.append(" "); |
| 499 | result.append(buffer); |
| 500 | result.append(" -> "); |
| 501 | result.append(linkto); |
| 502 | result.append("\n"); |
| 503 | } |
| 504 | } |
| 505 | } else { |
| 506 | result.append(" unexpected type for "); |
| 507 | result.append(buffer); |
| 508 | result.append("\n"); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | closedir(d); |
| 514 | } else { |
| 515 | result.append("couldn't open "); |
| 516 | result.append(buffer); |
| 517 | result.append("\n"); |
| 518 | } |
| 519 | |
Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 520 | gLooperRoster.dump(fd, args); |
| 521 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | bool dumpMem = false; |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 523 | bool unreachableMemory = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | for (size_t i = 0; i < args.size(); i++) { |
| 525 | if (args[i] == String16("-m")) { |
| 526 | dumpMem = true; |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 527 | } else if (args[i] == String16("--unreachable")) { |
| 528 | unreachableMemory = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | if (dumpMem) { |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 532 | result.append("\nDumping memory:\n"); |
| 533 | std::string s = dumpMemoryAddresses(100 /* limit */); |
| 534 | result.append(s.c_str(), s.size()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | } |
Andy Hung | 5354129 | 2016-04-13 16:48:51 -0700 | [diff] [blame] | 536 | if (unreachableMemory) { |
| 537 | result.append("\nDumping unreachable memory:\n"); |
| 538 | // TODO - should limit be an argument parameter? |
| 539 | std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */); |
| 540 | result.append(s.c_str(), s.size()); |
| 541 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } |
| 543 | write(fd, result.string(), result.size()); |
| 544 | return NO_ERROR; |
| 545 | } |
| 546 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 547 | void MediaPlayerService::removeClient(const wp<Client>& client) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | { |
| 549 | Mutex::Autolock lock(mLock); |
| 550 | mClients.remove(client); |
| 551 | } |
| 552 | |
Robert Shih | ee0a0e3 | 2016-08-16 16:50:54 -0700 | [diff] [blame] | 553 | bool MediaPlayerService::hasClient(wp<Client> client) |
| 554 | { |
| 555 | Mutex::Autolock lock(mLock); |
| 556 | return mClients.indexOf(client) != NAME_NOT_FOUND; |
| 557 | } |
| 558 | |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 559 | MediaPlayerService::Client::Client( |
| 560 | const sp<MediaPlayerService>& service, pid_t pid, |
| 561 | int32_t connId, const sp<IMediaPlayerClient>& client, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 562 | audio_session_t audioSessionId, uid_t uid) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 564 | ALOGV("Client(%d) constructor", connId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | mPid = pid; |
| 566 | mConnId = connId; |
| 567 | mService = service; |
| 568 | mClient = client; |
| 569 | mLoop = false; |
| 570 | mStatus = NO_INIT; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 571 | mAudioSessionId = audioSessionId; |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 572 | mUid = uid; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 573 | mRetransmitEndpointValid = false; |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 574 | mAudioAttributes = NULL; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 575 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 577 | ALOGD("create Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | mAntagonizer = new Antagonizer(notify, this); |
| 579 | #endif |
| 580 | } |
| 581 | |
| 582 | MediaPlayerService::Client::~Client() |
| 583 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 584 | ALOGV("Client(%d) destructor pid = %d", mConnId, mPid); |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 585 | mAudioOutput.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | wp<Client> client(this); |
| 587 | disconnect(); |
| 588 | mService->removeClient(client); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 589 | if (mAudioAttributes != NULL) { |
| 590 | free(mAudioAttributes); |
| 591 | } |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 592 | clearDeathNotifiers_l(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void MediaPlayerService::Client::disconnect() |
| 596 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 597 | ALOGV("disconnect(%d) from pid %d", mConnId, mPid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | // grab local reference and clear main reference to prevent future |
| 599 | // access to object |
| 600 | sp<MediaPlayerBase> p; |
| 601 | { |
| 602 | Mutex::Autolock l(mLock); |
| 603 | p = mPlayer; |
beanz | dcfefde | 2012-11-05 09:51:43 +0800 | [diff] [blame] | 604 | mClient.clear(); |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 605 | mPlayer.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 607 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | // clear the notification to prevent callbacks to dead client |
| 609 | // and reset the player. We assume the player will serialize |
| 610 | // access to itself if necessary. |
| 611 | if (p != 0) { |
| 612 | p->setNotifyCallback(0, 0); |
| 613 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 614 | ALOGD("kill Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | mAntagonizer->kill(); |
| 616 | #endif |
| 617 | p->reset(); |
| 618 | } |
| 619 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 620 | { |
| 621 | Mutex::Autolock l(mLock); |
| 622 | disconnectNativeWindow_l(); |
| 623 | } |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | IPCThreadState::self()->flushCommands(); |
| 626 | } |
| 627 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType) |
| 629 | { |
| 630 | // determine if we have the right player type |
Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 631 | sp<MediaPlayerBase> p = getPlayer(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | if ((p != NULL) && (p->playerType() != playerType)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 633 | ALOGV("delete player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | p.clear(); |
| 635 | } |
| 636 | if (p == NULL) { |
Ronghua Wu | 68845c1 | 2015-07-21 09:50:48 -0700 | [diff] [blame] | 637 | p = MediaPlayerFactory::createPlayer(playerType, this, notify, mPid); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 639 | |
Jason Simmons | db29e52 | 2011-08-12 13:46:55 -0700 | [diff] [blame] | 640 | if (p != NULL) { |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 641 | p->setUID(mUid); |
Jason Simmons | db29e52 | 2011-08-12 13:46:55 -0700 | [diff] [blame] | 642 | } |
Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 643 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | return p; |
| 645 | } |
| 646 | |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 647 | MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier( |
| 648 | const sp<IBinder>& service, |
| 649 | const sp<MediaPlayerBase>& listener, |
| 650 | int which) { |
| 651 | mService = service; |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 652 | mOmx = nullptr; |
| 653 | mListener = listener; |
| 654 | mWhich = which; |
| 655 | } |
| 656 | |
| 657 | MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier( |
| 658 | const sp<IOmx>& omx, |
| 659 | const sp<MediaPlayerBase>& listener, |
| 660 | int which) { |
| 661 | mService = nullptr; |
| 662 | mOmx = omx; |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 663 | mListener = listener; |
| 664 | mWhich = which; |
| 665 | } |
| 666 | |
| 667 | MediaPlayerService::Client::ServiceDeathNotifier::~ServiceDeathNotifier() { |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void MediaPlayerService::Client::ServiceDeathNotifier::binderDied(const wp<IBinder>& /*who*/) { |
| 671 | sp<MediaPlayerBase> listener = mListener.promote(); |
| 672 | if (listener != NULL) { |
| 673 | listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich); |
| 674 | } else { |
| 675 | ALOGW("listener for process %d death is gone", mWhich); |
| 676 | } |
| 677 | } |
| 678 | |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 679 | void MediaPlayerService::Client::ServiceDeathNotifier::serviceDied( |
| 680 | uint64_t /* cookie */, |
| 681 | const wp<::android::hidl::base::V1_0::IBase>& /* who */) { |
| 682 | sp<MediaPlayerBase> listener = mListener.promote(); |
| 683 | if (listener != NULL) { |
| 684 | listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich); |
| 685 | } else { |
| 686 | ALOGW("listener for process %d death is gone", mWhich); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | void MediaPlayerService::Client::ServiceDeathNotifier::unlinkToDeath() { |
| 691 | if (mService != nullptr) { |
| 692 | mService->unlinkToDeath(this); |
| 693 | mService = nullptr; |
| 694 | } else if (mOmx != nullptr) { |
| 695 | mOmx->unlinkToDeath(this); |
| 696 | mOmx = nullptr; |
| 697 | } |
| 698 | } |
| 699 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 700 | void MediaPlayerService::Client::clearDeathNotifiers_l() { |
Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 701 | if (mExtractorDeathListener != nullptr) { |
| 702 | mExtractorDeathListener->unlinkToDeath(); |
| 703 | mExtractorDeathListener = nullptr; |
| 704 | } |
| 705 | if (mCodecDeathListener != nullptr) { |
| 706 | mCodecDeathListener->unlinkToDeath(); |
| 707 | mCodecDeathListener = nullptr; |
| 708 | } |
| 709 | } |
| 710 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 711 | sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre( |
| 712 | player_type playerType) |
| 713 | { |
| 714 | ALOGV("player type = %d", playerType); |
| 715 | |
| 716 | // create the right type of player |
| 717 | sp<MediaPlayerBase> p = createPlayer(playerType); |
| 718 | if (p == NULL) { |
| 719 | return p; |
| 720 | } |
| 721 | |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 722 | sp<IServiceManager> sm = defaultServiceManager(); |
| 723 | sp<IBinder> binder = sm->getService(String16("media.extractor")); |
Marco Nelissen | 1b5a7ee | 2016-09-30 13:54:30 -0700 | [diff] [blame] | 724 | if (binder == NULL) { |
| 725 | ALOGE("extractor service not available"); |
| 726 | return NULL; |
| 727 | } |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 728 | sp<ServiceDeathNotifier> extractorDeathListener = |
| 729 | new ServiceDeathNotifier(binder, p, MEDIAEXTRACTOR_PROCESS_DEATH); |
| 730 | binder->linkToDeath(extractorDeathListener); |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 731 | |
Pawin Vongmasa | 620e466 | 2017-10-27 00:12:47 -0700 | [diff] [blame^] | 732 | sp<IOmx> omx = IOmx::getService(); |
| 733 | if (omx == nullptr) { |
| 734 | ALOGE("IOmx service is not available"); |
| 735 | return NULL; |
Marco Nelissen | 1b5a7ee | 2016-09-30 13:54:30 -0700 | [diff] [blame] | 736 | } |
Pawin Vongmasa | 620e466 | 2017-10-27 00:12:47 -0700 | [diff] [blame^] | 737 | sp<ServiceDeathNotifier> codecDeathListener = |
| 738 | new ServiceDeathNotifier(omx, p, MEDIACODEC_PROCESS_DEATH); |
| 739 | omx->linkToDeath(codecDeathListener, 0); |
Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 740 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 741 | Mutex::Autolock lock(mLock); |
| 742 | |
| 743 | clearDeathNotifiers_l(); |
| 744 | mExtractorDeathListener = extractorDeathListener; |
| 745 | mCodecDeathListener = codecDeathListener; |
| 746 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 747 | if (!p->hardwareOutput()) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 748 | mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(), |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 749 | mPid, mAudioAttributes); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 750 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 751 | } |
| 752 | |
| 753 | return p; |
| 754 | } |
| 755 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 756 | status_t MediaPlayerService::Client::setDataSource_post( |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 757 | const sp<MediaPlayerBase>& p, |
| 758 | status_t status) |
| 759 | { |
| 760 | ALOGV(" setDataSource"); |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 761 | if (status != OK) { |
| 762 | ALOGE(" error: %d", status); |
| 763 | return status; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // Set the re-transmission endpoint if one was chosen. |
| 767 | if (mRetransmitEndpointValid) { |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 768 | status = p->setRetransmitEndpoint(&mRetransmitEndpoint); |
| 769 | if (status != NO_ERROR) { |
| 770 | ALOGE("setRetransmitEndpoint error: %d", status); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 774 | if (status == OK) { |
| 775 | Mutex::Autolock lock(mLock); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 776 | mPlayer = p; |
| 777 | } |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 778 | return status; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 781 | status_t MediaPlayerService::Client::setDataSource( |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 782 | const sp<IMediaHTTPService> &httpService, |
| 783 | const char *url, |
| 784 | const KeyedVector<String8, String8> *headers) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 786 | ALOGV("setDataSource(%s)", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | if (url == NULL) |
| 788 | return UNKNOWN_ERROR; |
| 789 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 790 | if ((strncmp(url, "http://", 7) == 0) || |
| 791 | (strncmp(url, "https://", 8) == 0) || |
| 792 | (strncmp(url, "rtsp://", 7) == 0)) { |
| 793 | if (!checkPermission("android.permission.INTERNET")) { |
| 794 | return PERMISSION_DENIED; |
| 795 | } |
| 796 | } |
| 797 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | if (strncmp(url, "content://", 10) == 0) { |
| 799 | // get a filedescriptor for the content Uri and |
| 800 | // pass it to the setDataSource(fd) method |
| 801 | |
| 802 | String16 url16(url); |
| 803 | int fd = android::openContentProviderFile(url16); |
| 804 | if (fd < 0) |
| 805 | { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 806 | ALOGE("Couldn't open fd for %s", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | return UNKNOWN_ERROR; |
| 808 | } |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 809 | status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | close(fd); |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 811 | return mStatus = status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | } else { |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 813 | player_type playerType = MediaPlayerFactory::getPlayerType(this, url); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 814 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 815 | if (p == NULL) { |
| 816 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 819 | return mStatus = |
| 820 | setDataSource_post( |
| 821 | p, p->setDataSource(httpService, url, headers)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length) |
| 826 | { |
Marco Nelissen | 83b0fd9 | 2015-09-16 13:48:07 -0700 | [diff] [blame] | 827 | ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld", |
| 828 | fd, nameForFd(fd).c_str(), (long long) offset, (long long) length); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | struct stat sb; |
| 830 | int ret = fstat(fd, &sb); |
| 831 | if (ret != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 832 | 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] | 833 | return UNKNOWN_ERROR; |
| 834 | } |
| 835 | |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 836 | ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 837 | ALOGV("st_mode = %u", sb.st_mode); |
Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 838 | ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid)); |
| 839 | ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid)); |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 840 | ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | |
| 842 | if (offset >= sb.st_size) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 843 | ALOGE("offset error"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | return UNKNOWN_ERROR; |
| 845 | } |
| 846 | if (offset + length > sb.st_size) { |
| 847 | length = sb.st_size - offset; |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 848 | ALOGV("calculated length = %lld", (long long)length); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | } |
| 850 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 851 | player_type playerType = MediaPlayerFactory::getPlayerType(this, |
| 852 | fd, |
| 853 | offset, |
| 854 | length); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 855 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 856 | if (p == NULL) { |
| 857 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | // now set data source |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 861 | return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length)); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 864 | status_t MediaPlayerService::Client::setDataSource( |
| 865 | const sp<IStreamSource> &source) { |
| 866 | // create the right type of player |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 867 | player_type playerType = MediaPlayerFactory::getPlayerType(this, source); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 868 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 869 | if (p == NULL) { |
| 870 | return NO_INIT; |
| 871 | } |
| 872 | |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 873 | // now set data source |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 874 | return mStatus = setDataSource_post(p, p->setDataSource(source)); |
Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 875 | } |
| 876 | |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 877 | status_t MediaPlayerService::Client::setDataSource( |
| 878 | const sp<IDataSource> &source) { |
Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 879 | sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source); |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 880 | player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource); |
| 881 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 882 | if (p == NULL) { |
| 883 | return NO_INIT; |
| 884 | } |
| 885 | // now set data source |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 886 | return mStatus = setDataSource_post(p, p->setDataSource(dataSource)); |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 889 | void MediaPlayerService::Client::disconnectNativeWindow_l() { |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 890 | if (mConnectedWindow != NULL) { |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 891 | status_t err = nativeWindowDisconnect( |
| 892 | mConnectedWindow.get(), "disconnectNativeWindow"); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 893 | |
| 894 | if (err != OK) { |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 895 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 896 | strerror(-err), err); |
| 897 | } |
| 898 | } |
| 899 | mConnectedWindow.clear(); |
| 900 | } |
| 901 | |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 902 | status_t MediaPlayerService::Client::setVideoSurfaceTexture( |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 903 | const sp<IGraphicBufferProducer>& bufferProducer) |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 904 | { |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 905 | ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get()); |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 906 | sp<MediaPlayerBase> p = getPlayer(); |
| 907 | if (p == 0) return UNKNOWN_ERROR; |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 908 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 909 | sp<IBinder> binder(IInterface::asBinder(bufferProducer)); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 910 | if (mConnectedWindowBinder == binder) { |
| 911 | return OK; |
| 912 | } |
| 913 | |
| 914 | sp<ANativeWindow> anw; |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 915 | if (bufferProducer != NULL) { |
Marco Nelissen | ee08f7e | 2013-09-16 13:30:01 -0700 | [diff] [blame] | 916 | anw = new Surface(bufferProducer, true /* controlledByApp */); |
Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 917 | status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture"); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 918 | |
| 919 | if (err != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 920 | ALOGE("setVideoSurfaceTexture failed: %d", err); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 921 | // Note that we must do the reset before disconnecting from the ANW. |
| 922 | // Otherwise queue/dequeue calls could be made on the disconnected |
| 923 | // ANW, which may result in errors. |
| 924 | reset(); |
| 925 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 926 | Mutex::Autolock lock(mLock); |
| 927 | disconnectNativeWindow_l(); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 928 | |
| 929 | return err; |
| 930 | } |
| 931 | } |
| 932 | |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 933 | // Note that we must set the player's new GraphicBufferProducer before |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 934 | // disconnecting the old one. Otherwise queue/dequeue calls could be made |
| 935 | // on the disconnected ANW, which may result in errors. |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 936 | status_t err = p->setVideoSurfaceTexture(bufferProducer); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 937 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 938 | mLock.lock(); |
| 939 | disconnectNativeWindow_l(); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 940 | |
| 941 | if (err == OK) { |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 942 | mConnectedWindow = anw; |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 943 | mConnectedWindowBinder = binder; |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 944 | mLock.unlock(); |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 945 | } else { |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 946 | mLock.unlock(); |
| 947 | status_t err = nativeWindowDisconnect( |
| 948 | anw.get(), "disconnectNativeWindow"); |
| 949 | |
| 950 | if (err != OK) { |
| 951 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", |
| 952 | strerror(-err), err); |
| 953 | } |
Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | return err; |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 959 | status_t MediaPlayerService::Client::invoke(const Parcel& request, |
| 960 | Parcel *reply) |
| 961 | { |
| 962 | sp<MediaPlayerBase> p = getPlayer(); |
| 963 | if (p == NULL) return UNKNOWN_ERROR; |
| 964 | return p->invoke(request, reply); |
| 965 | } |
| 966 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 967 | // This call doesn't need to access the native player. |
| 968 | status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter) |
| 969 | { |
| 970 | status_t status; |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 971 | media::Metadata::Filter allow, drop; |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 972 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 973 | if (unmarshallFilter(filter, &allow, &status) && |
| 974 | unmarshallFilter(filter, &drop, &status)) { |
| 975 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 976 | |
| 977 | mMetadataAllow = allow; |
| 978 | mMetadataDrop = drop; |
| 979 | } |
| 980 | return status; |
| 981 | } |
| 982 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 983 | status_t MediaPlayerService::Client::getMetadata( |
Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 984 | bool update_only, bool /*apply_filter*/, Parcel *reply) |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 985 | { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 986 | sp<MediaPlayerBase> player = getPlayer(); |
| 987 | if (player == 0) return UNKNOWN_ERROR; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 988 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 989 | status_t status; |
| 990 | // Placeholder for the return code, updated by the caller. |
| 991 | reply->writeInt32(-1); |
| 992 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 993 | media::Metadata::Filter ids; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 994 | |
| 995 | // We don't block notifications while we fetch the data. We clear |
| 996 | // mMetadataUpdated first so we don't lose notifications happening |
| 997 | // during the rest of this call. |
| 998 | { |
| 999 | Mutex::Autolock lock(mLock); |
| 1000 | if (update_only) { |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1001 | ids = mMetadataUpdated; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1002 | } |
| 1003 | mMetadataUpdated.clear(); |
| 1004 | } |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1005 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1006 | media::Metadata metadata(reply); |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1007 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1008 | metadata.appendHeader(); |
| 1009 | status = player->getMetadata(ids, reply); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1010 | |
| 1011 | if (status != OK) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1012 | metadata.resetParcel(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1013 | ALOGE("getMetadata failed %d", status); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1014 | return status; |
| 1015 | } |
| 1016 | |
| 1017 | // FIXME: Implement filtering on the result. Not critical since |
| 1018 | // filtering takes place on the update notifications already. This |
| 1019 | // would be when all the metadata are fetch and a filter is set. |
| 1020 | |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1021 | // Everything is fine, update the metadata length. |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1022 | metadata.updateLength(); |
niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1023 | return OK; |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1026 | status_t MediaPlayerService::Client::setBufferingSettings( |
| 1027 | const BufferingSettings& buffering) |
| 1028 | { |
Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 1029 | ALOGV("[%d] setBufferingSettings{%s}", |
| 1030 | mConnId, buffering.toString().string()); |
Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1031 | sp<MediaPlayerBase> p = getPlayer(); |
| 1032 | if (p == 0) return UNKNOWN_ERROR; |
| 1033 | return p->setBufferingSettings(buffering); |
| 1034 | } |
| 1035 | |
| 1036 | status_t MediaPlayerService::Client::getDefaultBufferingSettings( |
| 1037 | BufferingSettings* buffering /* nonnull */) |
| 1038 | { |
| 1039 | sp<MediaPlayerBase> p = getPlayer(); |
| 1040 | // TODO: create mPlayer on demand. |
| 1041 | if (p == 0) return UNKNOWN_ERROR; |
| 1042 | status_t ret = p->getDefaultBufferingSettings(buffering); |
| 1043 | if (ret == NO_ERROR) { |
Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 1044 | ALOGV("[%d] getDefaultBufferingSettings{%s}", |
| 1045 | mConnId, buffering->toString().string()); |
Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1046 | } else { |
| 1047 | ALOGV("[%d] getDefaultBufferingSettings returned %d", mConnId, ret); |
| 1048 | } |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | status_t MediaPlayerService::Client::prepareAsync() |
| 1053 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1054 | ALOGV("[%d] prepareAsync", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | sp<MediaPlayerBase> p = getPlayer(); |
| 1056 | if (p == 0) return UNKNOWN_ERROR; |
| 1057 | status_t ret = p->prepareAsync(); |
| 1058 | #if CALLBACK_ANTAGONIZER |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1059 | ALOGD("start Antagonizer"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | if (ret == NO_ERROR) mAntagonizer->start(); |
| 1061 | #endif |
| 1062 | return ret; |
| 1063 | } |
| 1064 | |
| 1065 | status_t MediaPlayerService::Client::start() |
| 1066 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1067 | ALOGV("[%d] start", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | sp<MediaPlayerBase> p = getPlayer(); |
| 1069 | if (p == 0) return UNKNOWN_ERROR; |
| 1070 | p->setLooping(mLoop); |
| 1071 | return p->start(); |
| 1072 | } |
| 1073 | |
| 1074 | status_t MediaPlayerService::Client::stop() |
| 1075 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1076 | ALOGV("[%d] stop", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | sp<MediaPlayerBase> p = getPlayer(); |
| 1078 | if (p == 0) return UNKNOWN_ERROR; |
| 1079 | return p->stop(); |
| 1080 | } |
| 1081 | |
| 1082 | status_t MediaPlayerService::Client::pause() |
| 1083 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1084 | ALOGV("[%d] pause", mConnId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | sp<MediaPlayerBase> p = getPlayer(); |
| 1086 | if (p == 0) return UNKNOWN_ERROR; |
| 1087 | return p->pause(); |
| 1088 | } |
| 1089 | |
| 1090 | status_t MediaPlayerService::Client::isPlaying(bool* state) |
| 1091 | { |
| 1092 | *state = false; |
| 1093 | sp<MediaPlayerBase> p = getPlayer(); |
| 1094 | if (p == 0) return UNKNOWN_ERROR; |
| 1095 | *state = p->isPlaying(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1096 | ALOGV("[%d] isPlaying: %d", mConnId, *state); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | return NO_ERROR; |
| 1098 | } |
| 1099 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1100 | status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate) |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1101 | { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1102 | ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)", |
| 1103 | mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1104 | sp<MediaPlayerBase> p = getPlayer(); |
| 1105 | if (p == 0) return UNKNOWN_ERROR; |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1106 | return p->setPlaybackSettings(rate); |
| 1107 | } |
| 1108 | |
| 1109 | status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) |
| 1110 | { |
| 1111 | sp<MediaPlayerBase> p = getPlayer(); |
| 1112 | if (p == 0) return UNKNOWN_ERROR; |
| 1113 | status_t ret = p->getPlaybackSettings(rate); |
| 1114 | if (ret == NO_ERROR) { |
| 1115 | ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)", |
| 1116 | mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode); |
| 1117 | } else { |
| 1118 | ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret); |
| 1119 | } |
| 1120 | return ret; |
| 1121 | } |
| 1122 | |
| 1123 | status_t MediaPlayerService::Client::setSyncSettings( |
| 1124 | const AVSyncSettings& sync, float videoFpsHint) |
| 1125 | { |
| 1126 | ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)", |
| 1127 | mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint); |
| 1128 | sp<MediaPlayerBase> p = getPlayer(); |
| 1129 | if (p == 0) return UNKNOWN_ERROR; |
| 1130 | return p->setSyncSettings(sync, videoFpsHint); |
| 1131 | } |
| 1132 | |
| 1133 | status_t MediaPlayerService::Client::getSyncSettings( |
| 1134 | AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) |
| 1135 | { |
| 1136 | sp<MediaPlayerBase> p = getPlayer(); |
| 1137 | if (p == 0) return UNKNOWN_ERROR; |
| 1138 | status_t ret = p->getSyncSettings(sync, videoFps); |
| 1139 | if (ret == NO_ERROR) { |
| 1140 | ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)", |
| 1141 | mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps); |
| 1142 | } else { |
| 1143 | ALOGV("[%d] getSyncSettings returned %d", mConnId, ret); |
| 1144 | } |
| 1145 | return ret; |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | status_t MediaPlayerService::Client::getCurrentPosition(int *msec) |
| 1149 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1150 | ALOGV("getCurrentPosition"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | sp<MediaPlayerBase> p = getPlayer(); |
| 1152 | if (p == 0) return UNKNOWN_ERROR; |
| 1153 | status_t ret = p->getCurrentPosition(msec); |
| 1154 | if (ret == NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1155 | ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1157 | ALOGE("getCurrentPosition returned %d", ret); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | } |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | status_t MediaPlayerService::Client::getDuration(int *msec) |
| 1163 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1164 | ALOGV("getDuration"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | sp<MediaPlayerBase> p = getPlayer(); |
| 1166 | if (p == 0) return UNKNOWN_ERROR; |
| 1167 | status_t ret = p->getDuration(msec); |
| 1168 | if (ret == NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1169 | ALOGV("[%d] getDuration = %d", mConnId, *msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1171 | ALOGE("getDuration returned %d", ret); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | } |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1176 | status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) { |
| 1177 | ALOGV("setNextPlayer"); |
| 1178 | Mutex::Autolock l(mLock); |
| 1179 | sp<Client> c = static_cast<Client*>(player.get()); |
Wei Jia | 2828412 | 2016-08-30 13:49:06 -0700 | [diff] [blame] | 1180 | if (c != NULL && !mService->hasClient(c)) { |
Robert Shih | ee0a0e3 | 2016-08-16 16:50:54 -0700 | [diff] [blame] | 1181 | return BAD_VALUE; |
| 1182 | } |
| 1183 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1184 | mNextClient = c; |
John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 1185 | |
| 1186 | if (c != NULL) { |
| 1187 | if (mAudioOutput != NULL) { |
| 1188 | mAudioOutput->setNextOutput(c->mAudioOutput); |
| 1189 | } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) { |
| 1190 | ALOGE("no current audio output"); |
| 1191 | } |
| 1192 | |
| 1193 | if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) { |
| 1194 | mPlayer->setNextPlayer(mNextClient->getPlayer()); |
| 1195 | } |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1196 | } |
John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 1197 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1198 | return OK; |
| 1199 | } |
| 1200 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1201 | VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper( |
| 1202 | const sp<VolumeShaper::Configuration>& configuration, |
| 1203 | const sp<VolumeShaper::Operation>& operation) { |
| 1204 | // for hardware output, call player instead |
| 1205 | ALOGV("Client::applyVolumeShaper(%p)", this); |
| 1206 | sp<MediaPlayerBase> p = getPlayer(); |
| 1207 | { |
| 1208 | Mutex::Autolock l(mLock); |
| 1209 | if (p != 0 && p->hardwareOutput()) { |
| 1210 | // TODO: investigate internal implementation |
| 1211 | return VolumeShaper::Status(INVALID_OPERATION); |
| 1212 | } |
| 1213 | if (mAudioOutput.get() != nullptr) { |
| 1214 | return mAudioOutput->applyVolumeShaper(configuration, operation); |
| 1215 | } |
| 1216 | } |
| 1217 | return VolumeShaper::Status(INVALID_OPERATION); |
| 1218 | } |
| 1219 | |
| 1220 | sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) { |
| 1221 | // for hardware output, call player instead |
| 1222 | ALOGV("Client::getVolumeShaperState(%p)", this); |
| 1223 | sp<MediaPlayerBase> p = getPlayer(); |
| 1224 | { |
| 1225 | Mutex::Autolock l(mLock); |
| 1226 | if (p != 0 && p->hardwareOutput()) { |
| 1227 | // TODO: investigate internal implementation. |
| 1228 | return nullptr; |
| 1229 | } |
| 1230 | if (mAudioOutput.get() != nullptr) { |
| 1231 | return mAudioOutput->getVolumeShaperState(id); |
| 1232 | } |
| 1233 | } |
| 1234 | return nullptr; |
| 1235 | } |
| 1236 | |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1237 | status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | { |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1239 | ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | sp<MediaPlayerBase> p = getPlayer(); |
| 1241 | if (p == 0) return UNKNOWN_ERROR; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1242 | return p->seekTo(msec, mode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | status_t MediaPlayerService::Client::reset() |
| 1246 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1247 | ALOGV("[%d] reset", mConnId); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1248 | mRetransmitEndpointValid = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | sp<MediaPlayerBase> p = getPlayer(); |
| 1250 | if (p == 0) return UNKNOWN_ERROR; |
| 1251 | return p->reset(); |
| 1252 | } |
| 1253 | |
Wei Jia | 52c2851 | 2017-09-13 18:17:51 -0700 | [diff] [blame] | 1254 | status_t MediaPlayerService::Client::notifyAt(int64_t mediaTimeUs) |
| 1255 | { |
| 1256 | ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs); |
| 1257 | sp<MediaPlayerBase> p = getPlayer(); |
| 1258 | if (p == 0) return UNKNOWN_ERROR; |
| 1259 | return p->notifyAt(mediaTimeUs); |
| 1260 | } |
| 1261 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1262 | 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] | 1263 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1264 | ALOGV("[%d] setAudioStreamType(%d)", mConnId, type); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | // TODO: for hardware output, call player instead |
| 1266 | Mutex::Autolock l(mLock); |
| 1267 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); |
| 1268 | return NO_ERROR; |
| 1269 | } |
| 1270 | |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1271 | status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel) |
| 1272 | { |
| 1273 | if (mAudioAttributes != NULL) { free(mAudioAttributes); } |
| 1274 | mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1275 | if (mAudioAttributes == NULL) { |
| 1276 | return NO_MEMORY; |
| 1277 | } |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1278 | unmarshallAudioAttributes(parcel, mAudioAttributes); |
| 1279 | |
| 1280 | ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s", |
| 1281 | mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags, |
| 1282 | mAudioAttributes->tags); |
| 1283 | |
| 1284 | if (mAudioOutput != 0) { |
| 1285 | mAudioOutput->setAudioAttributes(mAudioAttributes); |
| 1286 | } |
| 1287 | return NO_ERROR; |
| 1288 | } |
| 1289 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | status_t MediaPlayerService::Client::setLooping(int loop) |
| 1291 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1292 | ALOGV("[%d] setLooping(%d)", mConnId, loop); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | mLoop = loop; |
| 1294 | sp<MediaPlayerBase> p = getPlayer(); |
| 1295 | if (p != 0) return p->setLooping(loop); |
| 1296 | return NO_ERROR; |
| 1297 | } |
| 1298 | |
| 1299 | status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume) |
| 1300 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1301 | ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); |
John Grossman | 761defc | 2012-02-09 15:09:05 -0800 | [diff] [blame] | 1302 | |
| 1303 | // for hardware output, call player instead |
| 1304 | sp<MediaPlayerBase> p = getPlayer(); |
| 1305 | { |
| 1306 | Mutex::Autolock l(mLock); |
| 1307 | if (p != 0 && p->hardwareOutput()) { |
| 1308 | MediaPlayerHWInterface* hwp = |
| 1309 | reinterpret_cast<MediaPlayerHWInterface*>(p.get()); |
| 1310 | return hwp->setVolume(leftVolume, rightVolume); |
| 1311 | } else { |
| 1312 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); |
| 1313 | return NO_ERROR; |
| 1314 | } |
| 1315 | } |
| 1316 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | return NO_ERROR; |
| 1318 | } |
| 1319 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1320 | status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level) |
| 1321 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1322 | ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1323 | Mutex::Autolock l(mLock); |
| 1324 | if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level); |
| 1325 | return NO_ERROR; |
| 1326 | } |
| 1327 | |
| 1328 | status_t MediaPlayerService::Client::attachAuxEffect(int effectId) |
| 1329 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1330 | ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1331 | Mutex::Autolock l(mLock); |
| 1332 | if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId); |
| 1333 | return NO_ERROR; |
| 1334 | } |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1335 | |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1336 | status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1337 | ALOGV("[%d] setParameter(%d)", mConnId, key); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1338 | switch (key) { |
| 1339 | case KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1340 | { |
| 1341 | Mutex::Autolock l(mLock); |
| 1342 | return setAudioAttributes_l(request); |
| 1343 | } |
| 1344 | default: |
| 1345 | sp<MediaPlayerBase> p = getPlayer(); |
| 1346 | if (p == 0) { return UNKNOWN_ERROR; } |
| 1347 | return p->setParameter(key, request); |
| 1348 | } |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1352 | ALOGV("[%d] getParameter(%d)", mConnId, key); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1353 | sp<MediaPlayerBase> p = getPlayer(); |
| 1354 | if (p == 0) return UNKNOWN_ERROR; |
| 1355 | return p->getParameter(key, reply); |
| 1356 | } |
| 1357 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1358 | status_t MediaPlayerService::Client::setRetransmitEndpoint( |
| 1359 | const struct sockaddr_in* endpoint) { |
| 1360 | |
| 1361 | if (NULL != endpoint) { |
| 1362 | uint32_t a = ntohl(endpoint->sin_addr.s_addr); |
| 1363 | uint16_t p = ntohs(endpoint->sin_port); |
| 1364 | ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId, |
| 1365 | (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p); |
| 1366 | } else { |
| 1367 | ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId); |
| 1368 | } |
| 1369 | |
| 1370 | sp<MediaPlayerBase> p = getPlayer(); |
| 1371 | |
| 1372 | // Right now, the only valid time to set a retransmit endpoint is before |
| 1373 | // player selection has been made (since the presence or absence of a |
| 1374 | // retransmit endpoint is going to determine which player is selected during |
| 1375 | // setDataSource). |
| 1376 | if (p != 0) return INVALID_OPERATION; |
| 1377 | |
| 1378 | if (NULL != endpoint) { |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 1379 | Mutex::Autolock lock(mLock); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1380 | mRetransmitEndpoint = *endpoint; |
| 1381 | mRetransmitEndpointValid = true; |
| 1382 | } else { |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 1383 | Mutex::Autolock lock(mLock); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1384 | mRetransmitEndpointValid = false; |
| 1385 | } |
| 1386 | |
| 1387 | return NO_ERROR; |
| 1388 | } |
| 1389 | |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1390 | status_t MediaPlayerService::Client::getRetransmitEndpoint( |
| 1391 | struct sockaddr_in* endpoint) |
| 1392 | { |
| 1393 | if (NULL == endpoint) |
| 1394 | return BAD_VALUE; |
| 1395 | |
| 1396 | sp<MediaPlayerBase> p = getPlayer(); |
| 1397 | |
| 1398 | if (p != NULL) |
| 1399 | return p->getRetransmitEndpoint(endpoint); |
| 1400 | |
Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 1401 | Mutex::Autolock lock(mLock); |
John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1402 | if (!mRetransmitEndpointValid) |
| 1403 | return NO_INIT; |
| 1404 | |
| 1405 | *endpoint = mRetransmitEndpoint; |
| 1406 | |
| 1407 | return NO_ERROR; |
| 1408 | } |
| 1409 | |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 1410 | void MediaPlayerService::Client::notify( |
| 1411 | 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] | 1412 | { |
| 1413 | Client* client = static_cast<Client*>(cookie); |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1414 | if (client == NULL) { |
| 1415 | return; |
| 1416 | } |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1417 | |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1418 | sp<IMediaPlayerClient> c; |
Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1419 | sp<Client> nextClient; |
| 1420 | status_t errStartNext = NO_ERROR; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1421 | { |
| 1422 | Mutex::Autolock l(client->mLock); |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1423 | c = client->mClient; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1424 | if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) { |
Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1425 | nextClient = client->mNextClient; |
| 1426 | |
John Grossman | cb0b755 | 2012-08-23 17:47:31 -0700 | [diff] [blame] | 1427 | if (client->mAudioOutput != NULL) |
| 1428 | client->mAudioOutput->switchToNextOutput(); |
Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1429 | |
| 1430 | errStartNext = nextClient->start(); |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if (nextClient != NULL) { |
| 1435 | sp<IMediaPlayerClient> nc; |
| 1436 | { |
| 1437 | Mutex::Autolock l(nextClient->mLock); |
| 1438 | nc = nextClient->mClient; |
| 1439 | } |
| 1440 | if (nc != NULL) { |
| 1441 | if (errStartNext == NO_ERROR) { |
| 1442 | nc->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj); |
| 1443 | } else { |
| 1444 | nc->notify(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN , 0, obj); |
| 1445 | ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext); |
Wei Jia | 2afac0c | 2016-01-07 12:13:07 -0800 | [diff] [blame] | 1446 | } |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1450 | if (MEDIA_INFO == msg && |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1451 | MEDIA_INFO_METADATA_UPDATE == ext1) { |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1452 | const media::Metadata::Type metadata_type = ext2; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1453 | |
| 1454 | if(client->shouldDropMetadata(metadata_type)) { |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | // Update the list of metadata that have changed. getMetadata |
| 1459 | // also access mMetadataUpdated and clears it. |
| 1460 | client->addNewMetadataUpdate(metadata_type); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1461 | } |
James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1462 | |
| 1463 | if (c != NULL) { |
| 1464 | ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2); |
| 1465 | c->notify(msg, ext1, ext2, obj); |
| 1466 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1469 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1470 | bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1471 | { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1472 | Mutex::Autolock lock(mLock); |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1473 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1474 | if (findMetadata(mMetadataDrop, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1475 | return true; |
| 1476 | } |
| 1477 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1478 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1479 | return false; |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1480 | } else { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1481 | return true; |
| 1482 | } |
| 1483 | } |
| 1484 | |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1485 | |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1486 | void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1487 | Mutex::Autolock lock(mLock); |
| 1488 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1489 | mMetadataUpdated.add(metadata_type); |
| 1490 | } |
| 1491 | } |
| 1492 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1493 | // Modular DRM |
| 1494 | status_t MediaPlayerService::Client::prepareDrm(const uint8_t uuid[16], |
| 1495 | const Vector<uint8_t>& drmSessionId) |
| 1496 | { |
| 1497 | ALOGV("[%d] prepareDrm", mConnId); |
| 1498 | sp<MediaPlayerBase> p = getPlayer(); |
| 1499 | if (p == 0) return UNKNOWN_ERROR; |
| 1500 | |
| 1501 | status_t ret = p->prepareDrm(uuid, drmSessionId); |
| 1502 | ALOGV("prepareDrm ret: %d", ret); |
| 1503 | |
| 1504 | return ret; |
| 1505 | } |
| 1506 | |
| 1507 | status_t MediaPlayerService::Client::releaseDrm() |
| 1508 | { |
| 1509 | ALOGV("[%d] releaseDrm", mConnId); |
| 1510 | sp<MediaPlayerBase> p = getPlayer(); |
| 1511 | if (p == 0) return UNKNOWN_ERROR; |
| 1512 | |
| 1513 | status_t ret = p->releaseDrm(); |
| 1514 | ALOGV("releaseDrm ret: %d", ret); |
| 1515 | |
| 1516 | return ret; |
| 1517 | } |
| 1518 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | #if CALLBACK_ANTAGONIZER |
| 1520 | const int Antagonizer::interval = 10000; // 10 msecs |
| 1521 | |
| 1522 | Antagonizer::Antagonizer(notify_callback_f cb, void* client) : |
| 1523 | mExit(false), mActive(false), mClient(client), mCb(cb) |
| 1524 | { |
| 1525 | createThread(callbackThread, this); |
| 1526 | } |
| 1527 | |
| 1528 | void Antagonizer::kill() |
| 1529 | { |
| 1530 | Mutex::Autolock _l(mLock); |
| 1531 | mActive = false; |
| 1532 | mExit = true; |
| 1533 | mCondition.wait(mLock); |
| 1534 | } |
| 1535 | |
| 1536 | int Antagonizer::callbackThread(void* user) |
| 1537 | { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1538 | ALOGD("Antagonizer started"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); |
| 1540 | while (!p->mExit) { |
| 1541 | if (p->mActive) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1542 | ALOGV("send event"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1543 | p->mCb(p->mClient, 0, 0, 0); |
| 1544 | } |
| 1545 | usleep(interval); |
| 1546 | } |
| 1547 | Mutex::Autolock _l(p->mLock); |
| 1548 | p->mCondition.signal(); |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1549 | ALOGD("Antagonizer stopped"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1550 | return 0; |
| 1551 | } |
| 1552 | #endif |
| 1553 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | #undef LOG_TAG |
| 1555 | #define LOG_TAG "AudioSink" |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 1556 | MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId, uid_t uid, int pid, |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1557 | const audio_attributes_t* attr) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1558 | : mCallback(NULL), |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1559 | mCallbackCookie(NULL), |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1560 | mCallbackData(NULL), |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1561 | mStreamType(AUDIO_STREAM_MUSIC), |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1562 | mLeftVolume(1.0), |
| 1563 | mRightVolume(1.0), |
| 1564 | mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT), |
| 1565 | mSampleRateHz(0), |
| 1566 | mMsecsPerFrame(0), |
| 1567 | mFrameSize(0), |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1568 | mSessionId(sessionId), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1569 | mUid(uid), |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1570 | mPid(pid), |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1571 | mSendLevel(0.0), |
| 1572 | mAuxEffectId(0), |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 1573 | mFlags(AUDIO_OUTPUT_FLAG_NONE), |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 1574 | mVolumeHandler(new media::VolumeHandler()) |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1575 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1576 | ALOGV("AudioOutput(%d)", sessionId); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1577 | if (attr != NULL) { |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1578 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1579 | if (mAttributes != NULL) { |
| 1580 | memcpy(mAttributes, attr, sizeof(audio_attributes_t)); |
| 1581 | mStreamType = audio_attributes_to_stream_type(attr); |
| 1582 | } |
| 1583 | } else { |
| 1584 | mAttributes = NULL; |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | setMinBufferCount(); |
| 1588 | } |
| 1589 | |
| 1590 | MediaPlayerService::AudioOutput::~AudioOutput() |
| 1591 | { |
| 1592 | close(); |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1593 | free(mAttributes); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1594 | delete mCallbackData; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1597 | //static |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | void MediaPlayerService::AudioOutput::setMinBufferCount() |
| 1599 | { |
| 1600 | char value[PROPERTY_VALUE_MAX]; |
| 1601 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 1602 | mIsOnEmulator = true; |
| 1603 | mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator |
| 1604 | } |
| 1605 | } |
| 1606 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1607 | // static |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1608 | bool MediaPlayerService::AudioOutput::isOnEmulator() |
| 1609 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1610 | setMinBufferCount(); // benign race wrt other threads |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | return mIsOnEmulator; |
| 1612 | } |
| 1613 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1614 | // static |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | int MediaPlayerService::AudioOutput::getMinBufferCount() |
| 1616 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1617 | setMinBufferCount(); // benign race wrt other threads |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | return mMinBufferCount; |
| 1619 | } |
| 1620 | |
| 1621 | ssize_t MediaPlayerService::AudioOutput::bufferSize() const |
| 1622 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1623 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | if (mTrack == 0) return NO_INIT; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1625 | return mTrack->frameCount() * mFrameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | ssize_t MediaPlayerService::AudioOutput::frameCount() const |
| 1629 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1630 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | if (mTrack == 0) return NO_INIT; |
| 1632 | return mTrack->frameCount(); |
| 1633 | } |
| 1634 | |
| 1635 | ssize_t MediaPlayerService::AudioOutput::channelCount() const |
| 1636 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1637 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | if (mTrack == 0) return NO_INIT; |
| 1639 | return mTrack->channelCount(); |
| 1640 | } |
| 1641 | |
| 1642 | ssize_t MediaPlayerService::AudioOutput::frameSize() const |
| 1643 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1644 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | if (mTrack == 0) return NO_INIT; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1646 | return mFrameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | uint32_t MediaPlayerService::AudioOutput::latency () const |
| 1650 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1651 | Mutex::Autolock lock(mLock); |
Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 1652 | if (mTrack == 0) return 0; |
| 1653 | return mTrack->latency(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | float MediaPlayerService::AudioOutput::msecsPerFrame() const |
| 1657 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1658 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1659 | return mMsecsPerFrame; |
| 1660 | } |
| 1661 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1662 | status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1663 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1664 | Mutex::Autolock lock(mLock); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1665 | if (mTrack == 0) return NO_INIT; |
| 1666 | return mTrack->getPosition(position); |
| 1667 | } |
| 1668 | |
Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 1669 | status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const |
| 1670 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1671 | Mutex::Autolock lock(mLock); |
Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 1672 | if (mTrack == 0) return NO_INIT; |
| 1673 | return mTrack->getTimestamp(ts); |
| 1674 | } |
| 1675 | |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1676 | // TODO: Remove unnecessary calls to getPlayedOutDurationUs() |
| 1677 | // as it acquires locks and may query the audio driver. |
| 1678 | // |
| 1679 | // Some calls could conceivably retrieve extrapolated data instead of |
| 1680 | // accessing getTimestamp() or getPosition() every time a data buffer with |
| 1681 | // a media time is received. |
| 1682 | // |
| 1683 | // Calculate duration of played samples if played at normal rate (i.e., 1.0). |
| 1684 | int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const |
| 1685 | { |
| 1686 | Mutex::Autolock lock(mLock); |
| 1687 | if (mTrack == 0 || mSampleRateHz == 0) { |
Wei Jia | 213f490 | 2015-10-22 08:55:25 -0700 | [diff] [blame] | 1688 | return 0; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | uint32_t numFramesPlayed; |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1692 | int64_t numFramesPlayedAtUs; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1693 | AudioTimestamp ts; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1694 | |
| 1695 | status_t res = mTrack->getTimestamp(ts); |
| 1696 | if (res == OK) { // case 1: mixing audio tracks and offloaded tracks. |
| 1697 | numFramesPlayed = ts.mPosition; |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1698 | numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000; |
| 1699 | //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1700 | } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track |
| 1701 | numFramesPlayed = 0; |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1702 | numFramesPlayedAtUs = nowUs; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1703 | //ALOGD("getTimestamp: WOULD_BLOCK %d %lld", |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1704 | // numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1705 | } else { // case 3: transitory at new track or audio fast tracks. |
| 1706 | res = mTrack->getPosition(&numFramesPlayed); |
| 1707 | CHECK_EQ(res, (status_t)OK); |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1708 | numFramesPlayedAtUs = nowUs; |
| 1709 | numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */ |
| 1710 | //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test |
| 1714 | // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours. |
| 1715 | int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz) |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1716 | + nowUs - numFramesPlayedAtUs; |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1717 | if (durationUs < 0) { |
| 1718 | // Occurs when numFramesPlayed position is very small and the following: |
| 1719 | // (1) In case 1, the time nowUs is computed before getTimestamp() is called and |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1720 | // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed. |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1721 | // (2) In case 3, using getPosition and adding mAudioSink->latency() to |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1722 | // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed. |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1723 | // |
| 1724 | // Both of these are transitory conditions. |
| 1725 | ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs); |
| 1726 | durationUs = 0; |
| 1727 | } |
| 1728 | ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)", |
Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1729 | (long long)durationUs, (long long)nowUs, |
| 1730 | numFramesPlayed, (long long)numFramesPlayedAtUs); |
Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1731 | return durationUs; |
| 1732 | } |
| 1733 | |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1734 | status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const |
| 1735 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1736 | Mutex::Autolock lock(mLock); |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1737 | if (mTrack == 0) return NO_INIT; |
Andy Hung | 2f6e73d | 2016-04-08 12:12:58 -0700 | [diff] [blame] | 1738 | ExtendedTimestamp ets; |
| 1739 | status_t status = mTrack->getTimestamp(&ets); |
| 1740 | if (status == OK || status == WOULD_BLOCK) { |
| 1741 | *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT]; |
| 1742 | } |
| 1743 | return status; |
Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1746 | status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs) |
| 1747 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1748 | Mutex::Autolock lock(mLock); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1749 | if (mTrack == 0) return NO_INIT; |
| 1750 | return mTrack->setParameters(keyValuePairs); |
| 1751 | } |
| 1752 | |
| 1753 | String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys) |
| 1754 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1755 | Mutex::Autolock lock(mLock); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1756 | if (mTrack == 0) return String8::empty(); |
| 1757 | return mTrack->getParameters(keys); |
| 1758 | } |
| 1759 | |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1760 | void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1761 | Mutex::Autolock lock(mLock); |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1762 | if (attributes == NULL) { |
| 1763 | free(mAttributes); |
| 1764 | mAttributes = NULL; |
| 1765 | } else { |
| 1766 | if (mAttributes == NULL) { |
| 1767 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1768 | } |
| 1769 | memcpy(mAttributes, attributes, sizeof(audio_attributes_t)); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1770 | mStreamType = audio_attributes_to_stream_type(attributes); |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType) |
| 1775 | { |
Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1776 | Mutex::Autolock lock(mLock); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1777 | // do not allow direct stream type modification if attributes have been set |
| 1778 | if (mAttributes == NULL) { |
| 1779 | mStreamType = streamType; |
| 1780 | } |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1783 | void MediaPlayerService::AudioOutput::deleteRecycledTrack_l() |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1784 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1785 | ALOGV("deleteRecycledTrack_l"); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1786 | if (mRecycledTrack != 0) { |
| 1787 | |
| 1788 | if (mCallbackData != NULL) { |
| 1789 | mCallbackData->setOutput(NULL); |
| 1790 | mCallbackData->endTrackSwitch(); |
| 1791 | } |
| 1792 | |
| 1793 | if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) { |
Andy Hung | e13f8a6 | 2016-03-30 14:20:42 -0700 | [diff] [blame] | 1794 | int32_t msec = 0; |
| 1795 | if (!mRecycledTrack->stopped()) { // check if active |
| 1796 | (void)mRecycledTrack->pendingDuration(&msec); |
| 1797 | } |
| 1798 | mRecycledTrack->stop(); // ensure full data drain |
| 1799 | ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec); |
| 1800 | if (msec > 0) { |
| 1801 | static const int32_t WAIT_LIMIT_MS = 3000; |
| 1802 | if (msec > WAIT_LIMIT_MS) { |
| 1803 | msec = WAIT_LIMIT_MS; |
| 1804 | } |
| 1805 | usleep(msec * 1000LL); |
| 1806 | } |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1807 | } |
| 1808 | // An offloaded track isn't flushed because the STREAM_END is reported |
| 1809 | // slightly prematurely to allow time for the gapless track switch |
| 1810 | // but this means that if we decide not to recycle the track there |
| 1811 | // could be a small amount of residual data still playing. We leave |
| 1812 | // AudioFlinger to drain the track. |
| 1813 | |
| 1814 | mRecycledTrack.clear(); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1815 | close_l(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1816 | delete mCallbackData; |
| 1817 | mCallbackData = NULL; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1821 | void MediaPlayerService::AudioOutput::close_l() |
| 1822 | { |
| 1823 | mTrack.clear(); |
| 1824 | } |
| 1825 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1826 | status_t MediaPlayerService::AudioOutput::open( |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1827 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, |
| 1828 | audio_format_t format, int bufferCount, |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1829 | AudioCallback cb, void *cookie, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1830 | audio_output_flags_t flags, |
Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 1831 | const audio_offload_info_t *offloadInfo, |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1832 | bool doNotReconnect, |
| 1833 | uint32_t suggestedFrameCount) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1834 | { |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1835 | ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask, |
| 1836 | format, bufferCount, mSessionId, flags); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1838 | // offloading is only supported in callback mode for now. |
| 1839 | // offloadInfo must be present if offload flag is set |
| 1840 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) && |
| 1841 | ((cb == NULL) || (offloadInfo == NULL))) { |
| 1842 | return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1845 | // compute frame count for the AudioTrack internal buffer |
| 1846 | size_t frameCount; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1847 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1848 | frameCount = 0; // AudioTrack will get frame count from AudioFlinger |
| 1849 | } else { |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1850 | // try to estimate the buffer processing fetch size from AudioFlinger. |
| 1851 | // framesPerBuffer is approximate and generally correct, except when it's not :-). |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1852 | uint32_t afSampleRate; |
| 1853 | size_t afFrameCount; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1854 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| 1855 | return NO_INIT; |
| 1856 | } |
| 1857 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| 1858 | return NO_INIT; |
| 1859 | } |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1860 | const size_t framesPerBuffer = |
| 1861 | (unsigned long long)sampleRate * afFrameCount / afSampleRate; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1862 | |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1863 | if (bufferCount == 0) { |
| 1864 | // use suggestedFrameCount |
| 1865 | bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer; |
| 1866 | } |
| 1867 | // Check argument bufferCount against the mininum buffer count |
| 1868 | if (bufferCount != 0 && bufferCount < mMinBufferCount) { |
| 1869 | ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount); |
| 1870 | bufferCount = mMinBufferCount; |
| 1871 | } |
| 1872 | // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger |
| 1873 | // which will be the minimum size permitted. |
| 1874 | frameCount = bufferCount * framesPerBuffer; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1875 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1876 | |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1877 | if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { |
Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1878 | channelMask = audio_channel_out_mask_from_count(channelCount); |
Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1879 | if (0 == channelMask) { |
| 1880 | ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount); |
| 1881 | return NO_INIT; |
| 1882 | } |
| 1883 | } |
Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1884 | |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1885 | Mutex::Autolock lock(mLock); |
Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1886 | mCallback = cb; |
| 1887 | mCallbackCookie = cookie; |
| 1888 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1889 | // Check whether we can recycle the track |
| 1890 | bool reuse = false; |
| 1891 | bool bothOffloaded = false; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1892 | |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1893 | if (mRecycledTrack != 0) { |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1894 | // check whether we are switching between two offloaded tracks |
| 1895 | bothOffloaded = (flags & mRecycledTrack->getFlags() |
| 1896 | & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1897 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1898 | // check if the existing track can be reused as-is, or if a new track needs to be created. |
| 1899 | reuse = true; |
| 1900 | |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1901 | if ((mCallbackData == NULL && mCallback != NULL) || |
| 1902 | (mCallbackData != NULL && mCallback == NULL)) { |
| 1903 | // recycled track uses callbacks but the caller wants to use writes, or vice versa |
| 1904 | ALOGV("can't chain callback and write"); |
| 1905 | reuse = false; |
| 1906 | } else if ((mRecycledTrack->getSampleRate() != sampleRate) || |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1907 | (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) { |
| 1908 | ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch", |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1909 | mRecycledTrack->getSampleRate(), sampleRate, |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1910 | mRecycledTrack->channelCount(), channelCount); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1911 | reuse = false; |
| 1912 | } else if (flags != mFlags) { |
| 1913 | ALOGV("output flags differ %08x/%08x", flags, mFlags); |
| 1914 | reuse = false; |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1915 | } else if (mRecycledTrack->format() != format) { |
| 1916 | reuse = false; |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1917 | } |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1918 | } else { |
| 1919 | ALOGV("no track available to recycle"); |
| 1920 | } |
| 1921 | |
| 1922 | ALOGV_IF(bothOffloaded, "both tracks offloaded"); |
| 1923 | |
| 1924 | // If we can't recycle and both tracks are offloaded |
| 1925 | // we must close the previous output before opening a new one |
| 1926 | if (bothOffloaded && !reuse) { |
| 1927 | ALOGV("both offloaded and not recycling"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1928 | deleteRecycledTrack_l(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | sp<AudioTrack> t; |
| 1932 | CallbackData *newcbd = NULL; |
| 1933 | |
| 1934 | // We don't attempt to create a new track if we are recycling an |
| 1935 | // offloaded track. But, if we are recycling a non-offloaded or we |
| 1936 | // are switching where one is offloaded and one isn't then we create |
| 1937 | // the new track in advance so that we can read additional stream info |
| 1938 | |
| 1939 | if (!(reuse && bothOffloaded)) { |
| 1940 | ALOGV("creating new AudioTrack"); |
| 1941 | |
| 1942 | if (mCallback != NULL) { |
| 1943 | newcbd = new CallbackData(this); |
| 1944 | t = new AudioTrack( |
| 1945 | mStreamType, |
| 1946 | sampleRate, |
| 1947 | format, |
| 1948 | channelMask, |
| 1949 | frameCount, |
| 1950 | flags, |
| 1951 | CallbackWrapper, |
| 1952 | newcbd, |
| 1953 | 0, // notification frames |
| 1954 | mSessionId, |
| 1955 | AudioTrack::TRANSFER_CALLBACK, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1956 | offloadInfo, |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1957 | mUid, |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1958 | mPid, |
Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 1959 | mAttributes, |
| 1960 | doNotReconnect); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1961 | } else { |
Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 1962 | // TODO: Due to buffer memory concerns, we use a max target playback speed |
| 1963 | // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed), |
| 1964 | // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed. |
| 1965 | const float targetSpeed = |
| 1966 | std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed); |
| 1967 | ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed, |
| 1968 | "track target speed:%f clamped from playback speed:%f", |
| 1969 | targetSpeed, mPlaybackRate.mSpeed); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1970 | t = new AudioTrack( |
| 1971 | mStreamType, |
| 1972 | sampleRate, |
| 1973 | format, |
| 1974 | channelMask, |
| 1975 | frameCount, |
| 1976 | flags, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1977 | NULL, // callback |
| 1978 | NULL, // user data |
| 1979 | 0, // notification frames |
| 1980 | mSessionId, |
| 1981 | AudioTrack::TRANSFER_DEFAULT, |
| 1982 | NULL, // offload info |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1983 | mUid, |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1984 | mPid, |
Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 1985 | mAttributes, |
Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 1986 | doNotReconnect, |
| 1987 | targetSpeed); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
| 1991 | ALOGE("Unable to create audio track"); |
| 1992 | delete newcbd; |
Glenn Kasten | 3e98ecd | 2015-05-18 13:13:24 -0700 | [diff] [blame] | 1993 | // t goes out of scope, so reference count drops to zero |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1994 | return NO_INIT; |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1995 | } else { |
| 1996 | // successful AudioTrack initialization implies a legacy stream type was generated |
| 1997 | // from the audio attributes |
| 1998 | mStreamType = t->streamType(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | if (reuse) { |
| 2003 | CHECK(mRecycledTrack != NULL); |
| 2004 | |
| 2005 | if (!bothOffloaded) { |
| 2006 | if (mRecycledTrack->frameCount() != t->frameCount()) { |
Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 2007 | ALOGV("framecount differs: %zu/%zu frames", |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2008 | mRecycledTrack->frameCount(), t->frameCount()); |
| 2009 | reuse = false; |
| 2010 | } |
| 2011 | } |
| 2012 | |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2013 | if (reuse) { |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2014 | ALOGV("chaining to next output and recycling track"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2015 | close_l(); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2016 | mTrack = mRecycledTrack; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2017 | mRecycledTrack.clear(); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2018 | if (mCallbackData != NULL) { |
| 2019 | mCallbackData->setOutput(this); |
| 2020 | } |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2021 | delete newcbd; |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2022 | return updateTrack(); |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2023 | } |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2026 | // we're not going to reuse the track, unblock and flush it |
| 2027 | // this was done earlier if both tracks are offloaded |
| 2028 | if (!bothOffloaded) { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2029 | deleteRecycledTrack_l(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL))); |
| 2033 | |
Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2034 | mCallbackData = newcbd; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2035 | ALOGV("setVolume"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2036 | t->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2037 | |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2038 | // Restore VolumeShapers for the MediaPlayer in case the track was recreated |
| 2039 | // due to an output sink error (e.g. offload to non-offload switch). |
| 2040 | mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status { |
| 2041 | sp<VolumeShaper::Operation> operationToEnd = |
| 2042 | new VolumeShaper::Operation(shaper.mOperation); |
| 2043 | // TODO: Ideally we would restore to the exact xOffset position |
| 2044 | // as returned by getVolumeShaperState(), but we don't have that |
| 2045 | // information when restoring at the client unless we periodically poll |
| 2046 | // the server or create shared memory state. |
| 2047 | // |
| 2048 | // For now, we simply advance to the end of the VolumeShaper effect |
| 2049 | // if it has been started. |
| 2050 | if (shaper.isStarted()) { |
Andy Hung | f370264 | 2017-05-05 17:33:32 -0700 | [diff] [blame] | 2051 | operationToEnd->setNormalizedTime(1.f); |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2052 | } |
| 2053 | return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2054 | }); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2055 | |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2056 | mSampleRateHz = sampleRate; |
Preetam Singh Ranawat | 2e17eef | 2015-08-12 12:11:46 -0700 | [diff] [blame] | 2057 | mFlags = flags; |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2058 | mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2059 | mFrameSize = t->frameSize(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | mTrack = t; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2061 | |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2062 | return updateTrack(); |
| 2063 | } |
| 2064 | |
| 2065 | status_t MediaPlayerService::AudioOutput::updateTrack() { |
| 2066 | if (mTrack == NULL) { |
| 2067 | return NO_ERROR; |
| 2068 | } |
| 2069 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2070 | status_t res = NO_ERROR; |
Wei Jia | 0162d00 | 2015-06-09 11:59:33 -0700 | [diff] [blame] | 2071 | // Note some output devices may give us a direct track even though we don't specify it. |
| 2072 | // Example: Line application b/17459982. |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2073 | if ((mTrack->getFlags() |
| 2074 | & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) { |
| 2075 | res = mTrack->setPlaybackRate(mPlaybackRate); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2076 | if (res == NO_ERROR) { |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2077 | mTrack->setAuxEffectSendLevel(mSendLevel); |
| 2078 | res = mTrack->attachAuxEffect(mAuxEffectId); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2079 | } |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2080 | } |
Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2081 | ALOGV("updateTrack() DONE status %d", res); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2082 | return res; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2083 | } |
| 2084 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2085 | status_t MediaPlayerService::AudioOutput::start() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2086 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2087 | ALOGV("start"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2088 | Mutex::Autolock lock(mLock); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2089 | if (mCallbackData != NULL) { |
| 2090 | mCallbackData->endTrackSwitch(); |
| 2091 | } |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2092 | if (mTrack != 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | mTrack->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2094 | mTrack->setAuxEffectSendLevel(mSendLevel); |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2095 | status_t status = mTrack->start(); |
| 2096 | if (status == NO_ERROR) { |
| 2097 | mVolumeHandler->setStarted(); |
| 2098 | } |
| 2099 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2100 | } |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2101 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2102 | } |
| 2103 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2104 | void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2105 | Mutex::Autolock lock(mLock); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2106 | mNextOutput = nextOutput; |
| 2107 | } |
Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 2108 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2109 | void MediaPlayerService::AudioOutput::switchToNextOutput() { |
| 2110 | ALOGV("switchToNextOutput"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2111 | |
| 2112 | // Try to acquire the callback lock before moving track (without incurring deadlock). |
| 2113 | const unsigned kMaxSwitchTries = 100; |
| 2114 | Mutex::Autolock lock(mLock); |
| 2115 | for (unsigned tries = 0;;) { |
| 2116 | if (mTrack == 0) { |
| 2117 | return; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2118 | } |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2119 | if (mNextOutput != NULL && mNextOutput != this) { |
| 2120 | if (mCallbackData != NULL) { |
| 2121 | // two alternative approaches |
| 2122 | #if 1 |
| 2123 | CallbackData *callbackData = mCallbackData; |
| 2124 | mLock.unlock(); |
| 2125 | // proper acquisition sequence |
| 2126 | callbackData->lock(); |
| 2127 | mLock.lock(); |
| 2128 | // Caution: it is unlikely that someone deleted our callback or changed our target |
| 2129 | if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) { |
| 2130 | // fatal if we are starved out. |
| 2131 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, |
| 2132 | "switchToNextOutput() cannot obtain correct lock sequence"); |
| 2133 | callbackData->unlock(); |
| 2134 | continue; |
| 2135 | } |
| 2136 | callbackData->mSwitching = true; // begin track switch |
Wei Jia | adee56a | 2016-08-05 15:40:34 -0700 | [diff] [blame] | 2137 | callbackData->setOutput(NULL); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2138 | #else |
| 2139 | // tryBeginTrackSwitch() returns false if the callback has the lock. |
| 2140 | if (!mCallbackData->tryBeginTrackSwitch()) { |
| 2141 | // fatal if we are starved out. |
| 2142 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, |
| 2143 | "switchToNextOutput() cannot obtain callback lock"); |
| 2144 | mLock.unlock(); |
| 2145 | usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput |
| 2146 | mLock.lock(); |
| 2147 | continue; |
| 2148 | } |
| 2149 | #endif |
| 2150 | } |
| 2151 | |
| 2152 | Mutex::Autolock nextLock(mNextOutput->mLock); |
| 2153 | |
| 2154 | // If the next output track is not NULL, then it has been |
| 2155 | // opened already for playback. |
| 2156 | // This is possible even without the next player being started, |
| 2157 | // for example, the next player could be prepared and seeked. |
| 2158 | // |
| 2159 | // Presuming it isn't advisable to force the track over. |
| 2160 | if (mNextOutput->mTrack == NULL) { |
| 2161 | ALOGD("Recycling track for gapless playback"); |
| 2162 | delete mNextOutput->mCallbackData; |
| 2163 | mNextOutput->mCallbackData = mCallbackData; |
| 2164 | mNextOutput->mRecycledTrack = mTrack; |
| 2165 | mNextOutput->mSampleRateHz = mSampleRateHz; |
| 2166 | mNextOutput->mMsecsPerFrame = mMsecsPerFrame; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2167 | mNextOutput->mFlags = mFlags; |
| 2168 | mNextOutput->mFrameSize = mFrameSize; |
| 2169 | close_l(); |
| 2170 | mCallbackData = NULL; // destruction handled by mNextOutput |
| 2171 | } else { |
| 2172 | ALOGW("Ignoring gapless playback because next player has already started"); |
| 2173 | // remove track in case resource needed for future players. |
| 2174 | if (mCallbackData != NULL) { |
| 2175 | mCallbackData->endTrackSwitch(); // release lock for callbacks before close. |
| 2176 | } |
| 2177 | close_l(); |
| 2178 | } |
| 2179 | } |
| 2180 | break; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
Wei Jia | 7d3f4df | 2015-03-03 15:28:00 -0800 | [diff] [blame] | 2184 | 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] | 2185 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2186 | Mutex::Autolock lock(mLock); |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2187 | 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] | 2188 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2189 | //ALOGV("write(%p, %u)", buffer, size); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2190 | if (mTrack != 0) { |
Andy Hung | 2f6e73d | 2016-04-08 12:12:58 -0700 | [diff] [blame] | 2191 | return mTrack->write(buffer, size, blocking); |
Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 2192 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2193 | return NO_INIT; |
| 2194 | } |
| 2195 | |
| 2196 | void MediaPlayerService::AudioOutput::stop() |
| 2197 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2198 | ALOGV("stop"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2199 | Mutex::Autolock lock(mLock); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2200 | if (mTrack != 0) mTrack->stop(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | void MediaPlayerService::AudioOutput::flush() |
| 2204 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2205 | ALOGV("flush"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2206 | Mutex::Autolock lock(mLock); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2207 | if (mTrack != 0) mTrack->flush(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | void MediaPlayerService::AudioOutput::pause() |
| 2211 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2212 | ALOGV("pause"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2213 | Mutex::Autolock lock(mLock); |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2214 | if (mTrack != 0) mTrack->pause(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | void MediaPlayerService::AudioOutput::close() |
| 2218 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2219 | ALOGV("close"); |
Erik Wolsheimer | 7845a1f | 2015-10-30 12:07:52 -0700 | [diff] [blame] | 2220 | sp<AudioTrack> track; |
| 2221 | { |
| 2222 | Mutex::Autolock lock(mLock); |
| 2223 | track = mTrack; |
| 2224 | close_l(); // clears mTrack |
| 2225 | } |
| 2226 | // destruction of the track occurs outside of mutex. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | void MediaPlayerService::AudioOutput::setVolume(float left, float right) |
| 2230 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2231 | ALOGV("setVolume(%f, %f)", left, right); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2232 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2233 | mLeftVolume = left; |
| 2234 | mRightVolume = right; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2235 | if (mTrack != 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2236 | mTrack->setVolume(left, right); |
| 2237 | } |
| 2238 | } |
| 2239 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2240 | status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate) |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2241 | { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2242 | ALOGV("setPlaybackRate(%f %f %d %d)", |
| 2243 | rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2244 | Mutex::Autolock lock(mLock); |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2245 | if (mTrack == 0) { |
| 2246 | // remember rate so that we can set it when the track is opened |
| 2247 | mPlaybackRate = rate; |
| 2248 | return OK; |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2249 | } |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2250 | status_t res = mTrack->setPlaybackRate(rate); |
| 2251 | if (res != NO_ERROR) { |
| 2252 | return res; |
| 2253 | } |
| 2254 | // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded |
| 2255 | CHECK_GT(rate.mSpeed, 0.f); |
| 2256 | mPlaybackRate = rate; |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2257 | if (mSampleRateHz != 0) { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2258 | mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz); |
Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2259 | } |
| 2260 | return res; |
| 2261 | } |
| 2262 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2263 | status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate) |
| 2264 | { |
| 2265 | ALOGV("setPlaybackRate"); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2266 | Mutex::Autolock lock(mLock); |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2267 | if (mTrack == 0) { |
| 2268 | return NO_INIT; |
| 2269 | } |
| 2270 | *rate = mTrack->getPlaybackRate(); |
| 2271 | return NO_ERROR; |
| 2272 | } |
| 2273 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2274 | status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level) |
| 2275 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2276 | ALOGV("setAuxEffectSendLevel(%f)", level); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2277 | Mutex::Autolock lock(mLock); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2278 | mSendLevel = level; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2279 | if (mTrack != 0) { |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2280 | return mTrack->setAuxEffectSendLevel(level); |
| 2281 | } |
| 2282 | return NO_ERROR; |
| 2283 | } |
| 2284 | |
| 2285 | status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId) |
| 2286 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2287 | ALOGV("attachAuxEffect(%d)", effectId); |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2288 | Mutex::Autolock lock(mLock); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2289 | mAuxEffectId = effectId; |
Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2290 | if (mTrack != 0) { |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2291 | return mTrack->attachAuxEffect(effectId); |
| 2292 | } |
| 2293 | return NO_ERROR; |
| 2294 | } |
| 2295 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2296 | VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper( |
| 2297 | const sp<VolumeShaper::Configuration>& configuration, |
| 2298 | const sp<VolumeShaper::Operation>& operation) |
| 2299 | { |
| 2300 | Mutex::Autolock lock(mLock); |
| 2301 | ALOGV("AudioOutput::applyVolumeShaper"); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2302 | |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2303 | mVolumeHandler->setIdIfNecessary(configuration); |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2304 | |
| 2305 | VolumeShaper::Status status; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2306 | if (mTrack != 0) { |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2307 | status = mTrack->applyVolumeShaper(configuration, operation); |
| 2308 | if (status >= 0) { |
| 2309 | (void)mVolumeHandler->applyVolumeShaper(configuration, operation); |
Andy Hung | f370264 | 2017-05-05 17:33:32 -0700 | [diff] [blame] | 2310 | if (mTrack->isPlaying()) { // match local AudioTrack to properly restore. |
| 2311 | mVolumeHandler->setStarted(); |
| 2312 | } |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2313 | } |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2314 | } else { |
Andy Hung | c01eddb | 2017-07-21 16:42:41 -0700 | [diff] [blame] | 2315 | // VolumeShapers are not affected when a track moves between players for |
| 2316 | // gapless playback (setNextMediaPlayer). |
| 2317 | // We forward VolumeShaper operations that do not change configuration |
| 2318 | // to the new player so that unducking may occur as expected. |
| 2319 | // Unducking is an idempotent operation, same if applied back-to-back. |
| 2320 | if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID |
| 2321 | && mNextOutput != nullptr) { |
| 2322 | ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s", |
| 2323 | configuration->toString().c_str(), operation->toString().c_str()); |
| 2324 | Mutex::Autolock nextLock(mNextOutput->mLock); |
| 2325 | |
| 2326 | // recycled track should be forwarded from this AudioSink by switchToNextOutput |
| 2327 | sp<AudioTrack> track = mNextOutput->mRecycledTrack; |
| 2328 | if (track != nullptr) { |
| 2329 | ALOGD("Forward VolumeShaper operation to recycled track %p", track.get()); |
| 2330 | (void)track->applyVolumeShaper(configuration, operation); |
| 2331 | } else { |
| 2332 | // There is a small chance that the unduck occurs after the next |
| 2333 | // player has already started, but before it is registered to receive |
| 2334 | // the unduck command. |
| 2335 | track = mNextOutput->mTrack; |
| 2336 | if (track != nullptr) { |
| 2337 | ALOGD("Forward VolumeShaper operation to track %p", track.get()); |
| 2338 | (void)track->applyVolumeShaper(configuration, operation); |
| 2339 | } |
| 2340 | } |
| 2341 | } |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2342 | status = mVolumeHandler->applyVolumeShaper(configuration, operation); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2343 | } |
Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2344 | return status; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id) |
| 2348 | { |
| 2349 | Mutex::Autolock lock(mLock); |
| 2350 | if (mTrack != 0) { |
| 2351 | return mTrack->getVolumeShaperState(id); |
Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2352 | } else { |
| 2353 | return mVolumeHandler->getVolumeShaperState(id); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2354 | } |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2355 | } |
| 2356 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2357 | // static |
| 2358 | void MediaPlayerService::AudioOutput::CallbackWrapper( |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 2359 | int event, void *cookie, void *info) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2360 | //ALOGV("callbackwrapper"); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2361 | CallbackData *data = (CallbackData*)cookie; |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2362 | // lock to ensure we aren't caught in the middle of a track switch. |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2363 | data->lock(); |
| 2364 | AudioOutput *me = data->getOutput(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2365 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2366 | if (me == NULL) { |
| 2367 | // no output set, likely because the track was scheduled to be reused |
| 2368 | // by another player, but the format turned out to be incompatible. |
| 2369 | data->unlock(); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2370 | if (buffer != NULL) { |
| 2371 | buffer->size = 0; |
| 2372 | } |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2373 | return; |
| 2374 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2375 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2376 | switch(event) { |
| 2377 | case AudioTrack::EVENT_MORE_DATA: { |
| 2378 | size_t actualSize = (*me->mCallback)( |
| 2379 | me, buffer->raw, buffer->size, me->mCallbackCookie, |
| 2380 | CB_EVENT_FILL_BUFFER); |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2381 | |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2382 | // Log when no data is returned from the callback. |
| 2383 | // (1) We may have no data (especially with network streaming sources). |
| 2384 | // (2) We may have reached the EOS and the audio track is not stopped yet. |
| 2385 | // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS. |
| 2386 | // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill). |
| 2387 | // |
| 2388 | // This is a benign busy-wait, with the next data request generated 10 ms or more later; |
| 2389 | // nevertheless for power reasons, we don't want to see too many of these. |
Andreas Huber | 2e8ffaf | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 2390 | |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2391 | ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned"); |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2392 | |
| 2393 | buffer->size = actualSize; |
| 2394 | } break; |
| 2395 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2396 | case AudioTrack::EVENT_STREAM_END: |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2397 | // currently only occurs for offloaded callbacks |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2398 | ALOGV("callbackwrapper: deliver EVENT_STREAM_END"); |
| 2399 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 2400 | me->mCallbackCookie, CB_EVENT_STREAM_END); |
| 2401 | break; |
| 2402 | |
| 2403 | case AudioTrack::EVENT_NEW_IAUDIOTRACK : |
| 2404 | ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN"); |
| 2405 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 2406 | me->mCallbackCookie, CB_EVENT_TEAR_DOWN); |
| 2407 | break; |
| 2408 | |
Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2409 | case AudioTrack::EVENT_UNDERRUN: |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2410 | // This occurs when there is no data available, typically |
Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2411 | // when there is a failure to supply data to the AudioTrack. It can also |
| 2412 | // occur in non-offloaded mode when the audio device comes out of standby. |
| 2413 | // |
Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2414 | // If an AudioTrack underruns it outputs silence. Since this happens suddenly |
| 2415 | // it may sound like an audible pop or glitch. |
| 2416 | // |
| 2417 | // The underrun event is sent once per track underrun; the condition is reset |
| 2418 | // when more data is sent to the AudioTrack. |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2419 | ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)"); |
Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2420 | break; |
| 2421 | |
Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2422 | default: |
| 2423 | ALOGE("received unknown event type: %d inside CallbackWrapper !", event); |
Andreas Huber | 51c1e0e | 2011-04-04 11:43:40 -0700 | [diff] [blame] | 2424 | } |
| 2425 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2426 | data->unlock(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2429 | audio_session_t MediaPlayerService::AudioOutput::getSessionId() const |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 2430 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2431 | Mutex::Autolock lock(mLock); |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 2432 | return mSessionId; |
| 2433 | } |
| 2434 | |
Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 2435 | uint32_t MediaPlayerService::AudioOutput::getSampleRate() const |
| 2436 | { |
Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2437 | Mutex::Autolock lock(mLock); |
Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 2438 | if (mTrack == 0) return 0; |
| 2439 | return mTrack->getSampleRate(); |
| 2440 | } |
| 2441 | |
Andy Hung | f2c87b3 | 2016-04-07 19:49:29 -0700 | [diff] [blame] | 2442 | int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const |
| 2443 | { |
| 2444 | Mutex::Autolock lock(mLock); |
| 2445 | if (mTrack == 0) { |
| 2446 | return 0; |
| 2447 | } |
| 2448 | int64_t duration; |
| 2449 | if (mTrack->getBufferDurationInUs(&duration) != OK) { |
| 2450 | return 0; |
| 2451 | } |
| 2452 | return duration; |
| 2453 | } |
| 2454 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2455 | //////////////////////////////////////////////////////////////////////////////// |
| 2456 | |
| 2457 | struct CallbackThread : public Thread { |
| 2458 | CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink, |
| 2459 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 2460 | void *cookie); |
| 2461 | |
| 2462 | protected: |
| 2463 | virtual ~CallbackThread(); |
| 2464 | |
| 2465 | virtual bool threadLoop(); |
| 2466 | |
| 2467 | private: |
| 2468 | wp<MediaPlayerBase::AudioSink> mSink; |
| 2469 | MediaPlayerBase::AudioSink::AudioCallback mCallback; |
| 2470 | void *mCookie; |
| 2471 | void *mBuffer; |
| 2472 | size_t mBufferSize; |
| 2473 | |
| 2474 | CallbackThread(const CallbackThread &); |
| 2475 | CallbackThread &operator=(const CallbackThread &); |
| 2476 | }; |
| 2477 | |
| 2478 | CallbackThread::CallbackThread( |
| 2479 | const wp<MediaPlayerBase::AudioSink> &sink, |
| 2480 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 2481 | void *cookie) |
| 2482 | : mSink(sink), |
| 2483 | mCallback(cb), |
| 2484 | mCookie(cookie), |
| 2485 | mBuffer(NULL), |
| 2486 | mBufferSize(0) { |
| 2487 | } |
| 2488 | |
| 2489 | CallbackThread::~CallbackThread() { |
| 2490 | if (mBuffer) { |
| 2491 | free(mBuffer); |
| 2492 | mBuffer = NULL; |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | bool CallbackThread::threadLoop() { |
| 2497 | sp<MediaPlayerBase::AudioSink> sink = mSink.promote(); |
| 2498 | if (sink == NULL) { |
| 2499 | return false; |
| 2500 | } |
| 2501 | |
| 2502 | if (mBuffer == NULL) { |
| 2503 | mBufferSize = sink->bufferSize(); |
| 2504 | mBuffer = malloc(mBufferSize); |
| 2505 | } |
| 2506 | |
| 2507 | size_t actualSize = |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 2508 | (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie, |
| 2509 | MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER); |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2510 | |
| 2511 | if (actualSize > 0) { |
| 2512 | sink->write(mBuffer, actualSize); |
Andy Hung | a31335a | 2014-08-20 17:37:59 -0700 | [diff] [blame] | 2513 | // Could return false on sink->write() error or short count. |
| 2514 | // Not necessarily appropriate but would work for AudioCache behavior. |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | return true; |
| 2518 | } |
| 2519 | |
| 2520 | //////////////////////////////////////////////////////////////////////////////// |
| 2521 | |
Chong Zhang | e5b9b69 | 2017-05-11 11:44:56 -0700 | [diff] [blame] | 2522 | void MediaPlayerService::addBatteryData(uint32_t params) { |
| 2523 | mBatteryTracker.addBatteryData(params); |
| 2524 | } |
| 2525 | |
| 2526 | status_t MediaPlayerService::pullBatteryData(Parcel* reply) { |
| 2527 | return mBatteryTracker.pullBatteryData(reply); |
| 2528 | } |
| 2529 | |
| 2530 | MediaPlayerService::BatteryTracker::BatteryTracker() { |
| 2531 | mBatteryAudio.refCount = 0; |
| 2532 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2533 | mBatteryAudio.deviceOn[i] = 0; |
| 2534 | mBatteryAudio.lastTime[i] = 0; |
| 2535 | mBatteryAudio.totalTime[i] = 0; |
| 2536 | } |
| 2537 | // speaker is on by default |
| 2538 | mBatteryAudio.deviceOn[SPEAKER] = 1; |
| 2539 | |
| 2540 | // reset battery stats |
| 2541 | // if the mediaserver has crashed, battery stats could be left |
| 2542 | // in bad state, reset the state upon service start. |
| 2543 | BatteryNotifier::getInstance().noteResetVideo(); |
| 2544 | } |
| 2545 | |
| 2546 | void MediaPlayerService::BatteryTracker::addBatteryData(uint32_t params) |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2547 | { |
| 2548 | Mutex::Autolock lock(mLock); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2549 | |
| 2550 | int32_t time = systemTime() / 1000000L; |
| 2551 | |
| 2552 | // change audio output devices. This notification comes from AudioFlinger |
| 2553 | if ((params & kBatteryDataSpeakerOn) |
| 2554 | || (params & kBatteryDataOtherAudioDeviceOn)) { |
| 2555 | |
| 2556 | int deviceOn[NUM_AUDIO_DEVICES]; |
| 2557 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2558 | deviceOn[i] = 0; |
| 2559 | } |
| 2560 | |
| 2561 | if ((params & kBatteryDataSpeakerOn) |
| 2562 | && (params & kBatteryDataOtherAudioDeviceOn)) { |
| 2563 | deviceOn[SPEAKER_AND_OTHER] = 1; |
| 2564 | } else if (params & kBatteryDataSpeakerOn) { |
| 2565 | deviceOn[SPEAKER] = 1; |
| 2566 | } else { |
| 2567 | deviceOn[OTHER_AUDIO_DEVICE] = 1; |
| 2568 | } |
| 2569 | |
| 2570 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2571 | if (mBatteryAudio.deviceOn[i] != deviceOn[i]){ |
| 2572 | |
| 2573 | if (mBatteryAudio.refCount > 0) { // if playing audio |
| 2574 | if (!deviceOn[i]) { |
| 2575 | mBatteryAudio.lastTime[i] += time; |
| 2576 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 2577 | mBatteryAudio.lastTime[i] = 0; |
| 2578 | } else { |
| 2579 | mBatteryAudio.lastTime[i] = 0 - time; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | mBatteryAudio.deviceOn[i] = deviceOn[i]; |
| 2584 | } |
| 2585 | } |
| 2586 | return; |
| 2587 | } |
| 2588 | |
Marco Nelissen | b7848f1 | 2014-12-04 08:57:56 -0800 | [diff] [blame] | 2589 | // an audio stream is started |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2590 | if (params & kBatteryDataAudioFlingerStart) { |
| 2591 | // record the start time only if currently no other audio |
| 2592 | // is being played |
| 2593 | if (mBatteryAudio.refCount == 0) { |
| 2594 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2595 | if (mBatteryAudio.deviceOn[i]) { |
| 2596 | mBatteryAudio.lastTime[i] -= time; |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | mBatteryAudio.refCount ++; |
| 2602 | return; |
| 2603 | |
| 2604 | } else if (params & kBatteryDataAudioFlingerStop) { |
| 2605 | if (mBatteryAudio.refCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2606 | ALOGW("Battery track warning: refCount is <= 0"); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2607 | return; |
| 2608 | } |
| 2609 | |
| 2610 | // record the stop time only if currently this is the only |
| 2611 | // audio being played |
| 2612 | if (mBatteryAudio.refCount == 1) { |
| 2613 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2614 | if (mBatteryAudio.deviceOn[i]) { |
| 2615 | mBatteryAudio.lastTime[i] += time; |
| 2616 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 2617 | mBatteryAudio.lastTime[i] = 0; |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | mBatteryAudio.refCount --; |
| 2623 | return; |
| 2624 | } |
| 2625 | |
Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 2626 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2627 | if (uid == AID_MEDIA) { |
| 2628 | return; |
| 2629 | } |
| 2630 | int index = mBatteryData.indexOfKey(uid); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2631 | |
| 2632 | if (index < 0) { // create a new entry for this UID |
| 2633 | BatteryUsageInfo info; |
| 2634 | info.audioTotalTime = 0; |
| 2635 | info.videoTotalTime = 0; |
| 2636 | info.audioLastTime = 0; |
| 2637 | info.videoLastTime = 0; |
| 2638 | info.refCount = 0; |
| 2639 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2640 | if (mBatteryData.add(uid, info) == NO_MEMORY) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2641 | ALOGE("Battery track error: no memory for new app"); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2642 | return; |
| 2643 | } |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | BatteryUsageInfo &info = mBatteryData.editValueFor(uid); |
| 2647 | |
| 2648 | if (params & kBatteryDataCodecStarted) { |
| 2649 | if (params & kBatteryDataTrackAudio) { |
| 2650 | info.audioLastTime -= time; |
| 2651 | info.refCount ++; |
| 2652 | } |
| 2653 | if (params & kBatteryDataTrackVideo) { |
| 2654 | info.videoLastTime -= time; |
| 2655 | info.refCount ++; |
| 2656 | } |
| 2657 | } else { |
| 2658 | if (info.refCount == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2659 | ALOGW("Battery track warning: refCount is already 0"); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2660 | return; |
| 2661 | } else if (info.refCount < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2662 | ALOGE("Battery track error: refCount < 0"); |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2663 | mBatteryData.removeItem(uid); |
| 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | if (params & kBatteryDataTrackAudio) { |
| 2668 | info.audioLastTime += time; |
| 2669 | info.refCount --; |
| 2670 | } |
| 2671 | if (params & kBatteryDataTrackVideo) { |
| 2672 | info.videoLastTime += time; |
| 2673 | info.refCount --; |
| 2674 | } |
| 2675 | |
| 2676 | // no stream is being played by this UID |
| 2677 | if (info.refCount == 0) { |
| 2678 | info.audioTotalTime += info.audioLastTime; |
| 2679 | info.audioLastTime = 0; |
| 2680 | info.videoTotalTime += info.videoLastTime; |
| 2681 | info.videoLastTime = 0; |
| 2682 | } |
| 2683 | } |
| 2684 | } |
| 2685 | |
Chong Zhang | e5b9b69 | 2017-05-11 11:44:56 -0700 | [diff] [blame] | 2686 | status_t MediaPlayerService::BatteryTracker::pullBatteryData(Parcel* reply) { |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2687 | Mutex::Autolock lock(mLock); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2688 | |
| 2689 | // audio output devices usage |
| 2690 | int32_t time = systemTime() / 1000000L; //in ms |
| 2691 | int32_t totalTime; |
| 2692 | |
| 2693 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2694 | totalTime = mBatteryAudio.totalTime[i]; |
| 2695 | |
| 2696 | if (mBatteryAudio.deviceOn[i] |
| 2697 | && (mBatteryAudio.lastTime[i] != 0)) { |
| 2698 | int32_t tmpTime = mBatteryAudio.lastTime[i] + time; |
| 2699 | totalTime += tmpTime; |
| 2700 | } |
| 2701 | |
| 2702 | reply->writeInt32(totalTime); |
| 2703 | // reset the total time |
| 2704 | mBatteryAudio.totalTime[i] = 0; |
| 2705 | } |
| 2706 | |
| 2707 | // codec usage |
Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2708 | BatteryUsageInfo info; |
| 2709 | int size = mBatteryData.size(); |
| 2710 | |
| 2711 | reply->writeInt32(size); |
| 2712 | int i = 0; |
| 2713 | |
| 2714 | while (i < size) { |
| 2715 | info = mBatteryData.valueAt(i); |
| 2716 | |
| 2717 | reply->writeInt32(mBatteryData.keyAt(i)); //UID |
| 2718 | reply->writeInt32(info.audioTotalTime); |
| 2719 | reply->writeInt32(info.videoTotalTime); |
| 2720 | |
| 2721 | info.audioTotalTime = 0; |
| 2722 | info.videoTotalTime = 0; |
| 2723 | |
| 2724 | // remove the UID entry where no stream is being played |
| 2725 | if (info.refCount <= 0) { |
| 2726 | mBatteryData.removeItemsAt(i); |
| 2727 | size --; |
| 2728 | i --; |
| 2729 | } |
| 2730 | i++; |
| 2731 | } |
| 2732 | return NO_ERROR; |
| 2733 | } |
niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 2734 | } // namespace android |