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