1/*
2 * Copyright 2012, 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 PLAYBACK_SESSION_H_
18
19#define PLAYBACK_SESSION_H_
20
21#include "Sender.h"
22#include "WifiDisplaySource.h"
23
24namespace android {
25
26struct ABuffer;
27struct BufferQueue;
28struct IHDCP;
29struct ISurfaceTexture;
30struct MediaPuller;
31struct MediaSource;
32struct TSPacketizer;
33
34// Encapsulates the state of an RTP/RTCP session in the context of wifi
35// display.
36struct WifiDisplaySource::PlaybackSession : public AHandler {
37    PlaybackSession(
38            const sp<ANetworkSession> &netSession,
39            const sp<AMessage> &notify,
40            const struct in_addr &interfaceAddr,
41            const sp<IHDCP> &hdcp);
42
43    status_t init(
44            const char *clientIP, int32_t clientRtp, int32_t clientRtcp,
45            Sender::TransportMode transportMode,
46            bool usePCMAudio);
47
48    void destroyAsync();
49
50    int32_t getRTPPort() const;
51
52    int64_t getLastLifesignUs() const;
53    void updateLiveness();
54
55    status_t play();
56    status_t finishPlay();
57    status_t pause();
58
59    sp<ISurfaceTexture> getSurfaceTexture();
60    int32_t width() const;
61    int32_t height() const;
62
63    void requestIDRFrame();
64
65    enum {
66        kWhatSessionDead,
67        kWhatBinaryData,
68        kWhatSessionEstablished,
69        kWhatSessionDestroyed,
70    };
71
72protected:
73    virtual void onMessageReceived(const sp<AMessage> &msg);
74    virtual ~PlaybackSession();
75
76private:
77    struct Track;
78
79    enum {
80        kWhatMediaPullerNotify,
81        kWhatConverterNotify,
82        kWhatTrackNotify,
83        kWhatSenderNotify,
84        kWhatUpdateSurface,
85        kWhatFinishPlay,
86        kWhatPacketize,
87        kWhatPause,
88        kWhatResume,
89    };
90
91    sp<ANetworkSession> mNetSession;
92    sp<Sender> mSender;
93    sp<ALooper> mSenderLooper;
94    sp<AMessage> mNotify;
95    in_addr mInterfaceAddr;
96    sp<IHDCP> mHDCP;
97    bool mWeAreDead;
98    bool mPaused;
99
100    int64_t mLastLifesignUs;
101
102    sp<TSPacketizer> mPacketizer;
103    sp<BufferQueue> mBufferQueue;
104
105    KeyedVector<size_t, sp<Track> > mTracks;
106    ssize_t mVideoTrackIndex;
107
108    int64_t mPrevTimeUs;
109
110    bool mAllTracksHavePacketizerIndex;
111
112    status_t setupPacketizer(bool usePCMAudio);
113
114    status_t addSource(
115            bool isVideo,
116            const sp<MediaSource> &source,
117            bool isRepeaterSource,
118            bool usePCMAudio,
119            size_t *numInputBuffers);
120
121    status_t addVideoSource();
122    status_t addAudioSource(bool usePCMAudio);
123
124    ssize_t appendTSData(
125            const void *data, size_t size, bool timeDiscontinuity, bool flush);
126
127    status_t onFinishPlay();
128    status_t onFinishPlay2();
129
130    bool allTracksHavePacketizerIndex();
131
132    status_t packetizeAccessUnit(
133            size_t trackIndex, sp<ABuffer> accessUnit,
134            sp<ABuffer> *packets);
135
136    status_t packetizeQueuedAccessUnits();
137
138    void notifySessionDead();
139
140    void drainAccessUnits();
141
142    // Returns true iff an access unit was successfully drained.
143    bool drainAccessUnit();
144
145    DISALLOW_EVIL_CONSTRUCTORS(PlaybackSession);
146};
147
148}  // namespace android
149
150#endif  // PLAYBACK_SESSION_H_
151
152