summaryrefslogtreecommitdiffstats
path: root/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java
blob: 5b2f579868d6214174879f5f28314c6778568aad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mozilla.thirdparty.com.google.android.exoplayer2.offline;

import androidx.annotation.Nullable;
import org.mozilla.thirdparty.com.google.android.exoplayer2.C;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.DataSink;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.DataSource;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.DummyDataSource;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.FileDataSource;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.PriorityDataSourceFactory;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.cache.Cache;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.cache.CacheDataSink;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.cache.CacheDataSinkFactory;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.cache.CacheKeyFactory;
import org.mozilla.thirdparty.com.google.android.exoplayer2.upstream.cache.CacheUtil;
import org.mozilla.thirdparty.com.google.android.exoplayer2.util.PriorityTaskManager;

/** A helper class that holds necessary parameters for {@link Downloader} construction. */
public final class DownloaderConstructorHelper {

  private final Cache cache;
  @Nullable private final CacheKeyFactory cacheKeyFactory;
  @Nullable private final PriorityTaskManager priorityTaskManager;
  private final CacheDataSourceFactory onlineCacheDataSourceFactory;
  private final CacheDataSourceFactory offlineCacheDataSourceFactory;

  /**
   * @param cache Cache instance to be used to store downloaded data.
   * @param upstreamFactory A {@link DataSource.Factory} for creating {@link DataSource}s for
   *     downloading data.
   */
  public DownloaderConstructorHelper(Cache cache, DataSource.Factory upstreamFactory) {
    this(
        cache,
        upstreamFactory,
        /* cacheReadDataSourceFactory= */ null,
        /* cacheWriteDataSinkFactory= */ null,
        /* priorityTaskManager= */ null);
  }

  /**
   * @param cache Cache instance to be used to store downloaded data.
   * @param upstreamFactory A {@link DataSource.Factory} for creating {@link DataSource}s for
   *     downloading data.
   * @param cacheReadDataSourceFactory A {@link DataSource.Factory} for creating {@link DataSource}s
   *     for reading data from the cache. If null then a {@link FileDataSource.Factory} will be
   *     used.
   * @param cacheWriteDataSinkFactory A {@link DataSink.Factory} for creating {@link DataSource}s
   *     for writing data to the cache. If null then a {@link CacheDataSinkFactory} will be used.
   * @param priorityTaskManager A {@link PriorityTaskManager} to use when downloading. If non-null,
   *     downloaders will register as tasks with priority {@link C#PRIORITY_DOWNLOAD} whilst
   *     downloading.
   */
  public DownloaderConstructorHelper(
      Cache cache,
      DataSource.Factory upstreamFactory,
      @Nullable DataSource.Factory cacheReadDataSourceFactory,
      @Nullable DataSink.Factory cacheWriteDataSinkFactory,
      @Nullable PriorityTaskManager priorityTaskManager) {
    this(
        cache,
        upstreamFactory,
        cacheReadDataSourceFactory,
        cacheWriteDataSinkFactory,
        priorityTaskManager,
        /* cacheKeyFactory= */ null);
  }

  /**
   * @param cache Cache instance to be used to store downloaded data.
   * @param upstreamFactory A {@link DataSource.Factory} for creating {@link DataSource}s for
   *     downloading data.
   * @param cacheReadDataSourceFactory A {@link DataSource.Factory} for creating {@link DataSource}s
   *     for reading data from the cache. If null then a {@link FileDataSource.Factory} will be
   *     used.
   * @param cacheWriteDataSinkFactory A {@link DataSink.Factory} for creating {@link DataSource}s
   *     for writing data to the cache. If null then a {@link CacheDataSinkFactory} will be used.
   * @param priorityTaskManager A {@link PriorityTaskManager} to use when downloading. If non-null,
   *     downloaders will register as tasks with priority {@link C#PRIORITY_DOWNLOAD} whilst
   *     downloading.
   * @param cacheKeyFactory An optional factory for cache keys.
   */
  public DownloaderConstructorHelper(
      Cache cache,
      DataSource.Factory upstreamFactory,
      @Nullable DataSource.Factory cacheReadDataSourceFactory,
      @Nullable DataSink.Factory cacheWriteDataSinkFactory,
      @Nullable PriorityTaskManager priorityTaskManager,
      @Nullable CacheKeyFactory cacheKeyFactory) {
    if (priorityTaskManager != null) {
      upstreamFactory =
          new PriorityDataSourceFactory(upstreamFactory, priorityTaskManager, C.PRIORITY_DOWNLOAD);
    }
    DataSource.Factory readDataSourceFactory =
        cacheReadDataSourceFactory != null
            ? cacheReadDataSourceFactory
            : new FileDataSource.Factory();
    if (cacheWriteDataSinkFactory == null) {
      cacheWriteDataSinkFactory =
          new CacheDataSinkFactory(cache, CacheDataSink.DEFAULT_FRAGMENT_SIZE);
    }
    onlineCacheDataSourceFactory =
        new CacheDataSourceFactory(
            cache,
            upstreamFactory,
            readDataSourceFactory,
            cacheWriteDataSinkFactory,
            CacheDataSource.FLAG_BLOCK_ON_CACHE,
            /* eventListener= */ null,
            cacheKeyFactory);
    offlineCacheDataSourceFactory =
        new CacheDataSourceFactory(
            cache,
            DummyDataSource.FACTORY,
            readDataSourceFactory,
            null,
            CacheDataSource.FLAG_BLOCK_ON_CACHE,
            /* eventListener= */ null,
            cacheKeyFactory);
    this.cache = cache;
    this.priorityTaskManager = priorityTaskManager;
    this.cacheKeyFactory = cacheKeyFactory;
  }

  /** Returns the {@link Cache} instance. */
  public Cache getCache() {
    return cache;
  }

  /** Returns the {@link CacheKeyFactory}. */
  public CacheKeyFactory getCacheKeyFactory() {
    return cacheKeyFactory != null ? cacheKeyFactory : CacheUtil.DEFAULT_CACHE_KEY_FACTORY;
  }

  /** Returns a {@link PriorityTaskManager} instance. */
  public PriorityTaskManager getPriorityTaskManager() {
    // Return a dummy PriorityTaskManager if none is provided. Create a new PriorityTaskManager
    // each time so clients don't affect each other over the dummy PriorityTaskManager instance.
    return priorityTaskManager != null ? priorityTaskManager : new PriorityTaskManager();
  }

  /** Returns a new {@link CacheDataSource} instance. */
  public CacheDataSource createCacheDataSource() {
    return onlineCacheDataSourceFactory.createDataSource();
  }

  /**
   * Returns a new {@link CacheDataSource} instance which accesses cache read-only and throws an
   * exception on cache miss.
   */
  public CacheDataSource createOfflineCacheDataSource() {
    return offlineCacheDataSourceFactory.createDataSource();
  }
}