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.
5 #ifndef CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_
6 #define CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_
13 #include "base/callback_forward.h"
14 #include "base/compiler_specific.h"
15 #include "base/lazy_instance.h"
16 #include "base/strings/string16.h"
17 #include "base/synchronization/lock.h"
18 #include "base/values.h"
19 #include "content/common/content_export.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "media/audio/audio_logging.h"
23 #include "media/base/media_log.h"
24 #include "media/capture/video/video_capture_device_info.h"
27 class AudioParameters
;
33 // This class stores information about currently active media.
34 class CONTENT_EXPORT MediaInternals
35 : NON_EXPORTED_BASE(public media::AudioLogFactory
),
36 public NotificationObserver
{
38 // Called with the update string.
39 typedef base::Callback
<void(const base::string16
&)> UpdateCallback
;
41 static MediaInternals
* GetInstance();
43 ~MediaInternals() override
;
45 // NotificationObserver implementation.
46 void Observe(int type
,
47 const NotificationSource
& source
,
48 const NotificationDetails
& details
) override
;
50 // Called when a MediaEvent occurs.
51 void OnMediaEvents(int render_process_id
,
52 const std::vector
<media::MediaLogEvent
>& events
);
54 // Add/remove update callbacks (see above). Must be called on the UI thread.
55 // The callbacks must also be fired on UI thread.
56 void AddUpdateCallback(const UpdateCallback
& callback
);
57 void RemoveUpdateCallback(const UpdateCallback
& callback
);
59 // Whether there are any update callbacks available. Can be called on any
63 // Replay all saved media events.
64 void SendHistoricalMediaEvents();
66 // Sends all audio cached data to each registered UpdateCallback.
67 void SendAudioStreamData();
69 // Sends all video capture capabilities cached data to each registered
71 void SendVideoCaptureDeviceCapabilities();
73 // Called to inform of the capabilities enumerated for video devices.
74 void UpdateVideoCaptureDeviceCapabilities(
75 const media::VideoCaptureDeviceInfos
& video_capture_device_infos
);
77 // AudioLogFactory implementation. Safe to call from any thread.
78 scoped_ptr
<media::AudioLog
> CreateAudioLog(AudioComponent component
) override
;
80 // If possible, i.e. a WebContents exists for the given RenderFrameHostID,
81 // tells an existing AudioLogEntry the WebContents title for easier
82 // differentiation on the UI.
83 void SetWebContentsTitleForAudioLogEntry(int component_id
,
84 int render_process_id
,
86 media::AudioLog
* audio_log
);
89 // Inner class to handle reporting pipelinestatus to UMA
90 class MediaInternalsUMAHandler
;
92 friend class AudioLogImpl
;
93 friend class MediaInternalsTest
;
94 friend struct base::DefaultLazyInstanceTraits
<MediaInternals
>;
96 // Pending events for a particular process.
97 using PendingEvents
= std::list
<media::MediaLogEvent
>;
99 // The maps between process ID and PendingEvents.
100 using PendingEventsMap
= std::map
<int, PendingEvents
>;
104 // Sends |update| to each registered UpdateCallback. Safe to call from any
105 // thread, but will forward to the IO thread.
106 void SendUpdate(const base::string16
& update
);
108 // Saves |event| so that it can be sent later in SendHistoricalMediaEvents().
109 void SaveEvent(int process_id
, const media::MediaLogEvent
& event
);
111 // Caches |value| under |cache_key| so that future UpdateAudioLog() calls
112 // will include the current data. Calls JavaScript |function|(|value|) for
113 // each registered UpdateCallback (if any).
114 enum AudioLogUpdateType
{
115 CREATE
, // Creates a new AudioLog cache entry.
116 UPDATE_IF_EXISTS
, // Updates an existing AudioLog cache entry, does
117 // nothing if it doesn't exist.
118 UPDATE_AND_DELETE
, // Deletes an existing AudioLog cache entry.
120 void UpdateAudioLog(AudioLogUpdateType type
,
121 const std::string
& cache_key
,
122 const std::string
& function
,
123 const base::DictionaryValue
* value
);
125 // Must only be accessed on the UI thread.
126 std::vector
<UpdateCallback
> update_callbacks_
;
127 PendingEventsMap pending_events_map_
;
129 // Must only be accessed on the IO thread.
130 base::ListValue video_capture_capabilities_cached_data_
;
132 NotificationRegistrar registrar_
;
134 // All variables below must be accessed under |lock_|.
137 base::DictionaryValue audio_streams_cached_data_
;
138 int owner_ids_
[AUDIO_COMPONENT_MAX
];
139 scoped_ptr
<MediaInternalsUMAHandler
> uma_handler_
;
141 DISALLOW_COPY_AND_ASSIGN(MediaInternals
);
144 } // namespace content
146 #endif // CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_