MediaSender.h revision 126568c7aeeb5570789e70a310477f44dbdbd885
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            RTPSender::TransportMode transportMode,
63            const char *remoteHost,
64            int32_t remoteRTPPort,
65            int32_t remoteRTCPPort,
66            int32_t *localRTPPort);
67
68    status_t queueAccessUnit(
69            size_t trackIndex, const sp<ABuffer> &accessUnit);
70
71protected:
72    virtual void onMessageReceived(const sp<AMessage> &msg);
73    virtual ~MediaSender();
74
75private:
76    enum {
77        kWhatSenderNotify,
78    };
79
80    enum Mode {
81        MODE_UNDEFINED,
82        MODE_TRANSPORT_STREAM,
83        MODE_ELEMENTARY_STREAMS,
84    };
85
86    struct TrackInfo {
87        sp<AMessage> mFormat;
88        uint32_t mFlags;
89        sp<RTPSender> mSender;
90        List<sp<ABuffer> > mAccessUnits;
91        ssize_t mPacketizerTrackIndex;
92        bool mIsAudio;
93    };
94
95    sp<ANetworkSession> mNetSession;
96    sp<AMessage> mNotify;
97
98    sp<IHDCP> mHDCP;
99
100    Mode mMode;
101    int32_t mGeneration;
102
103    Vector<TrackInfo> mTrackInfos;
104
105    sp<TSPacketizer> mTSPacketizer;
106    sp<RTPSender> mTSSender;
107    int64_t mPrevTimeUs;
108
109    size_t mInitDoneCount;
110
111    FILE *mLogFile;
112
113    void onSenderNotify(const sp<AMessage> &msg);
114
115    void notifyInitDone(status_t err);
116    void notifyError(status_t err);
117    void notifyNetworkStall(size_t numBytesQueued);
118
119    status_t packetizeAccessUnit(
120            size_t trackIndex,
121            sp<ABuffer> accessUnit,
122            sp<ABuffer> *tsPackets);
123
124    DISALLOW_EVIL_CONSTRUCTORS(MediaSender);
125};
126
127}  // namespace android
128
129#endif  // MEDIA_SENDER_H_
130
131