| Index: content/browser/media/session/media_session.h
|
| diff --git a/content/browser/media/android/media_session.h b/content/browser/media/session/media_session.h
|
| similarity index 53%
|
| rename from content/browser/media/android/media_session.h
|
| rename to content/browser/media/session/media_session.h
|
| index 12711245bfbf4bdc3c70d347f6f1ef5fdae5df8e..10961a8f24d41709b8ace7df0655d01fff12aabc 100644
|
| --- a/content/browser/media/android/media_session.h
|
| +++ b/content/browser/media/session/media_session.h
|
| @@ -2,16 +2,14 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#ifndef CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_
|
| -#define CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_
|
| +#ifndef CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
|
| +#define CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
|
|
|
| -#include <jni.h>
|
| #include <stddef.h>
|
|
|
| -#include "base/android/scoped_java_ref.h"
|
| #include "base/id_map.h"
|
| #include "base/macros.h"
|
| -#include "content/browser/media/android/media_session_uma_helper.h"
|
| +#include "content/browser/media/session/media_session_uma_helper.h"
|
| #include "content/common/content_export.h"
|
| #include "content/public/browser/web_contents_observer.h"
|
| #include "content/public/browser/web_contents_user_data.h"
|
| @@ -20,102 +18,111 @@ class MediaSessionBrowserTest;
|
|
|
| namespace content {
|
|
|
| +class MediaSessionDelegate;
|
| class MediaSessionObserver;
|
|
|
| -// MediaSession manages the Android AudioFocus for a given WebContents. It is
|
| -// requesting the audio focus, pausing when requested by the system and dropping
|
| -// it on demand.
|
| +// MediaSession manages the media session and audio focus for a given
|
| +// WebContents. It is requesting the audio focus, pausing when requested by the
|
| +// system and dropping it on demand.
|
| // The audio focus can be of two types: Transient or Content. A Transient audio
|
| // focus will allow other players to duck instead of pausing and will be
|
| // declared as temporary to the system. A Content audio focus will not be
|
| // declared as temporary and will not allow other players to duck. If a given
|
| // WebContents can only have one audio focus at a time, it will be Content in
|
| // case of Transient and Content audio focus are both requested.
|
| -// Android system interaction occurs in the Java counterpart to this class.
|
| -class CONTENT_EXPORT MediaSession
|
| - : public WebContentsObserver,
|
| - protected WebContentsUserData<MediaSession> {
|
| +// TODO(thakis,mlamouri): MediaSession isn't CONTENT_EXPORT'd because it creates
|
| +// complicated build issues with WebContentsUserData being a non-exported
|
| +// template, see htttps://crbug.com/589840. As a result, the class uses
|
| +// CONTENT_EXPORT for methods that are being used from tests. CONTENT_EXPORT
|
| +// should be moved back to the class when the Windows build will work with it.
|
| +class MediaSession : public WebContentsObserver,
|
| + protected WebContentsUserData<MediaSession> {
|
| public:
|
| enum class Type {
|
| Content,
|
| Transient
|
| };
|
|
|
| - static bool RegisterMediaSession(JNIEnv* env);
|
| + enum class SuspendType {
|
| + // Suspended by the system because a transient sound needs to be played.
|
| + SYSTEM,
|
| + // Suspended by the UI.
|
| + UI,
|
| + // Suspended by the page via script or user interaction.
|
| + CONTENT,
|
| + };
|
|
|
| // Returns the MediaSession associated to this WebContents. Creates one if
|
| // none is currently available.
|
| - static MediaSession* Get(WebContents* web_contents);
|
| + CONTENT_EXPORT static MediaSession* Get(WebContents* web_contents);
|
|
|
| ~MediaSession() override;
|
|
|
| // Adds the given player to the current media session. Returns whether the
|
| // player was successfully added. If it returns false, AddPlayer() should be
|
| // called again later.
|
| - bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type);
|
| + CONTENT_EXPORT bool AddPlayer(MediaSessionObserver* observer,
|
| + int player_id, Type type);
|
|
|
| // Removes the given player from the current media session. Abandons audio
|
| // focus if that was the last player in the session.
|
| - void RemovePlayer(MediaSessionObserver* observer, int player_id);
|
| + CONTENT_EXPORT void RemovePlayer(MediaSessionObserver* observer,
|
| + int player_id);
|
|
|
| // Removes all the players associated with |observer|. Abandons audio focus if
|
| // these were the last players in the session.
|
| - void RemovePlayers(MediaSessionObserver* observer);
|
| -
|
| - // Called when the Android system requests the MediaSession to be suspended.
|
| - // Called by Java through JNI.
|
| - void OnSuspend(JNIEnv* env,
|
| - const base::android::JavaParamRef<jobject>& obj,
|
| - jboolean temporary);
|
| + CONTENT_EXPORT void RemovePlayers(MediaSessionObserver* observer);
|
|
|
| - // Called when the Android system requests the MediaSession to duck.
|
| - // Called by Java through JNI.
|
| - void OnSetVolumeMultiplier(JNIEnv* env, jobject obj,
|
| - jdouble volume_multiplier);
|
| -
|
| - // Called when the Android system requests the MediaSession to be resumed.
|
| - // Called by Java through JNI.
|
| - void OnResume(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
|
| -
|
| - // Called when the Android system requests the MediaSession to duck.
|
| - // Called by Java through JNI.
|
| - void RecordSessionDuck(JNIEnv* env,
|
| - const base::android::JavaParamRef<jobject> &obj);
|
| + // Record that the session was ducked.
|
| + void RecordSessionDuck();
|
|
|
| // Called when a player is paused in the content.
|
| // If the paused player is the last player, we suspend the MediaSession.
|
| // Otherwise, the paused player will be removed from the MediaSession.
|
| - void OnPlayerPaused(MediaSessionObserver* observer, int player_id);
|
| + CONTENT_EXPORT void OnPlayerPaused(MediaSessionObserver* observer,
|
| + int player_id);
|
| +
|
| + // Resume the media session.
|
| + // |type| represents the origin of the request.
|
| + CONTENT_EXPORT void Resume(SuspendType type);
|
|
|
| - // Called when the user requests resuming the session. No-op if the session is
|
| - // not controllable.
|
| - void Resume();
|
| + // Suspend the media session.
|
| + // |type| represents the origin of the request.
|
| + CONTENT_EXPORT void Suspend(SuspendType type);
|
|
|
| - // Called when the user requests suspending the session. No-op if the session
|
| - // is not controllable.
|
| - void Suspend();
|
| + // Stop the media session.
|
| + // |type| represents the origin of the request.
|
| + CONTENT_EXPORT void Stop(SuspendType type);
|
|
|
| - // Called when the user requests stopping the session.
|
| - void Stop();
|
| + // Change the volume multiplier of the session to |volume_multiplier|.
|
| + CONTENT_EXPORT void SetVolumeMultiplier(double volume_multiplier);
|
|
|
| // Returns if the session can be controlled by Resume() and Suspend calls
|
| // above.
|
| - bool IsControllable() const;
|
| + CONTENT_EXPORT bool IsControllable() const;
|
| +
|
| + // Returns if the session is currently active.
|
| + CONTENT_EXPORT bool IsActive() const;
|
|
|
| // Returns if the session is currently suspended.
|
| - bool IsSuspended() const;
|
| + // TODO(mlamouri): IsSuspended() is basically !IsActive() but in order to not
|
| + // have a ridiculously huge refactoring, the change to fix that will happen in
|
| + // a follow-up.
|
| + bool IsReallySuspended() const;
|
| +
|
| + // Returns if the session is currently suspended or inactive.
|
| + CONTENT_EXPORT bool IsSuspended() const;
|
|
|
| private:
|
| friend class content::WebContentsUserData<MediaSession>;
|
| friend class ::MediaSessionBrowserTest;
|
|
|
| - // Resets the |j_media_session_| ref to prevent calling the Java backend
|
| - // during content_browsertests.
|
| - void ResetJavaRefForTest();
|
| - bool IsActiveForTest() const;
|
| - Type audio_focus_type_for_test() const;
|
| - void RemoveAllPlayersForTest();
|
| - MediaSessionUmaHelper* uma_helper_for_test();
|
| + CONTENT_EXPORT void SetDelegateForTests(
|
| + scoped_ptr<MediaSessionDelegate> delegate);
|
| + CONTENT_EXPORT bool IsActiveForTest() const;
|
| + CONTENT_EXPORT Type audio_focus_type_for_test() const;
|
| + CONTENT_EXPORT void RemoveAllPlayersForTest();
|
| + CONTENT_EXPORT MediaSessionUmaHelper* uma_helper_for_test();
|
|
|
| enum class State {
|
| ACTIVE,
|
| @@ -123,15 +130,6 @@ class CONTENT_EXPORT MediaSession
|
| INACTIVE
|
| };
|
|
|
| - enum class SuspendType {
|
| - // Suspended by the system because a transient sound needs to be played.
|
| - SYSTEM,
|
| - // Suspended by the UI.
|
| - UI,
|
| - // Suspended by the page via script or user interaction.
|
| - CONTENT,
|
| - };
|
| -
|
| // Representation of a player for the MediaSession.
|
| struct PlayerIdentifier {
|
| PlayerIdentifier(MediaSessionObserver* observer, int player_id);
|
| @@ -150,22 +148,19 @@ class CONTENT_EXPORT MediaSession
|
| };
|
| using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
|
|
|
| - explicit MediaSession(WebContents* web_contents);
|
| + CONTENT_EXPORT explicit MediaSession(WebContents* web_contents);
|
|
|
| - // Setup the JNI.
|
| void Initialize();
|
|
|
| - void OnSuspendInternal(SuspendType type, State new_state);
|
| - void OnResumeInternal(SuspendType type);
|
| - void OnSetVolumeMultiplierInternal(double volume_multiplier);
|
| + CONTENT_EXPORT void OnSuspendInternal(SuspendType type, State new_state);
|
| + CONTENT_EXPORT void OnResumeInternal(SuspendType type);
|
|
|
| - // Requests audio focus to Android using |j_media_session_|.
|
| - // Returns whether the request was granted. If |j_media_session_| is null, it
|
| - // will always return true.
|
| + // Requests audio focus to the MediaSessionDelegate.
|
| + // Returns whether the request was granted.
|
| bool RequestSystemAudioFocus(Type type);
|
|
|
| - // To be called after a call to AbandonAudioFocus() in order to call the Java
|
| - // MediaSession if the audio focus really need to be abandoned.
|
| + // To be called after a call to AbandonAudioFocus() in order request the
|
| + // delegate to abandon the audio focus.
|
| void AbandonSystemAudioFocusIfNeeded();
|
|
|
| // Notifies WebContents about the state change of the media session.
|
| @@ -175,7 +170,7 @@ class CONTENT_EXPORT MediaSession
|
| // It sets audio_focus_state_ and notifies observers about the state change.
|
| void SetAudioFocusState(State audio_focus_state);
|
|
|
| - base::android::ScopedJavaGlobalRef<jobject> j_media_session_;
|
| + scoped_ptr<MediaSessionDelegate> delegate_;
|
| PlayersMap players_;
|
|
|
| State audio_focus_state_;
|
| @@ -193,4 +188,4 @@ class CONTENT_EXPORT MediaSession
|
|
|
| } // namespace content
|
|
|
| -#endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_
|
| +#endif // CONTENT_BROWSER_MEDIA_SESSION_MEDIA_SESSION_H_
|
|
|