blob: 6dc578806dcb57a2f16cfa41805e294ee590327f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_MEDIA_BASE_RTPDATAENGINE_H_
29#define TALK_MEDIA_BASE_RTPDATAENGINE_H_
30
31#include <string>
32#include <vector>
33
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/media/base/constants.h"
36#include "talk/media/base/mediachannel.h"
37#include "talk/media/base/mediaengine.h"
38
39namespace cricket {
40
41struct DataCodec;
42
43class RtpDataEngine : public DataEngineInterface {
44 public:
45 RtpDataEngine();
46
47 virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type);
48
49 virtual const std::vector<DataCodec>& data_codecs() {
50 return data_codecs_;
51 }
52
53 // Mostly for testing with a fake clock. Ownership is passed in.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000054 void SetTiming(rtc::Timing* timing) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 timing_.reset(timing);
56 }
57
58 private:
59 std::vector<DataCodec> data_codecs_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000060 rtc::scoped_ptr<rtc::Timing> timing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061};
62
63// Keep track of sequence number and timestamp of an RTP stream. The
64// sequence number starts with a "random" value and increments. The
65// timestamp starts with a "random" value and increases monotonically
66// according to the clockrate.
67class RtpClock {
68 public:
69 RtpClock(int clockrate, uint16 first_seq_num, uint32 timestamp_offset)
70 : clockrate_(clockrate),
71 last_seq_num_(first_seq_num),
72 timestamp_offset_(timestamp_offset) {
73 }
74
75 // Given the current time (in number of seconds which must be
76 // monotonically increasing), Return the next sequence number and
77 // timestamp.
78 void Tick(double now, int* seq_num, uint32* timestamp);
79
80 private:
81 int clockrate_;
82 uint16 last_seq_num_;
83 uint32 timestamp_offset_;
84};
85
86class RtpDataMediaChannel : public DataMediaChannel {
87 public:
88 // Timing* Used for the RtpClock
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 explicit RtpDataMediaChannel(rtc::Timing* timing);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 // Sets Timing == NULL, so you'll need to call set_timer() before
91 // using it. This is needed by FakeMediaEngine.
92 RtpDataMediaChannel();
93 virtual ~RtpDataMediaChannel();
94
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 void set_timing(rtc::Timing* timing) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 timing_ = timing;
97 }
98
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000099 virtual bool SetStartSendBandwidth(int bps) { return true; }
100 virtual bool SetMaxSendBandwidth(int bps);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 virtual bool SetRecvRtpHeaderExtensions(
102 const std::vector<RtpHeaderExtension>& extensions) { return true; }
103 virtual bool SetSendRtpHeaderExtensions(
104 const std::vector<RtpHeaderExtension>& extensions) { return true; }
105 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs);
106 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs);
107 virtual bool AddSendStream(const StreamParams& sp);
108 virtual bool RemoveSendStream(uint32 ssrc);
109 virtual bool AddRecvStream(const StreamParams& sp);
110 virtual bool RemoveRecvStream(uint32 ssrc);
111 virtual bool SetSend(bool send) {
112 sending_ = send;
113 return true;
114 }
115 virtual bool SetReceive(bool receive) {
116 receiving_ = receive;
117 return true;
118 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000119 virtual void OnPacketReceived(rtc::Buffer* packet,
120 const rtc::PacketTime& packet_time);
121 virtual void OnRtcpReceived(rtc::Buffer* packet,
122 const rtc::PacketTime& packet_time) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 virtual void OnReadyToSend(bool ready) {}
124 virtual bool SendData(
125 const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000126 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 SendDataResult* result);
128
129 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000130 void Construct(rtc::Timing* timing);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131
132 bool sending_;
133 bool receiving_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000134 rtc::Timing* timing_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 std::vector<DataCodec> send_codecs_;
136 std::vector<DataCodec> recv_codecs_;
137 std::vector<StreamParams> send_streams_;
138 std::vector<StreamParams> recv_streams_;
139 std::map<uint32, RtpClock*> rtp_clock_by_send_ssrc_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000140 rtc::scoped_ptr<rtc::RateLimiter> send_limiter_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141};
142
143} // namespace cricket
144
145#endif // TALK_MEDIA_BASE_RTPDATAENGINE_H_