Skip to content

Commit

Permalink
Merge pull request #2503 from b95505017/rtmp_client
Browse files Browse the repository at this point in the history
Rtmp client
  • Loading branch information
ojw28 authored Jul 5, 2017
2 parents a046633 + 0ecbe5d commit 1279b7d
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
34 changes: 34 additions & 0 deletions extensions/rtmp/README.md
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions extensions/rtmp/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
17 changes: 17 additions & 0 deletions extensions/rtmp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->

<manifest package="com.google.android.exoplayer2.ext.rtmp"/>
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 1279b7d

Please sign in to comment.