AwesomePlayer.h revision 66b0a35c9a4136a84c3f73b1b25fcb5220138b95
1/*
2 * Copyright (C) 2009 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 AWESOME_PLAYER_H_
18
19#define AWESOME_PLAYER_H_
20
21#include "TimedEventQueue.h"
22
23#include <media/MediaPlayerInterface.h>
24#include <media/stagefright/OMXClient.h>
25#include <utils/threads.h>
26
27namespace android {
28
29struct AudioPlayer;
30struct MediaBuffer;
31struct MediaExtractor;
32struct MediaSource;
33struct Prefetcher;
34struct TimeSource;
35
36struct AwesomeRenderer : public RefBase {
37    AwesomeRenderer() {}
38
39    virtual void render(MediaBuffer *buffer) = 0;
40
41private:
42    AwesomeRenderer(const AwesomeRenderer &);
43    AwesomeRenderer &operator=(const AwesomeRenderer &);
44};
45
46struct AwesomePlayer {
47    AwesomePlayer();
48    ~AwesomePlayer();
49
50    void setListener(const wp<MediaPlayerBase> &listener);
51
52    status_t setDataSource(const char *uri);
53    status_t setDataSource(int fd, int64_t offset, int64_t length);
54
55    void reset();
56
57    status_t play();
58    status_t pause();
59
60    bool isPlaying() const;
61
62    void setISurface(const sp<ISurface> &isurface);
63    void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
64    status_t setLooping(bool shouldLoop);
65
66    status_t getDuration(int64_t *durationUs);
67    status_t getPosition(int64_t *positionUs);
68
69    status_t seekTo(int64_t timeUs);
70
71    status_t getVideoDimensions(int32_t *width, int32_t *height) const;
72
73private:
74    friend struct AwesomeEvent;
75
76    enum Flags {
77        PLAYING     = 1,
78        LOOPING     = 2,
79        FIRST_FRAME = 4,
80    };
81
82    mutable Mutex mLock;
83
84    OMXClient mClient;
85    TimedEventQueue mQueue;
86    wp<MediaPlayerBase> mListener;
87
88    sp<ISurface> mISurface;
89    sp<MediaPlayerBase::AudioSink> mAudioSink;
90
91    TimeSource *mTimeSource;
92
93    sp<MediaSource> mVideoSource;
94    sp<AwesomeRenderer> mVideoRenderer;
95
96    sp<MediaSource> mAudioSource;
97    AudioPlayer *mAudioPlayer;
98    int64_t mDurationUs;
99
100    uint32_t mFlags;
101
102    int32_t mVideoWidth, mVideoHeight;
103    int64_t mTimeSourceDeltaUs;
104    int64_t mVideoTimeUs;
105
106    bool mSeeking;
107    int64_t mSeekTimeUs;
108
109    sp<TimedEventQueue::Event> mVideoEvent;
110    bool mVideoEventPending;
111    sp<TimedEventQueue::Event> mStreamDoneEvent;
112    bool mStreamDoneEventPending;
113    sp<TimedEventQueue::Event> mBufferingEvent;
114    bool mBufferingEventPending;
115
116    void postVideoEvent_l(int64_t delayUs = -1);
117    void postBufferingEvent_l();
118    void postStreamDoneEvent_l();
119
120    MediaBuffer *mLastVideoBuffer;
121    MediaBuffer *mVideoBuffer;
122
123    sp<Prefetcher> mPrefetcher;
124
125    status_t setDataSource_l(const sp<MediaExtractor> &extractor);
126    void reset_l();
127    status_t seekTo_l(int64_t timeUs);
128    status_t pause_l();
129    void initRenderer_l();
130    void seekAudioIfNecessary_l();
131
132    void cancelPlayerEvents(bool keepBufferingGoing = false);
133
134    status_t setAudioSource(sp<MediaSource> source);
135    status_t setVideoSource(sp<MediaSource> source);
136
137    void onEvent(int32_t code);
138
139    static void AudioNotify(void *me, int what);
140    void onStreamDone();
141
142    void notifyListener_l(int msg, int ext1 = 0);
143
144    void onBufferingUpdate();
145
146    AwesomePlayer(const AwesomePlayer &);
147    AwesomePlayer &operator=(const AwesomePlayer &);
148};
149
150}  // namespace android
151
152#endif  // AWESOME_PLAYER_H_
153
154