1 // Copyright (c) 2012 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_PEPPER_PEPPER_MEDIA_DEVICE_MANAGER_H_ 6 #define CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_DEVICE_MANAGER_H_ 7 8 #include <map> 9 10 #include "base/memory/weak_ptr.h" 11 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" 12 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h" 13 #include "content/public/renderer/render_view_observer_tracker.h" 14 #include "content/public/renderer/render_view_observer.h" 15 16 namespace content { 17 class RenderViewImpl; 18 19 class PepperMediaDeviceManager 20 : public MediaStreamDispatcherEventHandler, 21 public PepperDeviceEnumerationHostHelper::Delegate, 22 public RenderViewObserver, 23 public RenderViewObserverTracker<PepperMediaDeviceManager>, 24 public base::SupportsWeakPtr<PepperMediaDeviceManager> { 25 public: 26 static PepperMediaDeviceManager* GetForRenderView(RenderView* render_view); 27 virtual ~PepperMediaDeviceManager(); 28 29 // PepperDeviceEnumerationHostHelper::Delegate implementation: 30 virtual int EnumerateDevices( 31 PP_DeviceType_Dev type, 32 const EnumerateDevicesCallback& callback) OVERRIDE; 33 virtual void StopEnumerateDevices(int request_id) OVERRIDE; 34 35 typedef base::Callback<void (int /* request_id */, 36 bool /* succeeded */, 37 const std::string& /* label */)> 38 OpenDeviceCallback; 39 40 // Opens the specified device. The request ID passed into the callback will be 41 // the same as the return value. If successful, the label passed into the 42 // callback identifies a audio/video steam, which can be used to call 43 // CloseDevice() and GetSesssionID(). 44 int OpenDevice(PP_DeviceType_Dev type, 45 const std::string& device_id, 46 const GURL& document_url, 47 const OpenDeviceCallback& callback); 48 // Cancels an request to open device, using the request ID returned by 49 // OpenDevice(). It is guaranteed that the callback passed into OpenDevice() 50 // won't be called afterwards. 51 void CancelOpenDevice(int request_id); 52 void CloseDevice(const std::string& label); 53 // Gets audio/video session ID given a label. 54 int GetSessionID(PP_DeviceType_Dev type, const std::string& label); 55 56 // MediaStreamDispatcherEventHandler implementation. 57 virtual void OnStreamGenerated( 58 int request_id, 59 const std::string& label, 60 const StreamDeviceInfoArray& audio_device_array, 61 const StreamDeviceInfoArray& video_device_array) OVERRIDE; 62 virtual void OnStreamGenerationFailed(int request_id) OVERRIDE; 63 virtual void OnStopGeneratedStream(const std::string& label) OVERRIDE; 64 virtual void OnDevicesEnumerated( 65 int request_id, 66 const StreamDeviceInfoArray& device_array) OVERRIDE; 67 virtual void OnDevicesEnumerationFailed(int request_id) OVERRIDE; 68 virtual void OnDeviceOpened( 69 int request_id, 70 const std::string& label, 71 const StreamDeviceInfo& device_info) OVERRIDE; 72 virtual void OnDeviceOpenFailed(int request_id) OVERRIDE; 73 74 // Stream type conversion. 75 static MediaStreamType FromPepperDeviceType(PP_DeviceType_Dev type); 76 static PP_DeviceType_Dev FromMediaStreamType(MediaStreamType type); 77 78 private: 79 PepperMediaDeviceManager(RenderView* render_view); 80 81 void NotifyDevicesEnumerated( 82 int request_id, 83 bool succeeded, 84 const StreamDeviceInfoArray& device_array); 85 86 void NotifyDeviceOpened(int request_id, 87 bool succeeded, 88 const std::string& label); 89 90 RenderViewImpl* GetRenderViewImpl(); 91 92 int next_id_; 93 94 typedef std::map<int, EnumerateDevicesCallback> EnumerateCallbackMap; 95 EnumerateCallbackMap enumerate_callbacks_; 96 97 typedef std::map<int, OpenDeviceCallback> OpenCallbackMap; 98 OpenCallbackMap open_callbacks_; 99 100 DISALLOW_COPY_AND_ASSIGN(PepperMediaDeviceManager); 101 }; 102 103 } // namespace content 104 105 #endif // CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_DEVICE_MANAGER_H_ 106