blob: 4bc1a4a7adf495961a61faa3e9fa2cefd1fc7b55 [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_MEDIAPLAYER2_H
18#define ANDROID_MEDIAPLAYER2_H
19
Wei Jiaec044b02018-02-19 12:41:23 -080020#include <media/AVSyncSettings.h>
Wei Jia53692fa2017-12-11 10:33:46 -080021#include <media/AudioResamplerPublic.h>
22#include <media/BufferingSettings.h>
Wei Jiaec044b02018-02-19 12:41:23 -080023#include <media/Metadata.h>
24#include <media/mediaplayer_common.h>
25#include <mediaplayer2/MediaPlayer2Interface.h>
26#include <mediaplayer2/MediaPlayer2Types.h>
Dichen Zhangc2465c52018-11-12 11:56:05 -080027#include <mediaplayer2/JObjectHolder.h>
Wei Jia53692fa2017-12-11 10:33:46 -080028
Dichen Zhangf8726912018-10-17 13:31:26 -070029#include <jni.h>
Wei Jiaec044b02018-02-19 12:41:23 -080030#include <utils/Errors.h>
31#include <utils/Mutex.h>
32#include <utils/RefBase.h>
33#include <utils/String16.h>
34#include <utils/Vector.h>
35#include <system/audio-base.h>
Wei Jia53692fa2017-12-11 10:33:46 -080036
Dongwon Kang0d7042d2018-10-12 16:52:14 -070037#include "jni.h"
38
Wei Jia53692fa2017-12-11 10:33:46 -080039namespace android {
40
Wei Jia28288fb2017-12-15 13:45:29 -080041struct ANativeWindowWrapper;
Wei Jiac2636032018-02-01 09:15:25 -080042struct DataSourceDesc;
Wei Jiaec044b02018-02-19 12:41:23 -080043class MediaPlayer2AudioOutput;
Wei Jia53692fa2017-12-11 10:33:46 -080044
Wei Jia53692fa2017-12-11 10:33:46 -080045// ref-counted object for callbacks
46class MediaPlayer2Listener: virtual public RefBase
47{
48public:
Dongwon Kang41929fb2018-09-09 08:29:56 -070049 virtual void notify(int64_t srcId, int msg, int ext1, int ext2,
50 const PlayerMessage *obj = NULL) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -080051};
52
Wei Jiaec044b02018-02-19 12:41:23 -080053class MediaPlayer2 : public MediaPlayer2InterfaceListener
Wei Jia53692fa2017-12-11 10:33:46 -080054{
55public:
Wei Jia53692fa2017-12-11 10:33:46 -080056 ~MediaPlayer2();
Wei Jiaec044b02018-02-19 12:41:23 -080057
Dichen Zhang23658642018-11-15 10:26:16 -080058 static sp<MediaPlayer2> Create(int32_t sessionId);
Wei Jiaec044b02018-02-19 12:41:23 -080059 static status_t DumpAll(int fd, const Vector<String16>& args);
60
Wei Jia53692fa2017-12-11 10:33:46 -080061 void disconnect();
62
Wei Jiad2bb1bd2018-02-08 09:47:37 -080063 status_t getSrcId(int64_t *srcId);
Wei Jiac2636032018-02-01 09:15:25 -080064 status_t setDataSource(const sp<DataSourceDesc> &dsd);
Wei Jia57aeffd2018-02-15 16:01:14 -080065 status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd);
66 status_t playNextDataSource(int64_t srcId);
Wei Jia28288fb2017-12-15 13:45:29 -080067 status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww);
Wei Jia53692fa2017-12-11 10:33:46 -080068 status_t setListener(const sp<MediaPlayer2Listener>& listener);
69 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
70 status_t setBufferingSettings(const BufferingSettings& buffering);
Wei Jia53692fa2017-12-11 10:33:46 -080071 status_t prepareAsync();
72 status_t start();
Wei Jia53692fa2017-12-11 10:33:46 -080073 status_t pause();
74 bool isPlaying();
Wei Jia1f043e42018-06-20 16:52:50 -070075 mediaplayer2_states getState();
Wei Jia53692fa2017-12-11 10:33:46 -080076 status_t setPlaybackSettings(const AudioPlaybackRate& rate);
77 status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
78 status_t setSyncSettings(const AVSyncSettings& sync, float videoFpsHint);
79 status_t getSyncSettings(
80 AVSyncSettings* sync /* nonnull */,
81 float* videoFps /* nonnull */);
82 status_t getVideoWidth(int *w);
83 status_t getVideoHeight(int *h);
84 status_t seekTo(
Wei Jia800fe372018-02-20 15:00:45 -080085 int64_t msec,
Wei Jia53692fa2017-12-11 10:33:46 -080086 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
87 status_t notifyAt(int64_t mediaTimeUs);
Wei Jia800fe372018-02-20 15:00:45 -080088 status_t getCurrentPosition(int64_t *msec);
89 status_t getDuration(int64_t *msec);
Wei Jia53692fa2017-12-11 10:33:46 -080090 status_t reset();
91 status_t setAudioStreamType(audio_stream_type_t type);
92 status_t getAudioStreamType(audio_stream_type_t *type);
93 status_t setLooping(int loop);
94 bool isLooping();
Dichen Zhang7398ca02018-10-15 10:25:12 -070095 status_t setVolume(float volume);
Wei Jiad2bb1bd2018-02-08 09:47:37 -080096 void notify(int64_t srcId, int msg, int ext1, int ext2,
Dongwon Kang41929fb2018-09-09 08:29:56 -070097 const PlayerMessage *obj = NULL);
Dongwon Kang9f631982018-07-10 12:34:41 -070098 status_t invoke(const PlayerMessage &request, PlayerMessage *reply);
Dichen Zhang23658642018-11-15 10:26:16 -080099 status_t setAudioSessionId(int32_t sessionId);
100 int32_t getAudioSessionId();
Wei Jia53692fa2017-12-11 10:33:46 -0800101 status_t setAuxEffectSendLevel(float level);
102 status_t attachAuxEffect(int effectId);
Dichen Zhangc2465c52018-11-12 11:56:05 -0800103 status_t setAudioAttributes(const jobject attributes);
104 jobject getAudioAttributes();
Wei Jia53692fa2017-12-11 10:33:46 -0800105 status_t getParameter(int key, Parcel* reply);
Wei Jia53692fa2017-12-11 10:33:46 -0800106
Wei Jia53692fa2017-12-11 10:33:46 -0800107 // Modular DRM
108 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId);
109 status_t releaseDrm();
110 // AudioRouting
Dongwon Kang0d7042d2018-10-12 16:52:14 -0700111 status_t setPreferredDevice(jobject device);
112 jobject getRoutedDevice();
Dichen Zhangf8726912018-10-17 13:31:26 -0700113 status_t addAudioDeviceCallback(jobject routingDelegate);
114 status_t removeAudioDeviceCallback(jobject listener);
Wei Jia53692fa2017-12-11 10:33:46 -0800115
Wei Jiaec044b02018-02-19 12:41:23 -0800116 status_t dump(int fd, const Vector<String16>& args);
Wei Jia53692fa2017-12-11 10:33:46 -0800117
Wei Jiaec044b02018-02-19 12:41:23 -0800118private:
Dichen Zhang23658642018-11-15 10:26:16 -0800119 MediaPlayer2(int32_t sessionId);
Wei Jiaec044b02018-02-19 12:41:23 -0800120 bool init();
121
Wei Jiaec044b02018-02-19 12:41:23 -0800122 // Disconnect from the currently connected ANativeWindow.
123 void disconnectNativeWindow_l();
124
Dichen Zhangc2465c52018-11-12 11:56:05 -0800125 status_t setAudioAttributes_l(const jobject attributes);
Wei Jiaec044b02018-02-19 12:41:23 -0800126
127 void clear_l();
Wei Jia800fe372018-02-20 15:00:45 -0800128 status_t seekTo_l(int64_t msec, MediaPlayer2SeekMode mode);
Wei Jiaec044b02018-02-19 12:41:23 -0800129 status_t prepareAsync_l();
Wei Jia800fe372018-02-20 15:00:45 -0800130 status_t getDuration_l(int64_t *msec);
Wei Jiaec044b02018-02-19 12:41:23 -0800131 status_t reset_l();
Dichen Zhangc2465c52018-11-12 11:56:05 -0800132 status_t checkState_l();
Wei Jiaec044b02018-02-19 12:41:23 -0800133
134 pid_t mPid;
135 uid_t mUid;
136 sp<MediaPlayer2Interface> mPlayer;
137 sp<MediaPlayer2AudioOutput> mAudioOutput;
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800138 int64_t mSrcId;
Wei Jia53692fa2017-12-11 10:33:46 -0800139 thread_id_t mLockThreadId;
Wei Jiaec044b02018-02-19 12:41:23 -0800140 mutable Mutex mLock;
Wei Jia53692fa2017-12-11 10:33:46 -0800141 Mutex mNotifyLock;
Wei Jia53692fa2017-12-11 10:33:46 -0800142 sp<MediaPlayer2Listener> mListener;
Wei Jia98787a72018-03-02 14:33:06 -0800143 media_player2_internal_states mCurrentState;
Wei Jia800fe372018-02-20 15:00:45 -0800144 int64_t mCurrentPosition;
Wei Jia53692fa2017-12-11 10:33:46 -0800145 MediaPlayer2SeekMode mCurrentSeekMode;
Wei Jia800fe372018-02-20 15:00:45 -0800146 int64_t mSeekPosition;
Wei Jia53692fa2017-12-11 10:33:46 -0800147 MediaPlayer2SeekMode mSeekMode;
Wei Jia53692fa2017-12-11 10:33:46 -0800148 audio_stream_type_t mStreamType;
Wei Jia53692fa2017-12-11 10:33:46 -0800149 bool mLoop;
Dichen Zhang7398ca02018-10-15 10:25:12 -0700150 float mVolume;
Wei Jia53692fa2017-12-11 10:33:46 -0800151 int mVideoWidth;
152 int mVideoHeight;
Dichen Zhang23658642018-11-15 10:26:16 -0800153 int32_t mAudioSessionId;
Dichen Zhangc2465c52018-11-12 11:56:05 -0800154 sp<JObjectHolder> mAudioAttributes;
Wei Jia53692fa2017-12-11 10:33:46 -0800155 float mSendLevel;
Wei Jiaec044b02018-02-19 12:41:23 -0800156 sp<ANativeWindowWrapper> mConnectedWindow;
Wei Jia53692fa2017-12-11 10:33:46 -0800157};
158
159}; // namespace android
160
161#endif // ANDROID_MEDIAPLAYER2_H