blob: 3e49e33c62b4bd601cd98ede5de97154bcb7d02b [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");
Warren Rehmaned849b82015-03-05 15:26:12 +0900293 // use checkService() to avoid blocking if service is not up yet
Chong Zhang0b30fd42014-07-23 14:46:05 -0700294 sp<IBatteryStats> batteryStats =
Warren Rehmaned849b82015-03-05 15:26:12 +0900295 interface_cast<IBatteryStats>(sm->checkService(name));
Chong Zhang0b30fd42014-07-23 14:46:05 -0700296 if (batteryStats != NULL) {
297 batteryStats->noteResetVideo();
298 batteryStats->noteResetAudio();
299 }
300 }
301
John Grossman44a7e422012-06-21 17:29:24 -0700302 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303}
304
305MediaPlayerService::~MediaPlayerService()
306{
Steve Block3856b092011-10-20 11:56:00 +0100307 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308}
309
Glenn Kastenf37971f2012-02-03 11:06:53 -0800310sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800312 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700313 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
314 wp<MediaRecorderClient> w = recorder;
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return recorder;
319}
320
Gloria Wangdac6a312009-10-29 15:46:37 -0700321void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
322{
323 Mutex::Autolock lock(mLock);
324 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800330 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 return retriever;
334}
335
Glenn Kastenf37971f2012-02-03 11:06:53 -0800336sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100337 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800339 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700341
342 sp<Client> c = new Client(
343 this, pid, connId, client, audioSessionId,
344 IPCThreadState::self()->getCallingUid());
345
Steve Block3856b092011-10-20 11:56:00 +0100346 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100347 IPCThreadState::self()->getCallingUid());
348
349 wp<Client> w = c;
350 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 Mutex::Autolock lock(mLock);
352 mClients.add(w);
353 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800354 return c;
355}
356
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700357sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
358 return MediaCodecList::getLocalInstance();
359}
360
Andreas Huber318ad9c2009-10-15 13:46:54 -0700361sp<IOMX> MediaPlayerService::getOMX() {
362 Mutex::Autolock autoLock(mLock);
363
364 if (mOMX.get() == NULL) {
365 mOMX = new OMX;
366 }
367
368 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700369}
370
Andreas Hubered3e3e02012-03-26 11:13:27 -0700371sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700372 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700373}
374
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800375sp<IDrm> MediaPlayerService::makeDrm() {
376 return new Drm;
377}
378
Andreas Huber279dcd82013-01-30 10:41:25 -0800379sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
380 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700381}
382
Jeff Brown2013a542012-09-04 21:38:42 -0700383sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
384 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700385 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
386 return NULL;
387 }
388
Jeff Brownced24b32012-09-05 17:48:03 -0700389 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700390}
391
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
393{
394 const size_t SIZE = 256;
395 char buffer[SIZE];
396 String8 result;
397
398 result.append(" AudioOutput\n");
399 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
400 mStreamType, mLeftVolume, mRightVolume);
401 result.append(buffer);
402 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800403 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700405 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
406 mAuxEffectId, mSendLevel);
407 result.append(buffer);
408
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 ::write(fd, result.string(), result.size());
410 if (mTrack != 0) {
411 mTrack->dump(fd, args);
412 }
413 return NO_ERROR;
414}
415
416status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
417{
418 const size_t SIZE = 256;
419 char buffer[SIZE];
420 String8 result;
421 result.append(" Client\n");
422 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
423 mPid, mConnId, mStatus, mLoop?"true": "false");
424 result.append(buffer);
425 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700426 if (mPlayer != NULL) {
427 mPlayer->dump(fd, args);
428 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 if (mAudioOutput != 0) {
430 mAudioOutput->dump(fd, args);
431 }
432 write(fd, "\n", 1);
433 return NO_ERROR;
434}
435
Marco Nelissenf09611f2015-02-13 14:12:42 -0800436/**
437 * The only arguments this understands right now are -c, -von and -voff,
438 * which are parsed by ALooperRoster::dump()
439 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
441{
442 const size_t SIZE = 256;
443 char buffer[SIZE];
444 String8 result;
445 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
446 snprintf(buffer, SIZE, "Permission Denial: "
447 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
448 IPCThreadState::self()->getCallingPid(),
449 IPCThreadState::self()->getCallingUid());
450 result.append(buffer);
451 } else {
452 Mutex::Autolock lock(mLock);
453 for (int i = 0, n = mClients.size(); i < n; ++i) {
454 sp<Client> c = mClients[i].promote();
455 if (c != 0) c->dump(fd, args);
456 }
James Dongb9141222010-07-08 11:16:11 -0700457 if (mMediaRecorderClients.size() == 0) {
458 result.append(" No media recorder client\n\n");
459 } else {
460 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
461 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700462 if (c != 0) {
463 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
464 result.append(buffer);
465 write(fd, result.string(), result.size());
466 result = "\n";
467 c->dump(fd, args);
468 }
James Dongb9141222010-07-08 11:16:11 -0700469 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700470 }
471
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800473 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474 FILE *f = fopen(buffer, "r");
475 if (f) {
476 while (!feof(f)) {
477 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700478 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700480 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800481 strstr(buffer, " /system/media/")) {
482 result.append(" ");
483 result.append(buffer);
484 }
485 }
486 fclose(f);
487 } else {
488 result.append("couldn't open ");
489 result.append(buffer);
490 result.append("\n");
491 }
492
Marco Nelissenf09611f2015-02-13 14:12:42 -0800493 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494 DIR *d = opendir(buffer);
495 if (d) {
496 struct dirent *ent;
497 while((ent = readdir(d)) != NULL) {
498 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800499 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500 struct stat s;
501 if (lstat(buffer, &s) == 0) {
502 if ((s.st_mode & S_IFMT) == S_IFLNK) {
503 char linkto[256];
504 int len = readlink(buffer, linkto, sizeof(linkto));
505 if(len > 0) {
506 if(len > 255) {
507 linkto[252] = '.';
508 linkto[253] = '.';
509 linkto[254] = '.';
510 linkto[255] = 0;
511 } else {
512 linkto[len] = 0;
513 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700514 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700516 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517 strstr(linkto, "/system/media/") == linkto) {
518 result.append(" ");
519 result.append(buffer);
520 result.append(" -> ");
521 result.append(linkto);
522 result.append("\n");
523 }
524 }
525 } else {
526 result.append(" unexpected type for ");
527 result.append(buffer);
528 result.append("\n");
529 }
530 }
531 }
532 }
533 closedir(d);
534 } else {
535 result.append("couldn't open ");
536 result.append(buffer);
537 result.append("\n");
538 }
539
Marco Nelissenf09611f2015-02-13 14:12:42 -0800540 gLooperRoster.dump(fd, args);
541
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542 bool dumpMem = false;
543 for (size_t i = 0; i < args.size(); i++) {
544 if (args[i] == String16("-m")) {
545 dumpMem = true;
546 }
547 }
548 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700549 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551 }
552 write(fd, result.string(), result.size());
553 return NO_ERROR;
554}
555
556void MediaPlayerService::removeClient(wp<Client> client)
557{
558 Mutex::Autolock lock(mLock);
559 mClients.remove(client);
560}
561
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700562MediaPlayerService::Client::Client(
563 const sp<MediaPlayerService>& service, pid_t pid,
564 int32_t connId, const sp<IMediaPlayerClient>& client,
565 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566{
Steve Block3856b092011-10-20 11:56:00 +0100567 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 mPid = pid;
569 mConnId = connId;
570 mService = service;
571 mClient = client;
572 mLoop = false;
573 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700574 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700575 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800576 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700577 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700578
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000580 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 mAntagonizer = new Antagonizer(notify, this);
582#endif
583}
584
585MediaPlayerService::Client::~Client()
586{
Steve Block3856b092011-10-20 11:56:00 +0100587 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 mAudioOutput.clear();
589 wp<Client> client(this);
590 disconnect();
591 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700592 if (mAudioAttributes != NULL) {
593 free(mAudioAttributes);
594 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595}
596
597void MediaPlayerService::Client::disconnect()
598{
Steve Block3856b092011-10-20 11:56:00 +0100599 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600 // grab local reference and clear main reference to prevent future
601 // access to object
602 sp<MediaPlayerBase> p;
603 {
604 Mutex::Autolock l(mLock);
605 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800606 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700608
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609 mPlayer.clear();
610
611 // clear the notification to prevent callbacks to dead client
612 // and reset the player. We assume the player will serialize
613 // access to itself if necessary.
614 if (p != 0) {
615 p->setNotifyCallback(0, 0);
616#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000617 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 mAntagonizer->kill();
619#endif
620 p->reset();
621 }
622
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700623 disconnectNativeWindow();
624
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625 IPCThreadState::self()->flushCommands();
626}
627
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
629{
630 // determine if we have the right player type
631 sp<MediaPlayerBase> p = mPlayer;
632 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100633 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 p.clear();
635 }
636 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700637 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700639
Jason Simmonsdb29e522011-08-12 13:46:55 -0700640 if (p != NULL) {
641 p->setUID(mUID);
642 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700643
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644 return p;
645}
646
John Grossmanc795b642012-02-22 15:38:35 -0800647sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
648 player_type playerType)
649{
650 ALOGV("player type = %d", playerType);
651
652 // create the right type of player
653 sp<MediaPlayerBase> p = createPlayer(playerType);
654 if (p == NULL) {
655 return p;
656 }
657
658 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800659 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700660 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800661 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
662 }
663
664 return p;
665}
666
667void MediaPlayerService::Client::setDataSource_post(
668 const sp<MediaPlayerBase>& p,
669 status_t status)
670{
671 ALOGV(" setDataSource");
672 mStatus = status;
673 if (mStatus != OK) {
674 ALOGE(" error: %d", mStatus);
675 return;
676 }
677
678 // Set the re-transmission endpoint if one was chosen.
679 if (mRetransmitEndpointValid) {
680 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
681 if (mStatus != NO_ERROR) {
682 ALOGE("setRetransmitEndpoint error: %d", mStatus);
683 }
684 }
685
686 if (mStatus == OK) {
687 mPlayer = p;
688 }
689}
690
Andreas Huber2db84552010-01-28 11:19:57 -0800691status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800692 const sp<IMediaHTTPService> &httpService,
693 const char *url,
694 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695{
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697 if (url == NULL)
698 return UNKNOWN_ERROR;
699
Dave Burked681bbb2011-08-30 14:39:17 +0100700 if ((strncmp(url, "http://", 7) == 0) ||
701 (strncmp(url, "https://", 8) == 0) ||
702 (strncmp(url, "rtsp://", 7) == 0)) {
703 if (!checkPermission("android.permission.INTERNET")) {
704 return PERMISSION_DENIED;
705 }
706 }
707
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 if (strncmp(url, "content://", 10) == 0) {
709 // get a filedescriptor for the content Uri and
710 // pass it to the setDataSource(fd) method
711
712 String16 url16(url);
713 int fd = android::openContentProviderFile(url16);
714 if (fd < 0)
715 {
Steve Block29357bc2012-01-06 19:20:56 +0000716 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800717 return UNKNOWN_ERROR;
718 }
719 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
720 close(fd);
721 return mStatus;
722 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700723 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800724 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
725 if (p == NULL) {
726 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800727 }
728
Andreas Huber1b86fe02014-01-29 11:13:26 -0800729 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730 return mStatus;
731 }
732}
733
734status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
735{
Steve Block3856b092011-10-20 11:56:00 +0100736 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800737 struct stat sb;
738 int ret = fstat(fd, &sb);
739 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000740 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741 return UNKNOWN_ERROR;
742 }
743
Steve Block3856b092011-10-20 11:56:00 +0100744 ALOGV("st_dev = %llu", sb.st_dev);
745 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700746 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
747 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749
750 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000751 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800752 ::close(fd);
753 return UNKNOWN_ERROR;
754 }
755 if (offset + length > sb.st_size) {
756 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800758 }
759
John Grossman44a7e422012-06-21 17:29:24 -0700760 player_type playerType = MediaPlayerFactory::getPlayerType(this,
761 fd,
762 offset,
763 length);
John Grossmanc795b642012-02-22 15:38:35 -0800764 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
765 if (p == NULL) {
766 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800767 }
768
769 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800770 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771 return mStatus;
772}
773
Andreas Hubere2b10282010-11-23 11:41:34 -0800774status_t MediaPlayerService::Client::setDataSource(
775 const sp<IStreamSource> &source) {
776 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700777 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800778 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800779 if (p == NULL) {
780 return NO_INIT;
781 }
782
Andreas Hubere2b10282010-11-23 11:41:34 -0800783 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800784 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800785 return mStatus;
786}
787
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700788void MediaPlayerService::Client::disconnectNativeWindow() {
789 if (mConnectedWindow != NULL) {
790 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
791 NATIVE_WINDOW_API_MEDIA);
792
793 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000794 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700795 strerror(-err), err);
796 }
797 }
798 mConnectedWindow.clear();
799}
800
Glenn Kasten11731182011-02-08 17:26:17 -0800801status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800802 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800803{
Andy McFadden484566c2012-12-18 09:46:54 -0800804 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800805 sp<MediaPlayerBase> p = getPlayer();
806 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700807
Marco Nelissenf8880202014-11-14 07:58:25 -0800808 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700809 if (mConnectedWindowBinder == binder) {
810 return OK;
811 }
812
813 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800814 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700815 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700816 status_t err = native_window_api_connect(anw.get(),
817 NATIVE_WINDOW_API_MEDIA);
818
819 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000820 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700821 // Note that we must do the reset before disconnecting from the ANW.
822 // Otherwise queue/dequeue calls could be made on the disconnected
823 // ANW, which may result in errors.
824 reset();
825
826 disconnectNativeWindow();
827
828 return err;
829 }
830 }
831
Andy McFadden484566c2012-12-18 09:46:54 -0800832 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700833 // disconnecting the old one. Otherwise queue/dequeue calls could be made
834 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800835 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700836
837 disconnectNativeWindow();
838
839 mConnectedWindow = anw;
840
841 if (err == OK) {
842 mConnectedWindowBinder = binder;
843 } else {
844 disconnectNativeWindow();
845 }
846
847 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800848}
849
Nicolas Catania1d187f12009-05-12 23:25:55 -0700850status_t MediaPlayerService::Client::invoke(const Parcel& request,
851 Parcel *reply)
852{
853 sp<MediaPlayerBase> p = getPlayer();
854 if (p == NULL) return UNKNOWN_ERROR;
855 return p->invoke(request, reply);
856}
857
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700858// This call doesn't need to access the native player.
859status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
860{
861 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700862 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700863
Nicolas Catania48290382009-07-10 13:53:06 -0700864 if (unmarshallFilter(filter, &allow, &status) &&
865 unmarshallFilter(filter, &drop, &status)) {
866 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700867
868 mMetadataAllow = allow;
869 mMetadataDrop = drop;
870 }
871 return status;
872}
873
Nicolas Catania48290382009-07-10 13:53:06 -0700874status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700875 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700876{
nikoa64c8c72009-07-20 15:07:26 -0700877 sp<MediaPlayerBase> player = getPlayer();
878 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700879
nikod608a812009-07-16 16:39:53 -0700880 status_t status;
881 // Placeholder for the return code, updated by the caller.
882 reply->writeInt32(-1);
883
nikoa64c8c72009-07-20 15:07:26 -0700884 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700885
886 // We don't block notifications while we fetch the data. We clear
887 // mMetadataUpdated first so we don't lose notifications happening
888 // during the rest of this call.
889 {
890 Mutex::Autolock lock(mLock);
891 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700892 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700893 }
894 mMetadataUpdated.clear();
895 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700896
nikoa64c8c72009-07-20 15:07:26 -0700897 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700898
nikoa64c8c72009-07-20 15:07:26 -0700899 metadata.appendHeader();
900 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700901
902 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700903 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000904 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700905 return status;
906 }
907
908 // FIXME: Implement filtering on the result. Not critical since
909 // filtering takes place on the update notifications already. This
910 // would be when all the metadata are fetch and a filter is set.
911
nikod608a812009-07-16 16:39:53 -0700912 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700913 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700914 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700915}
916
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800917status_t MediaPlayerService::Client::prepareAsync()
918{
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800920 sp<MediaPlayerBase> p = getPlayer();
921 if (p == 0) return UNKNOWN_ERROR;
922 status_t ret = p->prepareAsync();
923#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000924 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 if (ret == NO_ERROR) mAntagonizer->start();
926#endif
927 return ret;
928}
929
930status_t MediaPlayerService::Client::start()
931{
Steve Block3856b092011-10-20 11:56:00 +0100932 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800933 sp<MediaPlayerBase> p = getPlayer();
934 if (p == 0) return UNKNOWN_ERROR;
935 p->setLooping(mLoop);
936 return p->start();
937}
938
939status_t MediaPlayerService::Client::stop()
940{
Steve Block3856b092011-10-20 11:56:00 +0100941 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942 sp<MediaPlayerBase> p = getPlayer();
943 if (p == 0) return UNKNOWN_ERROR;
944 return p->stop();
945}
946
947status_t MediaPlayerService::Client::pause()
948{
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950 sp<MediaPlayerBase> p = getPlayer();
951 if (p == 0) return UNKNOWN_ERROR;
952 return p->pause();
953}
954
955status_t MediaPlayerService::Client::isPlaying(bool* state)
956{
957 *state = false;
958 sp<MediaPlayerBase> p = getPlayer();
959 if (p == 0) return UNKNOWN_ERROR;
960 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100961 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 return NO_ERROR;
963}
964
Wei Jia98160162015-02-04 17:01:11 -0800965status_t MediaPlayerService::Client::setPlaybackRate(float rate)
966{
967 ALOGV("[%d] setPlaybackRate(%f)", mConnId, rate);
968 sp<MediaPlayerBase> p = getPlayer();
969 if (p == 0) return UNKNOWN_ERROR;
970 return p->setPlaybackRate(rate);
971}
972
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800973status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
974{
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 sp<MediaPlayerBase> p = getPlayer();
977 if (p == 0) return UNKNOWN_ERROR;
978 status_t ret = p->getCurrentPosition(msec);
979 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000982 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800983 }
984 return ret;
985}
986
987status_t MediaPlayerService::Client::getDuration(int *msec)
988{
Steve Block3856b092011-10-20 11:56:00 +0100989 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800990 sp<MediaPlayerBase> p = getPlayer();
991 if (p == 0) return UNKNOWN_ERROR;
992 status_t ret = p->getDuration(msec);
993 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100994 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000996 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800997 }
998 return ret;
999}
1000
Marco Nelissen6b74d672012-02-28 16:07:44 -08001001status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1002 ALOGV("setNextPlayer");
1003 Mutex::Autolock l(mLock);
1004 sp<Client> c = static_cast<Client*>(player.get());
1005 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001006
1007 if (c != NULL) {
1008 if (mAudioOutput != NULL) {
1009 mAudioOutput->setNextOutput(c->mAudioOutput);
1010 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1011 ALOGE("no current audio output");
1012 }
1013
1014 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1015 mPlayer->setNextPlayer(mNextClient->getPlayer());
1016 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001017 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001018
Marco Nelissen6b74d672012-02-28 16:07:44 -08001019 return OK;
1020}
1021
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001022status_t MediaPlayerService::Client::seekTo(int msec)
1023{
Steve Block3856b092011-10-20 11:56:00 +01001024 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001025 sp<MediaPlayerBase> p = getPlayer();
1026 if (p == 0) return UNKNOWN_ERROR;
1027 return p->seekTo(msec);
1028}
1029
1030status_t MediaPlayerService::Client::reset()
1031{
Steve Block3856b092011-10-20 11:56:00 +01001032 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001033 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001034 sp<MediaPlayerBase> p = getPlayer();
1035 if (p == 0) return UNKNOWN_ERROR;
1036 return p->reset();
1037}
1038
Glenn Kastenfff6d712012-01-12 16:38:12 -08001039status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001040{
Steve Block3856b092011-10-20 11:56:00 +01001041 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001042 // TODO: for hardware output, call player instead
1043 Mutex::Autolock l(mLock);
1044 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1045 return NO_ERROR;
1046}
1047
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001048status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1049{
1050 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1051 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1052 unmarshallAudioAttributes(parcel, mAudioAttributes);
1053
1054 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1055 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1056 mAudioAttributes->tags);
1057
1058 if (mAudioOutput != 0) {
1059 mAudioOutput->setAudioAttributes(mAudioAttributes);
1060 }
1061 return NO_ERROR;
1062}
1063
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001064status_t MediaPlayerService::Client::setLooping(int loop)
1065{
Steve Block3856b092011-10-20 11:56:00 +01001066 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001067 mLoop = loop;
1068 sp<MediaPlayerBase> p = getPlayer();
1069 if (p != 0) return p->setLooping(loop);
1070 return NO_ERROR;
1071}
1072
1073status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1074{
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001076
1077 // for hardware output, call player instead
1078 sp<MediaPlayerBase> p = getPlayer();
1079 {
1080 Mutex::Autolock l(mLock);
1081 if (p != 0 && p->hardwareOutput()) {
1082 MediaPlayerHWInterface* hwp =
1083 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1084 return hwp->setVolume(leftVolume, rightVolume);
1085 } else {
1086 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1087 return NO_ERROR;
1088 }
1089 }
1090
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001091 return NO_ERROR;
1092}
1093
Eric Laurent2beeb502010-07-16 07:43:46 -07001094status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1095{
Steve Block3856b092011-10-20 11:56:00 +01001096 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001097 Mutex::Autolock l(mLock);
1098 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1099 return NO_ERROR;
1100}
1101
1102status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1103{
Steve Block3856b092011-10-20 11:56:00 +01001104 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001105 Mutex::Autolock l(mLock);
1106 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1107 return NO_ERROR;
1108}
Nicolas Catania48290382009-07-10 13:53:06 -07001109
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001110status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001111 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001112 switch (key) {
1113 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1114 {
1115 Mutex::Autolock l(mLock);
1116 return setAudioAttributes_l(request);
1117 }
1118 default:
1119 sp<MediaPlayerBase> p = getPlayer();
1120 if (p == 0) { return UNKNOWN_ERROR; }
1121 return p->setParameter(key, request);
1122 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001123}
1124
1125status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001127 sp<MediaPlayerBase> p = getPlayer();
1128 if (p == 0) return UNKNOWN_ERROR;
1129 return p->getParameter(key, reply);
1130}
1131
John Grossmanc795b642012-02-22 15:38:35 -08001132status_t MediaPlayerService::Client::setRetransmitEndpoint(
1133 const struct sockaddr_in* endpoint) {
1134
1135 if (NULL != endpoint) {
1136 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1137 uint16_t p = ntohs(endpoint->sin_port);
1138 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1139 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1140 } else {
1141 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1142 }
1143
1144 sp<MediaPlayerBase> p = getPlayer();
1145
1146 // Right now, the only valid time to set a retransmit endpoint is before
1147 // player selection has been made (since the presence or absence of a
1148 // retransmit endpoint is going to determine which player is selected during
1149 // setDataSource).
1150 if (p != 0) return INVALID_OPERATION;
1151
1152 if (NULL != endpoint) {
1153 mRetransmitEndpoint = *endpoint;
1154 mRetransmitEndpointValid = true;
1155 } else {
1156 mRetransmitEndpointValid = false;
1157 }
1158
1159 return NO_ERROR;
1160}
1161
John Grossman44a7e422012-06-21 17:29:24 -07001162status_t MediaPlayerService::Client::getRetransmitEndpoint(
1163 struct sockaddr_in* endpoint)
1164{
1165 if (NULL == endpoint)
1166 return BAD_VALUE;
1167
1168 sp<MediaPlayerBase> p = getPlayer();
1169
1170 if (p != NULL)
1171 return p->getRetransmitEndpoint(endpoint);
1172
1173 if (!mRetransmitEndpointValid)
1174 return NO_INIT;
1175
1176 *endpoint = mRetransmitEndpoint;
1177
1178 return NO_ERROR;
1179}
1180
Gloria Wangb483c472011-04-11 17:23:27 -07001181void MediaPlayerService::Client::notify(
1182 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001183{
1184 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001185 if (client == NULL) {
1186 return;
1187 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001188
James Dongb8a98252012-08-26 16:13:03 -07001189 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001190 {
1191 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001192 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001193 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001194 if (client->mAudioOutput != NULL)
1195 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001196 client->mNextClient->start();
1197 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1198 }
1199 }
1200
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001201 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001202 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001203 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001204
1205 if(client->shouldDropMetadata(metadata_type)) {
1206 return;
1207 }
1208
1209 // Update the list of metadata that have changed. getMetadata
1210 // also access mMetadataUpdated and clears it.
1211 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001212 }
James Dongb8a98252012-08-26 16:13:03 -07001213
1214 if (c != NULL) {
1215 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1216 c->notify(msg, ext1, ext2, obj);
1217 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001218}
1219
Nicolas Catania48290382009-07-10 13:53:06 -07001220
nikoa64c8c72009-07-20 15:07:26 -07001221bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001222{
Nicolas Catania48290382009-07-10 13:53:06 -07001223 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001224
Nicolas Catania48290382009-07-10 13:53:06 -07001225 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001226 return true;
1227 }
1228
Nicolas Catania48290382009-07-10 13:53:06 -07001229 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001230 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001231 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001232 return true;
1233 }
1234}
1235
Nicolas Catania48290382009-07-10 13:53:06 -07001236
nikoa64c8c72009-07-20 15:07:26 -07001237void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001238 Mutex::Autolock lock(mLock);
1239 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1240 mMetadataUpdated.add(metadata_type);
1241 }
1242}
1243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001244#if CALLBACK_ANTAGONIZER
1245const int Antagonizer::interval = 10000; // 10 msecs
1246
1247Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1248 mExit(false), mActive(false), mClient(client), mCb(cb)
1249{
1250 createThread(callbackThread, this);
1251}
1252
1253void Antagonizer::kill()
1254{
1255 Mutex::Autolock _l(mLock);
1256 mActive = false;
1257 mExit = true;
1258 mCondition.wait(mLock);
1259}
1260
1261int Antagonizer::callbackThread(void* user)
1262{
Steve Blockb8a80522011-12-20 16:23:08 +00001263 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1265 while (!p->mExit) {
1266 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001267 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 p->mCb(p->mClient, 0, 0, 0);
1269 }
1270 usleep(interval);
1271 }
1272 Mutex::Autolock _l(p->mLock);
1273 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001274 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 return 0;
1276}
1277#endif
1278
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279#undef LOG_TAG
1280#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001281MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1282 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001283 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001284 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001285 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001286 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001287 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001288 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001289 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001290 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001291 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001292 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 mLeftVolume = 1.0;
1294 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001295 mPlaybackRatePermille = 1000;
1296 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001298 mAuxEffectId = 0;
1299 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001301 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302}
1303
1304MediaPlayerService::AudioOutput::~AudioOutput()
1305{
1306 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001307 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001308}
1309
1310void MediaPlayerService::AudioOutput::setMinBufferCount()
1311{
1312 char value[PROPERTY_VALUE_MAX];
1313 if (property_get("ro.kernel.qemu", value, 0)) {
1314 mIsOnEmulator = true;
1315 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1316 }
1317}
1318
1319bool MediaPlayerService::AudioOutput::isOnEmulator()
1320{
1321 setMinBufferCount();
1322 return mIsOnEmulator;
1323}
1324
1325int MediaPlayerService::AudioOutput::getMinBufferCount()
1326{
1327 setMinBufferCount();
1328 return mMinBufferCount;
1329}
1330
1331ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1332{
1333 if (mTrack == 0) return NO_INIT;
1334 return mTrack->frameCount() * frameSize();
1335}
1336
1337ssize_t MediaPlayerService::AudioOutput::frameCount() const
1338{
1339 if (mTrack == 0) return NO_INIT;
1340 return mTrack->frameCount();
1341}
1342
1343ssize_t MediaPlayerService::AudioOutput::channelCount() const
1344{
1345 if (mTrack == 0) return NO_INIT;
1346 return mTrack->channelCount();
1347}
1348
1349ssize_t MediaPlayerService::AudioOutput::frameSize() const
1350{
1351 if (mTrack == 0) return NO_INIT;
1352 return mTrack->frameSize();
1353}
1354
1355uint32_t MediaPlayerService::AudioOutput::latency () const
1356{
Eric Laurentdb354e52012-03-05 17:27:11 -08001357 if (mTrack == 0) return 0;
1358 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359}
1360
1361float MediaPlayerService::AudioOutput::msecsPerFrame() const
1362{
1363 return mMsecsPerFrame;
1364}
1365
Marco Nelissen4110c102012-03-29 09:31:28 -07001366status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001367{
1368 if (mTrack == 0) return NO_INIT;
1369 return mTrack->getPosition(position);
1370}
1371
Lajos Molnar06ad1522014-08-28 07:27:44 -07001372status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1373{
1374 if (mTrack == 0) return NO_INIT;
1375 return mTrack->getTimestamp(ts);
1376}
1377
Marco Nelissen4110c102012-03-29 09:31:28 -07001378status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1379{
1380 if (mTrack == 0) return NO_INIT;
1381 *frameswritten = mBytesWritten / frameSize();
1382 return OK;
1383}
1384
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001385status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1386{
1387 if (mTrack == 0) return NO_INIT;
1388 return mTrack->setParameters(keyValuePairs);
1389}
1390
1391String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1392{
1393 if (mTrack == 0) return String8::empty();
1394 return mTrack->getParameters(keys);
1395}
1396
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001397void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1398 mAttributes = attributes;
1399}
1400
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001401void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1402{
1403 ALOGV("deleteRecycledTrack");
1404
1405 if (mRecycledTrack != 0) {
1406
1407 if (mCallbackData != NULL) {
1408 mCallbackData->setOutput(NULL);
1409 mCallbackData->endTrackSwitch();
1410 }
1411
1412 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1413 mRecycledTrack->flush();
1414 }
1415 // An offloaded track isn't flushed because the STREAM_END is reported
1416 // slightly prematurely to allow time for the gapless track switch
1417 // but this means that if we decide not to recycle the track there
1418 // could be a small amount of residual data still playing. We leave
1419 // AudioFlinger to drain the track.
1420
1421 mRecycledTrack.clear();
1422 delete mCallbackData;
1423 mCallbackData = NULL;
1424 close();
1425 }
1426}
1427
Andreas Huber20111aa2009-07-14 16:56:47 -07001428status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001429 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1430 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001431 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001432 audio_output_flags_t flags,
1433 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001434{
Andreas Huber20111aa2009-07-14 16:56:47 -07001435 mCallback = cb;
1436 mCallbackCookie = cookie;
1437
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438 // Check argument "bufferCount" against the mininum buffer count
1439 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001440 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 bufferCount = mMinBufferCount;
1442
1443 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001444 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1445 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001446 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001447 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001448 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001449
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001450 // offloading is only supported in callback mode for now.
1451 // offloadInfo must be present if offload flag is set
1452 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1453 ((cb == NULL) || (offloadInfo == NULL))) {
1454 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001455 }
1456
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001457 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1458 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1459 } else {
1460 uint32_t afSampleRate;
1461 size_t afFrameCount;
1462
1463 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1464 return NO_INIT;
1465 }
1466 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1467 return NO_INIT;
1468 }
1469
1470 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1471 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001472
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001473 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001474 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001475 if (0 == channelMask) {
1476 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1477 return NO_INIT;
1478 }
1479 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001480
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001481 // Check whether we can recycle the track
1482 bool reuse = false;
1483 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001484
Glenn Kasten2799d742013-05-30 14:33:29 -07001485 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001486 // check whether we are switching between two offloaded tracks
1487 bothOffloaded = (flags & mRecycledTrack->getFlags()
1488 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001489
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001490 // check if the existing track can be reused as-is, or if a new track needs to be created.
1491 reuse = true;
1492
Marco Nelissen67295b52012-06-11 14:52:53 -07001493 if ((mCallbackData == NULL && mCallback != NULL) ||
1494 (mCallbackData != NULL && mCallback == NULL)) {
1495 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1496 ALOGV("can't chain callback and write");
1497 reuse = false;
1498 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001499 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1500 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001501 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001502 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001503 reuse = false;
1504 } else if (flags != mFlags) {
1505 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1506 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001507 } else if (mRecycledTrack->format() != format) {
1508 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001509 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001510 } else {
1511 ALOGV("no track available to recycle");
1512 }
1513
1514 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1515
1516 // If we can't recycle and both tracks are offloaded
1517 // we must close the previous output before opening a new one
1518 if (bothOffloaded && !reuse) {
1519 ALOGV("both offloaded and not recycling");
1520 deleteRecycledTrack();
1521 }
1522
1523 sp<AudioTrack> t;
1524 CallbackData *newcbd = NULL;
1525
1526 // We don't attempt to create a new track if we are recycling an
1527 // offloaded track. But, if we are recycling a non-offloaded or we
1528 // are switching where one is offloaded and one isn't then we create
1529 // the new track in advance so that we can read additional stream info
1530
1531 if (!(reuse && bothOffloaded)) {
1532 ALOGV("creating new AudioTrack");
1533
1534 if (mCallback != NULL) {
1535 newcbd = new CallbackData(this);
1536 t = new AudioTrack(
1537 mStreamType,
1538 sampleRate,
1539 format,
1540 channelMask,
1541 frameCount,
1542 flags,
1543 CallbackWrapper,
1544 newcbd,
1545 0, // notification frames
1546 mSessionId,
1547 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001548 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001549 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001550 mPid,
1551 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001552 } else {
1553 t = new AudioTrack(
1554 mStreamType,
1555 sampleRate,
1556 format,
1557 channelMask,
1558 frameCount,
1559 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001560 NULL, // callback
1561 NULL, // user data
1562 0, // notification frames
1563 mSessionId,
1564 AudioTrack::TRANSFER_DEFAULT,
1565 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001566 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001567 mPid,
1568 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001569 }
1570
1571 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1572 ALOGE("Unable to create audio track");
1573 delete newcbd;
1574 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001575 } else {
1576 // successful AudioTrack initialization implies a legacy stream type was generated
1577 // from the audio attributes
1578 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001579 }
1580 }
1581
1582 if (reuse) {
1583 CHECK(mRecycledTrack != NULL);
1584
1585 if (!bothOffloaded) {
1586 if (mRecycledTrack->frameCount() != t->frameCount()) {
1587 ALOGV("framecount differs: %u/%u frames",
1588 mRecycledTrack->frameCount(), t->frameCount());
1589 reuse = false;
1590 }
1591 }
1592
Marco Nelissen67295b52012-06-11 14:52:53 -07001593 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001594 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001595 close();
1596 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001597 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001598 if (mCallbackData != NULL) {
1599 mCallbackData->setOutput(this);
1600 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001601 delete newcbd;
1602 return OK;
1603 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001604 }
1605
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001606 // we're not going to reuse the track, unblock and flush it
1607 // this was done earlier if both tracks are offloaded
1608 if (!bothOffloaded) {
1609 deleteRecycledTrack();
1610 }
1611
1612 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1613
Marco Nelissen67295b52012-06-11 14:52:53 -07001614 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001615 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001617
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001618 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001619 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001620 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001621 uint32_t pos;
1622 if (t->getPosition(&pos) == OK) {
1623 mBytesWritten = uint64_t(pos) * t->frameSize();
1624 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001625 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001626
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001627 status_t res = NO_ERROR;
1628 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1629 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1630 if (res == NO_ERROR) {
1631 t->setAuxEffectSendLevel(mSendLevel);
1632 res = t->attachAuxEffect(mAuxEffectId);
1633 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001634 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001635 ALOGV("open() DONE status %d", res);
1636 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001637}
1638
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001639status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001640{
Steve Block3856b092011-10-20 11:56:00 +01001641 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001642 if (mCallbackData != NULL) {
1643 mCallbackData->endTrackSwitch();
1644 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001645 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001646 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001647 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001648 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001650 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001651}
1652
Marco Nelissen6b74d672012-02-28 16:07:44 -08001653void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1654 mNextOutput = nextOutput;
1655}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001656
1657
Marco Nelissen6b74d672012-02-28 16:07:44 -08001658void MediaPlayerService::AudioOutput::switchToNextOutput() {
1659 ALOGV("switchToNextOutput");
1660 if (mNextOutput != NULL) {
1661 if (mCallbackData != NULL) {
1662 mCallbackData->beginTrackSwitch();
1663 }
1664 delete mNextOutput->mCallbackData;
1665 mNextOutput->mCallbackData = mCallbackData;
1666 mCallbackData = NULL;
1667 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001668 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001669 mNextOutput->mSampleRateHz = mSampleRateHz;
1670 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001671 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001672 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001673 }
1674}
1675
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001676ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1677{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001678 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001679
Steve Block3856b092011-10-20 11:56:00 +01001680 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001681 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001682 ssize_t ret = mTrack->write(buffer, size);
Andy Hunga31335a2014-08-20 17:37:59 -07001683 if (ret >= 0) {
1684 mBytesWritten += ret;
1685 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001686 return ret;
1687 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001688 return NO_INIT;
1689}
1690
1691void MediaPlayerService::AudioOutput::stop()
1692{
Steve Block3856b092011-10-20 11:56:00 +01001693 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001694 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001695}
1696
1697void MediaPlayerService::AudioOutput::flush()
1698{
Steve Block3856b092011-10-20 11:56:00 +01001699 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001700 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001701}
1702
1703void MediaPlayerService::AudioOutput::pause()
1704{
Steve Block3856b092011-10-20 11:56:00 +01001705 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001706 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001707}
1708
1709void MediaPlayerService::AudioOutput::close()
1710{
Steve Block3856b092011-10-20 11:56:00 +01001711 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001712 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001713}
1714
1715void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1716{
Steve Block3856b092011-10-20 11:56:00 +01001717 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001718 mLeftVolume = left;
1719 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001720 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001721 mTrack->setVolume(left, right);
1722 }
1723}
1724
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001725status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1726{
1727 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1728 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001729 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001730 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1731 } else {
1732 res = NO_INIT;
1733 }
1734 mPlaybackRatePermille = ratePermille;
1735 if (mSampleRateHz != 0) {
1736 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1737 }
1738 return res;
1739}
1740
Eric Laurent2beeb502010-07-16 07:43:46 -07001741status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1742{
Steve Block3856b092011-10-20 11:56:00 +01001743 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001744 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001745 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001746 return mTrack->setAuxEffectSendLevel(level);
1747 }
1748 return NO_ERROR;
1749}
1750
1751status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1752{
Steve Block3856b092011-10-20 11:56:00 +01001753 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001754 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001755 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001756 return mTrack->attachAuxEffect(effectId);
1757 }
1758 return NO_ERROR;
1759}
1760
Andreas Huber20111aa2009-07-14 16:56:47 -07001761// static
1762void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001763 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001764 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001765 CallbackData *data = (CallbackData*)cookie;
1766 data->lock();
1767 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001768 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001769 if (me == NULL) {
1770 // no output set, likely because the track was scheduled to be reused
1771 // by another player, but the format turned out to be incompatible.
1772 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001773 if (buffer != NULL) {
1774 buffer->size = 0;
1775 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001776 return;
1777 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001778
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001779 switch(event) {
1780 case AudioTrack::EVENT_MORE_DATA: {
1781 size_t actualSize = (*me->mCallback)(
1782 me, buffer->raw, buffer->size, me->mCallbackCookie,
1783 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001784
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301785 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1786 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001787 // We've reached EOS but the audio track is not stopped yet,
1788 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001789
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001790 memset(buffer->raw, 0, buffer->size);
1791 actualSize = buffer->size;
1792 }
1793
1794 buffer->size = actualSize;
1795 } break;
1796
1797
1798 case AudioTrack::EVENT_STREAM_END:
1799 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1800 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1801 me->mCallbackCookie, CB_EVENT_STREAM_END);
1802 break;
1803
1804 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1805 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1806 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1807 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1808 break;
1809
1810 default:
1811 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001812 }
1813
Marco Nelissen6b74d672012-02-28 16:07:44 -08001814 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001815}
1816
Marco Nelissen4110c102012-03-29 09:31:28 -07001817int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001818{
1819 return mSessionId;
1820}
1821
Eric Laurent6f59db12013-07-26 17:16:50 -07001822uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1823{
1824 if (mTrack == 0) return 0;
1825 return mTrack->getSampleRate();
1826}
1827
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001828////////////////////////////////////////////////////////////////////////////////
1829
1830struct CallbackThread : public Thread {
1831 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1832 MediaPlayerBase::AudioSink::AudioCallback cb,
1833 void *cookie);
1834
1835protected:
1836 virtual ~CallbackThread();
1837
1838 virtual bool threadLoop();
1839
1840private:
1841 wp<MediaPlayerBase::AudioSink> mSink;
1842 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1843 void *mCookie;
1844 void *mBuffer;
1845 size_t mBufferSize;
1846
1847 CallbackThread(const CallbackThread &);
1848 CallbackThread &operator=(const CallbackThread &);
1849};
1850
1851CallbackThread::CallbackThread(
1852 const wp<MediaPlayerBase::AudioSink> &sink,
1853 MediaPlayerBase::AudioSink::AudioCallback cb,
1854 void *cookie)
1855 : mSink(sink),
1856 mCallback(cb),
1857 mCookie(cookie),
1858 mBuffer(NULL),
1859 mBufferSize(0) {
1860}
1861
1862CallbackThread::~CallbackThread() {
1863 if (mBuffer) {
1864 free(mBuffer);
1865 mBuffer = NULL;
1866 }
1867}
1868
1869bool CallbackThread::threadLoop() {
1870 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1871 if (sink == NULL) {
1872 return false;
1873 }
1874
1875 if (mBuffer == NULL) {
1876 mBufferSize = sink->bufferSize();
1877 mBuffer = malloc(mBufferSize);
1878 }
1879
1880 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001881 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1882 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001883
1884 if (actualSize > 0) {
1885 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001886 // Could return false on sink->write() error or short count.
1887 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001888 }
1889
1890 return true;
1891}
1892
1893////////////////////////////////////////////////////////////////////////////////
1894
Gloria Wang7cf180c2011-02-19 18:37:57 -08001895void MediaPlayerService::addBatteryData(uint32_t params)
1896{
1897 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001898
1899 int32_t time = systemTime() / 1000000L;
1900
1901 // change audio output devices. This notification comes from AudioFlinger
1902 if ((params & kBatteryDataSpeakerOn)
1903 || (params & kBatteryDataOtherAudioDeviceOn)) {
1904
1905 int deviceOn[NUM_AUDIO_DEVICES];
1906 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1907 deviceOn[i] = 0;
1908 }
1909
1910 if ((params & kBatteryDataSpeakerOn)
1911 && (params & kBatteryDataOtherAudioDeviceOn)) {
1912 deviceOn[SPEAKER_AND_OTHER] = 1;
1913 } else if (params & kBatteryDataSpeakerOn) {
1914 deviceOn[SPEAKER] = 1;
1915 } else {
1916 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1917 }
1918
1919 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1920 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1921
1922 if (mBatteryAudio.refCount > 0) { // if playing audio
1923 if (!deviceOn[i]) {
1924 mBatteryAudio.lastTime[i] += time;
1925 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1926 mBatteryAudio.lastTime[i] = 0;
1927 } else {
1928 mBatteryAudio.lastTime[i] = 0 - time;
1929 }
1930 }
1931
1932 mBatteryAudio.deviceOn[i] = deviceOn[i];
1933 }
1934 }
1935 return;
1936 }
1937
Marco Nelissenb7848f12014-12-04 08:57:56 -08001938 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08001939 if (params & kBatteryDataAudioFlingerStart) {
1940 // record the start time only if currently no other audio
1941 // is being played
1942 if (mBatteryAudio.refCount == 0) {
1943 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1944 if (mBatteryAudio.deviceOn[i]) {
1945 mBatteryAudio.lastTime[i] -= time;
1946 }
1947 }
1948 }
1949
1950 mBatteryAudio.refCount ++;
1951 return;
1952
1953 } else if (params & kBatteryDataAudioFlingerStop) {
1954 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001955 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001956 return;
1957 }
1958
1959 // record the stop time only if currently this is the only
1960 // audio being played
1961 if (mBatteryAudio.refCount == 1) {
1962 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1963 if (mBatteryAudio.deviceOn[i]) {
1964 mBatteryAudio.lastTime[i] += time;
1965 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1966 mBatteryAudio.lastTime[i] = 0;
1967 }
1968 }
1969 }
1970
1971 mBatteryAudio.refCount --;
1972 return;
1973 }
1974
Gloria Wang7cf180c2011-02-19 18:37:57 -08001975 int uid = IPCThreadState::self()->getCallingUid();
1976 if (uid == AID_MEDIA) {
1977 return;
1978 }
1979 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001980
1981 if (index < 0) { // create a new entry for this UID
1982 BatteryUsageInfo info;
1983 info.audioTotalTime = 0;
1984 info.videoTotalTime = 0;
1985 info.audioLastTime = 0;
1986 info.videoLastTime = 0;
1987 info.refCount = 0;
1988
Gloria Wang9ee159b2011-02-24 14:51:45 -08001989 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001990 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001991 return;
1992 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001993 }
1994
1995 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1996
1997 if (params & kBatteryDataCodecStarted) {
1998 if (params & kBatteryDataTrackAudio) {
1999 info.audioLastTime -= time;
2000 info.refCount ++;
2001 }
2002 if (params & kBatteryDataTrackVideo) {
2003 info.videoLastTime -= time;
2004 info.refCount ++;
2005 }
2006 } else {
2007 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002008 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002009 return;
2010 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002011 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002012 mBatteryData.removeItem(uid);
2013 return;
2014 }
2015
2016 if (params & kBatteryDataTrackAudio) {
2017 info.audioLastTime += time;
2018 info.refCount --;
2019 }
2020 if (params & kBatteryDataTrackVideo) {
2021 info.videoLastTime += time;
2022 info.refCount --;
2023 }
2024
2025 // no stream is being played by this UID
2026 if (info.refCount == 0) {
2027 info.audioTotalTime += info.audioLastTime;
2028 info.audioLastTime = 0;
2029 info.videoTotalTime += info.videoLastTime;
2030 info.videoLastTime = 0;
2031 }
2032 }
2033}
2034
2035status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2036 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002037
2038 // audio output devices usage
2039 int32_t time = systemTime() / 1000000L; //in ms
2040 int32_t totalTime;
2041
2042 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2043 totalTime = mBatteryAudio.totalTime[i];
2044
2045 if (mBatteryAudio.deviceOn[i]
2046 && (mBatteryAudio.lastTime[i] != 0)) {
2047 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2048 totalTime += tmpTime;
2049 }
2050
2051 reply->writeInt32(totalTime);
2052 // reset the total time
2053 mBatteryAudio.totalTime[i] = 0;
2054 }
2055
2056 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002057 BatteryUsageInfo info;
2058 int size = mBatteryData.size();
2059
2060 reply->writeInt32(size);
2061 int i = 0;
2062
2063 while (i < size) {
2064 info = mBatteryData.valueAt(i);
2065
2066 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2067 reply->writeInt32(info.audioTotalTime);
2068 reply->writeInt32(info.videoTotalTime);
2069
2070 info.audioTotalTime = 0;
2071 info.videoTotalTime = 0;
2072
2073 // remove the UID entry where no stream is being played
2074 if (info.refCount <= 0) {
2075 mBatteryData.removeItemsAt(i);
2076 size --;
2077 i --;
2078 }
2079 i++;
2080 }
2081 return NO_ERROR;
2082}
nikoa64c8c72009-07-20 15:07:26 -07002083} // namespace android