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
18namespace base {
19class BinaryValue;
20class DictionaryValue;
21}
22
23class CastAudioSink;
24class CastSession;
25class CastVideoSink;
26
27// A key value pair structure for codec specific parameters.
28struct 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.
37struct 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  // Minimum latency.
46  // Default value (0) means use max_latency_ms.
47  int min_latency_ms;
48
49  // RTP specific field to identify a stream.
50  int ssrc;
51
52  // RTP specific field to idenfity the feedback stream.
53  int feedback_ssrc;
54
55  // Update frequency of payload sample.
56  int clock_rate;
57
58  // Maximum bitrate in kilobits per second.
59  int max_bitrate;
60
61  // Minimum bitrate in kilobits per second.
62  int min_bitrate;
63
64  // Number of audio channels.
65  int channels;
66
67  // The maximum frame rate.
68  double max_frame_rate;
69
70  // Width and height of the video content.
71  int width;
72  int height;
73
74  // Name of the codec used.
75  std::string codec_name;
76
77  // AES encryption key.
78  std::string aes_key;
79
80  // AES encryption IV mask.
81  std::string aes_iv_mask;
82
83  // List of codec specific parameters.
84  std::vector<CastCodecSpecificParams> codec_specific_params;
85
86  CastRtpPayloadParams();
87  ~CastRtpPayloadParams();
88};
89
90// Defines the parameters of a RTP stream.
91struct CastRtpParams {
92  explicit CastRtpParams(const CastRtpPayloadParams& payload_params);
93
94  // Payload parameters.
95  CastRtpPayloadParams payload;
96
97  // Names of supported RTCP features.
98  std::vector<std::string> rtcp_features;
99
100  CastRtpParams();
101  ~CastRtpParams();
102};
103
104// This object represents a RTP stream that encodes and optionally
105// encrypt audio or video data from a WebMediaStreamTrack.
106// Note that this object does not actually output packets. It allows
107// configuration of encoding and RTP parameters and control such a logical
108// stream.
109class CastRtpStream {
110 public:
111  typedef base::Callback<void(const std::string&)> ErrorCallback;
112
113  CastRtpStream(const blink::WebMediaStreamTrack& track,
114                const scoped_refptr<CastSession>& session);
115  ~CastRtpStream();
116
117  // Return parameters currently supported by this stream.
118  std::vector<CastRtpParams> GetSupportedParams();
119
120  // Return parameters set to this stream.
121  CastRtpParams GetParams();
122
123  // Begin encoding of media stream and then submit the encoded streams
124  // to underlying transport.
125  // When the stream is started |start_callback| is called.
126  // When the stream is stopped |stop_callback| is called.
127  // When there is an error |error_callback| is called with a message.
128  void Start(const CastRtpParams& params,
129             const base::Closure& start_callback,
130             const base::Closure& stop_callback,
131             const ErrorCallback& error_callback);
132
133  // Stop encoding.
134  void Stop();
135
136  // Enables or disables logging for this stream.
137  void ToggleLogging(bool enable);
138
139  // Get serialized raw events for this stream with |extra_data| attached,
140  // and invokes |callback| with the result.
141  void GetRawEvents(
142      const base::Callback<void(scoped_ptr<base::BinaryValue>)>& callback,
143      const std::string& extra_data);
144
145  // Get stats in DictionaryValue format and invokves |callback| with
146  // the result.
147  void GetStats(const base::Callback<void(
148      scoped_ptr<base::DictionaryValue>)>& callback);
149
150 private:
151  // Return true if this track is an audio track. Return false if this
152  // track is a video track.
153  bool IsAudio() const;
154
155  void DidEncounterError(const std::string& message);
156
157  blink::WebMediaStreamTrack track_;
158  const scoped_refptr<CastSession> cast_session_;
159  scoped_ptr<CastAudioSink> audio_sink_;
160  scoped_ptr<CastVideoSink> video_sink_;
161  CastRtpParams params_;
162  base::Closure stop_callback_;
163  ErrorCallback error_callback_;
164
165  base::WeakPtrFactory<CastRtpStream> weak_factory_;
166
167  DISALLOW_COPY_AND_ASSIGN(CastRtpStream);
168};
169
170#endif  // CHROME_RENDERER_MEDIA_CAST_RTP_STREAM_H_
171