blob: 2ba05c0e90f68d90dd2f1a30ed06d24d0d772ef6 [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
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryHeapBase.h>
40#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080041#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070045#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070046#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047
Andreas Huber1b86fe02014-01-29 11:13:26 -080048#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070049#include <media/IRemoteDisplay.h>
50#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051#include <media/MediaPlayerInterface.h>
52#include <media/mediarecorder.h>
53#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070054#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070056#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070057#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070058#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010059#include <media/stagefright/AudioPlayer.h>
60#include <media/stagefright/foundation/ADebug.h>
Marco Nelissenf09611f2015-02-13 14:12:42 -080061#include <media/stagefright/foundation/ALooperRoster.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070062#include <mediautils/BatteryNotifier.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.
Ruben Brunk99e69712015-05-26 17:25:07 -0700290 BatteryNotifier& notifier(BatteryNotifier::getInstance());
291 notifier.noteResetVideo();
292 notifier.noteResetAudio();
Chong Zhang0b30fd42014-07-23 14:46:05 -0700293
John Grossman44a7e422012-06-21 17:29:24 -0700294 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295}
296
297MediaPlayerService::~MediaPlayerService()
298{
Steve Block3856b092011-10-20 11:56:00 +0100299 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300}
301
Svet Ganovbe71aa22015-04-28 12:06:02 -0700302sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800304 pid_t pid = IPCThreadState::self()->getCallingPid();
Svet Ganovbe71aa22015-04-28 12:06:02 -0700305 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName);
Gloria Wangdac6a312009-10-29 15:46:37 -0700306 wp<MediaRecorderClient> w = recorder;
307 Mutex::Autolock lock(mLock);
308 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100309 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 return recorder;
311}
312
Gloria Wangdac6a312009-10-29 15:46:37 -0700313void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
314{
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700318}
319
Glenn Kastenf37971f2012-02-03 11:06:53 -0800320sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800322 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 return retriever;
326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100329 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800331 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700333
334 sp<Client> c = new Client(
335 this, pid, connId, client, audioSessionId,
336 IPCThreadState::self()->getCallingUid());
337
Steve Block3856b092011-10-20 11:56:00 +0100338 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100339 IPCThreadState::self()->getCallingUid());
340
341 wp<Client> w = c;
342 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 Mutex::Autolock lock(mLock);
344 mClients.add(w);
345 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800346 return c;
347}
348
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700349sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
350 return MediaCodecList::getLocalInstance();
351}
352
Andreas Huber318ad9c2009-10-15 13:46:54 -0700353sp<IOMX> MediaPlayerService::getOMX() {
354 Mutex::Autolock autoLock(mLock);
355
356 if (mOMX.get() == NULL) {
357 mOMX = new OMX;
358 }
359
360 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700361}
362
Andreas Hubered3e3e02012-03-26 11:13:27 -0700363sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700364 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700365}
366
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800367sp<IDrm> MediaPlayerService::makeDrm() {
368 return new Drm;
369}
370
Andreas Huber279dcd82013-01-30 10:41:25 -0800371sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
372 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700373}
374
Jeff Brown2013a542012-09-04 21:38:42 -0700375sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
Svet Ganovbe71aa22015-04-28 12:06:02 -0700376 const String16 &opPackageName,
Jeff Brown2013a542012-09-04 21:38:42 -0700377 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700378 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
379 return NULL;
380 }
381
Svet Ganovbe71aa22015-04-28 12:06:02 -0700382 return new RemoteDisplay(opPackageName, client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700383}
384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
386{
387 const size_t SIZE = 256;
388 char buffer[SIZE];
389 String8 result;
390
391 result.append(" AudioOutput\n");
392 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
393 mStreamType, mLeftVolume, mRightVolume);
394 result.append(buffer);
395 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800396 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700398 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
399 mAuxEffectId, mSendLevel);
400 result.append(buffer);
401
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 ::write(fd, result.string(), result.size());
403 if (mTrack != 0) {
404 mTrack->dump(fd, args);
405 }
406 return NO_ERROR;
407}
408
Lajos Molnar6d339f12015-04-17 16:15:53 -0700409status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410{
411 const size_t SIZE = 256;
412 char buffer[SIZE];
413 String8 result;
414 result.append(" Client\n");
415 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
416 mPid, mConnId, mStatus, mLoop?"true": "false");
417 result.append(buffer);
418 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700419 if (mPlayer != NULL) {
420 mPlayer->dump(fd, args);
421 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 if (mAudioOutput != 0) {
423 mAudioOutput->dump(fd, args);
424 }
425 write(fd, "\n", 1);
426 return NO_ERROR;
427}
428
Marco Nelissenf09611f2015-02-13 14:12:42 -0800429/**
430 * The only arguments this understands right now are -c, -von and -voff,
431 * which are parsed by ALooperRoster::dump()
432 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
434{
435 const size_t SIZE = 256;
436 char buffer[SIZE];
437 String8 result;
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530438 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
439 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
440
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
442 snprintf(buffer, SIZE, "Permission Denial: "
443 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
444 IPCThreadState::self()->getCallingPid(),
445 IPCThreadState::self()->getCallingUid());
446 result.append(buffer);
447 } else {
448 Mutex::Autolock lock(mLock);
449 for (int i = 0, n = mClients.size(); i < n; ++i) {
450 sp<Client> c = mClients[i].promote();
451 if (c != 0) c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530452 clients.add(c);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 }
James Dongb9141222010-07-08 11:16:11 -0700454 if (mMediaRecorderClients.size() == 0) {
455 result.append(" No media recorder client\n\n");
456 } else {
457 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
458 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700459 if (c != 0) {
460 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
461 result.append(buffer);
462 write(fd, result.string(), result.size());
463 result = "\n";
464 c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530465 mediaRecorderClients.add(c);
James Donge579e282011-10-18 22:29:20 -0700466 }
James Dongb9141222010-07-08 11:16:11 -0700467 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700468 }
469
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800471 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 FILE *f = fopen(buffer, "r");
473 if (f) {
474 while (!feof(f)) {
475 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700476 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700478 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 strstr(buffer, " /system/media/")) {
480 result.append(" ");
481 result.append(buffer);
482 }
483 }
484 fclose(f);
485 } else {
486 result.append("couldn't open ");
487 result.append(buffer);
488 result.append("\n");
489 }
490
Marco Nelissenf09611f2015-02-13 14:12:42 -0800491 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 DIR *d = opendir(buffer);
493 if (d) {
494 struct dirent *ent;
495 while((ent = readdir(d)) != NULL) {
496 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800497 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498 struct stat s;
499 if (lstat(buffer, &s) == 0) {
500 if ((s.st_mode & S_IFMT) == S_IFLNK) {
501 char linkto[256];
502 int len = readlink(buffer, linkto, sizeof(linkto));
503 if(len > 0) {
504 if(len > 255) {
505 linkto[252] = '.';
506 linkto[253] = '.';
507 linkto[254] = '.';
508 linkto[255] = 0;
509 } else {
510 linkto[len] = 0;
511 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700512 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700514 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 strstr(linkto, "/system/media/") == linkto) {
516 result.append(" ");
517 result.append(buffer);
518 result.append(" -> ");
519 result.append(linkto);
520 result.append("\n");
521 }
522 }
523 } else {
524 result.append(" unexpected type for ");
525 result.append(buffer);
526 result.append("\n");
527 }
528 }
529 }
530 }
531 closedir(d);
532 } else {
533 result.append("couldn't open ");
534 result.append(buffer);
535 result.append("\n");
536 }
537
Marco Nelissenf09611f2015-02-13 14:12:42 -0800538 gLooperRoster.dump(fd, args);
539
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 bool dumpMem = false;
541 for (size_t i = 0; i < args.size(); i++) {
542 if (args[i] == String16("-m")) {
543 dumpMem = true;
544 }
545 }
546 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700547 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 }
550 write(fd, result.string(), result.size());
551 return NO_ERROR;
552}
553
554void MediaPlayerService::removeClient(wp<Client> client)
555{
556 Mutex::Autolock lock(mLock);
557 mClients.remove(client);
558}
559
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700560MediaPlayerService::Client::Client(
561 const sp<MediaPlayerService>& service, pid_t pid,
562 int32_t connId, const sp<IMediaPlayerClient>& client,
563 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 mPid = pid;
567 mConnId = connId;
568 mService = service;
569 mClient = client;
570 mLoop = false;
571 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700572 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700573 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800574 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700575 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700576
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000578 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579 mAntagonizer = new Antagonizer(notify, this);
580#endif
581}
582
583MediaPlayerService::Client::~Client()
584{
Steve Block3856b092011-10-20 11:56:00 +0100585 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 mAudioOutput.clear();
587 wp<Client> client(this);
588 disconnect();
589 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700590 if (mAudioAttributes != NULL) {
591 free(mAudioAttributes);
592 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593}
594
595void MediaPlayerService::Client::disconnect()
596{
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 // grab local reference and clear main reference to prevent future
599 // access to object
600 sp<MediaPlayerBase> p;
601 {
602 Mutex::Autolock l(mLock);
603 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800604 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800605 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700606
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 mPlayer.clear();
608
609 // clear the notification to prevent callbacks to dead client
610 // and reset the player. We assume the player will serialize
611 // access to itself if necessary.
612 if (p != 0) {
613 p->setNotifyCallback(0, 0);
614#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000615 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800616 mAntagonizer->kill();
617#endif
618 p->reset();
619 }
620
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700621 disconnectNativeWindow();
622
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 IPCThreadState::self()->flushCommands();
624}
625
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
627{
628 // determine if we have the right player type
629 sp<MediaPlayerBase> p = mPlayer;
630 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632 p.clear();
633 }
634 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700635 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700637
Jason Simmonsdb29e522011-08-12 13:46:55 -0700638 if (p != NULL) {
639 p->setUID(mUID);
640 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700641
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642 return p;
643}
644
John Grossmanc795b642012-02-22 15:38:35 -0800645sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
646 player_type playerType)
647{
648 ALOGV("player type = %d", playerType);
649
650 // create the right type of player
651 sp<MediaPlayerBase> p = createPlayer(playerType);
652 if (p == NULL) {
653 return p;
654 }
655
656 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800657 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700658 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800659 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
660 }
661
662 return p;
663}
664
665void MediaPlayerService::Client::setDataSource_post(
666 const sp<MediaPlayerBase>& p,
667 status_t status)
668{
669 ALOGV(" setDataSource");
670 mStatus = status;
671 if (mStatus != OK) {
672 ALOGE(" error: %d", mStatus);
673 return;
674 }
675
676 // Set the re-transmission endpoint if one was chosen.
677 if (mRetransmitEndpointValid) {
678 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
679 if (mStatus != NO_ERROR) {
680 ALOGE("setRetransmitEndpoint error: %d", mStatus);
681 }
682 }
683
684 if (mStatus == OK) {
685 mPlayer = p;
686 }
687}
688
Andreas Huber2db84552010-01-28 11:19:57 -0800689status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800690 const sp<IMediaHTTPService> &httpService,
691 const char *url,
692 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693{
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695 if (url == NULL)
696 return UNKNOWN_ERROR;
697
Dave Burked681bbb2011-08-30 14:39:17 +0100698 if ((strncmp(url, "http://", 7) == 0) ||
699 (strncmp(url, "https://", 8) == 0) ||
700 (strncmp(url, "rtsp://", 7) == 0)) {
701 if (!checkPermission("android.permission.INTERNET")) {
702 return PERMISSION_DENIED;
703 }
704 }
705
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 if (strncmp(url, "content://", 10) == 0) {
707 // get a filedescriptor for the content Uri and
708 // pass it to the setDataSource(fd) method
709
710 String16 url16(url);
711 int fd = android::openContentProviderFile(url16);
712 if (fd < 0)
713 {
Steve Block29357bc2012-01-06 19:20:56 +0000714 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800715 return UNKNOWN_ERROR;
716 }
717 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
718 close(fd);
719 return mStatus;
720 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700721 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800722 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
723 if (p == NULL) {
724 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800725 }
726
Andreas Huber1b86fe02014-01-29 11:13:26 -0800727 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 return mStatus;
729 }
730}
731
732status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
733{
Steve Block3856b092011-10-20 11:56:00 +0100734 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735 struct stat sb;
736 int ret = fstat(fd, &sb);
737 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000738 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800739 return UNKNOWN_ERROR;
740 }
741
Ying Wang5fde15d2015-04-20 22:13:22 -0700742 ALOGV("st_dev = %llu", static_cast<uint64_t>(sb.st_dev));
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700744 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
745 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800747
748 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000749 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 ::close(fd);
751 return UNKNOWN_ERROR;
752 }
753 if (offset + length > sb.st_size) {
754 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756 }
757
John Grossman44a7e422012-06-21 17:29:24 -0700758 player_type playerType = MediaPlayerFactory::getPlayerType(this,
759 fd,
760 offset,
761 length);
John Grossmanc795b642012-02-22 15:38:35 -0800762 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
763 if (p == NULL) {
764 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 }
766
767 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800768 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 return mStatus;
770}
771
Andreas Hubere2b10282010-11-23 11:41:34 -0800772status_t MediaPlayerService::Client::setDataSource(
773 const sp<IStreamSource> &source) {
774 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700775 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800776 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800777 if (p == NULL) {
778 return NO_INIT;
779 }
780
Andreas Hubere2b10282010-11-23 11:41:34 -0800781 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800782 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800783 return mStatus;
784}
785
Chris Watkins99f31602015-03-20 13:06:33 -0700786status_t MediaPlayerService::Client::setDataSource(
787 const sp<IDataSource> &source) {
788 sp<DataSource> dataSource = DataSource::CreateFromIDataSource(source);
789 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
790 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
791 if (p == NULL) {
792 return NO_INIT;
793 }
794 // now set data source
795 setDataSource_post(p, p->setDataSource(dataSource));
796 return mStatus;
797}
798
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700799void MediaPlayerService::Client::disconnectNativeWindow() {
800 if (mConnectedWindow != NULL) {
801 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
802 NATIVE_WINDOW_API_MEDIA);
803
804 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000805 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700806 strerror(-err), err);
807 }
808 }
809 mConnectedWindow.clear();
810}
811
Glenn Kasten11731182011-02-08 17:26:17 -0800812status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800813 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800814{
Andy McFadden484566c2012-12-18 09:46:54 -0800815 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800816 sp<MediaPlayerBase> p = getPlayer();
817 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700818
Marco Nelissenf8880202014-11-14 07:58:25 -0800819 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700820 if (mConnectedWindowBinder == binder) {
821 return OK;
822 }
823
824 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800825 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700826 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700827 status_t err = native_window_api_connect(anw.get(),
828 NATIVE_WINDOW_API_MEDIA);
829
830 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000831 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700832 // Note that we must do the reset before disconnecting from the ANW.
833 // Otherwise queue/dequeue calls could be made on the disconnected
834 // ANW, which may result in errors.
835 reset();
836
837 disconnectNativeWindow();
838
839 return err;
840 }
841 }
842
Andy McFadden484566c2012-12-18 09:46:54 -0800843 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700844 // disconnecting the old one. Otherwise queue/dequeue calls could be made
845 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800846 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700847
848 disconnectNativeWindow();
849
850 mConnectedWindow = anw;
851
852 if (err == OK) {
853 mConnectedWindowBinder = binder;
854 } else {
855 disconnectNativeWindow();
856 }
857
858 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800859}
860
Nicolas Catania1d187f12009-05-12 23:25:55 -0700861status_t MediaPlayerService::Client::invoke(const Parcel& request,
862 Parcel *reply)
863{
864 sp<MediaPlayerBase> p = getPlayer();
865 if (p == NULL) return UNKNOWN_ERROR;
866 return p->invoke(request, reply);
867}
868
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700869// This call doesn't need to access the native player.
870status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
871{
872 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700873 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700874
Nicolas Catania48290382009-07-10 13:53:06 -0700875 if (unmarshallFilter(filter, &allow, &status) &&
876 unmarshallFilter(filter, &drop, &status)) {
877 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700878
879 mMetadataAllow = allow;
880 mMetadataDrop = drop;
881 }
882 return status;
883}
884
Nicolas Catania48290382009-07-10 13:53:06 -0700885status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700886 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700887{
nikoa64c8c72009-07-20 15:07:26 -0700888 sp<MediaPlayerBase> player = getPlayer();
889 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700890
nikod608a812009-07-16 16:39:53 -0700891 status_t status;
892 // Placeholder for the return code, updated by the caller.
893 reply->writeInt32(-1);
894
nikoa64c8c72009-07-20 15:07:26 -0700895 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700896
897 // We don't block notifications while we fetch the data. We clear
898 // mMetadataUpdated first so we don't lose notifications happening
899 // during the rest of this call.
900 {
901 Mutex::Autolock lock(mLock);
902 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700903 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700904 }
905 mMetadataUpdated.clear();
906 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700907
nikoa64c8c72009-07-20 15:07:26 -0700908 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700909
nikoa64c8c72009-07-20 15:07:26 -0700910 metadata.appendHeader();
911 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700912
913 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700914 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000915 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700916 return status;
917 }
918
919 // FIXME: Implement filtering on the result. Not critical since
920 // filtering takes place on the update notifications already. This
921 // would be when all the metadata are fetch and a filter is set.
922
nikod608a812009-07-16 16:39:53 -0700923 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700924 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700925 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700926}
927
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800928status_t MediaPlayerService::Client::prepareAsync()
929{
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 sp<MediaPlayerBase> p = getPlayer();
932 if (p == 0) return UNKNOWN_ERROR;
933 status_t ret = p->prepareAsync();
934#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000935 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936 if (ret == NO_ERROR) mAntagonizer->start();
937#endif
938 return ret;
939}
940
941status_t MediaPlayerService::Client::start()
942{
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 sp<MediaPlayerBase> p = getPlayer();
945 if (p == 0) return UNKNOWN_ERROR;
946 p->setLooping(mLoop);
947 return p->start();
948}
949
950status_t MediaPlayerService::Client::stop()
951{
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953 sp<MediaPlayerBase> p = getPlayer();
954 if (p == 0) return UNKNOWN_ERROR;
955 return p->stop();
956}
957
958status_t MediaPlayerService::Client::pause()
959{
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800961 sp<MediaPlayerBase> p = getPlayer();
962 if (p == 0) return UNKNOWN_ERROR;
963 return p->pause();
964}
965
966status_t MediaPlayerService::Client::isPlaying(bool* state)
967{
968 *state = false;
969 sp<MediaPlayerBase> p = getPlayer();
970 if (p == 0) return UNKNOWN_ERROR;
971 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800973 return NO_ERROR;
974}
975
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700976status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800977{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700978 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
979 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Wei Jia98160162015-02-04 17:01:11 -0800980 sp<MediaPlayerBase> p = getPlayer();
981 if (p == 0) return UNKNOWN_ERROR;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700982 return p->setPlaybackSettings(rate);
983}
984
985status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
986{
987 sp<MediaPlayerBase> p = getPlayer();
988 if (p == 0) return UNKNOWN_ERROR;
989 status_t ret = p->getPlaybackSettings(rate);
990 if (ret == NO_ERROR) {
991 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
992 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
993 } else {
994 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
995 }
996 return ret;
997}
998
999status_t MediaPlayerService::Client::setSyncSettings(
1000 const AVSyncSettings& sync, float videoFpsHint)
1001{
1002 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1003 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1004 sp<MediaPlayerBase> p = getPlayer();
1005 if (p == 0) return UNKNOWN_ERROR;
1006 return p->setSyncSettings(sync, videoFpsHint);
1007}
1008
1009status_t MediaPlayerService::Client::getSyncSettings(
1010 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1011{
1012 sp<MediaPlayerBase> p = getPlayer();
1013 if (p == 0) return UNKNOWN_ERROR;
1014 status_t ret = p->getSyncSettings(sync, videoFps);
1015 if (ret == NO_ERROR) {
1016 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1017 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1018 } else {
1019 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1020 }
1021 return ret;
Wei Jia98160162015-02-04 17:01:11 -08001022}
1023
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1025{
Steve Block3856b092011-10-20 11:56:00 +01001026 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001027 sp<MediaPlayerBase> p = getPlayer();
1028 if (p == 0) return UNKNOWN_ERROR;
1029 status_t ret = p->getCurrentPosition(msec);
1030 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001031 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001032 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001033 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001034 }
1035 return ret;
1036}
1037
1038status_t MediaPlayerService::Client::getDuration(int *msec)
1039{
Steve Block3856b092011-10-20 11:56:00 +01001040 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001041 sp<MediaPlayerBase> p = getPlayer();
1042 if (p == 0) return UNKNOWN_ERROR;
1043 status_t ret = p->getDuration(msec);
1044 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001045 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001046 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001047 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001048 }
1049 return ret;
1050}
1051
Marco Nelissen6b74d672012-02-28 16:07:44 -08001052status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1053 ALOGV("setNextPlayer");
1054 Mutex::Autolock l(mLock);
1055 sp<Client> c = static_cast<Client*>(player.get());
1056 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001057
1058 if (c != NULL) {
1059 if (mAudioOutput != NULL) {
1060 mAudioOutput->setNextOutput(c->mAudioOutput);
1061 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1062 ALOGE("no current audio output");
1063 }
1064
1065 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1066 mPlayer->setNextPlayer(mNextClient->getPlayer());
1067 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001068 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001069
Marco Nelissen6b74d672012-02-28 16:07:44 -08001070 return OK;
1071}
1072
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001073status_t MediaPlayerService::Client::seekTo(int msec)
1074{
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076 sp<MediaPlayerBase> p = getPlayer();
1077 if (p == 0) return UNKNOWN_ERROR;
1078 return p->seekTo(msec);
1079}
1080
1081status_t MediaPlayerService::Client::reset()
1082{
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001084 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001085 sp<MediaPlayerBase> p = getPlayer();
1086 if (p == 0) return UNKNOWN_ERROR;
1087 return p->reset();
1088}
1089
Glenn Kastenfff6d712012-01-12 16:38:12 -08001090status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001091{
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001093 // TODO: for hardware output, call player instead
1094 Mutex::Autolock l(mLock);
1095 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1096 return NO_ERROR;
1097}
1098
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001099status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1100{
1101 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1102 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1103 unmarshallAudioAttributes(parcel, mAudioAttributes);
1104
1105 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1106 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1107 mAudioAttributes->tags);
1108
1109 if (mAudioOutput != 0) {
1110 mAudioOutput->setAudioAttributes(mAudioAttributes);
1111 }
1112 return NO_ERROR;
1113}
1114
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115status_t MediaPlayerService::Client::setLooping(int loop)
1116{
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001118 mLoop = loop;
1119 sp<MediaPlayerBase> p = getPlayer();
1120 if (p != 0) return p->setLooping(loop);
1121 return NO_ERROR;
1122}
1123
1124status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1125{
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001127
1128 // for hardware output, call player instead
1129 sp<MediaPlayerBase> p = getPlayer();
1130 {
1131 Mutex::Autolock l(mLock);
1132 if (p != 0 && p->hardwareOutput()) {
1133 MediaPlayerHWInterface* hwp =
1134 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1135 return hwp->setVolume(leftVolume, rightVolume);
1136 } else {
1137 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1138 return NO_ERROR;
1139 }
1140 }
1141
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001142 return NO_ERROR;
1143}
1144
Eric Laurent2beeb502010-07-16 07:43:46 -07001145status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1146{
Steve Block3856b092011-10-20 11:56:00 +01001147 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001148 Mutex::Autolock l(mLock);
1149 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1150 return NO_ERROR;
1151}
1152
1153status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1154{
Steve Block3856b092011-10-20 11:56:00 +01001155 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001156 Mutex::Autolock l(mLock);
1157 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1158 return NO_ERROR;
1159}
Nicolas Catania48290382009-07-10 13:53:06 -07001160
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001161status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001162 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001163 switch (key) {
1164 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1165 {
1166 Mutex::Autolock l(mLock);
1167 return setAudioAttributes_l(request);
1168 }
1169 default:
1170 sp<MediaPlayerBase> p = getPlayer();
1171 if (p == 0) { return UNKNOWN_ERROR; }
1172 return p->setParameter(key, request);
1173 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001174}
1175
1176status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001178 sp<MediaPlayerBase> p = getPlayer();
1179 if (p == 0) return UNKNOWN_ERROR;
1180 return p->getParameter(key, reply);
1181}
1182
John Grossmanc795b642012-02-22 15:38:35 -08001183status_t MediaPlayerService::Client::setRetransmitEndpoint(
1184 const struct sockaddr_in* endpoint) {
1185
1186 if (NULL != endpoint) {
1187 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1188 uint16_t p = ntohs(endpoint->sin_port);
1189 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1190 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1191 } else {
1192 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1193 }
1194
1195 sp<MediaPlayerBase> p = getPlayer();
1196
1197 // Right now, the only valid time to set a retransmit endpoint is before
1198 // player selection has been made (since the presence or absence of a
1199 // retransmit endpoint is going to determine which player is selected during
1200 // setDataSource).
1201 if (p != 0) return INVALID_OPERATION;
1202
1203 if (NULL != endpoint) {
1204 mRetransmitEndpoint = *endpoint;
1205 mRetransmitEndpointValid = true;
1206 } else {
1207 mRetransmitEndpointValid = false;
1208 }
1209
1210 return NO_ERROR;
1211}
1212
John Grossman44a7e422012-06-21 17:29:24 -07001213status_t MediaPlayerService::Client::getRetransmitEndpoint(
1214 struct sockaddr_in* endpoint)
1215{
1216 if (NULL == endpoint)
1217 return BAD_VALUE;
1218
1219 sp<MediaPlayerBase> p = getPlayer();
1220
1221 if (p != NULL)
1222 return p->getRetransmitEndpoint(endpoint);
1223
1224 if (!mRetransmitEndpointValid)
1225 return NO_INIT;
1226
1227 *endpoint = mRetransmitEndpoint;
1228
1229 return NO_ERROR;
1230}
1231
Gloria Wangb483c472011-04-11 17:23:27 -07001232void MediaPlayerService::Client::notify(
1233 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234{
1235 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001236 if (client == NULL) {
1237 return;
1238 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001239
James Dongb8a98252012-08-26 16:13:03 -07001240 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001241 {
1242 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001243 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001244 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001245 if (client->mAudioOutput != NULL)
1246 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001247 client->mNextClient->start();
1248 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1249 }
1250 }
1251
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001252 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001253 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001254 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001255
1256 if(client->shouldDropMetadata(metadata_type)) {
1257 return;
1258 }
1259
1260 // Update the list of metadata that have changed. getMetadata
1261 // also access mMetadataUpdated and clears it.
1262 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001263 }
James Dongb8a98252012-08-26 16:13:03 -07001264
1265 if (c != NULL) {
1266 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1267 c->notify(msg, ext1, ext2, obj);
1268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269}
1270
Nicolas Catania48290382009-07-10 13:53:06 -07001271
nikoa64c8c72009-07-20 15:07:26 -07001272bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001273{
Nicolas Catania48290382009-07-10 13:53:06 -07001274 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001275
Nicolas Catania48290382009-07-10 13:53:06 -07001276 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001277 return true;
1278 }
1279
Nicolas Catania48290382009-07-10 13:53:06 -07001280 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001281 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001282 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001283 return true;
1284 }
1285}
1286
Nicolas Catania48290382009-07-10 13:53:06 -07001287
nikoa64c8c72009-07-20 15:07:26 -07001288void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001289 Mutex::Autolock lock(mLock);
1290 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1291 mMetadataUpdated.add(metadata_type);
1292 }
1293}
1294
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295#if CALLBACK_ANTAGONIZER
1296const int Antagonizer::interval = 10000; // 10 msecs
1297
1298Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1299 mExit(false), mActive(false), mClient(client), mCb(cb)
1300{
1301 createThread(callbackThread, this);
1302}
1303
1304void Antagonizer::kill()
1305{
1306 Mutex::Autolock _l(mLock);
1307 mActive = false;
1308 mExit = true;
1309 mCondition.wait(mLock);
1310}
1311
1312int Antagonizer::callbackThread(void* user)
1313{
Steve Blockb8a80522011-12-20 16:23:08 +00001314 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1316 while (!p->mExit) {
1317 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001318 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 p->mCb(p->mClient, 0, 0, 0);
1320 }
1321 usleep(interval);
1322 }
1323 Mutex::Autolock _l(p->mLock);
1324 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001325 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326 return 0;
1327}
1328#endif
1329
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001330#undef LOG_TAG
1331#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001332MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1333 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001334 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001335 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001336 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001337 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001338 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001339 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001340 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001341 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001342 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001343 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001344 mLeftVolume = 1.0;
1345 mRightVolume = 1.0;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001346 mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001347 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001348 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001349 mAuxEffectId = 0;
1350 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001351 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001352 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001353}
1354
1355MediaPlayerService::AudioOutput::~AudioOutput()
1356{
1357 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001358 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359}
1360
1361void MediaPlayerService::AudioOutput::setMinBufferCount()
1362{
1363 char value[PROPERTY_VALUE_MAX];
1364 if (property_get("ro.kernel.qemu", value, 0)) {
1365 mIsOnEmulator = true;
1366 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1367 }
1368}
1369
1370bool MediaPlayerService::AudioOutput::isOnEmulator()
1371{
1372 setMinBufferCount();
1373 return mIsOnEmulator;
1374}
1375
1376int MediaPlayerService::AudioOutput::getMinBufferCount()
1377{
1378 setMinBufferCount();
1379 return mMinBufferCount;
1380}
1381
1382ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1383{
1384 if (mTrack == 0) return NO_INIT;
1385 return mTrack->frameCount() * frameSize();
1386}
1387
1388ssize_t MediaPlayerService::AudioOutput::frameCount() const
1389{
1390 if (mTrack == 0) return NO_INIT;
1391 return mTrack->frameCount();
1392}
1393
1394ssize_t MediaPlayerService::AudioOutput::channelCount() const
1395{
1396 if (mTrack == 0) return NO_INIT;
1397 return mTrack->channelCount();
1398}
1399
1400ssize_t MediaPlayerService::AudioOutput::frameSize() const
1401{
1402 if (mTrack == 0) return NO_INIT;
1403 return mTrack->frameSize();
1404}
1405
1406uint32_t MediaPlayerService::AudioOutput::latency () const
1407{
Eric Laurentdb354e52012-03-05 17:27:11 -08001408 if (mTrack == 0) return 0;
1409 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410}
1411
1412float MediaPlayerService::AudioOutput::msecsPerFrame() const
1413{
1414 return mMsecsPerFrame;
1415}
1416
Marco Nelissen4110c102012-03-29 09:31:28 -07001417status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001418{
1419 if (mTrack == 0) return NO_INIT;
1420 return mTrack->getPosition(position);
1421}
1422
Lajos Molnar06ad1522014-08-28 07:27:44 -07001423status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1424{
1425 if (mTrack == 0) return NO_INIT;
1426 return mTrack->getTimestamp(ts);
1427}
1428
Marco Nelissen4110c102012-03-29 09:31:28 -07001429status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1430{
1431 if (mTrack == 0) return NO_INIT;
1432 *frameswritten = mBytesWritten / frameSize();
1433 return OK;
1434}
1435
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001436status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1437{
1438 if (mTrack == 0) return NO_INIT;
1439 return mTrack->setParameters(keyValuePairs);
1440}
1441
1442String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1443{
1444 if (mTrack == 0) return String8::empty();
1445 return mTrack->getParameters(keys);
1446}
1447
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001448void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1449 mAttributes = attributes;
1450}
1451
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001452void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1453{
1454 ALOGV("deleteRecycledTrack");
1455
1456 if (mRecycledTrack != 0) {
1457
1458 if (mCallbackData != NULL) {
1459 mCallbackData->setOutput(NULL);
1460 mCallbackData->endTrackSwitch();
1461 }
1462
1463 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1464 mRecycledTrack->flush();
1465 }
1466 // An offloaded track isn't flushed because the STREAM_END is reported
1467 // slightly prematurely to allow time for the gapless track switch
1468 // but this means that if we decide not to recycle the track there
1469 // could be a small amount of residual data still playing. We leave
1470 // AudioFlinger to drain the track.
1471
1472 mRecycledTrack.clear();
1473 delete mCallbackData;
1474 mCallbackData = NULL;
1475 close();
1476 }
1477}
1478
Andreas Huber20111aa2009-07-14 16:56:47 -07001479status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001480 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1481 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001482 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001483 audio_output_flags_t flags,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001484 const audio_offload_info_t *offloadInfo,
1485 bool doNotReconnect)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486{
Andreas Huber20111aa2009-07-14 16:56:47 -07001487 mCallback = cb;
1488 mCallbackCookie = cookie;
1489
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001490 // Check argument "bufferCount" against the mininum buffer count
1491 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001492 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001493 bufferCount = mMinBufferCount;
1494
1495 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001496 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1497 format, bufferCount, mSessionId, flags);
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001498 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001499
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001500 // offloading is only supported in callback mode for now.
1501 // offloadInfo must be present if offload flag is set
1502 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1503 ((cb == NULL) || (offloadInfo == NULL))) {
1504 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505 }
1506
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001507 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1508 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1509 } else {
1510 uint32_t afSampleRate;
1511 size_t afFrameCount;
1512
1513 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1514 return NO_INIT;
1515 }
1516 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1517 return NO_INIT;
1518 }
1519
1520 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1521 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001522
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001523 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001524 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001525 if (0 == channelMask) {
1526 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1527 return NO_INIT;
1528 }
1529 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001530
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001531 // Check whether we can recycle the track
1532 bool reuse = false;
1533 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001534
Glenn Kasten2799d742013-05-30 14:33:29 -07001535 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001536 // check whether we are switching between two offloaded tracks
1537 bothOffloaded = (flags & mRecycledTrack->getFlags()
1538 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001539
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001540 // check if the existing track can be reused as-is, or if a new track needs to be created.
1541 reuse = true;
1542
Marco Nelissen67295b52012-06-11 14:52:53 -07001543 if ((mCallbackData == NULL && mCallback != NULL) ||
1544 (mCallbackData != NULL && mCallback == NULL)) {
1545 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1546 ALOGV("can't chain callback and write");
1547 reuse = false;
1548 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001549 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1550 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001551 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001552 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001553 reuse = false;
1554 } else if (flags != mFlags) {
1555 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1556 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001557 } else if (mRecycledTrack->format() != format) {
1558 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001559 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001560 } else {
1561 ALOGV("no track available to recycle");
1562 }
1563
1564 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1565
1566 // If we can't recycle and both tracks are offloaded
1567 // we must close the previous output before opening a new one
1568 if (bothOffloaded && !reuse) {
1569 ALOGV("both offloaded and not recycling");
1570 deleteRecycledTrack();
1571 }
1572
1573 sp<AudioTrack> t;
1574 CallbackData *newcbd = NULL;
1575
1576 // We don't attempt to create a new track if we are recycling an
1577 // offloaded track. But, if we are recycling a non-offloaded or we
1578 // are switching where one is offloaded and one isn't then we create
1579 // the new track in advance so that we can read additional stream info
1580
1581 if (!(reuse && bothOffloaded)) {
1582 ALOGV("creating new AudioTrack");
1583
1584 if (mCallback != NULL) {
1585 newcbd = new CallbackData(this);
1586 t = new AudioTrack(
1587 mStreamType,
1588 sampleRate,
1589 format,
1590 channelMask,
1591 frameCount,
1592 flags,
1593 CallbackWrapper,
1594 newcbd,
1595 0, // notification frames
1596 mSessionId,
1597 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001598 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001599 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001600 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001601 mAttributes,
1602 doNotReconnect);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001603 } else {
1604 t = new AudioTrack(
1605 mStreamType,
1606 sampleRate,
1607 format,
1608 channelMask,
1609 frameCount,
1610 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001611 NULL, // callback
1612 NULL, // user data
1613 0, // notification frames
1614 mSessionId,
1615 AudioTrack::TRANSFER_DEFAULT,
1616 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001617 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001618 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001619 mAttributes,
1620 doNotReconnect);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001621 }
1622
1623 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1624 ALOGE("Unable to create audio track");
1625 delete newcbd;
Glenn Kasten3e98ecd2015-05-18 13:13:24 -07001626 // t goes out of scope, so reference count drops to zero
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001627 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001628 } else {
1629 // successful AudioTrack initialization implies a legacy stream type was generated
1630 // from the audio attributes
1631 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001632 }
1633 }
1634
1635 if (reuse) {
1636 CHECK(mRecycledTrack != NULL);
1637
1638 if (!bothOffloaded) {
1639 if (mRecycledTrack->frameCount() != t->frameCount()) {
1640 ALOGV("framecount differs: %u/%u frames",
1641 mRecycledTrack->frameCount(), t->frameCount());
1642 reuse = false;
1643 }
1644 }
1645
Marco Nelissen67295b52012-06-11 14:52:53 -07001646 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001647 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001648 close();
1649 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001650 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001651 if (mCallbackData != NULL) {
1652 mCallbackData->setOutput(this);
1653 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001654 delete newcbd;
1655 return OK;
1656 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001657 }
1658
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001659 // we're not going to reuse the track, unblock and flush it
1660 // this was done earlier if both tracks are offloaded
1661 if (!bothOffloaded) {
1662 deleteRecycledTrack();
1663 }
1664
1665 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1666
Marco Nelissen67295b52012-06-11 14:52:53 -07001667 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001668 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001669 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001670
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001671 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001672 mFlags = flags;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001673 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
Marco Nelissen99448602012-04-02 12:16:49 -07001674 uint32_t pos;
1675 if (t->getPosition(&pos) == OK) {
1676 mBytesWritten = uint64_t(pos) * t->frameSize();
1677 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001678 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001679
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001680 status_t res = NO_ERROR;
1681 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001682 res = t->setPlaybackRate(mPlaybackRate);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001683 if (res == NO_ERROR) {
1684 t->setAuxEffectSendLevel(mSendLevel);
1685 res = t->attachAuxEffect(mAuxEffectId);
1686 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001687 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001688 ALOGV("open() DONE status %d", res);
1689 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001690}
1691
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001692status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001693{
Steve Block3856b092011-10-20 11:56:00 +01001694 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001695 if (mCallbackData != NULL) {
1696 mCallbackData->endTrackSwitch();
1697 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001698 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001699 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001700 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001701 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001702 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001703 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001704}
1705
Marco Nelissen6b74d672012-02-28 16:07:44 -08001706void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1707 mNextOutput = nextOutput;
1708}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001709
1710
Marco Nelissen6b74d672012-02-28 16:07:44 -08001711void MediaPlayerService::AudioOutput::switchToNextOutput() {
1712 ALOGV("switchToNextOutput");
1713 if (mNextOutput != NULL) {
1714 if (mCallbackData != NULL) {
1715 mCallbackData->beginTrackSwitch();
1716 }
1717 delete mNextOutput->mCallbackData;
1718 mNextOutput->mCallbackData = mCallbackData;
1719 mCallbackData = NULL;
1720 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001721 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001722 mNextOutput->mSampleRateHz = mSampleRateHz;
1723 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001724 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001725 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001726 }
1727}
1728
Wei Jia7d3f4df2015-03-03 15:28:00 -08001729ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001730{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001731 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001732
Steve Block3856b092011-10-20 11:56:00 +01001733 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001734 if (mTrack != 0) {
Wei Jia7d3f4df2015-03-03 15:28:00 -08001735 ssize_t ret = mTrack->write(buffer, size, blocking);
Andy Hunga31335a2014-08-20 17:37:59 -07001736 if (ret >= 0) {
1737 mBytesWritten += ret;
1738 }
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001739 return ret;
1740 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001741 return NO_INIT;
1742}
1743
1744void MediaPlayerService::AudioOutput::stop()
1745{
Steve Block3856b092011-10-20 11:56:00 +01001746 ALOGV("stop");
Andy Hungda17c042015-06-01 15:53:14 -07001747 mBytesWritten = 0;
Glenn Kasten2799d742013-05-30 14:33:29 -07001748 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001749}
1750
1751void MediaPlayerService::AudioOutput::flush()
1752{
Steve Block3856b092011-10-20 11:56:00 +01001753 ALOGV("flush");
Andy Hungda17c042015-06-01 15:53:14 -07001754 mBytesWritten = 0;
Glenn Kasten2799d742013-05-30 14:33:29 -07001755 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001756}
1757
1758void MediaPlayerService::AudioOutput::pause()
1759{
Steve Block3856b092011-10-20 11:56:00 +01001760 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001761 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001762}
1763
1764void MediaPlayerService::AudioOutput::close()
1765{
Steve Block3856b092011-10-20 11:56:00 +01001766 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001767 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001768}
1769
1770void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1771{
Steve Block3856b092011-10-20 11:56:00 +01001772 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001773 mLeftVolume = left;
1774 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001775 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001776 mTrack->setVolume(left, right);
1777 }
1778}
1779
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001780status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001781{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001782 ALOGV("setPlaybackRate(%f %f %d %d)",
1783 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
1784 if (mTrack == 0) {
1785 // remember rate so that we can set it when the track is opened
1786 mPlaybackRate = rate;
1787 return OK;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001788 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001789 status_t res = mTrack->setPlaybackRate(rate);
1790 if (res != NO_ERROR) {
1791 return res;
1792 }
1793 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
1794 CHECK_GT(rate.mSpeed, 0.f);
1795 mPlaybackRate = rate;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001796 if (mSampleRateHz != 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001797 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001798 }
1799 return res;
1800}
1801
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001802status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
1803{
1804 ALOGV("setPlaybackRate");
1805 if (mTrack == 0) {
1806 return NO_INIT;
1807 }
1808 *rate = mTrack->getPlaybackRate();
1809 return NO_ERROR;
1810}
1811
Eric Laurent2beeb502010-07-16 07:43:46 -07001812status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1813{
Steve Block3856b092011-10-20 11:56:00 +01001814 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001815 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001816 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001817 return mTrack->setAuxEffectSendLevel(level);
1818 }
1819 return NO_ERROR;
1820}
1821
1822status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1823{
Steve Block3856b092011-10-20 11:56:00 +01001824 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001825 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001826 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001827 return mTrack->attachAuxEffect(effectId);
1828 }
1829 return NO_ERROR;
1830}
1831
Andreas Huber20111aa2009-07-14 16:56:47 -07001832// static
1833void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001834 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001835 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001836 CallbackData *data = (CallbackData*)cookie;
1837 data->lock();
1838 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001839 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001840 if (me == NULL) {
1841 // no output set, likely because the track was scheduled to be reused
1842 // by another player, but the format turned out to be incompatible.
1843 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001844 if (buffer != NULL) {
1845 buffer->size = 0;
1846 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001847 return;
1848 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001849
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001850 switch(event) {
1851 case AudioTrack::EVENT_MORE_DATA: {
1852 size_t actualSize = (*me->mCallback)(
1853 me, buffer->raw, buffer->size, me->mCallbackCookie,
1854 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001855
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301856 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1857 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001858 // We've reached EOS but the audio track is not stopped yet,
1859 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001860
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001861 memset(buffer->raw, 0, buffer->size);
1862 actualSize = buffer->size;
1863 }
1864
1865 buffer->size = actualSize;
1866 } break;
1867
1868
1869 case AudioTrack::EVENT_STREAM_END:
1870 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1871 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1872 me->mCallbackCookie, CB_EVENT_STREAM_END);
1873 break;
1874
1875 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1876 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1877 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1878 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1879 break;
1880
Glenn Kasten421743b2015-06-01 08:18:08 -07001881 case AudioTrack::EVENT_UNDERRUN:
1882 // This occurs when there is no data available, typically occurring
1883 // when there is a failure to supply data to the AudioTrack. It can also
1884 // occur in non-offloaded mode when the audio device comes out of standby.
1885 //
1886 // If you see this at the start of playback, there probably was a glitch.
1887 ALOGI("callbackwrapper: EVENT_UNDERRUN (discarded)");
1888 break;
1889
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001890 default:
1891 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001892 }
1893
Marco Nelissen6b74d672012-02-28 16:07:44 -08001894 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001895}
1896
Marco Nelissen4110c102012-03-29 09:31:28 -07001897int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001898{
1899 return mSessionId;
1900}
1901
Eric Laurent6f59db12013-07-26 17:16:50 -07001902uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1903{
1904 if (mTrack == 0) return 0;
1905 return mTrack->getSampleRate();
1906}
1907
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001908////////////////////////////////////////////////////////////////////////////////
1909
1910struct CallbackThread : public Thread {
1911 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1912 MediaPlayerBase::AudioSink::AudioCallback cb,
1913 void *cookie);
1914
1915protected:
1916 virtual ~CallbackThread();
1917
1918 virtual bool threadLoop();
1919
1920private:
1921 wp<MediaPlayerBase::AudioSink> mSink;
1922 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1923 void *mCookie;
1924 void *mBuffer;
1925 size_t mBufferSize;
1926
1927 CallbackThread(const CallbackThread &);
1928 CallbackThread &operator=(const CallbackThread &);
1929};
1930
1931CallbackThread::CallbackThread(
1932 const wp<MediaPlayerBase::AudioSink> &sink,
1933 MediaPlayerBase::AudioSink::AudioCallback cb,
1934 void *cookie)
1935 : mSink(sink),
1936 mCallback(cb),
1937 mCookie(cookie),
1938 mBuffer(NULL),
1939 mBufferSize(0) {
1940}
1941
1942CallbackThread::~CallbackThread() {
1943 if (mBuffer) {
1944 free(mBuffer);
1945 mBuffer = NULL;
1946 }
1947}
1948
1949bool CallbackThread::threadLoop() {
1950 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1951 if (sink == NULL) {
1952 return false;
1953 }
1954
1955 if (mBuffer == NULL) {
1956 mBufferSize = sink->bufferSize();
1957 mBuffer = malloc(mBufferSize);
1958 }
1959
1960 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001961 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1962 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001963
1964 if (actualSize > 0) {
1965 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07001966 // Could return false on sink->write() error or short count.
1967 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001968 }
1969
1970 return true;
1971}
1972
1973////////////////////////////////////////////////////////////////////////////////
1974
Gloria Wang7cf180c2011-02-19 18:37:57 -08001975void MediaPlayerService::addBatteryData(uint32_t params)
1976{
1977 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001978
1979 int32_t time = systemTime() / 1000000L;
1980
1981 // change audio output devices. This notification comes from AudioFlinger
1982 if ((params & kBatteryDataSpeakerOn)
1983 || (params & kBatteryDataOtherAudioDeviceOn)) {
1984
1985 int deviceOn[NUM_AUDIO_DEVICES];
1986 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1987 deviceOn[i] = 0;
1988 }
1989
1990 if ((params & kBatteryDataSpeakerOn)
1991 && (params & kBatteryDataOtherAudioDeviceOn)) {
1992 deviceOn[SPEAKER_AND_OTHER] = 1;
1993 } else if (params & kBatteryDataSpeakerOn) {
1994 deviceOn[SPEAKER] = 1;
1995 } else {
1996 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1997 }
1998
1999 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2000 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2001
2002 if (mBatteryAudio.refCount > 0) { // if playing audio
2003 if (!deviceOn[i]) {
2004 mBatteryAudio.lastTime[i] += time;
2005 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2006 mBatteryAudio.lastTime[i] = 0;
2007 } else {
2008 mBatteryAudio.lastTime[i] = 0 - time;
2009 }
2010 }
2011
2012 mBatteryAudio.deviceOn[i] = deviceOn[i];
2013 }
2014 }
2015 return;
2016 }
2017
Marco Nelissenb7848f12014-12-04 08:57:56 -08002018 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08002019 if (params & kBatteryDataAudioFlingerStart) {
2020 // record the start time only if currently no other audio
2021 // is being played
2022 if (mBatteryAudio.refCount == 0) {
2023 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2024 if (mBatteryAudio.deviceOn[i]) {
2025 mBatteryAudio.lastTime[i] -= time;
2026 }
2027 }
2028 }
2029
2030 mBatteryAudio.refCount ++;
2031 return;
2032
2033 } else if (params & kBatteryDataAudioFlingerStop) {
2034 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002035 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002036 return;
2037 }
2038
2039 // record the stop time only if currently this is the only
2040 // audio being played
2041 if (mBatteryAudio.refCount == 1) {
2042 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2043 if (mBatteryAudio.deviceOn[i]) {
2044 mBatteryAudio.lastTime[i] += time;
2045 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2046 mBatteryAudio.lastTime[i] = 0;
2047 }
2048 }
2049 }
2050
2051 mBatteryAudio.refCount --;
2052 return;
2053 }
2054
Gloria Wang7cf180c2011-02-19 18:37:57 -08002055 int uid = IPCThreadState::self()->getCallingUid();
2056 if (uid == AID_MEDIA) {
2057 return;
2058 }
2059 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002060
2061 if (index < 0) { // create a new entry for this UID
2062 BatteryUsageInfo info;
2063 info.audioTotalTime = 0;
2064 info.videoTotalTime = 0;
2065 info.audioLastTime = 0;
2066 info.videoLastTime = 0;
2067 info.refCount = 0;
2068
Gloria Wang9ee159b2011-02-24 14:51:45 -08002069 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002070 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002071 return;
2072 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002073 }
2074
2075 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2076
2077 if (params & kBatteryDataCodecStarted) {
2078 if (params & kBatteryDataTrackAudio) {
2079 info.audioLastTime -= time;
2080 info.refCount ++;
2081 }
2082 if (params & kBatteryDataTrackVideo) {
2083 info.videoLastTime -= time;
2084 info.refCount ++;
2085 }
2086 } else {
2087 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002088 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002089 return;
2090 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002091 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002092 mBatteryData.removeItem(uid);
2093 return;
2094 }
2095
2096 if (params & kBatteryDataTrackAudio) {
2097 info.audioLastTime += time;
2098 info.refCount --;
2099 }
2100 if (params & kBatteryDataTrackVideo) {
2101 info.videoLastTime += time;
2102 info.refCount --;
2103 }
2104
2105 // no stream is being played by this UID
2106 if (info.refCount == 0) {
2107 info.audioTotalTime += info.audioLastTime;
2108 info.audioLastTime = 0;
2109 info.videoTotalTime += info.videoLastTime;
2110 info.videoLastTime = 0;
2111 }
2112 }
2113}
2114
2115status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2116 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002117
2118 // audio output devices usage
2119 int32_t time = systemTime() / 1000000L; //in ms
2120 int32_t totalTime;
2121
2122 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2123 totalTime = mBatteryAudio.totalTime[i];
2124
2125 if (mBatteryAudio.deviceOn[i]
2126 && (mBatteryAudio.lastTime[i] != 0)) {
2127 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2128 totalTime += tmpTime;
2129 }
2130
2131 reply->writeInt32(totalTime);
2132 // reset the total time
2133 mBatteryAudio.totalTime[i] = 0;
2134 }
2135
2136 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002137 BatteryUsageInfo info;
2138 int size = mBatteryData.size();
2139
2140 reply->writeInt32(size);
2141 int i = 0;
2142
2143 while (i < size) {
2144 info = mBatteryData.valueAt(i);
2145
2146 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2147 reply->writeInt32(info.audioTotalTime);
2148 reply->writeInt32(info.videoTotalTime);
2149
2150 info.audioTotalTime = 0;
2151 info.videoTotalTime = 0;
2152
2153 // remove the UID entry where no stream is being played
2154 if (info.refCount <= 0) {
2155 mBatteryData.removeItemsAt(i);
2156 size --;
2157 i --;
2158 }
2159 i++;
2160 }
2161 return NO_ERROR;
2162}
nikoa64c8c72009-07-20 15:07:26 -07002163} // namespace android