blob: f2fc901ac30b8c9d63f3d3b967d3ff12c2cd71a4 [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
20#include <media/mediaplayer_common.h>
21
22#include <arpa/inet.h>
23
24#include <media/AudioResamplerPublic.h>
25#include <media/BufferingSettings.h>
26#include <media/MediaPlayer2EngineClient.h>
27#include <media/MediaPlayer2Engine.h>
28
29#include <utils/Condition.h>
30#include <utils/KeyedVector.h>
31#include <utils/String8.h>
32#include <utils/ThreadDefs.h>
33
Wei Jia53692fa2017-12-11 10:33:46 -080034namespace android {
35
36struct AVSyncSettings;
Wei Jia28288fb2017-12-15 13:45:29 -080037struct ANativeWindowWrapper;
Wei Jiac5c79da2017-12-21 18:03:05 -080038class DataSource;
Wei Jiac2636032018-02-01 09:15:25 -080039struct DataSourceDesc;
Wei Jiac5c79da2017-12-21 18:03:05 -080040struct MediaHTTPService;
Wei Jia53692fa2017-12-11 10:33:46 -080041
42enum media2_event_type {
43 MEDIA2_NOP = 0, // interface test message
44 MEDIA2_PREPARED = 1,
45 MEDIA2_PLAYBACK_COMPLETE = 2,
46 MEDIA2_BUFFERING_UPDATE = 3,
47 MEDIA2_SEEK_COMPLETE = 4,
48 MEDIA2_SET_VIDEO_SIZE = 5,
49 MEDIA2_STARTED = 6,
50 MEDIA2_PAUSED = 7,
51 MEDIA2_STOPPED = 8,
52 MEDIA2_SKIPPED = 9,
53 MEDIA2_NOTIFY_TIME = 98,
54 MEDIA2_TIMED_TEXT = 99,
55 MEDIA2_ERROR = 100,
56 MEDIA2_INFO = 200,
57 MEDIA2_SUBTITLE_DATA = 201,
58 MEDIA2_META_DATA = 202,
59 MEDIA2_DRM_INFO = 210,
60 MEDIA2_AUDIO_ROUTING_CHANGED = 10000,
61};
62
63// Generic error codes for the media player framework. Errors are fatal, the
64// playback must abort.
65//
66// Errors are communicated back to the client using the
67// MediaPlayer2Listener::notify method defined below.
68// In this situation, 'notify' is invoked with the following:
69// 'msg' is set to MEDIA_ERROR.
70// 'ext1' should be a value from the enum media2_error_type.
71// 'ext2' contains an implementation dependant error code to provide
72// more details. Should default to 0 when not used.
73//
74// The codes are distributed as follow:
75// 0xx: Reserved
76// 1xx: Android Player errors. Something went wrong inside the MediaPlayer2.
77// 2xx: Media errors (e.g Codec not supported). There is a problem with the
78// media itself.
79// 3xx: Runtime errors. Some extraordinary condition arose making the playback
80// impossible.
81//
82enum media2_error_type {
83 // 0xx
84 MEDIA2_ERROR_UNKNOWN = 1,
85 // 1xx
86 MEDIA2_ERROR_SERVER_DIED = 100,
87 // 2xx
88 MEDIA2_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200,
89 // 3xx
90};
91
92
93// Info and warning codes for the media player framework. These are non fatal,
94// the playback is going on but there might be some user visible issues.
95//
96// Info and warning messages are communicated back to the client using the
97// MediaPlayer2Listener::notify method defined below. In this situation,
98// 'notify' is invoked with the following:
99// 'msg' is set to MEDIA_INFO.
100// 'ext1' should be a value from the enum media2_info_type.
101// 'ext2' contains an implementation dependant info code to provide
102// more details. Should default to 0 when not used.
103//
104// The codes are distributed as follow:
105// 0xx: Reserved
106// 7xx: Android Player info/warning (e.g player lagging behind.)
107// 8xx: Media info/warning (e.g media badly interleaved.)
108//
109enum media2_info_type {
110 // 0xx
111 MEDIA2_INFO_UNKNOWN = 1,
112 // The player was started because it was used as the next player for another
113 // player, which just completed playback
114 MEDIA2_INFO_STARTED_AS_NEXT = 2,
115 // The player just pushed the very first video frame for rendering
116 MEDIA2_INFO_RENDERING_START = 3,
117 // 7xx
118 // The video is too complex for the decoder: it can't decode frames fast
119 // enough. Possibly only the audio plays fine at this stage.
120 MEDIA2_INFO_VIDEO_TRACK_LAGGING = 700,
121 // MediaPlayer2 is temporarily pausing playback internally in order to
122 // buffer more data.
123 MEDIA2_INFO_BUFFERING_START = 701,
124 // MediaPlayer2 is resuming playback after filling buffers.
125 MEDIA2_INFO_BUFFERING_END = 702,
126 // Bandwidth in recent past
127 MEDIA2_INFO_NETWORK_BANDWIDTH = 703,
128
129 // 8xx
130 // Bad interleaving means that a media has been improperly interleaved or not
131 // interleaved at all, e.g has all the video samples first then all the audio
132 // ones. Video is playing but a lot of disk seek may be happening.
133 MEDIA2_INFO_BAD_INTERLEAVING = 800,
134 // The media is not seekable (e.g live stream).
135 MEDIA2_INFO_NOT_SEEKABLE = 801,
136 // New media metadata is available.
137 MEDIA2_INFO_METADATA_UPDATE = 802,
138 // Audio can not be played.
139 MEDIA2_INFO_PLAY_AUDIO_ERROR = 804,
140 // Video can not be played.
141 MEDIA2_INFO_PLAY_VIDEO_ERROR = 805,
142
143 //9xx
144 MEDIA2_INFO_TIMED_TEXT_ERROR = 900,
145};
146
147
148
149enum media_player2_states {
150 MEDIA_PLAYER2_STATE_ERROR = 0,
151 MEDIA_PLAYER2_IDLE = 1 << 0,
152 MEDIA_PLAYER2_INITIALIZED = 1 << 1,
153 MEDIA_PLAYER2_PREPARING = 1 << 2,
154 MEDIA_PLAYER2_PREPARED = 1 << 3,
155 MEDIA_PLAYER2_STARTED = 1 << 4,
156 MEDIA_PLAYER2_PAUSED = 1 << 5,
157 MEDIA_PLAYER2_STOPPED = 1 << 6,
158 MEDIA_PLAYER2_PLAYBACK_COMPLETE = 1 << 7
159};
160
161// Keep KEY_PARAMETER_* in sync with MediaPlayer2.java.
162// The same enum space is used for both set and get, in case there are future keys that
163// can be both set and get. But as of now, all parameters are either set only or get only.
164enum media2_parameter_keys {
165 // Streaming/buffering parameters
166 MEDIA2_KEY_PARAMETER_CACHE_STAT_COLLECT_FREQ_MS = 1100, // set only
167
168 // Return a Parcel containing a single int, which is the channel count of the
169 // audio track, or zero for error (e.g. no audio track) or unknown.
170 MEDIA2_KEY_PARAMETER_AUDIO_CHANNEL_COUNT = 1200, // get only
171
172 // Playback rate expressed in permille (1000 is normal speed), saved as int32_t, with negative
173 // values used for rewinding or reverse playback.
174 MEDIA2_KEY_PARAMETER_PLAYBACK_RATE_PERMILLE = 1300, // set only
175
176 // Set a Parcel containing the value of a parcelled Java AudioAttribute instance
177 MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES = 1400 // set only
178};
179
180// Keep INVOKE_ID_* in sync with MediaPlayer2.java.
181enum media_player2_invoke_ids {
182 MEDIA_PLAYER2_INVOKE_ID_GET_TRACK_INFO = 1,
183 MEDIA_PLAYER2_INVOKE_ID_ADD_EXTERNAL_SOURCE = 2,
184 MEDIA_PLAYER2_INVOKE_ID_ADD_EXTERNAL_SOURCE_FD = 3,
185 MEDIA_PLAYER2_INVOKE_ID_SELECT_TRACK = 4,
186 MEDIA_PLAYER2_INVOKE_ID_UNSELECT_TRACK = 5,
187 MEDIA_PLAYER2_INVOKE_ID_SET_VIDEO_SCALING_MODE = 6,
188 MEDIA_PLAYER2_INVOKE_ID_GET_SELECTED_TRACK = 7
189};
190
191// ----------------------------------------------------------------------------
192// ref-counted object for callbacks
193class MediaPlayer2Listener: virtual public RefBase
194{
195public:
196 virtual void notify(int msg, int ext1, int ext2, const Parcel *obj) = 0;
197};
198
Wei Jia53692fa2017-12-11 10:33:46 -0800199class MediaPlayer2 : public MediaPlayer2EngineClient
200{
201public:
202 MediaPlayer2();
203 ~MediaPlayer2();
204 void disconnect();
205
Wei Jiac2636032018-02-01 09:15:25 -0800206 status_t setDataSource(const sp<DataSourceDesc> &dsd);
Wei Jia28288fb2017-12-15 13:45:29 -0800207 status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww);
Wei Jia53692fa2017-12-11 10:33:46 -0800208 status_t setListener(const sp<MediaPlayer2Listener>& listener);
209 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
210 status_t setBufferingSettings(const BufferingSettings& buffering);
211 status_t prepare();
212 status_t prepareAsync();
213 status_t start();
214 status_t stop();
215 status_t pause();
216 bool isPlaying();
217 status_t setPlaybackSettings(const AudioPlaybackRate& rate);
218 status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
219 status_t setSyncSettings(const AVSyncSettings& sync, float videoFpsHint);
220 status_t getSyncSettings(
221 AVSyncSettings* sync /* nonnull */,
222 float* videoFps /* nonnull */);
223 status_t getVideoWidth(int *w);
224 status_t getVideoHeight(int *h);
225 status_t seekTo(
226 int msec,
227 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
228 status_t notifyAt(int64_t mediaTimeUs);
229 status_t getCurrentPosition(int *msec);
230 status_t getDuration(int *msec);
231 status_t reset();
232 status_t setAudioStreamType(audio_stream_type_t type);
233 status_t getAudioStreamType(audio_stream_type_t *type);
234 status_t setLooping(int loop);
235 bool isLooping();
236 status_t setVolume(float leftVolume, float rightVolume);
237 void notify(int msg, int ext1, int ext2, const Parcel *obj = NULL);
238 status_t invoke(const Parcel& request, Parcel *reply);
239 status_t setMetadataFilter(const Parcel& filter);
240 status_t getMetadata(bool update_only, bool apply_filter, Parcel *metadata);
241 status_t setAudioSessionId(audio_session_t sessionId);
242 audio_session_t getAudioSessionId();
243 status_t setAuxEffectSendLevel(float level);
244 status_t attachAuxEffect(int effectId);
245 status_t setParameter(int key, const Parcel& request);
246 status_t getParameter(int key, Parcel* reply);
247 status_t setRetransmitEndpoint(const char* addrString, uint16_t port);
248 status_t setNextMediaPlayer(const sp<MediaPlayer2>& player);
249
250 media::VolumeShaper::Status applyVolumeShaper(
251 const sp<media::VolumeShaper::Configuration>& configuration,
252 const sp<media::VolumeShaper::Operation>& operation);
253 sp<media::VolumeShaper::State> getVolumeShaperState(int id);
254 // Modular DRM
255 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId);
256 status_t releaseDrm();
257 // AudioRouting
258 status_t setOutputDevice(audio_port_handle_t deviceId);
259 audio_port_handle_t getRoutedDeviceId();
260 status_t enableAudioDeviceCallback(bool enabled);
261
262private:
263 void clear_l();
264 status_t seekTo_l(int msec, MediaPlayer2SeekMode mode);
265 status_t prepareAsync_l();
266 status_t getDuration_l(int *msec);
267 status_t attachNewPlayer(const sp<MediaPlayer2Engine>& player);
268 status_t reset_l();
269 status_t doSetRetransmitEndpoint(const sp<MediaPlayer2Engine>& player);
270 status_t checkStateForKeySet_l(int key);
271
272 sp<MediaPlayer2Engine> mPlayer;
273 thread_id_t mLockThreadId;
274 Mutex mLock;
275 Mutex mNotifyLock;
276 Condition mSignal;
277 sp<MediaPlayer2Listener> mListener;
278 void* mCookie;
279 media_player2_states mCurrentState;
280 int mCurrentPosition;
281 MediaPlayer2SeekMode mCurrentSeekMode;
282 int mSeekPosition;
283 MediaPlayer2SeekMode mSeekMode;
284 bool mPrepareSync;
285 status_t mPrepareStatus;
286 audio_stream_type_t mStreamType;
287 Parcel* mAudioAttributesParcel;
288 bool mLoop;
289 float mLeftVolume;
290 float mRightVolume;
291 int mVideoWidth;
292 int mVideoHeight;
293 audio_session_t mAudioSessionId;
294 float mSendLevel;
295 struct sockaddr_in mRetransmitEndpoint;
296 bool mRetransmitEndpointValid;
297};
298
299}; // namespace android
300
301#endif // ANDROID_MEDIAPLAYER2_H