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