• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.media.metrics;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import com.android.internal.util.AnnotationValidations;
23 
24 import java.util.Objects;
25 
26 /**
27  * An instance of this class represents a session of media playback used to report playback
28  * metrics and events.
29  *
30  * Create a new instance using {@link MediaMetricsManager#createPlaybackSession}.
31  */
32 public final class PlaybackSession implements AutoCloseable {
33     private final @NonNull String mId;
34     private final @NonNull MediaMetricsManager mManager;
35     private final @NonNull LogSessionId mLogSessionId;
36     private boolean mClosed = false;
37 
38     /**
39      * Creates a new PlaybackSession.
40      *
41      * @hide
42      */
PlaybackSession(@onNull String id, @NonNull MediaMetricsManager manager)43     public PlaybackSession(@NonNull String id, @NonNull MediaMetricsManager manager) {
44         mId = id;
45         mManager = manager;
46         AnnotationValidations.validate(NonNull.class, null, mId);
47         AnnotationValidations.validate(NonNull.class, null, mManager);
48         mLogSessionId = new LogSessionId(mId);
49     }
50 
51     /**
52      * Reports playback metrics.
53      */
reportPlaybackMetrics(@onNull PlaybackMetrics metrics)54     public void reportPlaybackMetrics(@NonNull PlaybackMetrics metrics) {
55         mManager.reportPlaybackMetrics(mId, metrics);
56     }
57 
58     /**
59      * Reports error event.
60      */
reportPlaybackErrorEvent(@onNull PlaybackErrorEvent event)61     public void reportPlaybackErrorEvent(@NonNull PlaybackErrorEvent event) {
62         mManager.reportPlaybackErrorEvent(mId, event);
63     }
64 
65     /**
66      * Reports network event.
67      */
reportNetworkEvent(@onNull NetworkEvent event)68     public void reportNetworkEvent(@NonNull NetworkEvent event) {
69         mManager.reportNetworkEvent(mId, event);
70     }
71 
72     /**
73      * Reports playback state event.
74      */
reportPlaybackStateEvent(@onNull PlaybackStateEvent event)75     public void reportPlaybackStateEvent(@NonNull PlaybackStateEvent event) {
76         mManager.reportPlaybackStateEvent(mId, event);
77     }
78 
79     /**
80      * Reports track change event.
81      */
reportTrackChangeEvent(@onNull TrackChangeEvent event)82     public void reportTrackChangeEvent(@NonNull TrackChangeEvent event) {
83         mManager.reportTrackChangeEvent(mId, event);
84     }
85 
86     /**
87      * A session ID is used to identify a unique playback and to tie together lower-level
88      * playback components.
89      *
90      * Associate this session with a {@link MediaCodec} by passing the ID into
91      * {@link MediaFormat} through {@link MediaFormat#LOG_SESSION_ID} when
92      * creating the {@link MediaCodec}.
93      *
94      * Associate this session with an {@link AudioTrack} by calling
95      * {@link AudioTrack#setLogSessionId}.
96      *
97      * Associate this session with {@link MediaDrm} and {@link MediaCrypto} by calling
98      * {@link MediaDrm#getPlaybackComponent} and then calling
99      * {@link PlaybackComponent#setLogSessionId}.
100      */
getSessionId()101     public @NonNull LogSessionId getSessionId() {
102         return mLogSessionId;
103     }
104 
105     @Override
equals(@ullable Object o)106     public boolean equals(@Nullable Object o) {
107         if (this == o) return true;
108         if (o == null || getClass() != o.getClass()) return false;
109         PlaybackSession that = (PlaybackSession) o;
110         return Objects.equals(mId, that.mId);
111     }
112 
113     @Override
hashCode()114     public int hashCode() {
115         return Objects.hash(mId);
116     }
117 
118     @Override
close()119     public void close() {
120         mClosed = true;
121         mManager.releaseSessionId(mLogSessionId.getStringId());
122     }
123 }
124