• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_VIDEO_TRACK_ADAPTER_H_
6 #define CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_
7 
8 #include <vector>
9 
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "content/renderer/media/media_stream_video_track.h"
14 #include "media/base/video_frame.h"
15 
16 namespace content {
17 
18 // VideoTrackAdapter is a helper class used by MediaStreamVideoSource used for
19 // adapting the video resolution from a source implementation to the resolution
20 // a track requires. Different tracks can have different resolution constraints.
21 // The constraints can be set as max width and height as well as max and min
22 // aspect ratio.
23 // Video frames are delivered to a track using a VideoCaptureDeliverFrameCB on
24 // the IO-thread.
25 // Adaptations is done by wrapping the original media::VideoFrame in a new
26 // media::VideoFrame with a new visible_rect and natural_size.
27 class VideoTrackAdapter
28     : public base::RefCountedThreadSafe<VideoTrackAdapter> {
29  public:
30   typedef base::Callback<void(bool mute_state)> OnMutedCallback;
31 
32   explicit VideoTrackAdapter(
33       const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
34 
35   // Register |track| to receive video frames in |frame_callback| with
36   // a resolution within the boundaries of the arguments.
37   // Must be called on the main render thread. |frame_callback| is guaranteed to
38   // be released on the main render thread.
39   // |source_frame_rate| is used to calculate a prudent interval to check for
40   // passing frames and inform of the result via |on_muted_state_callback|.
41   void AddTrack(const MediaStreamVideoTrack* track,
42                 VideoCaptureDeliverFrameCB frame_callback,
43                 int max_width, int max_height,
44                 double min_aspect_ratio,
45                 double max_aspect_ratio,
46                 double max_frame_rate);
47   void RemoveTrack(const MediaStreamVideoTrack* track);
48 
49   // Delivers |frame| to all tracks that have registered a callback.
50   // Must be called on the IO-thread.
51   void DeliverFrameOnIO(
52       const scoped_refptr<media::VideoFrame>& frame,
53       const media::VideoCaptureFormat& format,
54       const base::TimeTicks& estimated_capture_time);
55 
io_message_loop()56   const scoped_refptr<base::MessageLoopProxy>& io_message_loop() {
57     DCHECK(thread_checker_.CalledOnValidThread());
58     return io_message_loop_;
59   }
60 
61   // Start monitor that frames are delivered to this object. I.E, that
62   // |DeliverFrameOnIO| is called with a frame rate of |source_frame_rate|.
63   // |on_muted_callback| is triggered on the main render thread.
64   void StartFrameMonitoring(double source_frame_rate,
65                             const OnMutedCallback& on_muted_callback);
66   void StopFrameMonitoring();
67 
68  private:
69   virtual ~VideoTrackAdapter();
70   friend class base::RefCountedThreadSafe<VideoTrackAdapter>;
71 
72   void AddTrackOnIO(
73       const MediaStreamVideoTrack* track,
74       VideoCaptureDeliverFrameCB frame_callback,
75       const gfx::Size& max_frame_size,
76       double min_aspect_ratio,
77       double max_aspect_ratio,
78       double max_frame_rate);
79   void RemoveTrackOnIO(const MediaStreamVideoTrack* track);
80 
81   void StartFrameMonitoringOnIO(
82     const OnMutedCallback& on_muted_state_callback,
83     double source_frame_rate);
84   void StopFrameMonitoringOnIO();
85 
86   // Compare |frame_counter_snapshot| with the current |frame_counter_|, and
87   // inform of the situation (muted, not muted) via |set_muted_state_callback|.
88   void CheckFramesReceivedOnIO(const OnMutedCallback& set_muted_state_callback,
89                                uint64 old_frame_counter_snapshot);
90 
91   // |thread_checker_| is bound to the main render thread.
92   base::ThreadChecker thread_checker_;
93 
94   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
95 
96   // |renderer_task_runner_| is used to ensure that
97   // VideoCaptureDeliverFrameCB is released on the main render thread.
98   scoped_refptr<base::SingleThreadTaskRunner> renderer_task_runner_;
99 
100   // VideoFrameResolutionAdapter is an inner class that is created on the main
101   // render thread but operates on the IO-thread. It does the resolution
102   // adaptation and delivers frames to all registered tracks on the IO-thread.
103   class VideoFrameResolutionAdapter;
104   typedef std::vector<scoped_refptr<VideoFrameResolutionAdapter> >
105       FrameAdapters;
106   FrameAdapters adapters_;
107 
108   // Set to true if frame monitoring has been started. It is only accessed on
109   // the IO-thread.
110   bool monitoring_frame_rate_;
111 
112   // Keeps track of it frames have been received. It is only accessed on the
113   // IO-thread.
114   bool muted_state_;
115 
116   // Running frame counter, accessed on the IO-thread.
117   uint64 frame_counter_;
118 
119   // Frame rate configured on the video source, accessed on the IO-thread.
120   float source_frame_rate_;
121 
122   DISALLOW_COPY_AND_ASSIGN(VideoTrackAdapter);
123 };
124 
125 }  // namespace content
126 
127 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_
128