| 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); | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 585 | mAudioOutput.clear(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | wp<Client> client(this); | 
|  | 587 | disconnect(); | 
|  | 588 | mService->removeClient(client); | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 589 | if (mAudioAttributes != NULL) { | 
|  | 590 | free(mAudioAttributes); | 
|  | 591 | } | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 592 | clearDeathNotifiers_l(); | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 593 | mAudioDeviceUpdatedListener.clear(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | } | 
|  | 595 |  | 
|  | 596 | void MediaPlayerService::Client::disconnect() | 
|  | 597 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 598 | ALOGV("disconnect(%d) from pid %d", mConnId, mPid); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | // grab local reference and clear main reference to prevent future | 
|  | 600 | // access to object | 
|  | 601 | sp<MediaPlayerBase> p; | 
|  | 602 | { | 
|  | 603 | Mutex::Autolock l(mLock); | 
|  | 604 | p = mPlayer; | 
| beanz | dcfefde | 2012-11-05 09:51:43 +0800 | [diff] [blame] | 605 | mClient.clear(); | 
| Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 606 | mPlayer.clear(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | } | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 608 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | // clear the notification to prevent callbacks to dead client | 
|  | 610 | // and reset the player. We assume the player will serialize | 
|  | 611 | // access to itself if necessary. | 
|  | 612 | if (p != 0) { | 
|  | 613 | p->setNotifyCallback(0, 0); | 
|  | 614 | #if CALLBACK_ANTAGONIZER | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 615 | ALOGD("kill Antagonizer"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | mAntagonizer->kill(); | 
|  | 617 | #endif | 
|  | 618 | p->reset(); | 
|  | 619 | } | 
|  | 620 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 621 | { | 
|  | 622 | Mutex::Autolock l(mLock); | 
|  | 623 | disconnectNativeWindow_l(); | 
|  | 624 | } | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 625 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | IPCThreadState::self()->flushCommands(); | 
|  | 627 | } | 
|  | 628 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType) | 
|  | 630 | { | 
|  | 631 | // determine if we have the right player type | 
| Wei Jia | 502c2f4 | 2017-07-13 17:47:56 -0700 | [diff] [blame] | 632 | sp<MediaPlayerBase> p = getPlayer(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | if ((p != NULL) && (p->playerType() != playerType)) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 634 | ALOGV("delete player"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | p.clear(); | 
|  | 636 | } | 
|  | 637 | if (p == NULL) { | 
| Ronghua Wu | 68845c1 | 2015-07-21 09:50:48 -0700 | [diff] [blame] | 638 | p = MediaPlayerFactory::createPlayer(playerType, this, notify, mPid); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | } | 
| Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 640 |  | 
| Jason Simmons | db29e52 | 2011-08-12 13:46:55 -0700 | [diff] [blame] | 641 | if (p != NULL) { | 
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 642 | p->setUID(mUid); | 
| Jason Simmons | db29e52 | 2011-08-12 13:46:55 -0700 | [diff] [blame] | 643 | } | 
| Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 644 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | return p; | 
|  | 646 | } | 
|  | 647 |  | 
| Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 648 | MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier( | 
|  | 649 | const sp<IBinder>& service, | 
|  | 650 | const sp<MediaPlayerBase>& listener, | 
|  | 651 | int which) { | 
|  | 652 | mService = service; | 
| Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 653 | mOmx = nullptr; | 
|  | 654 | mListener = listener; | 
|  | 655 | mWhich = which; | 
|  | 656 | } | 
|  | 657 |  | 
|  | 658 | MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier( | 
|  | 659 | const sp<IOmx>& omx, | 
|  | 660 | const sp<MediaPlayerBase>& listener, | 
|  | 661 | int which) { | 
|  | 662 | mService = nullptr; | 
|  | 663 | mOmx = omx; | 
| Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 664 | mListener = listener; | 
|  | 665 | mWhich = which; | 
|  | 666 | } | 
|  | 667 |  | 
|  | 668 | MediaPlayerService::Client::ServiceDeathNotifier::~ServiceDeathNotifier() { | 
| Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 669 | } | 
|  | 670 |  | 
|  | 671 | void MediaPlayerService::Client::ServiceDeathNotifier::binderDied(const wp<IBinder>& /*who*/) { | 
|  | 672 | sp<MediaPlayerBase> listener = mListener.promote(); | 
|  | 673 | if (listener != NULL) { | 
|  | 674 | listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich); | 
|  | 675 | } else { | 
|  | 676 | ALOGW("listener for process %d death is gone", mWhich); | 
|  | 677 | } | 
|  | 678 | } | 
|  | 679 |  | 
| Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 680 | void MediaPlayerService::Client::ServiceDeathNotifier::serviceDied( | 
|  | 681 | uint64_t /* cookie */, | 
|  | 682 | const wp<::android::hidl::base::V1_0::IBase>& /* who */) { | 
|  | 683 | sp<MediaPlayerBase> listener = mListener.promote(); | 
|  | 684 | if (listener != NULL) { | 
|  | 685 | listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich); | 
|  | 686 | } else { | 
|  | 687 | ALOGW("listener for process %d death is gone", mWhich); | 
|  | 688 | } | 
|  | 689 | } | 
|  | 690 |  | 
|  | 691 | void MediaPlayerService::Client::ServiceDeathNotifier::unlinkToDeath() { | 
|  | 692 | if (mService != nullptr) { | 
|  | 693 | mService->unlinkToDeath(this); | 
|  | 694 | mService = nullptr; | 
|  | 695 | } else if (mOmx != nullptr) { | 
|  | 696 | mOmx->unlinkToDeath(this); | 
|  | 697 | mOmx = nullptr; | 
|  | 698 | } | 
|  | 699 | } | 
|  | 700 |  | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 701 | void MediaPlayerService::Client::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate( | 
|  | 702 | audio_io_handle_t audioIo, | 
|  | 703 | audio_port_handle_t deviceId) { | 
|  | 704 | sp<MediaPlayerBase> listener = mListener.promote(); | 
|  | 705 | if (listener != NULL) { | 
|  | 706 | listener->sendEvent(MEDIA_AUDIO_ROUTING_CHANGED, audioIo, deviceId); | 
|  | 707 | } else { | 
|  | 708 | ALOGW("listener for process %d death is gone", MEDIA_AUDIO_ROUTING_CHANGED); | 
|  | 709 | } | 
|  | 710 | } | 
|  | 711 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 712 | void MediaPlayerService::Client::clearDeathNotifiers_l() { | 
| Pawin Vongmasa | 9c47c97 | 2017-02-08 04:09:38 -0800 | [diff] [blame] | 713 | if (mExtractorDeathListener != nullptr) { | 
|  | 714 | mExtractorDeathListener->unlinkToDeath(); | 
|  | 715 | mExtractorDeathListener = nullptr; | 
|  | 716 | } | 
|  | 717 | if (mCodecDeathListener != nullptr) { | 
|  | 718 | mCodecDeathListener->unlinkToDeath(); | 
|  | 719 | mCodecDeathListener = nullptr; | 
|  | 720 | } | 
|  | 721 | } | 
|  | 722 |  | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 723 | sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre( | 
|  | 724 | player_type playerType) | 
|  | 725 | { | 
|  | 726 | ALOGV("player type = %d", playerType); | 
|  | 727 |  | 
|  | 728 | // create the right type of player | 
|  | 729 | sp<MediaPlayerBase> p = createPlayer(playerType); | 
|  | 730 | if (p == NULL) { | 
|  | 731 | return p; | 
|  | 732 | } | 
|  | 733 |  | 
| Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 734 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 735 | sp<IBinder> binder = sm->getService(String16("media.extractor")); | 
| Marco Nelissen | 1b5a7ee | 2016-09-30 13:54:30 -0700 | [diff] [blame] | 736 | if (binder == NULL) { | 
|  | 737 | ALOGE("extractor service not available"); | 
|  | 738 | return NULL; | 
|  | 739 | } | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 740 | sp<ServiceDeathNotifier> extractorDeathListener = | 
|  | 741 | new ServiceDeathNotifier(binder, p, MEDIAEXTRACTOR_PROCESS_DEATH); | 
|  | 742 | binder->linkToDeath(extractorDeathListener); | 
| Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 743 |  | 
| Pawin Vongmasa | 620e466 | 2017-10-27 00:12:47 -0700 | [diff] [blame] | 744 | sp<IOmx> omx = IOmx::getService(); | 
|  | 745 | if (omx == nullptr) { | 
|  | 746 | ALOGE("IOmx service is not available"); | 
|  | 747 | return NULL; | 
| Marco Nelissen | 1b5a7ee | 2016-09-30 13:54:30 -0700 | [diff] [blame] | 748 | } | 
| Pawin Vongmasa | 620e466 | 2017-10-27 00:12:47 -0700 | [diff] [blame] | 749 | sp<ServiceDeathNotifier> codecDeathListener = | 
|  | 750 | new ServiceDeathNotifier(omx, p, MEDIACODEC_PROCESS_DEATH); | 
|  | 751 | omx->linkToDeath(codecDeathListener, 0); | 
| Marco Nelissen | 6dc3a3e | 2016-04-29 15:42:06 -0700 | [diff] [blame] | 752 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 753 | Mutex::Autolock lock(mLock); | 
|  | 754 |  | 
|  | 755 | clearDeathNotifiers_l(); | 
|  | 756 | mExtractorDeathListener = extractorDeathListener; | 
|  | 757 | mCodecDeathListener = codecDeathListener; | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 758 | mAudioDeviceUpdatedListener = new AudioDeviceUpdatedNotifier(p); | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 759 |  | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 760 | if (!p->hardwareOutput()) { | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 761 | mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(), | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 762 | mPid, mAudioAttributes, mAudioDeviceUpdatedListener); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 763 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); | 
|  | 764 | } | 
|  | 765 |  | 
|  | 766 | return p; | 
|  | 767 | } | 
|  | 768 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 769 | status_t MediaPlayerService::Client::setDataSource_post( | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 770 | const sp<MediaPlayerBase>& p, | 
|  | 771 | status_t status) | 
|  | 772 | { | 
|  | 773 | ALOGV(" setDataSource"); | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 774 | if (status != OK) { | 
|  | 775 | ALOGE("  error: %d", status); | 
|  | 776 | return status; | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 777 | } | 
|  | 778 |  | 
|  | 779 | // Set the re-transmission endpoint if one was chosen. | 
|  | 780 | if (mRetransmitEndpointValid) { | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 781 | status = p->setRetransmitEndpoint(&mRetransmitEndpoint); | 
|  | 782 | if (status != NO_ERROR) { | 
|  | 783 | ALOGE("setRetransmitEndpoint error: %d", status); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 784 | } | 
|  | 785 | } | 
|  | 786 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 787 | if (status == OK) { | 
|  | 788 | Mutex::Autolock lock(mLock); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 789 | mPlayer = p; | 
|  | 790 | } | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 791 | return status; | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 792 | } | 
|  | 793 |  | 
| Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 794 | status_t MediaPlayerService::Client::setDataSource( | 
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 795 | const sp<IMediaHTTPService> &httpService, | 
|  | 796 | const char *url, | 
|  | 797 | const KeyedVector<String8, String8> *headers) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 799 | ALOGV("setDataSource(%s)", url); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | if (url == NULL) | 
|  | 801 | return UNKNOWN_ERROR; | 
|  | 802 |  | 
| Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 803 | if ((strncmp(url, "http://", 7) == 0) || | 
|  | 804 | (strncmp(url, "https://", 8) == 0) || | 
|  | 805 | (strncmp(url, "rtsp://", 7) == 0)) { | 
|  | 806 | if (!checkPermission("android.permission.INTERNET")) { | 
|  | 807 | return PERMISSION_DENIED; | 
|  | 808 | } | 
|  | 809 | } | 
|  | 810 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | if (strncmp(url, "content://", 10) == 0) { | 
|  | 812 | // get a filedescriptor for the content Uri and | 
|  | 813 | // pass it to the setDataSource(fd) method | 
|  | 814 |  | 
|  | 815 | String16 url16(url); | 
|  | 816 | int fd = android::openContentProviderFile(url16); | 
|  | 817 | if (fd < 0) | 
|  | 818 | { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 819 | ALOGE("Couldn't open fd for %s", url); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | return UNKNOWN_ERROR; | 
|  | 821 | } | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 822 | status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | close(fd); | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 824 | return mStatus = status; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | } else { | 
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 826 | player_type playerType = MediaPlayerFactory::getPlayerType(this, url); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 827 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); | 
|  | 828 | if (p == NULL) { | 
|  | 829 | return NO_INIT; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 830 | } | 
|  | 831 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 832 | return mStatus = | 
|  | 833 | setDataSource_post( | 
|  | 834 | p, p->setDataSource(httpService, url, headers)); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | } | 
|  | 836 | } | 
|  | 837 |  | 
|  | 838 | status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length) | 
|  | 839 | { | 
| Marco Nelissen | 83b0fd9 | 2015-09-16 13:48:07 -0700 | [diff] [blame] | 840 | ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld", | 
|  | 841 | 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] | 842 | struct stat sb; | 
|  | 843 | int ret = fstat(fd, &sb); | 
|  | 844 | if (ret != 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 845 | 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] | 846 | return UNKNOWN_ERROR; | 
|  | 847 | } | 
|  | 848 |  | 
| Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 849 | ALOGV("st_dev  = %llu", static_cast<unsigned long long>(sb.st_dev)); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 850 | ALOGV("st_mode = %u", sb.st_mode); | 
| Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 851 | ALOGV("st_uid  = %lu", static_cast<unsigned long>(sb.st_uid)); | 
|  | 852 | ALOGV("st_gid  = %lu", static_cast<unsigned long>(sb.st_gid)); | 
| Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 853 | 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] | 854 |  | 
|  | 855 | if (offset >= sb.st_size) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 856 | ALOGE("offset error"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | return UNKNOWN_ERROR; | 
|  | 858 | } | 
|  | 859 | if (offset + length > sb.st_size) { | 
|  | 860 | length = sb.st_size - offset; | 
| Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 861 | ALOGV("calculated length = %lld", (long long)length); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | } | 
|  | 863 |  | 
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 864 | player_type playerType = MediaPlayerFactory::getPlayerType(this, | 
|  | 865 | fd, | 
|  | 866 | offset, | 
|  | 867 | length); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 868 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); | 
|  | 869 | if (p == NULL) { | 
|  | 870 | return NO_INIT; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | } | 
|  | 872 |  | 
|  | 873 | // now set data source | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 874 | return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length)); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | } | 
|  | 876 |  | 
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 877 | status_t MediaPlayerService::Client::setDataSource( | 
|  | 878 | const sp<IStreamSource> &source) { | 
|  | 879 | // create the right type of player | 
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 880 | player_type playerType = MediaPlayerFactory::getPlayerType(this, source); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 881 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); | 
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 882 | if (p == NULL) { | 
|  | 883 | return NO_INIT; | 
|  | 884 | } | 
|  | 885 |  | 
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 886 | // now set data source | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 887 | return mStatus = setDataSource_post(p, p->setDataSource(source)); | 
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 888 | } | 
|  | 889 |  | 
| Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 890 | status_t MediaPlayerService::Client::setDataSource( | 
|  | 891 | const sp<IDataSource> &source) { | 
| Dongwon Kang | d91dc5a | 2017-10-10 00:07:09 -0700 | [diff] [blame] | 892 | sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source); | 
| Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 893 | player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource); | 
|  | 894 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); | 
|  | 895 | if (p == NULL) { | 
|  | 896 | return NO_INIT; | 
|  | 897 | } | 
|  | 898 | // now set data source | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 899 | return mStatus = setDataSource_post(p, p->setDataSource(dataSource)); | 
| Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 900 | } | 
|  | 901 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 902 | void MediaPlayerService::Client::disconnectNativeWindow_l() { | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 903 | if (mConnectedWindow != NULL) { | 
| Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 904 | status_t err = nativeWindowDisconnect( | 
|  | 905 | mConnectedWindow.get(), "disconnectNativeWindow"); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 906 |  | 
|  | 907 | if (err != OK) { | 
| Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 908 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 909 | strerror(-err), err); | 
|  | 910 | } | 
|  | 911 | } | 
|  | 912 | mConnectedWindow.clear(); | 
|  | 913 | } | 
|  | 914 |  | 
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 915 | status_t MediaPlayerService::Client::setVideoSurfaceTexture( | 
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 916 | const sp<IGraphicBufferProducer>& bufferProducer) | 
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 917 | { | 
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 918 | ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get()); | 
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 919 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 920 | if (p == 0) return UNKNOWN_ERROR; | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 921 |  | 
| Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 922 | sp<IBinder> binder(IInterface::asBinder(bufferProducer)); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 923 | if (mConnectedWindowBinder == binder) { | 
|  | 924 | return OK; | 
|  | 925 | } | 
|  | 926 |  | 
|  | 927 | sp<ANativeWindow> anw; | 
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 928 | if (bufferProducer != NULL) { | 
| Marco Nelissen | ee08f7e | 2013-09-16 13:30:01 -0700 | [diff] [blame] | 929 | anw = new Surface(bufferProducer, true /* controlledByApp */); | 
| Chong Zhang | 181fd9b | 2017-02-16 15:53:03 -0800 | [diff] [blame] | 930 | status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture"); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 931 |  | 
|  | 932 | if (err != OK) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 933 | ALOGE("setVideoSurfaceTexture failed: %d", err); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 934 | // Note that we must do the reset before disconnecting from the ANW. | 
|  | 935 | // Otherwise queue/dequeue calls could be made on the disconnected | 
|  | 936 | // ANW, which may result in errors. | 
|  | 937 | reset(); | 
|  | 938 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 939 | Mutex::Autolock lock(mLock); | 
|  | 940 | disconnectNativeWindow_l(); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 941 |  | 
|  | 942 | return err; | 
|  | 943 | } | 
|  | 944 | } | 
|  | 945 |  | 
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 946 | // Note that we must set the player's new GraphicBufferProducer before | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 947 | // disconnecting the old one.  Otherwise queue/dequeue calls could be made | 
|  | 948 | // on the disconnected ANW, which may result in errors. | 
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 949 | status_t err = p->setVideoSurfaceTexture(bufferProducer); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 950 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 951 | mLock.lock(); | 
|  | 952 | disconnectNativeWindow_l(); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 953 |  | 
|  | 954 | if (err == OK) { | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 955 | mConnectedWindow = anw; | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 956 | mConnectedWindowBinder = binder; | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 957 | mLock.unlock(); | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 958 | } else { | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 959 | mLock.unlock(); | 
|  | 960 | status_t err = nativeWindowDisconnect( | 
|  | 961 | anw.get(), "disconnectNativeWindow"); | 
|  | 962 |  | 
|  | 963 | if (err != OK) { | 
|  | 964 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", | 
|  | 965 | strerror(-err), err); | 
|  | 966 | } | 
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 967 | } | 
|  | 968 |  | 
|  | 969 | return err; | 
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 970 | } | 
|  | 971 |  | 
| Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 972 | status_t MediaPlayerService::Client::invoke(const Parcel& request, | 
|  | 973 | Parcel *reply) | 
|  | 974 | { | 
|  | 975 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 976 | if (p == NULL) return UNKNOWN_ERROR; | 
|  | 977 | return p->invoke(request, reply); | 
|  | 978 | } | 
|  | 979 |  | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 980 | // This call doesn't need to access the native player. | 
|  | 981 | status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter) | 
|  | 982 | { | 
|  | 983 | status_t status; | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 984 | media::Metadata::Filter allow, drop; | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 985 |  | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 986 | if (unmarshallFilter(filter, &allow, &status) && | 
|  | 987 | unmarshallFilter(filter, &drop, &status)) { | 
|  | 988 | Mutex::Autolock lock(mLock); | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 989 |  | 
|  | 990 | mMetadataAllow = allow; | 
|  | 991 | mMetadataDrop = drop; | 
|  | 992 | } | 
|  | 993 | return status; | 
|  | 994 | } | 
|  | 995 |  | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 996 | status_t MediaPlayerService::Client::getMetadata( | 
| Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 997 | bool update_only, bool /*apply_filter*/, Parcel *reply) | 
| Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 998 | { | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 999 | sp<MediaPlayerBase> player = getPlayer(); | 
|  | 1000 | if (player == 0) return UNKNOWN_ERROR; | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1001 |  | 
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1002 | status_t status; | 
|  | 1003 | // Placeholder for the return code, updated by the caller. | 
|  | 1004 | reply->writeInt32(-1); | 
|  | 1005 |  | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1006 | media::Metadata::Filter ids; | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1007 |  | 
|  | 1008 | // We don't block notifications while we fetch the data. We clear | 
|  | 1009 | // mMetadataUpdated first so we don't lose notifications happening | 
|  | 1010 | // during the rest of this call. | 
|  | 1011 | { | 
|  | 1012 | Mutex::Autolock lock(mLock); | 
|  | 1013 | if (update_only) { | 
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1014 | ids = mMetadataUpdated; | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1015 | } | 
|  | 1016 | mMetadataUpdated.clear(); | 
|  | 1017 | } | 
| Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1018 |  | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1019 | media::Metadata metadata(reply); | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1020 |  | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1021 | metadata.appendHeader(); | 
|  | 1022 | status = player->getMetadata(ids, reply); | 
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1023 |  | 
|  | 1024 | if (status != OK) { | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1025 | metadata.resetParcel(); | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1026 | ALOGE("getMetadata failed %d", status); | 
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1027 | return status; | 
|  | 1028 | } | 
|  | 1029 |  | 
|  | 1030 | // FIXME: Implement filtering on the result. Not critical since | 
|  | 1031 | // filtering takes place on the update notifications already. This | 
|  | 1032 | // would be when all the metadata are fetch and a filter is set. | 
|  | 1033 |  | 
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1034 | // Everything is fine, update the metadata length. | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1035 | metadata.updateLength(); | 
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 1036 | return OK; | 
| Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 1037 | } | 
|  | 1038 |  | 
| Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1039 | status_t MediaPlayerService::Client::setBufferingSettings( | 
|  | 1040 | const BufferingSettings& buffering) | 
|  | 1041 | { | 
| Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 1042 | ALOGV("[%d] setBufferingSettings{%s}", | 
|  | 1043 | mConnId, buffering.toString().string()); | 
| Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1044 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1045 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1046 | return p->setBufferingSettings(buffering); | 
|  | 1047 | } | 
|  | 1048 |  | 
| Wei Jia | 9bb3803 | 2017-03-23 18:00:38 -0700 | [diff] [blame] | 1049 | status_t MediaPlayerService::Client::getBufferingSettings( | 
| Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1050 | BufferingSettings* buffering /* nonnull */) | 
|  | 1051 | { | 
|  | 1052 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1053 | // TODO: create mPlayer on demand. | 
|  | 1054 | if (p == 0) return UNKNOWN_ERROR; | 
| Wei Jia | 9bb3803 | 2017-03-23 18:00:38 -0700 | [diff] [blame] | 1055 | status_t ret = p->getBufferingSettings(buffering); | 
| Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1056 | if (ret == NO_ERROR) { | 
| Wei Jia | 9bb3803 | 2017-03-23 18:00:38 -0700 | [diff] [blame] | 1057 | ALOGV("[%d] getBufferingSettings{%s}", | 
| Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 1058 | mConnId, buffering->toString().string()); | 
| Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1059 | } else { | 
| Wei Jia | 9bb3803 | 2017-03-23 18:00:38 -0700 | [diff] [blame] | 1060 | ALOGE("[%d] getBufferingSettings returned %d", mConnId, ret); | 
| Wei Jia | d399e7e | 2016-10-26 15:49:11 -0700 | [diff] [blame] | 1061 | } | 
|  | 1062 | return ret; | 
|  | 1063 | } | 
|  | 1064 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | status_t MediaPlayerService::Client::prepareAsync() | 
|  | 1066 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1067 | ALOGV("[%d] prepareAsync", mConnId); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1069 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1070 | status_t ret = p->prepareAsync(); | 
|  | 1071 | #if CALLBACK_ANTAGONIZER | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1072 | ALOGD("start Antagonizer"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | if (ret == NO_ERROR) mAntagonizer->start(); | 
|  | 1074 | #endif | 
|  | 1075 | return ret; | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | status_t MediaPlayerService::Client::start() | 
|  | 1079 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1080 | ALOGV("[%d] start", mConnId); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1082 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1083 | p->setLooping(mLoop); | 
|  | 1084 | return p->start(); | 
|  | 1085 | } | 
|  | 1086 |  | 
|  | 1087 | status_t MediaPlayerService::Client::stop() | 
|  | 1088 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1089 | ALOGV("[%d] stop", mConnId); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1091 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1092 | return p->stop(); | 
|  | 1093 | } | 
|  | 1094 |  | 
|  | 1095 | status_t MediaPlayerService::Client::pause() | 
|  | 1096 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1097 | ALOGV("[%d] pause", mConnId); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1098 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1099 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1100 | return p->pause(); | 
|  | 1101 | } | 
|  | 1102 |  | 
|  | 1103 | status_t MediaPlayerService::Client::isPlaying(bool* state) | 
|  | 1104 | { | 
|  | 1105 | *state = false; | 
|  | 1106 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1107 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1108 | *state = p->isPlaying(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1109 | ALOGV("[%d] isPlaying: %d", mConnId, *state); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | return NO_ERROR; | 
|  | 1111 | } | 
|  | 1112 |  | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1113 | status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate) | 
| Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1114 | { | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1115 | ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)", | 
|  | 1116 | mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); | 
| Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1117 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1118 | if (p == 0) return UNKNOWN_ERROR; | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 1119 | return p->setPlaybackSettings(rate); | 
|  | 1120 | } | 
|  | 1121 |  | 
|  | 1122 | status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) | 
|  | 1123 | { | 
|  | 1124 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1125 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1126 | status_t ret = p->getPlaybackSettings(rate); | 
|  | 1127 | if (ret == NO_ERROR) { | 
|  | 1128 | ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)", | 
|  | 1129 | mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode); | 
|  | 1130 | } else { | 
|  | 1131 | ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret); | 
|  | 1132 | } | 
|  | 1133 | return ret; | 
|  | 1134 | } | 
|  | 1135 |  | 
|  | 1136 | status_t MediaPlayerService::Client::setSyncSettings( | 
|  | 1137 | const AVSyncSettings& sync, float videoFpsHint) | 
|  | 1138 | { | 
|  | 1139 | ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)", | 
|  | 1140 | mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint); | 
|  | 1141 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1142 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1143 | return p->setSyncSettings(sync, videoFpsHint); | 
|  | 1144 | } | 
|  | 1145 |  | 
|  | 1146 | status_t MediaPlayerService::Client::getSyncSettings( | 
|  | 1147 | AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) | 
|  | 1148 | { | 
|  | 1149 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1150 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1151 | status_t ret = p->getSyncSettings(sync, videoFps); | 
|  | 1152 | if (ret == NO_ERROR) { | 
|  | 1153 | ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)", | 
|  | 1154 | mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps); | 
|  | 1155 | } else { | 
|  | 1156 | ALOGV("[%d] getSyncSettings returned %d", mConnId, ret); | 
|  | 1157 | } | 
|  | 1158 | return ret; | 
| Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 1159 | } | 
|  | 1160 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | status_t MediaPlayerService::Client::getCurrentPosition(int *msec) | 
|  | 1162 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1163 | ALOGV("getCurrentPosition"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1165 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1166 | status_t ret = p->getCurrentPosition(msec); | 
|  | 1167 | if (ret == NO_ERROR) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1168 | ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1170 | ALOGE("getCurrentPosition returned %d", ret); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | } | 
|  | 1172 | return ret; | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | status_t MediaPlayerService::Client::getDuration(int *msec) | 
|  | 1176 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1177 | ALOGV("getDuration"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1179 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1180 | status_t ret = p->getDuration(msec); | 
|  | 1181 | if (ret == NO_ERROR) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1182 | ALOGV("[%d] getDuration = %d", mConnId, *msec); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1184 | ALOGE("getDuration returned %d", ret); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | } | 
|  | 1186 | return ret; | 
|  | 1187 | } | 
|  | 1188 |  | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1189 | status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) { | 
|  | 1190 | ALOGV("setNextPlayer"); | 
|  | 1191 | Mutex::Autolock l(mLock); | 
|  | 1192 | sp<Client> c = static_cast<Client*>(player.get()); | 
| Wei Jia | 2828412 | 2016-08-30 13:49:06 -0700 | [diff] [blame] | 1193 | if (c != NULL && !mService->hasClient(c)) { | 
| Robert Shih | ee0a0e3 | 2016-08-16 16:50:54 -0700 | [diff] [blame] | 1194 | return BAD_VALUE; | 
|  | 1195 | } | 
|  | 1196 |  | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1197 | mNextClient = c; | 
| John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 1198 |  | 
|  | 1199 | if (c != NULL) { | 
|  | 1200 | if (mAudioOutput != NULL) { | 
|  | 1201 | mAudioOutput->setNextOutput(c->mAudioOutput); | 
|  | 1202 | } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) { | 
|  | 1203 | ALOGE("no current audio output"); | 
|  | 1204 | } | 
|  | 1205 |  | 
|  | 1206 | if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) { | 
|  | 1207 | mPlayer->setNextPlayer(mNextClient->getPlayer()); | 
|  | 1208 | } | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1209 | } | 
| John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 1210 |  | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1211 | return OK; | 
|  | 1212 | } | 
|  | 1213 |  | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1214 | VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper( | 
|  | 1215 | const sp<VolumeShaper::Configuration>& configuration, | 
|  | 1216 | const sp<VolumeShaper::Operation>& operation) { | 
|  | 1217 | // for hardware output, call player instead | 
|  | 1218 | ALOGV("Client::applyVolumeShaper(%p)", this); | 
|  | 1219 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1220 | { | 
|  | 1221 | Mutex::Autolock l(mLock); | 
|  | 1222 | if (p != 0 && p->hardwareOutput()) { | 
|  | 1223 | // TODO: investigate internal implementation | 
|  | 1224 | return VolumeShaper::Status(INVALID_OPERATION); | 
|  | 1225 | } | 
|  | 1226 | if (mAudioOutput.get() != nullptr) { | 
|  | 1227 | return mAudioOutput->applyVolumeShaper(configuration, operation); | 
|  | 1228 | } | 
|  | 1229 | } | 
|  | 1230 | return VolumeShaper::Status(INVALID_OPERATION); | 
|  | 1231 | } | 
|  | 1232 |  | 
|  | 1233 | sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) { | 
|  | 1234 | // for hardware output, call player instead | 
|  | 1235 | ALOGV("Client::getVolumeShaperState(%p)", this); | 
|  | 1236 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1237 | { | 
|  | 1238 | Mutex::Autolock l(mLock); | 
|  | 1239 | if (p != 0 && p->hardwareOutput()) { | 
|  | 1240 | // TODO: investigate internal implementation. | 
|  | 1241 | return nullptr; | 
|  | 1242 | } | 
|  | 1243 | if (mAudioOutput.get() != nullptr) { | 
|  | 1244 | return mAudioOutput->getVolumeShaperState(id); | 
|  | 1245 | } | 
|  | 1246 | } | 
|  | 1247 | return nullptr; | 
|  | 1248 | } | 
|  | 1249 |  | 
| Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1250 | status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | { | 
| Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1252 | ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1254 | if (p == 0) return UNKNOWN_ERROR; | 
| Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 1255 | return p->seekTo(msec, mode); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | } | 
|  | 1257 |  | 
|  | 1258 | status_t MediaPlayerService::Client::reset() | 
|  | 1259 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1260 | ALOGV("[%d] reset", mConnId); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1261 | mRetransmitEndpointValid = false; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1263 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1264 | return p->reset(); | 
|  | 1265 | } | 
|  | 1266 |  | 
| Wei Jia | 52c2851 | 2017-09-13 18:17:51 -0700 | [diff] [blame] | 1267 | status_t MediaPlayerService::Client::notifyAt(int64_t mediaTimeUs) | 
|  | 1268 | { | 
|  | 1269 | ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs); | 
|  | 1270 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1271 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1272 | return p->notifyAt(mediaTimeUs); | 
|  | 1273 | } | 
|  | 1274 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1275 | 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] | 1276 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1277 | ALOGV("[%d] setAudioStreamType(%d)", mConnId, type); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | // TODO: for hardware output, call player instead | 
|  | 1279 | Mutex::Autolock l(mLock); | 
|  | 1280 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); | 
|  | 1281 | return NO_ERROR; | 
|  | 1282 | } | 
|  | 1283 |  | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1284 | status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel) | 
|  | 1285 | { | 
|  | 1286 | if (mAudioAttributes != NULL) { free(mAudioAttributes); } | 
|  | 1287 | mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); | 
| Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1288 | if (mAudioAttributes == NULL) { | 
|  | 1289 | return NO_MEMORY; | 
|  | 1290 | } | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1291 | unmarshallAudioAttributes(parcel, mAudioAttributes); | 
|  | 1292 |  | 
|  | 1293 | ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s", | 
|  | 1294 | mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags, | 
|  | 1295 | mAudioAttributes->tags); | 
|  | 1296 |  | 
|  | 1297 | if (mAudioOutput != 0) { | 
|  | 1298 | mAudioOutput->setAudioAttributes(mAudioAttributes); | 
|  | 1299 | } | 
|  | 1300 | return NO_ERROR; | 
|  | 1301 | } | 
|  | 1302 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | status_t MediaPlayerService::Client::setLooping(int loop) | 
|  | 1304 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1305 | ALOGV("[%d] setLooping(%d)", mConnId, loop); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | mLoop = loop; | 
|  | 1307 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1308 | if (p != 0) return p->setLooping(loop); | 
|  | 1309 | return NO_ERROR; | 
|  | 1310 | } | 
|  | 1311 |  | 
|  | 1312 | status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume) | 
|  | 1313 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1314 | ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); | 
| John Grossman | 761defc | 2012-02-09 15:09:05 -0800 | [diff] [blame] | 1315 |  | 
|  | 1316 | // for hardware output, call player instead | 
|  | 1317 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1318 | { | 
|  | 1319 | Mutex::Autolock l(mLock); | 
|  | 1320 | if (p != 0 && p->hardwareOutput()) { | 
|  | 1321 | MediaPlayerHWInterface* hwp = | 
|  | 1322 | reinterpret_cast<MediaPlayerHWInterface*>(p.get()); | 
|  | 1323 | return hwp->setVolume(leftVolume, rightVolume); | 
|  | 1324 | } else { | 
|  | 1325 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); | 
|  | 1326 | return NO_ERROR; | 
|  | 1327 | } | 
|  | 1328 | } | 
|  | 1329 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1330 | return NO_ERROR; | 
|  | 1331 | } | 
|  | 1332 |  | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1333 | status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level) | 
|  | 1334 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1335 | ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level); | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1336 | Mutex::Autolock l(mLock); | 
|  | 1337 | if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level); | 
|  | 1338 | return NO_ERROR; | 
|  | 1339 | } | 
|  | 1340 |  | 
|  | 1341 | status_t MediaPlayerService::Client::attachAuxEffect(int effectId) | 
|  | 1342 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1343 | ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId); | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1344 | Mutex::Autolock l(mLock); | 
|  | 1345 | if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId); | 
|  | 1346 | return NO_ERROR; | 
|  | 1347 | } | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1348 |  | 
| Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1349 | status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1350 | ALOGV("[%d] setParameter(%d)", mConnId, key); | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1351 | switch (key) { | 
|  | 1352 | case KEY_PARAMETER_AUDIO_ATTRIBUTES: | 
|  | 1353 | { | 
|  | 1354 | Mutex::Autolock l(mLock); | 
|  | 1355 | return setAudioAttributes_l(request); | 
|  | 1356 | } | 
|  | 1357 | default: | 
|  | 1358 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1359 | if (p == 0) { return UNKNOWN_ERROR; } | 
|  | 1360 | return p->setParameter(key, request); | 
|  | 1361 | } | 
| Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1362 | } | 
|  | 1363 |  | 
|  | 1364 | status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1365 | ALOGV("[%d] getParameter(%d)", mConnId, key); | 
| Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1366 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1367 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1368 | return p->getParameter(key, reply); | 
|  | 1369 | } | 
|  | 1370 |  | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1371 | status_t MediaPlayerService::Client::setRetransmitEndpoint( | 
|  | 1372 | const struct sockaddr_in* endpoint) { | 
|  | 1373 |  | 
|  | 1374 | if (NULL != endpoint) { | 
|  | 1375 | uint32_t a = ntohl(endpoint->sin_addr.s_addr); | 
|  | 1376 | uint16_t p = ntohs(endpoint->sin_port); | 
|  | 1377 | ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId, | 
|  | 1378 | (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p); | 
|  | 1379 | } else { | 
|  | 1380 | ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId); | 
|  | 1381 | } | 
|  | 1382 |  | 
|  | 1383 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1384 |  | 
|  | 1385 | // Right now, the only valid time to set a retransmit endpoint is before | 
|  | 1386 | // player selection has been made (since the presence or absence of a | 
|  | 1387 | // retransmit endpoint is going to determine which player is selected during | 
|  | 1388 | // setDataSource). | 
|  | 1389 | if (p != 0) return INVALID_OPERATION; | 
|  | 1390 |  | 
|  | 1391 | if (NULL != endpoint) { | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 1392 | Mutex::Autolock lock(mLock); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1393 | mRetransmitEndpoint = *endpoint; | 
|  | 1394 | mRetransmitEndpointValid = true; | 
|  | 1395 | } else { | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 1396 | Mutex::Autolock lock(mLock); | 
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1397 | mRetransmitEndpointValid = false; | 
|  | 1398 | } | 
|  | 1399 |  | 
|  | 1400 | return NO_ERROR; | 
|  | 1401 | } | 
|  | 1402 |  | 
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1403 | status_t MediaPlayerService::Client::getRetransmitEndpoint( | 
|  | 1404 | struct sockaddr_in* endpoint) | 
|  | 1405 | { | 
|  | 1406 | if (NULL == endpoint) | 
|  | 1407 | return BAD_VALUE; | 
|  | 1408 |  | 
|  | 1409 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1410 |  | 
|  | 1411 | if (p != NULL) | 
|  | 1412 | return p->getRetransmitEndpoint(endpoint); | 
|  | 1413 |  | 
| Pawin Vongmasa | f9c8146 | 2017-09-28 05:31:11 -0700 | [diff] [blame] | 1414 | Mutex::Autolock lock(mLock); | 
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1415 | if (!mRetransmitEndpointValid) | 
|  | 1416 | return NO_INIT; | 
|  | 1417 |  | 
|  | 1418 | *endpoint = mRetransmitEndpoint; | 
|  | 1419 |  | 
|  | 1420 | return NO_ERROR; | 
|  | 1421 | } | 
|  | 1422 |  | 
| Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 1423 | void MediaPlayerService::Client::notify( | 
| Pawin Vongmasa | d27ad6b | 2017-12-12 01:56:40 -0800 | [diff] [blame] | 1424 | const wp<IMediaPlayer> &listener, int msg, int ext1, int ext2, const Parcel *obj) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | { | 
| Pawin Vongmasa | d27ad6b | 2017-12-12 01:56:40 -0800 | [diff] [blame] | 1426 | sp<IMediaPlayer> spListener = listener.promote(); | 
|  | 1427 | if (spListener == NULL) { | 
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1428 | return; | 
|  | 1429 | } | 
| Pawin Vongmasa | d27ad6b | 2017-12-12 01:56:40 -0800 | [diff] [blame] | 1430 | Client* client = static_cast<Client*>(spListener.get()); | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1431 |  | 
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1432 | sp<IMediaPlayerClient> c; | 
| Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1433 | sp<Client> nextClient; | 
|  | 1434 | status_t errStartNext = NO_ERROR; | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1435 | { | 
|  | 1436 | Mutex::Autolock l(client->mLock); | 
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1437 | c = client->mClient; | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1438 | if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) { | 
| Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1439 | nextClient = client->mNextClient; | 
|  | 1440 |  | 
| John Grossman | cb0b755 | 2012-08-23 17:47:31 -0700 | [diff] [blame] | 1441 | if (client->mAudioOutput != NULL) | 
|  | 1442 | client->mAudioOutput->switchToNextOutput(); | 
| Haynes Mathew George | 6cb3375 | 2015-06-22 14:16:42 -0700 | [diff] [blame] | 1443 |  | 
|  | 1444 | errStartNext = nextClient->start(); | 
|  | 1445 | } | 
|  | 1446 | } | 
|  | 1447 |  | 
|  | 1448 | if (nextClient != NULL) { | 
|  | 1449 | sp<IMediaPlayerClient> nc; | 
|  | 1450 | { | 
|  | 1451 | Mutex::Autolock l(nextClient->mLock); | 
|  | 1452 | nc = nextClient->mClient; | 
|  | 1453 | } | 
|  | 1454 | if (nc != NULL) { | 
|  | 1455 | if (errStartNext == NO_ERROR) { | 
|  | 1456 | nc->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj); | 
|  | 1457 | } else { | 
|  | 1458 | nc->notify(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN , 0, obj); | 
|  | 1459 | ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext); | 
| Wei Jia | 2afac0c | 2016-01-07 12:13:07 -0800 | [diff] [blame] | 1460 | } | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1461 | } | 
|  | 1462 | } | 
|  | 1463 |  | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1464 | if (MEDIA_INFO == msg && | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1465 | MEDIA_INFO_METADATA_UPDATE == ext1) { | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1466 | const media::Metadata::Type metadata_type = ext2; | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1467 |  | 
|  | 1468 | if(client->shouldDropMetadata(metadata_type)) { | 
|  | 1469 | return; | 
|  | 1470 | } | 
|  | 1471 |  | 
|  | 1472 | // Update the list of metadata that have changed. getMetadata | 
|  | 1473 | // also access mMetadataUpdated and clears it. | 
|  | 1474 | client->addNewMetadataUpdate(metadata_type); | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1475 | } | 
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1476 |  | 
|  | 1477 | if (c != NULL) { | 
| Pawin Vongmasa | d27ad6b | 2017-12-12 01:56:40 -0800 | [diff] [blame] | 1478 | ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, spListener.get(), msg, ext1, ext2); | 
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1479 | c->notify(msg, ext1, ext2, obj); | 
|  | 1480 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | } | 
|  | 1482 |  | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1483 |  | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1484 | bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1485 | { | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1486 | Mutex::Autolock lock(mLock); | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1487 |  | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1488 | if (findMetadata(mMetadataDrop, code)) { | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1489 | return true; | 
|  | 1490 | } | 
|  | 1491 |  | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1492 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1493 | return false; | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1494 | } else { | 
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1495 | return true; | 
|  | 1496 | } | 
|  | 1497 | } | 
|  | 1498 |  | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1499 |  | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1500 | void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { | 
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1501 | Mutex::Autolock lock(mLock); | 
|  | 1502 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { | 
|  | 1503 | mMetadataUpdated.add(metadata_type); | 
|  | 1504 | } | 
|  | 1505 | } | 
|  | 1506 |  | 
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1507 | // Modular DRM | 
|  | 1508 | status_t MediaPlayerService::Client::prepareDrm(const uint8_t uuid[16], | 
|  | 1509 | const Vector<uint8_t>& drmSessionId) | 
|  | 1510 | { | 
|  | 1511 | ALOGV("[%d] prepareDrm", mConnId); | 
|  | 1512 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1513 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1514 |  | 
|  | 1515 | status_t ret = p->prepareDrm(uuid, drmSessionId); | 
|  | 1516 | ALOGV("prepareDrm ret: %d", ret); | 
|  | 1517 |  | 
|  | 1518 | return ret; | 
|  | 1519 | } | 
|  | 1520 |  | 
|  | 1521 | status_t MediaPlayerService::Client::releaseDrm() | 
|  | 1522 | { | 
|  | 1523 | ALOGV("[%d] releaseDrm", mConnId); | 
|  | 1524 | sp<MediaPlayerBase> p = getPlayer(); | 
|  | 1525 | if (p == 0) return UNKNOWN_ERROR; | 
|  | 1526 |  | 
|  | 1527 | status_t ret = p->releaseDrm(); | 
|  | 1528 | ALOGV("releaseDrm ret: %d", ret); | 
|  | 1529 |  | 
|  | 1530 | return ret; | 
|  | 1531 | } | 
|  | 1532 |  | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 1533 | status_t MediaPlayerService::Client::setOutputDevice(audio_port_handle_t deviceId) | 
|  | 1534 | { | 
|  | 1535 | ALOGV("[%d] setOutputDevice", mConnId); | 
|  | 1536 | { | 
|  | 1537 | Mutex::Autolock l(mLock); | 
|  | 1538 | if (mAudioOutput.get() != nullptr) { | 
|  | 1539 | return mAudioOutput->setOutputDevice(deviceId); | 
|  | 1540 | } | 
|  | 1541 | } | 
|  | 1542 | return NO_INIT; | 
|  | 1543 | } | 
|  | 1544 |  | 
|  | 1545 | status_t MediaPlayerService::Client::getRoutedDeviceId(audio_port_handle_t* deviceId) | 
|  | 1546 | { | 
|  | 1547 | ALOGV("[%d] getRoutedDeviceId", mConnId); | 
|  | 1548 | { | 
|  | 1549 | Mutex::Autolock l(mLock); | 
|  | 1550 | if (mAudioOutput.get() != nullptr) { | 
|  | 1551 | return mAudioOutput->getRoutedDeviceId(deviceId); | 
|  | 1552 | } | 
|  | 1553 | } | 
|  | 1554 | return NO_INIT; | 
|  | 1555 | } | 
|  | 1556 |  | 
|  | 1557 | status_t MediaPlayerService::Client::enableAudioDeviceCallback(bool enabled) | 
|  | 1558 | { | 
|  | 1559 | ALOGV("[%d] enableAudioDeviceCallback, %d", mConnId, enabled); | 
|  | 1560 | { | 
|  | 1561 | Mutex::Autolock l(mLock); | 
|  | 1562 | if (mAudioOutput.get() != nullptr) { | 
|  | 1563 | return mAudioOutput->enableAudioDeviceCallback(enabled); | 
|  | 1564 | } | 
|  | 1565 | } | 
|  | 1566 | return NO_INIT; | 
|  | 1567 | } | 
|  | 1568 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1569 | #if CALLBACK_ANTAGONIZER | 
|  | 1570 | const int Antagonizer::interval = 10000; // 10 msecs | 
|  | 1571 |  | 
| Pawin Vongmasa | d27ad6b | 2017-12-12 01:56:40 -0800 | [diff] [blame] | 1572 | Antagonizer::Antagonizer(notify_callback_f cb, const wp<IMediaPlayer> &client) : | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | mExit(false), mActive(false), mClient(client), mCb(cb) | 
|  | 1574 | { | 
|  | 1575 | createThread(callbackThread, this); | 
|  | 1576 | } | 
|  | 1577 |  | 
|  | 1578 | void Antagonizer::kill() | 
|  | 1579 | { | 
|  | 1580 | Mutex::Autolock _l(mLock); | 
|  | 1581 | mActive = false; | 
|  | 1582 | mExit = true; | 
|  | 1583 | mCondition.wait(mLock); | 
|  | 1584 | } | 
|  | 1585 |  | 
|  | 1586 | int Antagonizer::callbackThread(void* user) | 
|  | 1587 | { | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1588 | ALOGD("Antagonizer started"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); | 
|  | 1590 | while (!p->mExit) { | 
|  | 1591 | if (p->mActive) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1592 | ALOGV("send event"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | p->mCb(p->mClient, 0, 0, 0); | 
|  | 1594 | } | 
|  | 1595 | usleep(interval); | 
|  | 1596 | } | 
|  | 1597 | Mutex::Autolock _l(p->mLock); | 
|  | 1598 | p->mCondition.signal(); | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1599 | ALOGD("Antagonizer stopped"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | return 0; | 
|  | 1601 | } | 
|  | 1602 | #endif | 
|  | 1603 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | #undef LOG_TAG | 
|  | 1605 | #define LOG_TAG "AudioSink" | 
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 1606 | MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId, uid_t uid, int pid, | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 1607 | const audio_attributes_t* attr, const sp<AudioSystem::AudioDeviceCallback>& deviceCallback) | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1608 | : mCallback(NULL), | 
| Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1609 | mCallbackCookie(NULL), | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1610 | mCallbackData(NULL), | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1611 | mStreamType(AUDIO_STREAM_MUSIC), | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1612 | mLeftVolume(1.0), | 
|  | 1613 | mRightVolume(1.0), | 
|  | 1614 | mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT), | 
|  | 1615 | mSampleRateHz(0), | 
|  | 1616 | mMsecsPerFrame(0), | 
|  | 1617 | mFrameSize(0), | 
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1618 | mSessionId(sessionId), | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1619 | mUid(uid), | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1620 | mPid(pid), | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1621 | mSendLevel(0.0), | 
|  | 1622 | mAuxEffectId(0), | 
| Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 1623 | mFlags(AUDIO_OUTPUT_FLAG_NONE), | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 1624 | mVolumeHandler(new media::VolumeHandler()), | 
|  | 1625 | mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE), | 
| jiabin | 161b64fc | 2017-11-17 09:31:48 -0800 | [diff] [blame] | 1626 | mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE), | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 1627 | mDeviceCallbackEnabled(false), | 
|  | 1628 | mDeviceCallback(deviceCallback) | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1629 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1630 | ALOGV("AudioOutput(%d)", sessionId); | 
| Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1631 | if (attr != NULL) { | 
| Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1632 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); | 
|  | 1633 | if (mAttributes != NULL) { | 
|  | 1634 | memcpy(mAttributes, attr, sizeof(audio_attributes_t)); | 
|  | 1635 | mStreamType = audio_attributes_to_stream_type(attr); | 
|  | 1636 | } | 
|  | 1637 | } else { | 
|  | 1638 | mAttributes = NULL; | 
| Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1639 | } | 
|  | 1640 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | setMinBufferCount(); | 
|  | 1642 | } | 
|  | 1643 |  | 
|  | 1644 | MediaPlayerService::AudioOutput::~AudioOutput() | 
|  | 1645 | { | 
|  | 1646 | close(); | 
| Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1647 | free(mAttributes); | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1648 | delete mCallbackData; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | } | 
|  | 1650 |  | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1651 | //static | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | void MediaPlayerService::AudioOutput::setMinBufferCount() | 
|  | 1653 | { | 
|  | 1654 | char value[PROPERTY_VALUE_MAX]; | 
|  | 1655 | if (property_get("ro.kernel.qemu", value, 0)) { | 
|  | 1656 | mIsOnEmulator = true; | 
|  | 1657 | mMinBufferCount = 12;  // to prevent systematic buffer underrun for emulator | 
|  | 1658 | } | 
|  | 1659 | } | 
|  | 1660 |  | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1661 | // static | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | bool MediaPlayerService::AudioOutput::isOnEmulator() | 
|  | 1663 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1664 | setMinBufferCount(); // benign race wrt other threads | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | return mIsOnEmulator; | 
|  | 1666 | } | 
|  | 1667 |  | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1668 | // static | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1669 | int MediaPlayerService::AudioOutput::getMinBufferCount() | 
|  | 1670 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1671 | setMinBufferCount(); // benign race wrt other threads | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | return mMinBufferCount; | 
|  | 1673 | } | 
|  | 1674 |  | 
|  | 1675 | ssize_t MediaPlayerService::AudioOutput::bufferSize() const | 
|  | 1676 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1677 | Mutex::Autolock lock(mLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | if (mTrack == 0) return NO_INIT; | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1679 | return mTrack->frameCount() * mFrameSize; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | } | 
|  | 1681 |  | 
|  | 1682 | ssize_t MediaPlayerService::AudioOutput::frameCount() const | 
|  | 1683 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1684 | Mutex::Autolock lock(mLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1685 | if (mTrack == 0) return NO_INIT; | 
|  | 1686 | return mTrack->frameCount(); | 
|  | 1687 | } | 
|  | 1688 |  | 
|  | 1689 | ssize_t MediaPlayerService::AudioOutput::channelCount() const | 
|  | 1690 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1691 | Mutex::Autolock lock(mLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | if (mTrack == 0) return NO_INIT; | 
|  | 1693 | return mTrack->channelCount(); | 
|  | 1694 | } | 
|  | 1695 |  | 
|  | 1696 | ssize_t MediaPlayerService::AudioOutput::frameSize() const | 
|  | 1697 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1698 | Mutex::Autolock lock(mLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | if (mTrack == 0) return NO_INIT; | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1700 | return mFrameSize; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1701 | } | 
|  | 1702 |  | 
|  | 1703 | uint32_t MediaPlayerService::AudioOutput::latency () const | 
|  | 1704 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1705 | Mutex::Autolock lock(mLock); | 
| Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 1706 | if (mTrack == 0) return 0; | 
|  | 1707 | return mTrack->latency(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | } | 
|  | 1709 |  | 
|  | 1710 | float MediaPlayerService::AudioOutput::msecsPerFrame() const | 
|  | 1711 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1712 | Mutex::Autolock lock(mLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | return mMsecsPerFrame; | 
|  | 1714 | } | 
|  | 1715 |  | 
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1716 | status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const | 
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1717 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1718 | Mutex::Autolock lock(mLock); | 
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1719 | if (mTrack == 0) return NO_INIT; | 
|  | 1720 | return mTrack->getPosition(position); | 
|  | 1721 | } | 
|  | 1722 |  | 
| Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 1723 | status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const | 
|  | 1724 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1725 | Mutex::Autolock lock(mLock); | 
| Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 1726 | if (mTrack == 0) return NO_INIT; | 
|  | 1727 | return mTrack->getTimestamp(ts); | 
|  | 1728 | } | 
|  | 1729 |  | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1730 | // TODO: Remove unnecessary calls to getPlayedOutDurationUs() | 
|  | 1731 | // as it acquires locks and may query the audio driver. | 
|  | 1732 | // | 
|  | 1733 | // Some calls could conceivably retrieve extrapolated data instead of | 
|  | 1734 | // accessing getTimestamp() or getPosition() every time a data buffer with | 
|  | 1735 | // a media time is received. | 
|  | 1736 | // | 
|  | 1737 | // Calculate duration of played samples if played at normal rate (i.e., 1.0). | 
|  | 1738 | int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const | 
|  | 1739 | { | 
|  | 1740 | Mutex::Autolock lock(mLock); | 
|  | 1741 | if (mTrack == 0 || mSampleRateHz == 0) { | 
| Wei Jia | 213f490 | 2015-10-22 08:55:25 -0700 | [diff] [blame] | 1742 | return 0; | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1743 | } | 
|  | 1744 |  | 
|  | 1745 | uint32_t numFramesPlayed; | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1746 | int64_t numFramesPlayedAtUs; | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1747 | AudioTimestamp ts; | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1748 |  | 
|  | 1749 | status_t res = mTrack->getTimestamp(ts); | 
|  | 1750 | if (res == OK) {                 // case 1: mixing audio tracks and offloaded tracks. | 
|  | 1751 | numFramesPlayed = ts.mPosition; | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1752 | numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000; | 
|  | 1753 | //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1754 | } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track | 
|  | 1755 | numFramesPlayed = 0; | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1756 | numFramesPlayedAtUs = nowUs; | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1757 | //ALOGD("getTimestamp: WOULD_BLOCK %d %lld", | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1758 | //        numFramesPlayed, (long long)numFramesPlayedAtUs); | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1759 | } else {                         // case 3: transitory at new track or audio fast tracks. | 
|  | 1760 | res = mTrack->getPosition(&numFramesPlayed); | 
|  | 1761 | CHECK_EQ(res, (status_t)OK); | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1762 | numFramesPlayedAtUs = nowUs; | 
|  | 1763 | numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */ | 
|  | 1764 | //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1765 | } | 
|  | 1766 |  | 
|  | 1767 | // CHECK_EQ(numFramesPlayed & (1 << 31), 0);  // can't be negative until 12.4 hrs, test | 
|  | 1768 | // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours. | 
|  | 1769 | int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz) | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1770 | + nowUs - numFramesPlayedAtUs; | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1771 | if (durationUs < 0) { | 
|  | 1772 | // Occurs when numFramesPlayed position is very small and the following: | 
|  | 1773 | // (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] | 1774 | //     numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed. | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1775 | // (2) In case 3, using getPosition and adding mAudioSink->latency() to | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1776 | //     numFramesPlayedAtUs, by a time amount greater than numFramesPlayed. | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1777 | // | 
|  | 1778 | // Both of these are transitory conditions. | 
|  | 1779 | ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs); | 
|  | 1780 | durationUs = 0; | 
|  | 1781 | } | 
|  | 1782 | ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)", | 
| Andy Hung | 5d31380 | 2016-10-10 15:09:39 -0700 | [diff] [blame] | 1783 | (long long)durationUs, (long long)nowUs, | 
|  | 1784 | numFramesPlayed, (long long)numFramesPlayedAtUs); | 
| Wei Jia | c4ac817 | 2015-10-21 10:35:48 -0700 | [diff] [blame] | 1785 | return durationUs; | 
|  | 1786 | } | 
|  | 1787 |  | 
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1788 | status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const | 
|  | 1789 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1790 | Mutex::Autolock lock(mLock); | 
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1791 | if (mTrack == 0) return NO_INIT; | 
| Andy Hung | 2f6e73d | 2016-04-08 12:12:58 -0700 | [diff] [blame] | 1792 | ExtendedTimestamp ets; | 
|  | 1793 | status_t status = mTrack->getTimestamp(&ets); | 
|  | 1794 | if (status == OK || status == WOULD_BLOCK) { | 
|  | 1795 | *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT]; | 
|  | 1796 | } | 
|  | 1797 | return status; | 
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1798 | } | 
|  | 1799 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1800 | status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs) | 
|  | 1801 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1802 | Mutex::Autolock lock(mLock); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1803 | if (mTrack == 0) return NO_INIT; | 
|  | 1804 | return mTrack->setParameters(keyValuePairs); | 
|  | 1805 | } | 
|  | 1806 |  | 
|  | 1807 | String8  MediaPlayerService::AudioOutput::getParameters(const String8& keys) | 
|  | 1808 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1809 | Mutex::Autolock lock(mLock); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1810 | if (mTrack == 0) return String8::empty(); | 
|  | 1811 | return mTrack->getParameters(keys); | 
|  | 1812 | } | 
|  | 1813 |  | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1814 | void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1815 | Mutex::Autolock lock(mLock); | 
| Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1816 | if (attributes == NULL) { | 
|  | 1817 | free(mAttributes); | 
|  | 1818 | mAttributes = NULL; | 
|  | 1819 | } else { | 
|  | 1820 | if (mAttributes == NULL) { | 
|  | 1821 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); | 
|  | 1822 | } | 
|  | 1823 | memcpy(mAttributes, attributes, sizeof(audio_attributes_t)); | 
| Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1824 | mStreamType = audio_attributes_to_stream_type(attributes); | 
|  | 1825 | } | 
|  | 1826 | } | 
|  | 1827 |  | 
|  | 1828 | void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType) | 
|  | 1829 | { | 
| Jean-Michel Trivi | 2650e96 | 2015-07-22 18:14:02 -0700 | [diff] [blame] | 1830 | Mutex::Autolock lock(mLock); | 
| Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 1831 | // do not allow direct stream type modification if attributes have been set | 
|  | 1832 | if (mAttributes == NULL) { | 
|  | 1833 | mStreamType = streamType; | 
|  | 1834 | } | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1835 | } | 
|  | 1836 |  | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1837 | void MediaPlayerService::AudioOutput::deleteRecycledTrack_l() | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1838 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1839 | ALOGV("deleteRecycledTrack_l"); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1840 | if (mRecycledTrack != 0) { | 
|  | 1841 |  | 
|  | 1842 | if (mCallbackData != NULL) { | 
|  | 1843 | mCallbackData->setOutput(NULL); | 
|  | 1844 | mCallbackData->endTrackSwitch(); | 
|  | 1845 | } | 
|  | 1846 |  | 
|  | 1847 | if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) { | 
| Andy Hung | e13f8a6 | 2016-03-30 14:20:42 -0700 | [diff] [blame] | 1848 | int32_t msec = 0; | 
|  | 1849 | if (!mRecycledTrack->stopped()) { // check if active | 
|  | 1850 | (void)mRecycledTrack->pendingDuration(&msec); | 
|  | 1851 | } | 
|  | 1852 | mRecycledTrack->stop(); // ensure full data drain | 
|  | 1853 | ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec); | 
|  | 1854 | if (msec > 0) { | 
|  | 1855 | static const int32_t WAIT_LIMIT_MS = 3000; | 
|  | 1856 | if (msec > WAIT_LIMIT_MS) { | 
|  | 1857 | msec = WAIT_LIMIT_MS; | 
|  | 1858 | } | 
|  | 1859 | usleep(msec * 1000LL); | 
|  | 1860 | } | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1861 | } | 
|  | 1862 | // An offloaded track isn't flushed because the STREAM_END is reported | 
|  | 1863 | // slightly prematurely to allow time for the gapless track switch | 
|  | 1864 | // but this means that if we decide not to recycle the track there | 
|  | 1865 | // could be a small amount of residual data still playing. We leave | 
|  | 1866 | // AudioFlinger to drain the track. | 
|  | 1867 |  | 
|  | 1868 | mRecycledTrack.clear(); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1869 | close_l(); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1870 | delete mCallbackData; | 
|  | 1871 | mCallbackData = NULL; | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1872 | } | 
|  | 1873 | } | 
|  | 1874 |  | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1875 | void MediaPlayerService::AudioOutput::close_l() | 
|  | 1876 | { | 
|  | 1877 | mTrack.clear(); | 
|  | 1878 | } | 
|  | 1879 |  | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1880 | status_t MediaPlayerService::AudioOutput::open( | 
| Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1881 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, | 
|  | 1882 | audio_format_t format, int bufferCount, | 
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1883 | AudioCallback cb, void *cookie, | 
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1884 | audio_output_flags_t flags, | 
| Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 1885 | const audio_offload_info_t *offloadInfo, | 
| Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1886 | bool doNotReconnect, | 
|  | 1887 | uint32_t suggestedFrameCount) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1888 | { | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1889 | ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask, | 
|  | 1890 | format, bufferCount, mSessionId, flags); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1892 | // offloading is only supported in callback mode for now. | 
|  | 1893 | // offloadInfo must be present if offload flag is set | 
|  | 1894 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) && | 
|  | 1895 | ((cb == NULL) || (offloadInfo == NULL))) { | 
|  | 1896 | return BAD_VALUE; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1897 | } | 
|  | 1898 |  | 
| Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1899 | // compute frame count for the AudioTrack internal buffer | 
|  | 1900 | size_t frameCount; | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1901 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 1902 | frameCount = 0; // AudioTrack will get frame count from AudioFlinger | 
|  | 1903 | } else { | 
| Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1904 | // try to estimate the buffer processing fetch size from AudioFlinger. | 
|  | 1905 | // framesPerBuffer is approximate and generally correct, except when it's not :-). | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1906 | uint32_t afSampleRate; | 
|  | 1907 | size_t afFrameCount; | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1908 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { | 
|  | 1909 | return NO_INIT; | 
|  | 1910 | } | 
|  | 1911 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { | 
|  | 1912 | return NO_INIT; | 
|  | 1913 | } | 
| Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1914 | const size_t framesPerBuffer = | 
|  | 1915 | (unsigned long long)sampleRate * afFrameCount / afSampleRate; | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1916 |  | 
| Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1917 | if (bufferCount == 0) { | 
|  | 1918 | // use suggestedFrameCount | 
|  | 1919 | bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer; | 
|  | 1920 | } | 
|  | 1921 | // Check argument bufferCount against the mininum buffer count | 
|  | 1922 | if (bufferCount != 0 && bufferCount < mMinBufferCount) { | 
|  | 1923 | ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount); | 
|  | 1924 | bufferCount = mMinBufferCount; | 
|  | 1925 | } | 
|  | 1926 | // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger | 
|  | 1927 | // which will be the minimum size permitted. | 
|  | 1928 | frameCount = bufferCount * framesPerBuffer; | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1929 | } | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1930 |  | 
| Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1931 | if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { | 
| Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1932 | channelMask = audio_channel_out_mask_from_count(channelCount); | 
| Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1933 | if (0 == channelMask) { | 
|  | 1934 | ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount); | 
|  | 1935 | return NO_INIT; | 
|  | 1936 | } | 
|  | 1937 | } | 
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1938 |  | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1939 | Mutex::Autolock lock(mLock); | 
| Andy Hung | 179652e | 2015-05-31 22:49:46 -0700 | [diff] [blame] | 1940 | mCallback = cb; | 
|  | 1941 | mCallbackCookie = cookie; | 
|  | 1942 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1943 | // Check whether we can recycle the track | 
|  | 1944 | bool reuse = false; | 
|  | 1945 | bool bothOffloaded = false; | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1946 |  | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1947 | if (mRecycledTrack != 0) { | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1948 | // check whether we are switching between two offloaded tracks | 
|  | 1949 | bothOffloaded = (flags & mRecycledTrack->getFlags() | 
|  | 1950 | & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1951 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1952 | // check if the existing track can be reused as-is, or if a new track needs to be created. | 
|  | 1953 | reuse = true; | 
|  | 1954 |  | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1955 | if ((mCallbackData == NULL && mCallback != NULL) || | 
|  | 1956 | (mCallbackData != NULL && mCallback == NULL)) { | 
|  | 1957 | // recycled track uses callbacks but the caller wants to use writes, or vice versa | 
|  | 1958 | ALOGV("can't chain callback and write"); | 
|  | 1959 | reuse = false; | 
|  | 1960 | } else if ((mRecycledTrack->getSampleRate() != sampleRate) || | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1961 | (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) { | 
|  | 1962 | ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch", | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1963 | mRecycledTrack->getSampleRate(), sampleRate, | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1964 | mRecycledTrack->channelCount(), channelCount); | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1965 | reuse = false; | 
|  | 1966 | } else if (flags != mFlags) { | 
|  | 1967 | ALOGV("output flags differ %08x/%08x", flags, mFlags); | 
|  | 1968 | reuse = false; | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1969 | } else if (mRecycledTrack->format() != format) { | 
|  | 1970 | reuse = false; | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1971 | } | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1972 | } else { | 
|  | 1973 | ALOGV("no track available to recycle"); | 
|  | 1974 | } | 
|  | 1975 |  | 
|  | 1976 | ALOGV_IF(bothOffloaded, "both tracks offloaded"); | 
|  | 1977 |  | 
|  | 1978 | // If we can't recycle and both tracks are offloaded | 
|  | 1979 | // we must close the previous output before opening a new one | 
|  | 1980 | if (bothOffloaded && !reuse) { | 
|  | 1981 | ALOGV("both offloaded and not recycling"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 1982 | deleteRecycledTrack_l(); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1983 | } | 
|  | 1984 |  | 
|  | 1985 | sp<AudioTrack> t; | 
|  | 1986 | CallbackData *newcbd = NULL; | 
|  | 1987 |  | 
|  | 1988 | // We don't attempt to create a new track if we are recycling an | 
|  | 1989 | // offloaded track. But, if we are recycling a non-offloaded or we | 
|  | 1990 | // are switching where one is offloaded and one isn't then we create | 
|  | 1991 | // the new track in advance so that we can read additional stream info | 
|  | 1992 |  | 
|  | 1993 | if (!(reuse && bothOffloaded)) { | 
|  | 1994 | ALOGV("creating new AudioTrack"); | 
|  | 1995 |  | 
|  | 1996 | if (mCallback != NULL) { | 
|  | 1997 | newcbd = new CallbackData(this); | 
|  | 1998 | t = new AudioTrack( | 
|  | 1999 | mStreamType, | 
|  | 2000 | sampleRate, | 
|  | 2001 | format, | 
|  | 2002 | channelMask, | 
|  | 2003 | frameCount, | 
|  | 2004 | flags, | 
|  | 2005 | CallbackWrapper, | 
|  | 2006 | newcbd, | 
|  | 2007 | 0,  // notification frames | 
|  | 2008 | mSessionId, | 
|  | 2009 | AudioTrack::TRANSFER_CALLBACK, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2010 | offloadInfo, | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2011 | mUid, | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 2012 | mPid, | 
| Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 2013 | mAttributes, | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 2014 | doNotReconnect, | 
|  | 2015 | 1.0f,  // default value for maxRequiredSpeed | 
|  | 2016 | mSelectedDeviceId); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2017 | } else { | 
| Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 2018 | // TODO: Due to buffer memory concerns, we use a max target playback speed | 
|  | 2019 | // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed), | 
|  | 2020 | // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed. | 
|  | 2021 | const float targetSpeed = | 
|  | 2022 | std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed); | 
|  | 2023 | ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed, | 
|  | 2024 | "track target speed:%f clamped from playback speed:%f", | 
|  | 2025 | targetSpeed, mPlaybackRate.mSpeed); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2026 | t = new AudioTrack( | 
|  | 2027 | mStreamType, | 
|  | 2028 | sampleRate, | 
|  | 2029 | format, | 
|  | 2030 | channelMask, | 
|  | 2031 | frameCount, | 
|  | 2032 | flags, | 
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2033 | NULL, // callback | 
|  | 2034 | NULL, // user data | 
|  | 2035 | 0, // notification frames | 
|  | 2036 | mSessionId, | 
|  | 2037 | AudioTrack::TRANSFER_DEFAULT, | 
|  | 2038 | NULL, // offload info | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2039 | mUid, | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 2040 | mPid, | 
| Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 2041 | mAttributes, | 
| Andy Hung | ff874dc | 2016-04-11 16:49:09 -0700 | [diff] [blame] | 2042 | doNotReconnect, | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 2043 | targetSpeed, | 
|  | 2044 | mSelectedDeviceId); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2045 | } | 
|  | 2046 |  | 
|  | 2047 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { | 
|  | 2048 | ALOGE("Unable to create audio track"); | 
|  | 2049 | delete newcbd; | 
| Glenn Kasten | 3e98ecd | 2015-05-18 13:13:24 -0700 | [diff] [blame] | 2050 | // t goes out of scope, so reference count drops to zero | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2051 | return NO_INIT; | 
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 2052 | } else { | 
|  | 2053 | // successful AudioTrack initialization implies a legacy stream type was generated | 
|  | 2054 | // from the audio attributes | 
|  | 2055 | mStreamType = t->streamType(); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2056 | } | 
|  | 2057 | } | 
|  | 2058 |  | 
|  | 2059 | if (reuse) { | 
|  | 2060 | CHECK(mRecycledTrack != NULL); | 
|  | 2061 |  | 
|  | 2062 | if (!bothOffloaded) { | 
|  | 2063 | if (mRecycledTrack->frameCount() != t->frameCount()) { | 
| Andy Hung | 833b475 | 2016-04-04 17:15:48 -0700 | [diff] [blame] | 2064 | ALOGV("framecount differs: %zu/%zu frames", | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2065 | mRecycledTrack->frameCount(), t->frameCount()); | 
|  | 2066 | reuse = false; | 
|  | 2067 | } | 
|  | 2068 | } | 
|  | 2069 |  | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2070 | if (reuse) { | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2071 | ALOGV("chaining to next output and recycling track"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2072 | close_l(); | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2073 | mTrack = mRecycledTrack; | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2074 | mRecycledTrack.clear(); | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2075 | if (mCallbackData != NULL) { | 
|  | 2076 | mCallbackData->setOutput(this); | 
|  | 2077 | } | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2078 | delete newcbd; | 
| Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2079 | return updateTrack(); | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2080 | } | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2081 | } | 
|  | 2082 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2083 | // we're not going to reuse the track, unblock and flush it | 
|  | 2084 | // this was done earlier if both tracks are offloaded | 
|  | 2085 | if (!bothOffloaded) { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2086 | deleteRecycledTrack_l(); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2087 | } | 
|  | 2088 |  | 
|  | 2089 | CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL))); | 
|  | 2090 |  | 
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 2091 | mCallbackData = newcbd; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2092 | ALOGV("setVolume"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | t->setVolume(mLeftVolume, mRightVolume); | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2094 |  | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2095 | // Restore VolumeShapers for the MediaPlayer in case the track was recreated | 
|  | 2096 | // due to an output sink error (e.g. offload to non-offload switch). | 
|  | 2097 | mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status { | 
|  | 2098 | sp<VolumeShaper::Operation> operationToEnd = | 
|  | 2099 | new VolumeShaper::Operation(shaper.mOperation); | 
|  | 2100 | // TODO: Ideally we would restore to the exact xOffset position | 
|  | 2101 | // as returned by getVolumeShaperState(), but we don't have that | 
|  | 2102 | // information when restoring at the client unless we periodically poll | 
|  | 2103 | // the server or create shared memory state. | 
|  | 2104 | // | 
|  | 2105 | // For now, we simply advance to the end of the VolumeShaper effect | 
|  | 2106 | // if it has been started. | 
|  | 2107 | if (shaper.isStarted()) { | 
| Andy Hung | f370264 | 2017-05-05 17:33:32 -0700 | [diff] [blame] | 2108 | operationToEnd->setNormalizedTime(1.f); | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2109 | } | 
|  | 2110 | return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd); | 
| Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2111 | }); | 
| Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2112 |  | 
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2113 | mSampleRateHz = sampleRate; | 
| Preetam Singh Ranawat | 2e17eef | 2015-08-12 12:11:46 -0700 | [diff] [blame] | 2114 | mFlags = flags; | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2115 | mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2116 | mFrameSize = t->frameSize(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2117 | mTrack = t; | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2118 |  | 
| Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2119 | return updateTrack(); | 
|  | 2120 | } | 
|  | 2121 |  | 
|  | 2122 | status_t MediaPlayerService::AudioOutput::updateTrack() { | 
|  | 2123 | if (mTrack == NULL) { | 
|  | 2124 | return NO_ERROR; | 
|  | 2125 | } | 
|  | 2126 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2127 | status_t res = NO_ERROR; | 
| Wei Jia | 0162d00 | 2015-06-09 11:59:33 -0700 | [diff] [blame] | 2128 | // Note some output devices may give us a direct track even though we don't specify it. | 
|  | 2129 | // Example: Line application b/17459982. | 
| Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2130 | if ((mTrack->getFlags() | 
|  | 2131 | & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) { | 
|  | 2132 | res = mTrack->setPlaybackRate(mPlaybackRate); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2133 | if (res == NO_ERROR) { | 
| Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2134 | mTrack->setAuxEffectSendLevel(mSendLevel); | 
|  | 2135 | res = mTrack->attachAuxEffect(mAuxEffectId); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2136 | } | 
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2137 | } | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 2138 | mTrack->setOutputDevice(mSelectedDeviceId); | 
|  | 2139 | if (mDeviceCallbackEnabled) { | 
|  | 2140 | mTrack->addAudioDeviceCallback(mDeviceCallback.promote()); | 
|  | 2141 | } | 
| Wei Jia | e0bbac9 | 2016-07-18 16:04:53 -0700 | [diff] [blame] | 2142 | ALOGV("updateTrack() DONE status %d", res); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2143 | return res; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2144 | } | 
|  | 2145 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2146 | status_t MediaPlayerService::AudioOutput::start() | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2147 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2148 | ALOGV("start"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2149 | Mutex::Autolock lock(mLock); | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2150 | if (mCallbackData != NULL) { | 
|  | 2151 | mCallbackData->endTrackSwitch(); | 
|  | 2152 | } | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2153 | if (mTrack != 0) { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2154 | mTrack->setVolume(mLeftVolume, mRightVolume); | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2155 | mTrack->setAuxEffectSendLevel(mSendLevel); | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2156 | status_t status = mTrack->start(); | 
|  | 2157 | if (status == NO_ERROR) { | 
|  | 2158 | mVolumeHandler->setStarted(); | 
|  | 2159 | } | 
|  | 2160 | return status; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2161 | } | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2162 | return NO_INIT; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2163 | } | 
|  | 2164 |  | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2165 | void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2166 | Mutex::Autolock lock(mLock); | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2167 | mNextOutput = nextOutput; | 
|  | 2168 | } | 
| Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 2169 |  | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2170 | void MediaPlayerService::AudioOutput::switchToNextOutput() { | 
|  | 2171 | ALOGV("switchToNextOutput"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2172 |  | 
|  | 2173 | // Try to acquire the callback lock before moving track (without incurring deadlock). | 
|  | 2174 | const unsigned kMaxSwitchTries = 100; | 
|  | 2175 | Mutex::Autolock lock(mLock); | 
|  | 2176 | for (unsigned tries = 0;;) { | 
|  | 2177 | if (mTrack == 0) { | 
|  | 2178 | return; | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2179 | } | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2180 | if (mNextOutput != NULL && mNextOutput != this) { | 
|  | 2181 | if (mCallbackData != NULL) { | 
|  | 2182 | // two alternative approaches | 
|  | 2183 | #if 1 | 
|  | 2184 | CallbackData *callbackData = mCallbackData; | 
|  | 2185 | mLock.unlock(); | 
|  | 2186 | // proper acquisition sequence | 
|  | 2187 | callbackData->lock(); | 
|  | 2188 | mLock.lock(); | 
|  | 2189 | // Caution: it is unlikely that someone deleted our callback or changed our target | 
|  | 2190 | if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) { | 
|  | 2191 | // fatal if we are starved out. | 
|  | 2192 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, | 
|  | 2193 | "switchToNextOutput() cannot obtain correct lock sequence"); | 
|  | 2194 | callbackData->unlock(); | 
|  | 2195 | continue; | 
|  | 2196 | } | 
|  | 2197 | callbackData->mSwitching = true; // begin track switch | 
| Wei Jia | adee56a | 2016-08-05 15:40:34 -0700 | [diff] [blame] | 2198 | callbackData->setOutput(NULL); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2199 | #else | 
|  | 2200 | // tryBeginTrackSwitch() returns false if the callback has the lock. | 
|  | 2201 | if (!mCallbackData->tryBeginTrackSwitch()) { | 
|  | 2202 | // fatal if we are starved out. | 
|  | 2203 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, | 
|  | 2204 | "switchToNextOutput() cannot obtain callback lock"); | 
|  | 2205 | mLock.unlock(); | 
|  | 2206 | usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput | 
|  | 2207 | mLock.lock(); | 
|  | 2208 | continue; | 
|  | 2209 | } | 
|  | 2210 | #endif | 
|  | 2211 | } | 
|  | 2212 |  | 
|  | 2213 | Mutex::Autolock nextLock(mNextOutput->mLock); | 
|  | 2214 |  | 
|  | 2215 | // If the next output track is not NULL, then it has been | 
|  | 2216 | // opened already for playback. | 
|  | 2217 | // This is possible even without the next player being started, | 
|  | 2218 | // for example, the next player could be prepared and seeked. | 
|  | 2219 | // | 
|  | 2220 | // Presuming it isn't advisable to force the track over. | 
|  | 2221 | if (mNextOutput->mTrack == NULL) { | 
|  | 2222 | ALOGD("Recycling track for gapless playback"); | 
|  | 2223 | delete mNextOutput->mCallbackData; | 
|  | 2224 | mNextOutput->mCallbackData = mCallbackData; | 
|  | 2225 | mNextOutput->mRecycledTrack = mTrack; | 
|  | 2226 | mNextOutput->mSampleRateHz = mSampleRateHz; | 
|  | 2227 | mNextOutput->mMsecsPerFrame = mMsecsPerFrame; | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2228 | mNextOutput->mFlags = mFlags; | 
|  | 2229 | mNextOutput->mFrameSize = mFrameSize; | 
|  | 2230 | close_l(); | 
|  | 2231 | mCallbackData = NULL;  // destruction handled by mNextOutput | 
|  | 2232 | } else { | 
|  | 2233 | ALOGW("Ignoring gapless playback because next player has already started"); | 
|  | 2234 | // remove track in case resource needed for future players. | 
|  | 2235 | if (mCallbackData != NULL) { | 
|  | 2236 | mCallbackData->endTrackSwitch();  // release lock for callbacks before close. | 
|  | 2237 | } | 
|  | 2238 | close_l(); | 
|  | 2239 | } | 
|  | 2240 | } | 
|  | 2241 | break; | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2242 | } | 
|  | 2243 | } | 
|  | 2244 |  | 
| Wei Jia | 7d3f4df | 2015-03-03 15:28:00 -0800 | [diff] [blame] | 2245 | 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] | 2246 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2247 | Mutex::Autolock lock(mLock); | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2248 | 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] | 2249 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2250 | //ALOGV("write(%p, %u)", buffer, size); | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2251 | if (mTrack != 0) { | 
| Andy Hung | 2f6e73d | 2016-04-08 12:12:58 -0700 | [diff] [blame] | 2252 | return mTrack->write(buffer, size, blocking); | 
| Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 2253 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2254 | return NO_INIT; | 
|  | 2255 | } | 
|  | 2256 |  | 
|  | 2257 | void MediaPlayerService::AudioOutput::stop() | 
|  | 2258 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2259 | ALOGV("stop"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2260 | Mutex::Autolock lock(mLock); | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2261 | if (mTrack != 0) mTrack->stop(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2262 | } | 
|  | 2263 |  | 
|  | 2264 | void MediaPlayerService::AudioOutput::flush() | 
|  | 2265 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2266 | ALOGV("flush"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2267 | Mutex::Autolock lock(mLock); | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2268 | if (mTrack != 0) mTrack->flush(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2269 | } | 
|  | 2270 |  | 
|  | 2271 | void MediaPlayerService::AudioOutput::pause() | 
|  | 2272 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2273 | ALOGV("pause"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2274 | Mutex::Autolock lock(mLock); | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2275 | if (mTrack != 0) mTrack->pause(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2276 | } | 
|  | 2277 |  | 
|  | 2278 | void MediaPlayerService::AudioOutput::close() | 
|  | 2279 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2280 | ALOGV("close"); | 
| Erik Wolsheimer | 7845a1f | 2015-10-30 12:07:52 -0700 | [diff] [blame] | 2281 | sp<AudioTrack> track; | 
|  | 2282 | { | 
|  | 2283 | Mutex::Autolock lock(mLock); | 
|  | 2284 | track = mTrack; | 
|  | 2285 | close_l(); // clears mTrack | 
|  | 2286 | } | 
|  | 2287 | // destruction of the track occurs outside of mutex. | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2288 | } | 
|  | 2289 |  | 
|  | 2290 | void MediaPlayerService::AudioOutput::setVolume(float left, float right) | 
|  | 2291 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2292 | ALOGV("setVolume(%f, %f)", left, right); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2293 | Mutex::Autolock lock(mLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2294 | mLeftVolume = left; | 
|  | 2295 | mRightVolume = right; | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2296 | if (mTrack != 0) { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2297 | mTrack->setVolume(left, right); | 
|  | 2298 | } | 
|  | 2299 | } | 
|  | 2300 |  | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2301 | status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate) | 
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2302 | { | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2303 | ALOGV("setPlaybackRate(%f %f %d %d)", | 
|  | 2304 | rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2305 | Mutex::Autolock lock(mLock); | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2306 | if (mTrack == 0) { | 
|  | 2307 | // remember rate so that we can set it when the track is opened | 
|  | 2308 | mPlaybackRate = rate; | 
|  | 2309 | return OK; | 
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2310 | } | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2311 | status_t res = mTrack->setPlaybackRate(rate); | 
|  | 2312 | if (res != NO_ERROR) { | 
|  | 2313 | return res; | 
|  | 2314 | } | 
|  | 2315 | // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded | 
|  | 2316 | CHECK_GT(rate.mSpeed, 0.f); | 
|  | 2317 | mPlaybackRate = rate; | 
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2318 | if (mSampleRateHz != 0) { | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2319 | mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz); | 
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 2320 | } | 
|  | 2321 | return res; | 
|  | 2322 | } | 
|  | 2323 |  | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2324 | status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate) | 
|  | 2325 | { | 
|  | 2326 | ALOGV("setPlaybackRate"); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2327 | Mutex::Autolock lock(mLock); | 
| Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 2328 | if (mTrack == 0) { | 
|  | 2329 | return NO_INIT; | 
|  | 2330 | } | 
|  | 2331 | *rate = mTrack->getPlaybackRate(); | 
|  | 2332 | return NO_ERROR; | 
|  | 2333 | } | 
|  | 2334 |  | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2335 | status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level) | 
|  | 2336 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2337 | ALOGV("setAuxEffectSendLevel(%f)", level); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2338 | Mutex::Autolock lock(mLock); | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2339 | mSendLevel = level; | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2340 | if (mTrack != 0) { | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2341 | return mTrack->setAuxEffectSendLevel(level); | 
|  | 2342 | } | 
|  | 2343 | return NO_ERROR; | 
|  | 2344 | } | 
|  | 2345 |  | 
|  | 2346 | status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId) | 
|  | 2347 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2348 | ALOGV("attachAuxEffect(%d)", effectId); | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2349 | Mutex::Autolock lock(mLock); | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2350 | mAuxEffectId = effectId; | 
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 2351 | if (mTrack != 0) { | 
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 2352 | return mTrack->attachAuxEffect(effectId); | 
|  | 2353 | } | 
|  | 2354 | return NO_ERROR; | 
|  | 2355 | } | 
|  | 2356 |  | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 2357 | status_t MediaPlayerService::AudioOutput::setOutputDevice(audio_port_handle_t deviceId) | 
|  | 2358 | { | 
|  | 2359 | ALOGV("setOutputDevice(%d)", deviceId); | 
|  | 2360 | Mutex::Autolock lock(mLock); | 
|  | 2361 | mSelectedDeviceId = deviceId; | 
|  | 2362 | if (mTrack != 0) { | 
|  | 2363 | return mTrack->setOutputDevice(deviceId); | 
|  | 2364 | } | 
|  | 2365 | return NO_ERROR; | 
|  | 2366 | } | 
|  | 2367 |  | 
|  | 2368 | status_t MediaPlayerService::AudioOutput::getRoutedDeviceId(audio_port_handle_t* deviceId) | 
|  | 2369 | { | 
|  | 2370 | ALOGV("getRoutedDeviceId"); | 
|  | 2371 | Mutex::Autolock lock(mLock); | 
|  | 2372 | if (mTrack != 0) { | 
| jiabin | 161b64fc | 2017-11-17 09:31:48 -0800 | [diff] [blame] | 2373 | mRoutedDeviceId = mTrack->getRoutedDeviceId(); | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 2374 | } | 
| jiabin | 161b64fc | 2017-11-17 09:31:48 -0800 | [diff] [blame] | 2375 | *deviceId = mRoutedDeviceId; | 
|  | 2376 | return NO_ERROR; | 
| jiabin | 156c687 | 2017-10-06 09:47:15 -0700 | [diff] [blame] | 2377 | } | 
|  | 2378 |  | 
|  | 2379 | status_t MediaPlayerService::AudioOutput::enableAudioDeviceCallback(bool enabled) | 
|  | 2380 | { | 
|  | 2381 | ALOGV("enableAudioDeviceCallback, %d", enabled); | 
|  | 2382 | Mutex::Autolock lock(mLock); | 
|  | 2383 | mDeviceCallbackEnabled = enabled; | 
|  | 2384 | if (mTrack != 0) { | 
|  | 2385 | status_t status; | 
|  | 2386 | if (enabled) { | 
|  | 2387 | status = mTrack->addAudioDeviceCallback(mDeviceCallback.promote()); | 
|  | 2388 | } else { | 
|  | 2389 | status = mTrack->removeAudioDeviceCallback(mDeviceCallback.promote()); | 
|  | 2390 | } | 
|  | 2391 | return status; | 
|  | 2392 | } | 
|  | 2393 | return NO_ERROR; | 
|  | 2394 | } | 
|  | 2395 |  | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2396 | VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper( | 
|  | 2397 | const sp<VolumeShaper::Configuration>& configuration, | 
|  | 2398 | const sp<VolumeShaper::Operation>& operation) | 
|  | 2399 | { | 
|  | 2400 | Mutex::Autolock lock(mLock); | 
|  | 2401 | ALOGV("AudioOutput::applyVolumeShaper"); | 
| Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2402 |  | 
| Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2403 | mVolumeHandler->setIdIfNecessary(configuration); | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2404 |  | 
|  | 2405 | VolumeShaper::Status status; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2406 | if (mTrack != 0) { | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2407 | status = mTrack->applyVolumeShaper(configuration, operation); | 
|  | 2408 | if (status >= 0) { | 
|  | 2409 | (void)mVolumeHandler->applyVolumeShaper(configuration, operation); | 
| Andy Hung | f370264 | 2017-05-05 17:33:32 -0700 | [diff] [blame] | 2410 | if (mTrack->isPlaying()) { // match local AudioTrack to properly restore. | 
|  | 2411 | mVolumeHandler->setStarted(); | 
|  | 2412 | } | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2413 | } | 
| Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2414 | } else { | 
| Andy Hung | c01eddb | 2017-07-21 16:42:41 -0700 | [diff] [blame] | 2415 | // VolumeShapers are not affected when a track moves between players for | 
|  | 2416 | // gapless playback (setNextMediaPlayer). | 
|  | 2417 | // We forward VolumeShaper operations that do not change configuration | 
|  | 2418 | // to the new player so that unducking may occur as expected. | 
|  | 2419 | // Unducking is an idempotent operation, same if applied back-to-back. | 
|  | 2420 | if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID | 
|  | 2421 | && mNextOutput != nullptr) { | 
|  | 2422 | ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s", | 
|  | 2423 | configuration->toString().c_str(), operation->toString().c_str()); | 
|  | 2424 | Mutex::Autolock nextLock(mNextOutput->mLock); | 
|  | 2425 |  | 
|  | 2426 | // recycled track should be forwarded from this AudioSink by switchToNextOutput | 
|  | 2427 | sp<AudioTrack> track = mNextOutput->mRecycledTrack; | 
|  | 2428 | if (track != nullptr) { | 
|  | 2429 | ALOGD("Forward VolumeShaper operation to recycled track %p", track.get()); | 
|  | 2430 | (void)track->applyVolumeShaper(configuration, operation); | 
|  | 2431 | } else { | 
|  | 2432 | // There is a small chance that the unduck occurs after the next | 
|  | 2433 | // player has already started, but before it is registered to receive | 
|  | 2434 | // the unduck command. | 
|  | 2435 | track = mNextOutput->mTrack; | 
|  | 2436 | if (track != nullptr) { | 
|  | 2437 | ALOGD("Forward VolumeShaper operation to track %p", track.get()); | 
|  | 2438 | (void)track->applyVolumeShaper(configuration, operation); | 
|  | 2439 | } | 
|  | 2440 | } | 
|  | 2441 | } | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2442 | status = mVolumeHandler->applyVolumeShaper(configuration, operation); | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2443 | } | 
| Andy Hung | 39399b6 | 2017-04-21 15:07:45 -0700 | [diff] [blame] | 2444 | return status; | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2445 | } | 
|  | 2446 |  | 
|  | 2447 | sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id) | 
|  | 2448 | { | 
|  | 2449 | Mutex::Autolock lock(mLock); | 
|  | 2450 | if (mTrack != 0) { | 
|  | 2451 | return mTrack->getVolumeShaperState(id); | 
| Andy Hung | 4ef88d7 | 2017-02-21 19:47:53 -0800 | [diff] [blame] | 2452 | } else { | 
|  | 2453 | return mVolumeHandler->getVolumeShaperState(id); | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2454 | } | 
| Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 2455 | } | 
|  | 2456 |  | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2457 | // static | 
|  | 2458 | void MediaPlayerService::AudioOutput::CallbackWrapper( | 
| Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 2459 | int event, void *cookie, void *info) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2460 | //ALOGV("callbackwrapper"); | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2461 | CallbackData *data = (CallbackData*)cookie; | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2462 | // 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] | 2463 | data->lock(); | 
|  | 2464 | AudioOutput *me = data->getOutput(); | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2465 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2466 | if (me == NULL) { | 
|  | 2467 | // no output set, likely because the track was scheduled to be reused | 
|  | 2468 | // by another player, but the format turned out to be incompatible. | 
|  | 2469 | data->unlock(); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2470 | if (buffer != NULL) { | 
|  | 2471 | buffer->size = 0; | 
|  | 2472 | } | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2473 | return; | 
|  | 2474 | } | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2475 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2476 | switch(event) { | 
|  | 2477 | case AudioTrack::EVENT_MORE_DATA: { | 
|  | 2478 | size_t actualSize = (*me->mCallback)( | 
|  | 2479 | me, buffer->raw, buffer->size, me->mCallbackCookie, | 
|  | 2480 | CB_EVENT_FILL_BUFFER); | 
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2481 |  | 
| Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2482 | // Log when no data is returned from the callback. | 
|  | 2483 | // (1) We may have no data (especially with network streaming sources). | 
|  | 2484 | // (2) We may have reached the EOS and the audio track is not stopped yet. | 
|  | 2485 | // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS. | 
|  | 2486 | // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill). | 
|  | 2487 | // | 
|  | 2488 | // This is a benign busy-wait, with the next data request generated 10 ms or more later; | 
|  | 2489 | // 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] | 2490 |  | 
| Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2491 | ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned"); | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2492 |  | 
|  | 2493 | buffer->size = actualSize; | 
|  | 2494 | } break; | 
|  | 2495 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2496 | case AudioTrack::EVENT_STREAM_END: | 
| Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2497 | // currently only occurs for offloaded callbacks | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2498 | ALOGV("callbackwrapper: deliver EVENT_STREAM_END"); | 
|  | 2499 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, | 
|  | 2500 | me->mCallbackCookie, CB_EVENT_STREAM_END); | 
|  | 2501 | break; | 
|  | 2502 |  | 
|  | 2503 | case AudioTrack::EVENT_NEW_IAUDIOTRACK : | 
|  | 2504 | ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN"); | 
|  | 2505 | (*me->mCallback)(me,  NULL /* buffer */, 0 /* size */, | 
|  | 2506 | me->mCallbackCookie, CB_EVENT_TEAR_DOWN); | 
|  | 2507 | break; | 
|  | 2508 |  | 
| Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2509 | case AudioTrack::EVENT_UNDERRUN: | 
| Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2510 | // This occurs when there is no data available, typically | 
| Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2511 | // when there is a failure to supply data to the AudioTrack.  It can also | 
|  | 2512 | // occur in non-offloaded mode when the audio device comes out of standby. | 
|  | 2513 | // | 
| Andy Hung | 719b46b | 2015-05-31 22:18:25 -0700 | [diff] [blame] | 2514 | // If an AudioTrack underruns it outputs silence. Since this happens suddenly | 
|  | 2515 | // it may sound like an audible pop or glitch. | 
|  | 2516 | // | 
|  | 2517 | // The underrun event is sent once per track underrun; the condition is reset | 
|  | 2518 | // when more data is sent to the AudioTrack. | 
| Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2519 | ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)"); | 
| Glenn Kasten | 421743b | 2015-06-01 08:18:08 -0700 | [diff] [blame] | 2520 | break; | 
|  | 2521 |  | 
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 2522 | default: | 
|  | 2523 | ALOGE("received unknown event type: %d inside CallbackWrapper !", event); | 
| Andreas Huber | 51c1e0e | 2011-04-04 11:43:40 -0700 | [diff] [blame] | 2524 | } | 
|  | 2525 |  | 
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 2526 | data->unlock(); | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 2527 | } | 
|  | 2528 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2529 | audio_session_t MediaPlayerService::AudioOutput::getSessionId() const | 
| Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 2530 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2531 | Mutex::Autolock lock(mLock); | 
| Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 2532 | return mSessionId; | 
|  | 2533 | } | 
|  | 2534 |  | 
| Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 2535 | uint32_t MediaPlayerService::AudioOutput::getSampleRate() const | 
|  | 2536 | { | 
| Andy Hung | d1c7434 | 2015-07-07 16:54:23 -0700 | [diff] [blame] | 2537 | Mutex::Autolock lock(mLock); | 
| Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 2538 | if (mTrack == 0) return 0; | 
|  | 2539 | return mTrack->getSampleRate(); | 
|  | 2540 | } | 
|  | 2541 |  | 
| Andy Hung | f2c87b3 | 2016-04-07 19:49:29 -0700 | [diff] [blame] | 2542 | int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const | 
|  | 2543 | { | 
|  | 2544 | Mutex::Autolock lock(mLock); | 
|  | 2545 | if (mTrack == 0) { | 
|  | 2546 | return 0; | 
|  | 2547 | } | 
|  | 2548 | int64_t duration; | 
|  | 2549 | if (mTrack->getBufferDurationInUs(&duration) != OK) { | 
|  | 2550 | return 0; | 
|  | 2551 | } | 
|  | 2552 | return duration; | 
|  | 2553 | } | 
|  | 2554 |  | 
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2555 | //////////////////////////////////////////////////////////////////////////////// | 
|  | 2556 |  | 
|  | 2557 | struct CallbackThread : public Thread { | 
|  | 2558 | CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink, | 
|  | 2559 | MediaPlayerBase::AudioSink::AudioCallback cb, | 
|  | 2560 | void *cookie); | 
|  | 2561 |  | 
|  | 2562 | protected: | 
|  | 2563 | virtual ~CallbackThread(); | 
|  | 2564 |  | 
|  | 2565 | virtual bool threadLoop(); | 
|  | 2566 |  | 
|  | 2567 | private: | 
|  | 2568 | wp<MediaPlayerBase::AudioSink> mSink; | 
|  | 2569 | MediaPlayerBase::AudioSink::AudioCallback mCallback; | 
|  | 2570 | void *mCookie; | 
|  | 2571 | void *mBuffer; | 
|  | 2572 | size_t mBufferSize; | 
|  | 2573 |  | 
|  | 2574 | CallbackThread(const CallbackThread &); | 
|  | 2575 | CallbackThread &operator=(const CallbackThread &); | 
|  | 2576 | }; | 
|  | 2577 |  | 
|  | 2578 | CallbackThread::CallbackThread( | 
|  | 2579 | const wp<MediaPlayerBase::AudioSink> &sink, | 
|  | 2580 | MediaPlayerBase::AudioSink::AudioCallback cb, | 
|  | 2581 | void *cookie) | 
|  | 2582 | : mSink(sink), | 
|  | 2583 | mCallback(cb), | 
|  | 2584 | mCookie(cookie), | 
|  | 2585 | mBuffer(NULL), | 
|  | 2586 | mBufferSize(0) { | 
|  | 2587 | } | 
|  | 2588 |  | 
|  | 2589 | CallbackThread::~CallbackThread() { | 
|  | 2590 | if (mBuffer) { | 
|  | 2591 | free(mBuffer); | 
|  | 2592 | mBuffer = NULL; | 
|  | 2593 | } | 
|  | 2594 | } | 
|  | 2595 |  | 
|  | 2596 | bool CallbackThread::threadLoop() { | 
|  | 2597 | sp<MediaPlayerBase::AudioSink> sink = mSink.promote(); | 
|  | 2598 | if (sink == NULL) { | 
|  | 2599 | return false; | 
|  | 2600 | } | 
|  | 2601 |  | 
|  | 2602 | if (mBuffer == NULL) { | 
|  | 2603 | mBufferSize = sink->bufferSize(); | 
|  | 2604 | mBuffer = malloc(mBufferSize); | 
|  | 2605 | } | 
|  | 2606 |  | 
|  | 2607 | size_t actualSize = | 
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 2608 | (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie, | 
|  | 2609 | MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER); | 
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2610 |  | 
|  | 2611 | if (actualSize > 0) { | 
|  | 2612 | sink->write(mBuffer, actualSize); | 
| Andy Hung | a31335a | 2014-08-20 17:37:59 -0700 | [diff] [blame] | 2613 | // Could return false on sink->write() error or short count. | 
|  | 2614 | // Not necessarily appropriate but would work for AudioCache behavior. | 
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 2615 | } | 
|  | 2616 |  | 
|  | 2617 | return true; | 
|  | 2618 | } | 
|  | 2619 |  | 
|  | 2620 | //////////////////////////////////////////////////////////////////////////////// | 
|  | 2621 |  | 
| Chong Zhang | e5b9b69 | 2017-05-11 11:44:56 -0700 | [diff] [blame] | 2622 | void MediaPlayerService::addBatteryData(uint32_t params) { | 
|  | 2623 | mBatteryTracker.addBatteryData(params); | 
|  | 2624 | } | 
|  | 2625 |  | 
|  | 2626 | status_t MediaPlayerService::pullBatteryData(Parcel* reply) { | 
|  | 2627 | return mBatteryTracker.pullBatteryData(reply); | 
|  | 2628 | } | 
|  | 2629 |  | 
|  | 2630 | MediaPlayerService::BatteryTracker::BatteryTracker() { | 
|  | 2631 | mBatteryAudio.refCount = 0; | 
|  | 2632 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { | 
|  | 2633 | mBatteryAudio.deviceOn[i] = 0; | 
|  | 2634 | mBatteryAudio.lastTime[i] = 0; | 
|  | 2635 | mBatteryAudio.totalTime[i] = 0; | 
|  | 2636 | } | 
|  | 2637 | // speaker is on by default | 
|  | 2638 | mBatteryAudio.deviceOn[SPEAKER] = 1; | 
|  | 2639 |  | 
|  | 2640 | // reset battery stats | 
|  | 2641 | // if the mediaserver has crashed, battery stats could be left | 
|  | 2642 | // in bad state, reset the state upon service start. | 
|  | 2643 | BatteryNotifier::getInstance().noteResetVideo(); | 
|  | 2644 | } | 
|  | 2645 |  | 
|  | 2646 | void MediaPlayerService::BatteryTracker::addBatteryData(uint32_t params) | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2647 | { | 
|  | 2648 | Mutex::Autolock lock(mLock); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2649 |  | 
|  | 2650 | int32_t time = systemTime() / 1000000L; | 
|  | 2651 |  | 
|  | 2652 | // change audio output devices. This notification comes from AudioFlinger | 
|  | 2653 | if ((params & kBatteryDataSpeakerOn) | 
|  | 2654 | || (params & kBatteryDataOtherAudioDeviceOn)) { | 
|  | 2655 |  | 
|  | 2656 | int deviceOn[NUM_AUDIO_DEVICES]; | 
|  | 2657 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { | 
|  | 2658 | deviceOn[i] = 0; | 
|  | 2659 | } | 
|  | 2660 |  | 
|  | 2661 | if ((params & kBatteryDataSpeakerOn) | 
|  | 2662 | && (params & kBatteryDataOtherAudioDeviceOn)) { | 
|  | 2663 | deviceOn[SPEAKER_AND_OTHER] = 1; | 
|  | 2664 | } else if (params & kBatteryDataSpeakerOn) { | 
|  | 2665 | deviceOn[SPEAKER] = 1; | 
|  | 2666 | } else { | 
|  | 2667 | deviceOn[OTHER_AUDIO_DEVICE] = 1; | 
|  | 2668 | } | 
|  | 2669 |  | 
|  | 2670 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { | 
|  | 2671 | if (mBatteryAudio.deviceOn[i] != deviceOn[i]){ | 
|  | 2672 |  | 
|  | 2673 | if (mBatteryAudio.refCount > 0) { // if playing audio | 
|  | 2674 | if (!deviceOn[i]) { | 
|  | 2675 | mBatteryAudio.lastTime[i] += time; | 
|  | 2676 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; | 
|  | 2677 | mBatteryAudio.lastTime[i] = 0; | 
|  | 2678 | } else { | 
|  | 2679 | mBatteryAudio.lastTime[i] = 0 - time; | 
|  | 2680 | } | 
|  | 2681 | } | 
|  | 2682 |  | 
|  | 2683 | mBatteryAudio.deviceOn[i] = deviceOn[i]; | 
|  | 2684 | } | 
|  | 2685 | } | 
|  | 2686 | return; | 
|  | 2687 | } | 
|  | 2688 |  | 
| Marco Nelissen | b7848f1 | 2014-12-04 08:57:56 -0800 | [diff] [blame] | 2689 | // an audio stream is started | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2690 | if (params & kBatteryDataAudioFlingerStart) { | 
|  | 2691 | // record the start time only if currently no other audio | 
|  | 2692 | // is being played | 
|  | 2693 | if (mBatteryAudio.refCount == 0) { | 
|  | 2694 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { | 
|  | 2695 | if (mBatteryAudio.deviceOn[i]) { | 
|  | 2696 | mBatteryAudio.lastTime[i] -= time; | 
|  | 2697 | } | 
|  | 2698 | } | 
|  | 2699 | } | 
|  | 2700 |  | 
|  | 2701 | mBatteryAudio.refCount ++; | 
|  | 2702 | return; | 
|  | 2703 |  | 
|  | 2704 | } else if (params & kBatteryDataAudioFlingerStop) { | 
|  | 2705 | if (mBatteryAudio.refCount <= 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2706 | ALOGW("Battery track warning: refCount is <= 0"); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2707 | return; | 
|  | 2708 | } | 
|  | 2709 |  | 
|  | 2710 | // record the stop time only if currently this is the only | 
|  | 2711 | // audio being played | 
|  | 2712 | if (mBatteryAudio.refCount == 1) { | 
|  | 2713 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { | 
|  | 2714 | if (mBatteryAudio.deviceOn[i]) { | 
|  | 2715 | mBatteryAudio.lastTime[i] += time; | 
|  | 2716 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; | 
|  | 2717 | mBatteryAudio.lastTime[i] = 0; | 
|  | 2718 | } | 
|  | 2719 | } | 
|  | 2720 | } | 
|  | 2721 |  | 
|  | 2722 | mBatteryAudio.refCount --; | 
|  | 2723 | return; | 
|  | 2724 | } | 
|  | 2725 |  | 
| Andy Hung | 1afd098 | 2016-11-08 16:13:44 -0800 | [diff] [blame] | 2726 | uid_t uid = IPCThreadState::self()->getCallingUid(); | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2727 | if (uid == AID_MEDIA) { | 
|  | 2728 | return; | 
|  | 2729 | } | 
|  | 2730 | int index = mBatteryData.indexOfKey(uid); | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2731 |  | 
|  | 2732 | if (index < 0) { // create a new entry for this UID | 
|  | 2733 | BatteryUsageInfo info; | 
|  | 2734 | info.audioTotalTime = 0; | 
|  | 2735 | info.videoTotalTime = 0; | 
|  | 2736 | info.audioLastTime = 0; | 
|  | 2737 | info.videoLastTime = 0; | 
|  | 2738 | info.refCount = 0; | 
|  | 2739 |  | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2740 | if (mBatteryData.add(uid, info) == NO_MEMORY) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2741 | ALOGE("Battery track error: no memory for new app"); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2742 | return; | 
|  | 2743 | } | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2744 | } | 
|  | 2745 |  | 
|  | 2746 | BatteryUsageInfo &info = mBatteryData.editValueFor(uid); | 
|  | 2747 |  | 
|  | 2748 | if (params & kBatteryDataCodecStarted) { | 
|  | 2749 | if (params & kBatteryDataTrackAudio) { | 
|  | 2750 | info.audioLastTime -= time; | 
|  | 2751 | info.refCount ++; | 
|  | 2752 | } | 
|  | 2753 | if (params & kBatteryDataTrackVideo) { | 
|  | 2754 | info.videoLastTime -= time; | 
|  | 2755 | info.refCount ++; | 
|  | 2756 | } | 
|  | 2757 | } else { | 
|  | 2758 | if (info.refCount == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2759 | ALOGW("Battery track warning: refCount is already 0"); | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2760 | return; | 
|  | 2761 | } else if (info.refCount < 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2762 | ALOGE("Battery track error: refCount < 0"); | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2763 | mBatteryData.removeItem(uid); | 
|  | 2764 | return; | 
|  | 2765 | } | 
|  | 2766 |  | 
|  | 2767 | if (params & kBatteryDataTrackAudio) { | 
|  | 2768 | info.audioLastTime += time; | 
|  | 2769 | info.refCount --; | 
|  | 2770 | } | 
|  | 2771 | if (params & kBatteryDataTrackVideo) { | 
|  | 2772 | info.videoLastTime += time; | 
|  | 2773 | info.refCount --; | 
|  | 2774 | } | 
|  | 2775 |  | 
|  | 2776 | // no stream is being played by this UID | 
|  | 2777 | if (info.refCount == 0) { | 
|  | 2778 | info.audioTotalTime += info.audioLastTime; | 
|  | 2779 | info.audioLastTime = 0; | 
|  | 2780 | info.videoTotalTime += info.videoLastTime; | 
|  | 2781 | info.videoLastTime = 0; | 
|  | 2782 | } | 
|  | 2783 | } | 
|  | 2784 | } | 
|  | 2785 |  | 
| Chong Zhang | e5b9b69 | 2017-05-11 11:44:56 -0700 | [diff] [blame] | 2786 | status_t MediaPlayerService::BatteryTracker::pullBatteryData(Parcel* reply) { | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2787 | Mutex::Autolock lock(mLock); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2788 |  | 
|  | 2789 | // audio output devices usage | 
|  | 2790 | int32_t time = systemTime() / 1000000L; //in ms | 
|  | 2791 | int32_t totalTime; | 
|  | 2792 |  | 
|  | 2793 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { | 
|  | 2794 | totalTime = mBatteryAudio.totalTime[i]; | 
|  | 2795 |  | 
|  | 2796 | if (mBatteryAudio.deviceOn[i] | 
|  | 2797 | && (mBatteryAudio.lastTime[i] != 0)) { | 
|  | 2798 | int32_t tmpTime = mBatteryAudio.lastTime[i] + time; | 
|  | 2799 | totalTime += tmpTime; | 
|  | 2800 | } | 
|  | 2801 |  | 
|  | 2802 | reply->writeInt32(totalTime); | 
|  | 2803 | // reset the total time | 
|  | 2804 | mBatteryAudio.totalTime[i] = 0; | 
|  | 2805 | } | 
|  | 2806 |  | 
|  | 2807 | // codec usage | 
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2808 | BatteryUsageInfo info; | 
|  | 2809 | int size = mBatteryData.size(); | 
|  | 2810 |  | 
|  | 2811 | reply->writeInt32(size); | 
|  | 2812 | int i = 0; | 
|  | 2813 |  | 
|  | 2814 | while (i < size) { | 
|  | 2815 | info = mBatteryData.valueAt(i); | 
|  | 2816 |  | 
|  | 2817 | reply->writeInt32(mBatteryData.keyAt(i)); //UID | 
|  | 2818 | reply->writeInt32(info.audioTotalTime); | 
|  | 2819 | reply->writeInt32(info.videoTotalTime); | 
|  | 2820 |  | 
|  | 2821 | info.audioTotalTime = 0; | 
|  | 2822 | info.videoTotalTime = 0; | 
|  | 2823 |  | 
|  | 2824 | // remove the UID entry where no stream is being played | 
|  | 2825 | if (info.refCount <= 0) { | 
|  | 2826 | mBatteryData.removeItemsAt(i); | 
|  | 2827 | size --; | 
|  | 2828 | i --; | 
|  | 2829 | } | 
|  | 2830 | i++; | 
|  | 2831 | } | 
|  | 2832 | return NO_ERROR; | 
|  | 2833 | } | 
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 2834 | } // namespace android |