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