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