MediaSender.h revision 3a9682a86ead84d6f60d3f3aa01b2b4d34af983d
1/*
2 * Copyright 2013, 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 MEDIA_SENDER_H_
18
19#define MEDIA_SENDER_H_
20
21#include "rtp/RTPSender.h"
22
23#include <media/stagefright/foundation/ABase.h>
24#include <media/stagefright/foundation/AHandler.h>
25#include <utils/Errors.h>
26#include <utils/Vector.h>
27
28namespace android {
29
30struct ABuffer;
31struct ANetworkSession;
32struct AMessage;
33struct IHDCP;
34struct TSPacketizer;
35
36// This class facilitates sending of data from one or more media tracks
37// through one or more RTP channels, either providing a 1:1 mapping from
38// track to RTP channel or muxing all tracks into a single RTP channel and
39// using transport stream encapsulation.
40// Optionally the (video) data is encrypted using the provided hdcp object.
41struct MediaSender : public AHandler {
42    enum {
43        kWhatInitDone,
44        kWhatError,
45        kWhatNetworkStall,
46    };
47
48    MediaSender(
49            const sp<ANetworkSession> &netSession,
50            const sp<AMessage> &notify);
51
52    status_t setHDCP(const sp<IHDCP> &hdcp);
53
54    enum FlagBits {
55        FLAG_MANUALLY_PREPEND_SPS_PPS = 1,
56    };
57    ssize_t addTrack(const sp<AMessage> &format, uint32_t flags);
58
59    // If trackIndex == -1, initialize for transport stream muxing.
60    status_t initAsync(
61            ssize_t trackIndex,
62            const char *remoteHost,
63            int32_t remoteRTPPort,
64            RTPSender::TransportMode rtpMode,
65            int32_t remoteRTCPPort,
66            RTPSender::TransportMode rtcpMode,
67            int32_t *localRTPPort);
68
69    status_t queueAccessUnit(
70            size_t trackIndex, const sp<ABuffer> &accessUnit);
71
72protected:
73    virtual void onMessageReceived(const sp<AMessage> &msg);
74    virtual ~MediaSender();
75
76private:
77    enum {
78        kWhatSenderNotify,
79    };
80
81    enum Mode {
82        MODE_UNDEFINED,
83        MODE_TRANSPORT_STREAM,
84        MODE_ELEMENTARY_STREAMS,
85    };
86
87    struct TrackInfo {
88        sp<AMessage> mFormat;
89        uint32_t mFlags;
90        sp<RTPSender> mSender;
91        List<sp<ABuffer> > mAccessUnits;
92        ssize_t mPacketizerTrackIndex;
93        bool mIsAudio;
94    };
95
96    sp<ANetworkSession> mNetSession;
97    sp<AMessage> mNotify;
98
99    sp<IHDCP> mHDCP;
100
101    Mode mMode;
102    int32_t mGeneration;
103
104    Vector<TrackInfo> mTrackInfos;
105
106    sp<TSPacketizer> mTSPacketizer;
107    sp<RTPSender> mTSSender;
108    int64_t mPrevTimeUs;
109
110    size_t mInitDoneCount;
111
112    FILE *mLogFile;
113
114    void onSenderNotify(const sp<AMessage> &msg);
115
116    void notifyInitDone(status_t err);
117    void notifyError(status_t err);
118    void notifyNetworkStall(size_t numBytesQueued);
119
120    status_t packetizeAccessUnit(
121            size_t trackIndex,
122            sp<ABuffer> accessUnit,
123            sp<ABuffer> *tsPackets);
124
125    DISALLOW_EVIL_CONSTRUCTORS(MediaSender);
126};
127
128}  // namespace android
129
130#endif  // MEDIA_SENDER_H_
131
132