From 4343ce6058371d21527cc5aeee7015b67ef9ec09 Mon Sep 17 00:00:00 2001 From: Yu-Hsuan Lin Date: Sun, 30 Apr 2017 08:58:55 +0800 Subject: [PATCH] Rtmp extension --- extensions/rtmp/README.md | 34 +++++++++ extensions/rtmp/build.gradle | 29 ++++++++ extensions/rtmp/src/main/AndroidManifest.xml | 17 +++++ .../exoplayer2/ext/rtmp/RtmpDataSource.java | 70 +++++++++++++++++++ settings.gradle | 2 + 5 files changed, 152 insertions(+) create mode 100644 extensions/rtmp/README.md create mode 100644 extensions/rtmp/build.gradle create mode 100644 extensions/rtmp/src/main/AndroidManifest.xml create mode 100644 extensions/rtmp/src/main/java/com/google/android/exoplayer2/ext/rtmp/RtmpDataSource.java diff --git a/extensions/rtmp/README.md b/extensions/rtmp/README.md new file mode 100644 index 00000000000..cdd34f156b8 --- /dev/null +++ b/extensions/rtmp/README.md @@ -0,0 +1,34 @@ +# ExoPlayer RTMP Extension # + +## Description ## + +The RTMP Extension is an [DataSource][] implementation for playing [RTMP][] streaming using +[Librtmp Client for Android]. + +## Using the extension ## + +When building [MediaSource][], inject `RtmpDataSourceFactory` like this: + +```java +private MediaSource buildMediaSource(Uri uri, String overrideExtension) { + int type = TextUtils.isEmpty(overrideExtension) ? Util.inferContentType(uri) + : Util.inferContentType("." + overrideExtension); + switch (type) { + + // ... other types cases + + case C.TYPE_OTHER: + DataSource.Factory factory = uri.getScheme().equals("rtmp") ? new RtmpDataSourceFactory() : mediaDataSourceFactory; + return new ExtractorMediaSource(uri, factory, new DefaultExtractorsFactory(), mainHandler, eventLogger); + default: { + throw new IllegalStateException("Unsupported type: " + type); + } + } +} +``` + + +[DataSource]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/upstream/DataSource.html +[RTMP]: https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol +[Librtmp Client for Android]: https://github.com/ant-media/LibRtmp-Client-for-Android +[MediaSource]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/source/MediaSource.html diff --git a/extensions/rtmp/build.gradle b/extensions/rtmp/build.gradle new file mode 100644 index 00000000000..e0398255021 --- /dev/null +++ b/extensions/rtmp/build.gradle @@ -0,0 +1,29 @@ +// Copyright (C) 2016 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. +apply plugin: 'com.android.library' + +android { + compileSdkVersion project.ext.compileSdkVersion + buildToolsVersion project.ext.buildToolsVersion + + defaultConfig { + minSdkVersion 16 + targetSdkVersion project.ext.targetSdkVersion + } +} + +dependencies { + compile project(':library-core') + compile 'net.butterflytv.utils:rtmp-client:0.2.6.1' +} \ No newline at end of file diff --git a/extensions/rtmp/src/main/AndroidManifest.xml b/extensions/rtmp/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..272aa11a960 --- /dev/null +++ b/extensions/rtmp/src/main/AndroidManifest.xml @@ -0,0 +1,17 @@ + + + + diff --git a/extensions/rtmp/src/main/java/com/google/android/exoplayer2/ext/rtmp/RtmpDataSource.java b/extensions/rtmp/src/main/java/com/google/android/exoplayer2/ext/rtmp/RtmpDataSource.java new file mode 100644 index 00000000000..9b1e25c4000 --- /dev/null +++ b/extensions/rtmp/src/main/java/com/google/android/exoplayer2/ext/rtmp/RtmpDataSource.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2016 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 com.google.android.exoplayer2.ext.rtmp; + +import android.net.Uri; + +import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DataSpec; + +import net.butterflytv.rtmp_client.RtmpClient; + +import java.io.IOException; + +/** + * A Real-Time Messaging Protocol (RTMP) {@link DataSource}. + */ +public final class RtmpDataSource implements DataSource { + private final RtmpClient rtmpClient; + private Uri uri; + + public RtmpDataSource() { + rtmpClient = new RtmpClient(); + } + + @Override + public Uri getUri() { + return uri; + } + + @Override + public long open(DataSpec dataSpec) throws IOException { + uri = dataSpec.uri; + int result = rtmpClient.open(dataSpec.uri.toString(), false); + if (result < 0) { + return 0; + } + return C.LENGTH_UNSET; + } + + @Override + public void close() throws IOException { + rtmpClient.close(); + } + + @Override + public int read(byte[] buffer, int offset, int readLength) throws IOException { + return rtmpClient.read(buffer, offset, readLength); + } + + public final static class RtmpDataSourceFactory implements DataSource.Factory { + @Override + public DataSource createDataSource() { + return new RtmpDataSource(); + } + } +} diff --git a/settings.gradle b/settings.gradle index 544d2d4a216..339038292fc 100644 --- a/settings.gradle +++ b/settings.gradle @@ -26,6 +26,7 @@ include ':extension-gvr' include ':extension-okhttp' include ':extension-opus' include ':extension-vp9' +include ':extension-rtmp' // Uncomment the following line to use the Cronet Extension. // include ':extension-cronet' @@ -41,6 +42,7 @@ project(':extension-gvr').projectDir = new File(settingsDir, 'extensions/gvr') project(':extension-okhttp').projectDir = new File(settingsDir, 'extensions/okhttp') project(':extension-opus').projectDir = new File(settingsDir, 'extensions/opus') project(':extension-vp9').projectDir = new File(settingsDir, 'extensions/vp9') +project(':extension-rtmp').projectDir = new File(settingsDir, 'extensions/rtmp') // Uncomment the following line to use the Cronet Extension. // See extensions/cronet/README.md for details. // project(':extension-cronet').projectDir = new File(settingsDir, 'extensions/cronet')