protected override void OnDraw(Canvas canvas) { if (rtspCancel == null) { rtspClientStart(); } if (codec == null && (h264 || h265)) { codec = MediaCodec.CreateDecoderByType(h265 ? MediaFormat.MimetypeVideoHevc : MediaFormat.MimetypeVideoAvc); callbacks = new CallBacks(this); codec.SetCallback(callbacks); var mediafmt = MediaFormat.CreateVideoFormat(h265 ? MediaFormat.MimetypeVideoHevc : MediaFormat.MimetypeVideoAvc, 1920, 1080); codec.Configure(mediafmt, videoView.Holder.Surface, null, MediaCodecConfigFlags.None); codec.Start(); } base.OnDraw(canvas); }
private void initEncoder(string codec) { MediaFormat format = new MediaFormat(); switch (codec) { case "amrnb": audioEncoder = MediaCodec.CreateEncoderByType(MediaFormat.MimetypeAudioAmrNb); format.SetString(MediaFormat.KeyMime, MediaFormat.MimetypeAudioAmrNb); format.SetInteger(MediaFormat.KeySampleRate, 8000); format.SetInteger(MediaFormat.KeyBitRate, 7950); break; case "amrwb": audioEncoder = MediaCodec.CreateEncoderByType(MediaFormat.MimetypeAudioAmrWb); format.SetString(MediaFormat.KeyMime, MediaFormat.MimetypeAudioAmrWb); format.SetInteger(MediaFormat.KeySampleRate, 16000); format.SetInteger(MediaFormat.KeyBitRate, 18250); break; default: throw new Exception("Unknown recorder codec selected " + codec); } format.SetInteger(MediaFormat.KeyChannelCount, 1); format.SetInteger(MediaFormat.KeyMaxInputSize, bufferSize); audioEncoder.SetCallback(this); audioEncoder.Configure(format, null, null, MediaCodecConfigFlags.Encode); audioEncoder.Start(); }
public bool setup(DecoderCallback callback_obj, int width, int height) //format_hint is aviFileContent { callbackThread = new HandlerThread("H264DecoderHandler"); callbackThread.Start(); handler = new Handler(callbackThread.Looper); mDecoder = MediaCodec.CreateDecoderByType(MIME); mCallbackObj = callback_obj; myCallback = new MyCallback(mDecoder, mCallbackObj); mDecoder.SetCallback(myCallback, handler); //mOutputFormat = mDecoder.GetOutputFormat(); // option B inputFormat = MediaFormat.CreateVideoFormat(MIME, width, height); inputFormat.SetInteger(MediaFormat.KeyMaxInputSize, width * height); inputFormat.SetInteger("durationUs", 63446722); //inputFormat.SetInteger(MediaFormat.KeyColorFormat, (int)MediaCodecCapabilities.Formatyuv420semiplanar); //inputFormat.SetInteger(MediaFormat.KeyIFrameInterval, 60); try { mDecoder.Configure(inputFormat, null, null, 0 /* Decoder */); } catch (Exception ex) { throw ex; } Console.WriteLine("before mDecoder.Start()"); mDecoder.Start(); Console.WriteLine("after mDecoder.Start()"); return(true); }
public void SetupVideo(int width, int height, byte[] spsData, byte[] ppsData) { MediaFormat videoFormat = MediaFormat.CreateVideoFormat( mime: MediaFormat.MimetypeVideoAvc, width: width, height: height); /* * TODO: Use SPS / PPS * videoFormat.SetByteBuffer("csd-0", Java.Nio.ByteBuffer.Wrap(spsData)); * videoFormat.SetByteBuffer("csd-1", Java.Nio.ByteBuffer.Wrap(ppsData)); */ videoFormat.SetInteger(MediaFormat.KeyMaxInputSize, 100000); _videoCodec = MediaCodec.CreateDecoderByType( MediaFormat.MimetypeVideoAvc); _videoCodec.SetCallback(this); _videoCodec.Configure(format: videoFormat, surface: new Surface(_surface), crypto: null, flags: MediaCodecConfigFlags.None); _videoCodec.Start(); }
/// <summary> /// Initialize this instance. /// First, stop decoder and check if surface exists. /// Then configure MediaFormat and MediaCodec and start it. /// </summary> /// <returns>The initialize.</returns> public bool Initialize() { Initialized = false; if (!StopDecoder() || _surface == null) { return(Initialized); } _mediaFormat = GetMediaFormat(_mimeType, _videoWidth, _videoHeight); _mediaFormat.SetInteger(MediaFormat.KeyMaxWidth, _videoWidth); _mediaFormat.SetInteger(MediaFormat.KeyMaxHeight, _videoHeight); if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { _mediaFormat.SetInteger(MediaFormat.KeyOperatingRate, Short.MaxValue); } _mediaCodec.Configure( format: _mediaFormat, surface: _surface, crypto: null, flags: MediaCodecConfigFlags.None); _mediaCodec.SetVideoScalingMode(VideoScalingMode.ScaleToFit); _mediaCodec.SetCallback(this); _mediaCodec.Start(); Initialized = true; return(Initialized); }
public VideoStreamer(int width, int height) { MediaFormat videoFormat = MediaFormat.CreateVideoFormat(MediaFormat.MimetypeVideoAvc, width, height); MediaCodecList availableCodecs = new MediaCodecList(MediaCodecListKind.RegularCodecs); MediaCodec encoder = MediaCodec.CreateEncoderByType(MediaFormat.MimetypeVideoAvc); _backgroundThread = new HandlerThread("EncoderBackgroundWorker"); _backgroundThread.Start(); _backgroundHandler = new Handler(_backgroundThread.Looper); encoder.SetCallback(new EncoderCallback(), _backgroundHandler); encoder.Configure(videoFormat, null, null, MediaCodecConfigFlags.Encode); encoder.Start(); }
public void SetupAudio(int sampleRate, int channels, byte[] esdsData) { _audioTrack = new AudioTrack( new AudioAttributes.Builder() .SetUsage(AudioUsageKind.Media) .SetContentType(AudioContentType.Music) .SetFlags(AudioFlags.LowLatency) .Build(), new Android.Media.AudioFormat.Builder() .SetEncoding(Encoding.Pcm16bit) .SetSampleRate(44100) .SetChannelMask(ChannelOut.Stereo) .Build(), 4096, AudioTrackMode.Stream, AudioManager.AudioSessionIdGenerate); MediaFormat audioFormat = MediaFormat.CreateAudioFormat( mime: MediaFormat.MimetypeAudioAac, sampleRate: sampleRate, channelCount: channels); audioFormat.SetInteger(MediaFormat.KeyIsAdts, 0); audioFormat.SetInteger(MediaFormat.KeyAacProfile, (int)MediaCodecProfileType.Aacobjectlc); _audioCodec = MediaCodec.CreateDecoderByType( MediaFormat.MimetypeAudioAac); // TODO: Remove hardcoding byte profile = (byte)MediaCodecProfileType.Aacobjectlc; byte sampleIndex = AacAdtsAssembler.GetSamplingFrequencyIndex(sampleRate); byte[] csd0 = new byte[2]; csd0[0] = (byte)(((byte)profile << 3) | (sampleIndex >> 1)); csd0[1] = (byte)((byte)((sampleIndex << 7) & 0x80) | (channels << 3)); esdsData = csd0; audioFormat.SetByteBuffer("csd-0", Java.Nio.ByteBuffer.Wrap(esdsData)); _audioCodec.SetCallback(this); _audioCodec.Configure( format: audioFormat, surface: null, crypto: null, flags: MediaCodecConfigFlags.None); _audioCodec.Start(); _audioTrack.Play(); }
public void start() { if (running) { return; } running = true; lock (availableBuffers) { availableBuffers.Clear(); } audioEncoder = MediaCodec.CreateEncoderByType(encoderMimeType); audioEncoder.SetCallback(this); audioEncoder.Configure(mediaFormat, null, null, MediaCodecConfigFlags.Encode); audioEncoder.Start(); }
public void start() { if (running) { return; } running = true; delay = 5; lock (pendingFrames) { pendingFrames.Clear(); availableBuffers.Clear(); } audioDecoder = MediaCodec.CreateDecoderByType(decoderMimeType); audioDecoder.SetCallback(this); audioDecoder.Configure(mediaFormat, null, null, MediaCodecConfigFlags.None); audioDecoder.Start(); decodeThread = new Thread(decodeLoop); decodeThread.Start(); }