1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_RENDERER_MEDIA_CAST_RTP_STREAM_H_ 6 #define CHROME_RENDERER_MEDIA_CAST_RTP_STREAM_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/callback.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" 17 18 namespace base { 19 class BinaryValue; 20 class DictionaryValue; 21 } 22 23 class CastAudioSink; 24 class CastSession; 25 class CastVideoSink; 26 27 // A key value pair structure for codec specific parameters. 28 struct CastCodecSpecificParams { 29 std::string key; 30 std::string value; 31 32 CastCodecSpecificParams(); 33 ~CastCodecSpecificParams(); 34 }; 35 36 // Defines the basic properties of a payload supported by cast transport. 37 struct CastRtpPayloadParams { 38 // RTP specific field that identifies the content type. 39 int payload_type; 40 41 // Maximum latency in milliseconds. Implemetation tries to keep latency 42 // under this threshold. 43 int max_latency_ms; 44 45 // RTP specific field to identify a stream. 46 int ssrc; 47 48 // RTP specific field to idenfity the feedback stream. 49 int feedback_ssrc; 50 51 // Update frequency of payload sample. 52 int clock_rate; 53 54 // Maximum bitrate in kilobits per second. 55 int max_bitrate; 56 57 // Minimum bitrate in kilobits per second. 58 int min_bitrate; 59 60 // Number of audio channels. 61 int channels; 62 63 // Width and height of the video content. 64 int width; 65 int height; 66 67 // Name of the codec used. 68 std::string codec_name; 69 70 // AES encryption key. 71 std::string aes_key; 72 73 // AES encryption IV mask. 74 std::string aes_iv_mask; 75 76 // List of codec specific parameters. 77 std::vector<CastCodecSpecificParams> codec_specific_params; 78 79 CastRtpPayloadParams(); 80 ~CastRtpPayloadParams(); 81 }; 82 83 // Defines the parameters of a RTP stream. 84 struct CastRtpParams { 85 explicit CastRtpParams(const CastRtpPayloadParams& payload_params); 86 87 // Payload parameters. 88 CastRtpPayloadParams payload; 89 90 // Names of supported RTCP features. 91 std::vector<std::string> rtcp_features; 92 93 CastRtpParams(); 94 ~CastRtpParams(); 95 }; 96 97 // This object represents a RTP stream that encodes and optionally 98 // encrypt audio or video data from a WebMediaStreamTrack. 99 // Note that this object does not actually output packets. It allows 100 // configuration of encoding and RTP parameters and control such a logical 101 // stream. 102 class CastRtpStream { 103 public: 104 typedef base::Callback<void(const std::string&)> ErrorCallback; 105 106 CastRtpStream(const blink::WebMediaStreamTrack& track, 107 const scoped_refptr<CastSession>& session); 108 ~CastRtpStream(); 109 110 // Return parameters currently supported by this stream. 111 std::vector<CastRtpParams> GetSupportedParams(); 112 113 // Return parameters set to this stream. 114 CastRtpParams GetParams(); 115 116 // Begin encoding of media stream and then submit the encoded streams 117 // to underlying transport. 118 // When the stream is started |start_callback| is called. 119 // When the stream is stopped |stop_callback| is called. 120 // When there is an error |error_callback| is called with a message. 121 void Start(const CastRtpParams& params, 122 const base::Closure& start_callback, 123 const base::Closure& stop_callback, 124 const ErrorCallback& error_callback); 125 126 // Stop encoding. 127 void Stop(); 128 129 // Enables or disables logging for this stream. 130 void ToggleLogging(bool enable); 131 132 // Get serialized raw events for this stream with |extra_data| attached, 133 // and invokes |callback| with the result. 134 void GetRawEvents( 135 const base::Callback<void(scoped_ptr<base::BinaryValue>)>& callback, 136 const std::string& extra_data); 137 138 // Get stats in DictionaryValue format and invokves |callback| with 139 // the result. 140 void GetStats(const base::Callback<void( 141 scoped_ptr<base::DictionaryValue>)>& callback); 142 143 private: 144 // Return true if this track is an audio track. Return false if this 145 // track is a video track. 146 bool IsAudio() const; 147 148 void DidEncounterError(const std::string& message); 149 150 blink::WebMediaStreamTrack track_; 151 const scoped_refptr<CastSession> cast_session_; 152 scoped_ptr<CastAudioSink> audio_sink_; 153 scoped_ptr<CastVideoSink> video_sink_; 154 CastRtpParams params_; 155 base::WeakPtrFactory<CastRtpStream> weak_factory_; 156 base::Closure stop_callback_; 157 ErrorCallback error_callback_; 158 159 DISALLOW_COPY_AND_ASSIGN(CastRtpStream); 160 }; 161 162 #endif // CHROME_RENDERER_MEDIA_CAST_RTP_STREAM_H_ 163