1// Copyright 2014 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 CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_TRACK_METRICS_H_
6#define CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_TRACK_METRICS_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_vector.h"
10#include "base/threading/non_thread_safe.h"
11#include "content/common/content_export.h"
12#include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
13
14namespace webrtc {
15class MediaStreamInterface;
16class MediaStreamTrackInterface;
17}
18
19namespace content {
20
21class MediaStreamTrackMetricsObserver;
22class RTCPeerConnectionHandler;
23
24// Responsible for observing the connected lifetimes of tracks going
25// over a PeerConnection, and sending messages to the browser process
26// about lifetime events.
27//
28// There should be exactly one of these objects owned by each
29// RTCPeerConnectionHandler, and its lifetime should match the
30// lifetime of its owner.
31class CONTENT_EXPORT MediaStreamTrackMetrics : public base::NonThreadSafe {
32 public:
33  explicit MediaStreamTrackMetrics();
34  ~MediaStreamTrackMetrics();
35
36  enum StreamType { SENT_STREAM, RECEIVED_STREAM };
37
38  enum TrackType { AUDIO_TRACK, VIDEO_TRACK };
39
40  enum LifetimeEvent { CONNECTED, DISCONNECTED };
41
42  // Starts tracking lifetimes of all the tracks in |stream| and any
43  // tracks added or removed to/from the stream until |RemoveStream|
44  // is called or this object's lifetime ends.
45  void AddStream(StreamType type, webrtc::MediaStreamInterface* stream);
46
47  // Stops tracking lifetimes of tracks in |stream|.
48  void RemoveStream(StreamType type, webrtc::MediaStreamInterface* stream);
49
50  // Called to indicate changes in the ICE connection state for the
51  // PeerConnection this object is associated with. Used to generate
52  // the connected/disconnected lifetime events for these tracks.
53  void IceConnectionChange(
54      webrtc::PeerConnectionInterface::IceConnectionState new_state);
55
56  // Send a lifetime message to the browser process. Virtual so that
57  // it can be overridden in unit tests.
58  //
59  // |track_id| is the ID of the track that just got connected or
60  // disconnected.
61  //
62  // |is_audio| is true for an audio track, false for a video track.
63  //
64  // |start_lifetime| is true to indicate that it just got connected,
65  // false to indicate it is no longer connected.
66  //
67  // |is_remote| is true for remote streams (received over a
68  // PeerConnection), false for local streams (sent over a
69  // PeerConnection).
70  virtual void SendLifetimeMessage(const std::string& track_id,
71                                   TrackType track_type,
72                                   LifetimeEvent lifetime_event,
73                                   StreamType stream_type);
74
75 protected:
76  // Calls SendLifetimeMessage for |observer| depending on |ice_state_|.
77  void SendLifeTimeMessageDependingOnIceState(
78      MediaStreamTrackMetricsObserver* observer);
79
80  // Implements MakeUniqueId. |pc_id| is a cast of this object's
81  // |this| pointer to a 64-bit integer, which is usable as a unique
82  // ID for the PeerConnection this object is attached to (since there
83  // is a one-to-one relationship).
84  uint64 MakeUniqueIdImpl(uint64 pc_id,
85                          const std::string& track,
86                          StreamType stream_type);
87
88 private:
89  // Make a unique ID for the given track, that is valid while the
90  // track object and the PeerConnection it is attached to both exist.
91  uint64 MakeUniqueId(const std::string& track, StreamType stream_type);
92
93  typedef ScopedVector<MediaStreamTrackMetricsObserver> ObserverVector;
94  ObserverVector observers_;
95
96  webrtc::PeerConnectionInterface::IceConnectionState ice_state_;
97};
98
99}  // namespace
100
101#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_MEDIA_STREAM_TRACK_METRICS_H_
102