#ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_
#define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "content/common/content_export.h"
#include "content/public/renderer/render_view_observer.h"
#include "content/renderer/media/media_stream_client.h"
#include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
#include "content/renderer/media/media_stream_source.h"
#include "third_party/WebKit/public/platform/WebMediaStream.h"
#include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
#include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebUserMediaClient.h"
#include "third_party/WebKit/public/web/WebUserMediaRequest.h"
#include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
namespace content {
class MediaStreamAudioRenderer;
class MediaStreamDependencyFactory;
class MediaStreamDispatcher;
class MediaStreamVideoSource;
class VideoCapturerDelegate;
class WebRtcAudioRenderer;
class WebRtcLocalAudioRenderer;
class CONTENT_EXPORT MediaStreamImpl
: public RenderViewObserver,
NON_EXPORTED_BASE(public blink::WebUserMediaClient),
NON_EXPORTED_BASE(public MediaStreamClient),
public MediaStreamDispatcherEventHandler,
public base::SupportsWeakPtr<MediaStreamImpl>,
NON_EXPORTED_BASE(public base::NonThreadSafe) {
public:
MediaStreamImpl(
RenderView* render_view,
MediaStreamDispatcher* media_stream_dispatcher,
MediaStreamDependencyFactory* dependency_factory);
virtual ~MediaStreamImpl();
virtual void requestUserMedia(
const blink::WebUserMediaRequest& user_media_request) OVERRIDE;
virtual void cancelUserMediaRequest(
const blink::WebUserMediaRequest& user_media_request) OVERRIDE;
virtual bool IsMediaStream(const GURL& url) OVERRIDE;
virtual scoped_refptr<VideoFrameProvider> GetVideoFrameProvider(
const GURL& url,
const base::Closure& error_cb,
const VideoFrameProvider::RepaintCB& repaint_cb) OVERRIDE;
virtual scoped_refptr<MediaStreamAudioRenderer>
GetAudioRenderer(const GURL& url, int render_frame_id) OVERRIDE;
virtual void OnStreamGenerated(
int request_id,
const std::string& label,
const StreamDeviceInfoArray& audio_array,
const StreamDeviceInfoArray& video_array) OVERRIDE;
virtual void OnStreamGenerationFailed(
int request_id,
content::MediaStreamRequestResult result) OVERRIDE;
virtual void OnDeviceStopped(const std::string& label,
const StreamDeviceInfo& device_info) OVERRIDE;
virtual void OnDevicesEnumerated(
int request_id,
const StreamDeviceInfoArray& device_array) OVERRIDE;
virtual void OnDeviceOpened(
int request_id,
const std::string& label,
const StreamDeviceInfo& device_info) OVERRIDE;
virtual void OnDeviceOpenFailed(int request_id) OVERRIDE;
virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE;
virtual void FrameWillClose(blink::WebFrame* frame) OVERRIDE;
protected:
void OnLocalSourceStopped(const blink::WebMediaStreamSource& source);
void OnLocalMediaStreamStop(const std::string& label);
virtual void CompleteGetUserMediaRequest(
const blink::WebMediaStream& stream,
blink::WebUserMediaRequest* request_info,
content::MediaStreamRequestResult result);
virtual blink::WebMediaStream GetMediaStream(const GURL& url);
virtual MediaStreamVideoSource* CreateVideoSource(
const StreamDeviceInfo& device,
const MediaStreamSource::SourceStoppedCallback& stop_callback);
private:
class UserMediaRequestInfo
: public base::SupportsWeakPtr<UserMediaRequestInfo> {
public:
typedef base::Callback<void(UserMediaRequestInfo* request_info,
content::MediaStreamRequestResult result)>
ResourcesReady;
UserMediaRequestInfo(int request_id,
blink::WebFrame* frame,
const blink::WebUserMediaRequest& request,
bool enable_automatic_output_device_selection);
~UserMediaRequestInfo();
int request_id;
bool generated;
const bool enable_automatic_output_device_selection;
blink::WebFrame* frame;
blink::WebMediaStream web_stream;
blink::WebUserMediaRequest request;
void StartAudioTrack(const blink::WebMediaStreamTrack& track,
const blink::WebMediaConstraints& constraints);
blink::WebMediaStreamTrack CreateAndStartVideoTrack(
const blink::WebMediaStreamSource& source,
const blink::WebMediaConstraints& constraints,
MediaStreamDependencyFactory* factory);
void CallbackOnTracksStarted(const ResourcesReady& callback);
bool IsSourceUsed(const blink::WebMediaStreamSource& source) const;
void RemoveSource(const blink::WebMediaStreamSource& source);
bool AreAllSourcesRemoved() const { return sources_.empty(); };
private:
void OnTrackStarted(MediaStreamSource* source, bool success);
void CheckAllTracksStarted();
ResourcesReady ready_callback_;
bool request_failed_;
std::vector<blink::WebMediaStreamSource> sources_;
std::vector<MediaStreamSource*> sources_waiting_for_callback_;
};
typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests;
struct LocalStreamSource {
LocalStreamSource(blink::WebFrame* frame,
const blink::WebMediaStreamSource& source)
: frame(frame), source(source) {
}
blink::WebFrame* frame;
blink::WebMediaStreamSource source;
};
typedef std::vector<LocalStreamSource> LocalStreamSources;
void InitializeSourceObject(
const StreamDeviceInfo& device,
blink::WebMediaStreamSource::Type type,
const blink::WebMediaConstraints& constraints,
blink::WebFrame* frame,
blink::WebMediaStreamSource* webkit_source);
void CreateVideoTracks(
const StreamDeviceInfoArray& devices,
const blink::WebMediaConstraints& constraints,
blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks,
UserMediaRequestInfo* request);
void CreateAudioTracks(
const StreamDeviceInfoArray& devices,
const blink::WebMediaConstraints& constraints,
blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks,
UserMediaRequestInfo* request);
void OnCreateNativeTracksCompleted(
UserMediaRequestInfo* request,
content::MediaStreamRequestResult result);
UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id);
UserMediaRequestInfo* FindUserMediaRequestInfo(
const blink::WebUserMediaRequest& request);
UserMediaRequestInfo* FindUserMediaRequestInfo(const std::string& label);
void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request);
const blink::WebMediaStreamSource* FindLocalSource(
const StreamDeviceInfo& device) const;
bool IsSourceInRequests(const blink::WebMediaStreamSource& source) const;
void StopLocalSource(const blink::WebMediaStreamSource& source,
bool notify_dispatcher);
void StopUnreferencedSources(bool notify_dispatcher);
scoped_refptr<WebRtcAudioRenderer> CreateRemoteAudioRenderer(
webrtc::MediaStreamInterface* stream, int render_frame_id);
scoped_refptr<WebRtcLocalAudioRenderer> CreateLocalAudioRenderer(
const blink::WebMediaStreamTrack& audio_track,
int render_frame_id);
bool GetAuthorizedDeviceInfoForAudioRenderer(
int* session_id, int* output_sample_rate, int* output_buffer_size);
MediaStreamDependencyFactory* dependency_factory_;
MediaStreamDispatcher* media_stream_dispatcher_;
UserMediaRequests user_media_requests_;
LocalStreamSources local_sources_;
DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl);
};
}
#endif