1 // Copyright (c) 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 CONTENT_RENDERER_MEDIA_VIDEO_SOURCE_HANDLER_H_ 6 #define CONTENT_RENDERER_MEDIA_VIDEO_SOURCE_HANDLER_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/compiler_specific.h" 12 #include "base/gtest_prod_util.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/threading/thread_checker.h" 16 #include "content/common/content_export.h" 17 #include "media/base/video_frame.h" 18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" 19 20 namespace content { 21 22 class MediaStreamRegistryInterface; 23 class MediaStreamVideoSink; 24 class PpFrameReceiver; 25 26 // Interface used by the effects pepper plugin to get captured frame 27 // from the video track. 28 class CONTENT_EXPORT FrameReaderInterface { 29 public: 30 // Got a new captured frame. 31 virtual bool GotFrame(const scoped_refptr<media::VideoFrame>& frame) = 0; 32 33 protected: ~FrameReaderInterface()34 virtual ~FrameReaderInterface() {} 35 }; 36 37 // VideoSourceHandler is a glue class between MediaStreamVideoTrack and 38 // the effects pepper plugin host. 39 class CONTENT_EXPORT VideoSourceHandler { 40 public: 41 // |registry| is used to look up the media stream by url. If a NULL |registry| 42 // is given, the global blink::WebMediaStreamRegistry will be used. 43 explicit VideoSourceHandler(MediaStreamRegistryInterface* registry); 44 virtual ~VideoSourceHandler(); 45 // Connects to the first video track in the MediaStream specified by |url| and 46 // the received frames will be delivered via |reader|. 47 // Returns true on success and false on failure. 48 bool Open(const std::string& url, FrameReaderInterface* reader); 49 // Closes |reader|'s connection with the video track, i.e. stops receiving 50 // frames from the video track. 51 // Returns true on success and false on failure. 52 bool Close(FrameReaderInterface* reader); 53 54 private: 55 FRIEND_TEST_ALL_PREFIXES(VideoSourceHandlerTest, OpenClose); 56 57 struct SourceInfo { 58 SourceInfo(const blink::WebMediaStreamTrack& blink_track, 59 FrameReaderInterface* reader); 60 ~SourceInfo(); 61 62 scoped_ptr<PpFrameReceiver> receiver_; 63 }; 64 65 typedef std::map<FrameReaderInterface*, SourceInfo*> SourceInfoMap; 66 67 // Deliver VideoFrame to the MediaStreamVideoSink associated with 68 // |reader|. For testing only. 69 void DeliverFrameForTesting(FrameReaderInterface* reader, 70 const scoped_refptr<media::VideoFrame>& frame); 71 72 blink::WebMediaStreamTrack GetFirstVideoTrack(const std::string& url); 73 74 MediaStreamRegistryInterface* registry_; 75 SourceInfoMap reader_to_receiver_; 76 77 base::ThreadChecker thread_checker_; 78 79 DISALLOW_COPY_AND_ASSIGN(VideoSourceHandler); 80 }; 81 82 } // namespace content 83 84 #endif // CONTENT_RENDERER_MEDIA_VIDEO_SOURCE_HANDLER_H_ 85