blob: 0b18ae0d0222fb0f8e462b7997c0ba5d3be96c72 [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
Wei Jia98160162015-02-04 17:01:11 -0800964status_t MediaPlayerService::Client::setPlaybackRate(float rate)
965{
966 ALOGV("[%d] setPlaybackRate(%f)", mConnId, rate);
967 sp<MediaPlayerBase> p = getPlayer();
968 if (p == 0) return UNKNOWN_ERROR;
969 return p->setPlaybackRate(rate);
970}
971
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
973{
Steve Block3856b092011-10-20 11:56:00 +0100974 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975 sp<MediaPlayerBase> p = getPlayer();
976 if (p == 0) return UNKNOWN_ERROR;
977 status_t ret = p->getCurrentPosition(msec);
978 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800980 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000981 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982 }
983 return ret;
984}
985
986status_t MediaPlayerService::Client::getDuration(int *msec)
987{
Steve Block3856b092011-10-20 11:56:00 +0100988 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800989 sp<MediaPlayerBase> p = getPlayer();
990 if (p == 0) return UNKNOWN_ERROR;
991 status_t ret = p->getDuration(msec);
992 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800994 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000995 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800996 }
997 return ret;
998}
999
Marco Nelissen6b74d672012-02-28 16:07:44 -08001000status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1001 ALOGV("setNextPlayer");
1002 Mutex::Autolock l(mLock);
1003 sp<Client> c = static_cast<Client*>(player.get());
1004 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001005
1006 if (c != NULL) {
1007 if (mAudioOutput != NULL) {
1008 mAudioOutput->setNextOutput(c->mAudioOutput);
1009 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1010 ALOGE("no current audio output");
1011 }
1012
1013 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1014 mPlayer->setNextPlayer(mNextClient->getPlayer());
1015 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001016 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001017
Marco Nelissen6b74d672012-02-28 16:07:44 -08001018 return OK;
1019}
1020
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021status_t MediaPlayerService::Client::seekTo(int msec)
1022{
Steve Block3856b092011-10-20 11:56:00 +01001023 ALOGV("[%d] seekTo(%d)", mConnId, msec);
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->seekTo(msec);
1027}
1028
1029status_t MediaPlayerService::Client::reset()
1030{
Steve Block3856b092011-10-20 11:56:00 +01001031 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001032 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 sp<MediaPlayerBase> p = getPlayer();
1034 if (p == 0) return UNKNOWN_ERROR;
1035 return p->reset();
1036}
1037
Glenn Kastenfff6d712012-01-12 16:38:12 -08001038status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001039{
Steve Block3856b092011-10-20 11:56:00 +01001040 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001041 // TODO: for hardware output, call player instead
1042 Mutex::Autolock l(mLock);
1043 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1044 return NO_ERROR;
1045}
1046
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001047status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1048{
1049 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1050 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1051 unmarshallAudioAttributes(parcel, mAudioAttributes);
1052
1053 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1054 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1055 mAudioAttributes->tags);
1056
1057 if (mAudioOutput != 0) {
1058 mAudioOutput->setAudioAttributes(mAudioAttributes);
1059 }
1060 return NO_ERROR;
1061}
1062
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001063status_t MediaPlayerService::Client::setLooping(int loop)
1064{
Steve Block3856b092011-10-20 11:56:00 +01001065 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001066 mLoop = loop;
1067 sp<MediaPlayerBase> p = getPlayer();
1068 if (p != 0) return p->setLooping(loop);
1069 return NO_ERROR;
1070}
1071
1072status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1073{
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001075
1076 // for hardware output, call player instead
1077 sp<MediaPlayerBase> p = getPlayer();
1078 {
1079 Mutex::Autolock l(mLock);
1080 if (p != 0 && p->hardwareOutput()) {
1081 MediaPlayerHWInterface* hwp =
1082 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1083 return hwp->setVolume(leftVolume, rightVolume);
1084 } else {
1085 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1086 return NO_ERROR;
1087 }
1088 }
1089
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001090 return NO_ERROR;
1091}
1092
Eric Laurent2beeb502010-07-16 07:43:46 -07001093status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1094{
Steve Block3856b092011-10-20 11:56:00 +01001095 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001096 Mutex::Autolock l(mLock);
1097 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1098 return NO_ERROR;
1099}
1100
1101status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1102{
Steve Block3856b092011-10-20 11:56:00 +01001103 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001104 Mutex::Autolock l(mLock);
1105 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1106 return NO_ERROR;
1107}
Nicolas Catania48290382009-07-10 13:53:06 -07001108
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001109status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001110 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001111 switch (key) {
1112 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1113 {
1114 Mutex::Autolock l(mLock);
1115 return setAudioAttributes_l(request);
1116 }
1117 default:
1118 sp<MediaPlayerBase> p = getPlayer();
1119 if (p == 0) { return UNKNOWN_ERROR; }
1120 return p->setParameter(key, request);
1121 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001122}
1123
1124status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001125 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001126 sp<MediaPlayerBase> p = getPlayer();
1127 if (p == 0) return UNKNOWN_ERROR;
1128 return p->getParameter(key, reply);
1129}
1130
John Grossmanc795b642012-02-22 15:38:35 -08001131status_t MediaPlayerService::Client::setRetransmitEndpoint(
1132 const struct sockaddr_in* endpoint) {
1133
1134 if (NULL != endpoint) {
1135 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1136 uint16_t p = ntohs(endpoint->sin_port);
1137 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1138 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1139 } else {
1140 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1141 }
1142
1143 sp<MediaPlayerBase> p = getPlayer();
1144
1145 // Right now, the only valid time to set a retransmit endpoint is before
1146 // player selection has been made (since the presence or absence of a
1147 // retransmit endpoint is going to determine which player is selected during
1148 // setDataSource).
1149 if (p != 0) return INVALID_OPERATION;
1150
1151 if (NULL != endpoint) {
1152 mRetransmitEndpoint = *endpoint;
1153 mRetransmitEndpointValid = true;
1154 } else {
1155 mRetransmitEndpointValid = false;
1156 }
1157
1158 return NO_ERROR;
1159}
1160
John Grossman44a7e422012-06-21 17:29:24 -07001161status_t MediaPlayerService::Client::getRetransmitEndpoint(
1162 struct sockaddr_in* endpoint)
1163{
1164 if (NULL == endpoint)
1165 return BAD_VALUE;
1166
1167 sp<MediaPlayerBase> p = getPlayer();
1168
1169 if (p != NULL)
1170 return p->getRetransmitEndpoint(endpoint);
1171
1172 if (!mRetransmitEndpointValid)
1173 return NO_INIT;
1174
1175 *endpoint = mRetransmitEndpoint;
1176
1177 return NO_ERROR;
1178}
1179
Gloria Wangb483c472011-04-11 17:23:27 -07001180void MediaPlayerService::Client::notify(
1181 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001182{
1183 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001184 if (client == NULL) {
1185 return;
1186 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001187
James Dongb8a98252012-08-26 16:13:03 -07001188 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001189 {
1190 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001191 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001192 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001193 if (client->mAudioOutput != NULL)
1194 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001195 client->mNextClient->start();
1196 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1197 }
1198 }
1199
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001200 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001201 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001202 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001203
1204 if(client->shouldDropMetadata(metadata_type)) {
1205 return;
1206 }
1207
1208 // Update the list of metadata that have changed. getMetadata
1209 // also access mMetadataUpdated and clears it.
1210 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001211 }
James Dongb8a98252012-08-26 16:13:03 -07001212
1213 if (c != NULL) {
1214 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1215 c->notify(msg, ext1, ext2, obj);
1216 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217}
1218
Nicolas Catania48290382009-07-10 13:53:06 -07001219
nikoa64c8c72009-07-20 15:07:26 -07001220bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001221{
Nicolas Catania48290382009-07-10 13:53:06 -07001222 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001223
Nicolas Catania48290382009-07-10 13:53:06 -07001224 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001225 return true;
1226 }
1227
Nicolas Catania48290382009-07-10 13:53:06 -07001228 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001229 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001230 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001231 return true;
1232 }
1233}
1234
Nicolas Catania48290382009-07-10 13:53:06 -07001235
nikoa64c8c72009-07-20 15:07:26 -07001236void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001237 Mutex::Autolock lock(mLock);
1238 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1239 mMetadataUpdated.add(metadata_type);
1240 }
1241}
1242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001243#if CALLBACK_ANTAGONIZER
1244const int Antagonizer::interval = 10000; // 10 msecs
1245
1246Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1247 mExit(false), mActive(false), mClient(client), mCb(cb)
1248{
1249 createThread(callbackThread, this);
1250}
1251
1252void Antagonizer::kill()
1253{
1254 Mutex::Autolock _l(mLock);
1255 mActive = false;
1256 mExit = true;
1257 mCondition.wait(mLock);
1258}
1259
1260int Antagonizer::callbackThread(void* user)
1261{
Steve Blockb8a80522011-12-20 16:23:08 +00001262 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1264 while (!p->mExit) {
1265 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001266 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267 p->mCb(p->mClient, 0, 0, 0);
1268 }
1269 usleep(interval);
1270 }
1271 Mutex::Autolock _l(p->mLock);
1272 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001273 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 return 0;
1275}
1276#endif
1277
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278#undef LOG_TAG
1279#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001280MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1281 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001282 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001283 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001284 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001285 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001286 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001287 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001288 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001289 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001290 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001291 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292 mLeftVolume = 1.0;
1293 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001294 mPlaybackRatePermille = 1000;
1295 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001296 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001297 mAuxEffectId = 0;
1298 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001300 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301}
1302
1303MediaPlayerService::AudioOutput::~AudioOutput()
1304{
1305 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001306 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307}
1308
1309void MediaPlayerService::AudioOutput::setMinBufferCount()
1310{
1311 char value[PROPERTY_VALUE_MAX];
1312 if (property_get("ro.kernel.qemu", value, 0)) {
1313 mIsOnEmulator = true;
1314 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1315 }
1316}
1317
1318bool MediaPlayerService::AudioOutput::isOnEmulator()
1319{
1320 setMinBufferCount();
1321 return mIsOnEmulator;
1322}
1323
1324int MediaPlayerService::AudioOutput::getMinBufferCount()
1325{
1326 setMinBufferCount();
1327 return mMinBufferCount;
1328}
1329
1330ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1331{
1332 if (mTrack == 0) return NO_INIT;
1333 return mTrack->frameCount() * frameSize();
1334}
1335
1336ssize_t MediaPlayerService::AudioOutput::frameCount() const
1337{
1338 if (mTrack == 0) return NO_INIT;
1339 return mTrack->frameCount();
1340}
1341
1342ssize_t MediaPlayerService::AudioOutput::channelCount() const
1343{
1344 if (mTrack == 0) return NO_INIT;
1345 return mTrack->channelCount();
1346}
1347
1348ssize_t MediaPlayerService::AudioOutput::frameSize() const
1349{
1350 if (mTrack == 0) return NO_INIT;
1351 return mTrack->frameSize();
1352}
1353
1354uint32_t MediaPlayerService::AudioOutput::latency () const
1355{
Eric Laurentdb354e52012-03-05 17:27:11 -08001356 if (mTrack == 0) return 0;
1357 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001358}
1359
1360float MediaPlayerService::AudioOutput::msecsPerFrame() const
1361{
1362 return mMsecsPerFrame;
1363}
1364
Marco Nelissen4110c102012-03-29 09:31:28 -07001365status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001366{
1367 if (mTrack == 0) return NO_INIT;
1368 return mTrack->getPosition(position);
1369}
1370
Lajos Molnar06ad1522014-08-28 07:27:44 -07001371status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1372{
1373 if (mTrack == 0) return NO_INIT;
1374 return mTrack->getTimestamp(ts);
1375}
1376
Marco Nelissen4110c102012-03-29 09:31:28 -07001377status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1378{
1379 if (mTrack == 0) return NO_INIT;
1380 *frameswritten = mBytesWritten / frameSize();
1381 return OK;
1382}
1383
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001384status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1385{
1386 if (mTrack == 0) return NO_INIT;
1387 return mTrack->setParameters(keyValuePairs);
1388}
1389
1390String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1391{
1392 if (mTrack == 0) return String8::empty();
1393 return mTrack->getParameters(keys);
1394}
1395
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001396void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1397 mAttributes = attributes;
1398}
1399
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001400void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1401{
1402 ALOGV("deleteRecycledTrack");
1403
1404 if (mRecycledTrack != 0) {
1405
1406 if (mCallbackData != NULL) {
1407 mCallbackData->setOutput(NULL);
1408 mCallbackData->endTrackSwitch();
1409 }
1410
1411 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1412 mRecycledTrack->flush();
1413 }
1414 // An offloaded track isn't flushed because the STREAM_END is reported
1415 // slightly prematurely to allow time for the gapless track switch
1416 // but this means that if we decide not to recycle the track there
1417 // could be a small amount of residual data still playing. We leave
1418 // AudioFlinger to drain the track.
1419
1420 mRecycledTrack.clear();
1421 delete mCallbackData;
1422 mCallbackData = NULL;
1423 close();
1424 }
1425}
1426
Andreas Huber20111aa2009-07-14 16:56:47 -07001427status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001428 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1429 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001430 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001431 audio_output_flags_t flags,
1432 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001433{
Andreas Huber20111aa2009-07-14 16:56:47 -07001434 mCallback = cb;
1435 mCallbackCookie = cookie;
1436
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437 // Check argument "bufferCount" against the mininum buffer count
1438 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001439 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001440 bufferCount = mMinBufferCount;
1441
1442 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001443 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1444 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001445 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001446 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001447 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001448
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001449 // offloading is only supported in callback mode for now.
1450 // offloadInfo must be present if offload flag is set
1451 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1452 ((cb == NULL) || (offloadInfo == NULL))) {
1453 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 }
1455
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001456 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1457 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1458 } else {
1459 uint32_t afSampleRate;
1460 size_t afFrameCount;
1461
1462 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1463 return NO_INIT;
1464 }
1465 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1466 return NO_INIT;
1467 }
1468
1469 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1470 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001471
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001472 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001473 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001474 if (0 == channelMask) {
1475 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1476 return NO_INIT;
1477 }
1478 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001479
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001480 // Check whether we can recycle the track
1481 bool reuse = false;
1482 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001483
Glenn Kasten2799d742013-05-30 14:33:29 -07001484 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001485 // check whether we are switching between two offloaded tracks
1486 bothOffloaded = (flags & mRecycledTrack->getFlags()
1487 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001488
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001489 // check if the existing track can be reused as-is, or if a new track needs to be created.
1490 reuse = true;
1491
Marco Nelissen67295b52012-06-11 14:52:53 -07001492 if ((mCallbackData == NULL && mCallback != NULL) ||
1493 (mCallbackData != NULL && mCallback == NULL)) {
1494 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1495 ALOGV("can't chain callback and write");
1496 reuse = false;
1497 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001498 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1499 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001500 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001501 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001502 reuse = false;
1503 } else if (flags != mFlags) {
1504 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1505 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001506 } else if (mRecycledTrack->format() != format) {
1507 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001508 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001509 } else {
1510 ALOGV("no track available to recycle");
1511 }
1512
1513 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1514
1515 // If we can't recycle and both tracks are offloaded
1516 // we must close the previous output before opening a new one
1517 if (bothOffloaded && !reuse) {
1518 ALOGV("both offloaded and not recycling");
1519 deleteRecycledTrack();
1520 }
1521
1522 sp<AudioTrack> t;
1523 CallbackData *newcbd = NULL;
1524
1525 // We don't attempt to create a new track if we are recycling an
1526 // offloaded track. But, if we are recycling a non-offloaded or we
1527 // are switching where one is offloaded and one isn't then we create
1528 // the new track in advance so that we can read additional stream info
1529
1530 if (!(reuse && bothOffloaded)) {
1531 ALOGV("creating new AudioTrack");
1532
1533 if (mCallback != NULL) {
1534 newcbd = new CallbackData(this);
1535 t = new AudioTrack(
1536 mStreamType,
1537 sampleRate,
1538 format,
1539 channelMask,
1540 frameCount,
1541 flags,
1542 CallbackWrapper,
1543 newcbd,
1544 0, // notification frames
1545 mSessionId,
1546 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001547 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001548 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001549 mPid,
1550 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001551 } else {
1552 t = new AudioTrack(
1553 mStreamType,
1554 sampleRate,
1555 format,
1556 channelMask,
1557 frameCount,
1558 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001559 NULL, // callback
1560 NULL, // user data
1561 0, // notification frames
1562 mSessionId,
1563 AudioTrack::TRANSFER_DEFAULT,
1564 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001565 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001566 mPid,
1567 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001568 }
1569
1570 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1571 ALOGE("Unable to create audio track");
1572 delete newcbd;
1573 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001574 } else {
1575 // successful AudioTrack initialization implies a legacy stream type was generated
1576 // from the audio attributes
1577 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001578 }
1579 }
1580
1581 if (reuse) {
1582 CHECK(mRecycledTrack != NULL);
1583
1584 if (!bothOffloaded) {
1585 if (mRecycledTrack->frameCount() != t->frameCount()) {
1586 ALOGV("framecount differs: %u/%u frames",
1587 mRecycledTrack->frameCount(), t->frameCount());
1588 reuse = false;
1589 }
1590 }
1591
Marco Nelissen67295b52012-06-11 14:52:53 -07001592 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001593 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001594 close();
1595 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001596 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001597 if (mCallbackData != NULL) {
1598 mCallbackData->setOutput(this);
1599 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001600 delete newcbd;
1601 return OK;
1602 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001603 }
1604
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001605 // we're not going to reuse the track, unblock and flush it
1606 // this was done earlier if both tracks are offloaded
1607 if (!bothOffloaded) {
1608 deleteRecycledTrack();
1609 }
1610
1611 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1612
Marco Nelissen67295b52012-06-11 14:52:53 -07001613 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001614 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001615 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001616
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001617 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001618 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001619 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001620 uint32_t pos;
1621 if (t->getPosition(&pos) == OK) {
1622 mBytesWritten = uint64_t(pos) * t->frameSize();
1623 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001624 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001625
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001626 status_t res = NO_ERROR;
1627 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1628 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1629 if (res == NO_ERROR) {
1630 t->setAuxEffectSendLevel(mSendLevel);
1631 res = t->attachAuxEffect(mAuxEffectId);
1632 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001633 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001634 ALOGV("open() DONE status %d", res);
1635 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001636}
1637
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001638status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001639{
Steve Block3856b092011-10-20 11:56:00 +01001640 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001641 if (mCallbackData != NULL) {
1642 mCallbackData->endTrackSwitch();
1643 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001644 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001646 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001647 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001649 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001650}
1651
Marco Nelissen6b74d672012-02-28 16:07:44 -08001652void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1653 mNextOutput = nextOutput;
1654}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001655
1656
Marco Nelissen6b74d672012-02-28 16:07:44 -08001657void MediaPlayerService::AudioOutput::switchToNextOutput() {
1658 ALOGV("switchToNextOutput");
1659 if (mNextOutput != NULL) {
1660 if (mCallbackData != NULL) {
1661 mCallbackData->beginTrackSwitch();
1662 }
1663 delete mNextOutput->mCallbackData;
1664 mNextOutput->mCallbackData = mCallbackData;
1665 mCallbackData = NULL;
1666 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001667 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001668 mNextOutput->mSampleRateHz = mSampleRateHz;
1669 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001670 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001671 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001672 }
1673}
1674
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001675ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1676{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001677 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001678
Steve Block3856b092011-10-20 11:56:00 +01001679 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001680 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001681 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001682 if (ret >= 0) {
1683 mBytesWritten += ret;
1684 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001685 return ret;
1686 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001687 return NO_INIT;
1688}
1689
1690void MediaPlayerService::AudioOutput::stop()
1691{
Steve Block3856b092011-10-20 11:56:00 +01001692 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001693 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001694}
1695
1696void MediaPlayerService::AudioOutput::flush()
1697{
Steve Block3856b092011-10-20 11:56:00 +01001698 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001699 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001700}
1701
1702void MediaPlayerService::AudioOutput::pause()
1703{
Steve Block3856b092011-10-20 11:56:00 +01001704 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001705 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001706}
1707
1708void MediaPlayerService::AudioOutput::close()
1709{
Steve Block3856b092011-10-20 11:56:00 +01001710 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001711 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001712}
1713
1714void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1715{
Steve Block3856b092011-10-20 11:56:00 +01001716 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001717 mLeftVolume = left;
1718 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001719 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001720 mTrack->setVolume(left, right);
1721 }
1722}
1723
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001724status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1725{
1726 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1727 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001728 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001729 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1730 } else {
1731 res = NO_INIT;
1732 }
1733 mPlaybackRatePermille = ratePermille;
1734 if (mSampleRateHz != 0) {
1735 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1736 }
1737 return res;
1738}
1739
Eric Laurent2beeb502010-07-16 07:43:46 -07001740status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1741{
Steve Block3856b092011-10-20 11:56:00 +01001742 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001743 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001744 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001745 return mTrack->setAuxEffectSendLevel(level);
1746 }
1747 return NO_ERROR;
1748}
1749
1750status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1751{
Steve Block3856b092011-10-20 11:56:00 +01001752 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001753 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001754 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001755 return mTrack->attachAuxEffect(effectId);
1756 }
1757 return NO_ERROR;
1758}
1759
Andreas Huber20111aa2009-07-14 16:56:47 -07001760// static
1761void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001762 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001763 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001764 CallbackData *data = (CallbackData*)cookie;
1765 data->lock();
1766 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001767 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001768 if (me == NULL) {
1769 // no output set, likely because the track was scheduled to be reused
1770 // by another player, but the format turned out to be incompatible.
1771 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001772 if (buffer != NULL) {
1773 buffer->size = 0;
1774 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001775 return;
1776 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001777
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001778 switch(event) {
1779 case AudioTrack::EVENT_MORE_DATA: {
1780 size_t actualSize = (*me->mCallback)(
1781 me, buffer->raw, buffer->size, me->mCallbackCookie,
1782 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001783
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301784 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1785 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001786 // We've reached EOS but the audio track is not stopped yet,
1787 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001788
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001789 memset(buffer->raw, 0, buffer->size);
1790 actualSize = buffer->size;
1791 }
1792
1793 buffer->size = actualSize;
1794 } break;
1795
1796
1797 case AudioTrack::EVENT_STREAM_END:
1798 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1799 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1800 me->mCallbackCookie, CB_EVENT_STREAM_END);
1801 break;
1802
1803 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1804 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1805 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1806 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1807 break;
1808
1809 default:
1810 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001811 }
1812
Marco Nelissen6b74d672012-02-28 16:07:44 -08001813 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001814}
1815
Marco Nelissen4110c102012-03-29 09:31:28 -07001816int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001817{
1818 return mSessionId;
1819}
1820
Eric Laurent6f59db12013-07-26 17:16:50 -07001821uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1822{
1823 if (mTrack == 0) return 0;
1824 return mTrack->getSampleRate();
1825}
1826
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001827////////////////////////////////////////////////////////////////////////////////
1828
1829struct CallbackThread : public Thread {
1830 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1831 MediaPlayerBase::AudioSink::AudioCallback cb,
1832 void *cookie);
1833
1834protected:
1835 virtual ~CallbackThread();
1836
1837 virtual bool threadLoop();
1838
1839private:
1840 wp<MediaPlayerBase::AudioSink> mSink;
1841 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1842 void *mCookie;
1843 void *mBuffer;
1844 size_t mBufferSize;
1845
1846 CallbackThread(const CallbackThread &);
1847 CallbackThread &operator=(const CallbackThread &);
1848};
1849
1850CallbackThread::CallbackThread(
1851 const wp<MediaPlayerBase::AudioSink> &sink,
1852 MediaPlayerBase::AudioSink::AudioCallback cb,
1853 void *cookie)
1854 : mSink(sink),
1855 mCallback(cb),
1856 mCookie(cookie),
1857 mBuffer(NULL),
1858 mBufferSize(0) {
1859}
1860
1861CallbackThread::~CallbackThread() {
1862 if (mBuffer) {
1863 free(mBuffer);
1864 mBuffer = NULL;
1865 }
1866}
1867
1868bool CallbackThread::threadLoop() {
1869 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1870 if (sink == NULL) {
1871 return false;
1872 }
1873
1874 if (mBuffer == NULL) {
1875 mBufferSize = sink->bufferSize();
1876 mBuffer = malloc(mBufferSize);
1877 }
1878
1879 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001880 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1881 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001882
1883 if (actualSize > 0) {
1884 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001885 // Could return false on sink->write() error or short count.
1886 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001887 }
1888
1889 return true;
1890}
1891
1892////////////////////////////////////////////////////////////////////////////////
1893
Gloria Wang7cf180c2011-02-19 18:37:57 -08001894void MediaPlayerService::addBatteryData(uint32_t params)
1895{
1896 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001897
1898 int32_t time = systemTime() / 1000000L;
1899
1900 // change audio output devices. This notification comes from AudioFlinger
1901 if ((params & kBatteryDataSpeakerOn)
1902 || (params & kBatteryDataOtherAudioDeviceOn)) {
1903
1904 int deviceOn[NUM_AUDIO_DEVICES];
1905 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1906 deviceOn[i] = 0;
1907 }
1908
1909 if ((params & kBatteryDataSpeakerOn)
1910 && (params & kBatteryDataOtherAudioDeviceOn)) {
1911 deviceOn[SPEAKER_AND_OTHER] = 1;
1912 } else if (params & kBatteryDataSpeakerOn) {
1913 deviceOn[SPEAKER] = 1;
1914 } else {
1915 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1916 }
1917
1918 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1919 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1920
1921 if (mBatteryAudio.refCount > 0) { // if playing audio
1922 if (!deviceOn[i]) {
1923 mBatteryAudio.lastTime[i] += time;
1924 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1925 mBatteryAudio.lastTime[i] = 0;
1926 } else {
1927 mBatteryAudio.lastTime[i] = 0 - time;
1928 }
1929 }
1930
1931 mBatteryAudio.deviceOn[i] = deviceOn[i];
1932 }
1933 }
1934 return;
1935 }
1936
Marco Nelissenb7848f12014-12-04 08:57:56 -08001937 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08001938 if (params & kBatteryDataAudioFlingerStart) {
1939 // record the start time only if currently no other audio
1940 // is being played
1941 if (mBatteryAudio.refCount == 0) {
1942 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1943 if (mBatteryAudio.deviceOn[i]) {
1944 mBatteryAudio.lastTime[i] -= time;
1945 }
1946 }
1947 }
1948
1949 mBatteryAudio.refCount ++;
1950 return;
1951
1952 } else if (params & kBatteryDataAudioFlingerStop) {
1953 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001954 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001955 return;
1956 }
1957
1958 // record the stop time only if currently this is the only
1959 // audio being played
1960 if (mBatteryAudio.refCount == 1) {
1961 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1962 if (mBatteryAudio.deviceOn[i]) {
1963 mBatteryAudio.lastTime[i] += time;
1964 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1965 mBatteryAudio.lastTime[i] = 0;
1966 }
1967 }
1968 }
1969
1970 mBatteryAudio.refCount --;
1971 return;
1972 }
1973
Gloria Wang7cf180c2011-02-19 18:37:57 -08001974 int uid = IPCThreadState::self()->getCallingUid();
1975 if (uid == AID_MEDIA) {
1976 return;
1977 }
1978 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001979
1980 if (index < 0) { // create a new entry for this UID
1981 BatteryUsageInfo info;
1982 info.audioTotalTime = 0;
1983 info.videoTotalTime = 0;
1984 info.audioLastTime = 0;
1985 info.videoLastTime = 0;
1986 info.refCount = 0;
1987
Gloria Wang9ee159b2011-02-24 14:51:45 -08001988 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001989 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001990 return;
1991 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001992 }
1993
1994 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1995
1996 if (params & kBatteryDataCodecStarted) {
1997 if (params & kBatteryDataTrackAudio) {
1998 info.audioLastTime -= time;
1999 info.refCount ++;
2000 }
2001 if (params & kBatteryDataTrackVideo) {
2002 info.videoLastTime -= time;
2003 info.refCount ++;
2004 }
2005 } else {
2006 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002007 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002008 return;
2009 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002010 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002011 mBatteryData.removeItem(uid);
2012 return;
2013 }
2014
2015 if (params & kBatteryDataTrackAudio) {
2016 info.audioLastTime += time;
2017 info.refCount --;
2018 }
2019 if (params & kBatteryDataTrackVideo) {
2020 info.videoLastTime += time;
2021 info.refCount --;
2022 }
2023
2024 // no stream is being played by this UID
2025 if (info.refCount == 0) {
2026 info.audioTotalTime += info.audioLastTime;
2027 info.audioLastTime = 0;
2028 info.videoTotalTime += info.videoLastTime;
2029 info.videoLastTime = 0;
2030 }
2031 }
2032}
2033
2034status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2035 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002036
2037 // audio output devices usage
2038 int32_t time = systemTime() / 1000000L; //in ms
2039 int32_t totalTime;
2040
2041 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2042 totalTime = mBatteryAudio.totalTime[i];
2043
2044 if (mBatteryAudio.deviceOn[i]
2045 && (mBatteryAudio.lastTime[i] != 0)) {
2046 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2047 totalTime += tmpTime;
2048 }
2049
2050 reply->writeInt32(totalTime);
2051 // reset the total time
2052 mBatteryAudio.totalTime[i] = 0;
2053 }
2054
2055 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002056 BatteryUsageInfo info;
2057 int size = mBatteryData.size();
2058
2059 reply->writeInt32(size);
2060 int i = 0;
2061
2062 while (i < size) {
2063 info = mBatteryData.valueAt(i);
2064
2065 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2066 reply->writeInt32(info.audioTotalTime);
2067 reply->writeInt32(info.videoTotalTime);
2068
2069 info.audioTotalTime = 0;
2070 info.videoTotalTime = 0;
2071
2072 // remove the UID entry where no stream is being played
2073 if (info.refCount <= 0) {
2074 mBatteryData.removeItemsAt(i);
2075 size --;
2076 i --;
2077 }
2078 i++;
2079 }
2080 return NO_ERROR;
2081}
nikoa64c8c72009-07-20 15:07:26 -07002082} // namespace android