blob: b5d1cdb925899af5de222a27933a1c438f8981da [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
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 Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryHeapBase.h>
40#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080041#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070045#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070046#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047
Eric Laurent43562692015-07-15 16:49:07 -070048#include <media/AudioPolicyHelper.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080049#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070050#include <media/IRemoteDisplay.h>
51#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052#include <media/MediaPlayerInterface.h>
53#include <media/mediarecorder.h>
54#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070055#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070057#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070058#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070059#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010060#include <media/stagefright/AudioPlayer.h>
61#include <media/stagefright/foundation/ADebug.h>
Marco Nelissenf09611f2015-02-13 14:12:42 -080062#include <media/stagefright/foundation/ALooperRoster.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070063#include <mediautils/BatteryNotifier.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064
Dima Zavin64760242011-05-11 14:15:23 -070065#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070066
Gloria Wang7cf180c2011-02-19 18:37:57 -080067#include <private/android_filesystem_config.h>
68
James Dong559bf282012-03-28 10:29:14 -070069#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080070#include "MediaRecorderClient.h"
71#include "MediaPlayerService.h"
72#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070073#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080074
Nicolas Catania14d27472009-07-13 14:37:49 -070075#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070076#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080077#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070078
Andreas Huber20111aa2009-07-14 16:56:47 -070079#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070080
Andreas Hubered3e3e02012-03-26 11:13:27 -070081#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080082#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070083#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070084#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070085#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070086
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070087namespace {
nikoa64c8c72009-07-20 15:07:26 -070088using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070089using android::status_t;
90using android::OK;
91using android::BAD_VALUE;
92using android::NOT_ENOUGH_DATA;
93using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070094
95// Max number of entries in the filter.
96const int kMaxFilterSize = 64; // I pulled that out of thin air.
97
nikoa64c8c72009-07-20 15:07:26 -070098// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070099
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700100
101// Unmarshall a filter from a Parcel.
102// Filter format in a parcel:
103//
104// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106// | number of entries (n) |
107// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108// | metadata type 1 |
109// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110// | metadata type 2 |
111// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112// ....
113// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114// | metadata type n |
115// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116//
117// @param p Parcel that should start with a filter.
118// @param[out] filter On exit contains the list of metadata type to be
119// filtered.
120// @param[out] status On exit contains the status code to be returned.
121// @return true if the parcel starts with a valid filter.
122bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700123 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700124 status_t *status)
125{
Nicolas Catania48290382009-07-10 13:53:06 -0700126 int32_t val;
127 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 {
Steve Block29357bc2012-01-06 19:20:56 +0000129 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700130 *status = NOT_ENOUGH_DATA;
131 return false;
132 }
133
Nicolas Catania48290382009-07-10 13:53:06 -0700134 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135 {
Steve Block29357bc2012-01-06 19:20:56 +0000136 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137 *status = BAD_VALUE;
138 return false;
139 }
140
Nicolas Catania48290382009-07-10 13:53:06 -0700141 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700142
143 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700144 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145
nikoa64c8c72009-07-20 15:07:26 -0700146 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700147
Nicolas Catania48290382009-07-10 13:53:06 -0700148
149 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 {
Steve Block29357bc2012-01-06 19:20:56 +0000151 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700152 *status = NOT_ENOUGH_DATA;
153 return false;
154 }
155
nikoa64c8c72009-07-20 15:07:26 -0700156 const Metadata::Type *data =
157 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158
Nicolas Catania48290382009-07-10 13:53:06 -0700159 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 {
Steve Block29357bc2012-01-06 19:20:56 +0000161 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700162 *status = BAD_VALUE;
163 return false;
164 }
165
166 // TODO: The stl impl of vector would be more efficient here
167 // because it degenerates into a memcpy on pod types. Try to
168 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700169 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170 {
Nicolas Catania48290382009-07-10 13:53:06 -0700171 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172 ++data;
173 }
174 *status = OK;
175 return true;
176}
177
Nicolas Catania48290382009-07-10 13:53:06 -0700178// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700179// @param val To be searched.
180// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700181bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700182{
183 // Deal with empty and ANY right away
184 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700185 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700186
Nicolas Catania48290382009-07-10 13:53:06 -0700187 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700188}
189
190} // anonymous namespace
191
192
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700193namespace {
194using android::Parcel;
195using android::String16;
196
197// marshalling tag indicating flattened utf16 tags
198// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
199const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
200
201// Audio attributes format in a parcel:
202//
203// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
204// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205// | usage |
206// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207// | content_type |
208// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900209// | source |
210// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700211// | flags |
212// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
214// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215// | flattened tags in UTF16 |
216// | ... |
217// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218//
219// @param p Parcel that contains audio attributes.
220// @param[out] attributes On exit points to an initialized audio_attributes_t structure
221// @param[out] status On exit contains the status code to be returned.
222void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
223{
224 attributes->usage = (audio_usage_t) parcel.readInt32();
225 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900226 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700227 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
228 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
229 if (hasFlattenedTag) {
230 // the tags are UTF16, convert to UTF8
231 String16 tags = parcel.readString16();
232 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
233 if (realTagSize <= 0) {
234 strcpy(attributes->tags, "");
235 } else {
236 // copy the flattened string into the attributes as the destination for the conversion:
237 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
238 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
239 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
Sergio Giro0c9742d2016-06-28 18:24:52 +0100240 utf16_to_utf8(tags.string(), tagSize, attributes->tags,
241 sizeof(attributes->tags) / sizeof(attributes->tags[0]));
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700242 }
243 } else {
244 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
245 strcpy(attributes->tags, "");
246 }
247}
248} // anonymous namespace
249
250
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251namespace android {
252
Marco Nelissenf09611f2015-02-13 14:12:42 -0800253extern ALooperRoster gLooperRoster;
254
255
Dave Burked681bbb2011-08-30 14:39:17 +0100256static bool checkPermission(const char* permissionString) {
257#ifndef HAVE_ANDROID_OS
258 return true;
259#endif
260 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
261 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000262 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100263 return ok;
264}
265
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
268/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
269
270void MediaPlayerService::instantiate() {
271 defaultServiceManager()->addService(
272 String16("media.player"), new MediaPlayerService());
273}
274
275MediaPlayerService::MediaPlayerService()
276{
Steve Block3856b092011-10-20 11:56:00 +0100277 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800278 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800279
280 mBatteryAudio.refCount = 0;
281 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
282 mBatteryAudio.deviceOn[i] = 0;
283 mBatteryAudio.lastTime[i] = 0;
284 mBatteryAudio.totalTime[i] = 0;
285 }
286 // speaker is on by default
287 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700288
Chong Zhang0b30fd42014-07-23 14:46:05 -0700289 // reset battery stats
290 // if the mediaserver has crashed, battery stats could be left
291 // in bad state, reset the state upon service start.
Ruben Brunk99e69712015-05-26 17:25:07 -0700292 BatteryNotifier& notifier(BatteryNotifier::getInstance());
293 notifier.noteResetVideo();
294 notifier.noteResetAudio();
Chong Zhang0b30fd42014-07-23 14:46:05 -0700295
John Grossman44a7e422012-06-21 17:29:24 -0700296 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800297}
298
299MediaPlayerService::~MediaPlayerService()
300{
Steve Block3856b092011-10-20 11:56:00 +0100301 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302}
303
Svet Ganovbe71aa22015-04-28 12:06:02 -0700304sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800306 pid_t pid = IPCThreadState::self()->getCallingPid();
Svet Ganovbe71aa22015-04-28 12:06:02 -0700307 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName);
Gloria Wangdac6a312009-10-29 15:46:37 -0700308 wp<MediaRecorderClient> w = recorder;
309 Mutex::Autolock lock(mLock);
310 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100311 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 return recorder;
313}
314
Gloria Wangdac6a312009-10-29 15:46:37 -0700315void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
316{
317 Mutex::Autolock lock(mLock);
318 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100319 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700320}
321
Glenn Kastenf37971f2012-02-03 11:06:53 -0800322sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800324 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100326 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 return retriever;
328}
329
Glenn Kastenf37971f2012-02-03 11:06:53 -0800330sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100331 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800333 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700335
336 sp<Client> c = new Client(
337 this, pid, connId, client, audioSessionId,
338 IPCThreadState::self()->getCallingUid());
339
Steve Block3856b092011-10-20 11:56:00 +0100340 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100341 IPCThreadState::self()->getCallingUid());
342
343 wp<Client> w = c;
344 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345 Mutex::Autolock lock(mLock);
346 mClients.add(w);
347 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800348 return c;
349}
350
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700351sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
352 return MediaCodecList::getLocalInstance();
353}
354
Andreas Huber318ad9c2009-10-15 13:46:54 -0700355sp<IOMX> MediaPlayerService::getOMX() {
356 Mutex::Autolock autoLock(mLock);
357
358 if (mOMX.get() == NULL) {
359 mOMX = new OMX;
360 }
361
362 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700363}
364
Andreas Hubered3e3e02012-03-26 11:13:27 -0700365sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700366 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700367}
368
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800369sp<IDrm> MediaPlayerService::makeDrm() {
370 return new Drm;
371}
372
Andreas Huber279dcd82013-01-30 10:41:25 -0800373sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
374 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700375}
376
Jeff Brown2013a542012-09-04 21:38:42 -0700377sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
Svet Ganovbe71aa22015-04-28 12:06:02 -0700378 const String16 &opPackageName,
Jeff Brown2013a542012-09-04 21:38:42 -0700379 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700380 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
381 return NULL;
382 }
383
Svet Ganovbe71aa22015-04-28 12:06:02 -0700384 return new RemoteDisplay(opPackageName, client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700385}
386
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
388{
389 const size_t SIZE = 256;
390 char buffer[SIZE];
391 String8 result;
392
393 result.append(" AudioOutput\n");
394 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
395 mStreamType, mLeftVolume, mRightVolume);
396 result.append(buffer);
397 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800398 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700400 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
401 mAuxEffectId, mSendLevel);
402 result.append(buffer);
403
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 ::write(fd, result.string(), result.size());
405 if (mTrack != 0) {
406 mTrack->dump(fd, args);
407 }
408 return NO_ERROR;
409}
410
Lajos Molnar6d339f12015-04-17 16:15:53 -0700411status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412{
413 const size_t SIZE = 256;
414 char buffer[SIZE];
415 String8 result;
416 result.append(" Client\n");
417 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
418 mPid, mConnId, mStatus, mLoop?"true": "false");
419 result.append(buffer);
420 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700421 if (mPlayer != NULL) {
422 mPlayer->dump(fd, args);
423 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 if (mAudioOutput != 0) {
425 mAudioOutput->dump(fd, args);
426 }
427 write(fd, "\n", 1);
428 return NO_ERROR;
429}
430
Marco Nelissenf09611f2015-02-13 14:12:42 -0800431/**
432 * The only arguments this understands right now are -c, -von and -voff,
433 * which are parsed by ALooperRoster::dump()
434 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
436{
437 const size_t SIZE = 256;
438 char buffer[SIZE];
439 String8 result;
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530440 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
441 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
442
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
444 snprintf(buffer, SIZE, "Permission Denial: "
445 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
446 IPCThreadState::self()->getCallingPid(),
447 IPCThreadState::self()->getCallingUid());
448 result.append(buffer);
449 } else {
450 Mutex::Autolock lock(mLock);
451 for (int i = 0, n = mClients.size(); i < n; ++i) {
452 sp<Client> c = mClients[i].promote();
453 if (c != 0) c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530454 clients.add(c);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 }
James Dongb9141222010-07-08 11:16:11 -0700456 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 Donge579e282011-10-18 22:29:20 -0700461 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);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530467 mediaRecorderClients.add(c);
James Donge579e282011-10-18 22:29:20 -0700468 }
James Dongb9141222010-07-08 11:16:11 -0700469 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700470 }
471
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800473 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474 FILE *f = fopen(buffer, "r");
475 if (f) {
476 while (!feof(f)) {
477 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700478 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700480 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800481 strstr(buffer, " /system/media/")) {
482 result.append(" ");
483 result.append(buffer);
484 }
485 }
486 fclose(f);
487 } else {
488 result.append("couldn't open ");
489 result.append(buffer);
490 result.append("\n");
491 }
492
Marco Nelissenf09611f2015-02-13 14:12:42 -0800493 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494 DIR *d = opendir(buffer);
495 if (d) {
496 struct dirent *ent;
497 while((ent = readdir(d)) != NULL) {
498 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800499 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500 struct stat s;
501 if (lstat(buffer, &s) == 0) {
502 if ((s.st_mode & S_IFMT) == S_IFLNK) {
503 char linkto[256];
504 int len = readlink(buffer, linkto, sizeof(linkto));
505 if(len > 0) {
506 if(len > 255) {
507 linkto[252] = '.';
508 linkto[253] = '.';
509 linkto[254] = '.';
510 linkto[255] = 0;
511 } else {
512 linkto[len] = 0;
513 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700514 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700516 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517 strstr(linkto, "/system/media/") == linkto) {
518 result.append(" ");
519 result.append(buffer);
520 result.append(" -> ");
521 result.append(linkto);
522 result.append("\n");
523 }
524 }
525 } else {
526 result.append(" unexpected type for ");
527 result.append(buffer);
528 result.append("\n");
529 }
530 }
531 }
532 }
533 closedir(d);
534 } else {
535 result.append("couldn't open ");
536 result.append(buffer);
537 result.append("\n");
538 }
539
Marco Nelissenf09611f2015-02-13 14:12:42 -0800540 gLooperRoster.dump(fd, args);
541
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542 bool dumpMem = false;
543 for (size_t i = 0; i < args.size(); i++) {
544 if (args[i] == String16("-m")) {
545 dumpMem = true;
546 }
547 }
548 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700549 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551 }
552 write(fd, result.string(), result.size());
553 return NO_ERROR;
554}
555
556void MediaPlayerService::removeClient(wp<Client> client)
557{
558 Mutex::Autolock lock(mLock);
559 mClients.remove(client);
560}
561
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700562MediaPlayerService::Client::Client(
563 const sp<MediaPlayerService>& service, pid_t pid,
564 int32_t connId, const sp<IMediaPlayerClient>& client,
565 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566{
Steve Block3856b092011-10-20 11:56:00 +0100567 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 mPid = pid;
569 mConnId = connId;
570 mService = service;
571 mClient = client;
572 mLoop = false;
573 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700574 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700575 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800576 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700577 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700578
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000580 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 mAntagonizer = new Antagonizer(notify, this);
582#endif
583}
584
585MediaPlayerService::Client::~Client()
586{
Steve Block3856b092011-10-20 11:56:00 +0100587 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 mAudioOutput.clear();
589 wp<Client> client(this);
590 disconnect();
591 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700592 if (mAudioAttributes != NULL) {
593 free(mAudioAttributes);
594 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595}
596
597void MediaPlayerService::Client::disconnect()
598{
Steve Block3856b092011-10-20 11:56:00 +0100599 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600 // grab local reference and clear main reference to prevent future
601 // access to object
602 sp<MediaPlayerBase> p;
603 {
604 Mutex::Autolock l(mLock);
605 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800606 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700608
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609 mPlayer.clear();
610
611 // clear the notification to prevent callbacks to dead client
612 // and reset the player. We assume the player will serialize
613 // access to itself if necessary.
614 if (p != 0) {
615 p->setNotifyCallback(0, 0);
616#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000617 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 mAntagonizer->kill();
619#endif
620 p->reset();
621 }
622
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700623 disconnectNativeWindow();
624
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625 IPCThreadState::self()->flushCommands();
626}
627
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
629{
630 // determine if we have the right player type
631 sp<MediaPlayerBase> p = mPlayer;
632 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100633 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 p.clear();
635 }
636 if (p == NULL) {
Ronghua Wu68845c12015-07-21 09:50:48 -0700637 p = MediaPlayerFactory::createPlayer(playerType, this, notify, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700639
Jason Simmonsdb29e522011-08-12 13:46:55 -0700640 if (p != NULL) {
641 p->setUID(mUID);
642 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700643
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644 return p;
645}
646
John Grossmanc795b642012-02-22 15:38:35 -0800647sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
648 player_type playerType)
649{
650 ALOGV("player type = %d", playerType);
651
652 // create the right type of player
653 sp<MediaPlayerBase> p = createPlayer(playerType);
654 if (p == NULL) {
655 return p;
656 }
657
658 if (!p->hardwareOutput()) {
Jean-Michel Trivi2650e962015-07-22 18:14:02 -0700659 Mutex::Autolock l(mLock);
Marco Nelissend457c972014-02-11 08:47:07 -0800660 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700661 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800662 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
663 }
664
665 return p;
666}
667
668void MediaPlayerService::Client::setDataSource_post(
669 const sp<MediaPlayerBase>& p,
670 status_t status)
671{
672 ALOGV(" setDataSource");
673 mStatus = status;
674 if (mStatus != OK) {
675 ALOGE(" error: %d", mStatus);
676 return;
677 }
678
679 // Set the re-transmission endpoint if one was chosen.
680 if (mRetransmitEndpointValid) {
681 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
682 if (mStatus != NO_ERROR) {
683 ALOGE("setRetransmitEndpoint error: %d", mStatus);
684 }
685 }
686
687 if (mStatus == OK) {
688 mPlayer = p;
689 }
690}
691
Andreas Huber2db84552010-01-28 11:19:57 -0800692status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800693 const sp<IMediaHTTPService> &httpService,
694 const char *url,
695 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696{
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698 if (url == NULL)
699 return UNKNOWN_ERROR;
700
Dave Burked681bbb2011-08-30 14:39:17 +0100701 if ((strncmp(url, "http://", 7) == 0) ||
702 (strncmp(url, "https://", 8) == 0) ||
703 (strncmp(url, "rtsp://", 7) == 0)) {
704 if (!checkPermission("android.permission.INTERNET")) {
705 return PERMISSION_DENIED;
706 }
707 }
708
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709 if (strncmp(url, "content://", 10) == 0) {
710 // get a filedescriptor for the content Uri and
711 // pass it to the setDataSource(fd) method
712
713 String16 url16(url);
714 int fd = android::openContentProviderFile(url16);
715 if (fd < 0)
716 {
Steve Block29357bc2012-01-06 19:20:56 +0000717 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718 return UNKNOWN_ERROR;
719 }
720 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
721 close(fd);
722 return mStatus;
723 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700724 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800725 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
726 if (p == NULL) {
727 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 }
729
Andreas Huber1b86fe02014-01-29 11:13:26 -0800730 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800731 return mStatus;
732 }
733}
734
735status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
736{
Steve Block3856b092011-10-20 11:56:00 +0100737 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738 struct stat sb;
739 int ret = fstat(fd, &sb);
740 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000741 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742 return UNKNOWN_ERROR;
743 }
744
Ying Wang5fde15d2015-04-20 22:13:22 -0700745 ALOGV("st_dev = %llu", static_cast<uint64_t>(sb.st_dev));
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700747 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
748 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750
751 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000752 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800753 ::close(fd);
754 return UNKNOWN_ERROR;
755 }
756 if (offset + length > sb.st_size) {
757 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100758 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759 }
760
John Grossman44a7e422012-06-21 17:29:24 -0700761 player_type playerType = MediaPlayerFactory::getPlayerType(this,
762 fd,
763 offset,
764 length);
John Grossmanc795b642012-02-22 15:38:35 -0800765 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
766 if (p == NULL) {
767 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800768 }
769
770 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800771 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800772 return mStatus;
773}
774
Andreas Hubere2b10282010-11-23 11:41:34 -0800775status_t MediaPlayerService::Client::setDataSource(
776 const sp<IStreamSource> &source) {
777 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700778 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800779 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800780 if (p == NULL) {
781 return NO_INIT;
782 }
783
Andreas Hubere2b10282010-11-23 11:41:34 -0800784 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800785 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800786 return mStatus;
787}
788
Chris Watkins99f31602015-03-20 13:06:33 -0700789status_t MediaPlayerService::Client::setDataSource(
790 const sp<IDataSource> &source) {
791 sp<DataSource> dataSource = DataSource::CreateFromIDataSource(source);
792 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
793 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
794 if (p == NULL) {
795 return NO_INIT;
796 }
797 // now set data source
798 setDataSource_post(p, p->setDataSource(dataSource));
799 return mStatus;
800}
801
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700802void MediaPlayerService::Client::disconnectNativeWindow() {
803 if (mConnectedWindow != NULL) {
804 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
805 NATIVE_WINDOW_API_MEDIA);
806
807 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000808 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700809 strerror(-err), err);
810 }
811 }
812 mConnectedWindow.clear();
813}
814
Glenn Kasten11731182011-02-08 17:26:17 -0800815status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800816 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800817{
Andy McFadden484566c2012-12-18 09:46:54 -0800818 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800819 sp<MediaPlayerBase> p = getPlayer();
820 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700821
Marco Nelissenf8880202014-11-14 07:58:25 -0800822 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700823 if (mConnectedWindowBinder == binder) {
824 return OK;
825 }
826
827 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800828 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700829 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700830 status_t err = native_window_api_connect(anw.get(),
831 NATIVE_WINDOW_API_MEDIA);
832
833 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000834 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700835 // Note that we must do the reset before disconnecting from the ANW.
836 // Otherwise queue/dequeue calls could be made on the disconnected
837 // ANW, which may result in errors.
838 reset();
839
840 disconnectNativeWindow();
841
842 return err;
843 }
844 }
845
Andy McFadden484566c2012-12-18 09:46:54 -0800846 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700847 // disconnecting the old one. Otherwise queue/dequeue calls could be made
848 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800849 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700850
851 disconnectNativeWindow();
852
853 mConnectedWindow = anw;
854
855 if (err == OK) {
856 mConnectedWindowBinder = binder;
857 } else {
858 disconnectNativeWindow();
859 }
860
861 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800862}
863
Nicolas Catania1d187f12009-05-12 23:25:55 -0700864status_t MediaPlayerService::Client::invoke(const Parcel& request,
865 Parcel *reply)
866{
867 sp<MediaPlayerBase> p = getPlayer();
868 if (p == NULL) return UNKNOWN_ERROR;
869 return p->invoke(request, reply);
870}
871
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700872// This call doesn't need to access the native player.
873status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
874{
875 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700876 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700877
Nicolas Catania48290382009-07-10 13:53:06 -0700878 if (unmarshallFilter(filter, &allow, &status) &&
879 unmarshallFilter(filter, &drop, &status)) {
880 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700881
882 mMetadataAllow = allow;
883 mMetadataDrop = drop;
884 }
885 return status;
886}
887
Nicolas Catania48290382009-07-10 13:53:06 -0700888status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700889 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700890{
nikoa64c8c72009-07-20 15:07:26 -0700891 sp<MediaPlayerBase> player = getPlayer();
892 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700893
nikod608a812009-07-16 16:39:53 -0700894 status_t status;
895 // Placeholder for the return code, updated by the caller.
896 reply->writeInt32(-1);
897
nikoa64c8c72009-07-20 15:07:26 -0700898 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700899
900 // We don't block notifications while we fetch the data. We clear
901 // mMetadataUpdated first so we don't lose notifications happening
902 // during the rest of this call.
903 {
904 Mutex::Autolock lock(mLock);
905 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700906 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700907 }
908 mMetadataUpdated.clear();
909 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700910
nikoa64c8c72009-07-20 15:07:26 -0700911 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700912
nikoa64c8c72009-07-20 15:07:26 -0700913 metadata.appendHeader();
914 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700915
916 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700917 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000918 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700919 return status;
920 }
921
922 // FIXME: Implement filtering on the result. Not critical since
923 // filtering takes place on the update notifications already. This
924 // would be when all the metadata are fetch and a filter is set.
925
nikod608a812009-07-16 16:39:53 -0700926 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700927 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700928 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700929}
930
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931status_t MediaPlayerService::Client::prepareAsync()
932{
Steve Block3856b092011-10-20 11:56:00 +0100933 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934 sp<MediaPlayerBase> p = getPlayer();
935 if (p == 0) return UNKNOWN_ERROR;
936 status_t ret = p->prepareAsync();
937#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000938 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800939 if (ret == NO_ERROR) mAntagonizer->start();
940#endif
941 return ret;
942}
943
944status_t MediaPlayerService::Client::start()
945{
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 sp<MediaPlayerBase> p = getPlayer();
948 if (p == 0) return UNKNOWN_ERROR;
949 p->setLooping(mLoop);
950 return p->start();
951}
952
953status_t MediaPlayerService::Client::stop()
954{
Steve Block3856b092011-10-20 11:56:00 +0100955 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 sp<MediaPlayerBase> p = getPlayer();
957 if (p == 0) return UNKNOWN_ERROR;
958 return p->stop();
959}
960
961status_t MediaPlayerService::Client::pause()
962{
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 sp<MediaPlayerBase> p = getPlayer();
965 if (p == 0) return UNKNOWN_ERROR;
966 return p->pause();
967}
968
969status_t MediaPlayerService::Client::isPlaying(bool* state)
970{
971 *state = false;
972 sp<MediaPlayerBase> p = getPlayer();
973 if (p == 0) return UNKNOWN_ERROR;
974 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 return NO_ERROR;
977}
978
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700979status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800980{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700981 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
982 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Wei Jia98160162015-02-04 17:01:11 -0800983 sp<MediaPlayerBase> p = getPlayer();
984 if (p == 0) return UNKNOWN_ERROR;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700985 return p->setPlaybackSettings(rate);
986}
987
988status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
989{
990 sp<MediaPlayerBase> p = getPlayer();
991 if (p == 0) return UNKNOWN_ERROR;
992 status_t ret = p->getPlaybackSettings(rate);
993 if (ret == NO_ERROR) {
994 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
995 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
996 } else {
997 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
998 }
999 return ret;
1000}
1001
1002status_t MediaPlayerService::Client::setSyncSettings(
1003 const AVSyncSettings& sync, float videoFpsHint)
1004{
1005 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1006 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1007 sp<MediaPlayerBase> p = getPlayer();
1008 if (p == 0) return UNKNOWN_ERROR;
1009 return p->setSyncSettings(sync, videoFpsHint);
1010}
1011
1012status_t MediaPlayerService::Client::getSyncSettings(
1013 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1014{
1015 sp<MediaPlayerBase> p = getPlayer();
1016 if (p == 0) return UNKNOWN_ERROR;
1017 status_t ret = p->getSyncSettings(sync, videoFps);
1018 if (ret == NO_ERROR) {
1019 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1020 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1021 } else {
1022 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1023 }
1024 return ret;
Wei Jia98160162015-02-04 17:01:11 -08001025}
1026
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001027status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1028{
Steve Block3856b092011-10-20 11:56:00 +01001029 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001030 sp<MediaPlayerBase> p = getPlayer();
1031 if (p == 0) return UNKNOWN_ERROR;
1032 status_t ret = p->getCurrentPosition(msec);
1033 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001034 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001035 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001036 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001037 }
1038 return ret;
1039}
1040
1041status_t MediaPlayerService::Client::getDuration(int *msec)
1042{
Steve Block3856b092011-10-20 11:56:00 +01001043 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001044 sp<MediaPlayerBase> p = getPlayer();
1045 if (p == 0) return UNKNOWN_ERROR;
1046 status_t ret = p->getDuration(msec);
1047 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001048 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001049 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001050 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001051 }
1052 return ret;
1053}
1054
Marco Nelissen6b74d672012-02-28 16:07:44 -08001055status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1056 ALOGV("setNextPlayer");
1057 Mutex::Autolock l(mLock);
1058 sp<Client> c = static_cast<Client*>(player.get());
1059 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001060
1061 if (c != NULL) {
1062 if (mAudioOutput != NULL) {
1063 mAudioOutput->setNextOutput(c->mAudioOutput);
1064 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1065 ALOGE("no current audio output");
1066 }
1067
1068 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1069 mPlayer->setNextPlayer(mNextClient->getPlayer());
1070 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001071 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001072
Marco Nelissen6b74d672012-02-28 16:07:44 -08001073 return OK;
1074}
1075
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076status_t MediaPlayerService::Client::seekTo(int msec)
1077{
Steve Block3856b092011-10-20 11:56:00 +01001078 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001079 sp<MediaPlayerBase> p = getPlayer();
1080 if (p == 0) return UNKNOWN_ERROR;
1081 return p->seekTo(msec);
1082}
1083
1084status_t MediaPlayerService::Client::reset()
1085{
Steve Block3856b092011-10-20 11:56:00 +01001086 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001087 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001088 sp<MediaPlayerBase> p = getPlayer();
1089 if (p == 0) return UNKNOWN_ERROR;
1090 return p->reset();
1091}
1092
Glenn Kastenfff6d712012-01-12 16:38:12 -08001093status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001094{
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001096 // TODO: for hardware output, call player instead
1097 Mutex::Autolock l(mLock);
1098 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1099 return NO_ERROR;
1100}
1101
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001102status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1103{
1104 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1105 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001106 if (mAudioAttributes == NULL) {
1107 return NO_MEMORY;
1108 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001109 unmarshallAudioAttributes(parcel, mAudioAttributes);
1110
1111 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1112 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1113 mAudioAttributes->tags);
1114
1115 if (mAudioOutput != 0) {
1116 mAudioOutput->setAudioAttributes(mAudioAttributes);
1117 }
1118 return NO_ERROR;
1119}
1120
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001121status_t MediaPlayerService::Client::setLooping(int loop)
1122{
Steve Block3856b092011-10-20 11:56:00 +01001123 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001124 mLoop = loop;
1125 sp<MediaPlayerBase> p = getPlayer();
1126 if (p != 0) return p->setLooping(loop);
1127 return NO_ERROR;
1128}
1129
1130status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1131{
Steve Block3856b092011-10-20 11:56:00 +01001132 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001133
1134 // for hardware output, call player instead
1135 sp<MediaPlayerBase> p = getPlayer();
1136 {
1137 Mutex::Autolock l(mLock);
1138 if (p != 0 && p->hardwareOutput()) {
1139 MediaPlayerHWInterface* hwp =
1140 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1141 return hwp->setVolume(leftVolume, rightVolume);
1142 } else {
1143 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1144 return NO_ERROR;
1145 }
1146 }
1147
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001148 return NO_ERROR;
1149}
1150
Eric Laurent2beeb502010-07-16 07:43:46 -07001151status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1152{
Steve Block3856b092011-10-20 11:56:00 +01001153 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001154 Mutex::Autolock l(mLock);
1155 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1156 return NO_ERROR;
1157}
1158
1159status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1160{
Steve Block3856b092011-10-20 11:56:00 +01001161 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001162 Mutex::Autolock l(mLock);
1163 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1164 return NO_ERROR;
1165}
Nicolas Catania48290382009-07-10 13:53:06 -07001166
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001167status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001168 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001169 switch (key) {
1170 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1171 {
1172 Mutex::Autolock l(mLock);
1173 return setAudioAttributes_l(request);
1174 }
1175 default:
1176 sp<MediaPlayerBase> p = getPlayer();
1177 if (p == 0) { return UNKNOWN_ERROR; }
1178 return p->setParameter(key, request);
1179 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001180}
1181
1182status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001183 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001184 sp<MediaPlayerBase> p = getPlayer();
1185 if (p == 0) return UNKNOWN_ERROR;
1186 return p->getParameter(key, reply);
1187}
1188
John Grossmanc795b642012-02-22 15:38:35 -08001189status_t MediaPlayerService::Client::setRetransmitEndpoint(
1190 const struct sockaddr_in* endpoint) {
1191
1192 if (NULL != endpoint) {
1193 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1194 uint16_t p = ntohs(endpoint->sin_port);
1195 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1196 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1197 } else {
1198 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1199 }
1200
1201 sp<MediaPlayerBase> p = getPlayer();
1202
1203 // Right now, the only valid time to set a retransmit endpoint is before
1204 // player selection has been made (since the presence or absence of a
1205 // retransmit endpoint is going to determine which player is selected during
1206 // setDataSource).
1207 if (p != 0) return INVALID_OPERATION;
1208
1209 if (NULL != endpoint) {
1210 mRetransmitEndpoint = *endpoint;
1211 mRetransmitEndpointValid = true;
1212 } else {
1213 mRetransmitEndpointValid = false;
1214 }
1215
1216 return NO_ERROR;
1217}
1218
John Grossman44a7e422012-06-21 17:29:24 -07001219status_t MediaPlayerService::Client::getRetransmitEndpoint(
1220 struct sockaddr_in* endpoint)
1221{
1222 if (NULL == endpoint)
1223 return BAD_VALUE;
1224
1225 sp<MediaPlayerBase> p = getPlayer();
1226
1227 if (p != NULL)
1228 return p->getRetransmitEndpoint(endpoint);
1229
1230 if (!mRetransmitEndpointValid)
1231 return NO_INIT;
1232
1233 *endpoint = mRetransmitEndpoint;
1234
1235 return NO_ERROR;
1236}
1237
Gloria Wangb483c472011-04-11 17:23:27 -07001238void MediaPlayerService::Client::notify(
1239 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240{
1241 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001242 if (client == NULL) {
1243 return;
1244 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001245
James Dongb8a98252012-08-26 16:13:03 -07001246 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001247 {
1248 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001249 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001250 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001251 if (client->mAudioOutput != NULL)
1252 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001253 client->mNextClient->start();
1254 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1255 }
1256 }
1257
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001258 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001259 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001260 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001261
1262 if(client->shouldDropMetadata(metadata_type)) {
1263 return;
1264 }
1265
1266 // Update the list of metadata that have changed. getMetadata
1267 // also access mMetadataUpdated and clears it.
1268 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001269 }
James Dongb8a98252012-08-26 16:13:03 -07001270
1271 if (c != NULL) {
1272 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1273 c->notify(msg, ext1, ext2, obj);
1274 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275}
1276
Nicolas Catania48290382009-07-10 13:53:06 -07001277
nikoa64c8c72009-07-20 15:07:26 -07001278bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001279{
Nicolas Catania48290382009-07-10 13:53:06 -07001280 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001281
Nicolas Catania48290382009-07-10 13:53:06 -07001282 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001283 return true;
1284 }
1285
Nicolas Catania48290382009-07-10 13:53:06 -07001286 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001287 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001288 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001289 return true;
1290 }
1291}
1292
Nicolas Catania48290382009-07-10 13:53:06 -07001293
nikoa64c8c72009-07-20 15:07:26 -07001294void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001295 Mutex::Autolock lock(mLock);
1296 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1297 mMetadataUpdated.add(metadata_type);
1298 }
1299}
1300
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301#if CALLBACK_ANTAGONIZER
1302const int Antagonizer::interval = 10000; // 10 msecs
1303
1304Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1305 mExit(false), mActive(false), mClient(client), mCb(cb)
1306{
1307 createThread(callbackThread, this);
1308}
1309
1310void Antagonizer::kill()
1311{
1312 Mutex::Autolock _l(mLock);
1313 mActive = false;
1314 mExit = true;
1315 mCondition.wait(mLock);
1316}
1317
1318int Antagonizer::callbackThread(void* user)
1319{
Steve Blockb8a80522011-12-20 16:23:08 +00001320 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1322 while (!p->mExit) {
1323 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001324 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001325 p->mCb(p->mClient, 0, 0, 0);
1326 }
1327 usleep(interval);
1328 }
1329 Mutex::Autolock _l(p->mLock);
1330 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001331 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001332 return 0;
1333}
1334#endif
1335
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001336#undef LOG_TAG
1337#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001338MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1339 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001340 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001341 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001342 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001343 mBytesWritten(0),
Andy Hungd1c74342015-07-07 16:54:23 -07001344 mStreamType(AUDIO_STREAM_MUSIC),
Andy Hungd1c74342015-07-07 16:54:23 -07001345 mLeftVolume(1.0),
1346 mRightVolume(1.0),
1347 mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
1348 mSampleRateHz(0),
1349 mMsecsPerFrame(0),
1350 mFrameSize(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001351 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001352 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001353 mPid(pid),
Andy Hungd1c74342015-07-07 16:54:23 -07001354 mSendLevel(0.0),
1355 mAuxEffectId(0),
1356 mFlags(AUDIO_OUTPUT_FLAG_NONE)
1357{
Steve Block3856b092011-10-20 11:56:00 +01001358 ALOGV("AudioOutput(%d)", sessionId);
Eric Laurent43562692015-07-15 16:49:07 -07001359 if (attr != NULL) {
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001360 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1361 if (mAttributes != NULL) {
1362 memcpy(mAttributes, attr, sizeof(audio_attributes_t));
1363 mStreamType = audio_attributes_to_stream_type(attr);
1364 }
1365 } else {
1366 mAttributes = NULL;
Eric Laurent43562692015-07-15 16:49:07 -07001367 }
1368
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001369 setMinBufferCount();
1370}
1371
1372MediaPlayerService::AudioOutput::~AudioOutput()
1373{
1374 close();
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001375 free(mAttributes);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001376 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001377}
1378
Andy Hungd1c74342015-07-07 16:54:23 -07001379//static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001380void MediaPlayerService::AudioOutput::setMinBufferCount()
1381{
1382 char value[PROPERTY_VALUE_MAX];
1383 if (property_get("ro.kernel.qemu", value, 0)) {
1384 mIsOnEmulator = true;
1385 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1386 }
1387}
1388
Andy Hungd1c74342015-07-07 16:54:23 -07001389// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390bool MediaPlayerService::AudioOutput::isOnEmulator()
1391{
Andy Hungd1c74342015-07-07 16:54:23 -07001392 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001393 return mIsOnEmulator;
1394}
1395
Andy Hungd1c74342015-07-07 16:54:23 -07001396// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001397int MediaPlayerService::AudioOutput::getMinBufferCount()
1398{
Andy Hungd1c74342015-07-07 16:54:23 -07001399 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001400 return mMinBufferCount;
1401}
1402
1403ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1404{
Andy Hungd1c74342015-07-07 16:54:23 -07001405 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001406 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001407 return mTrack->frameCount() * mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001408}
1409
1410ssize_t MediaPlayerService::AudioOutput::frameCount() const
1411{
Andy Hungd1c74342015-07-07 16:54:23 -07001412 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001413 if (mTrack == 0) return NO_INIT;
1414 return mTrack->frameCount();
1415}
1416
1417ssize_t MediaPlayerService::AudioOutput::channelCount() const
1418{
Andy Hungd1c74342015-07-07 16:54:23 -07001419 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 if (mTrack == 0) return NO_INIT;
1421 return mTrack->channelCount();
1422}
1423
1424ssize_t MediaPlayerService::AudioOutput::frameSize() const
1425{
Andy Hungd1c74342015-07-07 16:54:23 -07001426 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001428 return mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429}
1430
1431uint32_t MediaPlayerService::AudioOutput::latency () const
1432{
Andy Hungd1c74342015-07-07 16:54:23 -07001433 Mutex::Autolock lock(mLock);
Eric Laurentdb354e52012-03-05 17:27:11 -08001434 if (mTrack == 0) return 0;
1435 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001436}
1437
1438float MediaPlayerService::AudioOutput::msecsPerFrame() const
1439{
Andy Hungd1c74342015-07-07 16:54:23 -07001440 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 return mMsecsPerFrame;
1442}
1443
Marco Nelissen4110c102012-03-29 09:31:28 -07001444status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001445{
Andy Hungd1c74342015-07-07 16:54:23 -07001446 Mutex::Autolock lock(mLock);
Eric Laurent342e9cf2010-01-19 17:37:09 -08001447 if (mTrack == 0) return NO_INIT;
1448 return mTrack->getPosition(position);
1449}
1450
Lajos Molnar06ad1522014-08-28 07:27:44 -07001451status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1452{
Andy Hungd1c74342015-07-07 16:54:23 -07001453 Mutex::Autolock lock(mLock);
Lajos Molnar06ad1522014-08-28 07:27:44 -07001454 if (mTrack == 0) return NO_INIT;
1455 return mTrack->getTimestamp(ts);
1456}
1457
Marco Nelissen4110c102012-03-29 09:31:28 -07001458status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1459{
Andy Hungd1c74342015-07-07 16:54:23 -07001460 Mutex::Autolock lock(mLock);
Marco Nelissen4110c102012-03-29 09:31:28 -07001461 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001462 *frameswritten = mBytesWritten / mFrameSize;
Marco Nelissen4110c102012-03-29 09:31:28 -07001463 return OK;
1464}
1465
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001466status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1467{
Andy Hungd1c74342015-07-07 16:54:23 -07001468 Mutex::Autolock lock(mLock);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001469 if (mTrack == 0) return NO_INIT;
1470 return mTrack->setParameters(keyValuePairs);
1471}
1472
1473String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1474{
Andy Hungd1c74342015-07-07 16:54:23 -07001475 Mutex::Autolock lock(mLock);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001476 if (mTrack == 0) return String8::empty();
1477 return mTrack->getParameters(keys);
1478}
1479
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001480void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
Andy Hungd1c74342015-07-07 16:54:23 -07001481 Mutex::Autolock lock(mLock);
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001482 if (attributes == NULL) {
1483 free(mAttributes);
1484 mAttributes = NULL;
1485 } else {
1486 if (mAttributes == NULL) {
1487 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1488 }
1489 memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
Eric Laurent43562692015-07-15 16:49:07 -07001490 mStreamType = audio_attributes_to_stream_type(attributes);
1491 }
1492}
1493
1494void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType)
1495{
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001496 Mutex::Autolock lock(mLock);
Eric Laurent43562692015-07-15 16:49:07 -07001497 // do not allow direct stream type modification if attributes have been set
1498 if (mAttributes == NULL) {
1499 mStreamType = streamType;
1500 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001501}
1502
Andy Hungd1c74342015-07-07 16:54:23 -07001503void MediaPlayerService::AudioOutput::deleteRecycledTrack_l()
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001504{
Andy Hungd1c74342015-07-07 16:54:23 -07001505 ALOGV("deleteRecycledTrack_l");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001506 if (mRecycledTrack != 0) {
1507
1508 if (mCallbackData != NULL) {
1509 mCallbackData->setOutput(NULL);
1510 mCallbackData->endTrackSwitch();
1511 }
1512
1513 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1514 mRecycledTrack->flush();
1515 }
1516 // An offloaded track isn't flushed because the STREAM_END is reported
1517 // slightly prematurely to allow time for the gapless track switch
1518 // but this means that if we decide not to recycle the track there
1519 // could be a small amount of residual data still playing. We leave
1520 // AudioFlinger to drain the track.
1521
1522 mRecycledTrack.clear();
Andy Hungd1c74342015-07-07 16:54:23 -07001523 close_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001524 delete mCallbackData;
1525 mCallbackData = NULL;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001526 }
1527}
1528
Andy Hungd1c74342015-07-07 16:54:23 -07001529void MediaPlayerService::AudioOutput::close_l()
1530{
1531 mTrack.clear();
1532}
1533
Andreas Huber20111aa2009-07-14 16:56:47 -07001534status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001535 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1536 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001537 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001538 audio_output_flags_t flags,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001539 const audio_offload_info_t *offloadInfo,
Andy Hung179652e2015-05-31 22:49:46 -07001540 bool doNotReconnect,
1541 uint32_t suggestedFrameCount)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542{
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001543 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1544 format, bufferCount, mSessionId, flags);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001545
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001546 // offloading is only supported in callback mode for now.
1547 // offloadInfo must be present if offload flag is set
1548 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1549 ((cb == NULL) || (offloadInfo == NULL))) {
1550 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551 }
1552
Andy Hung179652e2015-05-31 22:49:46 -07001553 // compute frame count for the AudioTrack internal buffer
1554 size_t frameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001555 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1556 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1557 } else {
Andy Hung179652e2015-05-31 22:49:46 -07001558 // try to estimate the buffer processing fetch size from AudioFlinger.
1559 // framesPerBuffer is approximate and generally correct, except when it's not :-).
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001560 uint32_t afSampleRate;
1561 size_t afFrameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001562 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1563 return NO_INIT;
1564 }
1565 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1566 return NO_INIT;
1567 }
Andy Hung179652e2015-05-31 22:49:46 -07001568 const size_t framesPerBuffer =
1569 (unsigned long long)sampleRate * afFrameCount / afSampleRate;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001570
Andy Hung179652e2015-05-31 22:49:46 -07001571 if (bufferCount == 0) {
1572 // use suggestedFrameCount
1573 bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer;
1574 }
1575 // Check argument bufferCount against the mininum buffer count
1576 if (bufferCount != 0 && bufferCount < mMinBufferCount) {
1577 ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount);
1578 bufferCount = mMinBufferCount;
1579 }
1580 // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger
1581 // which will be the minimum size permitted.
1582 frameCount = bufferCount * framesPerBuffer;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001583 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001584
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001585 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001586 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001587 if (0 == channelMask) {
1588 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1589 return NO_INIT;
1590 }
1591 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001592
Andy Hungd1c74342015-07-07 16:54:23 -07001593 Mutex::Autolock lock(mLock);
Andy Hung179652e2015-05-31 22:49:46 -07001594 mCallback = cb;
1595 mCallbackCookie = cookie;
1596
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001597 // Check whether we can recycle the track
1598 bool reuse = false;
1599 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001600
Glenn Kasten2799d742013-05-30 14:33:29 -07001601 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001602 // check whether we are switching between two offloaded tracks
1603 bothOffloaded = (flags & mRecycledTrack->getFlags()
1604 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001605
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001606 // check if the existing track can be reused as-is, or if a new track needs to be created.
1607 reuse = true;
1608
Marco Nelissen67295b52012-06-11 14:52:53 -07001609 if ((mCallbackData == NULL && mCallback != NULL) ||
1610 (mCallbackData != NULL && mCallback == NULL)) {
1611 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1612 ALOGV("can't chain callback and write");
1613 reuse = false;
1614 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001615 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1616 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001617 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001618 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001619 reuse = false;
1620 } else if (flags != mFlags) {
1621 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1622 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001623 } else if (mRecycledTrack->format() != format) {
1624 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001625 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001626 } else {
1627 ALOGV("no track available to recycle");
1628 }
1629
1630 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1631
1632 // If we can't recycle and both tracks are offloaded
1633 // we must close the previous output before opening a new one
1634 if (bothOffloaded && !reuse) {
1635 ALOGV("both offloaded and not recycling");
Andy Hungd1c74342015-07-07 16:54:23 -07001636 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001637 }
1638
1639 sp<AudioTrack> t;
1640 CallbackData *newcbd = NULL;
1641
1642 // We don't attempt to create a new track if we are recycling an
1643 // offloaded track. But, if we are recycling a non-offloaded or we
1644 // are switching where one is offloaded and one isn't then we create
1645 // the new track in advance so that we can read additional stream info
1646
1647 if (!(reuse && bothOffloaded)) {
1648 ALOGV("creating new AudioTrack");
1649
1650 if (mCallback != NULL) {
1651 newcbd = new CallbackData(this);
1652 t = new AudioTrack(
1653 mStreamType,
1654 sampleRate,
1655 format,
1656 channelMask,
1657 frameCount,
1658 flags,
1659 CallbackWrapper,
1660 newcbd,
1661 0, // notification frames
1662 mSessionId,
1663 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001664 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001665 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001666 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001667 mAttributes,
1668 doNotReconnect);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001669 } else {
1670 t = new AudioTrack(
1671 mStreamType,
1672 sampleRate,
1673 format,
1674 channelMask,
1675 frameCount,
1676 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001677 NULL, // callback
1678 NULL, // user data
1679 0, // notification frames
1680 mSessionId,
1681 AudioTrack::TRANSFER_DEFAULT,
1682 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001683 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001684 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001685 mAttributes,
1686 doNotReconnect);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001687 }
1688
1689 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1690 ALOGE("Unable to create audio track");
1691 delete newcbd;
Glenn Kasten3e98ecd2015-05-18 13:13:24 -07001692 // t goes out of scope, so reference count drops to zero
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001693 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001694 } else {
1695 // successful AudioTrack initialization implies a legacy stream type was generated
1696 // from the audio attributes
1697 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001698 }
1699 }
1700
1701 if (reuse) {
1702 CHECK(mRecycledTrack != NULL);
1703
1704 if (!bothOffloaded) {
1705 if (mRecycledTrack->frameCount() != t->frameCount()) {
1706 ALOGV("framecount differs: %u/%u frames",
1707 mRecycledTrack->frameCount(), t->frameCount());
1708 reuse = false;
1709 }
1710 }
1711
Marco Nelissen67295b52012-06-11 14:52:53 -07001712 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001713 ALOGV("chaining to next output and recycling track");
Andy Hungd1c74342015-07-07 16:54:23 -07001714 close_l();
Marco Nelissen67295b52012-06-11 14:52:53 -07001715 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001716 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001717 if (mCallbackData != NULL) {
1718 mCallbackData->setOutput(this);
1719 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001720 delete newcbd;
1721 return OK;
1722 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001723 }
1724
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001725 // we're not going to reuse the track, unblock and flush it
1726 // this was done earlier if both tracks are offloaded
1727 if (!bothOffloaded) {
Andy Hungd1c74342015-07-07 16:54:23 -07001728 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001729 }
1730
1731 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1732
Marco Nelissen67295b52012-06-11 14:52:53 -07001733 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001734 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001735 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001736
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001737 mSampleRateHz = sampleRate;
Preetam Singh Ranawat2e17eef2015-08-12 12:11:46 -07001738 mFlags = flags;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001739 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
Andy Hungd1c74342015-07-07 16:54:23 -07001740 mFrameSize = t->frameSize();
Marco Nelissen99448602012-04-02 12:16:49 -07001741 uint32_t pos;
1742 if (t->getPosition(&pos) == OK) {
Andy Hungd1c74342015-07-07 16:54:23 -07001743 mBytesWritten = uint64_t(pos) * mFrameSize;
Marco Nelissen99448602012-04-02 12:16:49 -07001744 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001745 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001746
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001747 status_t res = NO_ERROR;
Wei Jia0162d002015-06-09 11:59:33 -07001748 // Note some output devices may give us a direct track even though we don't specify it.
1749 // Example: Line application b/17459982.
Preetam Singh Ranawat2e17eef2015-08-12 12:11:46 -07001750 if ((t->getFlags() & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001751 res = t->setPlaybackRate(mPlaybackRate);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001752 if (res == NO_ERROR) {
1753 t->setAuxEffectSendLevel(mSendLevel);
1754 res = t->attachAuxEffect(mAuxEffectId);
1755 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001756 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001757 ALOGV("open() DONE status %d", res);
1758 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001759}
1760
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001761status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001762{
Steve Block3856b092011-10-20 11:56:00 +01001763 ALOGV("start");
Andy Hungd1c74342015-07-07 16:54:23 -07001764 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001765 if (mCallbackData != NULL) {
1766 mCallbackData->endTrackSwitch();
1767 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001768 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001769 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001770 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001771 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001772 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001773 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001774}
1775
Marco Nelissen6b74d672012-02-28 16:07:44 -08001776void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
Andy Hungd1c74342015-07-07 16:54:23 -07001777 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001778 mNextOutput = nextOutput;
1779}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001780
Marco Nelissen6b74d672012-02-28 16:07:44 -08001781void MediaPlayerService::AudioOutput::switchToNextOutput() {
1782 ALOGV("switchToNextOutput");
Andy Hungd1c74342015-07-07 16:54:23 -07001783
1784 // Try to acquire the callback lock before moving track (without incurring deadlock).
1785 const unsigned kMaxSwitchTries = 100;
1786 Mutex::Autolock lock(mLock);
1787 for (unsigned tries = 0;;) {
1788 if (mTrack == 0) {
1789 return;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001790 }
Andy Hungd1c74342015-07-07 16:54:23 -07001791 if (mNextOutput != NULL && mNextOutput != this) {
1792 if (mCallbackData != NULL) {
1793 // two alternative approaches
1794#if 1
1795 CallbackData *callbackData = mCallbackData;
1796 mLock.unlock();
1797 // proper acquisition sequence
1798 callbackData->lock();
1799 mLock.lock();
1800 // Caution: it is unlikely that someone deleted our callback or changed our target
1801 if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) {
1802 // fatal if we are starved out.
1803 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
1804 "switchToNextOutput() cannot obtain correct lock sequence");
1805 callbackData->unlock();
1806 continue;
1807 }
1808 callbackData->mSwitching = true; // begin track switch
1809#else
1810 // tryBeginTrackSwitch() returns false if the callback has the lock.
1811 if (!mCallbackData->tryBeginTrackSwitch()) {
1812 // fatal if we are starved out.
1813 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
1814 "switchToNextOutput() cannot obtain callback lock");
1815 mLock.unlock();
1816 usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput
1817 mLock.lock();
1818 continue;
1819 }
1820#endif
1821 }
1822
1823 Mutex::Autolock nextLock(mNextOutput->mLock);
1824
1825 // If the next output track is not NULL, then it has been
1826 // opened already for playback.
1827 // This is possible even without the next player being started,
1828 // for example, the next player could be prepared and seeked.
1829 //
1830 // Presuming it isn't advisable to force the track over.
1831 if (mNextOutput->mTrack == NULL) {
1832 ALOGD("Recycling track for gapless playback");
1833 delete mNextOutput->mCallbackData;
1834 mNextOutput->mCallbackData = mCallbackData;
1835 mNextOutput->mRecycledTrack = mTrack;
1836 mNextOutput->mSampleRateHz = mSampleRateHz;
1837 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
1838 mNextOutput->mBytesWritten = mBytesWritten;
1839 mNextOutput->mFlags = mFlags;
1840 mNextOutput->mFrameSize = mFrameSize;
1841 close_l();
1842 mCallbackData = NULL; // destruction handled by mNextOutput
1843 } else {
1844 ALOGW("Ignoring gapless playback because next player has already started");
1845 // remove track in case resource needed for future players.
1846 if (mCallbackData != NULL) {
1847 mCallbackData->endTrackSwitch(); // release lock for callbacks before close.
1848 }
1849 close_l();
1850 }
1851 }
1852 break;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001853 }
1854}
1855
Wei Jia7d3f4df2015-03-03 15:28:00 -08001856ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001857{
Andy Hungd1c74342015-07-07 16:54:23 -07001858 Mutex::Autolock lock(mLock);
Glenn Kastenadad3d72014-02-21 14:51:43 -08001859 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001860
Steve Block3856b092011-10-20 11:56:00 +01001861 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001862 if (mTrack != 0) {
Wei Jia7d3f4df2015-03-03 15:28:00 -08001863 ssize_t ret = mTrack->write(buffer, size, blocking);
Andy Hunga31335a2014-08-20 17:37:59 -07001864 if (ret >= 0) {
1865 mBytesWritten += ret;
1866 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001867 return ret;
1868 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001869 return NO_INIT;
1870}
1871
1872void MediaPlayerService::AudioOutput::stop()
1873{
Steve Block3856b092011-10-20 11:56:00 +01001874 ALOGV("stop");
Andy Hungd1c74342015-07-07 16:54:23 -07001875 Mutex::Autolock lock(mLock);
Andy Hungda17c042015-06-01 15:53:14 -07001876 mBytesWritten = 0;
Glenn Kasten2799d742013-05-30 14:33:29 -07001877 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001878}
1879
1880void MediaPlayerService::AudioOutput::flush()
1881{
Steve Block3856b092011-10-20 11:56:00 +01001882 ALOGV("flush");
Andy Hungd1c74342015-07-07 16:54:23 -07001883 Mutex::Autolock lock(mLock);
Andy Hungda17c042015-06-01 15:53:14 -07001884 mBytesWritten = 0;
Glenn Kasten2799d742013-05-30 14:33:29 -07001885 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001886}
1887
1888void MediaPlayerService::AudioOutput::pause()
1889{
Steve Block3856b092011-10-20 11:56:00 +01001890 ALOGV("pause");
Andy Hungd1c74342015-07-07 16:54:23 -07001891 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07001892 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001893}
1894
1895void MediaPlayerService::AudioOutput::close()
1896{
Steve Block3856b092011-10-20 11:56:00 +01001897 ALOGV("close");
Erik Wolsheimer7845a1f2015-10-30 12:07:52 -07001898 sp<AudioTrack> track;
1899 {
1900 Mutex::Autolock lock(mLock);
1901 track = mTrack;
1902 close_l(); // clears mTrack
1903 }
1904 // destruction of the track occurs outside of mutex.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001905}
1906
1907void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1908{
Steve Block3856b092011-10-20 11:56:00 +01001909 ALOGV("setVolume(%f, %f)", left, right);
Andy Hungd1c74342015-07-07 16:54:23 -07001910 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001911 mLeftVolume = left;
1912 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001913 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001914 mTrack->setVolume(left, right);
1915 }
1916}
1917
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001918status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001919{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001920 ALOGV("setPlaybackRate(%f %f %d %d)",
1921 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Andy Hungd1c74342015-07-07 16:54:23 -07001922 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001923 if (mTrack == 0) {
1924 // remember rate so that we can set it when the track is opened
1925 mPlaybackRate = rate;
1926 return OK;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001927 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001928 status_t res = mTrack->setPlaybackRate(rate);
1929 if (res != NO_ERROR) {
1930 return res;
1931 }
1932 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
1933 CHECK_GT(rate.mSpeed, 0.f);
1934 mPlaybackRate = rate;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001935 if (mSampleRateHz != 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001936 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001937 }
1938 return res;
1939}
1940
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001941status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
1942{
1943 ALOGV("setPlaybackRate");
Andy Hungd1c74342015-07-07 16:54:23 -07001944 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001945 if (mTrack == 0) {
1946 return NO_INIT;
1947 }
1948 *rate = mTrack->getPlaybackRate();
1949 return NO_ERROR;
1950}
1951
Eric Laurent2beeb502010-07-16 07:43:46 -07001952status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1953{
Steve Block3856b092011-10-20 11:56:00 +01001954 ALOGV("setAuxEffectSendLevel(%f)", level);
Andy Hungd1c74342015-07-07 16:54:23 -07001955 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07001956 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001957 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001958 return mTrack->setAuxEffectSendLevel(level);
1959 }
1960 return NO_ERROR;
1961}
1962
1963status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1964{
Steve Block3856b092011-10-20 11:56:00 +01001965 ALOGV("attachAuxEffect(%d)", effectId);
Andy Hungd1c74342015-07-07 16:54:23 -07001966 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07001967 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001968 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001969 return mTrack->attachAuxEffect(effectId);
1970 }
1971 return NO_ERROR;
1972}
1973
Andreas Huber20111aa2009-07-14 16:56:47 -07001974// static
1975void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001976 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001977 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001978 CallbackData *data = (CallbackData*)cookie;
Andy Hungd1c74342015-07-07 16:54:23 -07001979 // lock to ensure we aren't caught in the middle of a track switch.
Marco Nelissen6b74d672012-02-28 16:07:44 -08001980 data->lock();
1981 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001982 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001983 if (me == NULL) {
1984 // no output set, likely because the track was scheduled to be reused
1985 // by another player, but the format turned out to be incompatible.
1986 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001987 if (buffer != NULL) {
1988 buffer->size = 0;
1989 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001990 return;
1991 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001992
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001993 switch(event) {
1994 case AudioTrack::EVENT_MORE_DATA: {
1995 size_t actualSize = (*me->mCallback)(
1996 me, buffer->raw, buffer->size, me->mCallbackCookie,
1997 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001998
Andy Hung719b46b2015-05-31 22:18:25 -07001999 // Log when no data is returned from the callback.
2000 // (1) We may have no data (especially with network streaming sources).
2001 // (2) We may have reached the EOS and the audio track is not stopped yet.
2002 // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
2003 // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill).
2004 //
2005 // This is a benign busy-wait, with the next data request generated 10 ms or more later;
2006 // nevertheless for power reasons, we don't want to see too many of these.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08002007
Andy Hung719b46b2015-05-31 22:18:25 -07002008 ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002009
Andy Hung719b46b2015-05-31 22:18:25 -07002010 me->mBytesWritten += actualSize; // benign race with reader.
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002011 buffer->size = actualSize;
2012 } break;
2013
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002014 case AudioTrack::EVENT_STREAM_END:
Andy Hung719b46b2015-05-31 22:18:25 -07002015 // currently only occurs for offloaded callbacks
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002016 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
2017 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2018 me->mCallbackCookie, CB_EVENT_STREAM_END);
2019 break;
2020
2021 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
2022 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
2023 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2024 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
2025 break;
2026
Glenn Kasten421743b2015-06-01 08:18:08 -07002027 case AudioTrack::EVENT_UNDERRUN:
Andy Hung719b46b2015-05-31 22:18:25 -07002028 // This occurs when there is no data available, typically
Glenn Kasten421743b2015-06-01 08:18:08 -07002029 // when there is a failure to supply data to the AudioTrack. It can also
2030 // occur in non-offloaded mode when the audio device comes out of standby.
2031 //
Andy Hung719b46b2015-05-31 22:18:25 -07002032 // If an AudioTrack underruns it outputs silence. Since this happens suddenly
2033 // it may sound like an audible pop or glitch.
2034 //
2035 // The underrun event is sent once per track underrun; the condition is reset
2036 // when more data is sent to the AudioTrack.
Glenn Kasten421743b2015-06-01 08:18:08 -07002037 ALOGI("callbackwrapper: EVENT_UNDERRUN (discarded)");
2038 break;
2039
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002040 default:
2041 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07002042 }
2043
Marco Nelissen6b74d672012-02-28 16:07:44 -08002044 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07002045}
2046
Marco Nelissen4110c102012-03-29 09:31:28 -07002047int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002048{
Andy Hungd1c74342015-07-07 16:54:23 -07002049 Mutex::Autolock lock(mLock);
Eric Laurent8c563ed2010-10-07 18:23:03 -07002050 return mSessionId;
2051}
2052
Eric Laurent6f59db12013-07-26 17:16:50 -07002053uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
2054{
Andy Hungd1c74342015-07-07 16:54:23 -07002055 Mutex::Autolock lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -07002056 if (mTrack == 0) return 0;
2057 return mTrack->getSampleRate();
2058}
2059
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002060////////////////////////////////////////////////////////////////////////////////
2061
2062struct CallbackThread : public Thread {
2063 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2064 MediaPlayerBase::AudioSink::AudioCallback cb,
2065 void *cookie);
2066
2067protected:
2068 virtual ~CallbackThread();
2069
2070 virtual bool threadLoop();
2071
2072private:
2073 wp<MediaPlayerBase::AudioSink> mSink;
2074 MediaPlayerBase::AudioSink::AudioCallback mCallback;
2075 void *mCookie;
2076 void *mBuffer;
2077 size_t mBufferSize;
2078
2079 CallbackThread(const CallbackThread &);
2080 CallbackThread &operator=(const CallbackThread &);
2081};
2082
2083CallbackThread::CallbackThread(
2084 const wp<MediaPlayerBase::AudioSink> &sink,
2085 MediaPlayerBase::AudioSink::AudioCallback cb,
2086 void *cookie)
2087 : mSink(sink),
2088 mCallback(cb),
2089 mCookie(cookie),
2090 mBuffer(NULL),
2091 mBufferSize(0) {
2092}
2093
2094CallbackThread::~CallbackThread() {
2095 if (mBuffer) {
2096 free(mBuffer);
2097 mBuffer = NULL;
2098 }
2099}
2100
2101bool CallbackThread::threadLoop() {
2102 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2103 if (sink == NULL) {
2104 return false;
2105 }
2106
2107 if (mBuffer == NULL) {
2108 mBufferSize = sink->bufferSize();
2109 mBuffer = malloc(mBufferSize);
2110 }
2111
2112 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002113 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2114 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002115
2116 if (actualSize > 0) {
2117 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002118 // Could return false on sink->write() error or short count.
2119 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002120 }
2121
2122 return true;
2123}
2124
2125////////////////////////////////////////////////////////////////////////////////
2126
Gloria Wang7cf180c2011-02-19 18:37:57 -08002127void MediaPlayerService::addBatteryData(uint32_t params)
2128{
2129 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002130
2131 int32_t time = systemTime() / 1000000L;
2132
2133 // change audio output devices. This notification comes from AudioFlinger
2134 if ((params & kBatteryDataSpeakerOn)
2135 || (params & kBatteryDataOtherAudioDeviceOn)) {
2136
2137 int deviceOn[NUM_AUDIO_DEVICES];
2138 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2139 deviceOn[i] = 0;
2140 }
2141
2142 if ((params & kBatteryDataSpeakerOn)
2143 && (params & kBatteryDataOtherAudioDeviceOn)) {
2144 deviceOn[SPEAKER_AND_OTHER] = 1;
2145 } else if (params & kBatteryDataSpeakerOn) {
2146 deviceOn[SPEAKER] = 1;
2147 } else {
2148 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2149 }
2150
2151 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2152 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2153
2154 if (mBatteryAudio.refCount > 0) { // if playing audio
2155 if (!deviceOn[i]) {
2156 mBatteryAudio.lastTime[i] += time;
2157 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2158 mBatteryAudio.lastTime[i] = 0;
2159 } else {
2160 mBatteryAudio.lastTime[i] = 0 - time;
2161 }
2162 }
2163
2164 mBatteryAudio.deviceOn[i] = deviceOn[i];
2165 }
2166 }
2167 return;
2168 }
2169
Marco Nelissenb7848f12014-12-04 08:57:56 -08002170 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08002171 if (params & kBatteryDataAudioFlingerStart) {
2172 // record the start time only if currently no other audio
2173 // is being played
2174 if (mBatteryAudio.refCount == 0) {
2175 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2176 if (mBatteryAudio.deviceOn[i]) {
2177 mBatteryAudio.lastTime[i] -= time;
2178 }
2179 }
2180 }
2181
2182 mBatteryAudio.refCount ++;
2183 return;
2184
2185 } else if (params & kBatteryDataAudioFlingerStop) {
2186 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002187 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002188 return;
2189 }
2190
2191 // record the stop time only if currently this is the only
2192 // audio being played
2193 if (mBatteryAudio.refCount == 1) {
2194 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2195 if (mBatteryAudio.deviceOn[i]) {
2196 mBatteryAudio.lastTime[i] += time;
2197 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2198 mBatteryAudio.lastTime[i] = 0;
2199 }
2200 }
2201 }
2202
2203 mBatteryAudio.refCount --;
2204 return;
2205 }
2206
Gloria Wang7cf180c2011-02-19 18:37:57 -08002207 int uid = IPCThreadState::self()->getCallingUid();
2208 if (uid == AID_MEDIA) {
2209 return;
2210 }
2211 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002212
2213 if (index < 0) { // create a new entry for this UID
2214 BatteryUsageInfo info;
2215 info.audioTotalTime = 0;
2216 info.videoTotalTime = 0;
2217 info.audioLastTime = 0;
2218 info.videoLastTime = 0;
2219 info.refCount = 0;
2220
Gloria Wang9ee159b2011-02-24 14:51:45 -08002221 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002222 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002223 return;
2224 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002225 }
2226
2227 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2228
2229 if (params & kBatteryDataCodecStarted) {
2230 if (params & kBatteryDataTrackAudio) {
2231 info.audioLastTime -= time;
2232 info.refCount ++;
2233 }
2234 if (params & kBatteryDataTrackVideo) {
2235 info.videoLastTime -= time;
2236 info.refCount ++;
2237 }
2238 } else {
2239 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002240 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002241 return;
2242 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002243 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002244 mBatteryData.removeItem(uid);
2245 return;
2246 }
2247
2248 if (params & kBatteryDataTrackAudio) {
2249 info.audioLastTime += time;
2250 info.refCount --;
2251 }
2252 if (params & kBatteryDataTrackVideo) {
2253 info.videoLastTime += time;
2254 info.refCount --;
2255 }
2256
2257 // no stream is being played by this UID
2258 if (info.refCount == 0) {
2259 info.audioTotalTime += info.audioLastTime;
2260 info.audioLastTime = 0;
2261 info.videoTotalTime += info.videoLastTime;
2262 info.videoLastTime = 0;
2263 }
2264 }
2265}
2266
2267status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2268 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002269
2270 // audio output devices usage
2271 int32_t time = systemTime() / 1000000L; //in ms
2272 int32_t totalTime;
2273
2274 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2275 totalTime = mBatteryAudio.totalTime[i];
2276
2277 if (mBatteryAudio.deviceOn[i]
2278 && (mBatteryAudio.lastTime[i] != 0)) {
2279 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2280 totalTime += tmpTime;
2281 }
2282
2283 reply->writeInt32(totalTime);
2284 // reset the total time
2285 mBatteryAudio.totalTime[i] = 0;
2286 }
2287
2288 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002289 BatteryUsageInfo info;
2290 int size = mBatteryData.size();
2291
2292 reply->writeInt32(size);
2293 int i = 0;
2294
2295 while (i < size) {
2296 info = mBatteryData.valueAt(i);
2297
2298 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2299 reply->writeInt32(info.audioTotalTime);
2300 reply->writeInt32(info.videoTotalTime);
2301
2302 info.audioTotalTime = 0;
2303 info.videoTotalTime = 0;
2304
2305 // remove the UID entry where no stream is being played
2306 if (info.refCount <= 0) {
2307 mBatteryData.removeItemsAt(i);
2308 size --;
2309 i --;
2310 }
2311 i++;
2312 }
2313 return NO_ERROR;
2314}
nikoa64c8c72009-07-20 15:07:26 -07002315} // namespace android