| 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 | |
| Chong Zhang | 0b30fd4 | 2014-07-23 14:46:05 -0700 | [diff] [blame] | 37 | #include <binder/IBatteryStats.h> |
| Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 38 | #include <binder/IPCThreadState.h> |
| 39 | #include <binder/IServiceManager.h> |
| 40 | #include <binder/MemoryHeapBase.h> |
| 41 | #include <binder/MemoryBase.h> |
| Mathias Agopian | b1e7cd1 | 2013-02-14 17:11:27 -0800 | [diff] [blame] | 42 | #include <gui/Surface.h> |
| Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 43 | #include <utils/Errors.h> // for status_t |
| 44 | #include <utils/String8.h> |
| Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 45 | #include <utils/SystemClock.h> |
| Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 46 | #include <utils/Timers.h> |
| Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 47 | #include <utils/Vector.h> |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 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> |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 64 | #include <system/audio.h> |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 65 | |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 66 | #include <private/android_filesystem_config.h> |
| 67 | |
| James Dong | 559bf28 | 2012-03-28 10:29:14 -0700 | [diff] [blame] | 68 | #include "ActivityManager.h" |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | #include "MediaRecorderClient.h" |
| 70 | #include "MediaPlayerService.h" |
| 71 | #include "MetadataRetrieverClient.h" |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 72 | #include "MediaPlayerFactory.h" |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
| Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 74 | #include "TestPlayerStub.h" |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 75 | #include "StagefrightPlayer.h" |
| Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 76 | #include "nuplayer/NuPlayerDriver.h" |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 77 | |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 78 | #include <OMX.h> |
| Nicolas Catania | 14d2747 | 2009-07-13 14:37:49 -0700 | [diff] [blame] | 79 | |
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 80 | #include "Crypto.h" |
| Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 81 | #include "Drm.h" |
| Andreas Huber | 59451f8 | 2012-09-18 10:36:32 -0700 | [diff] [blame] | 82 | #include "HDCP.h" |
| Andreas Huber | b7319a7 | 2013-05-29 14:20:52 -0700 | [diff] [blame] | 83 | #include "HTTPBase.h" |
| Andreas Huber | 35213f1 | 2012-08-29 11:41:50 -0700 | [diff] [blame] | 84 | #include "RemoteDisplay.h" |
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 85 | |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 86 | namespace { |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 87 | using android::media::Metadata; |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 88 | using android::status_t; |
| 89 | using android::OK; |
| 90 | using android::BAD_VALUE; |
| 91 | using android::NOT_ENOUGH_DATA; |
| 92 | using android::Parcel; |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 93 | |
| 94 | // Max number of entries in the filter. |
| 95 | const int kMaxFilterSize = 64; // I pulled that out of thin air. |
| 96 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 97 | // FIXME: Move all the metadata related function in the Metadata.cpp |
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 98 | |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 99 | |
| 100 | // Unmarshall a filter from a Parcel. |
| 101 | // Filter format in a parcel: |
| 102 | // |
| 103 | // 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 |
| 104 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 105 | // | number of entries (n) | |
| 106 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 107 | // | metadata type 1 | |
| 108 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | // | metadata type 2 | |
| 110 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 111 | // .... |
| 112 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 113 | // | metadata type n | |
| 114 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 115 | // |
| 116 | // @param p Parcel that should start with a filter. |
| 117 | // @param[out] filter On exit contains the list of metadata type to be |
| 118 | // filtered. |
| 119 | // @param[out] status On exit contains the status code to be returned. |
| 120 | // @return true if the parcel starts with a valid filter. |
| 121 | bool unmarshallFilter(const Parcel& p, |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 122 | Metadata::Filter *filter, |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 123 | status_t *status) |
| 124 | { |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 125 | int32_t val; |
| 126 | if (p.readInt32(&val) != OK) |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 127 | { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 128 | ALOGE("Failed to read filter's length"); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 129 | *status = NOT_ENOUGH_DATA; |
| 130 | return false; |
| 131 | } |
| 132 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 133 | if( val > kMaxFilterSize || val < 0) |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 134 | { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 135 | ALOGE("Invalid filter len %d", val); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 136 | *status = BAD_VALUE; |
| 137 | return false; |
| 138 | } |
| 139 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 140 | const size_t num = val; |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 141 | |
| 142 | filter->clear(); |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 143 | filter->setCapacity(num); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 144 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 145 | size_t size = num * sizeof(Metadata::Type); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 146 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 147 | |
| 148 | if (p.dataAvail() < size) |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 149 | { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 150 | ALOGE("Filter too short expected %d but got %d", size, p.dataAvail()); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 151 | *status = NOT_ENOUGH_DATA; |
| 152 | return false; |
| 153 | } |
| 154 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 155 | const Metadata::Type *data = |
| 156 | static_cast<const Metadata::Type*>(p.readInplace(size)); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 157 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 158 | if (NULL == data) |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 159 | { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 160 | ALOGE("Filter had no data"); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 161 | *status = BAD_VALUE; |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // TODO: The stl impl of vector would be more efficient here |
| 166 | // because it degenerates into a memcpy on pod types. Try to |
| 167 | // replace later or use stl::set. |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 168 | for (size_t i = 0; i < num; ++i) |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 169 | { |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 170 | filter->add(*data); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 171 | ++data; |
| 172 | } |
| 173 | *status = OK; |
| 174 | return true; |
| 175 | } |
| 176 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 177 | // @param filter Of metadata type. |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 178 | // @param val To be searched. |
| 179 | // @return true if a match was found. |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 180 | bool findMetadata(const Metadata::Filter& filter, const int32_t val) |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 181 | { |
| 182 | // Deal with empty and ANY right away |
| 183 | if (filter.isEmpty()) return false; |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 184 | if (filter[0] == Metadata::kAny) return true; |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 185 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 186 | return filter.indexOf(val) >= 0; |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } // anonymous namespace |
| 190 | |
| 191 | |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 192 | namespace { |
| 193 | using android::Parcel; |
| 194 | using android::String16; |
| 195 | |
| 196 | // marshalling tag indicating flattened utf16 tags |
| 197 | // keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java |
| 198 | const int32_t kAudioAttributesMarshallTagFlattenTags = 1; |
| 199 | |
| 200 | // Audio attributes format in a parcel: |
| 201 | // |
| 202 | // 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 |
| 203 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 204 | // | usage | |
| 205 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 206 | // | content_type | |
| 207 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| Hyejin Kim | 4f418f9 | 2014-09-05 15:50:03 +0900 | [diff] [blame] | 208 | // | source | |
| 209 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 210 | // | flags | |
| 211 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 212 | // | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found |
| 213 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 214 | // | flattened tags in UTF16 | |
| 215 | // | ... | |
| 216 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 217 | // |
| 218 | // @param p Parcel that contains audio attributes. |
| 219 | // @param[out] attributes On exit points to an initialized audio_attributes_t structure |
| 220 | // @param[out] status On exit contains the status code to be returned. |
| 221 | void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes) |
| 222 | { |
| 223 | attributes->usage = (audio_usage_t) parcel.readInt32(); |
| 224 | attributes->content_type = (audio_content_type_t) parcel.readInt32(); |
| Hyejin Kim | 4f418f9 | 2014-09-05 15:50:03 +0900 | [diff] [blame] | 225 | attributes->source = (audio_source_t) parcel.readInt32(); |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 226 | attributes->flags = (audio_flags_mask_t) parcel.readInt32(); |
| 227 | const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags); |
| 228 | if (hasFlattenedTag) { |
| 229 | // the tags are UTF16, convert to UTF8 |
| 230 | String16 tags = parcel.readString16(); |
| 231 | ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size()); |
| 232 | if (realTagSize <= 0) { |
| 233 | strcpy(attributes->tags, ""); |
| 234 | } else { |
| 235 | // copy the flattened string into the attributes as the destination for the conversion: |
| 236 | // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it |
| 237 | size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ? |
| 238 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize; |
| 239 | utf16_to_utf8(tags.string(), tagSize, attributes->tags); |
| 240 | } |
| 241 | } else { |
| 242 | ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values"); |
| 243 | strcpy(attributes->tags, ""); |
| 244 | } |
| 245 | } |
| 246 | } // anonymous namespace |
| 247 | |
| 248 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | namespace android { |
| 250 | |
| Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 251 | extern ALooperRoster gLooperRoster; |
| 252 | |
| 253 | |
| Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 254 | static bool checkPermission(const char* permissionString) { |
| 255 | #ifndef HAVE_ANDROID_OS |
| 256 | return true; |
| 257 | #endif |
| 258 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 259 | bool ok = checkCallingPermission(String16(permissionString)); |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 260 | if (!ok) ALOGE("Request requires %s", permissionString); |
| Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 261 | return ok; |
| 262 | } |
| 263 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | // 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] | 265 | /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4; |
| 266 | /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false; |
| 267 | |
| 268 | void MediaPlayerService::instantiate() { |
| 269 | defaultServiceManager()->addService( |
| 270 | String16("media.player"), new MediaPlayerService()); |
| 271 | } |
| 272 | |
| 273 | MediaPlayerService::MediaPlayerService() |
| 274 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 275 | ALOGV("MediaPlayerService created"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | mNextConnId = 1; |
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 277 | |
| 278 | mBatteryAudio.refCount = 0; |
| 279 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 280 | mBatteryAudio.deviceOn[i] = 0; |
| 281 | mBatteryAudio.lastTime[i] = 0; |
| 282 | mBatteryAudio.totalTime[i] = 0; |
| 283 | } |
| 284 | // speaker is on by default |
| 285 | mBatteryAudio.deviceOn[SPEAKER] = 1; |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 286 | |
| Chong Zhang | 0b30fd4 | 2014-07-23 14:46:05 -0700 | [diff] [blame] | 287 | // reset battery stats |
| 288 | // if the mediaserver has crashed, battery stats could be left |
| 289 | // in bad state, reset the state upon service start. |
| 290 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 291 | if (sm != NULL) { |
| 292 | const String16 name("batterystats"); |
| 293 | sp<IBatteryStats> batteryStats = |
| 294 | interface_cast<IBatteryStats>(sm->getService(name)); |
| 295 | if (batteryStats != NULL) { |
| 296 | batteryStats->noteResetVideo(); |
| 297 | batteryStats->noteResetAudio(); |
| 298 | } |
| 299 | } |
| 300 | |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 301 | MediaPlayerFactory::registerBuiltinFactories(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | MediaPlayerService::~MediaPlayerService() |
| 305 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 306 | ALOGV("MediaPlayerService destroyed"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 309 | sp<IMediaRecorder> MediaPlayerService::createMediaRecorder() |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | { |
| Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 311 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 312 | sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid); |
| 313 | wp<MediaRecorderClient> w = recorder; |
| 314 | Mutex::Autolock lock(mLock); |
| 315 | mMediaRecorderClients.add(w); |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 316 | 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] | 317 | return recorder; |
| 318 | } |
| 319 | |
| Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 320 | void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client) |
| 321 | { |
| 322 | Mutex::Autolock lock(mLock); |
| 323 | mMediaRecorderClients.remove(client); |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 324 | ALOGV("Delete media recorder client"); |
| Gloria Wang | dac6a31 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 327 | sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever() |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | { |
| Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 329 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid); |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 331 | ALOGV("Create new media retriever from pid %d", pid); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | return retriever; |
| 333 | } |
| 334 | |
| Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 335 | sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client, |
| Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 336 | int audioSessionId) |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | { |
| Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 338 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | int32_t connId = android_atomic_inc(&mNextConnId); |
| Andreas Huber | 9b80c2b | 2011-06-30 15:47:02 -0700 | [diff] [blame] | 340 | |
| 341 | sp<Client> c = new Client( |
| 342 | this, pid, connId, client, audioSessionId, |
| 343 | IPCThreadState::self()->getCallingUid()); |
| 344 | |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 345 | ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid, |
| Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 346 | IPCThreadState::self()->getCallingUid()); |
| 347 | |
| 348 | wp<Client> w = c; |
| 349 | { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | Mutex::Autolock lock(mLock); |
| 351 | mClients.add(w); |
| 352 | } |
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 353 | return c; |
| 354 | } |
| 355 | |
| Lajos Molnar | 1381d4b | 2014-08-07 15:18:35 -0700 | [diff] [blame] | 356 | sp<IMediaCodecList> MediaPlayerService::getCodecList() const { |
| 357 | return MediaCodecList::getLocalInstance(); |
| 358 | } |
| 359 | |
| Andreas Huber | 318ad9c | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 360 | sp<IOMX> MediaPlayerService::getOMX() { |
| 361 | Mutex::Autolock autoLock(mLock); |
| 362 | |
| 363 | if (mOMX.get() == NULL) { |
| 364 | mOMX = new OMX; |
| 365 | } |
| 366 | |
| 367 | return mOMX; |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 370 | sp<ICrypto> MediaPlayerService::makeCrypto() { |
| Andreas Huber | 1bd139a | 2012-04-03 14:19:20 -0700 | [diff] [blame] | 371 | return new Crypto; |
| Andreas Huber | ed3e3e0 | 2012-03-26 11:13:27 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| Jeff Tinker | cc82dc6 | 2013-02-08 10:18:35 -0800 | [diff] [blame] | 374 | sp<IDrm> MediaPlayerService::makeDrm() { |
| 375 | return new Drm; |
| 376 | } |
| 377 | |
| Andreas Huber | 279dcd8 | 2013-01-30 10:41:25 -0800 | [diff] [blame] | 378 | sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) { |
| 379 | return new HDCP(createEncryptionModule); |
| Andreas Huber | 59451f8 | 2012-09-18 10:36:32 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| Jeff Brown | 2013a54 | 2012-09-04 21:38:42 -0700 | [diff] [blame] | 382 | sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay( |
| 383 | const sp<IRemoteDisplayClient>& client, const String8& iface) { |
| Jeff Brown | aba33d5 | 2012-09-07 17:38:58 -0700 | [diff] [blame] | 384 | if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) { |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| Jeff Brown | ced24b3 | 2012-09-05 17:48:03 -0700 | [diff] [blame] | 388 | return new RemoteDisplay(client, iface.string()); |
| Jeff Brown | 2013a54 | 2012-09-04 21:38:42 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const |
| 392 | { |
| 393 | const size_t SIZE = 256; |
| 394 | char buffer[SIZE]; |
| 395 | String8 result; |
| 396 | |
| 397 | result.append(" AudioOutput\n"); |
| 398 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", |
| 399 | mStreamType, mLeftVolume, mRightVolume); |
| 400 | result.append(buffer); |
| 401 | snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n", |
| Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 402 | mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | result.append(buffer); |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 404 | snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n", |
| 405 | mAuxEffectId, mSendLevel); |
| 406 | result.append(buffer); |
| 407 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | ::write(fd, result.string(), result.size()); |
| 409 | if (mTrack != 0) { |
| 410 | mTrack->dump(fd, args); |
| 411 | } |
| 412 | return NO_ERROR; |
| 413 | } |
| 414 | |
| 415 | status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const |
| 416 | { |
| 417 | const size_t SIZE = 256; |
| 418 | char buffer[SIZE]; |
| 419 | String8 result; |
| 420 | result.append(" Client\n"); |
| 421 | snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n", |
| 422 | mPid, mConnId, mStatus, mLoop?"true": "false"); |
| 423 | result.append(buffer); |
| 424 | write(fd, result.string(), result.size()); |
| Andreas Huber | a0b1d4b | 2011-06-07 15:52:25 -0700 | [diff] [blame] | 425 | if (mPlayer != NULL) { |
| 426 | mPlayer->dump(fd, args); |
| 427 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | if (mAudioOutput != 0) { |
| 429 | mAudioOutput->dump(fd, args); |
| 430 | } |
| 431 | write(fd, "\n", 1); |
| 432 | return NO_ERROR; |
| 433 | } |
| 434 | |
| Marco Nelissen | f09611f | 2015-02-13 14:12:42 -0800 | [diff] [blame] | 435 | /** |
| 436 | * The only arguments this understands right now are -c, -von and -voff, |
| 437 | * which are parsed by ALooperRoster::dump() |
| 438 | */ |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | status_t MediaPlayerService::dump(int fd, const Vector<String16>& args) |
| 440 | { |
| 441 | const size_t SIZE = 256; |
| 442 | char buffer[SIZE]; |
| 443 | String8 result; |
| 444 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 445 | snprintf(buffer, SIZE, "Permission Denial: " |
| 446 | "can't dump MediaPlayerService from pid=%d, uid=%d\n", |
| 447 | IPCThreadState::self()->getCallingPid(), |
| 448 | IPCThreadState::self()->getCallingUid()); |
| 449 | result.append(buffer); |
| 450 | } else { |
| 451 | Mutex::Autolock lock(mLock); |
| 452 | for (int i = 0, n = mClients.size(); i < n; ++i) { |
| 453 | sp<Client> c = mClients[i].promote(); |
| 454 | if (c != 0) c->dump(fd, args); |
| 455 | } |
| James Dong | b914122 | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 456 | if (mMediaRecorderClients.size() == 0) { |
| 457 | result.append(" No media recorder client\n\n"); |
| 458 | } else { |
| 459 | for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) { |
| 460 | sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote(); |
| James Dong | e579e28 | 2011-10-18 22:29:20 -0700 | [diff] [blame] | 461 | if (c != 0) { |
| 462 | snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid); |
| 463 | result.append(buffer); |
| 464 | write(fd, result.string(), result.size()); |
| 465 | result = "\n"; |
| 466 | c->dump(fd, args); |
| 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) { |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 636 | p = MediaPlayerFactory::createPlayer(playerType, this, notify); |
| 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()) { |
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 658 | mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(), |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 659 | mPid, mAudioAttributes); |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 660 | static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput); |
| 661 | } |
| 662 | |
| 663 | return p; |
| 664 | } |
| 665 | |
| 666 | void MediaPlayerService::Client::setDataSource_post( |
| 667 | const sp<MediaPlayerBase>& p, |
| 668 | status_t status) |
| 669 | { |
| 670 | ALOGV(" setDataSource"); |
| 671 | mStatus = status; |
| 672 | if (mStatus != OK) { |
| 673 | ALOGE(" error: %d", mStatus); |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | // Set the re-transmission endpoint if one was chosen. |
| 678 | if (mRetransmitEndpointValid) { |
| 679 | mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint); |
| 680 | if (mStatus != NO_ERROR) { |
| 681 | ALOGE("setRetransmitEndpoint error: %d", mStatus); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if (mStatus == OK) { |
| 686 | mPlayer = p; |
| 687 | } |
| 688 | } |
| 689 | |
| Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 690 | status_t MediaPlayerService::Client::setDataSource( |
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 691 | const sp<IMediaHTTPService> &httpService, |
| 692 | const char *url, |
| 693 | const KeyedVector<String8, String8> *headers) |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 695 | ALOGV("setDataSource(%s)", url); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | if (url == NULL) |
| 697 | return UNKNOWN_ERROR; |
| 698 | |
| Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 699 | if ((strncmp(url, "http://", 7) == 0) || |
| 700 | (strncmp(url, "https://", 8) == 0) || |
| 701 | (strncmp(url, "rtsp://", 7) == 0)) { |
| 702 | if (!checkPermission("android.permission.INTERNET")) { |
| 703 | return PERMISSION_DENIED; |
| 704 | } |
| 705 | } |
| 706 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | if (strncmp(url, "content://", 10) == 0) { |
| 708 | // get a filedescriptor for the content Uri and |
| 709 | // pass it to the setDataSource(fd) method |
| 710 | |
| 711 | String16 url16(url); |
| 712 | int fd = android::openContentProviderFile(url16); |
| 713 | if (fd < 0) |
| 714 | { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 715 | ALOGE("Couldn't open fd for %s", url); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | return UNKNOWN_ERROR; |
| 717 | } |
| 718 | setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus |
| 719 | close(fd); |
| 720 | return mStatus; |
| 721 | } else { |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 722 | player_type playerType = MediaPlayerFactory::getPlayerType(this, url); |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 723 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 724 | if (p == NULL) { |
| 725 | return NO_INIT; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 728 | setDataSource_post(p, p->setDataSource(httpService, url, headers)); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | return mStatus; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length) |
| 734 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 735 | 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] | 736 | struct stat sb; |
| 737 | int ret = fstat(fd, &sb); |
| 738 | if (ret != 0) { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 739 | 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] | 740 | return UNKNOWN_ERROR; |
| 741 | } |
| 742 | |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 743 | ALOGV("st_dev = %llu", sb.st_dev); |
| 744 | ALOGV("st_mode = %u", sb.st_mode); |
| Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 745 | ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid)); |
| 746 | ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid)); |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 747 | ALOGV("st_size = %llu", sb.st_size); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | |
| 749 | if (offset >= sb.st_size) { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 750 | ALOGE("offset error"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | return UNKNOWN_ERROR; |
| 752 | } |
| 753 | if (offset + length > sb.st_size) { |
| 754 | length = sb.st_size - offset; |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 755 | ALOGV("calculated length = %lld", length); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 758 | player_type playerType = MediaPlayerFactory::getPlayerType(this, |
| 759 | fd, |
| 760 | offset, |
| 761 | length); |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 762 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| 763 | if (p == NULL) { |
| 764 | return NO_INIT; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | // now set data source |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 768 | setDataSource_post(p, p->setDataSource(fd, offset, length)); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | return mStatus; |
| 770 | } |
| 771 | |
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 772 | status_t MediaPlayerService::Client::setDataSource( |
| 773 | const sp<IStreamSource> &source) { |
| 774 | // create the right type of player |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 775 | player_type playerType = MediaPlayerFactory::getPlayerType(this, source); |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 776 | sp<MediaPlayerBase> p = setDataSource_pre(playerType); |
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 777 | if (p == NULL) { |
| 778 | return NO_INIT; |
| 779 | } |
| 780 | |
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 781 | // now set data source |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 782 | setDataSource_post(p, p->setDataSource(source)); |
| Andreas Huber | e2b1028 | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 783 | return mStatus; |
| 784 | } |
| 785 | |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 786 | void MediaPlayerService::Client::disconnectNativeWindow() { |
| 787 | if (mConnectedWindow != NULL) { |
| 788 | status_t err = native_window_api_disconnect(mConnectedWindow.get(), |
| 789 | NATIVE_WINDOW_API_MEDIA); |
| 790 | |
| 791 | if (err != OK) { |
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 792 | ALOGW("native_window_api_disconnect returned an error: %s (%d)", |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 793 | strerror(-err), err); |
| 794 | } |
| 795 | } |
| 796 | mConnectedWindow.clear(); |
| 797 | } |
| 798 | |
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 799 | status_t MediaPlayerService::Client::setVideoSurfaceTexture( |
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 800 | const sp<IGraphicBufferProducer>& bufferProducer) |
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 801 | { |
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 802 | ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get()); |
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 803 | sp<MediaPlayerBase> p = getPlayer(); |
| 804 | if (p == 0) return UNKNOWN_ERROR; |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 805 | |
| Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 806 | sp<IBinder> binder(IInterface::asBinder(bufferProducer)); |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 807 | if (mConnectedWindowBinder == binder) { |
| 808 | return OK; |
| 809 | } |
| 810 | |
| 811 | sp<ANativeWindow> anw; |
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 812 | if (bufferProducer != NULL) { |
| Marco Nelissen | ee08f7e | 2013-09-16 13:30:01 -0700 | [diff] [blame] | 813 | anw = new Surface(bufferProducer, true /* controlledByApp */); |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 814 | status_t err = native_window_api_connect(anw.get(), |
| 815 | NATIVE_WINDOW_API_MEDIA); |
| 816 | |
| 817 | if (err != OK) { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 818 | ALOGE("setVideoSurfaceTexture failed: %d", err); |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 819 | // Note that we must do the reset before disconnecting from the ANW. |
| 820 | // Otherwise queue/dequeue calls could be made on the disconnected |
| 821 | // ANW, which may result in errors. |
| 822 | reset(); |
| 823 | |
| 824 | disconnectNativeWindow(); |
| 825 | |
| 826 | return err; |
| 827 | } |
| 828 | } |
| 829 | |
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 830 | // Note that we must set the player's new GraphicBufferProducer before |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 831 | // disconnecting the old one. Otherwise queue/dequeue calls could be made |
| 832 | // on the disconnected ANW, which may result in errors. |
| Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 833 | status_t err = p->setVideoSurfaceTexture(bufferProducer); |
| Jamie Gennis | 7dae00b | 2011-10-26 18:36:31 -0700 | [diff] [blame] | 834 | |
| 835 | disconnectNativeWindow(); |
| 836 | |
| 837 | mConnectedWindow = anw; |
| 838 | |
| 839 | if (err == OK) { |
| 840 | mConnectedWindowBinder = binder; |
| 841 | } else { |
| 842 | disconnectNativeWindow(); |
| 843 | } |
| 844 | |
| 845 | return err; |
| Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 848 | status_t MediaPlayerService::Client::invoke(const Parcel& request, |
| 849 | Parcel *reply) |
| 850 | { |
| 851 | sp<MediaPlayerBase> p = getPlayer(); |
| 852 | if (p == NULL) return UNKNOWN_ERROR; |
| 853 | return p->invoke(request, reply); |
| 854 | } |
| 855 | |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 856 | // This call doesn't need to access the native player. |
| 857 | status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter) |
| 858 | { |
| 859 | status_t status; |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 860 | media::Metadata::Filter allow, drop; |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 861 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 862 | if (unmarshallFilter(filter, &allow, &status) && |
| 863 | unmarshallFilter(filter, &drop, &status)) { |
| 864 | Mutex::Autolock lock(mLock); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 865 | |
| 866 | mMetadataAllow = allow; |
| 867 | mMetadataDrop = drop; |
| 868 | } |
| 869 | return status; |
| 870 | } |
| 871 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 872 | status_t MediaPlayerService::Client::getMetadata( |
| Mark Salyzyn | 77342f7 | 2014-06-18 16:31:32 -0700 | [diff] [blame] | 873 | bool update_only, bool /*apply_filter*/, Parcel *reply) |
| Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 874 | { |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 875 | sp<MediaPlayerBase> player = getPlayer(); |
| 876 | if (player == 0) return UNKNOWN_ERROR; |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 877 | |
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 878 | status_t status; |
| 879 | // Placeholder for the return code, updated by the caller. |
| 880 | reply->writeInt32(-1); |
| 881 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 882 | media::Metadata::Filter ids; |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 883 | |
| 884 | // We don't block notifications while we fetch the data. We clear |
| 885 | // mMetadataUpdated first so we don't lose notifications happening |
| 886 | // during the rest of this call. |
| 887 | { |
| 888 | Mutex::Autolock lock(mLock); |
| 889 | if (update_only) { |
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 890 | ids = mMetadataUpdated; |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 891 | } |
| 892 | mMetadataUpdated.clear(); |
| 893 | } |
| Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 894 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 895 | media::Metadata metadata(reply); |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 896 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 897 | metadata.appendHeader(); |
| 898 | status = player->getMetadata(ids, reply); |
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 899 | |
| 900 | if (status != OK) { |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 901 | metadata.resetParcel(); |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 902 | ALOGE("getMetadata failed %d", status); |
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 903 | return status; |
| 904 | } |
| 905 | |
| 906 | // FIXME: Implement filtering on the result. Not critical since |
| 907 | // filtering takes place on the update notifications already. This |
| 908 | // would be when all the metadata are fetch and a filter is set. |
| 909 | |
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 910 | // Everything is fine, update the metadata length. |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 911 | metadata.updateLength(); |
| niko | d608a81 | 2009-07-16 16:39:53 -0700 | [diff] [blame] | 912 | return OK; |
| Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | status_t MediaPlayerService::Client::prepareAsync() |
| 916 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 917 | ALOGV("[%d] prepareAsync", mConnId); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | sp<MediaPlayerBase> p = getPlayer(); |
| 919 | if (p == 0) return UNKNOWN_ERROR; |
| 920 | status_t ret = p->prepareAsync(); |
| 921 | #if CALLBACK_ANTAGONIZER |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 922 | ALOGD("start Antagonizer"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | if (ret == NO_ERROR) mAntagonizer->start(); |
| 924 | #endif |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | status_t MediaPlayerService::Client::start() |
| 929 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 930 | ALOGV("[%d] start", mConnId); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | sp<MediaPlayerBase> p = getPlayer(); |
| 932 | if (p == 0) return UNKNOWN_ERROR; |
| 933 | p->setLooping(mLoop); |
| 934 | return p->start(); |
| 935 | } |
| 936 | |
| 937 | status_t MediaPlayerService::Client::stop() |
| 938 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 939 | ALOGV("[%d] stop", mConnId); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | sp<MediaPlayerBase> p = getPlayer(); |
| 941 | if (p == 0) return UNKNOWN_ERROR; |
| 942 | return p->stop(); |
| 943 | } |
| 944 | |
| 945 | status_t MediaPlayerService::Client::pause() |
| 946 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 947 | ALOGV("[%d] pause", mConnId); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | sp<MediaPlayerBase> p = getPlayer(); |
| 949 | if (p == 0) return UNKNOWN_ERROR; |
| 950 | return p->pause(); |
| 951 | } |
| 952 | |
| 953 | status_t MediaPlayerService::Client::isPlaying(bool* state) |
| 954 | { |
| 955 | *state = false; |
| 956 | sp<MediaPlayerBase> p = getPlayer(); |
| 957 | if (p == 0) return UNKNOWN_ERROR; |
| 958 | *state = p->isPlaying(); |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 959 | ALOGV("[%d] isPlaying: %d", mConnId, *state); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | return NO_ERROR; |
| 961 | } |
| 962 | |
| 963 | status_t MediaPlayerService::Client::getCurrentPosition(int *msec) |
| 964 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 965 | ALOGV("getCurrentPosition"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | sp<MediaPlayerBase> p = getPlayer(); |
| 967 | if (p == 0) return UNKNOWN_ERROR; |
| 968 | status_t ret = p->getCurrentPosition(msec); |
| 969 | if (ret == NO_ERROR) { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 970 | ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | } else { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 972 | ALOGE("getCurrentPosition returned %d", ret); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | } |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | status_t MediaPlayerService::Client::getDuration(int *msec) |
| 978 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 979 | ALOGV("getDuration"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | sp<MediaPlayerBase> p = getPlayer(); |
| 981 | if (p == 0) return UNKNOWN_ERROR; |
| 982 | status_t ret = p->getDuration(msec); |
| 983 | if (ret == NO_ERROR) { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 984 | ALOGV("[%d] getDuration = %d", mConnId, *msec); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | } else { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 986 | ALOGE("getDuration returned %d", ret); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
| 988 | return ret; |
| 989 | } |
| 990 | |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 991 | status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) { |
| 992 | ALOGV("setNextPlayer"); |
| 993 | Mutex::Autolock l(mLock); |
| 994 | sp<Client> c = static_cast<Client*>(player.get()); |
| 995 | mNextClient = c; |
| John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 996 | |
| 997 | if (c != NULL) { |
| 998 | if (mAudioOutput != NULL) { |
| 999 | mAudioOutput->setNextOutput(c->mAudioOutput); |
| 1000 | } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) { |
| 1001 | ALOGE("no current audio output"); |
| 1002 | } |
| 1003 | |
| 1004 | if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) { |
| 1005 | mPlayer->setNextPlayer(mNextClient->getPlayer()); |
| 1006 | } |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1007 | } |
| John Grossman | 5f7e55e | 2012-08-24 14:47:25 -0700 | [diff] [blame] | 1008 | |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1009 | return OK; |
| 1010 | } |
| 1011 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | status_t MediaPlayerService::Client::seekTo(int msec) |
| 1013 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1014 | ALOGV("[%d] seekTo(%d)", mConnId, msec); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | sp<MediaPlayerBase> p = getPlayer(); |
| 1016 | if (p == 0) return UNKNOWN_ERROR; |
| 1017 | return p->seekTo(msec); |
| 1018 | } |
| 1019 | |
| 1020 | status_t MediaPlayerService::Client::reset() |
| 1021 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1022 | ALOGV("[%d] reset", mConnId); |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1023 | mRetransmitEndpointValid = false; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | sp<MediaPlayerBase> p = getPlayer(); |
| 1025 | if (p == 0) return UNKNOWN_ERROR; |
| 1026 | return p->reset(); |
| 1027 | } |
| 1028 | |
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1029 | 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] | 1030 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1031 | ALOGV("[%d] setAudioStreamType(%d)", mConnId, type); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | // TODO: for hardware output, call player instead |
| 1033 | Mutex::Autolock l(mLock); |
| 1034 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); |
| 1035 | return NO_ERROR; |
| 1036 | } |
| 1037 | |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1038 | status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel) |
| 1039 | { |
| 1040 | if (mAudioAttributes != NULL) { free(mAudioAttributes); } |
| 1041 | mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1042 | unmarshallAudioAttributes(parcel, mAudioAttributes); |
| 1043 | |
| 1044 | ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s", |
| 1045 | mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags, |
| 1046 | mAudioAttributes->tags); |
| 1047 | |
| 1048 | if (mAudioOutput != 0) { |
| 1049 | mAudioOutput->setAudioAttributes(mAudioAttributes); |
| 1050 | } |
| 1051 | return NO_ERROR; |
| 1052 | } |
| 1053 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | status_t MediaPlayerService::Client::setLooping(int loop) |
| 1055 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1056 | ALOGV("[%d] setLooping(%d)", mConnId, loop); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | mLoop = loop; |
| 1058 | sp<MediaPlayerBase> p = getPlayer(); |
| 1059 | if (p != 0) return p->setLooping(loop); |
| 1060 | return NO_ERROR; |
| 1061 | } |
| 1062 | |
| 1063 | status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume) |
| 1064 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1065 | ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); |
| John Grossman | 761defc | 2012-02-09 15:09:05 -0800 | [diff] [blame] | 1066 | |
| 1067 | // for hardware output, call player instead |
| 1068 | sp<MediaPlayerBase> p = getPlayer(); |
| 1069 | { |
| 1070 | Mutex::Autolock l(mLock); |
| 1071 | if (p != 0 && p->hardwareOutput()) { |
| 1072 | MediaPlayerHWInterface* hwp = |
| 1073 | reinterpret_cast<MediaPlayerHWInterface*>(p.get()); |
| 1074 | return hwp->setVolume(leftVolume, rightVolume); |
| 1075 | } else { |
| 1076 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); |
| 1077 | return NO_ERROR; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | return NO_ERROR; |
| 1082 | } |
| 1083 | |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1084 | status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level) |
| 1085 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1086 | ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level); |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1087 | Mutex::Autolock l(mLock); |
| 1088 | if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level); |
| 1089 | return NO_ERROR; |
| 1090 | } |
| 1091 | |
| 1092 | status_t MediaPlayerService::Client::attachAuxEffect(int effectId) |
| 1093 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1094 | ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId); |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1095 | Mutex::Autolock l(mLock); |
| 1096 | if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId); |
| 1097 | return NO_ERROR; |
| 1098 | } |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1099 | |
| Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1100 | status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1101 | ALOGV("[%d] setParameter(%d)", mConnId, key); |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1102 | switch (key) { |
| 1103 | case KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1104 | { |
| 1105 | Mutex::Autolock l(mLock); |
| 1106 | return setAudioAttributes_l(request); |
| 1107 | } |
| 1108 | default: |
| 1109 | sp<MediaPlayerBase> p = getPlayer(); |
| 1110 | if (p == 0) { return UNKNOWN_ERROR; } |
| 1111 | return p->setParameter(key, request); |
| 1112 | } |
| Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1116 | ALOGV("[%d] getParameter(%d)", mConnId, key); |
| Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 1117 | sp<MediaPlayerBase> p = getPlayer(); |
| 1118 | if (p == 0) return UNKNOWN_ERROR; |
| 1119 | return p->getParameter(key, reply); |
| 1120 | } |
| 1121 | |
| John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 1122 | status_t MediaPlayerService::Client::setRetransmitEndpoint( |
| 1123 | const struct sockaddr_in* endpoint) { |
| 1124 | |
| 1125 | if (NULL != endpoint) { |
| 1126 | uint32_t a = ntohl(endpoint->sin_addr.s_addr); |
| 1127 | uint16_t p = ntohs(endpoint->sin_port); |
| 1128 | ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId, |
| 1129 | (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p); |
| 1130 | } else { |
| 1131 | ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId); |
| 1132 | } |
| 1133 | |
| 1134 | sp<MediaPlayerBase> p = getPlayer(); |
| 1135 | |
| 1136 | // Right now, the only valid time to set a retransmit endpoint is before |
| 1137 | // player selection has been made (since the presence or absence of a |
| 1138 | // retransmit endpoint is going to determine which player is selected during |
| 1139 | // setDataSource). |
| 1140 | if (p != 0) return INVALID_OPERATION; |
| 1141 | |
| 1142 | if (NULL != endpoint) { |
| 1143 | mRetransmitEndpoint = *endpoint; |
| 1144 | mRetransmitEndpointValid = true; |
| 1145 | } else { |
| 1146 | mRetransmitEndpointValid = false; |
| 1147 | } |
| 1148 | |
| 1149 | return NO_ERROR; |
| 1150 | } |
| 1151 | |
| John Grossman | 44a7e42 | 2012-06-21 17:29:24 -0700 | [diff] [blame] | 1152 | status_t MediaPlayerService::Client::getRetransmitEndpoint( |
| 1153 | struct sockaddr_in* endpoint) |
| 1154 | { |
| 1155 | if (NULL == endpoint) |
| 1156 | return BAD_VALUE; |
| 1157 | |
| 1158 | sp<MediaPlayerBase> p = getPlayer(); |
| 1159 | |
| 1160 | if (p != NULL) |
| 1161 | return p->getRetransmitEndpoint(endpoint); |
| 1162 | |
| 1163 | if (!mRetransmitEndpointValid) |
| 1164 | return NO_INIT; |
| 1165 | |
| 1166 | *endpoint = mRetransmitEndpoint; |
| 1167 | |
| 1168 | return NO_ERROR; |
| 1169 | } |
| 1170 | |
| Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 1171 | void MediaPlayerService::Client::notify( |
| 1172 | 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] | 1173 | { |
| 1174 | Client* client = static_cast<Client*>(cookie); |
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1175 | if (client == NULL) { |
| 1176 | return; |
| 1177 | } |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1178 | |
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1179 | sp<IMediaPlayerClient> c; |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1180 | { |
| 1181 | Mutex::Autolock l(client->mLock); |
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1182 | c = client->mClient; |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1183 | if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) { |
| John Grossman | cb0b755 | 2012-08-23 17:47:31 -0700 | [diff] [blame] | 1184 | if (client->mAudioOutput != NULL) |
| 1185 | client->mAudioOutput->switchToNextOutput(); |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1186 | client->mNextClient->start(); |
| 1187 | client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1191 | if (MEDIA_INFO == msg && |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1192 | MEDIA_INFO_METADATA_UPDATE == ext1) { |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1193 | const media::Metadata::Type metadata_type = ext2; |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1194 | |
| 1195 | if(client->shouldDropMetadata(metadata_type)) { |
| 1196 | return; |
| 1197 | } |
| 1198 | |
| 1199 | // Update the list of metadata that have changed. getMetadata |
| 1200 | // also access mMetadataUpdated and clears it. |
| 1201 | client->addNewMetadataUpdate(metadata_type); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1202 | } |
| James Dong | b8a9825 | 2012-08-26 16:13:03 -0700 | [diff] [blame] | 1203 | |
| 1204 | if (c != NULL) { |
| 1205 | ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2); |
| 1206 | c->notify(msg, ext1, ext2, obj); |
| 1207 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1210 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1211 | bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1212 | { |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1213 | Mutex::Autolock lock(mLock); |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1214 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1215 | if (findMetadata(mMetadataDrop, code)) { |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1216 | return true; |
| 1217 | } |
| 1218 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1219 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1220 | return false; |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1221 | } else { |
| Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 1222 | return true; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1226 | |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 1227 | void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
| Nicolas Catania | 4829038 | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 1228 | Mutex::Autolock lock(mLock); |
| 1229 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1230 | mMetadataUpdated.add(metadata_type); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | #if CALLBACK_ANTAGONIZER |
| 1235 | const int Antagonizer::interval = 10000; // 10 msecs |
| 1236 | |
| 1237 | Antagonizer::Antagonizer(notify_callback_f cb, void* client) : |
| 1238 | mExit(false), mActive(false), mClient(client), mCb(cb) |
| 1239 | { |
| 1240 | createThread(callbackThread, this); |
| 1241 | } |
| 1242 | |
| 1243 | void Antagonizer::kill() |
| 1244 | { |
| 1245 | Mutex::Autolock _l(mLock); |
| 1246 | mActive = false; |
| 1247 | mExit = true; |
| 1248 | mCondition.wait(mLock); |
| 1249 | } |
| 1250 | |
| 1251 | int Antagonizer::callbackThread(void* user) |
| 1252 | { |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1253 | ALOGD("Antagonizer started"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); |
| 1255 | while (!p->mExit) { |
| 1256 | if (p->mActive) { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1257 | ALOGV("send event"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | p->mCb(p->mClient, 0, 0, 0); |
| 1259 | } |
| 1260 | usleep(interval); |
| 1261 | } |
| 1262 | Mutex::Autolock _l(p->mLock); |
| 1263 | p->mCondition.signal(); |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1264 | ALOGD("Antagonizer stopped"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
| 1267 | #endif |
| 1268 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | #undef LOG_TAG |
| 1270 | #define LOG_TAG "AudioSink" |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1271 | MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid, |
| 1272 | const audio_attributes_t* attr) |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1273 | : mCallback(NULL), |
| Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 1274 | mCallbackCookie(NULL), |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1275 | mCallbackData(NULL), |
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1276 | mBytesWritten(0), |
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1277 | mSessionId(sessionId), |
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1278 | mUid(uid), |
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1279 | mPid(pid), |
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1280 | mFlags(AUDIO_OUTPUT_FLAG_NONE) { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1281 | ALOGV("AudioOutput(%d)", sessionId); |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1282 | mStreamType = AUDIO_STREAM_MUSIC; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | mLeftVolume = 1.0; |
| 1284 | mRightVolume = 1.0; |
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1285 | mPlaybackRatePermille = 1000; |
| 1286 | mSampleRateHz = 0; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | mMsecsPerFrame = 0; |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1288 | mAuxEffectId = 0; |
| 1289 | mSendLevel = 0.0; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | setMinBufferCount(); |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1291 | mAttributes = attr; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | MediaPlayerService::AudioOutput::~AudioOutput() |
| 1295 | { |
| 1296 | close(); |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1297 | delete mCallbackData; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | void MediaPlayerService::AudioOutput::setMinBufferCount() |
| 1301 | { |
| 1302 | char value[PROPERTY_VALUE_MAX]; |
| 1303 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 1304 | mIsOnEmulator = true; |
| 1305 | mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | bool MediaPlayerService::AudioOutput::isOnEmulator() |
| 1310 | { |
| 1311 | setMinBufferCount(); |
| 1312 | return mIsOnEmulator; |
| 1313 | } |
| 1314 | |
| 1315 | int MediaPlayerService::AudioOutput::getMinBufferCount() |
| 1316 | { |
| 1317 | setMinBufferCount(); |
| 1318 | return mMinBufferCount; |
| 1319 | } |
| 1320 | |
| 1321 | ssize_t MediaPlayerService::AudioOutput::bufferSize() const |
| 1322 | { |
| 1323 | if (mTrack == 0) return NO_INIT; |
| 1324 | return mTrack->frameCount() * frameSize(); |
| 1325 | } |
| 1326 | |
| 1327 | ssize_t MediaPlayerService::AudioOutput::frameCount() const |
| 1328 | { |
| 1329 | if (mTrack == 0) return NO_INIT; |
| 1330 | return mTrack->frameCount(); |
| 1331 | } |
| 1332 | |
| 1333 | ssize_t MediaPlayerService::AudioOutput::channelCount() const |
| 1334 | { |
| 1335 | if (mTrack == 0) return NO_INIT; |
| 1336 | return mTrack->channelCount(); |
| 1337 | } |
| 1338 | |
| 1339 | ssize_t MediaPlayerService::AudioOutput::frameSize() const |
| 1340 | { |
| 1341 | if (mTrack == 0) return NO_INIT; |
| 1342 | return mTrack->frameSize(); |
| 1343 | } |
| 1344 | |
| 1345 | uint32_t MediaPlayerService::AudioOutput::latency () const |
| 1346 | { |
| Eric Laurent | db354e5 | 2012-03-05 17:27:11 -0800 | [diff] [blame] | 1347 | if (mTrack == 0) return 0; |
| 1348 | return mTrack->latency(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | float MediaPlayerService::AudioOutput::msecsPerFrame() const |
| 1352 | { |
| 1353 | return mMsecsPerFrame; |
| 1354 | } |
| 1355 | |
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1356 | status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const |
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1357 | { |
| 1358 | if (mTrack == 0) return NO_INIT; |
| 1359 | return mTrack->getPosition(position); |
| 1360 | } |
| 1361 | |
| Lajos Molnar | 06ad152 | 2014-08-28 07:27:44 -0700 | [diff] [blame] | 1362 | status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const |
| 1363 | { |
| 1364 | if (mTrack == 0) return NO_INIT; |
| 1365 | return mTrack->getTimestamp(ts); |
| 1366 | } |
| 1367 | |
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1368 | status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const |
| 1369 | { |
| 1370 | if (mTrack == 0) return NO_INIT; |
| 1371 | *frameswritten = mBytesWritten / frameSize(); |
| 1372 | return OK; |
| 1373 | } |
| 1374 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1375 | status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs) |
| 1376 | { |
| 1377 | if (mTrack == 0) return NO_INIT; |
| 1378 | return mTrack->setParameters(keyValuePairs); |
| 1379 | } |
| 1380 | |
| 1381 | String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys) |
| 1382 | { |
| 1383 | if (mTrack == 0) return String8::empty(); |
| 1384 | return mTrack->getParameters(keys); |
| 1385 | } |
| 1386 | |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1387 | void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) { |
| 1388 | mAttributes = attributes; |
| 1389 | } |
| 1390 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1391 | void MediaPlayerService::AudioOutput::deleteRecycledTrack() |
| 1392 | { |
| 1393 | ALOGV("deleteRecycledTrack"); |
| 1394 | |
| 1395 | if (mRecycledTrack != 0) { |
| 1396 | |
| 1397 | if (mCallbackData != NULL) { |
| 1398 | mCallbackData->setOutput(NULL); |
| 1399 | mCallbackData->endTrackSwitch(); |
| 1400 | } |
| 1401 | |
| 1402 | if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) { |
| 1403 | mRecycledTrack->flush(); |
| 1404 | } |
| 1405 | // An offloaded track isn't flushed because the STREAM_END is reported |
| 1406 | // slightly prematurely to allow time for the gapless track switch |
| 1407 | // but this means that if we decide not to recycle the track there |
| 1408 | // could be a small amount of residual data still playing. We leave |
| 1409 | // AudioFlinger to drain the track. |
| 1410 | |
| 1411 | mRecycledTrack.clear(); |
| 1412 | delete mCallbackData; |
| 1413 | mCallbackData = NULL; |
| 1414 | close(); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1418 | status_t MediaPlayerService::AudioOutput::open( |
| Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1419 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, |
| 1420 | audio_format_t format, int bufferCount, |
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1421 | AudioCallback cb, void *cookie, |
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1422 | audio_output_flags_t flags, |
| 1423 | const audio_offload_info_t *offloadInfo) |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | { |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1425 | mCallback = cb; |
| 1426 | mCallbackCookie = cookie; |
| 1427 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | // Check argument "bufferCount" against the mininum buffer count |
| 1429 | if (bufferCount < mMinBufferCount) { |
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1430 | ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | bufferCount = mMinBufferCount; |
| 1432 | |
| 1433 | } |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1434 | ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask, |
| 1435 | format, bufferCount, mSessionId, flags); |
| Glenn Kasten | 1127d65 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 1436 | uint32_t afSampleRate; |
| Glenn Kasten | 7da35f2 | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 1437 | size_t afFrameCount; |
| Glenn Kasten | bce50bf | 2014-02-27 15:29:51 -0800 | [diff] [blame] | 1438 | size_t frameCount; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1440 | // offloading is only supported in callback mode for now. |
| 1441 | // offloadInfo must be present if offload flag is set |
| 1442 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) && |
| 1443 | ((cb == NULL) || (offloadInfo == NULL))) { |
| 1444 | return BAD_VALUE; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1447 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1448 | frameCount = 0; // AudioTrack will get frame count from AudioFlinger |
| 1449 | } else { |
| 1450 | uint32_t afSampleRate; |
| 1451 | size_t afFrameCount; |
| 1452 | |
| 1453 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| 1454 | return NO_INIT; |
| 1455 | } |
| 1456 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| 1457 | return NO_INIT; |
| 1458 | } |
| 1459 | |
| 1460 | frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate; |
| 1461 | } |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1462 | |
| Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1463 | if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { |
| Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1464 | channelMask = audio_channel_out_mask_from_count(channelCount); |
| Jean-Michel Trivi | 786618f | 2012-03-02 14:54:07 -0800 | [diff] [blame] | 1465 | if (0 == channelMask) { |
| 1466 | ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount); |
| 1467 | return NO_INIT; |
| 1468 | } |
| 1469 | } |
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1470 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1471 | // Check whether we can recycle the track |
| 1472 | bool reuse = false; |
| 1473 | bool bothOffloaded = false; |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1474 | |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1475 | if (mRecycledTrack != 0) { |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1476 | // check whether we are switching between two offloaded tracks |
| 1477 | bothOffloaded = (flags & mRecycledTrack->getFlags() |
| 1478 | & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1479 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1480 | // check if the existing track can be reused as-is, or if a new track needs to be created. |
| 1481 | reuse = true; |
| 1482 | |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1483 | if ((mCallbackData == NULL && mCallback != NULL) || |
| 1484 | (mCallbackData != NULL && mCallback == NULL)) { |
| 1485 | // recycled track uses callbacks but the caller wants to use writes, or vice versa |
| 1486 | ALOGV("can't chain callback and write"); |
| 1487 | reuse = false; |
| 1488 | } else if ((mRecycledTrack->getSampleRate() != sampleRate) || |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1489 | (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) { |
| 1490 | ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch", |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1491 | mRecycledTrack->getSampleRate(), sampleRate, |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1492 | mRecycledTrack->channelCount(), channelCount); |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1493 | reuse = false; |
| 1494 | } else if (flags != mFlags) { |
| 1495 | ALOGV("output flags differ %08x/%08x", flags, mFlags); |
| 1496 | reuse = false; |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1497 | } else if (mRecycledTrack->format() != format) { |
| 1498 | reuse = false; |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1499 | } |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1500 | } else { |
| 1501 | ALOGV("no track available to recycle"); |
| 1502 | } |
| 1503 | |
| 1504 | ALOGV_IF(bothOffloaded, "both tracks offloaded"); |
| 1505 | |
| 1506 | // If we can't recycle and both tracks are offloaded |
| 1507 | // we must close the previous output before opening a new one |
| 1508 | if (bothOffloaded && !reuse) { |
| 1509 | ALOGV("both offloaded and not recycling"); |
| 1510 | deleteRecycledTrack(); |
| 1511 | } |
| 1512 | |
| 1513 | sp<AudioTrack> t; |
| 1514 | CallbackData *newcbd = NULL; |
| 1515 | |
| 1516 | // We don't attempt to create a new track if we are recycling an |
| 1517 | // offloaded track. But, if we are recycling a non-offloaded or we |
| 1518 | // are switching where one is offloaded and one isn't then we create |
| 1519 | // the new track in advance so that we can read additional stream info |
| 1520 | |
| 1521 | if (!(reuse && bothOffloaded)) { |
| 1522 | ALOGV("creating new AudioTrack"); |
| 1523 | |
| 1524 | if (mCallback != NULL) { |
| 1525 | newcbd = new CallbackData(this); |
| 1526 | t = new AudioTrack( |
| 1527 | mStreamType, |
| 1528 | sampleRate, |
| 1529 | format, |
| 1530 | channelMask, |
| 1531 | frameCount, |
| 1532 | flags, |
| 1533 | CallbackWrapper, |
| 1534 | newcbd, |
| 1535 | 0, // notification frames |
| 1536 | mSessionId, |
| 1537 | AudioTrack::TRANSFER_CALLBACK, |
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1538 | offloadInfo, |
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1539 | mUid, |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1540 | mPid, |
| 1541 | mAttributes); |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1542 | } else { |
| 1543 | t = new AudioTrack( |
| 1544 | mStreamType, |
| 1545 | sampleRate, |
| 1546 | format, |
| 1547 | channelMask, |
| 1548 | frameCount, |
| 1549 | flags, |
| Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1550 | NULL, // callback |
| 1551 | NULL, // user data |
| 1552 | 0, // notification frames |
| 1553 | mSessionId, |
| 1554 | AudioTrack::TRANSFER_DEFAULT, |
| 1555 | NULL, // offload info |
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 1556 | mUid, |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1557 | mPid, |
| 1558 | mAttributes); |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
| 1562 | ALOGE("Unable to create audio track"); |
| 1563 | delete newcbd; |
| 1564 | return NO_INIT; |
| Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 1565 | } else { |
| 1566 | // successful AudioTrack initialization implies a legacy stream type was generated |
| 1567 | // from the audio attributes |
| 1568 | mStreamType = t->streamType(); |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | if (reuse) { |
| 1573 | CHECK(mRecycledTrack != NULL); |
| 1574 | |
| 1575 | if (!bothOffloaded) { |
| 1576 | if (mRecycledTrack->frameCount() != t->frameCount()) { |
| 1577 | ALOGV("framecount differs: %u/%u frames", |
| 1578 | mRecycledTrack->frameCount(), t->frameCount()); |
| 1579 | reuse = false; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1583 | if (reuse) { |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1584 | ALOGV("chaining to next output and recycling track"); |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1585 | close(); |
| 1586 | mTrack = mRecycledTrack; |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1587 | mRecycledTrack.clear(); |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1588 | if (mCallbackData != NULL) { |
| 1589 | mCallbackData->setOutput(this); |
| 1590 | } |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1591 | delete newcbd; |
| 1592 | return OK; |
| 1593 | } |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1596 | // we're not going to reuse the track, unblock and flush it |
| 1597 | // this was done earlier if both tracks are offloaded |
| 1598 | if (!bothOffloaded) { |
| 1599 | deleteRecycledTrack(); |
| 1600 | } |
| 1601 | |
| 1602 | CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL))); |
| 1603 | |
| Marco Nelissen | 67295b5 | 2012-06-11 14:52:53 -0700 | [diff] [blame] | 1604 | mCallbackData = newcbd; |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1605 | ALOGV("setVolume"); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | t->setVolume(mLeftVolume, mRightVolume); |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1607 | |
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1608 | mSampleRateHz = sampleRate; |
| Eric Laurent | 1948eb3 | 2012-04-13 16:50:19 -0700 | [diff] [blame] | 1609 | mFlags = flags; |
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1610 | mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate; |
| Marco Nelissen | 9944860 | 2012-04-02 12:16:49 -0700 | [diff] [blame] | 1611 | uint32_t pos; |
| 1612 | if (t->getPosition(&pos) == OK) { |
| 1613 | mBytesWritten = uint64_t(pos) * t->frameSize(); |
| 1614 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | mTrack = t; |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1616 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1617 | status_t res = NO_ERROR; |
| 1618 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) { |
| 1619 | res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000); |
| 1620 | if (res == NO_ERROR) { |
| 1621 | t->setAuxEffectSendLevel(mSendLevel); |
| 1622 | res = t->attachAuxEffect(mAuxEffectId); |
| 1623 | } |
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1624 | } |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1625 | ALOGV("open() DONE status %d", res); |
| 1626 | return res; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | } |
| 1628 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1629 | status_t MediaPlayerService::AudioOutput::start() |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1631 | ALOGV("start"); |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1632 | if (mCallbackData != NULL) { |
| 1633 | mCallbackData->endTrackSwitch(); |
| 1634 | } |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1635 | if (mTrack != 0) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | mTrack->setVolume(mLeftVolume, mRightVolume); |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1637 | mTrack->setAuxEffectSendLevel(mSendLevel); |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1638 | return mTrack->start(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | } |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1640 | return NO_INIT; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1643 | void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) { |
| 1644 | mNextOutput = nextOutput; |
| 1645 | } |
| Marco Nelissen | 7ee8ac9 | 2010-01-12 09:23:54 -0800 | [diff] [blame] | 1646 | |
| 1647 | |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1648 | void MediaPlayerService::AudioOutput::switchToNextOutput() { |
| 1649 | ALOGV("switchToNextOutput"); |
| 1650 | if (mNextOutput != NULL) { |
| 1651 | if (mCallbackData != NULL) { |
| 1652 | mCallbackData->beginTrackSwitch(); |
| 1653 | } |
| 1654 | delete mNextOutput->mCallbackData; |
| 1655 | mNextOutput->mCallbackData = mCallbackData; |
| 1656 | mCallbackData = NULL; |
| 1657 | mNextOutput->mRecycledTrack = mTrack; |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1658 | mTrack.clear(); |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1659 | mNextOutput->mSampleRateHz = mSampleRateHz; |
| 1660 | mNextOutput->mMsecsPerFrame = mMsecsPerFrame; |
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1661 | mNextOutput->mBytesWritten = mBytesWritten; |
| Marco Nelissen | d791e09 | 2012-06-11 17:00:59 -0700 | [diff] [blame] | 1662 | mNextOutput->mFlags = mFlags; |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size) |
| 1667 | { |
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1668 | 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] | 1669 | |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1670 | //ALOGV("write(%p, %u)", buffer, size); |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1671 | if (mTrack != 0) { |
| Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1672 | ssize_t ret = mTrack->write(buffer, size); |
| Andy Hung | a31335a | 2014-08-20 17:37:59 -0700 | [diff] [blame] | 1673 | if (ret >= 0) { |
| 1674 | mBytesWritten += ret; |
| 1675 | } |
| Marco Nelissen | 10dbb8e | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 1676 | return ret; |
| 1677 | } |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | return NO_INIT; |
| 1679 | } |
| 1680 | |
| 1681 | void MediaPlayerService::AudioOutput::stop() |
| 1682 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1683 | ALOGV("stop"); |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1684 | if (mTrack != 0) mTrack->stop(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | void MediaPlayerService::AudioOutput::flush() |
| 1688 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1689 | ALOGV("flush"); |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1690 | if (mTrack != 0) mTrack->flush(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | void MediaPlayerService::AudioOutput::pause() |
| 1694 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1695 | ALOGV("pause"); |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1696 | if (mTrack != 0) mTrack->pause(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | void MediaPlayerService::AudioOutput::close() |
| 1700 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1701 | ALOGV("close"); |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1702 | mTrack.clear(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | void MediaPlayerService::AudioOutput::setVolume(float left, float right) |
| 1706 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1707 | ALOGV("setVolume(%f, %f)", left, right); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | mLeftVolume = left; |
| 1709 | mRightVolume = right; |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1710 | if (mTrack != 0) { |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | mTrack->setVolume(left, right); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1715 | status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille) |
| 1716 | { |
| 1717 | ALOGV("setPlaybackRatePermille(%d)", ratePermille); |
| 1718 | status_t res = NO_ERROR; |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1719 | if (mTrack != 0) { |
| Jean-Michel Trivi | 7a8b0ed | 2012-02-02 09:06:31 -0800 | [diff] [blame] | 1720 | res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000); |
| 1721 | } else { |
| 1722 | res = NO_INIT; |
| 1723 | } |
| 1724 | mPlaybackRatePermille = ratePermille; |
| 1725 | if (mSampleRateHz != 0) { |
| 1726 | mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz; |
| 1727 | } |
| 1728 | return res; |
| 1729 | } |
| 1730 | |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1731 | status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level) |
| 1732 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1733 | ALOGV("setAuxEffectSendLevel(%f)", level); |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1734 | mSendLevel = level; |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1735 | if (mTrack != 0) { |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1736 | return mTrack->setAuxEffectSendLevel(level); |
| 1737 | } |
| 1738 | return NO_ERROR; |
| 1739 | } |
| 1740 | |
| 1741 | status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId) |
| 1742 | { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1743 | ALOGV("attachAuxEffect(%d)", effectId); |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1744 | mAuxEffectId = effectId; |
| Glenn Kasten | 2799d74 | 2013-05-30 14:33:29 -0700 | [diff] [blame] | 1745 | if (mTrack != 0) { |
| Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1746 | return mTrack->attachAuxEffect(effectId); |
| 1747 | } |
| 1748 | return NO_ERROR; |
| 1749 | } |
| 1750 | |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1751 | // static |
| 1752 | void MediaPlayerService::AudioOutput::CallbackWrapper( |
| Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 1753 | int event, void *cookie, void *info) { |
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1754 | //ALOGV("callbackwrapper"); |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1755 | CallbackData *data = (CallbackData*)cookie; |
| 1756 | data->lock(); |
| 1757 | AudioOutput *me = data->getOutput(); |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1758 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1759 | if (me == NULL) { |
| 1760 | // no output set, likely because the track was scheduled to be reused |
| 1761 | // by another player, but the format turned out to be incompatible. |
| 1762 | data->unlock(); |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1763 | if (buffer != NULL) { |
| 1764 | buffer->size = 0; |
| 1765 | } |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1766 | return; |
| 1767 | } |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1768 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1769 | switch(event) { |
| 1770 | case AudioTrack::EVENT_MORE_DATA: { |
| 1771 | size_t actualSize = (*me->mCallback)( |
| 1772 | me, buffer->raw, buffer->size, me->mCallbackCookie, |
| 1773 | CB_EVENT_FILL_BUFFER); |
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1774 | |
| aarti jadhav-gaikwad | f257557 | 2014-08-13 15:04:39 +0530 | [diff] [blame] | 1775 | if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 && |
| 1776 | actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) { |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1777 | // We've reached EOS but the audio track is not stopped yet, |
| 1778 | // keep playing silence. |
| Andreas Huber | 2e8ffaf | 2010-02-18 16:45:13 -0800 | [diff] [blame] | 1779 | |
| Richard Fitzgerald | d89532e | 2013-05-14 13:18:21 +0100 | [diff] [blame] | 1780 | memset(buffer->raw, 0, buffer->size); |
| 1781 | actualSize = buffer->size; |
| 1782 | } |
| 1783 | |
| 1784 | buffer->size = actualSize; |
| 1785 | } break; |
| 1786 | |
| 1787 | |
| 1788 | case AudioTrack::EVENT_STREAM_END: |
| 1789 | ALOGV("callbackwrapper: deliver EVENT_STREAM_END"); |
| 1790 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 1791 | me->mCallbackCookie, CB_EVENT_STREAM_END); |
| 1792 | break; |
| 1793 | |
| 1794 | case AudioTrack::EVENT_NEW_IAUDIOTRACK : |
| 1795 | ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN"); |
| 1796 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 1797 | me->mCallbackCookie, CB_EVENT_TEAR_DOWN); |
| 1798 | break; |
| 1799 | |
| 1800 | default: |
| 1801 | ALOGE("received unknown event type: %d inside CallbackWrapper !", event); |
| Andreas Huber | 51c1e0e | 2011-04-04 11:43:40 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
| Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 1804 | data->unlock(); |
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| Marco Nelissen | 4110c10 | 2012-03-29 09:31:28 -0700 | [diff] [blame] | 1807 | int MediaPlayerService::AudioOutput::getSessionId() const |
| Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 1808 | { |
| 1809 | return mSessionId; |
| 1810 | } |
| 1811 | |
| Eric Laurent | 6f59db1 | 2013-07-26 17:16:50 -0700 | [diff] [blame] | 1812 | uint32_t MediaPlayerService::AudioOutput::getSampleRate() const |
| 1813 | { |
| 1814 | if (mTrack == 0) return 0; |
| 1815 | return mTrack->getSampleRate(); |
| 1816 | } |
| 1817 | |
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1818 | //////////////////////////////////////////////////////////////////////////////// |
| 1819 | |
| 1820 | struct CallbackThread : public Thread { |
| 1821 | CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink, |
| 1822 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 1823 | void *cookie); |
| 1824 | |
| 1825 | protected: |
| 1826 | virtual ~CallbackThread(); |
| 1827 | |
| 1828 | virtual bool threadLoop(); |
| 1829 | |
| 1830 | private: |
| 1831 | wp<MediaPlayerBase::AudioSink> mSink; |
| 1832 | MediaPlayerBase::AudioSink::AudioCallback mCallback; |
| 1833 | void *mCookie; |
| 1834 | void *mBuffer; |
| 1835 | size_t mBufferSize; |
| 1836 | |
| 1837 | CallbackThread(const CallbackThread &); |
| 1838 | CallbackThread &operator=(const CallbackThread &); |
| 1839 | }; |
| 1840 | |
| 1841 | CallbackThread::CallbackThread( |
| 1842 | const wp<MediaPlayerBase::AudioSink> &sink, |
| 1843 | MediaPlayerBase::AudioSink::AudioCallback cb, |
| 1844 | void *cookie) |
| 1845 | : mSink(sink), |
| 1846 | mCallback(cb), |
| 1847 | mCookie(cookie), |
| 1848 | mBuffer(NULL), |
| 1849 | mBufferSize(0) { |
| 1850 | } |
| 1851 | |
| 1852 | CallbackThread::~CallbackThread() { |
| 1853 | if (mBuffer) { |
| 1854 | free(mBuffer); |
| 1855 | mBuffer = NULL; |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | bool CallbackThread::threadLoop() { |
| 1860 | sp<MediaPlayerBase::AudioSink> sink = mSink.promote(); |
| 1861 | if (sink == NULL) { |
| 1862 | return false; |
| 1863 | } |
| 1864 | |
| 1865 | if (mBuffer == NULL) { |
| 1866 | mBufferSize = sink->bufferSize(); |
| 1867 | mBuffer = malloc(mBufferSize); |
| 1868 | } |
| 1869 | |
| 1870 | size_t actualSize = |
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1871 | (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie, |
| 1872 | MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER); |
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1873 | |
| 1874 | if (actualSize > 0) { |
| 1875 | sink->write(mBuffer, actualSize); |
| Andy Hung | a31335a | 2014-08-20 17:37:59 -0700 | [diff] [blame] | 1876 | // Could return false on sink->write() error or short count. |
| 1877 | // Not necessarily appropriate but would work for AudioCache behavior. |
| Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | return true; |
| 1881 | } |
| 1882 | |
| 1883 | //////////////////////////////////////////////////////////////////////////////// |
| 1884 | |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1885 | void MediaPlayerService::addBatteryData(uint32_t params) |
| 1886 | { |
| 1887 | Mutex::Autolock lock(mLock); |
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1888 | |
| 1889 | int32_t time = systemTime() / 1000000L; |
| 1890 | |
| 1891 | // change audio output devices. This notification comes from AudioFlinger |
| 1892 | if ((params & kBatteryDataSpeakerOn) |
| 1893 | || (params & kBatteryDataOtherAudioDeviceOn)) { |
| 1894 | |
| 1895 | int deviceOn[NUM_AUDIO_DEVICES]; |
| 1896 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1897 | deviceOn[i] = 0; |
| 1898 | } |
| 1899 | |
| 1900 | if ((params & kBatteryDataSpeakerOn) |
| 1901 | && (params & kBatteryDataOtherAudioDeviceOn)) { |
| 1902 | deviceOn[SPEAKER_AND_OTHER] = 1; |
| 1903 | } else if (params & kBatteryDataSpeakerOn) { |
| 1904 | deviceOn[SPEAKER] = 1; |
| 1905 | } else { |
| 1906 | deviceOn[OTHER_AUDIO_DEVICE] = 1; |
| 1907 | } |
| 1908 | |
| 1909 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1910 | if (mBatteryAudio.deviceOn[i] != deviceOn[i]){ |
| 1911 | |
| 1912 | if (mBatteryAudio.refCount > 0) { // if playing audio |
| 1913 | if (!deviceOn[i]) { |
| 1914 | mBatteryAudio.lastTime[i] += time; |
| 1915 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 1916 | mBatteryAudio.lastTime[i] = 0; |
| 1917 | } else { |
| 1918 | mBatteryAudio.lastTime[i] = 0 - time; |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | mBatteryAudio.deviceOn[i] = deviceOn[i]; |
| 1923 | } |
| 1924 | } |
| 1925 | return; |
| 1926 | } |
| 1927 | |
| Marco Nelissen | b7848f1 | 2014-12-04 08:57:56 -0800 | [diff] [blame] | 1928 | // an audio stream is started |
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1929 | if (params & kBatteryDataAudioFlingerStart) { |
| 1930 | // record the start time only if currently no other audio |
| 1931 | // is being played |
| 1932 | if (mBatteryAudio.refCount == 0) { |
| 1933 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1934 | if (mBatteryAudio.deviceOn[i]) { |
| 1935 | mBatteryAudio.lastTime[i] -= time; |
| 1936 | } |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | mBatteryAudio.refCount ++; |
| 1941 | return; |
| 1942 | |
| 1943 | } else if (params & kBatteryDataAudioFlingerStop) { |
| 1944 | if (mBatteryAudio.refCount <= 0) { |
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1945 | ALOGW("Battery track warning: refCount is <= 0"); |
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | // record the stop time only if currently this is the only |
| 1950 | // audio being played |
| 1951 | if (mBatteryAudio.refCount == 1) { |
| 1952 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 1953 | if (mBatteryAudio.deviceOn[i]) { |
| 1954 | mBatteryAudio.lastTime[i] += time; |
| 1955 | mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i]; |
| 1956 | mBatteryAudio.lastTime[i] = 0; |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | mBatteryAudio.refCount --; |
| 1962 | return; |
| 1963 | } |
| 1964 | |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1965 | int uid = IPCThreadState::self()->getCallingUid(); |
| 1966 | if (uid == AID_MEDIA) { |
| 1967 | return; |
| 1968 | } |
| 1969 | int index = mBatteryData.indexOfKey(uid); |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1970 | |
| 1971 | if (index < 0) { // create a new entry for this UID |
| 1972 | BatteryUsageInfo info; |
| 1973 | info.audioTotalTime = 0; |
| 1974 | info.videoTotalTime = 0; |
| 1975 | info.audioLastTime = 0; |
| 1976 | info.videoLastTime = 0; |
| 1977 | info.refCount = 0; |
| 1978 | |
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1979 | if (mBatteryData.add(uid, info) == NO_MEMORY) { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1980 | ALOGE("Battery track error: no memory for new app"); |
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 1981 | return; |
| 1982 | } |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | BatteryUsageInfo &info = mBatteryData.editValueFor(uid); |
| 1986 | |
| 1987 | if (params & kBatteryDataCodecStarted) { |
| 1988 | if (params & kBatteryDataTrackAudio) { |
| 1989 | info.audioLastTime -= time; |
| 1990 | info.refCount ++; |
| 1991 | } |
| 1992 | if (params & kBatteryDataTrackVideo) { |
| 1993 | info.videoLastTime -= time; |
| 1994 | info.refCount ++; |
| 1995 | } |
| 1996 | } else { |
| 1997 | if (info.refCount == 0) { |
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1998 | ALOGW("Battery track warning: refCount is already 0"); |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 1999 | return; |
| 2000 | } else if (info.refCount < 0) { |
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2001 | ALOGE("Battery track error: refCount < 0"); |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2002 | mBatteryData.removeItem(uid); |
| 2003 | return; |
| 2004 | } |
| 2005 | |
| 2006 | if (params & kBatteryDataTrackAudio) { |
| 2007 | info.audioLastTime += time; |
| 2008 | info.refCount --; |
| 2009 | } |
| 2010 | if (params & kBatteryDataTrackVideo) { |
| 2011 | info.videoLastTime += time; |
| 2012 | info.refCount --; |
| 2013 | } |
| 2014 | |
| 2015 | // no stream is being played by this UID |
| 2016 | if (info.refCount == 0) { |
| 2017 | info.audioTotalTime += info.audioLastTime; |
| 2018 | info.audioLastTime = 0; |
| 2019 | info.videoTotalTime += info.videoLastTime; |
| 2020 | info.videoLastTime = 0; |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | status_t MediaPlayerService::pullBatteryData(Parcel* reply) { |
| 2026 | Mutex::Autolock lock(mLock); |
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2027 | |
| 2028 | // audio output devices usage |
| 2029 | int32_t time = systemTime() / 1000000L; //in ms |
| 2030 | int32_t totalTime; |
| 2031 | |
| 2032 | for (int i = 0; i < NUM_AUDIO_DEVICES; i++) { |
| 2033 | totalTime = mBatteryAudio.totalTime[i]; |
| 2034 | |
| 2035 | if (mBatteryAudio.deviceOn[i] |
| 2036 | && (mBatteryAudio.lastTime[i] != 0)) { |
| 2037 | int32_t tmpTime = mBatteryAudio.lastTime[i] + time; |
| 2038 | totalTime += tmpTime; |
| 2039 | } |
| 2040 | |
| 2041 | reply->writeInt32(totalTime); |
| 2042 | // reset the total time |
| 2043 | mBatteryAudio.totalTime[i] = 0; |
| 2044 | } |
| 2045 | |
| 2046 | // codec usage |
| Gloria Wang | 7cf180c | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 2047 | BatteryUsageInfo info; |
| 2048 | int size = mBatteryData.size(); |
| 2049 | |
| 2050 | reply->writeInt32(size); |
| 2051 | int i = 0; |
| 2052 | |
| 2053 | while (i < size) { |
| 2054 | info = mBatteryData.valueAt(i); |
| 2055 | |
| 2056 | reply->writeInt32(mBatteryData.keyAt(i)); //UID |
| 2057 | reply->writeInt32(info.audioTotalTime); |
| 2058 | reply->writeInt32(info.videoTotalTime); |
| 2059 | |
| 2060 | info.audioTotalTime = 0; |
| 2061 | info.videoTotalTime = 0; |
| 2062 | |
| 2063 | // remove the UID entry where no stream is being played |
| 2064 | if (info.refCount <= 0) { |
| 2065 | mBatteryData.removeItemsAt(i); |
| 2066 | size --; |
| 2067 | i --; |
| 2068 | } |
| 2069 | i++; |
| 2070 | } |
| 2071 | return NO_ERROR; |
| 2072 | } |
| niko | a64c8c7 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 2073 | } // namespace android |