blob: 46b8b180d3ee7ea6d960ad9e5e51aed0744f3b2e [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 return UNKNOWN_ERROR;
752 }
753 if (offset + length > sb.st_size) {
754 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756 }
757
John Grossman44a7e422012-06-21 17:29:24 -0700758 player_type playerType = MediaPlayerFactory::getPlayerType(this,
759 fd,
760 offset,
761 length);
John Grossmanc795b642012-02-22 15:38:35 -0800762 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
763 if (p == NULL) {
764 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 }
766
767 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800768 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 return mStatus;
770}
771
Andreas Hubere2b10282010-11-23 11:41:34 -0800772status_t MediaPlayerService::Client::setDataSource(
773 const sp<IStreamSource> &source) {
774 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700775 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800776 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800777 if (p == NULL) {
778 return NO_INIT;
779 }
780
Andreas Hubere2b10282010-11-23 11:41:34 -0800781 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800782 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800783 return mStatus;
784}
785
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700786void MediaPlayerService::Client::disconnectNativeWindow() {
787 if (mConnectedWindow != NULL) {
788 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
789 NATIVE_WINDOW_API_MEDIA);
790
791 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000792 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700793 strerror(-err), err);
794 }
795 }
796 mConnectedWindow.clear();
797}
798
Glenn Kasten11731182011-02-08 17:26:17 -0800799status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800800 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800801{
Andy McFadden484566c2012-12-18 09:46:54 -0800802 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800803 sp<MediaPlayerBase> p = getPlayer();
804 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700805
Marco Nelissenf8880202014-11-14 07:58:25 -0800806 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700807 if (mConnectedWindowBinder == binder) {
808 return OK;
809 }
810
811 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800812 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700813 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700814 status_t err = native_window_api_connect(anw.get(),
815 NATIVE_WINDOW_API_MEDIA);
816
817 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000818 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700819 // Note that we must do the reset before disconnecting from the ANW.
820 // Otherwise queue/dequeue calls could be made on the disconnected
821 // ANW, which may result in errors.
822 reset();
823
824 disconnectNativeWindow();
825
826 return err;
827 }
828 }
829
Andy McFadden484566c2012-12-18 09:46:54 -0800830 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700831 // disconnecting the old one. Otherwise queue/dequeue calls could be made
832 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800833 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700834
835 disconnectNativeWindow();
836
837 mConnectedWindow = anw;
838
839 if (err == OK) {
840 mConnectedWindowBinder = binder;
841 } else {
842 disconnectNativeWindow();
843 }
844
845 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800846}
847
Nicolas Catania1d187f12009-05-12 23:25:55 -0700848status_t MediaPlayerService::Client::invoke(const Parcel& request,
849 Parcel *reply)
850{
851 sp<MediaPlayerBase> p = getPlayer();
852 if (p == NULL) return UNKNOWN_ERROR;
853 return p->invoke(request, reply);
854}
855
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700856// This call doesn't need to access the native player.
857status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
858{
859 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700860 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700861
Nicolas Catania48290382009-07-10 13:53:06 -0700862 if (unmarshallFilter(filter, &allow, &status) &&
863 unmarshallFilter(filter, &drop, &status)) {
864 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700865
866 mMetadataAllow = allow;
867 mMetadataDrop = drop;
868 }
869 return status;
870}
871
Nicolas Catania48290382009-07-10 13:53:06 -0700872status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700873 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700874{
nikoa64c8c72009-07-20 15:07:26 -0700875 sp<MediaPlayerBase> player = getPlayer();
876 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700877
nikod608a812009-07-16 16:39:53 -0700878 status_t status;
879 // Placeholder for the return code, updated by the caller.
880 reply->writeInt32(-1);
881
nikoa64c8c72009-07-20 15:07:26 -0700882 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700883
884 // We don't block notifications while we fetch the data. We clear
885 // mMetadataUpdated first so we don't lose notifications happening
886 // during the rest of this call.
887 {
888 Mutex::Autolock lock(mLock);
889 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700890 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700891 }
892 mMetadataUpdated.clear();
893 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700894
nikoa64c8c72009-07-20 15:07:26 -0700895 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700896
nikoa64c8c72009-07-20 15:07:26 -0700897 metadata.appendHeader();
898 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700899
900 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700901 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000902 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700903 return status;
904 }
905
906 // FIXME: Implement filtering on the result. Not critical since
907 // filtering takes place on the update notifications already. This
908 // would be when all the metadata are fetch and a filter is set.
909
nikod608a812009-07-16 16:39:53 -0700910 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700911 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700912 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700913}
914
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800915status_t MediaPlayerService::Client::prepareAsync()
916{
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 sp<MediaPlayerBase> p = getPlayer();
919 if (p == 0) return UNKNOWN_ERROR;
920 status_t ret = p->prepareAsync();
921#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000922 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800923 if (ret == NO_ERROR) mAntagonizer->start();
924#endif
925 return ret;
926}
927
928status_t MediaPlayerService::Client::start()
929{
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 sp<MediaPlayerBase> p = getPlayer();
932 if (p == 0) return UNKNOWN_ERROR;
933 p->setLooping(mLoop);
934 return p->start();
935}
936
937status_t MediaPlayerService::Client::stop()
938{
Steve Block3856b092011-10-20 11:56:00 +0100939 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800940 sp<MediaPlayerBase> p = getPlayer();
941 if (p == 0) return UNKNOWN_ERROR;
942 return p->stop();
943}
944
945status_t MediaPlayerService::Client::pause()
946{
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800948 sp<MediaPlayerBase> p = getPlayer();
949 if (p == 0) return UNKNOWN_ERROR;
950 return p->pause();
951}
952
953status_t MediaPlayerService::Client::isPlaying(bool* state)
954{
955 *state = false;
956 sp<MediaPlayerBase> p = getPlayer();
957 if (p == 0) return UNKNOWN_ERROR;
958 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100959 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800960 return NO_ERROR;
961}
962
963status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
964{
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800966 sp<MediaPlayerBase> p = getPlayer();
967 if (p == 0) return UNKNOWN_ERROR;
968 status_t ret = p->getCurrentPosition(msec);
969 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800971 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000972 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800973 }
974 return ret;
975}
976
977status_t MediaPlayerService::Client::getDuration(int *msec)
978{
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800980 sp<MediaPlayerBase> p = getPlayer();
981 if (p == 0) return UNKNOWN_ERROR;
982 status_t ret = p->getDuration(msec);
983 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100984 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800985 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000986 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800987 }
988 return ret;
989}
990
Marco Nelissen6b74d672012-02-28 16:07:44 -0800991status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
992 ALOGV("setNextPlayer");
993 Mutex::Autolock l(mLock);
994 sp<Client> c = static_cast<Client*>(player.get());
995 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700996
997 if (c != NULL) {
998 if (mAudioOutput != NULL) {
999 mAudioOutput->setNextOutput(c->mAudioOutput);
1000 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1001 ALOGE("no current audio output");
1002 }
1003
1004 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1005 mPlayer->setNextPlayer(mNextClient->getPlayer());
1006 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001007 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001008
Marco Nelissen6b74d672012-02-28 16:07:44 -08001009 return OK;
1010}
1011
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001012status_t MediaPlayerService::Client::seekTo(int msec)
1013{
Steve Block3856b092011-10-20 11:56:00 +01001014 ALOGV("[%d] seekTo(%d)", mConnId, msec);
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->seekTo(msec);
1018}
1019
1020status_t MediaPlayerService::Client::reset()
1021{
Steve Block3856b092011-10-20 11:56:00 +01001022 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001023 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024 sp<MediaPlayerBase> p = getPlayer();
1025 if (p == 0) return UNKNOWN_ERROR;
1026 return p->reset();
1027}
1028
Glenn Kastenfff6d712012-01-12 16:38:12 -08001029status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001030{
Steve Block3856b092011-10-20 11:56:00 +01001031 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001032 // TODO: for hardware output, call player instead
1033 Mutex::Autolock l(mLock);
1034 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1035 return NO_ERROR;
1036}
1037
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001038status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1039{
1040 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1041 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1042 unmarshallAudioAttributes(parcel, mAudioAttributes);
1043
1044 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1045 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1046 mAudioAttributes->tags);
1047
1048 if (mAudioOutput != 0) {
1049 mAudioOutput->setAudioAttributes(mAudioAttributes);
1050 }
1051 return NO_ERROR;
1052}
1053
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001054status_t MediaPlayerService::Client::setLooping(int loop)
1055{
Steve Block3856b092011-10-20 11:56:00 +01001056 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001057 mLoop = loop;
1058 sp<MediaPlayerBase> p = getPlayer();
1059 if (p != 0) return p->setLooping(loop);
1060 return NO_ERROR;
1061}
1062
1063status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1064{
Steve Block3856b092011-10-20 11:56:00 +01001065 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001066
1067 // for hardware output, call player instead
1068 sp<MediaPlayerBase> p = getPlayer();
1069 {
1070 Mutex::Autolock l(mLock);
1071 if (p != 0 && p->hardwareOutput()) {
1072 MediaPlayerHWInterface* hwp =
1073 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1074 return hwp->setVolume(leftVolume, rightVolume);
1075 } else {
1076 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1077 return NO_ERROR;
1078 }
1079 }
1080
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001081 return NO_ERROR;
1082}
1083
Eric Laurent2beeb502010-07-16 07:43:46 -07001084status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1085{
Steve Block3856b092011-10-20 11:56:00 +01001086 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001087 Mutex::Autolock l(mLock);
1088 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1089 return NO_ERROR;
1090}
1091
1092status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1093{
Steve Block3856b092011-10-20 11:56:00 +01001094 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001095 Mutex::Autolock l(mLock);
1096 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1097 return NO_ERROR;
1098}
Nicolas Catania48290382009-07-10 13:53:06 -07001099
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001100status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001101 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001102 switch (key) {
1103 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1104 {
1105 Mutex::Autolock l(mLock);
1106 return setAudioAttributes_l(request);
1107 }
1108 default:
1109 sp<MediaPlayerBase> p = getPlayer();
1110 if (p == 0) { return UNKNOWN_ERROR; }
1111 return p->setParameter(key, request);
1112 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001113}
1114
1115status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001116 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001117 sp<MediaPlayerBase> p = getPlayer();
1118 if (p == 0) return UNKNOWN_ERROR;
1119 return p->getParameter(key, reply);
1120}
1121
John Grossmanc795b642012-02-22 15:38:35 -08001122status_t MediaPlayerService::Client::setRetransmitEndpoint(
1123 const struct sockaddr_in* endpoint) {
1124
1125 if (NULL != endpoint) {
1126 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1127 uint16_t p = ntohs(endpoint->sin_port);
1128 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1129 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1130 } else {
1131 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1132 }
1133
1134 sp<MediaPlayerBase> p = getPlayer();
1135
1136 // Right now, the only valid time to set a retransmit endpoint is before
1137 // player selection has been made (since the presence or absence of a
1138 // retransmit endpoint is going to determine which player is selected during
1139 // setDataSource).
1140 if (p != 0) return INVALID_OPERATION;
1141
1142 if (NULL != endpoint) {
1143 mRetransmitEndpoint = *endpoint;
1144 mRetransmitEndpointValid = true;
1145 } else {
1146 mRetransmitEndpointValid = false;
1147 }
1148
1149 return NO_ERROR;
1150}
1151
John Grossman44a7e422012-06-21 17:29:24 -07001152status_t MediaPlayerService::Client::getRetransmitEndpoint(
1153 struct sockaddr_in* endpoint)
1154{
1155 if (NULL == endpoint)
1156 return BAD_VALUE;
1157
1158 sp<MediaPlayerBase> p = getPlayer();
1159
1160 if (p != NULL)
1161 return p->getRetransmitEndpoint(endpoint);
1162
1163 if (!mRetransmitEndpointValid)
1164 return NO_INIT;
1165
1166 *endpoint = mRetransmitEndpoint;
1167
1168 return NO_ERROR;
1169}
1170
Gloria Wangb483c472011-04-11 17:23:27 -07001171void MediaPlayerService::Client::notify(
1172 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001173{
1174 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001175 if (client == NULL) {
1176 return;
1177 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001178
James Dongb8a98252012-08-26 16:13:03 -07001179 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001180 {
1181 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001182 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001183 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001184 if (client->mAudioOutput != NULL)
1185 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001186 client->mNextClient->start();
1187 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1188 }
1189 }
1190
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001191 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001192 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001193 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001194
1195 if(client->shouldDropMetadata(metadata_type)) {
1196 return;
1197 }
1198
1199 // Update the list of metadata that have changed. getMetadata
1200 // also access mMetadataUpdated and clears it.
1201 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001202 }
James Dongb8a98252012-08-26 16:13:03 -07001203
1204 if (c != NULL) {
1205 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1206 c->notify(msg, ext1, ext2, obj);
1207 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208}
1209
Nicolas Catania48290382009-07-10 13:53:06 -07001210
nikoa64c8c72009-07-20 15:07:26 -07001211bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001212{
Nicolas Catania48290382009-07-10 13:53:06 -07001213 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001214
Nicolas Catania48290382009-07-10 13:53:06 -07001215 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001216 return true;
1217 }
1218
Nicolas Catania48290382009-07-10 13:53:06 -07001219 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001220 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001221 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001222 return true;
1223 }
1224}
1225
Nicolas Catania48290382009-07-10 13:53:06 -07001226
nikoa64c8c72009-07-20 15:07:26 -07001227void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001228 Mutex::Autolock lock(mLock);
1229 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1230 mMetadataUpdated.add(metadata_type);
1231 }
1232}
1233
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234#if CALLBACK_ANTAGONIZER
1235const int Antagonizer::interval = 10000; // 10 msecs
1236
1237Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1238 mExit(false), mActive(false), mClient(client), mCb(cb)
1239{
1240 createThread(callbackThread, this);
1241}
1242
1243void Antagonizer::kill()
1244{
1245 Mutex::Autolock _l(mLock);
1246 mActive = false;
1247 mExit = true;
1248 mCondition.wait(mLock);
1249}
1250
1251int Antagonizer::callbackThread(void* user)
1252{
Steve Blockb8a80522011-12-20 16:23:08 +00001253 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1255 while (!p->mExit) {
1256 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001257 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258 p->mCb(p->mClient, 0, 0, 0);
1259 }
1260 usleep(interval);
1261 }
1262 Mutex::Autolock _l(p->mLock);
1263 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001264 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 return 0;
1266}
1267#endif
1268
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269#undef LOG_TAG
1270#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001271MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1272 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001273 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001274 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001275 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001276 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001277 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001278 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001279 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001280 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001281 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001282 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283 mLeftVolume = 1.0;
1284 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001285 mPlaybackRatePermille = 1000;
1286 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001288 mAuxEffectId = 0;
1289 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001291 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292}
1293
1294MediaPlayerService::AudioOutput::~AudioOutput()
1295{
1296 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001297 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298}
1299
1300void MediaPlayerService::AudioOutput::setMinBufferCount()
1301{
1302 char value[PROPERTY_VALUE_MAX];
1303 if (property_get("ro.kernel.qemu", value, 0)) {
1304 mIsOnEmulator = true;
1305 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1306 }
1307}
1308
1309bool MediaPlayerService::AudioOutput::isOnEmulator()
1310{
1311 setMinBufferCount();
1312 return mIsOnEmulator;
1313}
1314
1315int MediaPlayerService::AudioOutput::getMinBufferCount()
1316{
1317 setMinBufferCount();
1318 return mMinBufferCount;
1319}
1320
1321ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1322{
1323 if (mTrack == 0) return NO_INIT;
1324 return mTrack->frameCount() * frameSize();
1325}
1326
1327ssize_t MediaPlayerService::AudioOutput::frameCount() const
1328{
1329 if (mTrack == 0) return NO_INIT;
1330 return mTrack->frameCount();
1331}
1332
1333ssize_t MediaPlayerService::AudioOutput::channelCount() const
1334{
1335 if (mTrack == 0) return NO_INIT;
1336 return mTrack->channelCount();
1337}
1338
1339ssize_t MediaPlayerService::AudioOutput::frameSize() const
1340{
1341 if (mTrack == 0) return NO_INIT;
1342 return mTrack->frameSize();
1343}
1344
1345uint32_t MediaPlayerService::AudioOutput::latency () const
1346{
Eric Laurentdb354e52012-03-05 17:27:11 -08001347 if (mTrack == 0) return 0;
1348 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001349}
1350
1351float MediaPlayerService::AudioOutput::msecsPerFrame() const
1352{
1353 return mMsecsPerFrame;
1354}
1355
Marco Nelissen4110c102012-03-29 09:31:28 -07001356status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001357{
1358 if (mTrack == 0) return NO_INIT;
1359 return mTrack->getPosition(position);
1360}
1361
Lajos Molnar06ad1522014-08-28 07:27:44 -07001362status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1363{
1364 if (mTrack == 0) return NO_INIT;
1365 return mTrack->getTimestamp(ts);
1366}
1367
Marco Nelissen4110c102012-03-29 09:31:28 -07001368status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1369{
1370 if (mTrack == 0) return NO_INIT;
1371 *frameswritten = mBytesWritten / frameSize();
1372 return OK;
1373}
1374
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001375status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1376{
1377 if (mTrack == 0) return NO_INIT;
1378 return mTrack->setParameters(keyValuePairs);
1379}
1380
1381String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1382{
1383 if (mTrack == 0) return String8::empty();
1384 return mTrack->getParameters(keys);
1385}
1386
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001387void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1388 mAttributes = attributes;
1389}
1390
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001391void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1392{
1393 ALOGV("deleteRecycledTrack");
1394
1395 if (mRecycledTrack != 0) {
1396
1397 if (mCallbackData != NULL) {
1398 mCallbackData->setOutput(NULL);
1399 mCallbackData->endTrackSwitch();
1400 }
1401
1402 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1403 mRecycledTrack->flush();
1404 }
1405 // An offloaded track isn't flushed because the STREAM_END is reported
1406 // slightly prematurely to allow time for the gapless track switch
1407 // but this means that if we decide not to recycle the track there
1408 // could be a small amount of residual data still playing. We leave
1409 // AudioFlinger to drain the track.
1410
1411 mRecycledTrack.clear();
1412 delete mCallbackData;
1413 mCallbackData = NULL;
1414 close();
1415 }
1416}
1417
Andreas Huber20111aa2009-07-14 16:56:47 -07001418status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001419 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1420 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001421 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001422 audio_output_flags_t flags,
1423 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424{
Andreas Huber20111aa2009-07-14 16:56:47 -07001425 mCallback = cb;
1426 mCallbackCookie = cookie;
1427
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001428 // Check argument "bufferCount" against the mininum buffer count
1429 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001430 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001431 bufferCount = mMinBufferCount;
1432
1433 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001434 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1435 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001436 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001437 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001438 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001439
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001440 // offloading is only supported in callback mode for now.
1441 // offloadInfo must be present if offload flag is set
1442 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1443 ((cb == NULL) || (offloadInfo == NULL))) {
1444 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001445 }
1446
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001447 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1448 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1449 } else {
1450 uint32_t afSampleRate;
1451 size_t afFrameCount;
1452
1453 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1454 return NO_INIT;
1455 }
1456 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1457 return NO_INIT;
1458 }
1459
1460 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1461 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001462
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001463 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001464 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001465 if (0 == channelMask) {
1466 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1467 return NO_INIT;
1468 }
1469 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001470
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001471 // Check whether we can recycle the track
1472 bool reuse = false;
1473 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001474
Glenn Kasten2799d742013-05-30 14:33:29 -07001475 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001476 // check whether we are switching between two offloaded tracks
1477 bothOffloaded = (flags & mRecycledTrack->getFlags()
1478 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001479
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001480 // check if the existing track can be reused as-is, or if a new track needs to be created.
1481 reuse = true;
1482
Marco Nelissen67295b52012-06-11 14:52:53 -07001483 if ((mCallbackData == NULL && mCallback != NULL) ||
1484 (mCallbackData != NULL && mCallback == NULL)) {
1485 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1486 ALOGV("can't chain callback and write");
1487 reuse = false;
1488 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001489 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1490 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001491 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001492 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001493 reuse = false;
1494 } else if (flags != mFlags) {
1495 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1496 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001497 } else if (mRecycledTrack->format() != format) {
1498 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001499 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001500 } else {
1501 ALOGV("no track available to recycle");
1502 }
1503
1504 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1505
1506 // If we can't recycle and both tracks are offloaded
1507 // we must close the previous output before opening a new one
1508 if (bothOffloaded && !reuse) {
1509 ALOGV("both offloaded and not recycling");
1510 deleteRecycledTrack();
1511 }
1512
1513 sp<AudioTrack> t;
1514 CallbackData *newcbd = NULL;
1515
1516 // We don't attempt to create a new track if we are recycling an
1517 // offloaded track. But, if we are recycling a non-offloaded or we
1518 // are switching where one is offloaded and one isn't then we create
1519 // the new track in advance so that we can read additional stream info
1520
1521 if (!(reuse && bothOffloaded)) {
1522 ALOGV("creating new AudioTrack");
1523
1524 if (mCallback != NULL) {
1525 newcbd = new CallbackData(this);
1526 t = new AudioTrack(
1527 mStreamType,
1528 sampleRate,
1529 format,
1530 channelMask,
1531 frameCount,
1532 flags,
1533 CallbackWrapper,
1534 newcbd,
1535 0, // notification frames
1536 mSessionId,
1537 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001538 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001539 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001540 mPid,
1541 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001542 } else {
1543 t = new AudioTrack(
1544 mStreamType,
1545 sampleRate,
1546 format,
1547 channelMask,
1548 frameCount,
1549 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001550 NULL, // callback
1551 NULL, // user data
1552 0, // notification frames
1553 mSessionId,
1554 AudioTrack::TRANSFER_DEFAULT,
1555 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001556 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001557 mPid,
1558 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001559 }
1560
1561 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1562 ALOGE("Unable to create audio track");
1563 delete newcbd;
1564 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001565 } else {
1566 // successful AudioTrack initialization implies a legacy stream type was generated
1567 // from the audio attributes
1568 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001569 }
1570 }
1571
1572 if (reuse) {
1573 CHECK(mRecycledTrack != NULL);
1574
1575 if (!bothOffloaded) {
1576 if (mRecycledTrack->frameCount() != t->frameCount()) {
1577 ALOGV("framecount differs: %u/%u frames",
1578 mRecycledTrack->frameCount(), t->frameCount());
1579 reuse = false;
1580 }
1581 }
1582
Marco Nelissen67295b52012-06-11 14:52:53 -07001583 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001584 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001585 close();
1586 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001587 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001588 if (mCallbackData != NULL) {
1589 mCallbackData->setOutput(this);
1590 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001591 delete newcbd;
1592 return OK;
1593 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001594 }
1595
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001596 // we're not going to reuse the track, unblock and flush it
1597 // this was done earlier if both tracks are offloaded
1598 if (!bothOffloaded) {
1599 deleteRecycledTrack();
1600 }
1601
1602 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1603
Marco Nelissen67295b52012-06-11 14:52:53 -07001604 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001605 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001607
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001608 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001609 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001610 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001611 uint32_t pos;
1612 if (t->getPosition(&pos) == OK) {
1613 mBytesWritten = uint64_t(pos) * t->frameSize();
1614 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001615 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001616
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001617 status_t res = NO_ERROR;
1618 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1619 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1620 if (res == NO_ERROR) {
1621 t->setAuxEffectSendLevel(mSendLevel);
1622 res = t->attachAuxEffect(mAuxEffectId);
1623 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001624 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001625 ALOGV("open() DONE status %d", res);
1626 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001627}
1628
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001629status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630{
Steve Block3856b092011-10-20 11:56:00 +01001631 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001632 if (mCallbackData != NULL) {
1633 mCallbackData->endTrackSwitch();
1634 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001635 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001636 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001637 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001638 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001639 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001640 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001641}
1642
Marco Nelissen6b74d672012-02-28 16:07:44 -08001643void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1644 mNextOutput = nextOutput;
1645}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001646
1647
Marco Nelissen6b74d672012-02-28 16:07:44 -08001648void MediaPlayerService::AudioOutput::switchToNextOutput() {
1649 ALOGV("switchToNextOutput");
1650 if (mNextOutput != NULL) {
1651 if (mCallbackData != NULL) {
1652 mCallbackData->beginTrackSwitch();
1653 }
1654 delete mNextOutput->mCallbackData;
1655 mNextOutput->mCallbackData = mCallbackData;
1656 mCallbackData = NULL;
1657 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001658 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001659 mNextOutput->mSampleRateHz = mSampleRateHz;
1660 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001661 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001662 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001663 }
1664}
1665
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001666ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1667{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001668 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001669
Steve Block3856b092011-10-20 11:56:00 +01001670 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001671 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001672 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001673 if (ret >= 0) {
1674 mBytesWritten += ret;
1675 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001676 return ret;
1677 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001678 return NO_INIT;
1679}
1680
1681void MediaPlayerService::AudioOutput::stop()
1682{
Steve Block3856b092011-10-20 11:56:00 +01001683 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001684 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001685}
1686
1687void MediaPlayerService::AudioOutput::flush()
1688{
Steve Block3856b092011-10-20 11:56:00 +01001689 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001690 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001691}
1692
1693void MediaPlayerService::AudioOutput::pause()
1694{
Steve Block3856b092011-10-20 11:56:00 +01001695 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001696 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001697}
1698
1699void MediaPlayerService::AudioOutput::close()
1700{
Steve Block3856b092011-10-20 11:56:00 +01001701 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001702 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001703}
1704
1705void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1706{
Steve Block3856b092011-10-20 11:56:00 +01001707 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001708 mLeftVolume = left;
1709 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001710 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001711 mTrack->setVolume(left, right);
1712 }
1713}
1714
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001715status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1716{
1717 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1718 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001719 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001720 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1721 } else {
1722 res = NO_INIT;
1723 }
1724 mPlaybackRatePermille = ratePermille;
1725 if (mSampleRateHz != 0) {
1726 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1727 }
1728 return res;
1729}
1730
Eric Laurent2beeb502010-07-16 07:43:46 -07001731status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1732{
Steve Block3856b092011-10-20 11:56:00 +01001733 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001734 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001735 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001736 return mTrack->setAuxEffectSendLevel(level);
1737 }
1738 return NO_ERROR;
1739}
1740
1741status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1742{
Steve Block3856b092011-10-20 11:56:00 +01001743 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001744 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001745 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001746 return mTrack->attachAuxEffect(effectId);
1747 }
1748 return NO_ERROR;
1749}
1750
Andreas Huber20111aa2009-07-14 16:56:47 -07001751// static
1752void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001753 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001754 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001755 CallbackData *data = (CallbackData*)cookie;
1756 data->lock();
1757 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001758 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001759 if (me == NULL) {
1760 // no output set, likely because the track was scheduled to be reused
1761 // by another player, but the format turned out to be incompatible.
1762 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001763 if (buffer != NULL) {
1764 buffer->size = 0;
1765 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001766 return;
1767 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001768
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001769 switch(event) {
1770 case AudioTrack::EVENT_MORE_DATA: {
1771 size_t actualSize = (*me->mCallback)(
1772 me, buffer->raw, buffer->size, me->mCallbackCookie,
1773 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001774
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301775 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1776 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001777 // We've reached EOS but the audio track is not stopped yet,
1778 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001779
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001780 memset(buffer->raw, 0, buffer->size);
1781 actualSize = buffer->size;
1782 }
1783
1784 buffer->size = actualSize;
1785 } break;
1786
1787
1788 case AudioTrack::EVENT_STREAM_END:
1789 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1790 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1791 me->mCallbackCookie, CB_EVENT_STREAM_END);
1792 break;
1793
1794 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1795 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1796 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1797 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1798 break;
1799
1800 default:
1801 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001802 }
1803
Marco Nelissen6b74d672012-02-28 16:07:44 -08001804 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001805}
1806
Marco Nelissen4110c102012-03-29 09:31:28 -07001807int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001808{
1809 return mSessionId;
1810}
1811
Eric Laurent6f59db12013-07-26 17:16:50 -07001812uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1813{
1814 if (mTrack == 0) return 0;
1815 return mTrack->getSampleRate();
1816}
1817
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001818////////////////////////////////////////////////////////////////////////////////
1819
1820struct CallbackThread : public Thread {
1821 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1822 MediaPlayerBase::AudioSink::AudioCallback cb,
1823 void *cookie);
1824
1825protected:
1826 virtual ~CallbackThread();
1827
1828 virtual bool threadLoop();
1829
1830private:
1831 wp<MediaPlayerBase::AudioSink> mSink;
1832 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1833 void *mCookie;
1834 void *mBuffer;
1835 size_t mBufferSize;
1836
1837 CallbackThread(const CallbackThread &);
1838 CallbackThread &operator=(const CallbackThread &);
1839};
1840
1841CallbackThread::CallbackThread(
1842 const wp<MediaPlayerBase::AudioSink> &sink,
1843 MediaPlayerBase::AudioSink::AudioCallback cb,
1844 void *cookie)
1845 : mSink(sink),
1846 mCallback(cb),
1847 mCookie(cookie),
1848 mBuffer(NULL),
1849 mBufferSize(0) {
1850}
1851
1852CallbackThread::~CallbackThread() {
1853 if (mBuffer) {
1854 free(mBuffer);
1855 mBuffer = NULL;
1856 }
1857}
1858
1859bool CallbackThread::threadLoop() {
1860 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1861 if (sink == NULL) {
1862 return false;
1863 }
1864
1865 if (mBuffer == NULL) {
1866 mBufferSize = sink->bufferSize();
1867 mBuffer = malloc(mBufferSize);
1868 }
1869
1870 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001871 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1872 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001873
1874 if (actualSize > 0) {
1875 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001876 // Could return false on sink->write() error or short count.
1877 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001878 }
1879
1880 return true;
1881}
1882
1883////////////////////////////////////////////////////////////////////////////////
1884
Gloria Wang7cf180c2011-02-19 18:37:57 -08001885void MediaPlayerService::addBatteryData(uint32_t params)
1886{
1887 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001888
1889 int32_t time = systemTime() / 1000000L;
1890
1891 // change audio output devices. This notification comes from AudioFlinger
1892 if ((params & kBatteryDataSpeakerOn)
1893 || (params & kBatteryDataOtherAudioDeviceOn)) {
1894
1895 int deviceOn[NUM_AUDIO_DEVICES];
1896 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1897 deviceOn[i] = 0;
1898 }
1899
1900 if ((params & kBatteryDataSpeakerOn)
1901 && (params & kBatteryDataOtherAudioDeviceOn)) {
1902 deviceOn[SPEAKER_AND_OTHER] = 1;
1903 } else if (params & kBatteryDataSpeakerOn) {
1904 deviceOn[SPEAKER] = 1;
1905 } else {
1906 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1907 }
1908
1909 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1910 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1911
1912 if (mBatteryAudio.refCount > 0) { // if playing audio
1913 if (!deviceOn[i]) {
1914 mBatteryAudio.lastTime[i] += time;
1915 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1916 mBatteryAudio.lastTime[i] = 0;
1917 } else {
1918 mBatteryAudio.lastTime[i] = 0 - time;
1919 }
1920 }
1921
1922 mBatteryAudio.deviceOn[i] = deviceOn[i];
1923 }
1924 }
1925 return;
1926 }
1927
Marco Nelissenb7848f12014-12-04 08:57:56 -08001928 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08001929 if (params & kBatteryDataAudioFlingerStart) {
1930 // record the start time only if currently no other audio
1931 // is being played
1932 if (mBatteryAudio.refCount == 0) {
1933 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1934 if (mBatteryAudio.deviceOn[i]) {
1935 mBatteryAudio.lastTime[i] -= time;
1936 }
1937 }
1938 }
1939
1940 mBatteryAudio.refCount ++;
1941 return;
1942
1943 } else if (params & kBatteryDataAudioFlingerStop) {
1944 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001945 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001946 return;
1947 }
1948
1949 // record the stop time only if currently this is the only
1950 // audio being played
1951 if (mBatteryAudio.refCount == 1) {
1952 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1953 if (mBatteryAudio.deviceOn[i]) {
1954 mBatteryAudio.lastTime[i] += time;
1955 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1956 mBatteryAudio.lastTime[i] = 0;
1957 }
1958 }
1959 }
1960
1961 mBatteryAudio.refCount --;
1962 return;
1963 }
1964
Gloria Wang7cf180c2011-02-19 18:37:57 -08001965 int uid = IPCThreadState::self()->getCallingUid();
1966 if (uid == AID_MEDIA) {
1967 return;
1968 }
1969 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001970
1971 if (index < 0) { // create a new entry for this UID
1972 BatteryUsageInfo info;
1973 info.audioTotalTime = 0;
1974 info.videoTotalTime = 0;
1975 info.audioLastTime = 0;
1976 info.videoLastTime = 0;
1977 info.refCount = 0;
1978
Gloria Wang9ee159b2011-02-24 14:51:45 -08001979 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001980 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001981 return;
1982 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001983 }
1984
1985 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1986
1987 if (params & kBatteryDataCodecStarted) {
1988 if (params & kBatteryDataTrackAudio) {
1989 info.audioLastTime -= time;
1990 info.refCount ++;
1991 }
1992 if (params & kBatteryDataTrackVideo) {
1993 info.videoLastTime -= time;
1994 info.refCount ++;
1995 }
1996 } else {
1997 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001998 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001999 return;
2000 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002001 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002002 mBatteryData.removeItem(uid);
2003 return;
2004 }
2005
2006 if (params & kBatteryDataTrackAudio) {
2007 info.audioLastTime += time;
2008 info.refCount --;
2009 }
2010 if (params & kBatteryDataTrackVideo) {
2011 info.videoLastTime += time;
2012 info.refCount --;
2013 }
2014
2015 // no stream is being played by this UID
2016 if (info.refCount == 0) {
2017 info.audioTotalTime += info.audioLastTime;
2018 info.audioLastTime = 0;
2019 info.videoTotalTime += info.videoLastTime;
2020 info.videoLastTime = 0;
2021 }
2022 }
2023}
2024
2025status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2026 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002027
2028 // audio output devices usage
2029 int32_t time = systemTime() / 1000000L; //in ms
2030 int32_t totalTime;
2031
2032 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2033 totalTime = mBatteryAudio.totalTime[i];
2034
2035 if (mBatteryAudio.deviceOn[i]
2036 && (mBatteryAudio.lastTime[i] != 0)) {
2037 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2038 totalTime += tmpTime;
2039 }
2040
2041 reply->writeInt32(totalTime);
2042 // reset the total time
2043 mBatteryAudio.totalTime[i] = 0;
2044 }
2045
2046 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002047 BatteryUsageInfo info;
2048 int size = mBatteryData.size();
2049
2050 reply->writeInt32(size);
2051 int i = 0;
2052
2053 while (i < size) {
2054 info = mBatteryData.valueAt(i);
2055
2056 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2057 reply->writeInt32(info.audioTotalTime);
2058 reply->writeInt32(info.videoTotalTime);
2059
2060 info.audioTotalTime = 0;
2061 info.videoTotalTime = 0;
2062
2063 // remove the UID entry where no stream is being played
2064 if (info.refCount <= 0) {
2065 mBatteryData.removeItemsAt(i);
2066 size --;
2067 i --;
2068 }
2069 i++;
2070 }
2071 return NO_ERROR;
2072}
nikoa64c8c72009-07-20 15:07:26 -07002073} // namespace android