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 A_RTP_WRITER_H_
18 
19 #define A_RTP_WRITER_H_
20 
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/foundation/AHandlerReflector.h>
23 #include <media/stagefright/foundation/AString.h>
24 #include <media/stagefright/foundation/base64.h>
25 #include <media/stagefright/MediaWriter.h>
26 
27 #include <arpa/inet.h>
28 #include <sys/socket.h>
29 
30 #define LOG_TO_FILES    0
31 
32 namespace android {
33 
34 struct ABuffer;
35 class MediaBuffer;
36 
37 struct ARTPWriter : public MediaWriter {
38     explicit ARTPWriter(int fd);
39 
40     virtual status_t addSource(const sp<MediaSource> &source);
41     virtual bool reachedEOS();
42     virtual status_t start(MetaData *params);
43     virtual status_t stop();
44     virtual status_t pause();
45 
46     virtual void onMessageReceived(const sp<AMessage> &msg);
47 
48 protected:
49     virtual ~ARTPWriter();
50 
51 private:
52     enum {
53         kWhatStart  = 'strt',
54         kWhatStop   = 'stop',
55         kWhatRead   = 'read',
56         kWhatSendSR = 'sr  ',
57     };
58 
59     enum {
60         kFlagStarted  = 1,
61         kFlagEOS      = 2,
62     };
63 
64     Mutex mLock;
65     Condition mCondition;
66     uint32_t mFlags;
67 
68     int mFd;
69 
70 #if LOG_TO_FILES
71     int mRTPFd;
72     int mRTCPFd;
73 #endif
74 
75     sp<MediaSource> mSource;
76     sp<ALooper> mLooper;
77     sp<AHandlerReflector<ARTPWriter> > mReflector;
78 
79     int mSocket;
80     struct sockaddr_in mRTPAddr;
81     struct sockaddr_in mRTCPAddr;
82 
83     AString mProfileLevel;
84     AString mSeqParamSet;
85     AString mPicParamSet;
86 
87     uint32_t mSourceID;
88     uint32_t mSeqNo;
89     uint32_t mRTPTimeBase;
90     uint32_t mNumRTPSent;
91     uint32_t mNumRTPOctetsSent;
92     uint32_t mLastRTPTime;
93     uint64_t mLastNTPTime;
94 
95     int32_t mNumSRsSent;
96 
97     enum {
98         INVALID,
99         H264,
100         H263,
101         AMR_NB,
102         AMR_WB,
103     } mMode;
104 
105     static uint64_t GetNowNTP();
106 
107     void onRead(const sp<AMessage> &msg);
108     void onSendSR(const sp<AMessage> &msg);
109 
110     void addSR(const sp<ABuffer> &buffer);
111     void addSDES(const sp<ABuffer> &buffer);
112 
113     void makeH264SPropParamSets(MediaBufferBase *buffer);
114     void dumpSessionDesc();
115 
116     void sendBye();
117     void sendAVCData(MediaBufferBase *mediaBuf);
118     void sendH263Data(MediaBufferBase *mediaBuf);
119     void sendAMRData(MediaBufferBase *mediaBuf);
120 
121     void send(const sp<ABuffer> &buffer, bool isRTCP);
122 
123     DISALLOW_EVIL_CONSTRUCTORS(ARTPWriter);
124 };
125 
126 }  // namespace android
127 
128 #endif  // A_RTP_WRITER_H_
129