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