blob: 694f1a4185d9b73ac9bb352f11409c8b4058c27d [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>
Marco Nelissenf09611f2015-02-13 14:12:42 -080062#include <media/stagefright/foundation/ALooperRoster.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063
Dima Zavin64760242011-05-11 14:15:23 -070064#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070065
Gloria Wang7cf180c2011-02-19 18:37:57 -080066#include <private/android_filesystem_config.h>
67
James Dong559bf282012-03-28 10:29:14 -070068#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069#include "MediaRecorderClient.h"
70#include "MediaPlayerService.h"
71#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070072#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073
Nicolas Catania14d27472009-07-13 14:37:49 -070074#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070075#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080076#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070077
Andreas Huber20111aa2009-07-14 16:56:47 -070078#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070079
Andreas Hubered3e3e02012-03-26 11:13:27 -070080#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080081#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070082#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070083#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070084#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070085
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070086namespace {
nikoa64c8c72009-07-20 15:07:26 -070087using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088using android::status_t;
89using android::OK;
90using android::BAD_VALUE;
91using android::NOT_ENOUGH_DATA;
92using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070093
94// Max number of entries in the filter.
95const int kMaxFilterSize = 64; // I pulled that out of thin air.
96
nikoa64c8c72009-07-20 15:07:26 -070097// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070098
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070099
100// Unmarshall a filter from a Parcel.
101// Filter format in a parcel:
102//
103// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | number of entries (n) |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type 1 |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// | metadata type 2 |
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111// ....
112// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113// | metadata type n |
114// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115//
116// @param p Parcel that should start with a filter.
117// @param[out] filter On exit contains the list of metadata type to be
118// filtered.
119// @param[out] status On exit contains the status code to be returned.
120// @return true if the parcel starts with a valid filter.
121bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700122 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 status_t *status)
124{
Nicolas Catania48290382009-07-10 13:53:06 -0700125 int32_t val;
126 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 {
Steve Block29357bc2012-01-06 19:20:56 +0000128 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700129 *status = NOT_ENOUGH_DATA;
130 return false;
131 }
132
Nicolas Catania48290382009-07-10 13:53:06 -0700133 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134 {
Steve Block29357bc2012-01-06 19:20:56 +0000135 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136 *status = BAD_VALUE;
137 return false;
138 }
139
Nicolas Catania48290382009-07-10 13:53:06 -0700140 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141
142 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700143 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
nikoa64c8c72009-07-20 15:07:26 -0700145 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146
Nicolas Catania48290382009-07-10 13:53:06 -0700147
148 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 {
Steve Block29357bc2012-01-06 19:20:56 +0000150 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 *status = NOT_ENOUGH_DATA;
152 return false;
153 }
154
nikoa64c8c72009-07-20 15:07:26 -0700155 const Metadata::Type *data =
156 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157
Nicolas Catania48290382009-07-10 13:53:06 -0700158 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700159 {
Steve Block29357bc2012-01-06 19:20:56 +0000160 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 *status = BAD_VALUE;
162 return false;
163 }
164
165 // TODO: The stl impl of vector would be more efficient here
166 // because it degenerates into a memcpy on pod types. Try to
167 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700168 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169 {
Nicolas Catania48290382009-07-10 13:53:06 -0700170 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700171 ++data;
172 }
173 *status = OK;
174 return true;
175}
176
Nicolas Catania48290382009-07-10 13:53:06 -0700177// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178// @param val To be searched.
179// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700180bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181{
182 // Deal with empty and ANY right away
183 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700184 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700185
Nicolas Catania48290382009-07-10 13:53:06 -0700186 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700187}
188
189} // anonymous namespace
190
191
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700192namespace {
193using android::Parcel;
194using android::String16;
195
196// marshalling tag indicating flattened utf16 tags
197// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
198const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
199
200// Audio attributes format in a parcel:
201//
202// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
203// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204// | usage |
205// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206// | content_type |
207// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900208// | source |
209// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700210// | flags |
211// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
213// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214// | flattened tags in UTF16 |
215// | ... |
216// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217//
218// @param p Parcel that contains audio attributes.
219// @param[out] attributes On exit points to an initialized audio_attributes_t structure
220// @param[out] status On exit contains the status code to be returned.
221void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
222{
223 attributes->usage = (audio_usage_t) parcel.readInt32();
224 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900225 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700226 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
227 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
228 if (hasFlattenedTag) {
229 // the tags are UTF16, convert to UTF8
230 String16 tags = parcel.readString16();
231 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
232 if (realTagSize <= 0) {
233 strcpy(attributes->tags, "");
234 } else {
235 // copy the flattened string into the attributes as the destination for the conversion:
236 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
237 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
238 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
239 utf16_to_utf8(tags.string(), tagSize, attributes->tags);
240 }
241 } else {
242 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
243 strcpy(attributes->tags, "");
244 }
245}
246} // anonymous namespace
247
248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249namespace android {
250
Marco Nelissenf09611f2015-02-13 14:12:42 -0800251extern ALooperRoster gLooperRoster;
252
253
Dave Burked681bbb2011-08-30 14:39:17 +0100254static bool checkPermission(const char* permissionString) {
255#ifndef HAVE_ANDROID_OS
256 return true;
257#endif
258 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
259 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000260 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100261 return ok;
262}
263
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264// 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 -0800265/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
266/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
267
268void MediaPlayerService::instantiate() {
269 defaultServiceManager()->addService(
270 String16("media.player"), new MediaPlayerService());
271}
272
273MediaPlayerService::MediaPlayerService()
274{
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800277
278 mBatteryAudio.refCount = 0;
279 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
280 mBatteryAudio.deviceOn[i] = 0;
281 mBatteryAudio.lastTime[i] = 0;
282 mBatteryAudio.totalTime[i] = 0;
283 }
284 // speaker is on by default
285 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700286
Chong Zhang0b30fd42014-07-23 14:46:05 -0700287 // reset battery stats
288 // if the mediaserver has crashed, battery stats could be left
289 // in bad state, reset the state upon service start.
290 const sp<IServiceManager> sm(defaultServiceManager());
291 if (sm != NULL) {
292 const String16 name("batterystats");
293 sp<IBatteryStats> batteryStats =
294 interface_cast<IBatteryStats>(sm->getService(name));
295 if (batteryStats != NULL) {
296 batteryStats->noteResetVideo();
297 batteryStats->noteResetAudio();
298 }
299 }
300
John Grossman44a7e422012-06-21 17:29:24 -0700301 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302}
303
304MediaPlayerService::~MediaPlayerService()
305{
Steve Block3856b092011-10-20 11:56:00 +0100306 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307}
308
Glenn Kastenf37971f2012-02-03 11:06:53 -0800309sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800311 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700312 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
313 wp<MediaRecorderClient> w = recorder;
314 Mutex::Autolock lock(mLock);
315 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100316 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 return recorder;
318}
319
Gloria Wangdac6a312009-10-29 15:46:37 -0700320void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
321{
322 Mutex::Autolock lock(mLock);
323 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700325}
326
Glenn Kastenf37971f2012-02-03 11:06:53 -0800327sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800329 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100331 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 return retriever;
333}
334
Glenn Kastenf37971f2012-02-03 11:06:53 -0800335sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100336 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800338 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700340
341 sp<Client> c = new Client(
342 this, pid, connId, client, audioSessionId,
343 IPCThreadState::self()->getCallingUid());
344
Steve Block3856b092011-10-20 11:56:00 +0100345 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100346 IPCThreadState::self()->getCallingUid());
347
348 wp<Client> w = c;
349 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350 Mutex::Autolock lock(mLock);
351 mClients.add(w);
352 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800353 return c;
354}
355
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700356sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
357 return MediaCodecList::getLocalInstance();
358}
359
Andreas Huber318ad9c2009-10-15 13:46:54 -0700360sp<IOMX> MediaPlayerService::getOMX() {
361 Mutex::Autolock autoLock(mLock);
362
363 if (mOMX.get() == NULL) {
364 mOMX = new OMX;
365 }
366
367 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700368}
369
Andreas Hubered3e3e02012-03-26 11:13:27 -0700370sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700371 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700372}
373
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800374sp<IDrm> MediaPlayerService::makeDrm() {
375 return new Drm;
376}
377
Andreas Huber279dcd82013-01-30 10:41:25 -0800378sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
379 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700380}
381
Jeff Brown2013a542012-09-04 21:38:42 -0700382sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
383 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700384 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
385 return NULL;
386 }
387
Jeff Brownced24b32012-09-05 17:48:03 -0700388 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700389}
390
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
392{
393 const size_t SIZE = 256;
394 char buffer[SIZE];
395 String8 result;
396
397 result.append(" AudioOutput\n");
398 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
399 mStreamType, mLeftVolume, mRightVolume);
400 result.append(buffer);
401 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800402 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800403 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700404 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
405 mAuxEffectId, mSendLevel);
406 result.append(buffer);
407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 ::write(fd, result.string(), result.size());
409 if (mTrack != 0) {
410 mTrack->dump(fd, args);
411 }
412 return NO_ERROR;
413}
414
415status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
416{
417 const size_t SIZE = 256;
418 char buffer[SIZE];
419 String8 result;
420 result.append(" Client\n");
421 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
422 mPid, mConnId, mStatus, mLoop?"true": "false");
423 result.append(buffer);
424 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700425 if (mPlayer != NULL) {
426 mPlayer->dump(fd, args);
427 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 if (mAudioOutput != 0) {
429 mAudioOutput->dump(fd, args);
430 }
431 write(fd, "\n", 1);
432 return NO_ERROR;
433}
434
Marco Nelissenf09611f2015-02-13 14:12:42 -0800435/**
436 * The only arguments this understands right now are -c, -von and -voff,
437 * which are parsed by ALooperRoster::dump()
438 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
440{
441 const size_t SIZE = 256;
442 char buffer[SIZE];
443 String8 result;
444 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
445 snprintf(buffer, SIZE, "Permission Denial: "
446 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
447 IPCThreadState::self()->getCallingPid(),
448 IPCThreadState::self()->getCallingUid());
449 result.append(buffer);
450 } else {
451 Mutex::Autolock lock(mLock);
452 for (int i = 0, n = mClients.size(); i < n; ++i) {
453 sp<Client> c = mClients[i].promote();
454 if (c != 0) c->dump(fd, args);
455 }
James 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);
467 }
James Dongb9141222010-07-08 11:16:11 -0700468 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700469 }
470
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800472 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 FILE *f = fopen(buffer, "r");
474 if (f) {
475 while (!feof(f)) {
476 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700477 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700479 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 strstr(buffer, " /system/media/")) {
481 result.append(" ");
482 result.append(buffer);
483 }
484 }
485 fclose(f);
486 } else {
487 result.append("couldn't open ");
488 result.append(buffer);
489 result.append("\n");
490 }
491
Marco Nelissenf09611f2015-02-13 14:12:42 -0800492 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 DIR *d = opendir(buffer);
494 if (d) {
495 struct dirent *ent;
496 while((ent = readdir(d)) != NULL) {
497 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800498 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499 struct stat s;
500 if (lstat(buffer, &s) == 0) {
501 if ((s.st_mode & S_IFMT) == S_IFLNK) {
502 char linkto[256];
503 int len = readlink(buffer, linkto, sizeof(linkto));
504 if(len > 0) {
505 if(len > 255) {
506 linkto[252] = '.';
507 linkto[253] = '.';
508 linkto[254] = '.';
509 linkto[255] = 0;
510 } else {
511 linkto[len] = 0;
512 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700513 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700515 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516 strstr(linkto, "/system/media/") == linkto) {
517 result.append(" ");
518 result.append(buffer);
519 result.append(" -> ");
520 result.append(linkto);
521 result.append("\n");
522 }
523 }
524 } else {
525 result.append(" unexpected type for ");
526 result.append(buffer);
527 result.append("\n");
528 }
529 }
530 }
531 }
532 closedir(d);
533 } else {
534 result.append("couldn't open ");
535 result.append(buffer);
536 result.append("\n");
537 }
538
Marco Nelissenf09611f2015-02-13 14:12:42 -0800539 gLooperRoster.dump(fd, args);
540
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541 bool dumpMem = false;
542 for (size_t i = 0; i < args.size(); i++) {
543 if (args[i] == String16("-m")) {
544 dumpMem = true;
545 }
546 }
547 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700548 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 }
551 write(fd, result.string(), result.size());
552 return NO_ERROR;
553}
554
555void MediaPlayerService::removeClient(wp<Client> client)
556{
557 Mutex::Autolock lock(mLock);
558 mClients.remove(client);
559}
560
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700561MediaPlayerService::Client::Client(
562 const sp<MediaPlayerService>& service, pid_t pid,
563 int32_t connId, const sp<IMediaPlayerClient>& client,
564 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565{
Steve Block3856b092011-10-20 11:56:00 +0100566 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567 mPid = pid;
568 mConnId = connId;
569 mService = service;
570 mClient = client;
571 mLoop = false;
572 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700573 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700574 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800575 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700576 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700577
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000579 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800580 mAntagonizer = new Antagonizer(notify, this);
581#endif
582}
583
584MediaPlayerService::Client::~Client()
585{
Steve Block3856b092011-10-20 11:56:00 +0100586 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 mAudioOutput.clear();
588 wp<Client> client(this);
589 disconnect();
590 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700591 if (mAudioAttributes != NULL) {
592 free(mAudioAttributes);
593 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800594}
595
596void MediaPlayerService::Client::disconnect()
597{
Steve Block3856b092011-10-20 11:56:00 +0100598 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 // grab local reference and clear main reference to prevent future
600 // access to object
601 sp<MediaPlayerBase> p;
602 {
603 Mutex::Autolock l(mLock);
604 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800605 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700607
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608 mPlayer.clear();
609
610 // clear the notification to prevent callbacks to dead client
611 // and reset the player. We assume the player will serialize
612 // access to itself if necessary.
613 if (p != 0) {
614 p->setNotifyCallback(0, 0);
615#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000616 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617 mAntagonizer->kill();
618#endif
619 p->reset();
620 }
621
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700622 disconnectNativeWindow();
623
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624 IPCThreadState::self()->flushCommands();
625}
626
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
628{
629 // determine if we have the right player type
630 sp<MediaPlayerBase> p = mPlayer;
631 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100632 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633 p.clear();
634 }
635 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700636 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700638
Jason Simmonsdb29e522011-08-12 13:46:55 -0700639 if (p != NULL) {
640 p->setUID(mUID);
641 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700642
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643 return p;
644}
645
John Grossmanc795b642012-02-22 15:38:35 -0800646sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
647 player_type playerType)
648{
649 ALOGV("player type = %d", playerType);
650
651 // create the right type of player
652 sp<MediaPlayerBase> p = createPlayer(playerType);
653 if (p == NULL) {
654 return p;
655 }
656
657 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800658 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700659 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800660 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
661 }
662
663 return p;
664}
665
666void MediaPlayerService::Client::setDataSource_post(
667 const sp<MediaPlayerBase>& p,
668 status_t status)
669{
670 ALOGV(" setDataSource");
671 mStatus = status;
672 if (mStatus != OK) {
673 ALOGE(" error: %d", mStatus);
674 return;
675 }
676
677 // Set the re-transmission endpoint if one was chosen.
678 if (mRetransmitEndpointValid) {
679 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
680 if (mStatus != NO_ERROR) {
681 ALOGE("setRetransmitEndpoint error: %d", mStatus);
682 }
683 }
684
685 if (mStatus == OK) {
686 mPlayer = p;
687 }
688}
689
Andreas Huber2db84552010-01-28 11:19:57 -0800690status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800691 const sp<IMediaHTTPService> &httpService,
692 const char *url,
693 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694{
Steve Block3856b092011-10-20 11:56:00 +0100695 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696 if (url == NULL)
697 return UNKNOWN_ERROR;
698
Dave Burked681bbb2011-08-30 14:39:17 +0100699 if ((strncmp(url, "http://", 7) == 0) ||
700 (strncmp(url, "https://", 8) == 0) ||
701 (strncmp(url, "rtsp://", 7) == 0)) {
702 if (!checkPermission("android.permission.INTERNET")) {
703 return PERMISSION_DENIED;
704 }
705 }
706
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 if (strncmp(url, "content://", 10) == 0) {
708 // get a filedescriptor for the content Uri and
709 // pass it to the setDataSource(fd) method
710
711 String16 url16(url);
712 int fd = android::openContentProviderFile(url16);
713 if (fd < 0)
714 {
Steve Block29357bc2012-01-06 19:20:56 +0000715 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716 return UNKNOWN_ERROR;
717 }
718 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
719 close(fd);
720 return mStatus;
721 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700722 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800723 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
724 if (p == NULL) {
725 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726 }
727
Andreas Huber1b86fe02014-01-29 11:13:26 -0800728 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729 return mStatus;
730 }
731}
732
733status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
734{
Steve Block3856b092011-10-20 11:56:00 +0100735 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800736 struct stat sb;
737 int ret = fstat(fd, &sb);
738 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000739 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800740 return UNKNOWN_ERROR;
741 }
742
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("st_dev = %llu", sb.st_dev);
744 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700745 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
746 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100747 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800748
749 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000750 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751 ::close(fd);
752 return UNKNOWN_ERROR;
753 }
754 if (offset + length > sb.st_size) {
755 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100756 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800757 }
758
John Grossman44a7e422012-06-21 17:29:24 -0700759 player_type playerType = MediaPlayerFactory::getPlayerType(this,
760 fd,
761 offset,
762 length);
John Grossmanc795b642012-02-22 15:38:35 -0800763 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
764 if (p == NULL) {
765 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800766 }
767
768 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800769 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800770 return mStatus;
771}
772
Andreas Hubere2b10282010-11-23 11:41:34 -0800773status_t MediaPlayerService::Client::setDataSource(
774 const sp<IStreamSource> &source) {
775 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700776 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800777 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800778 if (p == NULL) {
779 return NO_INIT;
780 }
781
Andreas Hubere2b10282010-11-23 11:41:34 -0800782 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800783 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800784 return mStatus;
785}
786
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700787void MediaPlayerService::Client::disconnectNativeWindow() {
788 if (mConnectedWindow != NULL) {
789 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
790 NATIVE_WINDOW_API_MEDIA);
791
792 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000793 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700794 strerror(-err), err);
795 }
796 }
797 mConnectedWindow.clear();
798}
799
Glenn Kasten11731182011-02-08 17:26:17 -0800800status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800801 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800802{
Andy McFadden484566c2012-12-18 09:46:54 -0800803 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800804 sp<MediaPlayerBase> p = getPlayer();
805 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700806
Marco Nelissenf8880202014-11-14 07:58:25 -0800807 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700808 if (mConnectedWindowBinder == binder) {
809 return OK;
810 }
811
812 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800813 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700814 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700815 status_t err = native_window_api_connect(anw.get(),
816 NATIVE_WINDOW_API_MEDIA);
817
818 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000819 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700820 // Note that we must do the reset before disconnecting from the ANW.
821 // Otherwise queue/dequeue calls could be made on the disconnected
822 // ANW, which may result in errors.
823 reset();
824
825 disconnectNativeWindow();
826
827 return err;
828 }
829 }
830
Andy McFadden484566c2012-12-18 09:46:54 -0800831 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700832 // disconnecting the old one. Otherwise queue/dequeue calls could be made
833 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800834 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700835
836 disconnectNativeWindow();
837
838 mConnectedWindow = anw;
839
840 if (err == OK) {
841 mConnectedWindowBinder = binder;
842 } else {
843 disconnectNativeWindow();
844 }
845
846 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800847}
848
Nicolas Catania1d187f12009-05-12 23:25:55 -0700849status_t MediaPlayerService::Client::invoke(const Parcel& request,
850 Parcel *reply)
851{
852 sp<MediaPlayerBase> p = getPlayer();
853 if (p == NULL) return UNKNOWN_ERROR;
854 return p->invoke(request, reply);
855}
856
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700857// This call doesn't need to access the native player.
858status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
859{
860 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700861 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700862
Nicolas Catania48290382009-07-10 13:53:06 -0700863 if (unmarshallFilter(filter, &allow, &status) &&
864 unmarshallFilter(filter, &drop, &status)) {
865 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700866
867 mMetadataAllow = allow;
868 mMetadataDrop = drop;
869 }
870 return status;
871}
872
Nicolas Catania48290382009-07-10 13:53:06 -0700873status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700874 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700875{
nikoa64c8c72009-07-20 15:07:26 -0700876 sp<MediaPlayerBase> player = getPlayer();
877 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700878
nikod608a812009-07-16 16:39:53 -0700879 status_t status;
880 // Placeholder for the return code, updated by the caller.
881 reply->writeInt32(-1);
882
nikoa64c8c72009-07-20 15:07:26 -0700883 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700884
885 // We don't block notifications while we fetch the data. We clear
886 // mMetadataUpdated first so we don't lose notifications happening
887 // during the rest of this call.
888 {
889 Mutex::Autolock lock(mLock);
890 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700891 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700892 }
893 mMetadataUpdated.clear();
894 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700895
nikoa64c8c72009-07-20 15:07:26 -0700896 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700897
nikoa64c8c72009-07-20 15:07:26 -0700898 metadata.appendHeader();
899 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700900
901 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700902 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000903 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700904 return status;
905 }
906
907 // FIXME: Implement filtering on the result. Not critical since
908 // filtering takes place on the update notifications already. This
909 // would be when all the metadata are fetch and a filter is set.
910
nikod608a812009-07-16 16:39:53 -0700911 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700912 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700913 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700914}
915
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800916status_t MediaPlayerService::Client::prepareAsync()
917{
Steve Block3856b092011-10-20 11:56:00 +0100918 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800919 sp<MediaPlayerBase> p = getPlayer();
920 if (p == 0) return UNKNOWN_ERROR;
921 status_t ret = p->prepareAsync();
922#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000923 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800924 if (ret == NO_ERROR) mAntagonizer->start();
925#endif
926 return ret;
927}
928
929status_t MediaPlayerService::Client::start()
930{
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932 sp<MediaPlayerBase> p = getPlayer();
933 if (p == 0) return UNKNOWN_ERROR;
934 p->setLooping(mLoop);
935 return p->start();
936}
937
938status_t MediaPlayerService::Client::stop()
939{
Steve Block3856b092011-10-20 11:56:00 +0100940 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800941 sp<MediaPlayerBase> p = getPlayer();
942 if (p == 0) return UNKNOWN_ERROR;
943 return p->stop();
944}
945
946status_t MediaPlayerService::Client::pause()
947{
Steve Block3856b092011-10-20 11:56:00 +0100948 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800949 sp<MediaPlayerBase> p = getPlayer();
950 if (p == 0) return UNKNOWN_ERROR;
951 return p->pause();
952}
953
954status_t MediaPlayerService::Client::isPlaying(bool* state)
955{
956 *state = false;
957 sp<MediaPlayerBase> p = getPlayer();
958 if (p == 0) return UNKNOWN_ERROR;
959 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800961 return NO_ERROR;
962}
963
964status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
965{
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 sp<MediaPlayerBase> p = getPlayer();
968 if (p == 0) return UNKNOWN_ERROR;
969 status_t ret = p->getCurrentPosition(msec);
970 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000973 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 }
975 return ret;
976}
977
978status_t MediaPlayerService::Client::getDuration(int *msec)
979{
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981 sp<MediaPlayerBase> p = getPlayer();
982 if (p == 0) return UNKNOWN_ERROR;
983 status_t ret = p->getDuration(msec);
984 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000987 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 }
989 return ret;
990}
991
Marco Nelissen6b74d672012-02-28 16:07:44 -0800992status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
993 ALOGV("setNextPlayer");
994 Mutex::Autolock l(mLock);
995 sp<Client> c = static_cast<Client*>(player.get());
996 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700997
998 if (c != NULL) {
999 if (mAudioOutput != NULL) {
1000 mAudioOutput->setNextOutput(c->mAudioOutput);
1001 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1002 ALOGE("no current audio output");
1003 }
1004
1005 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1006 mPlayer->setNextPlayer(mNextClient->getPlayer());
1007 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001008 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001009
Marco Nelissen6b74d672012-02-28 16:07:44 -08001010 return OK;
1011}
1012
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013status_t MediaPlayerService::Client::seekTo(int msec)
1014{
Steve Block3856b092011-10-20 11:56:00 +01001015 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001016 sp<MediaPlayerBase> p = getPlayer();
1017 if (p == 0) return UNKNOWN_ERROR;
1018 return p->seekTo(msec);
1019}
1020
1021status_t MediaPlayerService::Client::reset()
1022{
Steve Block3856b092011-10-20 11:56:00 +01001023 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001024 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001025 sp<MediaPlayerBase> p = getPlayer();
1026 if (p == 0) return UNKNOWN_ERROR;
1027 return p->reset();
1028}
1029
Glenn Kastenfff6d712012-01-12 16:38:12 -08001030status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001031{
Steve Block3856b092011-10-20 11:56:00 +01001032 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 // TODO: for hardware output, call player instead
1034 Mutex::Autolock l(mLock);
1035 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1036 return NO_ERROR;
1037}
1038
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001039status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1040{
1041 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1042 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1043 unmarshallAudioAttributes(parcel, mAudioAttributes);
1044
1045 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1046 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1047 mAudioAttributes->tags);
1048
1049 if (mAudioOutput != 0) {
1050 mAudioOutput->setAudioAttributes(mAudioAttributes);
1051 }
1052 return NO_ERROR;
1053}
1054
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001055status_t MediaPlayerService::Client::setLooping(int loop)
1056{
Steve Block3856b092011-10-20 11:56:00 +01001057 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001058 mLoop = loop;
1059 sp<MediaPlayerBase> p = getPlayer();
1060 if (p != 0) return p->setLooping(loop);
1061 return NO_ERROR;
1062}
1063
1064status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1065{
Steve Block3856b092011-10-20 11:56:00 +01001066 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001067
1068 // for hardware output, call player instead
1069 sp<MediaPlayerBase> p = getPlayer();
1070 {
1071 Mutex::Autolock l(mLock);
1072 if (p != 0 && p->hardwareOutput()) {
1073 MediaPlayerHWInterface* hwp =
1074 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1075 return hwp->setVolume(leftVolume, rightVolume);
1076 } else {
1077 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1078 return NO_ERROR;
1079 }
1080 }
1081
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001082 return NO_ERROR;
1083}
1084
Eric Laurent2beeb502010-07-16 07:43:46 -07001085status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1086{
Steve Block3856b092011-10-20 11:56:00 +01001087 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001088 Mutex::Autolock l(mLock);
1089 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1090 return NO_ERROR;
1091}
1092
1093status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1094{
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001096 Mutex::Autolock l(mLock);
1097 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1098 return NO_ERROR;
1099}
Nicolas Catania48290382009-07-10 13:53:06 -07001100
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001101status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001102 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001103 switch (key) {
1104 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1105 {
1106 Mutex::Autolock l(mLock);
1107 return setAudioAttributes_l(request);
1108 }
1109 default:
1110 sp<MediaPlayerBase> p = getPlayer();
1111 if (p == 0) { return UNKNOWN_ERROR; }
1112 return p->setParameter(key, request);
1113 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001114}
1115
1116status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001118 sp<MediaPlayerBase> p = getPlayer();
1119 if (p == 0) return UNKNOWN_ERROR;
1120 return p->getParameter(key, reply);
1121}
1122
John Grossmanc795b642012-02-22 15:38:35 -08001123status_t MediaPlayerService::Client::setRetransmitEndpoint(
1124 const struct sockaddr_in* endpoint) {
1125
1126 if (NULL != endpoint) {
1127 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1128 uint16_t p = ntohs(endpoint->sin_port);
1129 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1130 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1131 } else {
1132 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1133 }
1134
1135 sp<MediaPlayerBase> p = getPlayer();
1136
1137 // Right now, the only valid time to set a retransmit endpoint is before
1138 // player selection has been made (since the presence or absence of a
1139 // retransmit endpoint is going to determine which player is selected during
1140 // setDataSource).
1141 if (p != 0) return INVALID_OPERATION;
1142
1143 if (NULL != endpoint) {
1144 mRetransmitEndpoint = *endpoint;
1145 mRetransmitEndpointValid = true;
1146 } else {
1147 mRetransmitEndpointValid = false;
1148 }
1149
1150 return NO_ERROR;
1151}
1152
John Grossman44a7e422012-06-21 17:29:24 -07001153status_t MediaPlayerService::Client::getRetransmitEndpoint(
1154 struct sockaddr_in* endpoint)
1155{
1156 if (NULL == endpoint)
1157 return BAD_VALUE;
1158
1159 sp<MediaPlayerBase> p = getPlayer();
1160
1161 if (p != NULL)
1162 return p->getRetransmitEndpoint(endpoint);
1163
1164 if (!mRetransmitEndpointValid)
1165 return NO_INIT;
1166
1167 *endpoint = mRetransmitEndpoint;
1168
1169 return NO_ERROR;
1170}
1171
Gloria Wangb483c472011-04-11 17:23:27 -07001172void MediaPlayerService::Client::notify(
1173 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001174{
1175 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001176 if (client == NULL) {
1177 return;
1178 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001179
James Dongb8a98252012-08-26 16:13:03 -07001180 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001181 {
1182 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001183 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001184 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001185 if (client->mAudioOutput != NULL)
1186 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001187 client->mNextClient->start();
1188 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1189 }
1190 }
1191
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001192 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001193 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001194 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001195
1196 if(client->shouldDropMetadata(metadata_type)) {
1197 return;
1198 }
1199
1200 // Update the list of metadata that have changed. getMetadata
1201 // also access mMetadataUpdated and clears it.
1202 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001203 }
James Dongb8a98252012-08-26 16:13:03 -07001204
1205 if (c != NULL) {
1206 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1207 c->notify(msg, ext1, ext2, obj);
1208 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209}
1210
Nicolas Catania48290382009-07-10 13:53:06 -07001211
nikoa64c8c72009-07-20 15:07:26 -07001212bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001213{
Nicolas Catania48290382009-07-10 13:53:06 -07001214 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001215
Nicolas Catania48290382009-07-10 13:53:06 -07001216 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001217 return true;
1218 }
1219
Nicolas Catania48290382009-07-10 13:53:06 -07001220 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001221 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001222 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001223 return true;
1224 }
1225}
1226
Nicolas Catania48290382009-07-10 13:53:06 -07001227
nikoa64c8c72009-07-20 15:07:26 -07001228void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001229 Mutex::Autolock lock(mLock);
1230 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1231 mMetadataUpdated.add(metadata_type);
1232 }
1233}
1234
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235#if CALLBACK_ANTAGONIZER
1236const int Antagonizer::interval = 10000; // 10 msecs
1237
1238Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1239 mExit(false), mActive(false), mClient(client), mCb(cb)
1240{
1241 createThread(callbackThread, this);
1242}
1243
1244void Antagonizer::kill()
1245{
1246 Mutex::Autolock _l(mLock);
1247 mActive = false;
1248 mExit = true;
1249 mCondition.wait(mLock);
1250}
1251
1252int Antagonizer::callbackThread(void* user)
1253{
Steve Blockb8a80522011-12-20 16:23:08 +00001254 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1256 while (!p->mExit) {
1257 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001258 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 p->mCb(p->mClient, 0, 0, 0);
1260 }
1261 usleep(interval);
1262 }
1263 Mutex::Autolock _l(p->mLock);
1264 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001265 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 return 0;
1267}
1268#endif
1269
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270#undef LOG_TAG
1271#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001272MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1273 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001274 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001275 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001276 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001277 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001278 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001279 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001280 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001281 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001282 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001283 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 mLeftVolume = 1.0;
1285 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001286 mPlaybackRatePermille = 1000;
1287 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001289 mAuxEffectId = 0;
1290 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001292 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293}
1294
1295MediaPlayerService::AudioOutput::~AudioOutput()
1296{
1297 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001298 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299}
1300
1301void MediaPlayerService::AudioOutput::setMinBufferCount()
1302{
1303 char value[PROPERTY_VALUE_MAX];
1304 if (property_get("ro.kernel.qemu", value, 0)) {
1305 mIsOnEmulator = true;
1306 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1307 }
1308}
1309
1310bool MediaPlayerService::AudioOutput::isOnEmulator()
1311{
1312 setMinBufferCount();
1313 return mIsOnEmulator;
1314}
1315
1316int MediaPlayerService::AudioOutput::getMinBufferCount()
1317{
1318 setMinBufferCount();
1319 return mMinBufferCount;
1320}
1321
1322ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1323{
1324 if (mTrack == 0) return NO_INIT;
1325 return mTrack->frameCount() * frameSize();
1326}
1327
1328ssize_t MediaPlayerService::AudioOutput::frameCount() const
1329{
1330 if (mTrack == 0) return NO_INIT;
1331 return mTrack->frameCount();
1332}
1333
1334ssize_t MediaPlayerService::AudioOutput::channelCount() const
1335{
1336 if (mTrack == 0) return NO_INIT;
1337 return mTrack->channelCount();
1338}
1339
1340ssize_t MediaPlayerService::AudioOutput::frameSize() const
1341{
1342 if (mTrack == 0) return NO_INIT;
1343 return mTrack->frameSize();
1344}
1345
1346uint32_t MediaPlayerService::AudioOutput::latency () const
1347{
Eric Laurentdb354e52012-03-05 17:27:11 -08001348 if (mTrack == 0) return 0;
1349 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001350}
1351
1352float MediaPlayerService::AudioOutput::msecsPerFrame() const
1353{
1354 return mMsecsPerFrame;
1355}
1356
Marco Nelissen4110c102012-03-29 09:31:28 -07001357status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001358{
1359 if (mTrack == 0) return NO_INIT;
1360 return mTrack->getPosition(position);
1361}
1362
Lajos Molnar06ad1522014-08-28 07:27:44 -07001363status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1364{
1365 if (mTrack == 0) return NO_INIT;
1366 return mTrack->getTimestamp(ts);
1367}
1368
Marco Nelissen4110c102012-03-29 09:31:28 -07001369status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1370{
1371 if (mTrack == 0) return NO_INIT;
1372 *frameswritten = mBytesWritten / frameSize();
1373 return OK;
1374}
1375
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001376status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1377{
1378 if (mTrack == 0) return NO_INIT;
1379 return mTrack->setParameters(keyValuePairs);
1380}
1381
1382String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1383{
1384 if (mTrack == 0) return String8::empty();
1385 return mTrack->getParameters(keys);
1386}
1387
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001388void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1389 mAttributes = attributes;
1390}
1391
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001392void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1393{
1394 ALOGV("deleteRecycledTrack");
1395
1396 if (mRecycledTrack != 0) {
1397
1398 if (mCallbackData != NULL) {
1399 mCallbackData->setOutput(NULL);
1400 mCallbackData->endTrackSwitch();
1401 }
1402
1403 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1404 mRecycledTrack->flush();
1405 }
1406 // An offloaded track isn't flushed because the STREAM_END is reported
1407 // slightly prematurely to allow time for the gapless track switch
1408 // but this means that if we decide not to recycle the track there
1409 // could be a small amount of residual data still playing. We leave
1410 // AudioFlinger to drain the track.
1411
1412 mRecycledTrack.clear();
1413 delete mCallbackData;
1414 mCallbackData = NULL;
1415 close();
1416 }
1417}
1418
Andreas Huber20111aa2009-07-14 16:56:47 -07001419status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001420 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1421 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001422 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001423 audio_output_flags_t flags,
1424 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425{
Andreas Huber20111aa2009-07-14 16:56:47 -07001426 mCallback = cb;
1427 mCallbackCookie = cookie;
1428
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429 // Check argument "bufferCount" against the mininum buffer count
1430 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001431 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432 bufferCount = mMinBufferCount;
1433
1434 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001435 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1436 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001437 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001438 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001439 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001440
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001441 // offloading is only supported in callback mode for now.
1442 // offloadInfo must be present if offload flag is set
1443 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1444 ((cb == NULL) || (offloadInfo == NULL))) {
1445 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001446 }
1447
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001448 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1449 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1450 } else {
1451 uint32_t afSampleRate;
1452 size_t afFrameCount;
1453
1454 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1455 return NO_INIT;
1456 }
1457 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1458 return NO_INIT;
1459 }
1460
1461 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1462 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001463
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001464 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001465 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001466 if (0 == channelMask) {
1467 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1468 return NO_INIT;
1469 }
1470 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001471
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001472 // Check whether we can recycle the track
1473 bool reuse = false;
1474 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001475
Glenn Kasten2799d742013-05-30 14:33:29 -07001476 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001477 // check whether we are switching between two offloaded tracks
1478 bothOffloaded = (flags & mRecycledTrack->getFlags()
1479 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001480
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001481 // check if the existing track can be reused as-is, or if a new track needs to be created.
1482 reuse = true;
1483
Marco Nelissen67295b52012-06-11 14:52:53 -07001484 if ((mCallbackData == NULL && mCallback != NULL) ||
1485 (mCallbackData != NULL && mCallback == NULL)) {
1486 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1487 ALOGV("can't chain callback and write");
1488 reuse = false;
1489 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001490 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1491 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001492 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001493 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001494 reuse = false;
1495 } else if (flags != mFlags) {
1496 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1497 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001498 } else if (mRecycledTrack->format() != format) {
1499 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001500 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001501 } else {
1502 ALOGV("no track available to recycle");
1503 }
1504
1505 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1506
1507 // If we can't recycle and both tracks are offloaded
1508 // we must close the previous output before opening a new one
1509 if (bothOffloaded && !reuse) {
1510 ALOGV("both offloaded and not recycling");
1511 deleteRecycledTrack();
1512 }
1513
1514 sp<AudioTrack> t;
1515 CallbackData *newcbd = NULL;
1516
1517 // We don't attempt to create a new track if we are recycling an
1518 // offloaded track. But, if we are recycling a non-offloaded or we
1519 // are switching where one is offloaded and one isn't then we create
1520 // the new track in advance so that we can read additional stream info
1521
1522 if (!(reuse && bothOffloaded)) {
1523 ALOGV("creating new AudioTrack");
1524
1525 if (mCallback != NULL) {
1526 newcbd = new CallbackData(this);
1527 t = new AudioTrack(
1528 mStreamType,
1529 sampleRate,
1530 format,
1531 channelMask,
1532 frameCount,
1533 flags,
1534 CallbackWrapper,
1535 newcbd,
1536 0, // notification frames
1537 mSessionId,
1538 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001539 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001540 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001541 mPid,
1542 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001543 } else {
1544 t = new AudioTrack(
1545 mStreamType,
1546 sampleRate,
1547 format,
1548 channelMask,
1549 frameCount,
1550 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001551 NULL, // callback
1552 NULL, // user data
1553 0, // notification frames
1554 mSessionId,
1555 AudioTrack::TRANSFER_DEFAULT,
1556 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001557 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001558 mPid,
1559 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001560 }
1561
1562 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1563 ALOGE("Unable to create audio track");
1564 delete newcbd;
1565 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001566 } else {
1567 // successful AudioTrack initialization implies a legacy stream type was generated
1568 // from the audio attributes
1569 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001570 }
1571 }
1572
1573 if (reuse) {
1574 CHECK(mRecycledTrack != NULL);
1575
1576 if (!bothOffloaded) {
1577 if (mRecycledTrack->frameCount() != t->frameCount()) {
1578 ALOGV("framecount differs: %u/%u frames",
1579 mRecycledTrack->frameCount(), t->frameCount());
1580 reuse = false;
1581 }
1582 }
1583
Marco Nelissen67295b52012-06-11 14:52:53 -07001584 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001585 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001586 close();
1587 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001588 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001589 if (mCallbackData != NULL) {
1590 mCallbackData->setOutput(this);
1591 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001592 delete newcbd;
1593 return OK;
1594 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001595 }
1596
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001597 // we're not going to reuse the track, unblock and flush it
1598 // this was done earlier if both tracks are offloaded
1599 if (!bothOffloaded) {
1600 deleteRecycledTrack();
1601 }
1602
1603 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1604
Marco Nelissen67295b52012-06-11 14:52:53 -07001605 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001606 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001607 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001608
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001609 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001610 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001611 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001612 uint32_t pos;
1613 if (t->getPosition(&pos) == OK) {
1614 mBytesWritten = uint64_t(pos) * t->frameSize();
1615 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001617
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001618 status_t res = NO_ERROR;
1619 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1620 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1621 if (res == NO_ERROR) {
1622 t->setAuxEffectSendLevel(mSendLevel);
1623 res = t->attachAuxEffect(mAuxEffectId);
1624 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001625 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001626 ALOGV("open() DONE status %d", res);
1627 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001628}
1629
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001630status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001631{
Steve Block3856b092011-10-20 11:56:00 +01001632 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001633 if (mCallbackData != NULL) {
1634 mCallbackData->endTrackSwitch();
1635 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001636 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001637 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001638 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001639 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001640 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001641 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642}
1643
Marco Nelissen6b74d672012-02-28 16:07:44 -08001644void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1645 mNextOutput = nextOutput;
1646}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001647
1648
Marco Nelissen6b74d672012-02-28 16:07:44 -08001649void MediaPlayerService::AudioOutput::switchToNextOutput() {
1650 ALOGV("switchToNextOutput");
1651 if (mNextOutput != NULL) {
1652 if (mCallbackData != NULL) {
1653 mCallbackData->beginTrackSwitch();
1654 }
1655 delete mNextOutput->mCallbackData;
1656 mNextOutput->mCallbackData = mCallbackData;
1657 mCallbackData = NULL;
1658 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001659 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001660 mNextOutput->mSampleRateHz = mSampleRateHz;
1661 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001662 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001663 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001664 }
1665}
1666
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001667ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1668{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001669 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001670
Steve Block3856b092011-10-20 11:56:00 +01001671 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001672 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001673 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001674 if (ret >= 0) {
1675 mBytesWritten += ret;
1676 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001677 return ret;
1678 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679 return NO_INIT;
1680}
1681
1682void MediaPlayerService::AudioOutput::stop()
1683{
Steve Block3856b092011-10-20 11:56:00 +01001684 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001685 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001686}
1687
1688void MediaPlayerService::AudioOutput::flush()
1689{
Steve Block3856b092011-10-20 11:56:00 +01001690 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001691 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001692}
1693
1694void MediaPlayerService::AudioOutput::pause()
1695{
Steve Block3856b092011-10-20 11:56:00 +01001696 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001697 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001698}
1699
1700void MediaPlayerService::AudioOutput::close()
1701{
Steve Block3856b092011-10-20 11:56:00 +01001702 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001703 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001704}
1705
1706void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1707{
Steve Block3856b092011-10-20 11:56:00 +01001708 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001709 mLeftVolume = left;
1710 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001711 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001712 mTrack->setVolume(left, right);
1713 }
1714}
1715
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001716status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1717{
1718 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1719 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001720 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001721 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1722 } else {
1723 res = NO_INIT;
1724 }
1725 mPlaybackRatePermille = ratePermille;
1726 if (mSampleRateHz != 0) {
1727 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1728 }
1729 return res;
1730}
1731
Eric Laurent2beeb502010-07-16 07:43:46 -07001732status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1733{
Steve Block3856b092011-10-20 11:56:00 +01001734 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001735 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001736 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001737 return mTrack->setAuxEffectSendLevel(level);
1738 }
1739 return NO_ERROR;
1740}
1741
1742status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1743{
Steve Block3856b092011-10-20 11:56:00 +01001744 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001745 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001746 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001747 return mTrack->attachAuxEffect(effectId);
1748 }
1749 return NO_ERROR;
1750}
1751
Andreas Huber20111aa2009-07-14 16:56:47 -07001752// static
1753void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001754 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001755 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001756 CallbackData *data = (CallbackData*)cookie;
1757 data->lock();
1758 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001759 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001760 if (me == NULL) {
1761 // no output set, likely because the track was scheduled to be reused
1762 // by another player, but the format turned out to be incompatible.
1763 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001764 if (buffer != NULL) {
1765 buffer->size = 0;
1766 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001767 return;
1768 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001769
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001770 switch(event) {
1771 case AudioTrack::EVENT_MORE_DATA: {
1772 size_t actualSize = (*me->mCallback)(
1773 me, buffer->raw, buffer->size, me->mCallbackCookie,
1774 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001775
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301776 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1777 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001778 // We've reached EOS but the audio track is not stopped yet,
1779 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001780
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001781 memset(buffer->raw, 0, buffer->size);
1782 actualSize = buffer->size;
1783 }
1784
1785 buffer->size = actualSize;
1786 } break;
1787
1788
1789 case AudioTrack::EVENT_STREAM_END:
1790 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1791 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1792 me->mCallbackCookie, CB_EVENT_STREAM_END);
1793 break;
1794
1795 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1796 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1797 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1798 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1799 break;
1800
1801 default:
1802 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001803 }
1804
Marco Nelissen6b74d672012-02-28 16:07:44 -08001805 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001806}
1807
Marco Nelissen4110c102012-03-29 09:31:28 -07001808int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001809{
1810 return mSessionId;
1811}
1812
Eric Laurent6f59db12013-07-26 17:16:50 -07001813uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1814{
1815 if (mTrack == 0) return 0;
1816 return mTrack->getSampleRate();
1817}
1818
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001819////////////////////////////////////////////////////////////////////////////////
1820
1821struct CallbackThread : public Thread {
1822 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1823 MediaPlayerBase::AudioSink::AudioCallback cb,
1824 void *cookie);
1825
1826protected:
1827 virtual ~CallbackThread();
1828
1829 virtual bool threadLoop();
1830
1831private:
1832 wp<MediaPlayerBase::AudioSink> mSink;
1833 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1834 void *mCookie;
1835 void *mBuffer;
1836 size_t mBufferSize;
1837
1838 CallbackThread(const CallbackThread &);
1839 CallbackThread &operator=(const CallbackThread &);
1840};
1841
1842CallbackThread::CallbackThread(
1843 const wp<MediaPlayerBase::AudioSink> &sink,
1844 MediaPlayerBase::AudioSink::AudioCallback cb,
1845 void *cookie)
1846 : mSink(sink),
1847 mCallback(cb),
1848 mCookie(cookie),
1849 mBuffer(NULL),
1850 mBufferSize(0) {
1851}
1852
1853CallbackThread::~CallbackThread() {
1854 if (mBuffer) {
1855 free(mBuffer);
1856 mBuffer = NULL;
1857 }
1858}
1859
1860bool CallbackThread::threadLoop() {
1861 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1862 if (sink == NULL) {
1863 return false;
1864 }
1865
1866 if (mBuffer == NULL) {
1867 mBufferSize = sink->bufferSize();
1868 mBuffer = malloc(mBufferSize);
1869 }
1870
1871 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001872 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1873 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001874
1875 if (actualSize > 0) {
1876 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001877 // Could return false on sink->write() error or short count.
1878 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001879 }
1880
1881 return true;
1882}
1883
1884////////////////////////////////////////////////////////////////////////////////
1885
Gloria Wang7cf180c2011-02-19 18:37:57 -08001886void MediaPlayerService::addBatteryData(uint32_t params)
1887{
1888 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001889
1890 int32_t time = systemTime() / 1000000L;
1891
1892 // change audio output devices. This notification comes from AudioFlinger
1893 if ((params & kBatteryDataSpeakerOn)
1894 || (params & kBatteryDataOtherAudioDeviceOn)) {
1895
1896 int deviceOn[NUM_AUDIO_DEVICES];
1897 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1898 deviceOn[i] = 0;
1899 }
1900
1901 if ((params & kBatteryDataSpeakerOn)
1902 && (params & kBatteryDataOtherAudioDeviceOn)) {
1903 deviceOn[SPEAKER_AND_OTHER] = 1;
1904 } else if (params & kBatteryDataSpeakerOn) {
1905 deviceOn[SPEAKER] = 1;
1906 } else {
1907 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1908 }
1909
1910 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1911 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1912
1913 if (mBatteryAudio.refCount > 0) { // if playing audio
1914 if (!deviceOn[i]) {
1915 mBatteryAudio.lastTime[i] += time;
1916 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1917 mBatteryAudio.lastTime[i] = 0;
1918 } else {
1919 mBatteryAudio.lastTime[i] = 0 - time;
1920 }
1921 }
1922
1923 mBatteryAudio.deviceOn[i] = deviceOn[i];
1924 }
1925 }
1926 return;
1927 }
1928
Marco Nelissenb7848f12014-12-04 08:57:56 -08001929 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08001930 if (params & kBatteryDataAudioFlingerStart) {
1931 // record the start time only if currently no other audio
1932 // is being played
1933 if (mBatteryAudio.refCount == 0) {
1934 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1935 if (mBatteryAudio.deviceOn[i]) {
1936 mBatteryAudio.lastTime[i] -= time;
1937 }
1938 }
1939 }
1940
1941 mBatteryAudio.refCount ++;
1942 return;
1943
1944 } else if (params & kBatteryDataAudioFlingerStop) {
1945 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001946 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001947 return;
1948 }
1949
1950 // record the stop time only if currently this is the only
1951 // audio being played
1952 if (mBatteryAudio.refCount == 1) {
1953 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1954 if (mBatteryAudio.deviceOn[i]) {
1955 mBatteryAudio.lastTime[i] += time;
1956 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1957 mBatteryAudio.lastTime[i] = 0;
1958 }
1959 }
1960 }
1961
1962 mBatteryAudio.refCount --;
1963 return;
1964 }
1965
Gloria Wang7cf180c2011-02-19 18:37:57 -08001966 int uid = IPCThreadState::self()->getCallingUid();
1967 if (uid == AID_MEDIA) {
1968 return;
1969 }
1970 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001971
1972 if (index < 0) { // create a new entry for this UID
1973 BatteryUsageInfo info;
1974 info.audioTotalTime = 0;
1975 info.videoTotalTime = 0;
1976 info.audioLastTime = 0;
1977 info.videoLastTime = 0;
1978 info.refCount = 0;
1979
Gloria Wang9ee159b2011-02-24 14:51:45 -08001980 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001981 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001982 return;
1983 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001984 }
1985
1986 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1987
1988 if (params & kBatteryDataCodecStarted) {
1989 if (params & kBatteryDataTrackAudio) {
1990 info.audioLastTime -= time;
1991 info.refCount ++;
1992 }
1993 if (params & kBatteryDataTrackVideo) {
1994 info.videoLastTime -= time;
1995 info.refCount ++;
1996 }
1997 } else {
1998 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001999 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002000 return;
2001 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002002 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002003 mBatteryData.removeItem(uid);
2004 return;
2005 }
2006
2007 if (params & kBatteryDataTrackAudio) {
2008 info.audioLastTime += time;
2009 info.refCount --;
2010 }
2011 if (params & kBatteryDataTrackVideo) {
2012 info.videoLastTime += time;
2013 info.refCount --;
2014 }
2015
2016 // no stream is being played by this UID
2017 if (info.refCount == 0) {
2018 info.audioTotalTime += info.audioLastTime;
2019 info.audioLastTime = 0;
2020 info.videoTotalTime += info.videoLastTime;
2021 info.videoLastTime = 0;
2022 }
2023 }
2024}
2025
2026status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2027 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002028
2029 // audio output devices usage
2030 int32_t time = systemTime() / 1000000L; //in ms
2031 int32_t totalTime;
2032
2033 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2034 totalTime = mBatteryAudio.totalTime[i];
2035
2036 if (mBatteryAudio.deviceOn[i]
2037 && (mBatteryAudio.lastTime[i] != 0)) {
2038 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2039 totalTime += tmpTime;
2040 }
2041
2042 reply->writeInt32(totalTime);
2043 // reset the total time
2044 mBatteryAudio.totalTime[i] = 0;
2045 }
2046
2047 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002048 BatteryUsageInfo info;
2049 int size = mBatteryData.size();
2050
2051 reply->writeInt32(size);
2052 int i = 0;
2053
2054 while (i < size) {
2055 info = mBatteryData.valueAt(i);
2056
2057 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2058 reply->writeInt32(info.audioTotalTime);
2059 reply->writeInt32(info.videoTotalTime);
2060
2061 info.audioTotalTime = 0;
2062 info.videoTotalTime = 0;
2063
2064 // remove the UID entry where no stream is being played
2065 if (info.refCount <= 0) {
2066 mBatteryData.removeItemsAt(i);
2067 size --;
2068 i --;
2069 }
2070 i++;
2071 }
2072 return NO_ERROR;
2073}
nikoa64c8c72009-07-20 15:07:26 -07002074} // namespace android