blob: 1936218f32a99fa5ce027cdb547585a463a589c3 [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
Chong Zhang0b30fd42014-07-23 14:46:05 -070037#include <binder/IBatteryStats.h>
Mathias Agopian75624082009-05-19 19:08:10 -070038#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryHeapBase.h>
41#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080042#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070043#include <utils/Errors.h> // for status_t
44#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070045#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070046#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070047#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048
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>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062
Dima Zavin64760242011-05-11 14:15:23 -070063#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070064
Gloria Wang7cf180c2011-02-19 18:37:57 -080065#include <private/android_filesystem_config.h>
66
James Dong559bf282012-03-28 10:29:14 -070067#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068#include "MediaRecorderClient.h"
69#include "MediaPlayerService.h"
70#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070071#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072
Nicolas Catania14d27472009-07-13 14:37:49 -070073#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070074#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080075#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070076
Andreas Huber20111aa2009-07-14 16:56:47 -070077#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070078
Andreas Hubered3e3e02012-03-26 11:13:27 -070079#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080080#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070081#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070082#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070083#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070084
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085namespace {
nikoa64c8c72009-07-20 15:07:26 -070086using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070087using android::status_t;
88using android::OK;
89using android::BAD_VALUE;
90using android::NOT_ENOUGH_DATA;
91using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070092
93// Max number of entries in the filter.
94const int kMaxFilterSize = 64; // I pulled that out of thin air.
95
nikoa64c8c72009-07-20 15:07:26 -070096// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070097
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070098
99// Unmarshall a filter from a Parcel.
100// Filter format in a parcel:
101//
102// 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
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// | number of entries (n) |
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106// | metadata type 1 |
107// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108// | metadata type 2 |
109// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110// ....
111// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112// | metadata type n |
113// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114//
115// @param p Parcel that should start with a filter.
116// @param[out] filter On exit contains the list of metadata type to be
117// filtered.
118// @param[out] status On exit contains the status code to be returned.
119// @return true if the parcel starts with a valid filter.
120bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700121 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700122 status_t *status)
123{
Nicolas Catania48290382009-07-10 13:53:06 -0700124 int32_t val;
125 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 *status = NOT_ENOUGH_DATA;
129 return false;
130 }
131
Nicolas Catania48290382009-07-10 13:53:06 -0700132 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133 {
Steve Block29357bc2012-01-06 19:20:56 +0000134 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135 *status = BAD_VALUE;
136 return false;
137 }
138
Nicolas Catania48290382009-07-10 13:53:06 -0700139 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140
141 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700142 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143
nikoa64c8c72009-07-20 15:07:26 -0700144 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145
Nicolas Catania48290382009-07-10 13:53:06 -0700146
147 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148 {
Steve Block29357bc2012-01-06 19:20:56 +0000149 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 *status = NOT_ENOUGH_DATA;
151 return false;
152 }
153
nikoa64c8c72009-07-20 15:07:26 -0700154 const Metadata::Type *data =
155 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700156
Nicolas Catania48290382009-07-10 13:53:06 -0700157 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158 {
Steve Block29357bc2012-01-06 19:20:56 +0000159 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 *status = BAD_VALUE;
161 return false;
162 }
163
164 // TODO: The stl impl of vector would be more efficient here
165 // because it degenerates into a memcpy on pod types. Try to
166 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700167 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700168 {
Nicolas Catania48290382009-07-10 13:53:06 -0700169 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170 ++data;
171 }
172 *status = OK;
173 return true;
174}
175
Nicolas Catania48290382009-07-10 13:53:06 -0700176// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177// @param val To be searched.
178// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700179bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700180{
181 // Deal with empty and ANY right away
182 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700183 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700184
Nicolas Catania48290382009-07-10 13:53:06 -0700185 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700186}
187
188} // anonymous namespace
189
190
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700191namespace {
192using android::Parcel;
193using android::String16;
194
195// marshalling tag indicating flattened utf16 tags
196// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
197const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
198
199// Audio attributes format in a parcel:
200//
201// 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
202// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203// | usage |
204// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205// | content_type |
206// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900207// | source |
208// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700209// | flags |
210// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
212// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213// | flattened tags in UTF16 |
214// | ... |
215// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216//
217// @param p Parcel that contains audio attributes.
218// @param[out] attributes On exit points to an initialized audio_attributes_t structure
219// @param[out] status On exit contains the status code to be returned.
220void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
221{
222 attributes->usage = (audio_usage_t) parcel.readInt32();
223 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900224 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700225 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
226 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
227 if (hasFlattenedTag) {
228 // the tags are UTF16, convert to UTF8
229 String16 tags = parcel.readString16();
230 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
231 if (realTagSize <= 0) {
232 strcpy(attributes->tags, "");
233 } else {
234 // copy the flattened string into the attributes as the destination for the conversion:
235 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
236 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
237 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
238 utf16_to_utf8(tags.string(), tagSize, attributes->tags);
239 }
240 } else {
241 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
242 strcpy(attributes->tags, "");
243 }
244}
245} // anonymous namespace
246
247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248namespace android {
249
Dave Burked681bbb2011-08-30 14:39:17 +0100250static bool checkPermission(const char* permissionString) {
251#ifndef HAVE_ANDROID_OS
252 return true;
253#endif
254 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
255 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000256 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100257 return ok;
258}
259
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260// 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 -0800261/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
262/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
263
264void MediaPlayerService::instantiate() {
265 defaultServiceManager()->addService(
266 String16("media.player"), new MediaPlayerService());
267}
268
269MediaPlayerService::MediaPlayerService()
270{
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800273
274 mBatteryAudio.refCount = 0;
275 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
276 mBatteryAudio.deviceOn[i] = 0;
277 mBatteryAudio.lastTime[i] = 0;
278 mBatteryAudio.totalTime[i] = 0;
279 }
280 // speaker is on by default
281 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700282
Chong Zhang0b30fd42014-07-23 14:46:05 -0700283 // reset battery stats
284 // if the mediaserver has crashed, battery stats could be left
285 // in bad state, reset the state upon service start.
286 const sp<IServiceManager> sm(defaultServiceManager());
287 if (sm != NULL) {
288 const String16 name("batterystats");
289 sp<IBatteryStats> batteryStats =
290 interface_cast<IBatteryStats>(sm->getService(name));
291 if (batteryStats != NULL) {
292 batteryStats->noteResetVideo();
293 batteryStats->noteResetAudio();
294 }
295 }
296
John Grossman44a7e422012-06-21 17:29:24 -0700297 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298}
299
300MediaPlayerService::~MediaPlayerService()
301{
Steve Block3856b092011-10-20 11:56:00 +0100302 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303}
304
Glenn Kastenf37971f2012-02-03 11:06:53 -0800305sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800307 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700308 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
309 wp<MediaRecorderClient> w = recorder;
310 Mutex::Autolock lock(mLock);
311 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 return recorder;
314}
315
Gloria Wangdac6a312009-10-29 15:46:37 -0700316void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
317{
318 Mutex::Autolock lock(mLock);
319 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100320 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700321}
322
Glenn Kastenf37971f2012-02-03 11:06:53 -0800323sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800325 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100327 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 return retriever;
329}
330
Glenn Kastenf37971f2012-02-03 11:06:53 -0800331sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100332 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800334 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700336
337 sp<Client> c = new Client(
338 this, pid, connId, client, audioSessionId,
339 IPCThreadState::self()->getCallingUid());
340
Steve Block3856b092011-10-20 11:56:00 +0100341 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100342 IPCThreadState::self()->getCallingUid());
343
344 wp<Client> w = c;
345 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346 Mutex::Autolock lock(mLock);
347 mClients.add(w);
348 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800349 return c;
350}
351
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700352sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
353 return MediaCodecList::getLocalInstance();
354}
355
Andreas Huber318ad9c2009-10-15 13:46:54 -0700356sp<IOMX> MediaPlayerService::getOMX() {
357 Mutex::Autolock autoLock(mLock);
358
359 if (mOMX.get() == NULL) {
360 mOMX = new OMX;
361 }
362
363 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700364}
365
Andreas Hubered3e3e02012-03-26 11:13:27 -0700366sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700367 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700368}
369
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800370sp<IDrm> MediaPlayerService::makeDrm() {
371 return new Drm;
372}
373
Andreas Huber279dcd82013-01-30 10:41:25 -0800374sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
375 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700376}
377
Jeff Brown2013a542012-09-04 21:38:42 -0700378sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
379 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
Jeff Brownced24b32012-09-05 17:48:03 -0700384 return new RemoteDisplay(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
411status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
412{
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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
432{
433 const size_t SIZE = 256;
434 char buffer[SIZE];
435 String8 result;
436 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
437 snprintf(buffer, SIZE, "Permission Denial: "
438 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
439 IPCThreadState::self()->getCallingPid(),
440 IPCThreadState::self()->getCallingUid());
441 result.append(buffer);
442 } else {
443 Mutex::Autolock lock(mLock);
444 for (int i = 0, n = mClients.size(); i < n; ++i) {
445 sp<Client> c = mClients[i].promote();
446 if (c != 0) c->dump(fd, args);
447 }
James Dongb9141222010-07-08 11:16:11 -0700448 if (mMediaRecorderClients.size() == 0) {
449 result.append(" No media recorder client\n\n");
450 } else {
451 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
452 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700453 if (c != 0) {
454 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
455 result.append(buffer);
456 write(fd, result.string(), result.size());
457 result = "\n";
458 c->dump(fd, args);
459 }
James Dongb9141222010-07-08 11:16:11 -0700460 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700461 }
462
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700464 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800465 FILE *f = fopen(buffer, "r");
466 if (f) {
467 while (!feof(f)) {
468 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700469 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700471 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 strstr(buffer, " /system/media/")) {
473 result.append(" ");
474 result.append(buffer);
475 }
476 }
477 fclose(f);
478 } else {
479 result.append("couldn't open ");
480 result.append(buffer);
481 result.append("\n");
482 }
483
Glenn Kasten0512ab52011-05-04 17:58:57 -0700484 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 DIR *d = opendir(buffer);
486 if (d) {
487 struct dirent *ent;
488 while((ent = readdir(d)) != NULL) {
489 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700490 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491 struct stat s;
492 if (lstat(buffer, &s) == 0) {
493 if ((s.st_mode & S_IFMT) == S_IFLNK) {
494 char linkto[256];
495 int len = readlink(buffer, linkto, sizeof(linkto));
496 if(len > 0) {
497 if(len > 255) {
498 linkto[252] = '.';
499 linkto[253] = '.';
500 linkto[254] = '.';
501 linkto[255] = 0;
502 } else {
503 linkto[len] = 0;
504 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700505 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700507 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508 strstr(linkto, "/system/media/") == linkto) {
509 result.append(" ");
510 result.append(buffer);
511 result.append(" -> ");
512 result.append(linkto);
513 result.append("\n");
514 }
515 }
516 } else {
517 result.append(" unexpected type for ");
518 result.append(buffer);
519 result.append("\n");
520 }
521 }
522 }
523 }
524 closedir(d);
525 } else {
526 result.append("couldn't open ");
527 result.append(buffer);
528 result.append("\n");
529 }
530
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531 bool dumpMem = false;
532 for (size_t i = 0; i < args.size(); i++) {
533 if (args[i] == String16("-m")) {
534 dumpMem = true;
535 }
536 }
537 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700538 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 }
541 write(fd, result.string(), result.size());
542 return NO_ERROR;
543}
544
545void MediaPlayerService::removeClient(wp<Client> client)
546{
547 Mutex::Autolock lock(mLock);
548 mClients.remove(client);
549}
550
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700551MediaPlayerService::Client::Client(
552 const sp<MediaPlayerService>& service, pid_t pid,
553 int32_t connId, const sp<IMediaPlayerClient>& client,
554 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555{
Steve Block3856b092011-10-20 11:56:00 +0100556 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 mPid = pid;
558 mConnId = connId;
559 mService = service;
560 mClient = client;
561 mLoop = false;
562 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700563 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700564 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800565 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700566 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700567
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000569 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800570 mAntagonizer = new Antagonizer(notify, this);
571#endif
572}
573
574MediaPlayerService::Client::~Client()
575{
Steve Block3856b092011-10-20 11:56:00 +0100576 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 mAudioOutput.clear();
578 wp<Client> client(this);
579 disconnect();
580 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700581 if (mAudioAttributes != NULL) {
582 free(mAudioAttributes);
583 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584}
585
586void MediaPlayerService::Client::disconnect()
587{
Steve Block3856b092011-10-20 11:56:00 +0100588 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 // grab local reference and clear main reference to prevent future
590 // access to object
591 sp<MediaPlayerBase> p;
592 {
593 Mutex::Autolock l(mLock);
594 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800595 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700597
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 mPlayer.clear();
599
600 // clear the notification to prevent callbacks to dead client
601 // and reset the player. We assume the player will serialize
602 // access to itself if necessary.
603 if (p != 0) {
604 p->setNotifyCallback(0, 0);
605#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000606 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 mAntagonizer->kill();
608#endif
609 p->reset();
610 }
611
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700612 disconnectNativeWindow();
613
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614 IPCThreadState::self()->flushCommands();
615}
616
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
618{
619 // determine if we have the right player type
620 sp<MediaPlayerBase> p = mPlayer;
621 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 p.clear();
624 }
625 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700626 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700628
Jason Simmonsdb29e522011-08-12 13:46:55 -0700629 if (p != NULL) {
630 p->setUID(mUID);
631 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700632
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633 return p;
634}
635
John Grossmanc795b642012-02-22 15:38:35 -0800636sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
637 player_type playerType)
638{
639 ALOGV("player type = %d", playerType);
640
641 // create the right type of player
642 sp<MediaPlayerBase> p = createPlayer(playerType);
643 if (p == NULL) {
644 return p;
645 }
646
647 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800648 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700649 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800650 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
651 }
652
653 return p;
654}
655
656void MediaPlayerService::Client::setDataSource_post(
657 const sp<MediaPlayerBase>& p,
658 status_t status)
659{
660 ALOGV(" setDataSource");
661 mStatus = status;
662 if (mStatus != OK) {
663 ALOGE(" error: %d", mStatus);
664 return;
665 }
666
667 // Set the re-transmission endpoint if one was chosen.
668 if (mRetransmitEndpointValid) {
669 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
670 if (mStatus != NO_ERROR) {
671 ALOGE("setRetransmitEndpoint error: %d", mStatus);
672 }
673 }
674
675 if (mStatus == OK) {
676 mPlayer = p;
677 }
678}
679
Andreas Huber2db84552010-01-28 11:19:57 -0800680status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800681 const sp<IMediaHTTPService> &httpService,
682 const char *url,
683 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684{
Steve Block3856b092011-10-20 11:56:00 +0100685 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686 if (url == NULL)
687 return UNKNOWN_ERROR;
688
Dave Burked681bbb2011-08-30 14:39:17 +0100689 if ((strncmp(url, "http://", 7) == 0) ||
690 (strncmp(url, "https://", 8) == 0) ||
691 (strncmp(url, "rtsp://", 7) == 0)) {
692 if (!checkPermission("android.permission.INTERNET")) {
693 return PERMISSION_DENIED;
694 }
695 }
696
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697 if (strncmp(url, "content://", 10) == 0) {
698 // get a filedescriptor for the content Uri and
699 // pass it to the setDataSource(fd) method
700
701 String16 url16(url);
702 int fd = android::openContentProviderFile(url16);
703 if (fd < 0)
704 {
Steve Block29357bc2012-01-06 19:20:56 +0000705 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 return UNKNOWN_ERROR;
707 }
708 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
709 close(fd);
710 return mStatus;
711 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700712 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800713 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
714 if (p == NULL) {
715 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716 }
717
Andreas Huber1b86fe02014-01-29 11:13:26 -0800718 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719 return mStatus;
720 }
721}
722
723status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
724{
Steve Block3856b092011-10-20 11:56:00 +0100725 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726 struct stat sb;
727 int ret = fstat(fd, &sb);
728 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000729 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730 return UNKNOWN_ERROR;
731 }
732
Steve Block3856b092011-10-20 11:56:00 +0100733 ALOGV("st_dev = %llu", sb.st_dev);
734 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700735 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
736 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100737 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738
739 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000740 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741 ::close(fd);
742 return UNKNOWN_ERROR;
743 }
744 if (offset + length > sb.st_size) {
745 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800747 }
748
John Grossman44a7e422012-06-21 17:29:24 -0700749 player_type playerType = MediaPlayerFactory::getPlayerType(this,
750 fd,
751 offset,
752 length);
John Grossmanc795b642012-02-22 15:38:35 -0800753 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
754 if (p == NULL) {
755 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756 }
757
758 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800759 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760 return mStatus;
761}
762
Andreas Hubere2b10282010-11-23 11:41:34 -0800763status_t MediaPlayerService::Client::setDataSource(
764 const sp<IStreamSource> &source) {
765 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700766 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800767 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800768 if (p == NULL) {
769 return NO_INIT;
770 }
771
Andreas Hubere2b10282010-11-23 11:41:34 -0800772 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800773 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800774 return mStatus;
775}
776
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700777void MediaPlayerService::Client::disconnectNativeWindow() {
778 if (mConnectedWindow != NULL) {
779 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
780 NATIVE_WINDOW_API_MEDIA);
781
782 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000783 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700784 strerror(-err), err);
785 }
786 }
787 mConnectedWindow.clear();
788}
789
Glenn Kasten11731182011-02-08 17:26:17 -0800790status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800791 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800792{
Andy McFadden484566c2012-12-18 09:46:54 -0800793 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800794 sp<MediaPlayerBase> p = getPlayer();
795 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700796
Marco Nelissenf8880202014-11-14 07:58:25 -0800797 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700798 if (mConnectedWindowBinder == binder) {
799 return OK;
800 }
801
802 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800803 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700804 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700805 status_t err = native_window_api_connect(anw.get(),
806 NATIVE_WINDOW_API_MEDIA);
807
808 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000809 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700810 // Note that we must do the reset before disconnecting from the ANW.
811 // Otherwise queue/dequeue calls could be made on the disconnected
812 // ANW, which may result in errors.
813 reset();
814
815 disconnectNativeWindow();
816
817 return err;
818 }
819 }
820
Andy McFadden484566c2012-12-18 09:46:54 -0800821 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700822 // disconnecting the old one. Otherwise queue/dequeue calls could be made
823 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800824 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700825
826 disconnectNativeWindow();
827
828 mConnectedWindow = anw;
829
830 if (err == OK) {
831 mConnectedWindowBinder = binder;
832 } else {
833 disconnectNativeWindow();
834 }
835
836 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800837}
838
Nicolas Catania1d187f12009-05-12 23:25:55 -0700839status_t MediaPlayerService::Client::invoke(const Parcel& request,
840 Parcel *reply)
841{
842 sp<MediaPlayerBase> p = getPlayer();
843 if (p == NULL) return UNKNOWN_ERROR;
844 return p->invoke(request, reply);
845}
846
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700847// This call doesn't need to access the native player.
848status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
849{
850 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700851 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700852
Nicolas Catania48290382009-07-10 13:53:06 -0700853 if (unmarshallFilter(filter, &allow, &status) &&
854 unmarshallFilter(filter, &drop, &status)) {
855 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700856
857 mMetadataAllow = allow;
858 mMetadataDrop = drop;
859 }
860 return status;
861}
862
Nicolas Catania48290382009-07-10 13:53:06 -0700863status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700864 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700865{
nikoa64c8c72009-07-20 15:07:26 -0700866 sp<MediaPlayerBase> player = getPlayer();
867 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700868
nikod608a812009-07-16 16:39:53 -0700869 status_t status;
870 // Placeholder for the return code, updated by the caller.
871 reply->writeInt32(-1);
872
nikoa64c8c72009-07-20 15:07:26 -0700873 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700874
875 // We don't block notifications while we fetch the data. We clear
876 // mMetadataUpdated first so we don't lose notifications happening
877 // during the rest of this call.
878 {
879 Mutex::Autolock lock(mLock);
880 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700881 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700882 }
883 mMetadataUpdated.clear();
884 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700885
nikoa64c8c72009-07-20 15:07:26 -0700886 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700887
nikoa64c8c72009-07-20 15:07:26 -0700888 metadata.appendHeader();
889 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700890
891 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700892 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000893 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700894 return status;
895 }
896
897 // FIXME: Implement filtering on the result. Not critical since
898 // filtering takes place on the update notifications already. This
899 // would be when all the metadata are fetch and a filter is set.
900
nikod608a812009-07-16 16:39:53 -0700901 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700902 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700903 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700904}
905
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906status_t MediaPlayerService::Client::prepareAsync()
907{
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 sp<MediaPlayerBase> p = getPlayer();
910 if (p == 0) return UNKNOWN_ERROR;
911 status_t ret = p->prepareAsync();
912#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000913 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914 if (ret == NO_ERROR) mAntagonizer->start();
915#endif
916 return ret;
917}
918
919status_t MediaPlayerService::Client::start()
920{
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800922 sp<MediaPlayerBase> p = getPlayer();
923 if (p == 0) return UNKNOWN_ERROR;
924 p->setLooping(mLoop);
925 return p->start();
926}
927
928status_t MediaPlayerService::Client::stop()
929{
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 sp<MediaPlayerBase> p = getPlayer();
932 if (p == 0) return UNKNOWN_ERROR;
933 return p->stop();
934}
935
936status_t MediaPlayerService::Client::pause()
937{
Steve Block3856b092011-10-20 11:56:00 +0100938 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800939 sp<MediaPlayerBase> p = getPlayer();
940 if (p == 0) return UNKNOWN_ERROR;
941 return p->pause();
942}
943
944status_t MediaPlayerService::Client::isPlaying(bool* state)
945{
946 *state = false;
947 sp<MediaPlayerBase> p = getPlayer();
948 if (p == 0) return UNKNOWN_ERROR;
949 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100950 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800951 return NO_ERROR;
952}
953
954status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
955{
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800957 sp<MediaPlayerBase> p = getPlayer();
958 if (p == 0) return UNKNOWN_ERROR;
959 status_t ret = p->getCurrentPosition(msec);
960 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100961 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000963 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 }
965 return ret;
966}
967
968status_t MediaPlayerService::Client::getDuration(int *msec)
969{
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800971 sp<MediaPlayerBase> p = getPlayer();
972 if (p == 0) return UNKNOWN_ERROR;
973 status_t ret = p->getDuration(msec);
974 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000977 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800978 }
979 return ret;
980}
981
Marco Nelissen6b74d672012-02-28 16:07:44 -0800982status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
983 ALOGV("setNextPlayer");
984 Mutex::Autolock l(mLock);
985 sp<Client> c = static_cast<Client*>(player.get());
986 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700987
988 if (c != NULL) {
989 if (mAudioOutput != NULL) {
990 mAudioOutput->setNextOutput(c->mAudioOutput);
991 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
992 ALOGE("no current audio output");
993 }
994
995 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
996 mPlayer->setNextPlayer(mNextClient->getPlayer());
997 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800998 }
John Grossman5f7e55e2012-08-24 14:47:25 -0700999
Marco Nelissen6b74d672012-02-28 16:07:44 -08001000 return OK;
1001}
1002
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003status_t MediaPlayerService::Client::seekTo(int msec)
1004{
Steve Block3856b092011-10-20 11:56:00 +01001005 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001006 sp<MediaPlayerBase> p = getPlayer();
1007 if (p == 0) return UNKNOWN_ERROR;
1008 return p->seekTo(msec);
1009}
1010
1011status_t MediaPlayerService::Client::reset()
1012{
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001014 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001015 sp<MediaPlayerBase> p = getPlayer();
1016 if (p == 0) return UNKNOWN_ERROR;
1017 return p->reset();
1018}
1019
Glenn Kastenfff6d712012-01-12 16:38:12 -08001020status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021{
Steve Block3856b092011-10-20 11:56:00 +01001022 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001023 // TODO: for hardware output, call player instead
1024 Mutex::Autolock l(mLock);
1025 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1026 return NO_ERROR;
1027}
1028
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001029status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1030{
1031 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1032 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1033 unmarshallAudioAttributes(parcel, mAudioAttributes);
1034
1035 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1036 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1037 mAudioAttributes->tags);
1038
1039 if (mAudioOutput != 0) {
1040 mAudioOutput->setAudioAttributes(mAudioAttributes);
1041 }
1042 return NO_ERROR;
1043}
1044
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001045status_t MediaPlayerService::Client::setLooping(int loop)
1046{
Steve Block3856b092011-10-20 11:56:00 +01001047 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001048 mLoop = loop;
1049 sp<MediaPlayerBase> p = getPlayer();
1050 if (p != 0) return p->setLooping(loop);
1051 return NO_ERROR;
1052}
1053
1054status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1055{
Steve Block3856b092011-10-20 11:56:00 +01001056 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001057
1058 // for hardware output, call player instead
1059 sp<MediaPlayerBase> p = getPlayer();
1060 {
1061 Mutex::Autolock l(mLock);
1062 if (p != 0 && p->hardwareOutput()) {
1063 MediaPlayerHWInterface* hwp =
1064 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1065 return hwp->setVolume(leftVolume, rightVolume);
1066 } else {
1067 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1068 return NO_ERROR;
1069 }
1070 }
1071
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001072 return NO_ERROR;
1073}
1074
Eric Laurent2beeb502010-07-16 07:43:46 -07001075status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1076{
Steve Block3856b092011-10-20 11:56:00 +01001077 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001078 Mutex::Autolock l(mLock);
1079 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1080 return NO_ERROR;
1081}
1082
1083status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1084{
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001086 Mutex::Autolock l(mLock);
1087 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1088 return NO_ERROR;
1089}
Nicolas Catania48290382009-07-10 13:53:06 -07001090
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001091status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001093 switch (key) {
1094 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1095 {
1096 Mutex::Autolock l(mLock);
1097 return setAudioAttributes_l(request);
1098 }
1099 default:
1100 sp<MediaPlayerBase> p = getPlayer();
1101 if (p == 0) { return UNKNOWN_ERROR; }
1102 return p->setParameter(key, request);
1103 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001104}
1105
1106status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001108 sp<MediaPlayerBase> p = getPlayer();
1109 if (p == 0) return UNKNOWN_ERROR;
1110 return p->getParameter(key, reply);
1111}
1112
John Grossmanc795b642012-02-22 15:38:35 -08001113status_t MediaPlayerService::Client::setRetransmitEndpoint(
1114 const struct sockaddr_in* endpoint) {
1115
1116 if (NULL != endpoint) {
1117 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1118 uint16_t p = ntohs(endpoint->sin_port);
1119 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1120 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1121 } else {
1122 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1123 }
1124
1125 sp<MediaPlayerBase> p = getPlayer();
1126
1127 // Right now, the only valid time to set a retransmit endpoint is before
1128 // player selection has been made (since the presence or absence of a
1129 // retransmit endpoint is going to determine which player is selected during
1130 // setDataSource).
1131 if (p != 0) return INVALID_OPERATION;
1132
1133 if (NULL != endpoint) {
1134 mRetransmitEndpoint = *endpoint;
1135 mRetransmitEndpointValid = true;
1136 } else {
1137 mRetransmitEndpointValid = false;
1138 }
1139
1140 return NO_ERROR;
1141}
1142
John Grossman44a7e422012-06-21 17:29:24 -07001143status_t MediaPlayerService::Client::getRetransmitEndpoint(
1144 struct sockaddr_in* endpoint)
1145{
1146 if (NULL == endpoint)
1147 return BAD_VALUE;
1148
1149 sp<MediaPlayerBase> p = getPlayer();
1150
1151 if (p != NULL)
1152 return p->getRetransmitEndpoint(endpoint);
1153
1154 if (!mRetransmitEndpointValid)
1155 return NO_INIT;
1156
1157 *endpoint = mRetransmitEndpoint;
1158
1159 return NO_ERROR;
1160}
1161
Gloria Wangb483c472011-04-11 17:23:27 -07001162void MediaPlayerService::Client::notify(
1163 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001164{
1165 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001166 if (client == NULL) {
1167 return;
1168 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001169
James Dongb8a98252012-08-26 16:13:03 -07001170 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001171 {
1172 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001173 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001174 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001175 if (client->mAudioOutput != NULL)
1176 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001177 client->mNextClient->start();
1178 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1179 }
1180 }
1181
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001182 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001183 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001184 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001185
1186 if(client->shouldDropMetadata(metadata_type)) {
1187 return;
1188 }
1189
1190 // Update the list of metadata that have changed. getMetadata
1191 // also access mMetadataUpdated and clears it.
1192 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001193 }
James Dongb8a98252012-08-26 16:13:03 -07001194
1195 if (c != NULL) {
1196 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1197 c->notify(msg, ext1, ext2, obj);
1198 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001199}
1200
Nicolas Catania48290382009-07-10 13:53:06 -07001201
nikoa64c8c72009-07-20 15:07:26 -07001202bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001203{
Nicolas Catania48290382009-07-10 13:53:06 -07001204 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001205
Nicolas Catania48290382009-07-10 13:53:06 -07001206 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001207 return true;
1208 }
1209
Nicolas Catania48290382009-07-10 13:53:06 -07001210 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001211 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001212 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001213 return true;
1214 }
1215}
1216
Nicolas Catania48290382009-07-10 13:53:06 -07001217
nikoa64c8c72009-07-20 15:07:26 -07001218void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001219 Mutex::Autolock lock(mLock);
1220 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1221 mMetadataUpdated.add(metadata_type);
1222 }
1223}
1224
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225#if CALLBACK_ANTAGONIZER
1226const int Antagonizer::interval = 10000; // 10 msecs
1227
1228Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1229 mExit(false), mActive(false), mClient(client), mCb(cb)
1230{
1231 createThread(callbackThread, this);
1232}
1233
1234void Antagonizer::kill()
1235{
1236 Mutex::Autolock _l(mLock);
1237 mActive = false;
1238 mExit = true;
1239 mCondition.wait(mLock);
1240}
1241
1242int Antagonizer::callbackThread(void* user)
1243{
Steve Blockb8a80522011-12-20 16:23:08 +00001244 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1246 while (!p->mExit) {
1247 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001248 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249 p->mCb(p->mClient, 0, 0, 0);
1250 }
1251 usleep(interval);
1252 }
1253 Mutex::Autolock _l(p->mLock);
1254 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001255 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256 return 0;
1257}
1258#endif
1259
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260#undef LOG_TAG
1261#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001262MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1263 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001264 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001265 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001266 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001267 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001268 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001269 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001270 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001271 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001272 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001273 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 mLeftVolume = 1.0;
1275 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001276 mPlaybackRatePermille = 1000;
1277 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001279 mAuxEffectId = 0;
1280 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001282 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283}
1284
1285MediaPlayerService::AudioOutput::~AudioOutput()
1286{
1287 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001288 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001289}
1290
1291void MediaPlayerService::AudioOutput::setMinBufferCount()
1292{
1293 char value[PROPERTY_VALUE_MAX];
1294 if (property_get("ro.kernel.qemu", value, 0)) {
1295 mIsOnEmulator = true;
1296 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1297 }
1298}
1299
1300bool MediaPlayerService::AudioOutput::isOnEmulator()
1301{
1302 setMinBufferCount();
1303 return mIsOnEmulator;
1304}
1305
1306int MediaPlayerService::AudioOutput::getMinBufferCount()
1307{
1308 setMinBufferCount();
1309 return mMinBufferCount;
1310}
1311
1312ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1313{
1314 if (mTrack == 0) return NO_INIT;
1315 return mTrack->frameCount() * frameSize();
1316}
1317
1318ssize_t MediaPlayerService::AudioOutput::frameCount() const
1319{
1320 if (mTrack == 0) return NO_INIT;
1321 return mTrack->frameCount();
1322}
1323
1324ssize_t MediaPlayerService::AudioOutput::channelCount() const
1325{
1326 if (mTrack == 0) return NO_INIT;
1327 return mTrack->channelCount();
1328}
1329
1330ssize_t MediaPlayerService::AudioOutput::frameSize() const
1331{
1332 if (mTrack == 0) return NO_INIT;
1333 return mTrack->frameSize();
1334}
1335
1336uint32_t MediaPlayerService::AudioOutput::latency () const
1337{
Eric Laurentdb354e52012-03-05 17:27:11 -08001338 if (mTrack == 0) return 0;
1339 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001340}
1341
1342float MediaPlayerService::AudioOutput::msecsPerFrame() const
1343{
1344 return mMsecsPerFrame;
1345}
1346
Marco Nelissen4110c102012-03-29 09:31:28 -07001347status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001348{
1349 if (mTrack == 0) return NO_INIT;
1350 return mTrack->getPosition(position);
1351}
1352
Lajos Molnar06ad1522014-08-28 07:27:44 -07001353status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1354{
1355 if (mTrack == 0) return NO_INIT;
1356 return mTrack->getTimestamp(ts);
1357}
1358
Marco Nelissen4110c102012-03-29 09:31:28 -07001359status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1360{
1361 if (mTrack == 0) return NO_INIT;
1362 *frameswritten = mBytesWritten / frameSize();
1363 return OK;
1364}
1365
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001366status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1367{
1368 if (mTrack == 0) return NO_INIT;
1369 return mTrack->setParameters(keyValuePairs);
1370}
1371
1372String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1373{
1374 if (mTrack == 0) return String8::empty();
1375 return mTrack->getParameters(keys);
1376}
1377
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001378void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1379 mAttributes = attributes;
1380}
1381
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001382void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1383{
1384 ALOGV("deleteRecycledTrack");
1385
1386 if (mRecycledTrack != 0) {
1387
1388 if (mCallbackData != NULL) {
1389 mCallbackData->setOutput(NULL);
1390 mCallbackData->endTrackSwitch();
1391 }
1392
1393 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1394 mRecycledTrack->flush();
1395 }
1396 // An offloaded track isn't flushed because the STREAM_END is reported
1397 // slightly prematurely to allow time for the gapless track switch
1398 // but this means that if we decide not to recycle the track there
1399 // could be a small amount of residual data still playing. We leave
1400 // AudioFlinger to drain the track.
1401
1402 mRecycledTrack.clear();
1403 delete mCallbackData;
1404 mCallbackData = NULL;
1405 close();
1406 }
1407}
1408
Andreas Huber20111aa2009-07-14 16:56:47 -07001409status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001410 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1411 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001412 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001413 audio_output_flags_t flags,
1414 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001415{
Andreas Huber20111aa2009-07-14 16:56:47 -07001416 mCallback = cb;
1417 mCallbackCookie = cookie;
1418
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419 // Check argument "bufferCount" against the mininum buffer count
1420 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001421 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001422 bufferCount = mMinBufferCount;
1423
1424 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001425 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1426 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001427 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001428 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001429 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001430
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001431 // offloading is only supported in callback mode for now.
1432 // offloadInfo must be present if offload flag is set
1433 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1434 ((cb == NULL) || (offloadInfo == NULL))) {
1435 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001436 }
1437
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001438 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1439 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1440 } else {
1441 uint32_t afSampleRate;
1442 size_t afFrameCount;
1443
1444 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1445 return NO_INIT;
1446 }
1447 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1448 return NO_INIT;
1449 }
1450
1451 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1452 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001453
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001454 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001455 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001456 if (0 == channelMask) {
1457 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1458 return NO_INIT;
1459 }
1460 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001461
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001462 // Check whether we can recycle the track
1463 bool reuse = false;
1464 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001465
Glenn Kasten2799d742013-05-30 14:33:29 -07001466 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001467 // check whether we are switching between two offloaded tracks
1468 bothOffloaded = (flags & mRecycledTrack->getFlags()
1469 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001470
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001471 // check if the existing track can be reused as-is, or if a new track needs to be created.
1472 reuse = true;
1473
Marco Nelissen67295b52012-06-11 14:52:53 -07001474 if ((mCallbackData == NULL && mCallback != NULL) ||
1475 (mCallbackData != NULL && mCallback == NULL)) {
1476 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1477 ALOGV("can't chain callback and write");
1478 reuse = false;
1479 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001480 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1481 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001482 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001483 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001484 reuse = false;
1485 } else if (flags != mFlags) {
1486 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1487 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001488 } else if (mRecycledTrack->format() != format) {
1489 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001490 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001491 } else {
1492 ALOGV("no track available to recycle");
1493 }
1494
1495 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1496
1497 // If we can't recycle and both tracks are offloaded
1498 // we must close the previous output before opening a new one
1499 if (bothOffloaded && !reuse) {
1500 ALOGV("both offloaded and not recycling");
1501 deleteRecycledTrack();
1502 }
1503
1504 sp<AudioTrack> t;
1505 CallbackData *newcbd = NULL;
1506
1507 // We don't attempt to create a new track if we are recycling an
1508 // offloaded track. But, if we are recycling a non-offloaded or we
1509 // are switching where one is offloaded and one isn't then we create
1510 // the new track in advance so that we can read additional stream info
1511
1512 if (!(reuse && bothOffloaded)) {
1513 ALOGV("creating new AudioTrack");
1514
1515 if (mCallback != NULL) {
1516 newcbd = new CallbackData(this);
1517 t = new AudioTrack(
1518 mStreamType,
1519 sampleRate,
1520 format,
1521 channelMask,
1522 frameCount,
1523 flags,
1524 CallbackWrapper,
1525 newcbd,
1526 0, // notification frames
1527 mSessionId,
1528 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001529 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001530 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001531 mPid,
1532 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001533 } else {
1534 t = new AudioTrack(
1535 mStreamType,
1536 sampleRate,
1537 format,
1538 channelMask,
1539 frameCount,
1540 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001541 NULL, // callback
1542 NULL, // user data
1543 0, // notification frames
1544 mSessionId,
1545 AudioTrack::TRANSFER_DEFAULT,
1546 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001547 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001548 mPid,
1549 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001550 }
1551
1552 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1553 ALOGE("Unable to create audio track");
1554 delete newcbd;
1555 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001556 } else {
1557 // successful AudioTrack initialization implies a legacy stream type was generated
1558 // from the audio attributes
1559 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001560 }
1561 }
1562
1563 if (reuse) {
1564 CHECK(mRecycledTrack != NULL);
1565
1566 if (!bothOffloaded) {
1567 if (mRecycledTrack->frameCount() != t->frameCount()) {
1568 ALOGV("framecount differs: %u/%u frames",
1569 mRecycledTrack->frameCount(), t->frameCount());
1570 reuse = false;
1571 }
1572 }
1573
Marco Nelissen67295b52012-06-11 14:52:53 -07001574 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001575 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001576 close();
1577 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001578 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001579 if (mCallbackData != NULL) {
1580 mCallbackData->setOutput(this);
1581 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001582 delete newcbd;
1583 return OK;
1584 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001585 }
1586
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001587 // we're not going to reuse the track, unblock and flush it
1588 // this was done earlier if both tracks are offloaded
1589 if (!bothOffloaded) {
1590 deleteRecycledTrack();
1591 }
1592
1593 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1594
Marco Nelissen67295b52012-06-11 14:52:53 -07001595 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001596 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001597 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001598
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001599 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001600 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001601 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001602 uint32_t pos;
1603 if (t->getPosition(&pos) == OK) {
1604 mBytesWritten = uint64_t(pos) * t->frameSize();
1605 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001607
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001608 status_t res = NO_ERROR;
1609 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1610 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1611 if (res == NO_ERROR) {
1612 t->setAuxEffectSendLevel(mSendLevel);
1613 res = t->attachAuxEffect(mAuxEffectId);
1614 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001615 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001616 ALOGV("open() DONE status %d", res);
1617 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001618}
1619
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001620status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621{
Steve Block3856b092011-10-20 11:56:00 +01001622 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001623 if (mCallbackData != NULL) {
1624 mCallbackData->endTrackSwitch();
1625 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001626 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001627 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001628 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001629 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001631 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001632}
1633
Marco Nelissen6b74d672012-02-28 16:07:44 -08001634void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1635 mNextOutput = nextOutput;
1636}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001637
1638
Marco Nelissen6b74d672012-02-28 16:07:44 -08001639void MediaPlayerService::AudioOutput::switchToNextOutput() {
1640 ALOGV("switchToNextOutput");
1641 if (mNextOutput != NULL) {
1642 if (mCallbackData != NULL) {
1643 mCallbackData->beginTrackSwitch();
1644 }
1645 delete mNextOutput->mCallbackData;
1646 mNextOutput->mCallbackData = mCallbackData;
1647 mCallbackData = NULL;
1648 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001649 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001650 mNextOutput->mSampleRateHz = mSampleRateHz;
1651 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001652 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001653 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001654 }
1655}
1656
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001657ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1658{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001659 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001660
Steve Block3856b092011-10-20 11:56:00 +01001661 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001662 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001663 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001664 if (ret >= 0) {
1665 mBytesWritten += ret;
1666 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001667 return ret;
1668 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001669 return NO_INIT;
1670}
1671
1672void MediaPlayerService::AudioOutput::stop()
1673{
Steve Block3856b092011-10-20 11:56:00 +01001674 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001675 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001676}
1677
1678void MediaPlayerService::AudioOutput::flush()
1679{
Steve Block3856b092011-10-20 11:56:00 +01001680 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001681 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001682}
1683
1684void MediaPlayerService::AudioOutput::pause()
1685{
Steve Block3856b092011-10-20 11:56:00 +01001686 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001687 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001688}
1689
1690void MediaPlayerService::AudioOutput::close()
1691{
Steve Block3856b092011-10-20 11:56:00 +01001692 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001693 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001694}
1695
1696void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1697{
Steve Block3856b092011-10-20 11:56:00 +01001698 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001699 mLeftVolume = left;
1700 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001701 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001702 mTrack->setVolume(left, right);
1703 }
1704}
1705
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001706status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1707{
1708 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1709 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001710 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001711 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1712 } else {
1713 res = NO_INIT;
1714 }
1715 mPlaybackRatePermille = ratePermille;
1716 if (mSampleRateHz != 0) {
1717 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1718 }
1719 return res;
1720}
1721
Eric Laurent2beeb502010-07-16 07:43:46 -07001722status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1723{
Steve Block3856b092011-10-20 11:56:00 +01001724 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001725 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001726 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001727 return mTrack->setAuxEffectSendLevel(level);
1728 }
1729 return NO_ERROR;
1730}
1731
1732status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1733{
Steve Block3856b092011-10-20 11:56:00 +01001734 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001735 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001736 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001737 return mTrack->attachAuxEffect(effectId);
1738 }
1739 return NO_ERROR;
1740}
1741
Andreas Huber20111aa2009-07-14 16:56:47 -07001742// static
1743void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001744 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001745 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001746 CallbackData *data = (CallbackData*)cookie;
1747 data->lock();
1748 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001749 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001750 if (me == NULL) {
1751 // no output set, likely because the track was scheduled to be reused
1752 // by another player, but the format turned out to be incompatible.
1753 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001754 if (buffer != NULL) {
1755 buffer->size = 0;
1756 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001757 return;
1758 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001759
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001760 switch(event) {
1761 case AudioTrack::EVENT_MORE_DATA: {
1762 size_t actualSize = (*me->mCallback)(
1763 me, buffer->raw, buffer->size, me->mCallbackCookie,
1764 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001765
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301766 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1767 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001768 // We've reached EOS but the audio track is not stopped yet,
1769 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001770
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001771 memset(buffer->raw, 0, buffer->size);
1772 actualSize = buffer->size;
1773 }
1774
1775 buffer->size = actualSize;
1776 } break;
1777
1778
1779 case AudioTrack::EVENT_STREAM_END:
1780 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1781 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1782 me->mCallbackCookie, CB_EVENT_STREAM_END);
1783 break;
1784
1785 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1786 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1787 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1788 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1789 break;
1790
1791 default:
1792 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001793 }
1794
Marco Nelissen6b74d672012-02-28 16:07:44 -08001795 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001796}
1797
Marco Nelissen4110c102012-03-29 09:31:28 -07001798int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001799{
1800 return mSessionId;
1801}
1802
Eric Laurent6f59db12013-07-26 17:16:50 -07001803uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1804{
1805 if (mTrack == 0) return 0;
1806 return mTrack->getSampleRate();
1807}
1808
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001809////////////////////////////////////////////////////////////////////////////////
1810
1811struct CallbackThread : public Thread {
1812 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1813 MediaPlayerBase::AudioSink::AudioCallback cb,
1814 void *cookie);
1815
1816protected:
1817 virtual ~CallbackThread();
1818
1819 virtual bool threadLoop();
1820
1821private:
1822 wp<MediaPlayerBase::AudioSink> mSink;
1823 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1824 void *mCookie;
1825 void *mBuffer;
1826 size_t mBufferSize;
1827
1828 CallbackThread(const CallbackThread &);
1829 CallbackThread &operator=(const CallbackThread &);
1830};
1831
1832CallbackThread::CallbackThread(
1833 const wp<MediaPlayerBase::AudioSink> &sink,
1834 MediaPlayerBase::AudioSink::AudioCallback cb,
1835 void *cookie)
1836 : mSink(sink),
1837 mCallback(cb),
1838 mCookie(cookie),
1839 mBuffer(NULL),
1840 mBufferSize(0) {
1841}
1842
1843CallbackThread::~CallbackThread() {
1844 if (mBuffer) {
1845 free(mBuffer);
1846 mBuffer = NULL;
1847 }
1848}
1849
1850bool CallbackThread::threadLoop() {
1851 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1852 if (sink == NULL) {
1853 return false;
1854 }
1855
1856 if (mBuffer == NULL) {
1857 mBufferSize = sink->bufferSize();
1858 mBuffer = malloc(mBufferSize);
1859 }
1860
1861 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001862 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1863 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001864
1865 if (actualSize > 0) {
1866 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001867 // Could return false on sink->write() error or short count.
1868 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001869 }
1870
1871 return true;
1872}
1873
1874////////////////////////////////////////////////////////////////////////////////
1875
Gloria Wang7cf180c2011-02-19 18:37:57 -08001876void MediaPlayerService::addBatteryData(uint32_t params)
1877{
1878 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001879
1880 int32_t time = systemTime() / 1000000L;
1881
1882 // change audio output devices. This notification comes from AudioFlinger
1883 if ((params & kBatteryDataSpeakerOn)
1884 || (params & kBatteryDataOtherAudioDeviceOn)) {
1885
1886 int deviceOn[NUM_AUDIO_DEVICES];
1887 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1888 deviceOn[i] = 0;
1889 }
1890
1891 if ((params & kBatteryDataSpeakerOn)
1892 && (params & kBatteryDataOtherAudioDeviceOn)) {
1893 deviceOn[SPEAKER_AND_OTHER] = 1;
1894 } else if (params & kBatteryDataSpeakerOn) {
1895 deviceOn[SPEAKER] = 1;
1896 } else {
1897 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1898 }
1899
1900 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1901 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1902
1903 if (mBatteryAudio.refCount > 0) { // if playing audio
1904 if (!deviceOn[i]) {
1905 mBatteryAudio.lastTime[i] += time;
1906 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1907 mBatteryAudio.lastTime[i] = 0;
1908 } else {
1909 mBatteryAudio.lastTime[i] = 0 - time;
1910 }
1911 }
1912
1913 mBatteryAudio.deviceOn[i] = deviceOn[i];
1914 }
1915 }
1916 return;
1917 }
1918
Marco Nelissenb7848f12014-12-04 08:57:56 -08001919 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08001920 if (params & kBatteryDataAudioFlingerStart) {
1921 // record the start time only if currently no other audio
1922 // is being played
1923 if (mBatteryAudio.refCount == 0) {
1924 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1925 if (mBatteryAudio.deviceOn[i]) {
1926 mBatteryAudio.lastTime[i] -= time;
1927 }
1928 }
1929 }
1930
1931 mBatteryAudio.refCount ++;
1932 return;
1933
1934 } else if (params & kBatteryDataAudioFlingerStop) {
1935 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001936 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001937 return;
1938 }
1939
1940 // record the stop time only if currently this is the only
1941 // audio being played
1942 if (mBatteryAudio.refCount == 1) {
1943 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1944 if (mBatteryAudio.deviceOn[i]) {
1945 mBatteryAudio.lastTime[i] += time;
1946 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1947 mBatteryAudio.lastTime[i] = 0;
1948 }
1949 }
1950 }
1951
1952 mBatteryAudio.refCount --;
1953 return;
1954 }
1955
Gloria Wang7cf180c2011-02-19 18:37:57 -08001956 int uid = IPCThreadState::self()->getCallingUid();
1957 if (uid == AID_MEDIA) {
1958 return;
1959 }
1960 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001961
1962 if (index < 0) { // create a new entry for this UID
1963 BatteryUsageInfo info;
1964 info.audioTotalTime = 0;
1965 info.videoTotalTime = 0;
1966 info.audioLastTime = 0;
1967 info.videoLastTime = 0;
1968 info.refCount = 0;
1969
Gloria Wang9ee159b2011-02-24 14:51:45 -08001970 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001971 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001972 return;
1973 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001974 }
1975
1976 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1977
1978 if (params & kBatteryDataCodecStarted) {
1979 if (params & kBatteryDataTrackAudio) {
1980 info.audioLastTime -= time;
1981 info.refCount ++;
1982 }
1983 if (params & kBatteryDataTrackVideo) {
1984 info.videoLastTime -= time;
1985 info.refCount ++;
1986 }
1987 } else {
1988 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001989 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001990 return;
1991 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001992 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001993 mBatteryData.removeItem(uid);
1994 return;
1995 }
1996
1997 if (params & kBatteryDataTrackAudio) {
1998 info.audioLastTime += time;
1999 info.refCount --;
2000 }
2001 if (params & kBatteryDataTrackVideo) {
2002 info.videoLastTime += time;
2003 info.refCount --;
2004 }
2005
2006 // no stream is being played by this UID
2007 if (info.refCount == 0) {
2008 info.audioTotalTime += info.audioLastTime;
2009 info.audioLastTime = 0;
2010 info.videoTotalTime += info.videoLastTime;
2011 info.videoLastTime = 0;
2012 }
2013 }
2014}
2015
2016status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2017 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002018
2019 // audio output devices usage
2020 int32_t time = systemTime() / 1000000L; //in ms
2021 int32_t totalTime;
2022
2023 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2024 totalTime = mBatteryAudio.totalTime[i];
2025
2026 if (mBatteryAudio.deviceOn[i]
2027 && (mBatteryAudio.lastTime[i] != 0)) {
2028 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2029 totalTime += tmpTime;
2030 }
2031
2032 reply->writeInt32(totalTime);
2033 // reset the total time
2034 mBatteryAudio.totalTime[i] = 0;
2035 }
2036
2037 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002038 BatteryUsageInfo info;
2039 int size = mBatteryData.size();
2040
2041 reply->writeInt32(size);
2042 int i = 0;
2043
2044 while (i < size) {
2045 info = mBatteryData.valueAt(i);
2046
2047 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2048 reply->writeInt32(info.audioTotalTime);
2049 reply->writeInt32(info.videoTotalTime);
2050
2051 info.audioTotalTime = 0;
2052 info.videoTotalTime = 0;
2053
2054 // remove the UID entry where no stream is being played
2055 if (info.refCount <= 0) {
2056 mBatteryData.removeItemsAt(i);
2057 size --;
2058 i --;
2059 }
2060 i++;
2061 }
2062 return NO_ERROR;
2063}
nikoa64c8c72009-07-20 15:07:26 -07002064} // namespace android