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