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