• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 NUPLAYER_RENDERER_H_
18 
19 #define NUPLAYER_RENDERER_H_
20 
21 #include "NuPlayer.h"
22 
23 namespace android {
24 
25 struct ABuffer;
26 
27 struct NuPlayer::Renderer : public AHandler {
28     enum Flags {
29         FLAG_REAL_TIME = 1,
30     };
31     Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
32              const sp<AMessage> &notify,
33              uint32_t flags = 0);
34 
35     void queueBuffer(
36             bool audio,
37             const sp<ABuffer> &buffer,
38             const sp<AMessage> &notifyConsumed);
39 
40     void queueEOS(bool audio, status_t finalResult);
41 
42     void flush(bool audio);
43 
44     void signalTimeDiscontinuity();
45 
46     void signalAudioSinkChanged();
47 
48     void pause();
49     void resume();
50 
51     enum {
52         kWhatEOS                 = 'eos ',
53         kWhatFlushComplete       = 'fluC',
54         kWhatPosition            = 'posi',
55         kWhatVideoRenderingStart = 'vdrd',
56     };
57 
58 protected:
59     virtual ~Renderer();
60 
61     virtual void onMessageReceived(const sp<AMessage> &msg);
62 
63 private:
64     enum {
65         kWhatDrainAudioQueue    = 'draA',
66         kWhatDrainVideoQueue    = 'draV',
67         kWhatQueueBuffer        = 'queB',
68         kWhatQueueEOS           = 'qEOS',
69         kWhatFlush              = 'flus',
70         kWhatAudioSinkChanged   = 'auSC',
71         kWhatPause              = 'paus',
72         kWhatResume             = 'resm',
73     };
74 
75     struct QueueEntry {
76         sp<ABuffer> mBuffer;
77         sp<AMessage> mNotifyConsumed;
78         size_t mOffset;
79         status_t mFinalResult;
80     };
81 
82     static const int64_t kMinPositionUpdateDelayUs;
83 
84     sp<MediaPlayerBase::AudioSink> mAudioSink;
85     sp<AMessage> mNotify;
86     uint32_t mFlags;
87     List<QueueEntry> mAudioQueue;
88     List<QueueEntry> mVideoQueue;
89     uint32_t mNumFramesWritten;
90 
91     bool mDrainAudioQueuePending;
92     bool mDrainVideoQueuePending;
93     int32_t mAudioQueueGeneration;
94     int32_t mVideoQueueGeneration;
95 
96     int64_t mAnchorTimeMediaUs;
97     int64_t mAnchorTimeRealUs;
98 
99     Mutex mFlushLock;  // protects the following 2 member vars.
100     bool mFlushingAudio;
101     bool mFlushingVideo;
102 
103     bool mHasAudio;
104     bool mHasVideo;
105     bool mSyncQueues;
106 
107     bool mPaused;
108     bool mVideoRenderingStarted;
109 
110     int64_t mLastPositionUpdateUs;
111     int64_t mVideoLateByUs;
112 
113     bool onDrainAudioQueue();
114     void postDrainAudioQueue(int64_t delayUs = 0);
115 
116     void onDrainVideoQueue();
117     void postDrainVideoQueue();
118 
119     void onQueueBuffer(const sp<AMessage> &msg);
120     void onQueueEOS(const sp<AMessage> &msg);
121     void onFlush(const sp<AMessage> &msg);
122     void onAudioSinkChanged();
123     void onPause();
124     void onResume();
125 
126     void notifyEOS(bool audio, status_t finalResult);
127     void notifyFlushComplete(bool audio);
128     void notifyPosition();
129     void notifyVideoLateBy(int64_t lateByUs);
130     void notifyVideoRenderingStart();
131 
132     void flushQueue(List<QueueEntry> *queue);
133     bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
134     void syncQueuesDone();
135 
136     DISALLOW_EVIL_CONSTRUCTORS(Renderer);
137 };
138 
139 }  // namespace android
140 
141 #endif  // NUPLAYER_RENDERER_H_
142